Browse Source

Reimplement UIUpdater in terms of DynamicUI

This way the UIUpdater gets called for third party lafs.
pull/323/head
Jannis Weis 3 years ago
parent
commit
48076a90eb
No known key found for this signature in database
GPG Key ID: 7C9D8D4B558049AB
  1. 13
      core/src/main/java/com/github/weisj/darklaf/components/DynamicUI.java
  2. 54
      core/src/main/java/com/github/weisj/darklaf/listener/UIUpdater.java

13
core/src/main/java/com/github/weisj/darklaf/components/DynamicUI.java

@ -27,6 +27,7 @@ import java.util.function.Consumer;
import javax.swing.*; import javax.swing.*;
public final class DynamicUI { public final class DynamicUI {
private static final Map<Object, List<Consumer<Object>>> listeners = new WeakHashMap<>(); private static final Map<Object, List<Consumer<Object>>> listeners = new WeakHashMap<>();
@ -67,6 +68,18 @@ public final class DynamicUI {
return object; return object;
} }
public static <T> void removeCallback(final T object, final Consumer<T> callback) {
synchronized (listeners) {
listeners.compute(object, (k, v) -> {
if (v == null) return null;
if (!v.remove(callback)) return v;
if (v.size() == 0) return null;
if (v.size() == 1) return Collections.singletonList(v.get(0));
return v;
});
}
}
public static <T extends AbstractButton> T withLocalizedText(final T comp, final String textKey) { public static <T extends AbstractButton> T withLocalizedText(final T comp, final String textKey) {
return withDynamic(comp, c -> c.setText(UIManager.getString(textKey, c.getLocale()))); return withDynamic(comp, c -> c.setText(UIManager.getString(textKey, c.getLocale())));
} }

54
core/src/main/java/com/github/weisj/darklaf/listener/UIUpdater.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,
@ -20,22 +20,18 @@
*/ */
package com.github.weisj.darklaf.listener; package com.github.weisj.darklaf.listener;
import java.awt.*; import java.util.function.Consumer;
import java.lang.ref.WeakReference;
import javax.swing.*; import javax.swing.*;
import com.github.weisj.darklaf.LafManager; import com.github.weisj.darklaf.components.DynamicUI;
import com.github.weisj.darklaf.theme.event.ThemeChangeEvent;
import com.github.weisj.darklaf.theme.event.ThemeChangeListener;
/** /**
* Listener that updates the component ui after a new theme has been installed. This listener only * Listener that updates the component ui after a new laf has been installed. This listener only
* needs to be used with the top most component. This listener isn't needed for components that are * needs to be used with the top most component. This listener isn't needed for components that are
* part of a visible ui hierarchy. The listener has to be added with * part of a visible ui hierarchy.
* {@link LafManager#addThemeChangeListener(ThemeChangeListener)}
*/ */
public class UIUpdater implements ThemeChangeListener { public final class UIUpdater implements Consumer<JComponent> {
private static final String KEY_UPDATER = "JComponent.uiUpdaterLister"; private static final String KEY_UPDATER = "JComponent.uiUpdaterLister";
@ -47,18 +43,18 @@ public class UIUpdater implements ThemeChangeListener {
public static void registerComponent(final JComponent component) { public static void registerComponent(final JComponent component) {
if (component == null) return; if (component == null) return;
removeComponent(component); removeComponent(component);
UIUpdater updater = new UIUpdater(component); UIUpdater updater = new UIUpdater();
component.putClientProperty(KEY_UPDATER, updater); component.putClientProperty(KEY_UPDATER, updater);
LafManager.addThemeChangeListener(new UIUpdater(component)); DynamicUI.registerCallback(component, updater, false);
} }
/** /**
* Remove the registered {@link UIUpdater} from the component. This needs to be called in order for * Remove the registered {@link UIUpdater} from the component.
* the component to be garbage collected.
* *
* @param component the component to unregister. * @param component the component to unregister.
*/ */
public static void removeComponent(final JComponent component) { public static void removeComponent(final JComponent component) {
if (component == null) return;
Object updater = component.getClientProperty(KEY_UPDATER); Object updater = component.getClientProperty(KEY_UPDATER);
if (updater instanceof UIUpdater) { if (updater instanceof UIUpdater) {
removeComponent(component, (UIUpdater) updater); removeComponent(component, (UIUpdater) updater);
@ -66,34 +62,14 @@ public class UIUpdater implements ThemeChangeListener {
} }
private static void removeComponent(final JComponent component, final UIUpdater updater) { private static void removeComponent(final JComponent component, final UIUpdater updater) {
if (component != null) component.putClientProperty(KEY_UPDATER, null); component.putClientProperty(KEY_UPDATER, null);
LafManager.removeThemeChangeListener(updater); DynamicUI.removeCallback(component, updater);
} }
private final WeakReference<JComponent> component; private UIUpdater() {}
/**
* Creates a new ui updater for the given component. After a new theme has been installed This
* listener only needs to be used with the top most component. This listener isn't needed for
* components that are part of a visible ui hierarchy. The listener has to be added with
* {@link LafManager#addThemeChangeListener(ThemeChangeListener)}
*
* @param component the component to update.
*/
public UIUpdater(final JComponent component) {
this.component = new WeakReference<>(component);
}
@Override
public void themeChanged(final ThemeChangeEvent e) {}
@Override @Override
public void themeInstalled(final ThemeChangeEvent e) { public void accept(final JComponent c) {
JComponent comp = component.get(); SwingUtilities.updateComponentTreeUI(c);
if (comp != null) {
SwingUtilities.updateComponentTreeUI(comp);
} else {
removeComponent(null, this);
}
} }
} }

Loading…
Cancel
Save