From a434cac90d7d96b2f0e686f0729131ba79a919b8 Mon Sep 17 00:00:00 2001 From: weisj Date: Thu, 4 Jun 2020 10:44:41 +0200 Subject: [PATCH] Broadened scope of JButton-properties that JToggleButton can use. Provides a more consistent api between the two components. Added option to disable button background. Fixed editor of combobox being cut off. --- .../components/ClosableTabComponent.java | 4 +- .../darklaf/ui/button/ButtonConstants.java | 11 +- .../darklaf/ui/button/DarkButtonBorder.java | 2 +- .../weisj/darklaf/ui/button/DarkButtonUI.java | 53 +++--- .../ui/combobox/DarkComboBoxBorder.java | 2 +- .../darklaf/ui/combobox/DarkComboBoxUI.java | 3 +- .../ui/splitpane/DarkSplitPaneDivider.java | 2 +- .../darklaf/ui/tabbedpane/MoreTabsButton.java | 2 +- .../darklaf/ui/text/DarkTextFieldUI.java | 2 +- .../ui/togglebutton/DarkToggleButtonUI.java | 21 ++- .../github/weisj/darklaf/util/DarkUIUtil.java | 16 ++ core/src/test/java/DemoLauncher.java | 2 + .../java/ui/button/AbstractButtonDemo.java | 170 ++++++++++++++++++ core/src/test/java/ui/button/ButtonDemo.java | 124 +------------ .../test/java/ui/button/ToggleButtonDemo.java | 52 +----- .../platform/windows/ui/WindowsTitlePane.java | 5 +- 16 files changed, 270 insertions(+), 201 deletions(-) create mode 100644 core/src/test/java/ui/button/AbstractButtonDemo.java diff --git a/core/src/main/java/com/github/weisj/darklaf/components/ClosableTabComponent.java b/core/src/main/java/com/github/weisj/darklaf/components/ClosableTabComponent.java index 943cc55e..7730397c 100644 --- a/core/src/main/java/com/github/weisj/darklaf/components/ClosableTabComponent.java +++ b/core/src/main/java/com/github/weisj/darklaf/components/ClosableTabComponent.java @@ -81,10 +81,12 @@ public class ClosableTabComponent extends JPanel { protected TabButton(final ClosableTabComponent tabComponent) { this.tabComponent = tabComponent; - putClientProperty(DarkButtonUI.KEY_VARIANT, DarkButtonUI.VARIANT_ONLY_LABEL); + putClientProperty(DarkButtonUI.KEY_NO_BACKGROUND, true); putClientProperty(DarkButtonUI.KEY_NO_BORDERLESS_OVERWRITE, true); + putClientProperty(DarkButtonUI.KEY_VARIANT, DarkButtonUI.VARIANT_BORDERLESS_RECTANGULAR); setOpaque(false); setRolloverEnabled(true); + setBorderPainted(false); setIcon(UIManager.getIcon("TabbedPane.tabCloseIcon")); setRolloverIcon(UIManager.getIcon("TabbedPane.tabCloseHoverIcon")); addActionListener(this); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/button/ButtonConstants.java b/core/src/main/java/com/github/weisj/darklaf/ui/button/ButtonConstants.java index 78bb0b2b..6ca0ac0c 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/button/ButtonConstants.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/button/ButtonConstants.java @@ -42,6 +42,7 @@ public interface ButtonConstants { String KEY_NO_BORDERLESS_OVERWRITE = "JButton.noBorderlessOverwrite"; String KEY_CORNER = "JButton.cornerFlag"; String KEY_ROUND = "JButton.round"; + String KEY_NO_BACKGROUND = "JButton.noBackground"; String KEY_LEFT_NEIGHBOUR = "JButton.leftNeighbour"; String KEY_RIGHT_NEIGHBOUR = "JButton.rightNeighbour"; @@ -52,7 +53,6 @@ public interface ButtonConstants { String KEY_BOTTOM_LEFT_NEIGHBOUR = "JButton.bottomLeftNeighbour"; String KEY_BOTTOM_RIGHT_NEIGHBOUR = "JButton.bottomRightNeighbour"; - String VARIANT_ONLY_LABEL = "onlyLabel"; String VARIANT_BORDERLESS_RECTANGULAR = "borderlessRectangular"; String VARIANT_BORDERLESS = "borderless"; String VARIANT_NONE = "none"; @@ -98,7 +98,7 @@ public interface ButtonConstants { static boolean isBorderlessVariant(final Component c) { if (isBorderlessRectangular(c)) return true; - if (c instanceof JButton) { + if (c instanceof JButton || c instanceof JToggleButton) { return PropertyUtil.isPropertyEqual(c, KEY_VARIANT, VARIANT_BORDERLESS) || doConvertToBorderless((AbstractButton) c); } @@ -110,7 +110,8 @@ public interface ButtonConstants { } static boolean doConvertToBorderless(final AbstractButton b) { - return isIconOnly(b) && !b.isFocusable() && convertIconButtonToBorderless(b) && (b instanceof JButton); + return isIconOnly(b) && !b.isFocusable() && convertIconButtonToBorderless(b) + && (b instanceof JButton || b instanceof JToggleButton); } static boolean convertIconButtonToBorderless(final AbstractButton b) { @@ -130,4 +131,8 @@ public interface ButtonConstants { static JComponent getNeighbour(final String key, final Component comp) { return PropertyUtil.getObject(comp, key, JComponent.class); } + + static boolean isNoBackground(final AbstractButton b) { + return PropertyUtil.getBooleanProperty(b, KEY_NO_BACKGROUND); + } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonBorder.java b/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonBorder.java index d014b2ba..fad27c8c 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonBorder.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonBorder.java @@ -281,7 +281,7 @@ public class DarkButtonBorder implements Border, UIResource { protected Color getBorderColor(final Component c, final boolean focus) { if (focus) { return focusBorderColor; - } else if (c instanceof JButton && ((JButton) c).isDefaultButton() && c.isEnabled()) { + } else if (c instanceof JComponent && ButtonConstants.isDefaultButton((JComponent) c) && c.isEnabled()) { return defaultBorderColor; } else if (c.isEnabled()) { return borderColor; 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 a0e4902b..fa874b4a 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 @@ -36,6 +36,8 @@ import javax.swing.plaf.basic.BasicButtonListener; import javax.swing.plaf.basic.BasicButtonUI; import javax.swing.plaf.basic.BasicGraphicsUtils; +import sun.swing.SwingUtilities2; + import com.github.weisj.darklaf.components.tooltip.ToolTipStyle; import com.github.weisj.darklaf.delegate.AbstractButtonLayoutDelegate; import com.github.weisj.darklaf.graphics.GraphicsContext; @@ -49,8 +51,6 @@ import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyUtil; -import sun.swing.SwingUtilities2; - /** * @author Jannis Weis */ @@ -59,6 +59,7 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { protected static final Rectangle viewRect = new Rectangle(); protected static final Rectangle textRect = new Rectangle(); protected static final Rectangle iconRect = new Rectangle(); + protected static final RoundRectangle2D hitArea = new RoundRectangle2D.Float(); protected int borderSize; protected int shadowHeight; protected boolean drawOutline; @@ -154,7 +155,9 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { public void paint(final Graphics g, final JComponent c) { GraphicsContext config = new GraphicsContext(g); AbstractButton b = (AbstractButton) c; - paintButtonBackground(g, c); + if (!ButtonConstants.isNoBackground(b)) { + paintButtonBackground(g, c); + } Font font = g.getFont(); if (ButtonConstants.isDefaultButton(b) && !font.isBold()) { @@ -237,42 +240,45 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { protected void paintBorderlessBackground(final AbstractButton b, final Graphics2D g, final int arc, final int width, final int height, final Insets margin) { - if (b.isEnabled() && b.getModel().isRollover()) { + if (isRolloverBorderless(b)) { GraphicsUtil.setupAAPainting(g); g.setColor(getBorderlessBackground(b)); + boolean borderlessRectangular = ButtonConstants.isBorderlessRectangular(b); + if (!borderlessRectangular) DarkUIUtil.addInsets(margin, borderSize); if (ButtonConstants.isBorderlessRectangular(b)) { g.fillRect(margin.left, margin.top, width - margin.left - margin.right, height - margin.top - margin.bottom); - PaintUtil.drawRect(g, margin.left, margin.top, width - margin.left - margin.right, - height - margin.top - margin.bottom, 1); } else if (ButtonConstants.doConvertToBorderless(b)) { int size = Math.min(width - margin.left - margin.right, height - margin.left - margin.right); if (!drawOutline) { - g.fillRoundRect((width - size) / 2 + 2, (height - size) / 2 + 2, - size - 4, size - 4, arc, arc); + g.fillRoundRect((width - size) / 2, (height - size) / 2, size, size, arc, arc); } else { g.setColor(getBorderlessOutline(b)); - g.drawRoundRect((width - size) / 2 + 2, (height - size) / 2 + 2, - size - 4, size - 4, arc, arc); + PaintUtil.paintLineBorder(g, (width - size) / 2.0f, + (height - size) / 2.0f, size, size, arc); } } else { if (!drawOutline) { - g.fillRoundRect(margin.left + 2, margin.top + 2, - width - margin.left - margin.right - 4, - height - margin.top - margin.bottom - 4, + g.fillRoundRect(margin.left, margin.top, + width - margin.left - margin.right, + height - margin.top - margin.bottom, arc, arc); } else { g.setColor(getBorderlessOutline(b)); - g.drawRoundRect(margin.left + 2, margin.top + 2, - width - margin.left - margin.right - 4, - height - margin.top - margin.bottom - 4, - arc, arc); + PaintUtil.paintLineBorder(g, margin.left, margin.top, + width - margin.left - margin.right, + height - margin.top - margin.bottom, + arc); } } } } + protected boolean isRolloverBorderless(final AbstractButton b) { + return b.isEnabled() && b.getModel().isRollover(); + } + protected void paintText(final Graphics g, final AbstractButton b, final String text) { ButtonModel model = b.getModel(); g.setColor(getForeground(b)); @@ -354,11 +360,14 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { } protected Color getBorderlessBackground(final AbstractButton c) { - boolean armed = c.getModel().isArmed(); - return armed ? PropertyUtil.getColor(c, KEY_CLICK_COLOR, borderlessClick) + return isArmedBorderless(c) ? PropertyUtil.getColor(c, KEY_CLICK_COLOR, borderlessClick) : PropertyUtil.getColor(c, KEY_HOVER_COLOR, borderlessHover); } + protected boolean isArmedBorderless(final AbstractButton b) { + return b.getModel().isArmed(); + } + protected Color getBorderlessOutline(final AbstractButton c) { boolean armed = c.getModel().isArmed(); return armed ? borderlessOutlineClick : borderlessOutlineHover; @@ -417,9 +426,9 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants { return super.contains(c, x, y); } if (!(x >= 0 && x <= c.getWidth() && y >= 0 && y <= c.getHeight())) return false; - int bs = borderSize; + int bs = c.getBorder() instanceof DarkButtonBorder ? borderSize : 0; int arc = getArc(c); - return new RoundRectangle2D.Float(bs, bs, c.getWidth() - 2 * bs, c.getHeight() - 2 * bs, - arc, arc).contains(x, y); + hitArea.setRoundRect(bs, bs, c.getWidth() - 2 * bs, c.getHeight() - 2 * bs, arc, arc); + return hitArea.contains(x, y); } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxBorder.java b/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxBorder.java index c39ba888..b524ab01 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxBorder.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxBorder.java @@ -95,7 +95,7 @@ public class DarkComboBoxBorder implements Border, UIResource { if (comboBox.isEditable()) { Rectangle arrowBounds = arrowButton.getBounds(); boolean leftToRight = comboBox.getComponentOrientation().isLeftToRight(); - int off = leftToRight ? arrowBounds.x : arrowBounds.x + arrowBounds.width; + int off = leftToRight ? arrowBounds.x : arrowBounds.x + arrowBounds.width - 1; g.setColor(borderColor); g.fillRect(off, editBSize, 1, height - 2 * editBSize); } 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 63ff937f..ec91910b 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 @@ -324,8 +324,7 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants rect.x += boxPadding.left; rect.width -= boxPadding.left; } else { - rect.width -= borderSize + 1; - rect.x += 1; + rect.width -= boxPadding.right - borderSize; } } return rect; diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/splitpane/DarkSplitPaneDivider.java b/core/src/main/java/com/github/weisj/darklaf/ui/splitpane/DarkSplitPaneDivider.java index 1391b6a6..7db8494a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/splitpane/DarkSplitPaneDivider.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/splitpane/DarkSplitPaneDivider.java @@ -120,9 +120,9 @@ public class DarkSplitPaneDivider extends BasicSplitPaneDivider { protected static class OneTouchButton extends JButton implements UIResource { protected OneTouchButton() { - putClientProperty(DarkButtonUI.KEY_VARIANT, DarkButtonUI.VARIANT_ONLY_LABEL); setMinimumSize(new Dimension(ONE_TOUCH_SIZE, ONE_TOUCH_SIZE)); setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); + putClientProperty(DarkButtonUI.KEY_NO_BACKGROUND, true); setRequestFocusEnabled(false); setBorderPainted(false); setFocusPainted(false); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java index b57a4673..b5e3ecd4 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java @@ -46,7 +46,7 @@ public class MoreTabsButton extends DarkTabAreaButton { icon = ui.getMoreTabsIcon(); pad = UIManager.getInt("TabbedPane.moreTabsButton.pad"); setIcon(EmptyIcon.create(icon.getIconWidth(), icon.getIconHeight())); - putClientProperty(DarkButtonUI.KEY_VARIANT, DarkButtonUI.VARIANT_ONLY_LABEL); + putClientProperty(DarkButtonUI.KEY_NO_BACKGROUND, true); putClientProperty(DarkButtonUI.KEY_SQUARE, true); setFocusable(false); setFont(getFont().deriveFont(8f)); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java index 88a3639e..497466eb 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java @@ -154,7 +154,7 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh protected void adjustTextRect(final JTextComponent c, final Rectangle r) { boolean ltr = c.getComponentOrientation().isLeftToRight(); - if (!isInCell(c)) DarkUIUtil.applyInsets(r, padding); + if (!isInCell(c) && getBorder(c) != null) DarkUIUtil.applyInsets(r, padding); if (doPaintLeftIcon(c)) { int w = getLeftIcon(c).getIconWidth() + padding.left; if (ltr) r.x += w; diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java index 8b54f413..fdad7384 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java @@ -25,7 +25,6 @@ package com.github.weisj.darklaf.ui.togglebutton; import java.awt.*; -import java.awt.geom.RoundRectangle2D; import javax.swing.*; import javax.swing.plaf.ComponentUI; @@ -37,6 +36,7 @@ import sun.swing.SwingUtilities2; 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.ui.button.ButtonConstants; import com.github.weisj.darklaf.ui.button.DarkButtonUI; /** @@ -132,7 +132,9 @@ public class DarkToggleButtonUI extends DarkButtonUI implements ToggleButtonCons @Override protected Color getForeground(final AbstractButton button) { - if (button.isSelected() && !ToggleButtonConstants.isSlider(button) + if (button.isSelected() + && !ToggleButtonConstants.isSlider(button) + && !ButtonConstants.isBorderlessVariant(button) && button.getForeground() instanceof UIResource) { return selectedForeground; } @@ -162,6 +164,16 @@ public class DarkToggleButtonUI extends DarkButtonUI implements ToggleButtonCons } } + @Override + protected boolean isRolloverBorderless(final AbstractButton b) { + return super.isRolloverBorderless(b) || b.isSelected(); + } + + @Override + protected boolean isArmedBorderless(final AbstractButton b) { + return super.isArmedBorderless(b) || b.isSelected(); + } + protected Color getToggleBorderColor(final AbstractButton b) { if (b.hasFocus()) { return focusBorderColor; @@ -230,7 +242,8 @@ public class DarkToggleButtonUI extends DarkButtonUI implements ToggleButtonCons if (!ToggleButtonConstants.isSlider(c)) return super.contains(c, x, y); if (!(x >= 0 && x <= c.getWidth() && y >= 0 && y <= c.getHeight())) return false; Rectangle bounds = getSliderBounds(c); - return new RoundRectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height, - bounds.height, bounds.height).contains(x, y); + int arc = Math.min(bounds.width, bounds.height); + hitArea.setRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arc, arc); + return hitArea.contains(x, y); } } diff --git a/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java b/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java index af39f7c0..e07fbec8 100644 --- a/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java +++ b/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java @@ -81,6 +81,22 @@ public final class DarkUIUtil { } } + public static Insets addInsets(final Insets ins1, final int extra) { + ins1.left += extra; + ins1.right += extra; + ins1.top += extra; + ins1.bottom += extra; + return ins1; + } + + public static Insets addInsets(final Insets ins1, final int extra, final boolean createNew) { + if (createNew) { + return addInsets(addInsets(new Insets(0, 0, 0, 0), ins1), extra); + } else { + return addInsets(ins1, extra); + } + } + public static Insets addInsets(final Insets ins1, final Insets ins2) { if (ins2 == null) return ins1; if (ins1 != null) { diff --git a/core/src/test/java/DemoLauncher.java b/core/src/test/java/DemoLauncher.java index a7773337..bac075f6 100644 --- a/core/src/test/java/DemoLauncher.java +++ b/core/src/test/java/DemoLauncher.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; import java.net.URISyntaxException; import java.net.URL; import java.util.*; @@ -74,6 +75,7 @@ public class DemoLauncher implements ComponentDemo { demoClasses = getClasses("ui", "icon", "defaults").filter(demoType::isAssignableFrom) .filter(cls -> !cls.equals(DemoEntry.class)) .filter(cls -> !cls.isInterface()) + .filter(cls -> !Modifier.isAbstract(cls.getModifiers())) .map(this::getInstance) .filter(Objects::nonNull) .map(demoType::cast) diff --git a/core/src/test/java/ui/button/AbstractButtonDemo.java b/core/src/test/java/ui/button/AbstractButtonDemo.java new file mode 100644 index 00000000..ef26640e --- /dev/null +++ b/core/src/test/java/ui/button/AbstractButtonDemo.java @@ -0,0 +1,170 @@ +/* + * 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 ui.button; + +import java.awt.*; + +import javax.swing.*; + +import ui.ComponentDemo; +import ui.DemoPanel; + +import com.github.weisj.darklaf.components.color.QuickColorChooser; +import com.github.weisj.darklaf.ui.button.DarkButtonUI; +import com.github.weisj.darklaf.util.AlignmentExt; + +public abstract class AbstractButtonDemo implements ComponentDemo { + + @Override + public JComponent createComponent() { + T button = createButton(); + DemoPanel panel = new DemoPanel(button); + addControls(panel, button); + return panel; + } + + protected void addCheckBoxControls(final JPanel controlPanel, final T button) {} + + protected abstract T createButton(); + + protected void addControls(final DemoPanel panel, final T button) { + JPanel controlPanel = panel.addControls(); + controlPanel.add(new JCheckBox("enabled") { + { + setSelected(button.isEnabled()); + addActionListener(e -> button.setEnabled(isSelected())); + } + }); + controlPanel.add(new JCheckBox("focusable") { + { + setSelected(button.isFocusable()); + addActionListener(e -> button.setFocusable(isSelected())); + } + }); + controlPanel.add(new JCheckBox("LeftToRight") { + { + setSelected(button.getComponentOrientation().isLeftToRight()); + addActionListener(e -> button.setComponentOrientation(isSelected() ? ComponentOrientation.LEFT_TO_RIGHT + : ComponentOrientation.RIGHT_TO_LEFT)); + } + }); + controlPanel.add(new JCheckBox("Rollover") { + { + setSelected(button.isRolloverEnabled()); + addActionListener(e -> button.setRolloverEnabled(isSelected())); + } + }); + controlPanel.add(new JCheckBox(DarkButtonUI.KEY_SQUARE) { + { + setSelected(false); + addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_SQUARE, isSelected())); + } + }); + controlPanel.add(new JCheckBox(DarkButtonUI.KEY_ROUND) { + { + setSelected(false); + addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_ROUND, isSelected())); + } + }); + controlPanel.add(new JCheckBox(DarkButtonUI.KEY_THIN) { + { + setSelected(false); + addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_THIN, isSelected())); + } + }); + controlPanel.add(new JCheckBox(DarkButtonUI.KEY_ALT_ARC) { + { + setSelected(false); + addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_ALT_ARC, isSelected())); + } + }); + controlPanel.add(new JCheckBox(DarkButtonUI.KEY_NO_BACKGROUND) { + { + setSelected(false); + addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_NO_BACKGROUND, isSelected())); + } + }); + controlPanel.add(new JCheckBox("Button.defaultButtonFollowsFocus") { + { + setSelected(UIManager.getBoolean("Button.defaultButtonFollowsFocus")); + addActionListener(e -> UIManager.put("Button.defaultButtonFollowsFocus", isSelected())); + } + }); + addCheckBoxControls(controlPanel, button); + + controlPanel = panel.addControls(); + controlPanel.add(new JCheckBox("Text enabled") { + { + setSelected(true); + addActionListener(e -> button.setText(isSelected() ? "Test Button" : null)); + } + }); + controlPanel.add(new JCheckBox("Icon enabled") { + final Icon icon = button.getIcon(); + { + setEnabled(icon != null); + setSelected(icon != null); + addActionListener(e -> button.setIcon(isSelected() ? icon : null)); + } + }); + + controlPanel = panel.addControls(); + controlPanel.add(new QuickColorChooser(DarkButtonUI.KEY_HOVER_COLOR, Color.BLACK, + (b, c) -> button.putClientProperty(DarkButtonUI.KEY_HOVER_COLOR, + b ? c : null))); + controlPanel.add(new QuickColorChooser(DarkButtonUI.KEY_HOVER_COLOR, Color.BLACK, + (b, c) -> button.putClientProperty(DarkButtonUI.KEY_CLICK_COLOR, + b ? c : null))); + + controlPanel = panel.addControls(); + controlPanel.add(new JLabel(DarkButtonUI.KEY_VARIANT + ":")); + controlPanel.add(new JComboBox() { + { + addItem(DarkButtonUI.VARIANT_NONE); + addItem(DarkButtonUI.VARIANT_BORDERLESS); + addItem(DarkButtonUI.VARIANT_BORDERLESS_RECTANGULAR); + setSelectedItem(DarkButtonUI.VARIANT_NONE); + addItemListener(e -> button.putClientProperty(DarkButtonUI.KEY_VARIANT, e.getItem())); + } + }); + controlPanel.add(new JLabel(DarkButtonUI.KEY_CORNER + ":")); + controlPanel.add(new JComboBox() { + { + addItem("None"); + for (AlignmentExt a : AlignmentExt.values()) { + addItem(a.name()); + } + setSelectedItem("None"); + addItemListener(e -> { + if ("None".equals(e.getItem())) { + button.putClientProperty(DarkButtonUI.KEY_CORNER, null); + } else { + button.putClientProperty(DarkButtonUI.KEY_CORNER, AlignmentExt.valueOf(e.getItem().toString())); + } + }); + } + }); + } +} diff --git a/core/src/test/java/ui/button/ButtonDemo.java b/core/src/test/java/ui/button/ButtonDemo.java index 9b625600..5f922074 100644 --- a/core/src/test/java/ui/button/ButtonDemo.java +++ b/core/src/test/java/ui/button/ButtonDemo.java @@ -29,40 +29,24 @@ import java.awt.*; import javax.swing.*; import ui.ComponentDemo; -import ui.DemoPanel; import ui.DemoResources; -import com.github.weisj.darklaf.components.color.QuickColorChooser; -import com.github.weisj.darklaf.ui.button.DarkButtonUI; -import com.github.weisj.darklaf.util.AlignmentExt; - -public class ButtonDemo implements ComponentDemo { +public class ButtonDemo extends AbstractButtonDemo { public static void main(final String[] args) { ComponentDemo.showDemo(new ButtonDemo()); } @Override - public JComponent createComponent() { + protected JButton createButton() { Icon icon = DemoResources.FOLDER_ICON; JButton button = new JButton("Test Button", icon); - DemoPanel panel = new DemoPanel(button); - button.setToolTipText("TipText"); + return button; + } - JPanel controlPanel = panel.addControls(); - controlPanel.add(new JCheckBox("enabled") { - { - setSelected(button.isEnabled()); - addActionListener(e -> button.setEnabled(isSelected())); - } - }); - controlPanel.add(new JCheckBox("focusable") { - { - setSelected(button.isFocusable()); - addActionListener(e -> button.setFocusable(isSelected())); - } - }); + @Override + protected void addCheckBoxControls(final JPanel controlPanel, final JButton button) { controlPanel.add(new JCheckBox("default") { { setSelected(button.isDefaultButton()); @@ -70,102 +54,6 @@ public class ButtonDemo implements ComponentDemo { .setDefaultButton(isSelected() ? button : null)); } }); - controlPanel.add(new JCheckBox("LeftToRight") { - { - setSelected(button.getComponentOrientation().isLeftToRight()); - addActionListener(e -> button.setComponentOrientation(isSelected() ? ComponentOrientation.LEFT_TO_RIGHT - : ComponentOrientation.RIGHT_TO_LEFT)); - } - }); - controlPanel.add(new JCheckBox("Rollover") { - { - setSelected(button.isRolloverEnabled()); - addActionListener(e -> button.setRolloverEnabled(isSelected())); - } - }); - controlPanel.add(new JCheckBox(DarkButtonUI.KEY_SQUARE) { - { - setSelected(false); - addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_SQUARE, isSelected())); - } - }); - controlPanel.add(new JCheckBox(DarkButtonUI.KEY_ROUND) { - { - setSelected(false); - addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_ROUND, isSelected())); - } - }); - controlPanel.add(new JCheckBox(DarkButtonUI.KEY_THIN) { - { - setSelected(false); - addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_THIN, isSelected())); - } - }); - controlPanel.add(new JCheckBox(DarkButtonUI.KEY_ALT_ARC) { - { - setSelected(false); - addActionListener(e -> button.putClientProperty(DarkButtonUI.KEY_ALT_ARC, isSelected())); - } - }); - controlPanel.add(new JCheckBox("Button.defaultButtonFollowsFocus") { - { - setSelected(UIManager.getBoolean("Button.defaultButtonFollowsFocus")); - addActionListener(e -> UIManager.put("Button.defaultButtonFollowsFocus", isSelected())); - } - }); - - controlPanel = panel.addControls(); - controlPanel.add(new JCheckBox("Text enabled") { - { - setSelected(true); - addActionListener(e -> button.setText(isSelected() ? "Test Button" : null)); - } - }); - controlPanel.add(new JCheckBox("Icon enabled") { - { - setSelected(true); - addActionListener(e -> button.setIcon(isSelected() ? icon : null)); - } - }); - - controlPanel = panel.addControls(); - controlPanel.add(new QuickColorChooser(DarkButtonUI.KEY_HOVER_COLOR, Color.BLACK, - (b, c) -> button.putClientProperty(DarkButtonUI.KEY_HOVER_COLOR, - b ? c : null))); - controlPanel.add(new QuickColorChooser(DarkButtonUI.KEY_HOVER_COLOR, Color.BLACK, - (b, c) -> button.putClientProperty(DarkButtonUI.KEY_CLICK_COLOR, - b ? c : null))); - - controlPanel = panel.addControls(); - controlPanel.add(new JLabel(DarkButtonUI.KEY_VARIANT + ":")); - controlPanel.add(new JComboBox() { - { - addItem(DarkButtonUI.VARIANT_NONE); - addItem(DarkButtonUI.VARIANT_BORDERLESS); - addItem(DarkButtonUI.VARIANT_BORDERLESS_RECTANGULAR); - addItem(DarkButtonUI.VARIANT_ONLY_LABEL); - setSelectedItem(DarkButtonUI.VARIANT_NONE); - addItemListener(e -> button.putClientProperty(DarkButtonUI.KEY_VARIANT, e.getItem())); - } - }); - controlPanel.add(new JLabel(DarkButtonUI.KEY_CORNER + ":")); - controlPanel.add(new JComboBox() { - { - addItem("None"); - for (AlignmentExt a : AlignmentExt.values()) { - addItem(a.name()); - } - setSelectedItem("None"); - addItemListener(e -> { - if ("None".equals(e.getItem())) { - button.putClientProperty(DarkButtonUI.KEY_CORNER, null); - } else { - button.putClientProperty(DarkButtonUI.KEY_CORNER, AlignmentExt.valueOf(e.getItem().toString())); - } - }); - } - }); - return panel; } @Override diff --git a/core/src/test/java/ui/button/ToggleButtonDemo.java b/core/src/test/java/ui/button/ToggleButtonDemo.java index 9406e6c4..e4dab3d1 100644 --- a/core/src/test/java/ui/button/ToggleButtonDemo.java +++ b/core/src/test/java/ui/button/ToggleButtonDemo.java @@ -24,63 +24,24 @@ */ package ui.button; -import java.awt.*; - import javax.swing.*; import ui.ComponentDemo; import ui.DemoPanel; import ui.DemoResources; -import com.github.weisj.darklaf.ui.togglebutton.DarkToggleButtonUI; import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonConstants; -public class ToggleButtonDemo implements ComponentDemo { +public class ToggleButtonDemo extends AbstractButtonDemo { public static void main(final String[] args) { ComponentDemo.showDemo(new ToggleButtonDemo()); } @Override - public JComponent createComponent() { - Icon icon = DemoResources.FOLDER_ICON; - JToggleButton button = new JToggleButton("Test ToggleButton", icon); - DemoPanel panel = new DemoPanel(button); - + protected void addControls(final DemoPanel panel, final JToggleButton button) { + super.addControls(panel, button); JPanel controlPanel = panel.addControls(); - controlPanel.add(new JCheckBox("enabled") { - { - setSelected(button.isEnabled()); - addActionListener(e -> button.setEnabled(isSelected())); - } - }); - controlPanel.add(new JCheckBox("LeftToRight") { - { - setSelected(button.getComponentOrientation().isLeftToRight()); - addActionListener(e -> button.setComponentOrientation(isSelected() ? ComponentOrientation.LEFT_TO_RIGHT - : ComponentOrientation.RIGHT_TO_LEFT)); - } - }); - controlPanel.add(new JCheckBox(DarkToggleButtonUI.KEY_IS_TREE_EDITOR) { - { - setSelected(false); - addActionListener(e -> button.putClientProperty(DarkToggleButtonUI.KEY_IS_TREE_EDITOR, isSelected())); - } - }); - controlPanel.add(new JCheckBox(DarkToggleButtonUI.KEY_IS_TABLE_EDITOR) { - { - setSelected(false); - addActionListener(e -> button.putClientProperty(DarkToggleButtonUI.KEY_IS_TABLE_EDITOR, isSelected())); - } - }); - controlPanel.add(new JCheckBox("Rollover") { - { - setSelected(button.isRolloverEnabled()); - addActionListener(e -> button.setRolloverEnabled(isSelected())); - } - }, "span"); - - controlPanel = panel.addControls(); controlPanel.add(new JLabel(ToggleButtonConstants.KEY_VARIANT + ":")); controlPanel.add(new JComboBox() { { @@ -90,7 +51,12 @@ public class ToggleButtonDemo implements ComponentDemo { addItemListener(e -> button.putClientProperty("JToggleButton.variant", e.getItem())); } }); - return panel; + } + + @Override + protected JToggleButton createButton() { + Icon icon = DemoResources.FOLDER_ICON; + return new JToggleButton("Test ToggleButton", icon); } @Override diff --git a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java index eb11fd1e..7f6083c9 100644 --- a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java +++ b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java @@ -324,9 +324,8 @@ public class WindowsTitlePane extends CustomTitlePane { windowIconButton = new JButton(); windowIconButton.putClientProperty("JButton.noShadowOverwrite", true); windowIconButton.setComponentPopupMenu(createMenu()); - windowIconButton.putClientProperty("JButton.variant", "onlyLabel"); - windowIconButton.addActionListener(e -> windowIconButton - .getComponentPopupMenu() + windowIconButton.putClientProperty("JButton.noBackground", true); + windowIconButton.addActionListener(e -> windowIconButton.getComponentPopupMenu() .show(windowIconButton, windowIconButton.getWidth() / 2, windowIconButton.getHeight() / 2));