From 6945e5b244d128455d3fc406f41cc02b34a1eddb Mon Sep 17 00:00:00 2001 From: Jannis Weis <31143295+weisJ@users.noreply.github.com> Date: Thu, 24 Mar 2022 15:52:27 +0100 Subject: [PATCH] Don't rely on specific LaF properties for windows title bar buttons --- .../platform/windows/ui/TitleBarButton.java | 73 +++++++++++++++++++ .../platform/windows/ui/WindowsTitlePane.java | 36 +++++---- .../windows/windows_decorations.properties | 4 +- 3 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/TitleBarButton.java diff --git a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/TitleBarButton.java b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/TitleBarButton.java new file mode 100644 index 00000000..8e68c679 --- /dev/null +++ b/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(); + } +} 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 7605481e..6d6fb742 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 @@ -72,9 +72,9 @@ public class WindowsTitlePane extends CustomTitlePane { private TitlebarIcon restoreIcon; private TitlebarIcon minimizeIcon; private JButton windowIconButton; - private JButton closeButton; - private JButton maximizeToggleButton; - private JButton minimizeButton; + private TitleBarButton closeButton; + private TitleBarButton maximizeToggleButton; + private TitleBarButton minimizeButton; private Action closeAction; private Action restoreAction; private Action maximizeAction; @@ -86,7 +86,9 @@ public class WindowsTitlePane extends CustomTitlePane { private Color inactiveBackground; private Color inactiveForeground; + private Color hover; private Color inactiveHover; + private Color click; private Color inactiveClick; private Color activeBackground; private Color activeForeground; @@ -120,24 +122,24 @@ public class WindowsTitlePane extends CustomTitlePane { 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); } - private static JButton createButton(final Icon icon, final Action action, final boolean close) { - JButton button = new JButton(action); + private TitleBarButton createButton(final Icon icon, final Action action, final boolean close) { + TitleBarButton button = new TitleBarButton(action); button.setRolloverEnabled(true); if (close) { - button.putClientProperty("JButton.borderless.hover", - UIManager.getColor("Windows.TitlePane.close.rollOverColor")); - button.putClientProperty("JButton.borderless.click", - UIManager.getColor("Windows.TitlePane.close.clickColor")); + button.setHoverColor(UIManager.getColor("Windows.TitlePane.close.rollOverColor")); + button.setClickColor(UIManager.getColor("Windows.TitlePane.close.clickColor")); + } else { + button.setHoverColor(hover); + button.setClickColor(click); } button.putClientProperty("JButton.noBorderlessOverwrite", true); button.setFocusable(false); button.setOpaque(false); button.setRolloverEnabled(true); - button.putClientProperty("JButton.variant", "borderlessRectangular"); button.putClientProperty("paintActive", Boolean.TRUE); button.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, button.getText()); button.putClientProperty("JToolTip.style", "plain"); @@ -298,6 +300,8 @@ public class WindowsTitlePane extends CustomTitlePane { } inactiveBackground = UIManager.getColor("Windows.TitlePane.inactiveBackground"); inactiveForeground = UIManager.getColor("Windows.TitlePane.inactiveForeground"); + hover = UIManager.getColor("Windows.TitlePane.backgroundHover"); + click = UIManager.getColor("Windows.TitlePane.backgroundClick"); inactiveHover = UIManager.getColor("Windows.TitlePane.inactiveBackgroundHover"); inactiveClick = UIManager.getColor("Windows.TitlePane.inactiveBackgroundClick"); border = UIManager.getColor("Windows.TitlePane.borderColor"); @@ -432,13 +436,13 @@ public class WindowsTitlePane extends CustomTitlePane { getRootPane().repaint(); } - protected void setButtonActive(final JButton button, final boolean active) { + protected void setButtonActive(final TitleBarButton button, final boolean active) { if (active) { - button.putClientProperty("JButton.borderless.hover", null); - button.putClientProperty("JButton.borderless.click", null); + button.setHoverColor(hover); + button.setClickColor(click); } else { - button.putClientProperty("JButton.borderless.hover", inactiveHover); - button.putClientProperty("JButton.borderless.click", inactiveClick); + button.setHoverColor(inactiveHover); + button.setClickColor(inactiveClick); } } diff --git a/windows/src/main/resources/com/github/weisj/darklaf/platform/windows/windows_decorations.properties b/windows/src/main/resources/com/github/weisj/darklaf/platform/windows/windows_decorations.properties index d37b776d..b5335fe6 100644 --- a/windows/src/main/resources/com/github/weisj/darklaf/platform/windows/windows_decorations.properties +++ b/windows/src/main/resources/com/github/weisj/darklaf/platform/windows/windows_decorations.properties @@ -1,6 +1,6 @@ # 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 # 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.foreground = %textForegroundSecondary Windows.TitlePane.inactiveBackground = %background +Windows.TitlePane.backgroundHover = %hoverHighlight +Windows.TitlePane.backgroundClick = %clickHighlight Windows.TitlePane.inactiveBackgroundHover = %hoverHighlight Windows.TitlePane.inactiveBackgroundClick = %clickHighlight Windows.TitlePane.inactiveForeground = %textForegroundInactive