Browse Source

Improved caret position for non standard carets.

pull/170/head
weisj 5 years ago
parent
commit
5bcda602f6
  1. 59
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java
  2. 3
      core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectLineAction.java
  3. 3
      core/src/main/java/com/github/weisj/darklaf/ui/text/action/SelectWordAction.java
  4. 4
      core/src/test/java/ui/text/TextComponentDemo.java

59
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);
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;
private final int width;
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();
switch (style) {
case BLOCK_STYLE :
if (textAreaBg == null) {
textAreaBg = Color.white;
}
switch (style) {
case BLOCK_STYLE :
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);

3
core/src/main/java/com/github/weisj/darklaf/ui/text/SelectLineAction.java → 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;

3
core/src/main/java/com/github/weisj/darklaf/ui/text/SelectWordAction.java → 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;

4
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<T extends JTextComponent> 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();

Loading…
Cancel
Save