From e2277918c65c980b92245f5a56594224349bfbd8 Mon Sep 17 00:00:00 2001 From: Jannis Weis <31143295+weisJ@users.noreply.github.com> Date: Sun, 2 Jan 2022 19:00:45 +0100 Subject: [PATCH] Restore previous Laf when creating the base laf When we instantiate a Darklaf we have to create the base laf. For macOS this may require us to install the system laf in the UIManager. We should restore it the way we found it to ensure no surprises if one instantiates Darklaf without trying to install it directly afterwards. Because this makes the instantiation heavier we skip the restoring if we know we are installing the laf directly afterwards e.g. when installing through the LafManager. --- .../com/github/weisj/darklaf/DarkLaf.java | 24 +++++++++++++------ .../github/weisj/darklaf/LafInstaller.java | 4 ++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/DarkLaf.java b/core/src/main/java/com/github/weisj/darklaf/DarkLaf.java index 5f89c815..ff15a014 100644 --- a/core/src/main/java/com/github/weisj/darklaf/DarkLaf.java +++ b/core/src/main/java/com/github/weisj/darklaf/DarkLaf.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2019-2021 Jannis Weis + * Copyright (c) 2019-2022 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, @@ -80,30 +80,40 @@ public class DarkLaf extends ThemedLookAndFeel { private final boolean runListenerCallback; - DarkLaf(final Theme theme, final boolean runListenerCallback) { + DarkLaf(final Theme theme, final boolean runListenerCallback, final boolean isBeingInstalled) { this.runListenerCallback = runListenerCallback; this.theme = theme; - this.base = getBase(); + this.base = getBase(isBeingInstalled); } - /** Create Custom Darcula LaF. */ + /** Create Custom DarkLaf. */ public DarkLaf() { - this(null, true); + this(null, true, false); } - private LookAndFeel getBase() { + private LookAndFeel getBase(boolean isBeingInstalled) { LookAndFeel baseLaf; if (SystemInfo.isWindows || SystemInfo.isLinux) { baseLaf = new MetalLookAndFeel(); } else { final String systemLafClassName = UIManager.getSystemLookAndFeelClassName(); final LookAndFeel currentLaf = UIManager.getLookAndFeel(); - if (currentLaf != null && systemLafClassName.equals(currentLaf.getClass().getName())) { + if (currentLaf instanceof DarkLaf) { + // Share the base laf + baseLaf = ((DarkLaf) currentLaf).base; + } else if (currentLaf != null && systemLafClassName.equals(currentLaf.getClass().getName())) { baseLaf = currentOrFallback(currentLaf); } else { try { UIManager.setLookAndFeel(systemLafClassName); baseLaf = currentOrFallback(UIManager.getLookAndFeel()); + if (!isBeingInstalled) { + /* + * Reset to previous state. This path only gets hit if someone installs the laf by manually + * instantiating it. + */ + UIManager.setLookAndFeel(currentLaf); + } } catch (final Exception e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); throw new IllegalStateException("Could not load base LaF class." + e.getMessage()); diff --git a/core/src/main/java/com/github/weisj/darklaf/LafInstaller.java b/core/src/main/java/com/github/weisj/darklaf/LafInstaller.java index ac640c4a..69259ba1 100644 --- a/core/src/main/java/com/github/weisj/darklaf/LafInstaller.java +++ b/core/src/main/java/com/github/weisj/darklaf/LafInstaller.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2021 Jannis Weis + * Copyright (c) 2021-2022 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, @@ -49,7 +49,7 @@ final class LafInstaller { try { LOGGER.fine(() -> "Installing theme " + theme); LafTransition transition = LafTransition.showSnapshot(); - UIManager.setLookAndFeel(new DarkLaf(theme, false)); + UIManager.setLookAndFeel(new DarkLaf(theme, false, true)); updateLaf(); SwingUtilities.invokeLater(transition::runTransition); notifyThemeInstalled(theme);