From 5bcda602f64f741842a95a52f3d31978c2673ffe Mon Sep 17 00:00:00 2001 From: weisj Date: Sat, 2 May 2020 23:44:42 +0200 Subject: [PATCH] Improved caret position for non standard carets. --- .../weisj/darklaf/ui/text/DarkCaret.java | 63 ++++++++++--------- .../text/{ => action}/SelectLineAction.java | 3 +- .../text/{ => action}/SelectWordAction.java | 3 +- .../test/java/ui/text/TextComponentDemo.java | 4 +- 4 files changed, 39 insertions(+), 34 deletions(-) rename core/src/main/java/com/github/weisj/darklaf/ui/text/{ => action}/SelectLineAction.java (99%) rename core/src/main/java/com/github/weisj/darklaf/ui/text/{ => action}/SelectWordAction.java (98%) 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 56249111..95c7c58e 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 @@ -37,6 +37,8 @@ import javax.swing.text.*; import javax.swing.text.DefaultHighlighterDark.DarkHighlightPainter; import com.github.weisj.darklaf.graphics.PaintUtil; +import com.github.weisj.darklaf.ui.text.action.SelectLineAction; +import com.github.weisj.darklaf.ui.text.action.SelectWordAction; /** * @author Jannis Weis @@ -88,11 +90,6 @@ public class DarkCaret extends DefaultCaret implements UIResource { } } - @Override - protected void positionCaret(final MouseEvent e) { - super.positionCaret(e); - } - public boolean isAlwaysVisible() { return alwaysVisible; } @@ -117,26 +114,32 @@ public class DarkCaret extends DefaultCaret implements UIResource { } public enum CaretStyle { - VERTICAL_LINE_STYLE(1), - UNDERLINE_STYLE(0), - BLOCK_STYLE(0), - BLOCK_BORDER_STYLE(0), - THICK_VERTICAL_LINE_STYLE(1); - - private final int width; + VERTICAL_LINE_STYLE(1, false), + UNDERLINE_STYLE(1, true), + BLOCK_STYLE(1, true), + BLOCK_BORDER_STYLE(1, true), + THICK_VERTICAL_LINE_STYLE(2, false); + + private final int size; + private final boolean charWidth; + + CaretStyle(final int size, final boolean charWidth) { + this.size = size; + this.charWidth = charWidth; + } - CaretStyle(final int width) { - this.width = width; + public int getSize() { + return size; } - public int getWidth() { - return width; + public boolean isCharacterWidth() { + return charWidth; } } @Override public double getWidth() { - return style.getWidth(); + return getStyle().getSize(); } /** @@ -264,15 +267,20 @@ public class DarkCaret extends DefaultCaret implements UIResource { } } + @Override + public boolean isVisible() { + return super.isVisible() || isAlwaysVisible(); + } + @Override public void paint(final Graphics g) { - if (isVisible() || alwaysVisible) { + if (isVisible()) { JTextComponent textArea = getComponent(); g.setColor(textArea.getCaretColor()); TextUI mapper = textArea.getUI(); Rectangle r; try { - r = mapper.modelToView(textArea, getDot(), Position.Bias.Forward); + r = mapper.modelToView(textArea, getDot(), getDotBias()); } catch (BadLocationException ex) { r = new Rectangle(0, 0, 0, 0); } @@ -294,28 +302,25 @@ public class DarkCaret extends DefaultCaret implements UIResource { r.y += 1; Color textAreaBg = textArea.getBackground(); + if (textAreaBg == null) { + textAreaBg = Color.white; + } switch (style) { case BLOCK_STYLE : - if (textAreaBg == null) { - textAreaBg = Color.white; - } g.setXORMode(textAreaBg); g.fillRect(r.x, r.y, r.width, r.height); break; case BLOCK_BORDER_STYLE : - PaintUtil.drawRect(g, r.x, r.y, r.width, r.height, 1); + PaintUtil.drawRect(g, r.x, r.y, r.width, r.height, style.getSize()); break; case UNDERLINE_STYLE : - if (textAreaBg == null) { - textAreaBg = Color.white; - } g.setXORMode(textAreaBg); int y = r.y + r.height; - g.fillRect(r.x, y - 1, r.width, 1); + g.fillRect(r.x, y - style.getSize(), r.width, style.getSize()); break; case THICK_VERTICAL_LINE_STYLE : case VERTICAL_LINE_STYLE : - g.fillRect(r.x, r.y, style.getWidth(), r.height); + g.fillRect(r.x, r.y, style.getSize(), r.height); break; } } @@ -347,7 +352,7 @@ public class DarkCaret extends DefaultCaret implements UIResource { } private void validateWidth(final Rectangle rect) { - if (rect != null && rect.width <= 1) { + if (rect != null && (rect.width <= 1 || getStyle().isCharacterWidth())) { JTextComponent textArea = getComponent(); try { textArea.getDocument().getText(getDot(), 1, seg); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/SelectLineAction.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectLineAction.java similarity index 99% rename from core/src/main/java/com/github/weisj/darklaf/ui/text/SelectLineAction.java rename to core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectLineAction.java index 51435299..e909a48c 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/SelectLineAction.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectLineAction.java @@ -20,9 +20,8 @@ * 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; +package com.github.weisj.darklaf.ui.text.action; import java.awt.event.ActionEvent; diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/SelectWordAction.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectWordAction.java similarity index 98% rename from core/src/main/java/com/github/weisj/darklaf/ui/text/SelectWordAction.java rename to core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectWordAction.java index 30f1c59c..6fc29095 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/SelectWordAction.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectWordAction.java @@ -20,9 +20,8 @@ * 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; +package com.github.weisj.darklaf.ui.text.action; import java.awt.event.ActionEvent; diff --git a/core/src/test/java/ui/text/TextComponentDemo.java b/core/src/test/java/ui/text/TextComponentDemo.java index cea48967..36e25107 100644 --- a/core/src/test/java/ui/text/TextComponentDemo.java +++ b/core/src/test/java/ui/text/TextComponentDemo.java @@ -24,6 +24,8 @@ */ package ui.text; +import java.awt.*; + import javax.swing.*; import javax.swing.border.Border; import javax.swing.text.JTextComponent; @@ -41,7 +43,7 @@ public abstract class TextComponentDemo implements Com public JComponent createComponent() { T text = createTextComponent(); text.setText(StringUtil.repeat(StringUtil.repeat("Word ", 5) + "\n", 5)); - DemoPanel panel = new DemoPanel(text); + DemoPanel panel = new DemoPanel(text, new BorderLayout(), 10); Border textBorder = new DarkTextBorder(); Border border = text.getBorder();