Skip to content

Commit

Permalink
Make dialog window use an owner window
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Nov 30, 2024
1 parent c5e0c2e commit 142f149
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ui/src/main/java/io/xeres/ui/support/util/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.xeres.ui.custom.DisclosedHyperlink;
import io.xeres.ui.support.clipboard.ClipboardUtils;
import io.xeres.ui.support.uri.UriService;
import io.xeres.ui.support.window.WindowManager;
import javafx.application.Platform;
import javafx.css.PseudoClass;
import javafx.event.ActionEvent;
Expand Down Expand Up @@ -153,6 +154,14 @@ private static Alert buildAlert(AlertType alertType, String title, String messag
var alert = new Alert(alertType);
var stage = (Stage) alert.getDialogPane().getScene().getWindow();

// Try to intelligently set the owner window to indicate to the
// user that there's some action needed if he clicks it
var defaultOwnerWindow = WindowManager.getDefaultOwnerWindow();
if (defaultOwnerWindow != null)
{
alert.initOwner(defaultOwnerWindow);
}

UiUtils.setDefaultIcon(stage); // required for the window's title bar icon
UiUtils.setDefaultStyle(stage.getScene()); // required for the default styles being applied
// Setting dark borders doesn't work because dialogs aren't in JavaFX' built-in windows list
Expand Down
14 changes: 13 additions & 1 deletion ui/src/main/java/io/xeres/ui/support/window/WindowManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class WindowManager
private static AppThemeManager appThemeManager;

private static WindowBorder windowBorder;
private Window rootWindow;
private static Window rootWindow;

private String fullTitle;

Expand Down Expand Up @@ -512,6 +512,18 @@ public void calculateWindowDecorationSizes(Stage stage)
windowBorder = UiBorders.calculateWindowDecorationSizes(stage);
}

/**
* Gets the default owner window. Usually the last focus window otherwise the main window.
*
* @return the default owner window, can be null
*/
public static Window getDefaultOwnerWindow()
{
return Window.getWindows().stream()
.filter(Window::isFocused)
.findFirst().orElse(rootWindow);
}

static Optional<Window> getOpenedWindow(Class<? extends WindowController> controllerClass)
{
return Window.getWindows().stream()
Expand Down

0 comments on commit 142f149

Please sign in to comment.