Route native macOS Quit through the tray confirmation lifecycle
This commit is contained in:
@@ -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
|
own Save/Discard/Cancel prompts. `close()` removes handlers/icon and restores the
|
||||||
previous implicit-exit setting; call it during application shutdown as well.
|
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.
|
Unsupported, failed or still-pending tray installation never hides the only window.
|
||||||
Window close restores it instead. Applications must retain an accessible explicit
|
Window close restores it instead. Applications must retain an accessible explicit
|
||||||
Quit action for that fallback. Native callbacks are marshalled onto FX. An install
|
Quit action for that fallback. Native callbacks are marshalled onto FX. An install
|
||||||
|
|||||||
@@ -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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,6 +38,7 @@ public final class TraySupport implements AutoCloseable {
|
|||||||
private volatile boolean closed;
|
private volatile boolean closed;
|
||||||
private boolean quitting;
|
private boolean quitting;
|
||||||
private Registration registration;
|
private Registration registration;
|
||||||
|
private final Runnable removeNativeQuit;
|
||||||
private final Object lifecycleLock = new Object();
|
private final Object lifecycleLock = new Object();
|
||||||
|
|
||||||
/** Attaches asynchronously to the platform tray. Call on the JavaFX application thread. */
|
/** 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.mayQuit = Objects.requireNonNull(mayQuit);
|
||||||
this.quit = Objects.requireNonNull(quit);
|
this.quit = Objects.requireNonNull(quit);
|
||||||
previousImplicitExit = Platform.isImplicitExit();
|
previousImplicitExit = Platform.isImplicitExit();
|
||||||
|
removeNativeQuit = MacApplicationQuit.install(() -> onFx(this::requestQuit));
|
||||||
closeHandler = event -> {
|
closeHandler = event -> {
|
||||||
event.consume();
|
event.consume();
|
||||||
if (available.get()) stage.hide(); else open();
|
if (available.get()) stage.hide(); else open();
|
||||||
@@ -105,6 +107,7 @@ public final class TraySupport implements AutoCloseable {
|
|||||||
closed = true;
|
closed = true;
|
||||||
if (registration != null) registration.close();
|
if (registration != null) registration.close();
|
||||||
}
|
}
|
||||||
|
removeNativeQuit.run();
|
||||||
stage.removeEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, closeHandler);
|
stage.removeEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, closeHandler);
|
||||||
available.set(false);
|
available.set(false);
|
||||||
Platform.setImplicitExit(previousImplicitExit);
|
Platform.setImplicitExit(previousImplicitExit);
|
||||||
|
|||||||
@@ -33,8 +33,15 @@ class TraySupportTest {
|
|||||||
stage.fireEvent(new WindowEvent(stage,WindowEvent.WINDOW_CLOSE_REQUEST));
|
stage.fireEvent(new WindowEvent(stage,WindowEvent.WINDOW_CLOSE_REQUEST));
|
||||||
assertFalse(stage.isShowing()); assertFalse(exited.get());
|
assertFalse(stage.isShowing()); assertFalse(exited.get());
|
||||||
open.get().run(); assertTrue(stage.isShowing());
|
open.get().run(); assertTrue(stage.isShowing());
|
||||||
quit.get().run(); assertFalse(exited.get());
|
Runnable nativeQuit = System.getProperty("os.name", "").startsWith("Mac")
|
||||||
allowed.set(true); quit.get().run(); assertTrue(exited.get());
|
? () -> {
|
||||||
|
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());
|
tray.close(); assertTrue(removed.get());
|
||||||
var unavailable=TraySupport.attach(stage,new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB),"test",() -> true,() -> {},
|
var unavailable=TraySupport.attach(stage,new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB),"test",() -> true,() -> {},
|
||||||
(icon,label,onOpen,onQuit) -> CompletableFuture.completedFuture(null));
|
(icon,label,onOpen,onQuit) -> CompletableFuture.completedFuture(null));
|
||||||
|
|||||||
Reference in New Issue
Block a user