Browse Source

Avoid clearing menu selection if action doesn't clear the selection path.

Inherit from BasicLookAndFeel to avoid breaking code which checks whether the laf is of type BasicLookAndFeel. Fixes #227
pull/229/head
weisj 4 years ago
parent
commit
7e19ea237c
  1. 49
      core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuItemUIBase.java
  2. 16
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabber.java
  3. 2
      core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/radiobutton/DarkRadioButtonMenuItemUI.java
  4. 1
      core/src/test/java/ui/ComponentDemo.java
  5. 38
      theme/src/main/java/com/github/weisj/darklaf/theme/laf/NoOpBasicLookAndFeel.java
  6. 4
      theme/src/main/java/com/github/weisj/darklaf/theme/laf/ThemedLookAndFeel.java

49
core/src/main/java/com/github/weisj/darklaf/ui/menu/DarkMenuItemUIBase.java

@ -46,6 +46,7 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
protected int acceleratorTextOffset;
protected boolean useEvenHeight;
private boolean closeOnClick;
public static ComponentUI createUI(final JComponent c) {
return new DarkMenuItemUIBase();
@ -58,6 +59,7 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
@Override
public void installUI(final JComponent c) {
super.installUI(c);
closeOnClick = !UIManager.getBoolean(getPropertyPrefix() + ".doNotCloseOnMouseClick");
useEvenHeight = !Boolean.TRUE.equals(UIManager.get(getPropertyPrefix() + ".evenHeight"));
acceleratorTextOffset = UIManager.getInt(getPropertyPrefix() + ".acceleratorTextOffset");
acceleratorFont = UIManager.getFont("MenuItem.font");
@ -93,8 +95,7 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
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();
GraphicsContext context = new GraphicsContext(g);
JMenuItem mi = (JMenuItem) c;
g.setFont(mi.getFont());
@ -106,16 +107,18 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
MenuItemLayoutHelper.LayoutResult lr = lh.layoutMenuItem();
paintBackground(g, mi, background);
paintCheckIcon(g, mi, lh, lr, holdc, foreground);
paintIcon(g, mi, lh, lr, holdc);
context.restore();
paintCheckIcon(g, mi, lh, lr, foreground);
context.restore();
paintIcon(g, mi, lh, lr);
g.setColor(foreground);
paintText(g, mi, lh, lr);
paintAccText(g, mi, lh, lr);
paintArrowIcon(g, mi, lh, lr, foreground);
// Restore original graphics font and color
g.setColor(holdc);
g.setFont(holdf);
context.restore();
}
protected MenuItemLayoutHelper getMenuItemLayoutHelper(final Icon checkIcon, final Icon arrowIcon,
@ -126,18 +129,15 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
}
protected void paintCheckIcon(final Graphics g, final JMenuItem mi, final MenuItemLayoutHelper lh,
final MenuItemLayoutHelper.LayoutResult lr, final Color holdc, final Color foreground) {
final MenuItemLayoutHelper.LayoutResult lr, final Color foreground) {
if (lh.getCheckIcon() != null) {
ButtonModel model = mi.getModel();
if (model.isArmed() || (mi instanceof JMenu && model.isSelected())) {
g.setColor(foreground);
} else {
g.setColor(holdc);
}
if (lh.useCheckAndArrow()) {
lh.getCheckIcon().paintIcon(mi, g, lr.getCheckRect().x, lr.getCheckRect().y);
}
g.setColor(holdc);
}
}
@ -170,7 +170,7 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
}
protected void paintIcon(final Graphics g, final JMenuItem mi, final MenuItemLayoutHelper lh,
final MenuItemLayoutHelper.LayoutResult lr, final Color holdc) {
final MenuItemLayoutHelper.LayoutResult lr) {
if (lh.getIcon() != null) {
Icon icon;
ButtonModel model = mi.getModel();
@ -188,7 +188,6 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
if (icon != null) {
icon.paintIcon(mi, g, lr.getIconRect().x, lr.getIconRect().y);
g.setColor(holdc);
}
}
}
@ -232,11 +231,10 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
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.fillRect(0, 0, menuWidth, menuHeight);
g.setColor(oldColor);
} else if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) {
g.setColor(bgColor);
@ -245,6 +243,25 @@ public class DarkMenuItemUIBase extends BasicMenuItemUI {
}
}
@Override
protected void doClick(final MenuSelectionManager selectionManager) {
if (isCloseOnClick()) {
MenuSelectionManager msm = selectionManager;
// Visual feedback
if (msm == null) {
msm = MenuSelectionManager.defaultManager();
}
msm.clearSelectedPath();
}
JMenuItem item = menuItem;
item.doClick(0);
item.setArmed(!isCloseOnClick());
}
protected boolean isCloseOnClick() {
return closeOnClick;
}
@Override
protected Dimension getPreferredMenuItemSize(final JComponent c, final Icon checkIcon, final Icon arrowIcon,
final int defaultTextIconGap) {

16
core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabber.java

@ -237,23 +237,7 @@ public class MouseGrabber implements ChangeListener, AWTEventListener, Component
for (JPopupMenu popup : popups) {
popup.putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.TRUE);
}
MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath();
if (path.length > 0) {
/*
* Change here: Clear armed/selected/rollover state of MenuItem. This is needed if the menuItem
* changes the Laf.
*/
Component c = path[path.length - 1].getComponent();
if (c instanceof AbstractButton) {
SwingUtilities.invokeLater(() -> {
((AbstractButton) c).getModel().setArmed(false);
((AbstractButton) c).getModel().setRollover(false);
if (c instanceof JMenuItem) ((JMenuItem) c).setSelected(false);
});
}
}
MenuSelectionManager.defaultManager().clearSelectedPath();
} catch (RuntimeException | Error ex) {
realUngrabWindow();
throw ex;

2
core/src/main/java/com/github/weisj/darklaf/ui/togglebutton/radiobutton/DarkRadioButtonMenuItemUI.java

@ -92,7 +92,7 @@ public class DarkRadioButtonMenuItemUI extends DarkMenuItemUIBase implements Tog
@Override
protected void paintCheckIcon(final Graphics g2, final JMenuItem mi, final MenuItemLayoutHelper lh,
final MenuItemLayoutHelper.LayoutResult lr, final Color holdc, final Color foreground) {
final MenuItemLayoutHelper.LayoutResult lr, final Color foreground) {
Rectangle rect = lr.getCheckRect();
if (mi.getIcon() == null) {
rect.y = lr.getIconRect().y;

1
core/src/test/java/ui/ComponentDemo.java

@ -132,6 +132,7 @@ public interface ComponentDemo {
static JMenu createThemeMenu() {
JMenu menu = new JMenu("Theme");
menu.setMnemonic('T');
ButtonGroup bg = new ButtonGroup();
for (UIManager.LookAndFeelInfo theme : LafManager.getRegisteredThemeInfos()) {
createThemeItem(menu, bg, theme);

38
theme/src/main/java/com/github/weisj/darklaf/theme/laf/NoOpBasicLookAndFeel.java

@ -0,0 +1,38 @@
/*
* 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.theme.laf;
import javax.swing.*;
import javax.swing.plaf.basic.BasicLookAndFeel;
public abstract class NoOpBasicLookAndFeel extends BasicLookAndFeel {
@Override
public abstract UIDefaults getDefaults();
@Override
public abstract void initialize();
@Override
public abstract void uninitialize();
}

4
theme/src/main/java/com/github/weisj/darklaf/theme/laf/ThemedLookAndFeel.java

@ -21,11 +21,9 @@
*/
package com.github.weisj.darklaf.theme.laf;
import javax.swing.*;
import com.github.weisj.darklaf.theme.Theme;
public abstract class ThemedLookAndFeel extends LookAndFeel {
public abstract class ThemedLookAndFeel extends NoOpBasicLookAndFeel {
protected abstract void setTheme(final Theme theme);

Loading…
Cancel
Save