From efa446a32d35dc5ea7c3d07570ad7d9624304155 Mon Sep 17 00:00:00 2001 From: weisj Date: Tue, 1 Dec 2020 23:45:32 +0100 Subject: [PATCH] Add option to change the shared ThemeSettingsPanel. --- .../weisj/darklaf/settings/ThemeSettings.java | 17 +++++++- .../github/weisj/darklaf/util/LazyValue.java | 6 ++- .../weisj/darklaf/util/MutableLazyValue.java | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java diff --git a/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java b/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java index b5180554..0888d60a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java +++ b/core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java @@ -41,6 +41,7 @@ import com.github.weisj.darklaf.theme.info.PreferredThemeStyle; import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.LazyValue; import com.github.weisj.darklaf.util.LogUtil; +import com.github.weisj.darklaf.util.MutableLazyValue; public class ThemeSettings implements ThemePreferenceListener { @@ -48,7 +49,7 @@ public class ThemeSettings implements ThemePreferenceListener { private static final LazyValue instance = new LazyValue<>(ThemeSettings::new); private static final LazyValue icon = new LazyValue<>(() -> UIManager.getIcon("ThemeSettings.icon")); - private final LazyValue settingsPanel; + private final MutableLazyValue settingsPanel; private JDialog dialog; @@ -88,7 +89,7 @@ public class ThemeSettings implements ThemePreferenceListener { LafManager.addThemePreferenceChangeListener(this); currentConfiguration = new DefaultSettingsConfiguration(); savedConfiguration = new DefaultSettingsConfiguration(); - settingsPanel = new LazyValue<>(() -> { + settingsPanel = new MutableLazyValue<>(() -> { ThemeSettingsPanel panel = new ThemeSettingsPanel(); panel.loadConfiguration(currentConfiguration); return panel; @@ -147,6 +148,18 @@ public class ThemeSettings implements ThemePreferenceListener { return settingsPanel.get(); } + /** + * Set the shared {@link ThemeSettingsPanel}. + * + * @param panel the @link ThemeSettingsPanel}. + */ + public void setThemeSettingsPanel(final ThemeSettingsPanel panel) { + if (panel != null) { + settingsPanel.set(panel); + panel.loadConfiguration(currentConfiguration); + } + } + /** * Returns whether the option to follow the system settings is enabled. * diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java b/utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java index 33a9d006..422493a6 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java @@ -51,6 +51,10 @@ public class LazyValue { return value; } + protected void set(final T value) { + this.value = value; + } + public void ifPresent(final Consumer action) { if (isInitialized() && value != null) { action.accept(value); @@ -64,7 +68,7 @@ public class LazyValue { } public T get() { - if (value == null) value = load(); + if (value == null) set(load()); return value; } } diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java b/utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java new file mode 100644 index 00000000..f050e2a2 --- /dev/null +++ b/utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java @@ -0,0 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jannis Weis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package com.github.weisj.darklaf.util; + +import java.util.function.Supplier; + +public class MutableLazyValue extends LazyValue { + + + public MutableLazyValue(final T value) { + super(value); + } + + public MutableLazyValue(final Supplier supplier) { + super(supplier); + } + + public void set(final T value) { + super.set(value); + } +}