From b0bdd2ce2ad0e4806b5d83e59115553181dc9835 Mon Sep 17 00:00:00 2001 From: tferr Date: Sun, 3 Apr 2022 20:22:17 -0400 Subject: [PATCH] OptionsLookAndFeel: Support for FlatLaf This implements support for FlatLaf Look and Feels. A static method is also included so that Look and Feel can be set at startup. E.g., for Fjii the following call in the StartupMacros could be used to set the Look and Feel programmatically at startup: eval("js","importClass(Packages.org.scijava.ui.swing.options.OptionsLookAndFeel);OptionsLookAndFeel.setupLookAndFeel('FlatLaf Dark');") NB: there are currently several hardwired foreground/background colors in several classes (e.g., ConsolePanel) that will interfere with dark themes. Signed-off-by: Curtis Rueden --- pom.xml | 6 ++ .../ui/swing/options/OptionsLookAndFeel.java | 58 ++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 72cf18fd..10ab6316 100644 --- a/pom.xml +++ b/pom.xml @@ -110,6 +110,7 @@ sign,deploy-to-scijava + 2.4 1.3.2 0.1 @@ -157,6 +158,11 @@ org.jfree jfreesvg + + com.formdev + flatlaf + ${flatlaf.version} + com.github.sbridges.object-inspector object-inspector diff --git a/src/main/java/org/scijava/ui/swing/options/OptionsLookAndFeel.java b/src/main/java/org/scijava/ui/swing/options/OptionsLookAndFeel.java index 4b748b7c..063fbf70 100644 --- a/src/main/java/org/scijava/ui/swing/options/OptionsLookAndFeel.java +++ b/src/main/java/org/scijava/ui/swing/options/OptionsLookAndFeel.java @@ -41,6 +41,11 @@ import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.UnsupportedLookAndFeelException; +import com.formdev.flatlaf.FlatLightLaf; +import com.formdev.flatlaf.FlatDarkLaf; +import com.formdev.flatlaf.FlatIntelliJLaf; +import com.formdev.flatlaf.FlatDarculaLaf; + import org.scijava.display.Display; import org.scijava.display.DisplayService; import org.scijava.log.LogService; @@ -131,8 +136,15 @@ public void run() { protected void initLookAndFeel() { final String lafClass = UIManager.getLookAndFeel().getClass().getName(); + LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels(); + + // Make UIManager aware of FlatLaf look and feels, as needed + if (!isRegistered(lookAndFeels, FlatLightLaf.NAME)) FlatLightLaf.installLafInfo(); + if (!isRegistered(lookAndFeels, FlatDarkLaf.NAME)) FlatDarkLaf.installLafInfo(); + if (!isRegistered(lookAndFeels, FlatDarculaLaf.NAME)) FlatDarculaLaf.installLafInfo(); + if (!isRegistered(lookAndFeels, FlatIntelliJLaf.NAME)) FlatIntelliJLaf.installLafInfo(); + lookAndFeels = UIManager.getInstalledLookAndFeels(); // retrieve updated list - final LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels(); final ArrayList lookAndFeelChoices = new ArrayList<>(); for (final LookAndFeelInfo lafInfo : lookAndFeels) { final String lafName = lafInfo.getName(); @@ -149,6 +161,15 @@ protected void initLookAndFeel() { // -- Helper methods -- + /** Assesses whether lookAndFeels contains the laf associated with lafName*/ + private boolean isRegistered(final LookAndFeelInfo[] lookAndFeels, final String lafName) { + for (final LookAndFeelInfo lafInfo : lookAndFeels) { + if (lafInfo.getName().equals(lafName)) + return true; + } + return false; + } + /** Tells all known Swing components to change to the new Look & Feel. */ private void refreshSwingComponents() { // TODO: Change this hacky logic to call a clean UIService API @@ -202,6 +223,41 @@ private DisplayService displayService() { return getContext().service(DisplayService.class); } + /** + * Sets the application look and feel. + *

+ * Useful for setting up the look and feel early on in application startup + * routine (e.g., through a macro or script) + *

+ * + * @param lookAndFeel the look and feel. Supported values include "FlatLaf + * Light", "FlatLaf Dark", "FlatLaf Darcula", "FlatLaf + * IntelliJ", and JVM defaults + * ("javax.swing.plaf.metal.MetalLookAndFeel", etc.) + */ + public static void setupLookAndFeel(final String lookAndFeel) { + switch (lookAndFeel) { + case FlatLightLaf.NAME: + FlatLightLaf.setup(); + return; + case FlatDarkLaf.NAME: + FlatDarkLaf.setup(); + return; + case FlatDarculaLaf.NAME: + FlatDarculaLaf.setup(); + return; + case FlatIntelliJLaf.NAME: + FlatIntelliJLaf.setup(); + return; + default: + try { + UIManager.setLookAndFeel(lookAndFeel); + } catch (final Exception ex) { + ex.printStackTrace(); + } + } + } + // -- Deprecated methods -- @Deprecated