Browse Source

Memory: Further ensure no memory leaks for with shared resources and wrappers can occur.

pull/245/head
weisj 3 years ago
parent
commit
acc4aaf7c2
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 26
      core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java
  2. 1
      core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkEditorPaneUIBridge.java
  3. 4
      core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkTextFieldUIBridge.java
  4. 43
      core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyEditorPane.java
  5. 9
      core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyTextFieldUI.java
  6. 5
      utils/src/main/java/com/github/weisj/darklaf/util/value/WeakShared.java

26
core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java

@ -41,7 +41,7 @@ import com.github.weisj.darklaf.theme.info.PreferredThemeStyle;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.LazyValue;
import com.github.weisj.darklaf.util.LogUtil;
import com.github.weisj.darklaf.util.MutableLazyValue;
import com.github.weisj.darklaf.util.value.WeakShared;
public class ThemeSettings implements ThemePreferenceListener {
@ -49,7 +49,8 @@ public class ThemeSettings implements ThemePreferenceListener {
private static final LazyValue<ThemeSettings> instance = new LazyValue<>(ThemeSettings::new);
private static final LazyValue<Icon> icon = new LazyValue<>(() -> UIManager.getIcon("ThemeSettings.icon"));
private final MutableLazyValue<ThemeSettingsPanel> settingsPanel;
private final WeakShared<ThemeSettingsPanel> settingsPanel;
private ThemeSettingsPanel customThemeSettingsPanel;
private JDialog dialog;
@ -89,7 +90,7 @@ public class ThemeSettings implements ThemePreferenceListener {
LafManager.addThemePreferenceChangeListener(this);
currentConfiguration = new DefaultSettingsConfiguration();
savedConfiguration = new DefaultSettingsConfiguration();
settingsPanel = new MutableLazyValue<>(() -> {
settingsPanel = new WeakShared<>(() -> {
ThemeSettingsPanel panel = new ThemeSettingsPanel();
panel.loadConfiguration(currentConfiguration);
return panel;
@ -155,8 +156,10 @@ public class ThemeSettings implements ThemePreferenceListener {
*/
public void setThemeSettingsPanel(final ThemeSettingsPanel panel) {
if (panel != null) {
settingsPanel.set(panel);
customThemeSettingsPanel = panel;
panel.loadConfiguration(currentConfiguration);
} else {
customThemeSettingsPanel = null;
}
}
@ -381,9 +384,7 @@ public class ThemeSettings implements ThemePreferenceListener {
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
});
JButton apply = new JButton(UIManager.getString("dialog_apply", l));
apply.addActionListener(e -> {
apply();
});
apply.addActionListener(e -> apply());
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
@ -404,12 +405,19 @@ public class ThemeSettings implements ThemePreferenceListener {
updateSettingsPanel();
}
private ThemeSettingsPanel getThemeSettingsPanelWeak() {
if (customThemeSettingsPanel != null) return customThemeSettingsPanel;
return settingsPanel.getIfPresent();
}
private void updateSettingsPanel() {
settingsPanel.ifPresent(p -> p.loadConfiguration(currentConfiguration));
ThemeSettingsPanel panel = getThemeSettingsPanelWeak();
if (panel != null) panel.loadConfiguration(currentConfiguration);
}
private void fetchFromSettingsPanel() {
settingsPanel.ifPresent(ThemeSettingsPanel::updateConfiguration);
ThemeSettingsPanel panel = getThemeSettingsPanelWeak();
if (panel != null) panel.updateConfiguration();
}
/** Saves the settings. */

1
core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkEditorPaneUIBridge.java

@ -80,6 +80,7 @@ public abstract class DarkEditorPaneUIBridge extends DarkTextUI {
}
protected void updateDisplayProperties() {
editorPane.setEditorPane((JEditorPane) editor);
updateDisplayProperties((JEditorPane) getComponent(),
new PropertyChangeEvent(editorPane, PropertyKey.FONT, editorPane.getFont(), editorPane.getFont()));
}

4
core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkTextFieldUIBridge.java

@ -69,7 +69,9 @@ public abstract class DarkTextFieldUIBridge extends DarkTextUI {
@Override
public int getBaseline(final JComponent c, final int width, final int height) {
basicTextFieldUI.setRootView(getRootView((JTextComponent) c));
return basicTextFieldUI.getBaseline(c, width, height);
int baseLine = basicTextFieldUI.getBaseline(c, width, height);
basicTextFieldUI.setRootView(null);
return baseLine;
}
@Override

43
core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyEditorPane.java

@ -27,6 +27,7 @@ import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.beans.PropertyChangeListener;
import java.lang.ref.WeakReference;
import javax.swing.*;
import javax.swing.event.DocumentListener;
@ -38,18 +39,18 @@ import javax.swing.text.Highlighter;
public class DummyEditorPane extends JEditorPane {
private JEditorPane editorPane;
private WeakReference<JEditorPane> editorPane;
private PropertyChangeListener propertyChangeListener;
public void setEditorPane(final JEditorPane editorPane) {
this.editorPane = editorPane;
if (editorPane != null) {
copyProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
copyProperty(JEditorPane.W3C_LENGTH_UNITS);
public void setEditorPane(final JEditorPane editor) {
this.editorPane = new WeakReference<>(editor);
if (editor != null) {
copyProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, editor);
copyProperty(JEditorPane.W3C_LENGTH_UNITS, editor);
}
}
protected void copyProperty(final String key) {
protected void copyProperty(final String key, final JEditorPane editorPane) {
putClientProperty(key, editorPane.getClientProperty(key));
}
@ -57,33 +58,43 @@ public class DummyEditorPane extends JEditorPane {
return propertyChangeListener;
}
private JEditorPane getEditor() {
return editorPane != null ? editorPane.get() : null;
}
@Override
public Document getDocument() {
if (editorPane == null) return super.getDocument();
return editorPane.getDocument();
JEditorPane editor = getEditor();
if (editor == null) return super.getDocument();
return editor.getDocument();
}
@Override
public Font getFont() {
if (editorPane == null) return null;
return editorPane.getFont();
JEditorPane editor = getEditor();
if (editor == null) return null;
return editor.getFont();
}
@Override
public Color getForeground() {
if (editorPane == null) return null;
return editorPane.getForeground();
JEditorPane editor = getEditor();
if (editor == null) return null;
return editor.getForeground();
}
@Override
public Color getBackground() {
if (editorPane == null) return null;
return editorPane.getBackground();
JEditorPane editor = getEditor();
if (editor == null) return null;
return editor.getBackground();
}
@Override
public boolean isEditable() {
return editorPane.isEditable();
JEditorPane editor = getEditor();
if (editor == null) return false;
return editor.isEditable();
}
@Override

9
core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyTextFieldUI.java

@ -21,20 +21,23 @@
*/
package com.github.weisj.darklaf.ui.text.dummy;
import java.lang.ref.WeakReference;
import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
public class DummyTextFieldUI extends BasicTextFieldUI {
private View rootView;
private WeakReference<View> rootView;
@Override
public View getRootView(final JTextComponent tc) {
return rootView != null ? rootView : super.getRootView(tc);
View view = rootView != null ? rootView.get() : null;
return view != null ? view : super.getRootView(tc);
}
public void setRootView(final View rootView) {
this.rootView = rootView;
this.rootView = new WeakReference<>(rootView);
}
}

5
utils/src/main/java/com/github/weisj/darklaf/util/value/WeakShared.java

@ -56,4 +56,9 @@ public class WeakShared<T> {
}
return value;
}
public T getIfPresent() {
WeakReference<T> ref = reference;
return ref != null ? ref.get() : null;
}
}

Loading…
Cancel
Save