From 4de2b16e17d87392b59a40848b0d62a8f1c4cffc Mon Sep 17 00:00:00 2001 From: weisj Date: Wed, 18 Mar 2020 14:02:56 +0100 Subject: [PATCH] Made JTextField.keepSelectionOnFocusLost true by default. --- .../darklaf/ui/text/DarkTextFieldUI.java | 4 ++- .../weisj/darklaf/ui/text/DarkTextUI.java | 34 ++++++------------- .../darklaf/properties/ui/text.properties | 1 + 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java index 7db6b4e4..8fcf643e 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java @@ -56,7 +56,7 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh protected static Icon searchWithHistory; private final FocusListener focusListener = new FocusAdapter() { public void focusLost(final FocusEvent e) { - if (!Boolean.TRUE.equals(getComponent().getClientProperty(KEY_KEEP_SELECTION_ON_FOCUS_LOST))) { + if (Boolean.FALSE.equals(getComponent().getClientProperty(KEY_KEEP_SELECTION_ON_FOCUS_LOST))) { getComponent().select(0, 0); } } @@ -242,6 +242,8 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh @Override protected void installDefaults() { super.installDefaults(); + editor.putClientProperty(KEY_KEEP_SELECTION_ON_FOCUS_LOST, + UIManager.getBoolean("TextField.keepSelectionOnFocusLost")); arcSize = UIManager.getInt("TextField.arc"); borderSize = UIManager.getInt("TextField.borderThickness"); searchArcSize = UIManager.getInt("TextField.searchArc"); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java index 07783224..69b55b3a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java @@ -83,8 +83,7 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi @Override protected void installDefaults() { super.installDefaults(); - editor.putClientProperty(KEY_ROUNDED_SELECTION, - UIManager.getBoolean("TextComponent.roundedSelection")); + editor.putClientProperty(KEY_ROUNDED_SELECTION, UIManager.getBoolean("TextComponent.roundedSelection")); disabledColor = UIManager.getColor(getPropertyPrefix() + ".disabledBackground"); inactiveColor = UIManager.getColor(getPropertyPrefix() + ".inactiveBackground"); } @@ -237,9 +236,7 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi protected InputMap getInputMap() { InputMap map = new InputMapUIResource(); - InputMap shared = - (InputMap) DefaultLookup.get(editor, this, - getPropertyPrefix() + ".focusInputMap"); + InputMap shared = (InputMap) DefaultLookup.get(editor, this, getPropertyPrefix() + ".focusInputMap"); if (shared != null) { map.setParent(shared); } @@ -270,8 +267,7 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi if (getEditorKit(editor) instanceof DefaultEditorKit) { if (map != null) { Object obj = map.get(DefaultEditorKit.insertBreakAction); - if (obj != null - && obj instanceof DefaultEditorKit.InsertBreakAction) { + if (obj instanceof DefaultEditorKit.InsertBreakAction) { Action action = new TextActionWrapper((TextAction) obj); componentMap.put(action.getValue(Action.NAME), action); } @@ -307,8 +303,7 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi km.clear(); if (accelerator != '\0') { km.put(KeyStroke.getKeyStroke(accelerator, getFocusAcceleratorKeyMask()), "requestFocus"); - km.put(KeyStroke.getKeyStroke(accelerator, - DarkSwingUtil.setAltGraphMask(getFocusAcceleratorKeyMask())), + km.put(KeyStroke.getKeyStroke(accelerator, DarkSwingUtil.setAltGraphMask(getFocusAcceleratorKeyMask())), "requestFocus"); } } @@ -326,12 +321,9 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi for (Action a : actions) { map.put(a.getValue(Action.NAME), a); } - map.put(TransferHandler.getCutAction().getValue(Action.NAME), - TransferHandler.getCutAction()); - map.put(TransferHandler.getCopyAction().getValue(Action.NAME), - TransferHandler.getCopyAction()); - map.put(TransferHandler.getPasteAction().getValue(Action.NAME), - TransferHandler.getPasteAction()); + map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); + map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); + map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); return map; } @@ -357,14 +349,10 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi */ EditorKit editorKit = getEditorKit(editor); if (editorKit instanceof DefaultEditorKit) { - Set storedForwardTraversalKeys = - editor.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); - Set storedBackwardTraversalKeys = - editor.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); - Set forwardTraversalKeys = - new HashSet<>(storedForwardTraversalKeys); - Set backwardTraversalKeys = - new HashSet<>(storedBackwardTraversalKeys); + Set storedForwardTraversalKeys = editor.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); + Set storedBackwardTraversalKeys = editor.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); + Set forwardTraversalKeys = new HashSet<>(storedForwardTraversalKeys); + Set backwardTraversalKeys = new HashSet<>(storedBackwardTraversalKeys); if (editor.isEditable()) { forwardTraversalKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); backwardTraversalKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK)); diff --git a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/text.properties b/core/src/main/resources/com/github/weisj/darklaf/properties/ui/text.properties index 927e881a..aa73f242 100644 --- a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/text.properties +++ b/core/src/main/resources/com/github/weisj/darklaf/properties/ui/text.properties @@ -40,6 +40,7 @@ TextField.inactiveBackground = %textBackgroundInactive TextField.background = %textBackground TextField.selectionBackground = %textSelectionBackground TextField.selectionForeground = %textSelectionForeground +TextField.keepSelectionOnFocusLost = true TextField.arc = %arc TextField.focusArc = %arcFocus