From b979a36c7f503f04eecf24ed8b5719379fb7aa97 Mon Sep 17 00:00:00 2001 From: Jannis Weis <31143295+weisJ@users.noreply.github.com> Date: Mon, 18 Apr 2022 00:39:24 +0200 Subject: [PATCH] Attribute focus of currently open popupmenu to the invoker --- .../weisj/darklaf/ui/popupmenu/MouseGrabber.java | 8 +++++++- .../darklaf/ui/popupmenu/MouseGrabberUtil.java | 6 +++++- .../github/weisj/darklaf/ui/util/DarkUIUtil.java | 16 +++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabber.java b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabber.java index 2d02340b..e9925bef 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabber.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabber.java @@ -1,7 +1,7 @@ /* * 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 * 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) { final Toolkit tk = Toolkit.getDefaultToolkit(); // A grab needs to be added diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java index 37722673..92da8183 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java @@ -1,7 +1,7 @@ /* * 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 * associated documentation files (the "Software"), to deal in the Software without restriction, @@ -36,6 +36,10 @@ public final class MouseGrabberUtil { private static MouseGrabber mouseGrabber; + public static MouseGrabber currentGrabber() { + return mouseGrabber; + } + public static void uninstallMouseGrabber() { if (mouseGrabber != null) { mouseGrabber.uninstall(); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/util/DarkUIUtil.java b/core/src/main/java/com/github/weisj/darklaf/ui/util/DarkUIUtil.java index 9cd64244..ba879a4a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/util/DarkUIUtil.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/util/DarkUIUtil.java @@ -1,7 +1,7 @@ /* * 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 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.ui.cell.CellRenderer; 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.util.PropertyUtil; @@ -223,9 +225,21 @@ public final class DarkUIUtil { if (owner == null) { owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); } + JPopupMenu popupMenu = getOpenPopupMenu(); + if (popupMenu != null) { + owner = popupMenu.getInvoker(); + } 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) { if (a == b1 || a == b2) return true; for (Container p = a.getParent(); p != null; p = p.getParent()) {