Browse Source

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.
pull/188/head
weisj 5 years ago
parent
commit
a434cac90d
  1. 4
      core/src/main/java/com/github/weisj/darklaf/components/ClosableTabComponent.java
  2. 11
      core/src/main/java/com/github/weisj/darklaf/ui/button/ButtonConstants.java
  3. 2
      core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonBorder.java
  4. 51
      core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java
  5. 2
      core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxBorder.java
  6. 3
      core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java
  7. 2
      core/src/main/java/com/github/weisj/darklaf/ui/splitpane/DarkSplitPaneDivider.java
  8. 2
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java
  9. 2
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java
  10. 21
      core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java
  11. 16
      core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java
  12. 2
      core/src/test/java/DemoLauncher.java
  13. 170
      core/src/test/java/ui/button/AbstractButtonDemo.java
  14. 124
      core/src/test/java/ui/button/ButtonDemo.java
  15. 52
      core/src/test/java/ui/button/ToggleButtonDemo.java
  16. 5
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java

4
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) { protected TabButton(final ClosableTabComponent tabComponent) {
this.tabComponent = 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_NO_BORDERLESS_OVERWRITE, true);
putClientProperty(DarkButtonUI.KEY_VARIANT, DarkButtonUI.VARIANT_BORDERLESS_RECTANGULAR);
setOpaque(false); setOpaque(false);
setRolloverEnabled(true); setRolloverEnabled(true);
setBorderPainted(false);
setIcon(UIManager.getIcon("TabbedPane.tabCloseIcon")); setIcon(UIManager.getIcon("TabbedPane.tabCloseIcon"));
setRolloverIcon(UIManager.getIcon("TabbedPane.tabCloseHoverIcon")); setRolloverIcon(UIManager.getIcon("TabbedPane.tabCloseHoverIcon"));
addActionListener(this); addActionListener(this);

11
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_NO_BORDERLESS_OVERWRITE = "JButton.noBorderlessOverwrite";
String KEY_CORNER = "JButton.cornerFlag"; String KEY_CORNER = "JButton.cornerFlag";
String KEY_ROUND = "JButton.round"; String KEY_ROUND = "JButton.round";
String KEY_NO_BACKGROUND = "JButton.noBackground";
String KEY_LEFT_NEIGHBOUR = "JButton.leftNeighbour"; String KEY_LEFT_NEIGHBOUR = "JButton.leftNeighbour";
String KEY_RIGHT_NEIGHBOUR = "JButton.rightNeighbour"; String KEY_RIGHT_NEIGHBOUR = "JButton.rightNeighbour";
@ -52,7 +53,6 @@ public interface ButtonConstants {
String KEY_BOTTOM_LEFT_NEIGHBOUR = "JButton.bottomLeftNeighbour"; String KEY_BOTTOM_LEFT_NEIGHBOUR = "JButton.bottomLeftNeighbour";
String KEY_BOTTOM_RIGHT_NEIGHBOUR = "JButton.bottomRightNeighbour"; String KEY_BOTTOM_RIGHT_NEIGHBOUR = "JButton.bottomRightNeighbour";
String VARIANT_ONLY_LABEL = "onlyLabel";
String VARIANT_BORDERLESS_RECTANGULAR = "borderlessRectangular"; String VARIANT_BORDERLESS_RECTANGULAR = "borderlessRectangular";
String VARIANT_BORDERLESS = "borderless"; String VARIANT_BORDERLESS = "borderless";
String VARIANT_NONE = "none"; String VARIANT_NONE = "none";
@ -98,7 +98,7 @@ public interface ButtonConstants {
static boolean isBorderlessVariant(final Component c) { static boolean isBorderlessVariant(final Component c) {
if (isBorderlessRectangular(c)) return true; if (isBorderlessRectangular(c)) return true;
if (c instanceof JButton) { if (c instanceof JButton || c instanceof JToggleButton) {
return PropertyUtil.isPropertyEqual(c, KEY_VARIANT, VARIANT_BORDERLESS) return PropertyUtil.isPropertyEqual(c, KEY_VARIANT, VARIANT_BORDERLESS)
|| doConvertToBorderless((AbstractButton) c); || doConvertToBorderless((AbstractButton) c);
} }
@ -110,7 +110,8 @@ public interface ButtonConstants {
} }
static boolean doConvertToBorderless(final AbstractButton b) { 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) { static boolean convertIconButtonToBorderless(final AbstractButton b) {
@ -130,4 +131,8 @@ public interface ButtonConstants {
static JComponent getNeighbour(final String key, final Component comp) { static JComponent getNeighbour(final String key, final Component comp) {
return PropertyUtil.getObject(comp, key, JComponent.class); return PropertyUtil.getObject(comp, key, JComponent.class);
} }
static boolean isNoBackground(final AbstractButton b) {
return PropertyUtil.getBooleanProperty(b, KEY_NO_BACKGROUND);
}
} }

2
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) { protected Color getBorderColor(final Component c, final boolean focus) {
if (focus) { if (focus) {
return focusBorderColor; 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; return defaultBorderColor;
} else if (c.isEnabled()) { } else if (c.isEnabled()) {
return borderColor; return borderColor;

51
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.BasicButtonUI;
import javax.swing.plaf.basic.BasicGraphicsUtils; import javax.swing.plaf.basic.BasicGraphicsUtils;
import sun.swing.SwingUtilities2;
import com.github.weisj.darklaf.components.tooltip.ToolTipStyle; import com.github.weisj.darklaf.components.tooltip.ToolTipStyle;
import com.github.weisj.darklaf.delegate.AbstractButtonLayoutDelegate; import com.github.weisj.darklaf.delegate.AbstractButtonLayoutDelegate;
import com.github.weisj.darklaf.graphics.GraphicsContext; 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.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil; import com.github.weisj.darklaf.util.PropertyUtil;
import sun.swing.SwingUtilities2;
/** /**
* @author Jannis Weis * @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 viewRect = new Rectangle();
protected static final Rectangle textRect = new Rectangle(); protected static final Rectangle textRect = new Rectangle();
protected static final Rectangle iconRect = new Rectangle(); protected static final Rectangle iconRect = new Rectangle();
protected static final RoundRectangle2D hitArea = new RoundRectangle2D.Float();
protected int borderSize; protected int borderSize;
protected int shadowHeight; protected int shadowHeight;
protected boolean drawOutline; protected boolean drawOutline;
@ -154,7 +155,9 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
public void paint(final Graphics g, final JComponent c) { public void paint(final Graphics g, final JComponent c) {
GraphicsContext config = new GraphicsContext(g); GraphicsContext config = new GraphicsContext(g);
AbstractButton b = (AbstractButton) c; AbstractButton b = (AbstractButton) c;
if (!ButtonConstants.isNoBackground(b)) {
paintButtonBackground(g, c); paintButtonBackground(g, c);
}
Font font = g.getFont(); Font font = g.getFont();
if (ButtonConstants.isDefaultButton(b) && !font.isBold()) { 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, protected void paintBorderlessBackground(final AbstractButton b, final Graphics2D g, final int arc,
final int width, final int height, final Insets margin) { final int width, final int height, final Insets margin) {
if (b.isEnabled() && b.getModel().isRollover()) { if (isRolloverBorderless(b)) {
GraphicsUtil.setupAAPainting(g); GraphicsUtil.setupAAPainting(g);
g.setColor(getBorderlessBackground(b)); g.setColor(getBorderlessBackground(b));
boolean borderlessRectangular = ButtonConstants.isBorderlessRectangular(b);
if (!borderlessRectangular) DarkUIUtil.addInsets(margin, borderSize);
if (ButtonConstants.isBorderlessRectangular(b)) { if (ButtonConstants.isBorderlessRectangular(b)) {
g.fillRect(margin.left, margin.top, width - margin.left - margin.right, g.fillRect(margin.left, margin.top, width - margin.left - margin.right,
height - margin.top - margin.bottom); 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)) { } else if (ButtonConstants.doConvertToBorderless(b)) {
int size = Math.min(width - margin.left - margin.right, int size = Math.min(width - margin.left - margin.right,
height - margin.left - margin.right); height - margin.left - margin.right);
if (!drawOutline) { if (!drawOutline) {
g.fillRoundRect((width - size) / 2 + 2, (height - size) / 2 + 2, g.fillRoundRect((width - size) / 2, (height - size) / 2, size, size, arc, arc);
size - 4, size - 4, arc, arc);
} else { } else {
g.setColor(getBorderlessOutline(b)); g.setColor(getBorderlessOutline(b));
g.drawRoundRect((width - size) / 2 + 2, (height - size) / 2 + 2, PaintUtil.paintLineBorder(g, (width - size) / 2.0f,
size - 4, size - 4, arc, arc); (height - size) / 2.0f, size, size, arc);
} }
} else { } else {
if (!drawOutline) { if (!drawOutline) {
g.fillRoundRect(margin.left + 2, margin.top + 2, g.fillRoundRect(margin.left, margin.top,
width - margin.left - margin.right - 4, width - margin.left - margin.right,
height - margin.top - margin.bottom - 4, height - margin.top - margin.bottom,
arc, arc); arc, arc);
} else { } else {
g.setColor(getBorderlessOutline(b)); g.setColor(getBorderlessOutline(b));
g.drawRoundRect(margin.left + 2, margin.top + 2, PaintUtil.paintLineBorder(g, margin.left, margin.top,
width - margin.left - margin.right - 4, width - margin.left - margin.right,
height - margin.top - margin.bottom - 4, height - margin.top - margin.bottom,
arc, arc); 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) { protected void paintText(final Graphics g, final AbstractButton b, final String text) {
ButtonModel model = b.getModel(); ButtonModel model = b.getModel();
g.setColor(getForeground(b)); g.setColor(getForeground(b));
@ -354,11 +360,14 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
} }
protected Color getBorderlessBackground(final AbstractButton c) { protected Color getBorderlessBackground(final AbstractButton c) {
boolean armed = c.getModel().isArmed(); return isArmedBorderless(c) ? PropertyUtil.getColor(c, KEY_CLICK_COLOR, borderlessClick)
return armed ? PropertyUtil.getColor(c, KEY_CLICK_COLOR, borderlessClick)
: PropertyUtil.getColor(c, KEY_HOVER_COLOR, borderlessHover); : PropertyUtil.getColor(c, KEY_HOVER_COLOR, borderlessHover);
} }
protected boolean isArmedBorderless(final AbstractButton b) {
return b.getModel().isArmed();
}
protected Color getBorderlessOutline(final AbstractButton c) { protected Color getBorderlessOutline(final AbstractButton c) {
boolean armed = c.getModel().isArmed(); boolean armed = c.getModel().isArmed();
return armed ? borderlessOutlineClick : borderlessOutlineHover; return armed ? borderlessOutlineClick : borderlessOutlineHover;
@ -417,9 +426,9 @@ public class DarkButtonUI extends BasicButtonUI implements ButtonConstants {
return super.contains(c, x, y); return super.contains(c, x, y);
} }
if (!(x >= 0 && x <= c.getWidth() && y >= 0 && y <= c.getHeight())) return false; 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); int arc = getArc(c);
return new RoundRectangle2D.Float(bs, bs, c.getWidth() - 2 * bs, c.getHeight() - 2 * bs, hitArea.setRoundRect(bs, bs, c.getWidth() - 2 * bs, c.getHeight() - 2 * bs, arc, arc);
arc, arc).contains(x, y); return hitArea.contains(x, y);
} }
} }

2
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()) { if (comboBox.isEditable()) {
Rectangle arrowBounds = arrowButton.getBounds(); Rectangle arrowBounds = arrowButton.getBounds();
boolean leftToRight = comboBox.getComponentOrientation().isLeftToRight(); 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.setColor(borderColor);
g.fillRect(off, editBSize, 1, height - 2 * editBSize); g.fillRect(off, editBSize, 1, height - 2 * editBSize);
} }

3
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.x += boxPadding.left;
rect.width -= boxPadding.left; rect.width -= boxPadding.left;
} else { } else {
rect.width -= borderSize + 1; rect.width -= boxPadding.right - borderSize;
rect.x += 1;
} }
} }
return rect; return rect;

2
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 static class OneTouchButton extends JButton implements UIResource {
protected OneTouchButton() { protected OneTouchButton() {
putClientProperty(DarkButtonUI.KEY_VARIANT, DarkButtonUI.VARIANT_ONLY_LABEL);
setMinimumSize(new Dimension(ONE_TOUCH_SIZE, ONE_TOUCH_SIZE)); setMinimumSize(new Dimension(ONE_TOUCH_SIZE, ONE_TOUCH_SIZE));
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
putClientProperty(DarkButtonUI.KEY_NO_BACKGROUND, true);
setRequestFocusEnabled(false); setRequestFocusEnabled(false);
setBorderPainted(false); setBorderPainted(false);
setFocusPainted(false); setFocusPainted(false);

2
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java

@ -46,7 +46,7 @@ public class MoreTabsButton extends DarkTabAreaButton {
icon = ui.getMoreTabsIcon(); icon = ui.getMoreTabsIcon();
pad = UIManager.getInt("TabbedPane.moreTabsButton.pad"); pad = UIManager.getInt("TabbedPane.moreTabsButton.pad");
setIcon(EmptyIcon.create(icon.getIconWidth(), icon.getIconHeight())); 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); putClientProperty(DarkButtonUI.KEY_SQUARE, true);
setFocusable(false); setFocusable(false);
setFont(getFont().deriveFont(8f)); setFont(getFont().deriveFont(8f));

2
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) { protected void adjustTextRect(final JTextComponent c, final Rectangle r) {
boolean ltr = c.getComponentOrientation().isLeftToRight(); 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)) { if (doPaintLeftIcon(c)) {
int w = getLeftIcon(c).getIconWidth() + padding.left; int w = getLeftIcon(c).getIconWidth() + padding.left;
if (ltr) r.x += w; if (ltr) r.x += w;

21
core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/DarkToggleButtonUI.java

@ -25,7 +25,6 @@
package com.github.weisj.darklaf.ui.togglebutton; package com.github.weisj.darklaf.ui.togglebutton;
import java.awt.*; import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import javax.swing.*; import javax.swing.*;
import javax.swing.plaf.ComponentUI; 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.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.ui.button.ButtonConstants;
import com.github.weisj.darklaf.ui.button.DarkButtonUI; import com.github.weisj.darklaf.ui.button.DarkButtonUI;
/** /**
@ -132,7 +132,9 @@ public class DarkToggleButtonUI extends DarkButtonUI implements ToggleButtonCons
@Override @Override
protected Color getForeground(final AbstractButton button) { 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) { && button.getForeground() instanceof UIResource) {
return selectedForeground; 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) { protected Color getToggleBorderColor(final AbstractButton b) {
if (b.hasFocus()) { if (b.hasFocus()) {
return focusBorderColor; return focusBorderColor;
@ -230,7 +242,8 @@ public class DarkToggleButtonUI extends DarkButtonUI implements ToggleButtonCons
if (!ToggleButtonConstants.isSlider(c)) return super.contains(c, x, y); if (!ToggleButtonConstants.isSlider(c)) return super.contains(c, x, y);
if (!(x >= 0 && x <= c.getWidth() && y >= 0 && y <= c.getHeight())) return false; if (!(x >= 0 && x <= c.getWidth() && y >= 0 && y <= c.getHeight())) return false;
Rectangle bounds = getSliderBounds(c); Rectangle bounds = getSliderBounds(c);
return new RoundRectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height, int arc = Math.min(bounds.width, bounds.height);
bounds.height, bounds.height).contains(x, y); hitArea.setRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, arc, arc);
return hitArea.contains(x, y);
} }
} }

16
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) { public static Insets addInsets(final Insets ins1, final Insets ins2) {
if (ins2 == null) return ins1; if (ins2 == null) return ins1;
if (ins1 != null) { if (ins1 != null) {

2
core/src/test/java/DemoLauncher.java

@ -25,6 +25,7 @@
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
@ -74,6 +75,7 @@ public class DemoLauncher implements ComponentDemo {
demoClasses = getClasses("ui", "icon", "defaults").filter(demoType::isAssignableFrom) demoClasses = getClasses("ui", "icon", "defaults").filter(demoType::isAssignableFrom)
.filter(cls -> !cls.equals(DemoEntry.class)) .filter(cls -> !cls.equals(DemoEntry.class))
.filter(cls -> !cls.isInterface()) .filter(cls -> !cls.isInterface())
.filter(cls -> !Modifier.isAbstract(cls.getModifiers()))
.map(this::getInstance) .map(this::getInstance)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(demoType::cast) .map(demoType::cast)

170
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<T extends AbstractButton> 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<String>() {
{
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<String>() {
{
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()));
}
});
}
});
}
}

124
core/src/test/java/ui/button/ButtonDemo.java

@ -29,40 +29,24 @@ import java.awt.*;
import javax.swing.*; import javax.swing.*;
import ui.ComponentDemo; import ui.ComponentDemo;
import ui.DemoPanel;
import ui.DemoResources; import ui.DemoResources;
import com.github.weisj.darklaf.components.color.QuickColorChooser; public class ButtonDemo extends AbstractButtonDemo<JButton> {
import com.github.weisj.darklaf.ui.button.DarkButtonUI;
import com.github.weisj.darklaf.util.AlignmentExt;
public class ButtonDemo implements ComponentDemo {
public static void main(final String[] args) { public static void main(final String[] args) {
ComponentDemo.showDemo(new ButtonDemo()); ComponentDemo.showDemo(new ButtonDemo());
} }
@Override @Override
public JComponent createComponent() { protected JButton createButton() {
Icon icon = DemoResources.FOLDER_ICON; Icon icon = DemoResources.FOLDER_ICON;
JButton button = new JButton("Test Button", icon); JButton button = new JButton("Test Button", icon);
DemoPanel panel = new DemoPanel(button);
button.setToolTipText("TipText"); 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") { controlPanel.add(new JCheckBox("default") {
{ {
setSelected(button.isDefaultButton()); setSelected(button.isDefaultButton());
@ -70,102 +54,6 @@ public class ButtonDemo implements ComponentDemo {
.setDefaultButton(isSelected() ? button : null)); .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<String>() {
{
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<String>() {
{
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 @Override

52
core/src/test/java/ui/button/ToggleButtonDemo.java

@ -24,63 +24,24 @@
*/ */
package ui.button; package ui.button;
import java.awt.*;
import javax.swing.*; import javax.swing.*;
import ui.ComponentDemo; import ui.ComponentDemo;
import ui.DemoPanel; import ui.DemoPanel;
import ui.DemoResources; import ui.DemoResources;
import com.github.weisj.darklaf.ui.togglebutton.DarkToggleButtonUI;
import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonConstants; import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonConstants;
public class ToggleButtonDemo implements ComponentDemo { public class ToggleButtonDemo extends AbstractButtonDemo<JToggleButton> {
public static void main(final String[] args) { public static void main(final String[] args) {
ComponentDemo.showDemo(new ToggleButtonDemo()); ComponentDemo.showDemo(new ToggleButtonDemo());
} }
@Override @Override
public JComponent createComponent() { protected void addControls(final DemoPanel panel, final JToggleButton button) {
Icon icon = DemoResources.FOLDER_ICON; super.addControls(panel, button);
JToggleButton button = new JToggleButton("Test ToggleButton", icon);
DemoPanel panel = new DemoPanel(button);
JPanel controlPanel = panel.addControls(); 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 JLabel(ToggleButtonConstants.KEY_VARIANT + ":"));
controlPanel.add(new JComboBox<String>() { controlPanel.add(new JComboBox<String>() {
{ {
@ -90,7 +51,12 @@ public class ToggleButtonDemo implements ComponentDemo {
addItemListener(e -> button.putClientProperty("JToggleButton.variant", e.getItem())); 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 @Override

5
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 = new JButton();
windowIconButton.putClientProperty("JButton.noShadowOverwrite", true); windowIconButton.putClientProperty("JButton.noShadowOverwrite", true);
windowIconButton.setComponentPopupMenu(createMenu()); windowIconButton.setComponentPopupMenu(createMenu());
windowIconButton.putClientProperty("JButton.variant", "onlyLabel"); windowIconButton.putClientProperty("JButton.noBackground", true);
windowIconButton.addActionListener(e -> windowIconButton windowIconButton.addActionListener(e -> windowIconButton.getComponentPopupMenu()
.getComponentPopupMenu()
.show(windowIconButton, .show(windowIconButton,
windowIconButton.getWidth() / 2, windowIconButton.getWidth() / 2,
windowIconButton.getHeight() / 2)); windowIconButton.getHeight() / 2));

Loading…
Cancel
Save