Browse Source

Focus: Make focus parent mechanism available without having access to the FocusParentHelper class.

pull/245/head
weisj 4 years ago
parent
commit
3ff51442e1
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 18
      core/src/main/java/com/github/weisj/darklaf/focus/FocusParentHelper.java
  2. 3
      core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java

18
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; if (e.getID() != FocusEvent.FOCUS_GAINED && e.getID() != FocusEvent.FOCUS_LOST) return;
Component comp = e.getComponent(); Component comp = e.getComponent();
listeners.forEach((c, focusParent) -> { 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); RepaintAction repaintAction = PropertyUtil.getObject(c, KEY_FOCUS_ACTION, RepaintAction.class);
if (repaintAction != null) { if (repaintAction != null) {
repaintAction.accept(c); repaintAction.accept(c);
@ -54,6 +54,15 @@ public final class FocusParentHelper {
}, AWTEvent.FOCUS_EVENT_MASK); }, 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) { public static void setFocusParent(final JComponent c, final JComponent focusParent) {
setFocusParent(c, focusParent, Component::repaint); setFocusParent(c, focusParent, Component::repaint);
} }
@ -62,12 +71,9 @@ public final class FocusParentHelper {
final RepaintAction focusChangedAction) { final RepaintAction focusChangedAction) {
if (c == null) return; if (c == null) return;
c.putClientProperty(KEY_FOCUS_PARENT, focusParent); c.putClientProperty(KEY_FOCUS_PARENT, focusParent);
if (focusParent == null) { updateFocusParentRegistry(c, focusParent);
c.putClientProperty(KEY_FOCUS_ACTION, null); if (focusParent != null) {
listeners.remove(c);
} else {
c.putClientProperty(KEY_FOCUS_ACTION, focusChangedAction); c.putClientProperty(KEY_FOCUS_ACTION, focusChangedAction);
listeners.put(c, focusParent);
} }
} }

3
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) { public static boolean hasFocus(final Component c, final FocusEvent e) {
Component focusParent = PropertyUtil.getObject(c, FocusParentHelper.KEY_FOCUS_PARENT, Component.class); 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); return hasFocusImpl(c, focusParent, e);
} }

Loading…
Cancel
Save