Skip to content

Instantly share code, notes, and snippets.

@rsmudge
Created March 21, 2018 02:06
Show Gist options
  • Save rsmudge/87ce80cd8d8d185c5870d559af2dc0c2 to your computer and use it in GitHub Desktop.
Save rsmudge/87ce80cd8d8d185c5870d559af2dc0c2 to your computer and use it in GitHub Desktop.
How to add a popup handler to a Swing component in Aggressor Script/Sleep
# demonstrate how to add a popup handler to a Swing component in Sleep
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
# safely add a listener to show a popup
sub setupPopupMenu {
# we're using fork({}) to run this in a separate Aggressor Script environment.
# This reduces deadlock potential due to Sleep's global interpreter lock
#
# this especially matters as our mouse listener will be fired for *everything*
# to include mouse movements.
fork({
[$component addMouseListener: lambda({
if ([$1 isPopupTrigger]) {
show_popup($1, $name, $component);
}
}, \$component, \$name)];
}, $component => $1, $name => $2);
}
popup mickey_menu {
item "Hello World" {
[$1 setText: "Hey, you clicked me"];
}
}
command mickey {
local('$component');
$component = [new JLabel: "Hello World"];
addTab("Mouse Test", $component, "...");
setupPopupMenu($component, "mickey_menu");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment