diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ScaledSVGIcon.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ScaledSVGIcon.java index aef13317..dd397503 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ScaledSVGIcon.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ScaledSVGIcon.java @@ -22,7 +22,10 @@ import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.net.URISyntaxException; +import java.net.URL; import javax.swing.ImageIcon; +import javax.swing.UIManager; +import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.util.UIScale; import com.kitfox.svg.app.beans.SVGIcon; @@ -33,31 +36,57 @@ import com.kitfox.svg.app.beans.SVGIcon; public class ScaledSVGIcon extends ImageIcon { + private final String name; private final SVGIcon svgIcon; + private boolean dark; public ScaledSVGIcon( String name ) { + this.name = name; + svgIcon = new SVGIcon(); svgIcon.setAntiAlias( true ); + } + + private void update() { + if( dark == isDarkLaf() && svgIcon.getSvgURI() != null ) + return; + + dark = isDarkLaf(); + URL url = getIconURL( name, dark ); + if( url == null & dark ) + url = getIconURL( name, false ); try { - svgIcon.setSvgURI( getClass().getResource( name ).toURI() ); + svgIcon.setSvgURI( url.toURI() ); } catch( URISyntaxException ex ) { ex.printStackTrace(); } } + private URL getIconURL( String name, boolean dark ) { + if( dark ) { + int dotIndex = name.lastIndexOf( '.' ); + name = name.substring( 0, dotIndex ) + "_dark" + name.substring( dotIndex ); + } + return getClass().getResource( name ); + } + @Override public int getIconWidth() { + update(); return UIScale.scale( svgIcon.getIconWidth() ); } @Override public int getIconHeight() { + update(); return UIScale.scale( svgIcon.getIconHeight() ); } @Override public void paintIcon( Component c, Graphics g, int x, int y ) { + update(); + Graphics2D g2 = (Graphics2D) g.create(); try { FlatUIUtils.setRenderingHints( g2 ); @@ -73,6 +102,8 @@ public class ScaledSVGIcon @Override public Image getImage() { + update(); + BufferedImage image = new BufferedImage( getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB ); Graphics2D g = image.createGraphics(); try { @@ -82,4 +113,22 @@ public class ScaledSVGIcon } return image; } + + private static Boolean darkLaf; + + private static boolean isDarkLaf() { + if( darkLaf == null ) { + lafChanged(); + + UIManager.addPropertyChangeListener( e -> { + lafChanged(); + } ); + } + + return darkLaf; + } + + private static void lafChanged() { + darkLaf = (UIManager.getLookAndFeel() instanceof FlatDarkLaf); + } } diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/back_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/back_dark.svg new file mode 100644 index 00000000..88abaa73 --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/back_dark.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/copy_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/copy_dark.svg new file mode 100644 index 00000000..4ff18263 --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/copy_dark.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/forward_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/forward_dark.svg new file mode 100644 index 00000000..d031c78f --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/forward_dark.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/menu-cut_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/menu-cut_dark.svg new file mode 100644 index 00000000..6bae57d8 --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/menu-cut_dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/menu-paste_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/menu-paste_dark.svg new file mode 100644 index 00000000..10ca357f --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/menu-paste_dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/redo_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/redo_dark.svg new file mode 100644 index 00000000..63b1a69d --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/redo_dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/refresh_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/refresh_dark.svg new file mode 100644 index 00000000..0b0f5440 --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/refresh_dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/show_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/show_dark.svg new file mode 100644 index 00000000..24255733 --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/show_dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/undo_dark.svg b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/undo_dark.svg new file mode 100644 index 00000000..1ab3b7f3 --- /dev/null +++ b/flatlaf-demo/src/main/resources/com/formdev/flatlaf/demo/icons/undo_dark.svg @@ -0,0 +1,3 @@ + + +