From 7581cf0b454c6b27777a00608a1ee31661f3fab8 Mon Sep 17 00:00:00 2001 From: Konrad Neitzel Date: Sat, 14 Nov 2026 09:16:19 +0100 Subject: [PATCH] Route native macOS Quit through the tray confirmation lifecycle --- fx/docs/tray-support.md | 10 ++++++ .../neitzel/fx/tray/MacApplicationQuit.java | 34 +++++++++++++++++++ .../java/de/neitzel/fx/tray/TraySupport.java | 3 ++ .../de/neitzel/fx/tray/TraySupportTest.java | 11 ++++-- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 fx/src/main/java/de/neitzel/fx/tray/MacApplicationQuit.java diff --git a/fx/docs/tray-support.md b/fx/docs/tray-support.md index a9b0226..2870aa9 100644 --- a/fx/docs/tray-support.md +++ b/fx/docs/tray-support.md @@ -13,6 +13,16 @@ the registration is removed and `quit` runs (typically Platform::exit). Applicat own Save/Discard/Cancel prompts. `close()` removes handlers/icon and restores the previous implicit-exit setting; call it during application shutdown as well. +On macOS, native application Quit requests (including Dock/menu Quit) use this +same confirmation path, including when the window is hidden or the tray is +unavailable. JavaFX normally translates these requests into window-close events; +AWT Desktop quit handlers do not intercept them. `MacApplicationQuit` isolates +the JavaFX 21 Glass hook, delegates other application events to the previous +handler, and restores it on close. One primary-window attachment owns this hook. +This uses an internal JavaFX API and needs rechecking when upgrading JavaFX. +Classpath applications need no extra flags. When JavaFX is on the module path, +add `--add-exports=javafx.graphics/com.sun.glass.ui=ALL-UNNAMED` at runtime. + Unsupported, failed or still-pending tray installation never hides the only window. Window close restores it instead. Applications must retain an accessible explicit Quit action for that fallback. Native callbacks are marshalled onto FX. An install diff --git a/fx/src/main/java/de/neitzel/fx/tray/MacApplicationQuit.java b/fx/src/main/java/de/neitzel/fx/tray/MacApplicationQuit.java new file mode 100644 index 0000000..7f633b9 --- /dev/null +++ b/fx/src/main/java/de/neitzel/fx/tray/MacApplicationQuit.java @@ -0,0 +1,34 @@ +package de.neitzel.fx.tray; + +import com.sun.glass.ui.Application; + +/** Isolates the JavaFX 21 native Quit hook, which has no public JavaFX equivalent. */ +final class MacApplicationQuit { + private MacApplicationQuit() {} + + static Runnable install(Runnable quit) { + if (!System.getProperty("os.name", "").startsWith("Mac")) return () -> {}; + Application app = Application.GetApplication(); + Application.EventHandler previous = app.getEventHandler(); + Application.EventHandler handler = new Application.EventHandler() { + @Override public void handleQuitAction(Application a, long time) { quit.run(); } + @Override public void handleWillFinishLaunchingAction(Application a, long t) { if (previous != null) previous.handleWillFinishLaunchingAction(a,t); } + @Override public void handleDidFinishLaunchingAction(Application a, long t) { if (previous != null) previous.handleDidFinishLaunchingAction(a,t); } + @Override public void handleWillBecomeActiveAction(Application a, long t) { if (previous != null) previous.handleWillBecomeActiveAction(a,t); } + @Override public void handleDidBecomeActiveAction(Application a, long t) { if (previous != null) previous.handleDidBecomeActiveAction(a,t); } + @Override public void handleWillResignActiveAction(Application a, long t) { if (previous != null) previous.handleWillResignActiveAction(a,t); } + @Override public void handleDidResignActiveAction(Application a, long t) { if (previous != null) previous.handleDidResignActiveAction(a,t); } + @Override public void handleDidReceiveMemoryWarning(Application a, long t) { if (previous != null) previous.handleDidReceiveMemoryWarning(a,t); } + @Override public void handleWillHideAction(Application a, long t) { if (previous != null) previous.handleWillHideAction(a,t); } + @Override public void handleDidHideAction(Application a, long t) { if (previous != null) previous.handleDidHideAction(a,t); } + @Override public void handleWillUnhideAction(Application a, long t) { if (previous != null) previous.handleWillUnhideAction(a,t); } + @Override public void handleDidUnhideAction(Application a, long t) { if (previous != null) previous.handleDidUnhideAction(a,t); } + @Override public void handleOpenFilesAction(Application a, long t, String[] files) { if (previous != null) previous.handleOpenFilesAction(a,t,files); } + @Override public boolean handleThemeChanged(String theme) { return previous != null && previous.handleThemeChanged(theme); } + }; + app.setEventHandler(handler); + return () -> { + if (app.getEventHandler() == handler) app.setEventHandler(previous); + }; + } +} diff --git a/fx/src/main/java/de/neitzel/fx/tray/TraySupport.java b/fx/src/main/java/de/neitzel/fx/tray/TraySupport.java index e209bd2..de9b253 100644 --- a/fx/src/main/java/de/neitzel/fx/tray/TraySupport.java +++ b/fx/src/main/java/de/neitzel/fx/tray/TraySupport.java @@ -38,6 +38,7 @@ public final class TraySupport implements AutoCloseable { private volatile boolean closed; private boolean quitting; private Registration registration; + private final Runnable removeNativeQuit; private final Object lifecycleLock = new Object(); /** Attaches asynchronously to the platform tray. Call on the JavaFX application thread. */ @@ -56,6 +57,7 @@ public final class TraySupport implements AutoCloseable { this.mayQuit = Objects.requireNonNull(mayQuit); this.quit = Objects.requireNonNull(quit); previousImplicitExit = Platform.isImplicitExit(); + removeNativeQuit = MacApplicationQuit.install(() -> onFx(this::requestQuit)); closeHandler = event -> { event.consume(); if (available.get()) stage.hide(); else open(); @@ -105,6 +107,7 @@ public final class TraySupport implements AutoCloseable { closed = true; if (registration != null) registration.close(); } + removeNativeQuit.run(); stage.removeEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, closeHandler); available.set(false); Platform.setImplicitExit(previousImplicitExit); diff --git a/fx/src/test/java/de/neitzel/fx/tray/TraySupportTest.java b/fx/src/test/java/de/neitzel/fx/tray/TraySupportTest.java index bf2d18c..9a2c70d 100644 --- a/fx/src/test/java/de/neitzel/fx/tray/TraySupportTest.java +++ b/fx/src/test/java/de/neitzel/fx/tray/TraySupportTest.java @@ -33,8 +33,15 @@ class TraySupportTest { stage.fireEvent(new WindowEvent(stage,WindowEvent.WINDOW_CLOSE_REQUEST)); assertFalse(stage.isShowing()); assertFalse(exited.get()); open.get().run(); assertTrue(stage.isShowing()); - quit.get().run(); assertFalse(exited.get()); - allowed.set(true); quit.get().run(); assertTrue(exited.get()); + Runnable nativeQuit = System.getProperty("os.name", "").startsWith("Mac") + ? () -> { + var app = com.sun.glass.ui.Application.GetApplication(); + app.getEventHandler().handleQuitAction(app, System.nanoTime()); + } : quit.get(); + // Native Quit must use the same confirmation as the tray, even hidden. + stage.hide(); nativeQuit.run(); + assertTrue(stage.isShowing()); assertFalse(exited.get()); + allowed.set(true); nativeQuit.run(); assertTrue(exited.get()); tray.close(); assertTrue(removed.get()); var unavailable=TraySupport.attach(stage,new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB),"test",() -> true,() -> {}, (icon,label,onOpen,onQuit) -> CompletableFuture.completedFuture(null));