Route native macOS Quit through the tray confirmation lifecycle
This commit is contained in:
@@ -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 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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user