Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,39 @@

import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXPopup;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.KeyValue;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.transform.Scale;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
import org.jackhuang.hmcl.game.GameInstanceID;
import org.jackhuang.hmcl.game.GameInstanceManifest;
import org.jackhuang.hmcl.game.HMCLGameRepository;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.ui.construct.ImageContainer;
import org.jackhuang.hmcl.ui.construct.RipplerContainer;
import org.jackhuang.hmcl.ui.construct.TwoLineListItem;
Expand All @@ -46,20 +60,68 @@
import java.util.List;

import static org.jackhuang.hmcl.util.i18n.I18n.i18n;

import static org.jackhuang.hmcl.ui.FXUtils.SINE;
/// @author Glavo
public final class GameListPopupMenu extends StackPane {

private static final String KEY = GameListPopupMenu.class.getName() + ".popup";
private static final String HIDING_KEY = GameListPopupMenu.class.getName() + ".hiding";

public static boolean hideShowing(Node owner) {
JFXPopup popup = (JFXPopup) owner.getProperties().get(KEY);
if (popup != null && popup.isShowing()) {
public static void hideAnimated(JFXPopup popup) {
if (popup == null || !popup.isShowing()) {
return;
}

if (Boolean.TRUE.equals(popup.getProperties().put(HIDING_KEY, true))) {
return;
}

if (!AnimationUtils.isAnimationEnabled()) {
popup.hide();
return;
}

Node content = popup.getPopupContent();
if (content == null) {
popup.hide();
return true;
} else {
return;
}

Node container = content.getParent() != null ? content.getParent() : content;
Bounds bounds = container.getLayoutBounds();

Scale scaleTransform = new Scale(1.0, 1.0, bounds.getWidth(), bounds.getHeight());
container.getTransforms().setAll(scaleTransform);

Timeline closeAnimation = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(container.opacityProperty(), 1.0, SINE),
new KeyValue(scaleTransform.xProperty(), 1.0, SINE),
new KeyValue(scaleTransform.yProperty(), 1.0, SINE)
),
new KeyFrame(Duration.millis(160),
new KeyValue(container.opacityProperty(), 0.0, SINE),
new KeyValue(scaleTransform.xProperty(), 0.0, SINE),
new KeyValue(scaleTransform.yProperty(), 0.0, SINE)
)
);

closeAnimation.setOnFinished(event -> {
popup.hide();
container.getTransforms().clear();
container.setOpacity(1.0);
});

FXUtils.playAnimation(container, "popup-close", closeAnimation);
}

public static boolean hideShowing(Node owner) {
if (!(owner.getProperties().get(KEY) instanceof JFXPopup popup && popup.isShowing())) {
return false;
}

hideAnimated(popup);
return true;
}

/// Shows an instance selection popup relative to its owner.
Expand All @@ -80,9 +142,68 @@ public static JFXPopup showAndGetPopup(Node owner, JFXPopup.PopupVPosition vAlig
.toList());
JFXPopup popup = new JFXPopup(menu);
owner.getProperties().put(KEY, popup);
popup.addEventFilter(WindowEvent.WINDOW_HIDDEN, event -> owner.getProperties().remove(KEY, popup));
popup.show(owner, vAlign, hAlign, initOffsetX, initOffsetY, true);
popup.setAutoHide(false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Route Escape through the animated dismissal

When a keyboard user presses Escape, JFXPopup.initialize() still leaves hideOnEscape enabled, so JavaFX calls popup.hide() directly and bypasses hideAnimated. Thus this standard dismissal path remains instantaneous despite the new closing animation; disable native Escape hiding and handle Escape by invoking the animated helper.

Useful? React with 👍 / 👎.

popup.setHideOnEscape(false);
Scene scene = owner.getScene();
EventHandler<MouseEvent> outsideClickHandler = event -> {
if (popup.isShowing()) {
Bounds ownerBounds = owner.localToScreen(owner.getBoundsInLocal());
if (ownerBounds != null && ownerBounds.contains(event.getScreenX(), event.getScreenY())) {
return;
}

Bounds popupBounds = menu.localToScreen(menu.getBoundsInLocal());
if (popupBounds != null && !popupBounds.contains(event.getScreenX(), event.getScreenY())) {
hideAnimated(popup);
}
}
};

EventHandler<ScrollEvent> outsideScrollHandler = event -> {
if (popup.isShowing()) {
Bounds popupBounds = menu.localToScreen(menu.getBoundsInLocal());
if (popupBounds != null && !popupBounds.contains(event.getScreenX(), event.getScreenY())) {
hideAnimated(popup);
Comment on lines +164 to +166

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exclude the toggle button from outside-click dismissal

When animations are disabled, clicking MainPage's menu button while the popup is open reaches this MOUSE_PRESSED filter before the button's action handler. hideAnimated then hides synchronously, WINDOW_HIDDEN removes KEY, and MainPage.java:262-266 sees no showing popup and immediately opens a new one, so mouse users with animations disabled cannot close the menu via its toggle button. Exclude presses on the owner from this outside-click path or retain the dismissal state until the action runs.

Useful? React with 👍 / 👎.

}
}
};

EventHandler<KeyEvent> escHandler = event -> {
if (event.getCode() == KeyCode.ESCAPE && popup.isShowing()) {
event.consume();
hideAnimated(popup);
}
};

ChangeListener<Boolean> focusListener = (obs, wasFocused, isFocused) -> {
if (!isFocused && popup.isShowing()) {
hideAnimated(popup);
}
};

if (scene != null) {
scene.addEventFilter(MouseEvent.MOUSE_PRESSED, outsideClickHandler);
Comment on lines +184 to +185

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve dismissal when scrolling outside the popup

With autoHide disabled, the replacement outside-dismissal logic listens only for MOUSE_PRESSED. JavaFX's native popup auto-hide also handles outside ScrollEvent.SCROLL, so scrolling the owner scene now moves the underlying UI while leaving the game-list popup open; register the equivalent scroll filter and route it through hideAnimated as well.

Useful? React with 👍 / 👎.

scene.addEventFilter(ScrollEvent.SCROLL, outsideScrollHandler);
scene.addEventFilter(KeyEvent.KEY_PRESSED, escHandler);
if (scene.getWindow() != null) {
scene.getWindow().focusedProperty().addListener(focusListener);
}
}
popup.focusedProperty().addListener(focusListener);

popup.addEventFilter(WindowEvent.WINDOW_HIDDEN, event -> {
owner.getProperties().remove(KEY, popup);
if (scene != null) {
scene.removeEventFilter(MouseEvent.MOUSE_PRESSED, outsideClickHandler);
scene.removeEventFilter(ScrollEvent.SCROLL, outsideScrollHandler);
scene.removeEventFilter(KeyEvent.KEY_PRESSED, escHandler);
if (scene.getWindow() != null) {
scene.getWindow().focusedProperty().removeListener(focusListener);
}
}
popup.focusedProperty().removeListener(focusListener);
});
popup.show(owner, vAlign, hAlign, initOffsetX, initOffsetY, false);
return popup;
}

Expand Down Expand Up @@ -155,7 +276,7 @@ public Cell(ListView<GameItem> listView) {
if (item != null) {
item.getRepository().setSelectedInstance(new GameInstanceID(item.getId()));
if (getScene().getWindow() instanceof JFXPopup popup)
popup.hide();
hideAnimated(popup);
}
});

Expand Down