From 9491569068bb5a8819d1376495a3b88b1d1951ed Mon Sep 17 00:00:00 2001 From: weisj Date: Sat, 4 Apr 2020 16:33:05 +0200 Subject: [PATCH] Cleaned up menu ui. --- .../darklaf/decorators/MouseDelegate.java | 74 ----- .../decorators/MouseInputDelegate.java | 112 ++++++++ .../weisj/darklaf/ui/menu/DarkMenuUI.java | 263 +++--------------- 3 files changed, 146 insertions(+), 303 deletions(-) delete mode 100644 core/src/main/java/com/github/weisj/darklaf/decorators/MouseDelegate.java create mode 100644 core/src/main/java/com/github/weisj/darklaf/decorators/MouseInputDelegate.java diff --git a/core/src/main/java/com/github/weisj/darklaf/decorators/MouseDelegate.java b/core/src/main/java/com/github/weisj/darklaf/decorators/MouseDelegate.java deleted file mode 100644 index fd192e6b..00000000 --- a/core/src/main/java/com/github/weisj/darklaf/decorators/MouseDelegate.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 com.github.weisj.darklaf.decorators; - -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; - -public class MouseDelegate implements MouseListener { - - protected MouseListener delegate; - - public MouseDelegate(final MouseListener delegate) { - setDelegate(delegate); - } - - public MouseListener getDelegate() { - return delegate; - } - - public void setDelegate(final MouseListener delegate) { - this.delegate = delegate; - if (delegate == null) { - this.delegate = new MouseAdapter() { - }; - } - } - - @Override - public void mouseClicked(final MouseEvent e) { - delegate.mouseClicked(e); - } - - @Override - public void mousePressed(final MouseEvent e) { - delegate.mousePressed(e); - } - - @Override - public void mouseReleased(final MouseEvent e) { - delegate.mousePressed(e); - } - - @Override - public void mouseEntered(final MouseEvent e) { - delegate.mouseEntered(e); - } - - @Override - public void mouseExited(final MouseEvent e) { - delegate.mouseExited(e); - } -} diff --git a/core/src/main/java/com/github/weisj/darklaf/decorators/MouseInputDelegate.java b/core/src/main/java/com/github/weisj/darklaf/decorators/MouseInputDelegate.java new file mode 100644 index 00000000..5acc7d93 --- /dev/null +++ b/core/src/main/java/com/github/weisj/darklaf/decorators/MouseInputDelegate.java @@ -0,0 +1,112 @@ +/* + * 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 com.github.weisj.darklaf.decorators; + +import javax.swing.event.MouseInputListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; + +public class MouseInputDelegate implements MouseInputListener { + + protected MouseListener mouseDelegate; + protected MouseMotionListener motionDelegate; + + public MouseInputDelegate(final MouseListener mouseListener) { + this(mouseListener, null); + } + + public MouseInputDelegate(final MouseMotionListener motionDelegate) { + this(null, motionDelegate); + } + + public MouseInputDelegate(final MouseListener mouseListener, final MouseMotionListener motionDelegate) { + setMotionDelegate(motionDelegate); + setMouseDelegate(mouseListener); + } + + public MouseInputDelegate(final MouseInputListener delegate) { + this(delegate, delegate); + } + + public MouseListener getMouseDelegate() { + return mouseDelegate; + } + + public MouseMotionListener getMotionDelegate() { + return motionDelegate; + } + + public void setMotionDelegate(final MouseMotionListener motionDelegate) { + this.motionDelegate = motionDelegate; + if (motionDelegate == null) { + this.motionDelegate = new MouseAdapter() { + }; + } + } + + public void setMouseDelegate(final MouseListener delegate) { + this.mouseDelegate = delegate; + if (delegate == null) { + this.mouseDelegate = new MouseAdapter() { + }; + } + } + + @Override + public void mouseClicked(final MouseEvent e) { + getMouseDelegate().mouseClicked(e); + } + + @Override + public void mousePressed(final MouseEvent e) { + getMouseDelegate().mousePressed(e); + } + + @Override + public void mouseReleased(final MouseEvent e) { + getMouseDelegate().mousePressed(e); + } + + @Override + public void mouseEntered(final MouseEvent e) { + getMouseDelegate().mouseEntered(e); + } + + @Override + public void mouseExited(final MouseEvent e) { + getMouseDelegate().mouseExited(e); + } + + @Override + public void mouseDragged(final MouseEvent e) { + getMotionDelegate().mouseDragged(e); + } + + @Override + public void mouseMoved(final MouseEvent e) { + getMotionDelegate().mouseMoved(e); + } +} diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuUI.java index e01fc0ab..10187d2f 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuUI.java @@ -23,15 +23,12 @@ */ package com.github.weisj.darklaf.ui.menu; -import com.github.weisj.darklaf.decorators.MouseDelegate; -import com.github.weisj.darklaf.util.DarkUIUtil; +import com.github.weisj.darklaf.decorators.MouseInputDelegate; import com.github.weisj.darklaf.util.GraphicsContext; import com.github.weisj.darklaf.util.GraphicsUtil; -import com.github.weisj.darklaf.util.StringUtil; -import sun.swing.MenuItemLayoutHelper; -import sun.swing.SwingUtilities2; import javax.swing.*; +import javax.swing.event.MouseInputListener; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicMenuUI; import java.awt.*; @@ -55,48 +52,40 @@ public class DarkMenuUI extends BasicMenuUI { } @Override - protected void installListeners() { - super.installListeners(); - for (MouseListener listener : menu.getMouseListeners()) { - if (listener.getClass().getEnclosingClass().equals(BasicMenuUI.class)) { - menu.removeMouseListener(listener); - mouseListener = new MouseDelegate(listener) { - private boolean pressed = false; - private boolean wasEnabled = false; - - @Override - public void mouseEntered(final MouseEvent e) { - MenuSelectionManager manager = MenuSelectionManager.defaultManager(); - MenuElement[] selectedPath = manager.getSelectedPath(); - for (MenuElement element : selectedPath) { - if (element.equals(menu)) { - return; - } - } - super.mouseEntered(e); + protected MouseInputListener createMouseInputListener(final JComponent c) { + return new MouseInputDelegate(super.createMouseInputListener(c)) { + private boolean pressed = false; + private boolean wasEnabled = false; + + @Override + public void mouseEntered(final MouseEvent e) { + MenuSelectionManager manager = MenuSelectionManager.defaultManager(); + MenuElement[] selectedPath = manager.getSelectedPath(); + for (MenuElement element : selectedPath) { + if (element.equals(menu)) { + return; } + } + super.mouseEntered(e); + } - @Override - public void mousePressed(final MouseEvent e) { - pressed = true; - wasEnabled = menu.isEnabled() && menu.isSelected() && menu.getPopupMenu().isShowing(); - super.mousePressed(e); - } + @Override + public void mousePressed(final MouseEvent e) { + pressed = true; + wasEnabled = menu.isEnabled() && menu.isSelected() && menu.getPopupMenu().isShowing(); + super.mousePressed(e); + } - @Override - public void mouseReleased(final MouseEvent e) { - if (!menu.isEnabled()) return; - if (pressed && wasEnabled) { - pressed = false; - return; - } - super.mouseReleased(e); - } - }; - menu.addMouseListener(mouseListener); - break; + @Override + public void mouseReleased(final MouseEvent e) { + if (!menu.isEnabled()) return; + if (pressed && wasEnabled) { + pressed = false; + return; + } + super.mouseReleased(e); } - } + }; } @Override @@ -115,9 +104,11 @@ public class DarkMenuUI extends BasicMenuUI { } public void paint(final Graphics g, final JComponent c) { + GraphicsContext config = GraphicsUtil.setupAntialiasing(g); paintMenuItem(g, c, checkIcon, getArrowIcon(), selectionBackground, isSelected(c) ? selectionForeground : c.getForeground(), defaultTextIconGap); + config.restore(); } protected Icon getArrowIcon() { @@ -130,190 +121,4 @@ public class DarkMenuUI extends BasicMenuUI { if (!(menuItem instanceof JMenuItem)) return false; return menuItem.isEnabled() && ((JMenuItem) menuItem).isArmed(); } - - private static void rightAlignAccText(final MenuItemLayoutHelper lh, - final MenuItemLayoutHelper.LayoutResult lr) { - Rectangle accRect = lr.getAccRect(); - ButtonModel model = lh.getMenuItem().getModel(); - if (model.isEnabled()) { - accRect.x = lh.getViewRect().x + lh.getViewRect().width - - lh.getMenuItem().getIconTextGap() - lr.getAccRect().width; - } - } - - protected void paintMenuItem(final Graphics g, final JComponent c, - final Icon checkIcon, final Icon arrowIcon, - final Color background, final Color foreground, - final int defaultTextIconGap) { - // Save original graphics font and color - Font holdf = g.getFont(); - Color holdc = g.getColor(); - - JMenuItem mi = (JMenuItem) c; - g.setFont(mi.getFont()); - - Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight()); - DarkUIUtil.applyInsets(viewRect, mi.getInsets()); - - MenuItemLayoutHelper lh = new MenuItemLayoutHelper(mi, checkIcon, - arrowIcon, viewRect, defaultTextIconGap, - acceleratorDelimiter, - mi.getComponentOrientation().isLeftToRight(), mi.getFont(), - acceleratorFont, - MenuItemLayoutHelper.useCheckAndArrow(menuItem), - getPropertyPrefix()); - MenuItemLayoutHelper.LayoutResult lr = lh.layoutMenuItem(); - - paintBackground(g, mi, background); - paintCheckIcon(g, lh, lr, holdc, foreground); - paintIcon(g, lh, lr, holdc); - g.setColor(foreground); - paintText(g, lh, lr); - paintAccText(g, lh, lr); - paintArrowIcon(g, lh, lr, foreground); - - // Restore original graphics font and color - g.setColor(holdc); - g.setFont(holdf); - } - - protected void paintCheckIcon(final Graphics g, final MenuItemLayoutHelper lh, - final MenuItemLayoutHelper.LayoutResult lr, - final Color holdc, final Color foreground) { - if (lh.getCheckIcon() != null) { - ButtonModel model = lh.getMenuItem().getModel(); - if (model.isArmed() || (lh.getMenuItem() instanceof JMenu - && model.isSelected())) { - g.setColor(foreground); - } else { - g.setColor(holdc); - } - if (lh.useCheckAndArrow()) { - lh.getCheckIcon().paintIcon(lh.getMenuItem(), g, - lr.getCheckRect().x, lr.getCheckRect().y); - } - g.setColor(holdc); - } - } - - protected void paintAccText(final Graphics g, final MenuItemLayoutHelper lh, - final MenuItemLayoutHelper.LayoutResult lr) { - GraphicsContext config = GraphicsUtil.setupAntialiasing(g); - rightAlignAccText(lh, lr); - if (!StringUtil.isBlank(lh.getAccText())) { - ButtonModel model = lh.getMenuItem().getModel(); - g.setFont(lh.getAccFontMetrics().getFont()); - if (!model.isEnabled()) { - // *** paint the accText disabled - if (disabledForeground != null) { - g.setColor(disabledForeground); - SwingUtilities2.drawString(lh.getMenuItem(), g, - lh.getAccText(), lr.getAccRect().x, - lr.getAccRect().y + lh.getAccFontMetrics().getAscent()); - } else { - g.setColor(lh.getMenuItem().getBackground().brighter()); - SwingUtilities2.drawString(lh.getMenuItem(), g, - lh.getAccText(), lr.getAccRect().x, - lr.getAccRect().y + lh.getAccFontMetrics().getAscent()); - g.setColor(lh.getMenuItem().getBackground().darker()); - SwingUtilities2.drawString(lh.getMenuItem(), g, - lh.getAccText(), lr.getAccRect().x - 1, - lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1); - } - } else { - // *** paint the accText normally - if (model.isArmed() - || (lh.getMenuItem() instanceof JMenu - && model.isSelected())) { - g.setColor(acceleratorSelectionForeground); - } else { - g.setColor(acceleratorForeground); - } - SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(), - lr.getAccRect().x, lr.getAccRect().y + - lh.getAccFontMetrics().getAscent()); - } - } - config.restore(); - } - - protected void paintIcon(final Graphics g, final MenuItemLayoutHelper lh, - final MenuItemLayoutHelper.LayoutResult lr, final Color holdc) { - if (lh.getIcon() != null) { - Icon icon; - ButtonModel model = lh.getMenuItem().getModel(); - if (!model.isEnabled()) { - icon = lh.getMenuItem().getDisabledIcon(); - } else if (model.isPressed() && model.isArmed()) { - icon = lh.getMenuItem().getPressedIcon(); - if (icon == null) { - // Use default icon - icon = lh.getMenuItem().getIcon(); - } - } else { - icon = lh.getMenuItem().getIcon(); - } - - if (icon != null) { - icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x, lr.getIconRect().y); - g.setColor(holdc); - } - } - } - - protected void paintText(final Graphics g, final MenuItemLayoutHelper lh, - final MenuItemLayoutHelper.LayoutResult lr) { - GraphicsContext config = GraphicsUtil.setupAntialiasing(g); - if (!StringUtil.isBlank(lh.getText())) { - if (lh.getHtmlView() != null) { - // Text is HTML - lh.getHtmlView().paint(g, lr.getTextRect()); - } else { - // Text isn't HTML - paintText(g, lh.getMenuItem(), lr.getTextRect(), lh.getText()); - } - } - config.restore(); - } - - protected void paintArrowIcon(final Graphics g, final MenuItemLayoutHelper lh, - final MenuItemLayoutHelper.LayoutResult lr, - final Color foreground) { - if (lh.getArrowIcon() != null) { - ButtonModel model = lh.getMenuItem().getModel(); - if (model.isArmed() || (lh.getMenuItem() instanceof JMenu - && model.isSelected())) { - g.setColor(foreground); - } - if (lh.useCheckAndArrow()) { - lh.getArrowIcon().paintIcon(lh.getMenuItem(), g, - lr.getArrowRect().x, lr.getArrowRect().y); - } - } - } - - @Override - protected void paintBackground(final Graphics g, final JMenuItem menuItem, final Color bgColor) { - ButtonModel model = menuItem.getModel(); - Color oldColor = g.getColor(); - int menuWidth = menuItem.getWidth(); - int menuHeight = menuItem.getHeight() + 1; - - boolean parentOpaque = menuItem.getParent().isOpaque(); - if (menuItem.isOpaque() && parentOpaque) { - if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) { - g.setColor(bgColor); - g.fillRect(0, 0, menuWidth, menuHeight); - } else { - g.setColor(menuItem.getBackground()); - g.fillRect(0, 0, menuWidth, menuHeight); - } - g.setColor(oldColor); - } else if (model.isArmed() || (menuItem instanceof JMenu && - model.isSelected())) { - g.setColor(bgColor); - g.fillRect(0, 0, menuWidth, menuHeight); - g.setColor(oldColor); - } - } }