From 89f6ff86709262f7773195cbfd4faac831f0c035 Mon Sep 17 00:00:00 2001 From: weisj Date: Sat, 13 Jun 2020 14:24:23 +0200 Subject: [PATCH] Only update frame icon on theme change if necessary. --- .../weisj/darklaf/graphics/ImageUtil.java | 29 ++++++++++++++----- .../weisj/darklaf/icons/ThemedIcon.java | 29 +++++++++++++++++++ .../weisj/darklaf/icons/ThemedSVGIcon.java | 2 +- 3 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedIcon.java diff --git a/core/src/main/java/com/github/weisj/darklaf/graphics/ImageUtil.java b/core/src/main/java/com/github/weisj/darklaf/graphics/ImageUtil.java index bd5d8b33..ed17d4fa 100644 --- a/core/src/main/java/com/github/weisj/darklaf/graphics/ImageUtil.java +++ b/core/src/main/java/com/github/weisj/darklaf/graphics/ImageUtil.java @@ -26,12 +26,17 @@ package com.github.weisj.darklaf.graphics; import java.awt.*; import java.awt.image.BufferedImage; +import java.beans.PropertyChangeListener; +import java.util.function.BiConsumer; import javax.swing.*; import com.github.weisj.darklaf.LafManager; import com.github.weisj.darklaf.icons.ImageSource; +import com.github.weisj.darklaf.icons.ThemedIcon; +import com.github.weisj.darklaf.icons.UIAwareIcon; import com.github.weisj.darklaf.theme.event.ThemeInstalledListener; +import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.Scale; /** @@ -54,23 +59,31 @@ public final class ImageUtil { } public static Image createFrameIcon(final Icon icon, final JFrame c) { - if (icon == null) return null; - if (c != null) { - ThemeInstalledListener listener = e -> c.setIconImage(iconToImage(icon, c)); - LafManager.addThemeChangeListener(listener); - } - return createScaledFrameIcon(icon, c); + return createFrameIcon(icon, c, JFrame::setIconImage); } public static Image createFrameIcon(final Icon icon, final JDialog c) { + return createFrameIcon(icon, c, JDialog::setIconImage); + } + + public static Image createFrameIcon(final Icon icon, final T c, + final BiConsumer iconSetter) { if (icon == null) return null; if (c != null) { - ThemeInstalledListener listener = e -> c.setIconImage(iconToImage(icon, c)); - LafManager.addThemeChangeListener(listener); + if (iconNeedUpdates(icon)) { + ThemeInstalledListener listener = e -> iconSetter.accept(c, iconToImage(icon, c)); + LafManager.addThemeChangeListener(listener); + } + PropertyChangeListener propertyChangeListener = e -> iconSetter.accept(c, iconToImage(icon, c)); + c.addPropertyChangeListener(PropertyKey.GRAPHICS_CONFIGURATION, propertyChangeListener); } return createScaledFrameIcon(icon, c); } + private static boolean iconNeedUpdates(final Icon icon) { + return icon instanceof UIAwareIcon || icon instanceof ThemedIcon; + } + private static Image createScaledFrameIcon(final Icon icon, final Window c) { if (c != null && !c.isVisible()) { Component parent = c.getParent(); diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedIcon.java new file mode 100644 index 00000000..42c3416a --- /dev/null +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedIcon.java @@ -0,0 +1,29 @@ +/* + * 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.icons; + +import javax.swing.*; + +public interface ThemedIcon extends Icon {} diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedSVGIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedSVGIcon.java index 7d8a6734..64d16b35 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedSVGIcon.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedSVGIcon.java @@ -30,7 +30,7 @@ import java.net.URI; /** * @author Jannis Weis */ -public class ThemedSVGIcon extends DarkSVGIcon { +public class ThemedSVGIcon extends DarkSVGIcon implements ThemedIcon { private Object currentTheme;