Browse Source

Don't rely on specific LaF properties for windows title bar buttons

decorations
Jannis Weis 3 years ago
parent
commit
6945e5b244
No known key found for this signature in database
GPG Key ID: 7C9D8D4B558049AB
  1. 73
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/TitleBarButton.java
  2. 36
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java
  3. 4
      windows/src/main/resources/com/github/weisj/darklaf/platform/windows/windows_decorations.properties

73
windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/TitleBarButton.java

@ -0,0 +1,73 @@
/*
* MIT License
*
* Copyright (c) 2022 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.platform.windows.ui;
import java.awt.*;
import javax.swing.*;
class TitleBarButton extends JButton {
private Color hoverColor;
private Color clickColor;
public TitleBarButton(final Action action) {
super(action);
setBorder(BorderFactory.createEmptyBorder());
setBorderPainted(false);
}
@Override
public Color getBackground() {
if (getModel().isArmed()) {
return clickColor;
} else if (getModel().isRollover()) {
return hoverColor;
}
return super.getBackground();
}
@Override
public void paint(final Graphics g) {
Icon icon = null;
if (getModel().isArmed()) {
g.setColor(clickColor);
g.fillRect(0, 0, getWidth(), getHeight());
icon = getPressedIcon();
} else if (getModel().isRollover()) {
g.setColor(hoverColor);
g.fillRect(0, 0, getWidth(), getHeight());
icon = getRolloverIcon();
}
if (icon == null) icon = getIcon();
icon.paintIcon(this, g, (getWidth() - icon.getIconWidth()) / 2, (getHeight() - icon.getIconHeight()) / 2);
}
public void setHoverColor(Color hoverColor) {
this.hoverColor = hoverColor;
if (getModel().isRollover()) repaint();
}
public void setClickColor(Color clickColor) {
this.clickColor = clickColor;
if (getModel().isArmed()) repaint();
}
}

36
windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java

@ -72,9 +72,9 @@ public class WindowsTitlePane extends CustomTitlePane {
private TitlebarIcon restoreIcon; private TitlebarIcon restoreIcon;
private TitlebarIcon minimizeIcon; private TitlebarIcon minimizeIcon;
private JButton windowIconButton; private JButton windowIconButton;
private JButton closeButton; private TitleBarButton closeButton;
private JButton maximizeToggleButton; private TitleBarButton maximizeToggleButton;
private JButton minimizeButton; private TitleBarButton minimizeButton;
private Action closeAction; private Action closeAction;
private Action restoreAction; private Action restoreAction;
private Action maximizeAction; private Action maximizeAction;
@ -86,7 +86,9 @@ public class WindowsTitlePane extends CustomTitlePane {
private Color inactiveBackground; private Color inactiveBackground;
private Color inactiveForeground; private Color inactiveForeground;
private Color hover;
private Color inactiveHover; private Color inactiveHover;
private Color click;
private Color inactiveClick; private Color inactiveClick;
private Color activeBackground; private Color activeBackground;
private Color activeForeground; private Color activeForeground;
@ -120,24 +122,24 @@ public class WindowsTitlePane extends CustomTitlePane {
rootPane.repaint(); rootPane.repaint();
} }
private static JButton createButton(final Icon icon, final Action action) { private TitleBarButton createButton(final Icon icon, final Action action) {
return createButton(icon, action, false); return createButton(icon, action, false);
} }
private static JButton createButton(final Icon icon, final Action action, final boolean close) { private TitleBarButton createButton(final Icon icon, final Action action, final boolean close) {
JButton button = new JButton(action); TitleBarButton button = new TitleBarButton(action);
button.setRolloverEnabled(true); button.setRolloverEnabled(true);
if (close) { if (close) {
button.putClientProperty("JButton.borderless.hover", button.setHoverColor(UIManager.getColor("Windows.TitlePane.close.rollOverColor"));
UIManager.getColor("Windows.TitlePane.close.rollOverColor")); button.setClickColor(UIManager.getColor("Windows.TitlePane.close.clickColor"));
button.putClientProperty("JButton.borderless.click", } else {
UIManager.getColor("Windows.TitlePane.close.clickColor")); button.setHoverColor(hover);
button.setClickColor(click);
} }
button.putClientProperty("JButton.noBorderlessOverwrite", true); button.putClientProperty("JButton.noBorderlessOverwrite", true);
button.setFocusable(false); button.setFocusable(false);
button.setOpaque(false); button.setOpaque(false);
button.setRolloverEnabled(true); button.setRolloverEnabled(true);
button.putClientProperty("JButton.variant", "borderlessRectangular");
button.putClientProperty("paintActive", Boolean.TRUE); button.putClientProperty("paintActive", Boolean.TRUE);
button.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, button.getText()); button.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, button.getText());
button.putClientProperty("JToolTip.style", "plain"); button.putClientProperty("JToolTip.style", "plain");
@ -298,6 +300,8 @@ public class WindowsTitlePane extends CustomTitlePane {
} }
inactiveBackground = UIManager.getColor("Windows.TitlePane.inactiveBackground"); inactiveBackground = UIManager.getColor("Windows.TitlePane.inactiveBackground");
inactiveForeground = UIManager.getColor("Windows.TitlePane.inactiveForeground"); inactiveForeground = UIManager.getColor("Windows.TitlePane.inactiveForeground");
hover = UIManager.getColor("Windows.TitlePane.backgroundHover");
click = UIManager.getColor("Windows.TitlePane.backgroundClick");
inactiveHover = UIManager.getColor("Windows.TitlePane.inactiveBackgroundHover"); inactiveHover = UIManager.getColor("Windows.TitlePane.inactiveBackgroundHover");
inactiveClick = UIManager.getColor("Windows.TitlePane.inactiveBackgroundClick"); inactiveClick = UIManager.getColor("Windows.TitlePane.inactiveBackgroundClick");
border = UIManager.getColor("Windows.TitlePane.borderColor"); border = UIManager.getColor("Windows.TitlePane.borderColor");
@ -432,13 +436,13 @@ public class WindowsTitlePane extends CustomTitlePane {
getRootPane().repaint(); getRootPane().repaint();
} }
protected void setButtonActive(final JButton button, final boolean active) { protected void setButtonActive(final TitleBarButton button, final boolean active) {
if (active) { if (active) {
button.putClientProperty("JButton.borderless.hover", null); button.setHoverColor(hover);
button.putClientProperty("JButton.borderless.click", null); button.setClickColor(click);
} else { } else {
button.putClientProperty("JButton.borderless.hover", inactiveHover); button.setHoverColor(inactiveHover);
button.putClientProperty("JButton.borderless.click", inactiveClick); button.setClickColor(inactiveClick);
} }
} }

4
windows/src/main/resources/com/github/weisj/darklaf/platform/windows/windows_decorations.properties

@ -1,6 +1,6 @@
# MIT License # MIT License
# #
# Copyright (c) 2019-2021 Jannis Weis # Copyright (c) 2019-2022 Jannis Weis
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal # of this software and associated documentation files (the "Software"), to deal
@ -40,6 +40,8 @@ Windows.TitlePane.borderColor = %borderSecondary
Windows.TitlePane.background = %background Windows.TitlePane.background = %background
Windows.TitlePane.foreground = %textForegroundSecondary Windows.TitlePane.foreground = %textForegroundSecondary
Windows.TitlePane.inactiveBackground = %background Windows.TitlePane.inactiveBackground = %background
Windows.TitlePane.backgroundHover = %hoverHighlight
Windows.TitlePane.backgroundClick = %clickHighlight
Windows.TitlePane.inactiveBackgroundHover = %hoverHighlight Windows.TitlePane.inactiveBackgroundHover = %hoverHighlight
Windows.TitlePane.inactiveBackgroundClick = %clickHighlight Windows.TitlePane.inactiveBackgroundClick = %clickHighlight
Windows.TitlePane.inactiveForeground = %textForegroundInactive Windows.TitlePane.inactiveForeground = %textForegroundInactive

Loading…
Cancel
Save