From d913e79e3f6a54b1e66b941a3beca6b2e2359f1f Mon Sep 17 00:00:00 2001 From: Harry Huang Date: Mon, 7 Oct 2024 22:28:47 +0800 Subject: [PATCH] refactor: reuse toggle wrapper --- .../harryh/arkpets/controllers/BehaviorModule.java | 7 +------ .../harryh/arkpets/controllers/SettingsModule.java | 14 ++------------ .../src/cn/harryh/arkpets/utils/GuiPrefabs.java | 9 +++++++++ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/desktop/src/cn/harryh/arkpets/controllers/BehaviorModule.java b/desktop/src/cn/harryh/arkpets/controllers/BehaviorModule.java index e139bd39..8895cc70 100644 --- a/desktop/src/cn/harryh/arkpets/controllers/BehaviorModule.java +++ b/desktop/src/cn/harryh/arkpets/controllers/BehaviorModule.java @@ -132,12 +132,7 @@ private void initConfigBehavior() { app.config.save(); }); - toggleConfigDeployPosition.setOnAction(e -> { - if (wrapperConfigDeployPosition.isVisible()) - GuiPrefabs.fadeOutNode(wrapperConfigDeployPosition, durationFast, null); - else - GuiPrefabs.fadeInNode(wrapperConfigDeployPosition, durationFast, null); - }); + GuiPrefabs.bindToggleAndWrapper(toggleConfigDeployPosition, wrapperConfigDeployPosition, durationFast); GuiComponents.DotPickerSetup setupDeployPosition = new GuiComponents.DotPickerSetup(configDeployPosition); setupDeployPosition.setRelXY(app.config.initial_position_x, app.config.initial_position_y); setupDeployPosition.setOnDotPicked(e -> { diff --git a/desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java b/desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java index 4c7c904c..df8f3b0d 100644 --- a/desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java +++ b/desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java @@ -169,12 +169,7 @@ public String getContent() { app.config.save(); }); - toggleConfigRenderOutline.setOnAction(e -> { - if (wrapperConfigRenderOutline.isVisible()) - GuiPrefabs.fadeOutNode(wrapperConfigRenderOutline, durationFast, null); - else - GuiPrefabs.fadeInNode(wrapperConfigRenderOutline, durationFast, null); - }); + GuiPrefabs.bindToggleAndWrapper(toggleConfigRenderOutline, wrapperConfigRenderOutline, durationFast); new ComboBoxSetup<>(configRenderOutline).setItems(new NamedItem<>("始终开启", ArkConfig.RenderOutline.ALWAYS.ordinal()), new NamedItem<>("处于前台时", ArkConfig.RenderOutline.FOCUSED.ordinal()), new NamedItem<>("点击时", ArkConfig.RenderOutline.PRESSING.ordinal()), @@ -204,12 +199,7 @@ public String getContent() { app.config.save(); }); - toggleConfigRenderOpacity.setOnAction(e -> { - if (wrapperConfigRenderOpacity.isVisible()) - GuiPrefabs.fadeOutNode(wrapperConfigRenderOpacity, durationFast, null); - else - GuiPrefabs.fadeInNode(wrapperConfigRenderOpacity, durationFast, null); - }); + GuiPrefabs.bindToggleAndWrapper(toggleConfigRenderOpacity, wrapperConfigRenderOpacity, durationFast); final int minOpacity = 10; GuiComponents.SliderSetup setupRenderOpacityDim = new GuiComponents.SimpleIntegerSliderSetup(configRenderOpacityDim); setupRenderOpacityDim diff --git a/desktop/src/cn/harryh/arkpets/utils/GuiPrefabs.java b/desktop/src/cn/harryh/arkpets/utils/GuiPrefabs.java index 0c1787b0..b653fcec 100644 --- a/desktop/src/cn/harryh/arkpets/utils/GuiPrefabs.java +++ b/desktop/src/cn/harryh/arkpets/utils/GuiPrefabs.java @@ -105,6 +105,15 @@ public static void fadeOutWindow(Window window, Duration duration, EventHandler< timeline.playFromStart(); } + public static void bindToggleAndWrapper(JFXButton toggle, Node wrapper, Duration duration) { + toggle.setOnAction(e -> { + if (wrapper.isVisible()) + GuiPrefabs.fadeOutNode(wrapper, duration, null); + else + GuiPrefabs.fadeInNode(wrapper, duration, null); + }); + } + public static void replaceStyleClass(Node node, String from, String to) { HashSet styleClass = new HashSet<>(Set.copyOf(node.getStyleClass())); styleClass.remove(from);