Skip to content

Commit

Permalink
Fixed line highlight after delete (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugen authored Aug 21, 2020
1 parent 3c9578f commit 2b8af0b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ public int getCurrentLineEndInParargraph() {
}

private double caretPrevY = -1;
private Selection<PS, SEG, S> lineHighlighter;
private LineSelection<PS, SEG, S> lineHighlighter;
private ObjectProperty<Paint> lineHighlighterFill;

/**
Expand Down Expand Up @@ -1228,34 +1228,13 @@ public void setLineHighlighterOn( boolean show )
{
if ( lineHighlighter != null ) return;

lineHighlighter = new SelectionImpl<>( "line-highlighter", this, path ->
{
if ( lineHighlighterFill == null ) path.setHighlightFill( Color.YELLOW );
else path.highlightFillProperty().bind( lineHighlighterFill );

path.getElements().addListener( (Change<? extends PathElement> chg) ->
{
if ( chg.next() && chg.wasAdded() || chg.wasReplaced() ) {
double width = path.getParent().getLayoutBounds().getWidth();
// The path is limited to the bounds of the text, so here it's altered to the area's width
chg.getAddedSubList().stream().skip(1).limit(2).forEach( ele -> ((LineTo) ele).setX( width ) );
// The path can wrap onto another line if enough text is inserted, so here it's trimmed
if ( chg.getAddedSize() > 5 ) path.getElements().remove( 5, 10 );
// Highlight masks the downward selection of text on the last line, so move it behind
path.toBack();
}
} );
} );
lineHighlighter = new LineSelection<>( this, lineHighlighterFill );

Runnable adjustHighlighterRange = () ->
{
if ( lineHighlighter != null )
{
int p = getCurrentParagraph();
int start = getCurrentLineStartInParargraph();
int end = getCurrentLineEndInParargraph();
if (end == 0) end++;// +1 for empty lines
lineHighlighter.selectRange( p, start, p, end );
lineHighlighter.selectCurrentLine();
}
};

Expand Down Expand Up @@ -1677,8 +1656,8 @@ private Cell<Paragraph<PS, SEG, S>, ParagraphBox<PS, SEG, S>> createCell(
if (p == null) {
// create & configure path
Val<IndexRange> range = Val.create(
() -> boxIndex != -1
? getParagraphSelection(selection, boxIndex)
() -> box.getIndex() != -1
? getParagraphSelection(selection, box.getIndex())
: EMPTY_RANGE,
selection.rangeProperty()
);
Expand Down
47 changes: 47 additions & 0 deletions richtextfx/src/main/java/org/fxmisc/richtext/LineSelection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.fxmisc.richtext;

import javafx.beans.property.ObjectProperty;
import javafx.collections.ListChangeListener.Change;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.PathElement;

class LineSelection<PS, SEG, S> extends SelectionImpl<PS, SEG, S>
{
LineSelection( GenericStyledArea<PS, SEG, S> area, ObjectProperty<Paint> lineHighlighterFill )
{
super( "line-highlighter", area, path ->
{
if ( lineHighlighterFill == null ) path.setHighlightFill( Color.YELLOW );
else path.highlightFillProperty().bind( lineHighlighterFill );

path.getElements().addListener( (Change<? extends PathElement> chg) ->
{
if ( chg.next() && chg.wasAdded() || chg.wasReplaced() ) {
double width = path.getParent().getLayoutBounds().getWidth();
// The path is limited to the bounds of the text, so here it's altered to the area's width
chg.getAddedSubList().stream().skip(1).limit(2).forEach( ele -> ((LineTo) ele).setX( width ) );
// The path can wrap onto another line if enough text is inserted, so here it's trimmed
if ( chg.getAddedSize() > 5 ) path.getElements().remove( 5, 10 );
// Highlight masks the downward selection of text on the last line, so move it behind
path.toBack();
}
} );
} );
}

@Override
public void selectRange( int start, int end )
{
selectCurrentLine();
}

public void selectCurrentLine()
{
int p = getArea().getCurrentParagraph();
int start = getArea().getAbsolutePosition( p, getArea().getCurrentLineStartInParargraph() );
int end = getArea().getAbsolutePosition( p, getArea().getCurrentLineEndInParargraph() );
super.selectRange( start, (end > start) ? end : start+1 ); // +1 for empty lines
}
}

0 comments on commit 2b8af0b

Please sign in to comment.