Browse Source

Supply visual padding hints to layout managers such as MigLayout through client properties.

pull/222/head
weisj 4 years ago
parent
commit
2faf4c2cb0
  1. 40
      core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingListener.java
  2. 20
      core/src/main/java/com/github/weisj/darklaf/ui/VisualPaddingProvider.java
  3. 6
      core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java
  4. 6
      core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java
  5. 6
      core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java
  6. 6
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java
  7. 7
      core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/radiobutton/DarkRadioButtonUI.java
  8. 8
      utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java

40
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));
}
}
}

20
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 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 { public interface VisualPaddingProvider {
String VISUAL_PADDING_PROP = "visualPadding";
Insets getVisualPaddings(Component component); 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);
}
}
} }

6
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.GraphicsUtil;
import com.github.weisj.darklaf.graphics.PaintUtil; import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.graphics.StringPainter; 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.togglebutton.ToggleButtonFocusNavigationActions;
import com.github.weisj.darklaf.ui.tooltip.ToolTipConstants; import com.github.weisj.darklaf.ui.tooltip.ToolTipConstants;
import com.github.weisj.darklaf.util.AlignmentExt; import com.github.weisj.darklaf.util.AlignmentExt;
@ -87,6 +88,7 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
protected int arc; protected int arc;
protected int altArc; protected int altArc;
protected ToggleButtonFocusNavigationActions keyboardActions; protected ToggleButtonFocusNavigationActions keyboardActions;
protected VisualPaddingListener visualPaddingListener;
public static ComponentUI createUI(final JComponent c) { public static ComponentUI createUI(final JComponent c) {
return new DarkButtonUI(); return new DarkButtonUI();
@ -146,6 +148,8 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
super.installListeners(b); super.installListeners(b);
keyboardActions = new ToggleButtonFocusNavigationActions(b); keyboardActions = new ToggleButtonFocusNavigationActions(b);
keyboardActions.installActions(); keyboardActions.installActions();
visualPaddingListener = new VisualPaddingListener();
b.addPropertyChangeListener(visualPaddingListener);
} }
@Override @Override
@ -164,6 +168,8 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
super.uninstallListeners(b); super.uninstallListeners(b);
keyboardActions.uninstallActions(); keyboardActions.uninstallActions();
keyboardActions = null; keyboardActions = null;
b.removePropertyChangeListener(visualPaddingListener);
visualPaddingListener = null;
} }
protected void validateLayout() { protected void validateLayout() {

6
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.components.ArrowButton;
import com.github.weisj.darklaf.ui.DividedWidgetPainter; 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.list.DarkDefaultListCellRenderer;
import com.github.weisj.darklaf.ui.popupmenu.DarkPopupMenuUI; import com.github.weisj.darklaf.ui.popupmenu.DarkPopupMenuUI;
import com.github.weisj.darklaf.ui.text.DarkTextUI; 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 { public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants {
protected DarkComboBoxListener comboBoxListener; protected DarkComboBoxListener comboBoxListener;
protected VisualPaddingListener visualPaddingListener;
protected int arcSize; protected int arcSize;
protected int borderSize; protected int borderSize;
@ -104,6 +106,8 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants
comboBoxListener = createComboBoxListener(); comboBoxListener = createComboBoxListener();
comboBox.addMouseListener(comboBoxListener); comboBox.addMouseListener(comboBoxListener);
comboBox.addPropertyChangeListener(comboBoxListener); comboBox.addPropertyChangeListener(comboBoxListener);
visualPaddingListener = new VisualPaddingListener();
comboBox.addPropertyChangeListener(visualPaddingListener);
} }
protected DarkComboBoxListener createComboBoxListener() { protected DarkComboBoxListener createComboBoxListener() {
@ -116,6 +120,8 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants
comboBox.removeMouseListener(comboBoxListener); comboBox.removeMouseListener(comboBoxListener);
comboBox.removePropertyChangeListener(comboBoxListener); comboBox.removePropertyChangeListener(comboBoxListener);
comboBoxListener = null; comboBoxListener = null;
comboBox.removePropertyChangeListener(visualPaddingListener);
visualPaddingListener = null;
} }
public JComboBox<?> getComboBox() { public JComboBox<?> getComboBox() {

6
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.components.ArrowButton;
import com.github.weisj.darklaf.ui.DividedWidgetPainter; 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.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil; import com.github.weisj.darklaf.util.PropertyUtil;
@ -60,6 +61,7 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements SpinnerConstants {
private JComponent editor; private JComponent editor;
private JButton prevButton; private JButton prevButton;
private JComponent editorComponent; private JComponent editorComponent;
private VisualPaddingListener visualPaddingListener;
public static ComponentUI createUI(final JComponent c) { public static ComponentUI createUI(final JComponent c) {
return new DarkSpinnerUI(); return new DarkSpinnerUI();
@ -93,6 +95,8 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements SpinnerConstants {
spinner.addMouseListener(spinnerListener); spinner.addMouseListener(spinnerListener);
spinner.addMouseWheelListener(spinnerListener); spinner.addMouseWheelListener(spinnerListener);
spinner.addPropertyChangeListener(spinnerListener); spinner.addPropertyChangeListener(spinnerListener);
visualPaddingListener = new VisualPaddingListener();
spinner.addPropertyChangeListener(visualPaddingListener);
} }
protected DarkSpinnerListener createSpinnerListener() { protected DarkSpinnerListener createSpinnerListener() {
@ -109,6 +113,8 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements SpinnerConstants {
spinner.removeMouseWheelListener(spinnerListener); spinner.removeMouseWheelListener(spinnerListener);
spinner.removePropertyChangeListener(spinnerListener); spinner.removePropertyChangeListener(spinnerListener);
spinnerListener = null; spinnerListener = null;
spinner.removePropertyChangeListener(visualPaddingListener);
visualPaddingListener = null;
} }
protected LayoutManager createLayout() { protected LayoutManager createLayout() {

6
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.PaintUtil;
import com.github.weisj.darklaf.graphics.StringPainter; import com.github.weisj.darklaf.graphics.StringPainter;
import com.github.weisj.darklaf.ui.OpacityBufferedUI; 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.cell.DarkCellBorder;
import com.github.weisj.darklaf.ui.list.DarkListUI; import com.github.weisj.darklaf.ui.list.DarkListUI;
import com.github.weisj.darklaf.ui.table.DarkTableUI; 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 static final String TOGGLE_INSERT = "toggle_insert";
protected DarkTextListener textListener; protected DarkTextListener textListener;
protected VisualPaddingListener visualPaddingListener;
protected JTextComponent editor; protected JTextComponent editor;
protected DefaultTextRenderer defaultTextRenderer; protected DefaultTextRenderer defaultTextRenderer;
@ -183,6 +185,8 @@ public abstract class DarkTextUI extends BasicTextUI implements OpacityBufferedU
textListener = createTextListener(); textListener = createTextListener();
editor.addFocusListener(textListener); editor.addFocusListener(textListener);
editor.addPropertyChangeListener(textListener); editor.addPropertyChangeListener(textListener);
visualPaddingListener = new VisualPaddingListener();
editor.addPropertyChangeListener(visualPaddingListener);
} }
protected DarkTextListener createTextListener() { protected DarkTextListener createTextListener() {
@ -195,6 +199,8 @@ public abstract class DarkTextUI extends BasicTextUI implements OpacityBufferedU
editor.removeFocusListener(textListener); editor.removeFocusListener(textListener);
editor.removePropertyChangeListener(textListener); editor.removePropertyChangeListener(textListener);
textListener = null; textListener = null;
editor.removePropertyChangeListener(visualPaddingListener);
visualPaddingListener = null;
} }
protected Color getBackground(final JTextComponent c) { protected Color getBackground(final JTextComponent c) {

7
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.basic.BasicButtonListener;
import javax.swing.plaf.metal.MetalRadioButtonUI; import javax.swing.plaf.metal.MetalRadioButtonUI;
import com.github.weisj.darklaf.graphics.PaintUtil;
import sun.swing.SwingUtilities2; import sun.swing.SwingUtilities2;
import com.github.weisj.darklaf.graphics.GraphicsContext; import com.github.weisj.darklaf.graphics.GraphicsContext;
import com.github.weisj.darklaf.graphics.GraphicsUtil; import com.github.weisj.darklaf.graphics.GraphicsUtil;
import com.github.weisj.darklaf.graphics.StringPainter; 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.DarkToggleButtonUI;
import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonConstants; import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonConstants;
import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonFocusNavigationActions; import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonFocusNavigationActions;
@ -58,6 +58,7 @@ public class DarkRadioButtonUI extends MetalRadioButtonUI implements PropertyCha
private Icon stateIcon; private Icon stateIcon;
protected BasicButtonListener buttonListener; protected BasicButtonListener buttonListener;
protected ToggleButtonFocusNavigationActions keyboardAction; protected ToggleButtonFocusNavigationActions keyboardAction;
protected VisualPaddingListener visualPaddingListener;
public static ComponentUI createUI(final JComponent c) { public static ComponentUI createUI(final JComponent c) {
return new DarkRadioButtonUI(); return new DarkRadioButtonUI();
@ -97,6 +98,8 @@ public class DarkRadioButtonUI extends MetalRadioButtonUI implements PropertyCha
button.addPropertyChangeListener(this); button.addPropertyChangeListener(this);
keyboardAction = new ToggleButtonFocusNavigationActions(button); keyboardAction = new ToggleButtonFocusNavigationActions(button);
keyboardAction.installActions(); keyboardAction.installActions();
visualPaddingListener = new VisualPaddingListener();
button.addPropertyChangeListener(visualPaddingListener);
} }
@Override @Override
@ -121,6 +124,8 @@ public class DarkRadioButtonUI extends MetalRadioButtonUI implements PropertyCha
button.removePropertyChangeListener(this); button.removePropertyChangeListener(this);
keyboardAction.uninstallActions(); keyboardAction.uninstallActions();
keyboardAction = null; keyboardAction = null;
button.removePropertyChangeListener(visualPaddingListener);
visualPaddingListener = null;
} }
@Override @Override

8
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) { 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); c.putClientProperty(key, value);
} }
} }

Loading…
Cancel
Save