Skip to content

Commit 92dc6ff

Browse files
committed
Fixes : #1205
Snippet99 requires modification as Command+Q is not implemented. Currently, the snippet demonstrates a message box pop-up when attempting to close the dialog window, but pressing Command+Q closes the window without triggering this behavior. The snippet has been updated to implement Command+Q functionality and to add an SWT dispose listener to the display object.
1 parent 02e20d6 commit 92dc6ff

File tree

1 file changed

+20
-7
lines changed
  • examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets

1 file changed

+20
-7
lines changed

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet99.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,26 @@ public static void main (String [] args) {
3030
Display display = new Display ();
3131
final Shell shell = new Shell (display);
3232
shell.setText("Snippet 99");
33-
shell.addListener (SWT.Close, event -> {
34-
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
35-
messageBox.setText("Information");
36-
messageBox.setMessage("Close the shell?");
37-
messageBox.setButtonLabels(Map.of(SWT.YES, "Close"));
38-
event.doit = messageBox.open () == SWT.YES;
39-
});
33+
// Listener for when the shell close button is clicked
34+
shell.addListener (SWT.Close, event -> {
35+
if (!shell.isDisposed()) {
36+
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
37+
messageBox.setText("Information");
38+
messageBox.setMessage("Close the shell?");
39+
messageBox.setButtonLabels(Map.of(SWT.YES, "Close"));
40+
event.doit = messageBox.open () == SWT.YES;
41+
}
42+
});
43+
// Dispose listener to implement Cmd+Q (on macOS)
44+
display.addListener(SWT.Dispose, event -> {
45+
if (!shell.isDisposed()) {
46+
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
47+
messageBox.setText("Information");
48+
messageBox.setMessage("Close the application?");
49+
messageBox.setButtonLabels(Map.of(SWT.YES, "Close"));
50+
event.doit = messageBox.open() == SWT.YES;
51+
}
52+
});
4053
shell.pack ();
4154
shell.open();
4255
while (!shell.isDisposed ()) {

0 commit comments

Comments
 (0)