Browse Source

Fixed caret painting for insert mode with light themes.

pull/170/head
weisj 5 years ago
parent
commit
3b6573a850
  1. 4
      core/src/main/java/com/github/weisj/darklaf/color/DarkColorModelRGB.java
  2. 19
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java
  3. 4
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkEditorPaneUI.java
  4. 12
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java
  5. 63
      core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkKeyTypedAction.java
  6. 2
      core/src/main/java/com/github/weisj/darklaf/ui/text/action/DarkTextAction.java
  7. 76
      core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeleteNextCharAction.java
  8. 76
      core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeletePreviousCharAction.java

4
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()}; 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) { public Color getColorFromValues(final int[] values) {
return new Color(values[0], values[1], values[2]); return new Color(values[0], values[1], values[2]);
} }

19
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 pasteOnMiddleMouseClick;
private boolean insertMode; private boolean insertMode;
private boolean deleteCharMode; private boolean expandMode;
public DarkCaret() { public DarkCaret() {
this(null, null); this(null, null);
@ -82,8 +82,8 @@ public class DarkCaret extends DefaultCaret implements UIResource {
} }
} }
public void setDeleteCharMode(final boolean deleteCharMode) { public void setExpandMode(final boolean expandMode) {
this.deleteCharMode = deleteCharMode; this.expandMode = expandMode;
} }
@Override @Override
@ -91,10 +91,9 @@ public class DarkCaret extends DefaultCaret implements UIResource {
int mark = super.getMark(); int mark = super.getMark();
int dot = super.getDot(); int dot = super.getDot();
JTextComponent target = getComponent(); JTextComponent target = getComponent();
if (isInsertMode() if (expandMode && isInsertMode()
&& target != null && target != null
&& mark == dot && mark == dot
&& !deleteCharMode
&& !isEndOfLine(target, dot)) { && !isEndOfLine(target, dot)) {
mark += 1; 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];
}
} }

4
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.HTMLDocument;
import javax.swing.text.html.StyleSheet; 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.ui.text.action.ToggleInsertAction;
import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil; import com.github.weisj.darklaf.util.PropertyUtil;
@ -262,8 +260,6 @@ public class DarkEditorPaneUI extends DarkTextUI {
am.put(TransferHandler.getPasteAction().getValue(Action.NAME), am.put(TransferHandler.getPasteAction().getValue(Action.NAME),
TransferHandler.getPasteAction()); TransferHandler.getPasteAction());
if (editorKit instanceof DefaultEditorKit) { if (editorKit instanceof DefaultEditorKit) {
am.put(DefaultEditorKit.deletePrevCharAction, new DeletePreviousCharAction());
am.put(DefaultEditorKit.deleteNextCharAction, new DeleteNextCharAction());
am.put(TOGGLE_INSERT, new ToggleInsertAction()); am.put(TOGGLE_INSERT, new ToggleInsertAction());
} }
return am; return am;

12
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.list.DarkListUI;
import com.github.weisj.darklaf.ui.table.DarkTableCellBorder; import com.github.weisj.darklaf.ui.table.DarkTableCellBorder;
import com.github.weisj.darklaf.ui.table.DarkTableUI; 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.DarkKeyTypedAction;
import com.github.weisj.darklaf.ui.text.action.DeletePreviousCharAction;
import com.github.weisj.darklaf.ui.text.action.ToggleInsertAction; import com.github.weisj.darklaf.ui.text.action.ToggleInsertAction;
import com.github.weisj.darklaf.ui.tree.DarkTreeUI; import com.github.weisj.darklaf.ui.tree.DarkTreeUI;
import com.github.weisj.darklaf.util.DarkUIUtil; 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); Action action = new TextActionWrapper((TextAction) obj);
componentMap.put(action.getValue(Action.NAME), action); 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()); map.put(TOGGLE_INSERT, new ToggleInsertAction());
} }
} }
@ -396,6 +393,13 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi
return componentMap; 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. * Invoked when the focus accelerator changes, this will update the key bindings as necessary.
* *

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

2
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; if (textComponent == null) return;
Caret c = textComponent.getCaret(); Caret c = textComponent.getCaret();
if (c instanceof DarkCaret) { if (c instanceof DarkCaret) {
((DarkCaret) c).setDeleteCharMode(isDeleteMode); ((DarkCaret) c).setExpandMode(isDeleteMode);
} }
} }
} }

76
core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeleteNextCharAction.java

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

76
core/src/main/java/com/github/weisj/darklaf/ui/text/action/DeletePreviousCharAction.java

@ -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);
}
}
Loading…
Cancel
Save