Browse Source

Fixed focus border for buttons with cut off border.

Added demo for grouped buttons.
pull/127/head
weisj 5 years ago
parent
commit
ce9715df80
  1. 33
      core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonBorder.java
  2. 21
      core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java
  3. 10
      core/src/main/java/com/github/weisj/darklaf/ui/button/DarkToggleButtonUI.java
  4. 8
      core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java
  5. 75
      core/src/test/java/ui/button/GroupedButtonDemo.java

33
core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonBorder.java

@ -148,13 +148,12 @@ public class DarkButtonBorder implements Border, UIResource {
AlignmentExt corner = getCornerFlag(c);
boolean paintShadow = showDropShadow(corner);
boolean focus = paintFocus(c);
int shadowHeight = paintShadow ? getShadowSize() : 0;
int borderSize = getBorderSize();
Insets insetMask = new Insets(borderSize, borderSize, borderSize, borderSize);
Insets focusIns = new Insets(0, 0, 0, 0);
if (corner != null) {
focusIns = corner.maskInsets(focusIns, -borderSize - focusArc);
insetMask = corner.maskInsets(insetMask, -arc);
}
@ -162,23 +161,27 @@ public class DarkButtonBorder implements Border, UIResource {
int by = insetMask.top;
int bw = width - insetMask.left - insetMask.right;
int bh = height - insetMask.top - insetMask.bottom;
int fx = focusIns.left;
int fy = focusIns.top;
int fw = width - focusIns.left - focusIns.right;
int fh = height - focusIns.top - focusIns.bottom;
if (c.isEnabled() && paintShadow) {
paintShadow((Graphics2D) g, bx, by, bw, bh, arc);
}
if (paintFocus(c)) {
g.translate(fx, fy);
DarkUIUtil.paintFocusBorder(g2, fw, fh - shadowHeight, focusArc, borderSize);
g.translate(-fx, -fy);
if (corner == null) {
if (focus) {
DarkUIUtil.paintFocusBorder(g2, width, height - shadowHeight, focusArc, borderSize);
}
g2.setColor(getBorderColor(c, focus));
DarkUIUtil.paintLineBorder(g2, bx, by, bw, bh - shadowHeight, arc);
} else {
g2.setColor(getBorderColor(c, false));
DarkUIUtil.paintLineBorder(g2, bx, by, bw, bh - shadowHeight, arc);
if (focus) {
g2.setColor(getBorderColor(c, true));
DarkUIUtil.paintLineBorder(g2, borderSize, borderSize, width - 2 * borderSize,
height - 2 * borderSize - shadowHeight, arc);
DarkUIUtil.paintFocusBorder(g2, width, height - shadowHeight, focusArc, borderSize);
}
}
g2.setColor(getBorderColor(c));
DarkUIUtil.paintLineBorder(g2, bx, by, bw, bh - shadowHeight, arc);
config.restore();
}
@ -206,8 +209,8 @@ public class DarkButtonBorder implements Border, UIResource {
return false;
}
protected Color getBorderColor(final Component c) {
if (paintFocus(c)) {
protected Color getBorderColor(final Component c, final boolean focus) {
if (focus) {
return focusBorderColor;
} else if (c instanceof JButton && ((JButton) c).isDefaultButton() && c.isEnabled()) {
return defaultBorderColor;

21
core/src/main/java/com/github/weisj/darklaf/ui/button/DarkButtonUI.java

@ -362,17 +362,25 @@ public class DarkButtonUI extends BasicButtonUI implements PropertyChangeListene
if (isShadowVariant(c)) {
paintShadowBackground(g, c, g2, b, arc, width, height, margin);
} else {
paintDefaultBackground((Graphics2D) g, c, g2, arc, width, height);
paintDefaultBackground((Graphics2D) g, b, g2, arc, width, height);
}
}
}
protected void paintDefaultBackground(final Graphics2D g, final JComponent c, final Graphics2D g2,
protected void paintDefaultBackground(final Graphics2D g, final AbstractButton c, final Graphics2D g2,
final int arc, final int width, final int height) {
g2.setColor(getBackgroundColor(c));
int shadow = DarkButtonBorder.showDropShadow(c) ? shadowHeight : 0;
int effectiveArc = isSquare(c) && !chooseAlternativeArc(c) ? 0 : arc;
Rectangle bgRect = getEffectiveRect(width, height, c, -(effectiveArc + 1));
AlignmentExt corner = DarkButtonBorder.getCornerFlag(c);
boolean focus = c.hasFocus() && c.isFocusPainted();
Rectangle bgRect = getEffectiveRect(width, height, c, -(effectiveArc + 1), corner, focus);
g2.setColor(getBackgroundColor(c));
paintBackgroundRect(g, g2, shadow, effectiveArc, bgRect);
}
private void paintBackgroundRect(final Graphics2D g, final Graphics2D g2,
final int shadow, final int effectiveArc, final Rectangle bgRect) {
if (effectiveArc == 0) {
g2.fillRect(bgRect.x, bgRect.y, bgRect.width, bgRect.height - shadow);
} else {
@ -380,13 +388,12 @@ public class DarkButtonUI extends BasicButtonUI implements PropertyChangeListene
}
}
protected Rectangle getEffectiveRect(final int width, final int height, final JComponent c, final int adjustment) {
AlignmentExt corner = DarkButtonBorder.getCornerFlag(c);
protected Rectangle getEffectiveRect(final int width, final int height, final AbstractButton c,
final int adjustment, final AlignmentExt corner, final boolean focus) {
Insets insetMask = new Insets(borderSize, borderSize, borderSize, borderSize);
if (corner != null) {
insetMask = corner.maskInsets(insetMask, adjustment);
}
int bx = insetMask.left;
int by = insetMask.top;
int bw = width - insetMask.left - insetMask.right;

10
core/src/main/java/com/github/weisj/darklaf/ui/button/DarkToggleButtonUI.java

@ -119,18 +119,16 @@ public class DarkToggleButtonUI extends DarkButtonUI {
AbstractButton b = (AbstractButton) c;
boolean rollOver = (b.isRolloverEnabled() || doConvertToShadow(b)) && (((JButton) c).getModel().isRollover());
boolean clicked = b.getModel().isArmed();
boolean isSelected = c instanceof JToggleButton && ((JToggleButton) c).isSelected();
if (c.isEnabled()) {
if (isSelected) return background;
if (clicked) {
return clickBackground;
} else if (rollOver) {
return hoverBackground;
} else {
if (c instanceof JToggleButton && c.isEnabled()) {
if (((JToggleButton) c).isSelected()) {
return background;
} else {
return backgroundInactive;
}
if (c instanceof JToggleButton) {
return backgroundInactive;
} else {
return super.getBackgroundColor(c);
}

8
core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java

@ -100,6 +100,14 @@ public final class DarkUIUtil {
config.restore();
}
public static void fillFocusRect(final Graphics2D g, final int x, final int y, final int width, final int height) {
GraphicsContext config = new GraphicsContext(g);
g.setComposite(DarkUIUtil.GLOW_ALPHA);
Outline.focus.setGraphicsColor(g, true);
g.fillRect(x, y, width, height);
config.restore();
}
public static void paintFocusOval(final Graphics2D g, final int x, final int y, final int width, final int height) {
paintFocusOval(g, (float) x, (float) y, (float) width, (float) height);
}

75
core/src/test/java/ui/button/GroupedButtonDemo.java

@ -0,0 +1,75 @@
/*
* 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 com.github.weisj.darklaf.icons.IconLoader;
import com.github.weisj.darklaf.ui.button.DarkButtonUI;
import com.github.weisj.darklaf.util.AlignmentExt;
import ui.ComponentDemo;
import ui.DemoPanel;
import javax.swing.*;
public class GroupedButtonDemo implements ComponentDemo {
public static void main(final String[] args) {
ComponentDemo.showDemo(new GroupedButtonDemo());
}
@Override
public JComponent createComponent() {
Box box = new Box(BoxLayout.LINE_AXIS);
Icon icon = IconLoader.get().getIcon("menu/listFiles.svg", 19, 19, true);
Icon iconSelected = IconLoader.get().getIcon("menu/listFilesSelected.svg", 19, 19, true);
ButtonGroup bg = new ButtonGroup();
box.add(createButton(icon, iconSelected, bg, AlignmentExt.LEFT));
for (int i = 0; i < 3; i++) {
box.add(createButton(icon, iconSelected, bg, AlignmentExt.MIDDLE_HORIZONTAL));
}
box.add(createButton(icon, iconSelected, bg, AlignmentExt.RIGHT, true));
return new DemoPanel(box);
}
protected AbstractButton createButton(final Icon icon, final Icon selected,
final ButtonGroup bg, final AlignmentExt a) {
return createButton(icon, selected, bg, a, false);
}
protected AbstractButton createButton(final Icon icon, final Icon selected,
final ButtonGroup bg, final AlignmentExt a,
final boolean isSelected) {
return new JToggleButton(icon) {{
setSelectedIcon(selected);
putClientProperty(DarkButtonUI.KEY_THIN, true);
putClientProperty(DarkButtonUI.KEY_CORNER, a);
setSelected(isSelected);
// bg.add(this);
}};
}
@Override
public String getTitle() {
return "Grouped Button Demo";
}
}
Loading…
Cancel
Save