diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingListener.java b/core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingListener.java new file mode 100644 index 00000000..45094b1b --- /dev/null +++ b/core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingListener.java @@ -0,0 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2020 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, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package com.github.weisj.darklaf.ui; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; + +import javax.swing.*; + +import com.github.weisj.darklaf.util.PropertyKey; +import com.github.weisj.darklaf.util.Types; + +public class VisualPaddingListener implements PropertyChangeListener { + + @Override + public void propertyChange(final PropertyChangeEvent evt) { + if (PropertyKey.BORDER.equals(evt.getPropertyName())) { + VisualPaddingProvider.updateProperty(Types.safeCast(evt.getSource(), JComponent.class)); + } + } +} diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingProvider.java b/core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingProvider.java index fafde850..e6ace401 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingProvider.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingProvider.java @@ -23,6 +23,26 @@ package com.github.weisj.darklaf.ui; import java.awt.*; +import javax.swing.*; +import javax.swing.border.Border; +import javax.swing.plaf.InsetsUIResource; + +import com.github.weisj.darklaf.util.PropertyUtil; + public interface VisualPaddingProvider { + + String VISUAL_PADDING_PROP = "visualPadding"; + Insets getVisualPaddings(Component component); + + static void updateProperty(final JComponent c) { + Border b = c.getBorder(); + if (b instanceof VisualPaddingProvider) { + Insets ins = ((VisualPaddingProvider) b).getVisualPaddings(c); + PropertyUtil.installProperty(c, VISUAL_PADDING_PROP, + new InsetsUIResource(ins.top, ins.left, ins.bottom, ins.right)); + } else { + PropertyUtil.uninstallProperty(c, VISUAL_PADDING_PROP); + } + } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java index 446e7679..b886b95a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java @@ -39,6 +39,7 @@ import com.github.weisj.darklaf.graphics.GraphicsContext; import com.github.weisj.darklaf.graphics.GraphicsUtil; import com.github.weisj.darklaf.graphics.PaintUtil; import com.github.weisj.darklaf.graphics.StringPainter; +import com.github.weisj.darklaf.ui.VisualPaddingListener; import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonFocusNavigationActions; import com.github.weisj.darklaf.ui.tooltip.ToolTipConstants; import com.github.weisj.darklaf.util.AlignmentExt; @@ -87,6 +88,7 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { protected int arc; protected int altArc; protected ToggleButtonFocusNavigationActions keyboardActions; + protected VisualPaddingListener visualPaddingListener; public static ComponentUI createUI(final JComponent c) { return new DarkButtonUI(); @@ -146,6 +148,8 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { super.installListeners(b); keyboardActions = new ToggleButtonFocusNavigationActions(b); keyboardActions.installActions(); + visualPaddingListener = new VisualPaddingListener(); + b.addPropertyChangeListener(visualPaddingListener); } @Override @@ -164,6 +168,8 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { super.uninstallListeners(b); keyboardActions.uninstallActions(); keyboardActions = null; + b.removePropertyChangeListener(visualPaddingListener); + visualPaddingListener = null; } protected void validateLayout() { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java index 0aad1da9..ea963da0 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java @@ -37,6 +37,7 @@ import javax.swing.text.JTextComponent; import com.github.weisj.darklaf.components.ArrowButton; import com.github.weisj.darklaf.ui.DividedWidgetPainter; +import com.github.weisj.darklaf.ui.VisualPaddingListener; import com.github.weisj.darklaf.ui.list.DarkDefaultListCellRenderer; import com.github.weisj.darklaf.ui.popupmenu.DarkPopupMenuUI; import com.github.weisj.darklaf.ui.text.DarkTextUI; @@ -51,6 +52,7 @@ import com.github.weisj.darklaf.util.PropertyUtil; public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants { protected DarkComboBoxListener comboBoxListener; + protected VisualPaddingListener visualPaddingListener; protected int arcSize; protected int borderSize; @@ -104,6 +106,8 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants comboBoxListener = createComboBoxListener(); comboBox.addMouseListener(comboBoxListener); comboBox.addPropertyChangeListener(comboBoxListener); + visualPaddingListener = new VisualPaddingListener(); + comboBox.addPropertyChangeListener(visualPaddingListener); } protected DarkComboBoxListener createComboBoxListener() { @@ -116,6 +120,8 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants comboBox.removeMouseListener(comboBoxListener); comboBox.removePropertyChangeListener(comboBoxListener); comboBoxListener = null; + comboBox.removePropertyChangeListener(visualPaddingListener); + visualPaddingListener = null; } public JComboBox getComboBox() { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java index c2aa1dad..538786f0 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java @@ -34,6 +34,7 @@ import javax.swing.text.JTextComponent; import com.github.weisj.darklaf.components.ArrowButton; import com.github.weisj.darklaf.ui.DividedWidgetPainter; +import com.github.weisj.darklaf.ui.VisualPaddingListener; import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyUtil; @@ -60,6 +61,7 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements SpinnerConstants { private JComponent editor; private JButton prevButton; private JComponent editorComponent; + private VisualPaddingListener visualPaddingListener; public static ComponentUI createUI(final JComponent c) { return new DarkSpinnerUI(); @@ -93,6 +95,8 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements SpinnerConstants { spinner.addMouseListener(spinnerListener); spinner.addMouseWheelListener(spinnerListener); spinner.addPropertyChangeListener(spinnerListener); + visualPaddingListener = new VisualPaddingListener(); + spinner.addPropertyChangeListener(visualPaddingListener); } protected DarkSpinnerListener createSpinnerListener() { @@ -109,6 +113,8 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements SpinnerConstants { spinner.removeMouseWheelListener(spinnerListener); spinner.removePropertyChangeListener(spinnerListener); spinnerListener = null; + spinner.removePropertyChangeListener(visualPaddingListener); + visualPaddingListener = null; } protected LayoutManager createLayout() { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java index f25f486c..46aafd7e 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java @@ -38,6 +38,7 @@ import com.github.weisj.darklaf.graphics.GraphicsUtil; import com.github.weisj.darklaf.graphics.PaintUtil; import com.github.weisj.darklaf.graphics.StringPainter; import com.github.weisj.darklaf.ui.OpacityBufferedUI; +import com.github.weisj.darklaf.ui.VisualPaddingListener; import com.github.weisj.darklaf.ui.cell.DarkCellBorder; import com.github.weisj.darklaf.ui.list.DarkListUI; import com.github.weisj.darklaf.ui.table.DarkTableUI; @@ -65,6 +66,7 @@ public abstract class DarkTextUI extends BasicTextUI implements OpacityBufferedU protected static final String TOGGLE_INSERT = "toggle_insert"; protected DarkTextListener textListener; + protected VisualPaddingListener visualPaddingListener; protected JTextComponent editor; protected DefaultTextRenderer defaultTextRenderer; @@ -183,6 +185,8 @@ public abstract class DarkTextUI extends BasicTextUI implements OpacityBufferedU textListener = createTextListener(); editor.addFocusListener(textListener); editor.addPropertyChangeListener(textListener); + visualPaddingListener = new VisualPaddingListener(); + editor.addPropertyChangeListener(visualPaddingListener); } protected DarkTextListener createTextListener() { @@ -195,6 +199,8 @@ public abstract class DarkTextUI extends BasicTextUI implements OpacityBufferedU editor.removeFocusListener(textListener); editor.removePropertyChangeListener(textListener); textListener = null; + editor.removePropertyChangeListener(visualPaddingListener); + visualPaddingListener = null; } protected Color getBackground(final JTextComponent c) { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/radiobutton/DarkRadioButtonUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/radiobutton/DarkRadioButtonUI.java index 9339304c..7e4f9f4e 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/radiobutton/DarkRadioButtonUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/radiobutton/DarkRadioButtonUI.java @@ -32,12 +32,12 @@ import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicButtonListener; import javax.swing.plaf.metal.MetalRadioButtonUI; -import com.github.weisj.darklaf.graphics.PaintUtil; import sun.swing.SwingUtilities2; import com.github.weisj.darklaf.graphics.GraphicsContext; import com.github.weisj.darklaf.graphics.GraphicsUtil; import com.github.weisj.darklaf.graphics.StringPainter; +import com.github.weisj.darklaf.ui.VisualPaddingListener; import com.github.weisj.darklaf.ui.togglebutton.DarkToggleButtonUI; import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonConstants; import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonFocusNavigationActions; @@ -58,6 +58,7 @@ public class DarkRadioButtonUI extends MetalRadioButtonUI implements PropertyCha private Icon stateIcon; protected BasicButtonListener buttonListener; protected ToggleButtonFocusNavigationActions keyboardAction; + protected VisualPaddingListener visualPaddingListener; public static ComponentUI createUI(final JComponent c) { return new DarkRadioButtonUI(); @@ -97,6 +98,8 @@ public class DarkRadioButtonUI extends MetalRadioButtonUI implements PropertyCha button.addPropertyChangeListener(this); keyboardAction = new ToggleButtonFocusNavigationActions(button); keyboardAction.installActions(); + visualPaddingListener = new VisualPaddingListener(); + button.addPropertyChangeListener(visualPaddingListener); } @Override @@ -121,6 +124,8 @@ public class DarkRadioButtonUI extends MetalRadioButtonUI implements PropertyCha button.removePropertyChangeListener(this); keyboardAction.uninstallActions(); keyboardAction = null; + button.removePropertyChangeListener(visualPaddingListener); + visualPaddingListener = null; } @Override diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java index bd2bc70f..50ff1691 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java @@ -67,8 +67,14 @@ public class PropertyUtil { } } + public static void uninstallProperty(final JComponent c, final String key) { + installProperty(c, key, null); + } + public static void installProperty(final JComponent c, final String key, final Object value) { - if (c.getClientProperty(key) == null) { + if (c == null) return; + Object current = c.getClientProperty(key); + if (current == null || current instanceof UIResource) { c.putClientProperty(key, value); } }