Browse Source

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.
pull/295/head
Jannis Weis 3 years ago
parent
commit
e2277918c6
  1. 24
      core/src/main/java/com/github/weisj/darklaf/DarkLaf.java
  2. 4
      core/src/main/java/com/github/weisj/darklaf/LafInstaller.java

24
core/src/main/java/com/github/weisj/darklaf/DarkLaf.java

@ -1,7 +1,7 @@
/* /*
* MIT License * 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 * 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, * 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; 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.runListenerCallback = runListenerCallback;
this.theme = theme; this.theme = theme;
this.base = getBase(); this.base = getBase(isBeingInstalled);
} }
/** Create Custom Darcula LaF. */ /** Create Custom DarkLaf. */
public DarkLaf() { public DarkLaf() {
this(null, true); this(null, true, false);
} }
private LookAndFeel getBase() { private LookAndFeel getBase(boolean isBeingInstalled) {
LookAndFeel baseLaf; LookAndFeel baseLaf;
if (SystemInfo.isWindows || SystemInfo.isLinux) { if (SystemInfo.isWindows || SystemInfo.isLinux) {
baseLaf = new MetalLookAndFeel(); baseLaf = new MetalLookAndFeel();
} else { } else {
final String systemLafClassName = UIManager.getSystemLookAndFeelClassName(); final String systemLafClassName = UIManager.getSystemLookAndFeelClassName();
final LookAndFeel currentLaf = UIManager.getLookAndFeel(); 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); baseLaf = currentOrFallback(currentLaf);
} else { } else {
try { try {
UIManager.setLookAndFeel(systemLafClassName); UIManager.setLookAndFeel(systemLafClassName);
baseLaf = currentOrFallback(UIManager.getLookAndFeel()); 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) { } catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e); LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new IllegalStateException("Could not load base LaF class." + e.getMessage()); throw new IllegalStateException("Could not load base LaF class." + e.getMessage());

4
core/src/main/java/com/github/weisj/darklaf/LafInstaller.java

@ -1,7 +1,7 @@
/* /*
* MIT License * 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 * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
@ -49,7 +49,7 @@ final class LafInstaller {
try { try {
LOGGER.fine(() -> "Installing theme " + theme); LOGGER.fine(() -> "Installing theme " + theme);
LafTransition transition = LafTransition.showSnapshot(); LafTransition transition = LafTransition.showSnapshot();
UIManager.setLookAndFeel(new DarkLaf(theme, false)); UIManager.setLookAndFeel(new DarkLaf(theme, false, true));
updateLaf(); updateLaf();
SwingUtilities.invokeLater(transition::runTransition); SwingUtilities.invokeLater(transition::runTransition);
notifyThemeInstalled(theme); notifyThemeInstalled(theme);

Loading…
Cancel
Save