Browse Source

Attribute focus of currently open popupmenu to the invoker

pull/323/head
Jannis Weis 3 years ago
parent
commit
b979a36c7f
No known key found for this signature in database
GPG Key ID: 7C9D8D4B558049AB
  1. 8
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabber.java
  2. 6
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java
  3. 16
      core/src/main/java/com/github/weisj/darklaf/ui/util/DarkUIUtil.java

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

@ -1,7 +1,7 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2020-2021 Jannis Weis * Copyright (c) 2020-2022 Jannis Weis
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
@ -48,6 +48,12 @@ public class MouseGrabber implements ChangeListener, AWTEventListener, Component
} }
} }
public MenuElement selectedElement() {
if (lastPathSelected == null) return null;
if (lastPathSelected.length == 0) return null;
return lastPathSelected[lastPathSelected.length - 1];
}
protected void grabWindow(final MenuElement[] newPath) { protected void grabWindow(final MenuElement[] newPath) {
final Toolkit tk = Toolkit.getDefaultToolkit(); final Toolkit tk = Toolkit.getDefaultToolkit();
// A grab needs to be added // A grab needs to be added

6
core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java

@ -1,7 +1,7 @@
/* /*
* MIT License * MIT License
* *
* Copyright (c) 2020-2021 Jannis Weis * Copyright (c) 2020-2022 Jannis Weis
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
@ -36,6 +36,10 @@ public final class MouseGrabberUtil {
private static MouseGrabber mouseGrabber; private static MouseGrabber mouseGrabber;
public static MouseGrabber currentGrabber() {
return mouseGrabber;
}
public static void uninstallMouseGrabber() { public static void uninstallMouseGrabber() {
if (mouseGrabber != null) { if (mouseGrabber != null) {
mouseGrabber.uninstall(); mouseGrabber.uninstall();

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

@ -1,7 +1,7 @@
/* /*
* 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 of this software and * 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, * associated documentation files (the "Software"), to deal in the Software without restriction,
@ -46,6 +46,8 @@ import com.github.weisj.darklaf.properties.icons.IconLoader;
import com.github.weisj.darklaf.properties.icons.IconResolver; import com.github.weisj.darklaf.properties.icons.IconResolver;
import com.github.weisj.darklaf.ui.cell.CellRenderer; import com.github.weisj.darklaf.ui.cell.CellRenderer;
import com.github.weisj.darklaf.ui.popupmenu.DarkPopupMenuUI; import com.github.weisj.darklaf.ui.popupmenu.DarkPopupMenuUI;
import com.github.weisj.darklaf.ui.popupmenu.MouseGrabber;
import com.github.weisj.darklaf.ui.popupmenu.MouseGrabberUtil;
import com.github.weisj.darklaf.ui.table.header.DarkTableHeaderRendererPane; import com.github.weisj.darklaf.ui.table.header.DarkTableHeaderRendererPane;
import com.github.weisj.darklaf.util.PropertyUtil; import com.github.weisj.darklaf.util.PropertyUtil;
@ -223,9 +225,21 @@ public final class DarkUIUtil {
if (owner == null) { if (owner == null) {
owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
} }
JPopupMenu popupMenu = getOpenPopupMenu();
if (popupMenu != null) {
owner = popupMenu.getInvoker();
}
return owner != null && isDescendingFrom(owner, c, focusParent); return owner != null && isDescendingFrom(owner, c, focusParent);
} }
private static JPopupMenu getOpenPopupMenu() {
MouseGrabber grabber = MouseGrabberUtil.currentGrabber();
if (grabber == null) return null;
MenuElement menuElement = grabber.selectedElement();
if (menuElement == null) return null;
return getParentOfType(JPopupMenu.class, menuElement.getComponent(), 2);
}
private static boolean isDescendingFrom(final Component a, final Component b1, final Component b2) { private static boolean isDescendingFrom(final Component a, final Component b1, final Component b2) {
if (a == b1 || a == b2) return true; if (a == b1 || a == b2) return true;
for (Container p = a.getParent(); p != null; p = p.getParent()) { for (Container p = a.getParent(); p != null; p = p.getParent()) {

Loading…
Cancel
Save