From 3ff51442e18eba4ff1822d1ee1dfe7a2e2731559 Mon Sep 17 00:00:00 2001 From: weisj <31143295+weisJ@users.noreply.github.com> Date: Wed, 26 May 2021 15:38:54 +0200 Subject: [PATCH] Focus: Make focus parent mechanism available without having access to the FocusParentHelper class. --- .../weisj/darklaf/focus/FocusParentHelper.java | 18 ++++++++++++------ .../github/weisj/darklaf/util/DarkUIUtil.java | 3 +++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/focus/FocusParentHelper.java b/core/src/main/java/com/github/weisj/darklaf/focus/FocusParentHelper.java index c346caa6..27590043 100644 --- a/core/src/main/java/com/github/weisj/darklaf/focus/FocusParentHelper.java +++ b/core/src/main/java/com/github/weisj/darklaf/focus/FocusParentHelper.java @@ -44,7 +44,7 @@ public final class FocusParentHelper { if (e.getID() != FocusEvent.FOCUS_GAINED && e.getID() != FocusEvent.FOCUS_LOST) return; Component comp = e.getComponent(); listeners.forEach((c, focusParent) -> { - if (SwingUtilities.isDescendingFrom(comp, focusParent)) { + if (c instanceof JComponent && SwingUtilities.isDescendingFrom(comp, focusParent)) { RepaintAction repaintAction = PropertyUtil.getObject(c, KEY_FOCUS_ACTION, RepaintAction.class); if (repaintAction != null) { repaintAction.accept(c); @@ -54,6 +54,15 @@ public final class FocusParentHelper { }, AWTEvent.FOCUS_EVENT_MASK); } + public static void updateFocusParentRegistry(final JComponent c, final Component parent) { + if (parent == null) { + c.putClientProperty(KEY_FOCUS_ACTION, null); + listeners.remove(c); + } else { + listeners.put(c, parent); + } + } + public static void setFocusParent(final JComponent c, final JComponent focusParent) { setFocusParent(c, focusParent, Component::repaint); } @@ -62,12 +71,9 @@ public final class FocusParentHelper { final RepaintAction focusChangedAction) { if (c == null) return; c.putClientProperty(KEY_FOCUS_PARENT, focusParent); - if (focusParent == null) { - c.putClientProperty(KEY_FOCUS_ACTION, null); - listeners.remove(c); - } else { + updateFocusParentRegistry(c, focusParent); + if (focusParent != null) { c.putClientProperty(KEY_FOCUS_ACTION, focusChangedAction); - listeners.put(c, focusParent); } } diff --git a/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java b/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java index 0f513f2f..0f48d8d8 100644 --- a/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java +++ b/core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java @@ -192,6 +192,9 @@ public final class DarkUIUtil { */ public static boolean hasFocus(final Component c, final FocusEvent e) { Component focusParent = PropertyUtil.getObject(c, FocusParentHelper.KEY_FOCUS_PARENT, Component.class); + if (c instanceof JComponent) { + FocusParentHelper.updateFocusParentRegistry((JComponent) c, focusParent); + } return hasFocusImpl(c, focusParent, e); }