Skip to content

Commit

Permalink
feat: add ContextMenuHelper#hookContext
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Feb 13, 2024
1 parent a6c6a2d commit b8783c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/main/java/viewtify/ui/dock/TabArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import javafx.scene.control.skin.TabPaneSkin;
import javafx.scene.input.DragEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.WindowEvent;

import kiss.I;
import kiss.WiseConsumer;
Expand Down Expand Up @@ -381,9 +380,9 @@ void registerMenu(WiseConsumer<UILabel> menuBuilder) {
node.registerIcon(label -> {
menuBuilder.accept(label);

label.when(WindowEvent.WINDOW_SHOWING, () -> {
label.hookContext(() -> {
DockSystem.latestMenuActivatedTabArea = this;
}).when(WindowEvent.WINDOW_HIDDEN, () -> {
}, () -> {
DockSystem.latestMenuActivatedTabArea = null;
});
});
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/viewtify/ui/helper/ContextMenuHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.EventTarget;
import javafx.event.EventType;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
Expand Down Expand Up @@ -370,17 +369,16 @@ private Variable<EventTarget> target(Object o) {
}

/**
* Register window event.
* Hook context event.
*
* @param type
* @param action
* @return
*/
default Self when(EventType<WindowEvent> type, WiseRunnable action) {
if (action != null) {
context().addEventHandler(type, e -> {
action.run();
});
default Self hookContext(WiseRunnable beforeShowing, WiseRunnable afterHidden) {
if (beforeShowing != null) {
context().addEventHandler(WindowEvent.WINDOW_SHOWING, e -> beforeShowing.run());
}
if (afterHidden != null) {
context().addEventHandler(WindowEvent.WINDOW_HIDDEN, e -> afterHidden.run());
}
return (Self) this;
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/viewtify/ui/helper/EnhancedContextMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
import javafx.scene.input.ContextMenuEvent;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

import viewtify.ui.anime.Anime;
import viewtify.util.MonkeyPatch;

public class EnhancedContextMenu extends ContextMenu {
public class EnhancedContextMenu extends ContextMenu implements UserActionHelper<EnhancedContextMenu> {

/** The reusable event consumer. */
static final EventHandler<ContextMenuEvent> NOOP = ContextMenuEvent::consume;
Expand All @@ -46,6 +47,14 @@ public void handle(WindowEvent x) {
});
}

/**
* {@inheritDoc}
*/
@Override
public Object ui() {
return this;
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit b8783c4

Please sign in to comment.