Skip to content

Commit

Permalink
feat: animate the context menu when showing
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 19, 2023
1 parent b767c59 commit b37b200
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/main/java/viewtify/ui/UIContextMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public UIMenuItem menu(Variable text) {
public void menu(Object text, Consumer<UIContextMenu> sub) {
Menu menu = assignID(new Menu(String.valueOf(text)));
menu.getProperties().put(id, null);
menu.setOnShowing(e -> {
System.out.println("SHOW " + e);
});
menuProvider.get().add(menu);

sub.accept(new UIContextMenu(text, menu::getItems));
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/viewtify/ui/anime/Anime.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.util.Duration;

import kiss.Disposable;
import kiss.WiseRunnable;
import viewtify.Viewtify;
Expand Down Expand Up @@ -251,6 +252,17 @@ public final Anime move(UserInterfaceProvider<? extends Node> node, double x, do
return moveX(node, x).moveY(node, y);
}

/**
* Animate location.
*
* @param node
* @param x
* @return
*/
public final Anime moveX(Node node, double x) {
return effect(node.translateYProperty(), x);
}

/**
* Animate location.
*
Expand All @@ -259,7 +271,18 @@ public final Anime move(UserInterfaceProvider<? extends Node> node, double x, do
* @return
*/
public final Anime moveX(UserInterfaceProvider<? extends Node> node, double x) {
return effect(node.ui().translateXProperty(), x);
return moveX(node.ui(), x);
}

/**
* Animate location.
*
* @param node
* @param y
* @return
*/
public final Anime moveY(Node node, double y) {
return effect(node.translateYProperty(), y);
}

/**
Expand All @@ -270,7 +293,7 @@ public final Anime moveX(UserInterfaceProvider<? extends Node> node, double x) {
* @return
*/
public final Anime moveY(UserInterfaceProvider<? extends Node> node, double y) {
return effect(node.ui().translateYProperty(), y);
return moveY(node.ui(), y);
}

/**
Expand Down
66 changes: 48 additions & 18 deletions src/main/java/viewtify/ui/helper/ContextMenuHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

import kiss.Variable;
import viewtify.ui.UIContextMenu;
import viewtify.ui.UserInterfaceProvider;

Expand Down Expand Up @@ -177,7 +182,6 @@ private Self showContextOn(Node anchor, Side location, double dx, double dy) {
EnhancedContextMenu context = context();
if (context.canShow()) {
context.show(anchor, location, dx, dy);
context.assign(anchor);
}
return (Self) this;
}
Expand Down Expand Up @@ -233,38 +237,49 @@ default boolean isContextShowing() {
}

/**
* Change the control of showing and hiding the context menu from right-click to left-click.
* Change the control of showing and hiding the context menu from right-click to left-click and
* ignore native context requesting.
*
* @return
*/
default Self behaveLikeButton() {
if (this instanceof UserActionHelper action) {
behaveLikeButton(action);
} else if (this instanceof EventTarget target) {
behaveLikeButton(UserActionHelper.of(target));
} else if (this instanceof UserInterfaceProvider provider && provider.ui() instanceof EventTarget target) {
behaveLikeButton(UserActionHelper.of(target));
}
target(this).to(x -> x.addEventHandler(MouseEvent.MOUSE_PRESSED, e -> {
if (e.getButton() == MouseButton.PRIMARY) {
toggleContext();
}
}));
return disableNativeContextRequest();
}

/**
* Enable the context menu.
*
* @return
*/
default Self enableContext() {
context().disable = false;

return (Self) this;
}

/**
* Change the control of showing and hiding the context menu from right-click to left-click.
* Enable the context menu.
*
* @param action
* @return
*/
private void behaveLikeButton(UserActionHelper<?> action) {
// action.when(User.ContextMenu.preliminarily(), ContextMenuEvent::consume);
action.when(User.LeftPress, ContextMenuHelper.this::toggleContext);
default Self disableContext() {
context().disable = true;

return (Self) this;
}

/**
* Enable the context menu.
*
* @return
*/
default Self enableContext() {
context().disable = false;
default Self enableNativeContextRequest() {
target(this).to(x -> x.removeEventFilter(ContextMenuEvent.CONTEXT_MENU_REQUESTED, EnhancedContextMenu.NOOP));

return (Self) this;
}
Expand All @@ -274,9 +289,24 @@ default Self enableContext() {
*
* @return
*/
default Self disableContext() {
context().disable = true;
default Self disableNativeContextRequest() {
target(this).to(x -> x.addEventFilter(ContextMenuEvent.CONTEXT_MENU_REQUESTED, EnhancedContextMenu.NOOP));

return (Self) this;
}

/**
* Wrap by {@link UserActionHelper}.
*
* @param o
* @return
*/
private Variable<EventTarget> target(Object o) {
if (o instanceof EventTarget target) {
return Variable.of(target);
} else if (o instanceof UserInterfaceProvider provider && provider.ui() instanceof EventTarget target) {
return Variable.of(target);
}
return Variable.empty();
}
}
25 changes: 15 additions & 10 deletions src/main/java/viewtify/ui/helper/EnhancedContextMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@
import javafx.scene.input.MouseEvent;
import javafx.stage.WindowEvent;

final class EnhancedContextMenu extends ContextMenu implements EventHandler<ContextMenuEvent> {
import viewtify.ui.anime.Anime;

final class EnhancedContextMenu extends ContextMenu {

/** The reusable event consumer. */
static final EventHandler<ContextMenuEvent> NOOP = ContextMenuEvent::consume;

/** The disable state. */
boolean disable;

/** The last hidden time. */
long lastHidden;

/**
*
*/
EnhancedContextMenu() {
addEventHandler(WindowEvent.WINDOW_HIDDEN, e -> lastHidden = System.currentTimeMillis());

addEventHandler(WindowEvent.WINDOW_SHOWING, new EventHandler<>() {

@Override
Expand Down Expand Up @@ -81,15 +88,13 @@ boolean canShow() {
* {@inheritDoc}
*/
@Override
public void handle(ContextMenuEvent event) {
System.out.println("STOP");
event.consume();
}
protected void show() {
super.show();

/**
* @param anchor
*/
void assign(Node anchor) {
anchor.addEventFilter(ContextMenuEvent.CONTEXT_MENU_REQUESTED, this);
Node node = getStyleableNode();
node.setTranslateY(-10);
node.setOpacity(0);

Anime.define().opacity(node, 1).moveY(node, 0).run();
}
}
2 changes: 1 addition & 1 deletion src/main/java/viewtify/ui/helper/UserActionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private <E extends Event> Self when(Signal<User<E>> actionTypes, WiseBiConsumer<
}

/**
* Create temporary {@link UserActionHelper}.
* Wrap by {@link UserActionHelper}.
*
* @param eventTarget
* @return
Expand Down

0 comments on commit b37b200

Please sign in to comment.