Browse Source

Add default implementation of UIAwareIcon which don't rely on the IconLoader mechanism for external custom icons.

pull/214/head
weisj 4 years ago
parent
commit
c4f64a4971
  1. 108
      property-loader/src/main/java/com/github/weisj/darklaf/icons/DefaultUIAwareIcon.java
  2. 23
      property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java
  3. 70
      property-loader/src/main/java/com/github/weisj/darklaf/icons/LazyUIAwareIcon.java
  4. 56
      property-loader/src/main/java/com/github/weisj/darklaf/icons/SimpleUIAwareIcon.java
  5. 16
      property-loader/src/main/java/com/github/weisj/darklaf/icons/UIAwareIcon.java

108
property-loader/src/main/java/com/github/weisj/darklaf/icons/DefaultUIAwareIcon.java

@ -0,0 +1,108 @@
/*
* 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 java.awt.*;
import javax.swing.*;
/**
* Default base implementation for {@link UIAwareIcon}. The specific loading mechanism needs to be
* implemented in the subclass.
*/
public abstract class DefaultUIAwareIcon implements UIAwareIcon {
private final UIAwareIcon dual;
protected Icon light;
protected Icon dark;
public DefaultUIAwareIcon() {
this.dual = createDual();
}
protected abstract UIAwareIcon createDual();
protected DefaultUIAwareIcon(final DefaultUIAwareIcon dual) {
this.dark = dual.light;
this.light = dual.dark;
this.dual = dual;
}
@Override
public UIAwareIcon getDual() {
return dual;
}
private boolean isDark() {
return IconLoader.getAwareStyle() == AwareIconStyle.DARK;
}
private Icon getEffectiveIcon() {
return isDark() ? getDarkIcon() : getLightIcon();
}
/**
* Load the light version of the icon. This method should never return a null value.
*
* @return the light icon.
*/
protected abstract Icon loadLightIcon();
/**
* Load the dark version of the icon. This method should never return a null value.
*
* @return the dark icon.
*/
protected abstract Icon loadDarkIcon();
private Icon getLightIcon() {
if (light == null) {
light = loadLightIcon();
}
return light;
}
private Icon getDarkIcon() {
if (dark == null) {
dark = loadDarkIcon();
}
return dark;
}
@Override
public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
getEffectiveIcon().paintIcon(c, g, x, y);
}
@Override
public int getIconWidth() {
return getEffectiveIcon().getIconWidth();
}
@Override
public int getIconHeight() {
return getEffectiveIcon().getIconHeight();
}
}

23
property-loader/src/main/java/com/github/weisj/darklaf/icons/IconLoader.java

@ -158,6 +158,29 @@ public final class IconLoader {
return getUIAwareIcon(path, getDefaultWidth(path), getDefaultHeight(path));
}
/**
* Creates a new {@link UIAwareIcon} which is loaded lazily through the given supplier.
*
* @param lightIconSupplier the supplier for the light icon.
* @param darkIconSupplier the supplier for the dark icon.
* @return the {@link UIAwareIcon}
*/
public UIAwareIcon createUIAwareIcon(final IconSupplier<Icon> lightIconSupplier,
final IconSupplier<Icon> darkIconSupplier) {
return new LazyUIAwareIcon(lightIconSupplier, darkIconSupplier);
}
/**
* Creates a new {@link UIAwareIcon} from the given icon.
*
* @param light the light version of the icon.
* @param dark the dark version of the icon.
* @return the {@link UIAwareIcon}.
*/
public UIAwareIcon createUIAwareIcon(final Icon light, final Icon dark) {
return new SimpleUIAwareIcon(light, dark);
}
/**
* Get an aware icon. If [path] is the search root of the current icon loader then the icon resource
* will be resolved to [path]/dark/[icon_path] and [path]/light/[icon_path]

70
property-loader/src/main/java/com/github/weisj/darklaf/icons/LazyUIAwareIcon.java

@ -0,0 +1,70 @@
/*
* 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 class LazyUIAwareIcon extends DefaultUIAwareIcon {
private IconSupplier<Icon> lightSupplier;
private IconSupplier<Icon> darkSupplier;
public LazyUIAwareIcon(final IconSupplier<Icon> lightSupplier, final IconSupplier<Icon> darkSupplier) {
this.lightSupplier = lightSupplier;
this.darkSupplier = darkSupplier;
}
protected LazyUIAwareIcon(final LazyUIAwareIcon dual) {
super(dual);
this.lightSupplier = dual.darkSupplier;
this.darkSupplier = dual.lightSupplier;
}
@Override
protected UIAwareIcon createDual() {
return new LazyUIAwareIcon(this);
}
protected Icon loadLightIcon() {
Icon icon;
if (lightSupplier != null) {
icon = lightSupplier.getIcon();
lightSupplier = null;
} else {
// Should never happen.
icon = EmptyIcon.create(0);
}
return icon;
}
protected Icon loadDarkIcon() {
Icon icon;
if (darkSupplier != null) {
icon = darkSupplier.getIcon();
darkSupplier = null;
} else {
// Should never happen.
icon = EmptyIcon.create(0);
}
return icon;
}
}

56
property-loader/src/main/java/com/github/weisj/darklaf/icons/SimpleUIAwareIcon.java

@ -0,0 +1,56 @@
/*
* 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.*;
/**
* Implementation of {@link UIAwareIcon} which delegates to preloaded icons.
*/
public class SimpleUIAwareIcon extends DefaultUIAwareIcon {
public SimpleUIAwareIcon(final Icon light, final Icon dark) {
this.light = light;
this.dark = dark;
}
protected SimpleUIAwareIcon(final SimpleUIAwareIcon dual) {
super(dual);
this.light = dual.dark;
this.dark = dual.light;
}
@Override
protected UIAwareIcon createDual() {
return new SimpleUIAwareIcon(this);
}
@Override
protected Icon loadLightIcon() {
return null;
}
@Override
protected Icon loadDarkIcon() {
return null;
}
}

16
property-loader/src/main/java/com/github/weisj/darklaf/icons/UIAwareIcon.java

@ -21,10 +21,20 @@
*/
package com.github.weisj.darklaf.icons;
import javax.swing.*;
/** @author Jannis Weis */
/**
* Icon which dynamically adapts to whether the UI is currently {@link AwareIconStyle#LIGHT light}
* or {@link AwareIconStyle#DARK dark}.
*/
public interface UIAwareIcon extends DynamicIcon {
/**
* Return the dual version of the icon. i.e. if the current icon is light then the dual version will
* be dark and vice versa.
*
* Implementations of this method should respect that the appearance of the icon after invoking this
* method twice should be the same.
*
* @return the dual icon.
*/
UIAwareIcon getDual();
}

Loading…
Cancel
Save