|
| 1 | +/* |
| 2 | + * Hello Minecraft! Launcher |
| 3 | + * Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.jackhuang.hmcl.ui; |
| 19 | + |
| 20 | +import com.jfoenix.controls.JFXDialog; |
| 21 | +import javafx.application.Platform; |
| 22 | +import javafx.beans.value.ChangeListener; |
| 23 | +import javafx.beans.value.ObservableValue; |
| 24 | +import javafx.event.EventHandler; |
| 25 | +import javafx.scene.Node; |
| 26 | +import javafx.scene.layout.StackPane; |
| 27 | +import org.jackhuang.hmcl.ui.animation.AnimationUtils; |
| 28 | +import org.jackhuang.hmcl.ui.construct.DialogAware; |
| 29 | +import org.jackhuang.hmcl.ui.construct.DialogCloseEvent; |
| 30 | +import org.jackhuang.hmcl.ui.construct.JFXDialogPane; |
| 31 | +import org.jackhuang.hmcl.ui.decorator.Decorator; |
| 32 | +import org.jetbrains.annotations.Nullable; |
| 33 | + |
| 34 | +import java.util.Optional; |
| 35 | +import java.util.function.Consumer; |
| 36 | + |
| 37 | +public final class DialogUtils { |
| 38 | + private DialogUtils() { |
| 39 | + } |
| 40 | + |
| 41 | + public static final String PROPERTY_DIALOG_INSTANCE = DialogUtils.class.getName() + ".dialog.instance"; |
| 42 | + public static final String PROPERTY_DIALOG_PANE_INSTANCE = DialogUtils.class.getName() + ".dialog.pane.instance"; |
| 43 | + public static final String PROPERTY_DIALOG_CLOSE_HANDLER = DialogUtils.class.getName() + ".dialog.closeListener"; |
| 44 | + |
| 45 | + public static final String PROPERTY_PARENT_PANE_REF = DialogUtils.class.getName() + ".dialog.parentPaneRef"; |
| 46 | + public static final String PROPERTY_PARENT_DIALOG_REF = DialogUtils.class.getName() + ".dialog.parentDialogRef"; |
| 47 | + |
| 48 | + public static void show(Decorator decorator, Node content) { |
| 49 | + if (decorator.getDrawerWrapper() == null) { |
| 50 | + Platform.runLater(() -> show(decorator, content)); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + show(decorator.getDrawerWrapper(), content, (dialog) -> { |
| 55 | + JFXDialogPane pane = (JFXDialogPane) dialog.getContent(); |
| 56 | + decorator.capableDraggingWindow(dialog); |
| 57 | + decorator.forbidDraggingWindow(pane); |
| 58 | + dialog.setDialogContainer(decorator.getDrawerWrapper()); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + public static void show(StackPane container, Node content) { |
| 63 | + show(container, content, null); |
| 64 | + } |
| 65 | + |
| 66 | + public static void show(StackPane container, Node content, @Nullable Consumer<JFXDialog> onDialogCreated) { |
| 67 | + FXUtils.checkFxUserThread(); |
| 68 | + |
| 69 | + JFXDialog dialog = (JFXDialog) container.getProperties().get(PROPERTY_DIALOG_INSTANCE); |
| 70 | + JFXDialogPane dialogPane = (JFXDialogPane) container.getProperties().get(PROPERTY_DIALOG_PANE_INSTANCE); |
| 71 | + |
| 72 | + if (dialog == null) { |
| 73 | + dialog = new JFXDialog(AnimationUtils.isAnimationEnabled() |
| 74 | + ? JFXDialog.DialogTransition.CENTER |
| 75 | + : JFXDialog.DialogTransition.NONE); |
| 76 | + dialogPane = new JFXDialogPane(); |
| 77 | + |
| 78 | + dialog.setContent(dialogPane); |
| 79 | + dialog.setDialogContainer(container); |
| 80 | + dialog.setOverlayClose(false); |
| 81 | + |
| 82 | + container.getProperties().put(PROPERTY_DIALOG_INSTANCE, dialog); |
| 83 | + container.getProperties().put(PROPERTY_DIALOG_PANE_INSTANCE, dialogPane); |
| 84 | + |
| 85 | + if (onDialogCreated != null) { |
| 86 | + onDialogCreated.accept(dialog); |
| 87 | + } |
| 88 | + |
| 89 | + dialog.show(); |
| 90 | + } |
| 91 | + |
| 92 | + content.getProperties().put(PROPERTY_PARENT_PANE_REF, dialogPane); |
| 93 | + content.getProperties().put(PROPERTY_PARENT_DIALOG_REF, dialog); |
| 94 | + |
| 95 | + dialogPane.push(content); |
| 96 | + |
| 97 | + EventHandler<DialogCloseEvent> handler = event -> close(content); |
| 98 | + content.getProperties().put(PROPERTY_DIALOG_CLOSE_HANDLER, handler); |
| 99 | + content.addEventHandler(DialogCloseEvent.CLOSE, handler); |
| 100 | + |
| 101 | + handleDialogShown(dialog, content); |
| 102 | + } |
| 103 | + |
| 104 | + private static void handleDialogShown(JFXDialog dialog, Node node) { |
| 105 | + if (dialog.isVisible()) { |
| 106 | + dialog.requestFocus(); |
| 107 | + if (node instanceof DialogAware dialogAware) |
| 108 | + dialogAware.onDialogShown(); |
| 109 | + } else { |
| 110 | + dialog.visibleProperty().addListener(new ChangeListener<>() { |
| 111 | + @Override |
| 112 | + public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { |
| 113 | + if (newValue) { |
| 114 | + dialog.requestFocus(); |
| 115 | + if (node instanceof DialogAware dialogAware) |
| 116 | + dialogAware.onDialogShown(); |
| 117 | + observable.removeListener(this); |
| 118 | + } |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @SuppressWarnings("unchecked") |
| 125 | + public static void close(Node content) { |
| 126 | + FXUtils.checkFxUserThread(); |
| 127 | + |
| 128 | + Optional.ofNullable(content.getProperties().get(PROPERTY_DIALOG_CLOSE_HANDLER)) |
| 129 | + .ifPresent(handler -> content.removeEventHandler(DialogCloseEvent.CLOSE, (EventHandler<DialogCloseEvent>) handler)); |
| 130 | + |
| 131 | + JFXDialogPane pane = (JFXDialogPane) content.getProperties().get(PROPERTY_PARENT_PANE_REF); |
| 132 | + JFXDialog dialog = (JFXDialog) content.getProperties().get(PROPERTY_PARENT_DIALOG_REF); |
| 133 | + |
| 134 | + if (dialog != null && pane != null) { |
| 135 | + if (pane.size() == 1 && pane.peek().orElse(null) == content) { |
| 136 | + dialog.setOnDialogClosed(e -> pane.pop(content)); |
| 137 | + dialog.close(); |
| 138 | + |
| 139 | + StackPane container = dialog.getDialogContainer(); |
| 140 | + if (container != null) { |
| 141 | + container.getProperties().remove(PROPERTY_DIALOG_INSTANCE); |
| 142 | + container.getProperties().remove(PROPERTY_DIALOG_PANE_INSTANCE); |
| 143 | + container.getProperties().remove(PROPERTY_PARENT_DIALOG_REF); |
| 144 | + container.getProperties().remove(PROPERTY_PARENT_PANE_REF); |
| 145 | + } |
| 146 | + } else { |
| 147 | + pane.pop(content); |
| 148 | + } |
| 149 | + |
| 150 | + if (content instanceof DialogAware dialogAware) { |
| 151 | + dialogAware.onDialogClosed(); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments