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;
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);
}
}

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) {
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);
}

Loading…
Cancel
Save