From 3b6573a85064e5f1b4f11662f309522248714125 Mon Sep 17 00:00:00 2001 From: weisj Date: Sun, 3 May 2020 18:13:02 +0200 Subject: [PATCH] Fixed caret painting for insert mode with light themes. --- .../darklaf/color/DarkColorModelRGB.java | 4 + .../weisj/darklaf/ui/text/DarkCaret.java | 19 +++-- .../darklaf/ui/text/DarkEditorPaneUI.java | 4 - .../weisj/darklaf/ui/text/DarkTextUI.java | 12 ++- .../ui/text/action/DarkKeyTypedAction.java | 63 +++++++++++++++ .../ui/text/action/DarkTextAction.java | 2 +- .../ui/text/action/DeleteNextCharAction.java | 76 ------------------- .../text/action/DeletePreviousCharAction.java | 76 ------------------- 8 files changed, 90 insertions(+), 166 deletions(-) create mode 100644 core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkKeyTypedAction.java delete mode 100644 core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeleteNextCharAction.java delete mode 100644 core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeletePreviousCharAction.java diff --git a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java index 635fe7d4..3bd4246d 100644 --- a/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java +++ b/core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java @@ -65,6 +65,10 @@ public class DarkColorModelRGB extends DarkColorModel { return new int[]{color.getRed(), color.getGreen(), color.getBlue()}; } + public int[] getValuesFromColorWithAlpha(final Color color) { + return new int[]{color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()}; + } + public Color getColorFromValues(final int[] values) { return new Color(values[0], values[1], values[2]); } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java index 9cb38524..16be03f3 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java @@ -56,7 +56,7 @@ public class DarkCaret extends DefaultCaret implements UIResource { private boolean pasteOnMiddleMouseClick; private boolean insertMode; - private boolean deleteCharMode; + private boolean expandMode; public DarkCaret() { this(null, null); @@ -82,8 +82,8 @@ public class DarkCaret extends DefaultCaret implements UIResource { } } - public void setDeleteCharMode(final boolean deleteCharMode) { - this.deleteCharMode = deleteCharMode; + public void setExpandMode(final boolean expandMode) { + this.expandMode = expandMode; } @Override @@ -91,10 +91,9 @@ public class DarkCaret extends DefaultCaret implements UIResource { int mark = super.getMark(); int dot = super.getDot(); JTextComponent target = getComponent(); - if (isInsertMode() + if (expandMode && isInsertMode() && target != null && mark == dot - && !deleteCharMode && !isEndOfLine(target, dot)) { mark += 1; } @@ -419,4 +418,14 @@ public class DarkCaret extends DefaultCaret implements UIResource { } } } + + private char getCharAtCaret() { + JTextComponent textArea = getComponent(); + try { + textArea.getDocument().getText(getDot(), 1, seg); + } catch (BadLocationException e) { + return ' '; + } + return seg.array[seg.offset]; + } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkEditorPaneUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkEditorPaneUI.java index 86d345fe..822b2271 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkEditorPaneUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkEditorPaneUI.java @@ -35,8 +35,6 @@ import javax.swing.text.*; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.StyleSheet; -import com.github.weisj.darklaf.ui.text.action.DeleteNextCharAction; -import com.github.weisj.darklaf.ui.text.action.DeletePreviousCharAction; import com.github.weisj.darklaf.ui.text.action.ToggleInsertAction; import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyUtil; @@ -262,8 +260,6 @@ public class DarkEditorPaneUI extends DarkTextUI { am.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); if (editorKit instanceof DefaultEditorKit) { - am.put(DefaultEditorKit.deletePrevCharAction, new DeletePreviousCharAction()); - am.put(DefaultEditorKit.deleteNextCharAction, new DeleteNextCharAction()); am.put(TOGGLE_INSERT, new ToggleInsertAction()); } return am; 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 925076f5..9c86ed15 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 @@ -48,8 +48,7 @@ import com.github.weisj.darklaf.graphics.PaintUtil; import com.github.weisj.darklaf.ui.list.DarkListUI; import com.github.weisj.darklaf.ui.table.DarkTableCellBorder; import com.github.weisj.darklaf.ui.table.DarkTableUI; -import com.github.weisj.darklaf.ui.text.action.DeleteNextCharAction; -import com.github.weisj.darklaf.ui.text.action.DeletePreviousCharAction; +import com.github.weisj.darklaf.ui.text.action.DarkKeyTypedAction; import com.github.weisj.darklaf.ui.text.action.ToggleInsertAction; import com.github.weisj.darklaf.ui.tree.DarkTreeUI; import com.github.weisj.darklaf.util.DarkUIUtil; @@ -385,8 +384,6 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi Action action = new TextActionWrapper((TextAction) obj); componentMap.put(action.getValue(Action.NAME), action); } - map.put(DefaultEditorKit.deletePrevCharAction, new DeletePreviousCharAction()); - map.put(DefaultEditorKit.deleteNextCharAction, new DeleteNextCharAction()); map.put(TOGGLE_INSERT, new ToggleInsertAction()); } } @@ -396,6 +393,13 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi return componentMap; } + @Override + protected Keymap createKeymap() { + Keymap km = super.createKeymap(); + km.setDefaultAction(new DarkKeyTypedAction()); + return km; + } + /** * Invoked when the focus accelerator changes, this will update the key bindings as necessary. * diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkKeyTypedAction.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkKeyTypedAction.java new file mode 100644 index 00000000..8d0d5df8 --- /dev/null +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkKeyTypedAction.java @@ -0,0 +1,63 @@ +/* + * MIT License + * + * Copyright (c) 2020 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, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ +package com.github.weisj.darklaf.ui.text.action; + +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; + +import javax.swing.text.Caret; +import javax.swing.text.DefaultEditorKit; +import javax.swing.text.JTextComponent; + +import com.github.weisj.darklaf.ui.text.DarkCaret; + +public class DarkKeyTypedAction extends DefaultEditorKit.DefaultKeyTypedAction { + + @Override + public void actionPerformed(final ActionEvent e) { + JTextComponent target = getTextComponent(e); + if ((target != null) && (e != null)) { + if ((!target.isEditable()) || (!target.isEnabled())) { + return; + } + String content = e.getActionCommand(); + Caret c = target.getCaret(); + if (c instanceof DarkCaret) { + boolean isDelete = !content.isEmpty(); + if (isDelete) { + char key = content.charAt(0); + isDelete = key == KeyEvent.VK_DELETE || key == KeyEvent.VK_BACK_SPACE; + } + if (((DarkCaret) c).isInsertMode()) { + ((DarkCaret) c).setExpandMode(!isDelete); + } + super.actionPerformed(e); + ((DarkCaret) c).setExpandMode(false); + return; + } + } + super.actionPerformed(e); + } +} diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkTextAction.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkTextAction.java index 58380e4d..e1932caa 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkTextAction.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkTextAction.java @@ -45,7 +45,7 @@ public abstract class DarkTextAction extends TextAction { if (textComponent == null) return; Caret c = textComponent.getCaret(); if (c instanceof DarkCaret) { - ((DarkCaret) c).setDeleteCharMode(isDeleteMode); + ((DarkCaret) c).setExpandMode(isDeleteMode); } } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeleteNextCharAction.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeleteNextCharAction.java deleted file mode 100644 index 535eff42..00000000 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeleteNextCharAction.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 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, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ -package com.github.weisj.darklaf.ui.text.action; - -import java.awt.event.ActionEvent; - -import javax.swing.*; -import javax.swing.text.*; - -public class DeleteNextCharAction extends DarkTextAction { - - public DeleteNextCharAction() { - super(DefaultEditorKit.deleteNextCharAction); - } - - @Override - public void actionPerformed(final ActionEvent e) { - JTextComponent target = getTextComponent(e); - setupDeleteMode(target, true); - boolean beep = true; - if ((target != null) && (target.isEditable())) { - try { - Document doc = target.getDocument(); - Caret caret = target.getCaret(); - int dot = caret.getDot(); - int mark = caret.getMark(); - if (dot != mark) { - doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); - beep = false; - } else if (dot < doc.getLength()) { - int delChars = 1; - - if (dot < doc.getLength() - 1) { - String dotChars = doc.getText(dot, 2); - char c0 = dotChars.charAt(0); - char c1 = dotChars.charAt(1); - - if (c0 >= '\uD800' && c0 <= '\uDBFF' && - c1 >= '\uDC00' && c1 <= '\uDFFF') { - delChars = 2; - } - } - - doc.remove(dot, delChars); - beep = false; - } - } catch (BadLocationException ignored) {} - } - if (beep) { - UIManager.getLookAndFeel().provideErrorFeedback(target); - } - setupDeleteMode(target, false); - } -} diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeletePreviousCharAction.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeletePreviousCharAction.java deleted file mode 100644 index 3e362b8d..00000000 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeletePreviousCharAction.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 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, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ -package com.github.weisj.darklaf.ui.text.action; - -import java.awt.event.ActionEvent; - -import javax.swing.*; -import javax.swing.text.*; - -public class DeletePreviousCharAction extends DarkTextAction { - - public DeletePreviousCharAction() { - super(DefaultEditorKit.deletePrevCharAction); - } - - @Override - public void actionPerformed(final ActionEvent e) { - JTextComponent target = getTextComponent(e); - setupDeleteMode(target, true); - boolean beep = true; - if ((target != null) && (target.isEditable())) { - try { - Document doc = target.getDocument(); - Caret caret = target.getCaret(); - int dot = caret.getDot(); - int mark = caret.getMark(); - if (dot != mark) { - doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); - beep = false; - } else if (dot > 0) { - int delChars = 1; - - if (dot > 1) { - String dotChars = doc.getText(dot - 2, 2); - char c0 = dotChars.charAt(0); - char c1 = dotChars.charAt(1); - - if (c0 >= '\uD800' && c0 <= '\uDBFF' && - c1 >= '\uDC00' && c1 <= '\uDFFF') { - delChars = 2; - } - } - - doc.remove(dot - delChars, delChars); - beep = false; - } - } catch (BadLocationException ignored) {} - } - if (beep) { - UIManager.getLookAndFeel().provideErrorFeedback(target); - } - setupDeleteMode(target, false); - } -}