Browse Source

Only update frame icon on theme change if necessary.

pull/188/head
weisj 5 years ago
parent
commit
89f6ff8670
  1. 27
      core/src/main/java/com/github/weisj/darklaf/graphics/ImageUtil.java
  2. 29
      property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedIcon.java
  3. 2
      property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedSVGIcon.java

27
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.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.beans.PropertyChangeListener;
import java.util.function.BiConsumer;
import javax.swing.*; import javax.swing.*;
import com.github.weisj.darklaf.LafManager; import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.icons.ImageSource; 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.theme.event.ThemeInstalledListener;
import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.Scale; 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) { public static Image createFrameIcon(final Icon icon, final JFrame c) {
if (icon == null) return null; return createFrameIcon(icon, c, JFrame::setIconImage);
if (c != null) {
ThemeInstalledListener listener = e -> c.setIconImage(iconToImage(icon, c));
LafManager.addThemeChangeListener(listener);
}
return createScaledFrameIcon(icon, c);
} }
public static Image createFrameIcon(final Icon icon, final JDialog c) { public static Image createFrameIcon(final Icon icon, final JDialog c) {
return createFrameIcon(icon, c, JDialog::setIconImage);
}
public static <T extends Window> Image createFrameIcon(final Icon icon, final T c,
final BiConsumer<T, Image> iconSetter) {
if (icon == null) return null; if (icon == null) return null;
if (c != null) { if (c != null) {
ThemeInstalledListener listener = e -> c.setIconImage(iconToImage(icon, c)); if (iconNeedUpdates(icon)) {
ThemeInstalledListener listener = e -> iconSetter.accept(c, iconToImage(icon, c));
LafManager.addThemeChangeListener(listener); LafManager.addThemeChangeListener(listener);
} }
PropertyChangeListener propertyChangeListener = e -> iconSetter.accept(c, iconToImage(icon, c));
c.addPropertyChangeListener(PropertyKey.GRAPHICS_CONFIGURATION, propertyChangeListener);
}
return createScaledFrameIcon(icon, c); 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) { private static Image createScaledFrameIcon(final Icon icon, final Window c) {
if (c != null && !c.isVisible()) { if (c != null && !c.isVisible()) {
Component parent = c.getParent(); Component parent = c.getParent();

29
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 {}

2
property-loader/src/main/java/com/github/weisj/darklaf/icons/ThemedSVGIcon.java

@ -30,7 +30,7 @@ import java.net.URI;
/** /**
* @author Jannis Weis * @author Jannis Weis
*/ */
public class ThemedSVGIcon extends DarkSVGIcon { public class ThemedSVGIcon extends DarkSVGIcon implements ThemedIcon {
private Object currentTheme; private Object currentTheme;

Loading…
Cancel
Save