Skip to content

Commit

Permalink
Modify caret
Browse files Browse the repository at this point in the history
  • Loading branch information
naotsugu committed Jan 28, 2024
1 parent fbeba3c commit 2eeb015
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@
* @author Naotsugu Kobayashi
*/
public interface CaretMulti extends Caret {

void add(long charOffset);

void clear();

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public interface EditorModel extends EditorDraw {

// -- mouse behavior ------------------------------------------------------

void click(double x, double y);
void click(double x, double y, boolean isShortcutDown);
void clickDouble(double x, double y);
void clickTriple(double x, double y);
void dragged(double x, double y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ public class CaretCore implements CaretMulti {
/** The planets. */
private final List<Caret> planets;

/** The offset to layout line function. */
private final Function<Long, LayoutLine> offsetToLine;


/**
* Constructor.
* @param core the core caret
* @param planets the planets
* @param offsetToLine the offset to layout line function
*/
private CaretCore(Caret core, List<Caret> planets) {
private CaretCore(Caret core, List<Caret> planets, Function<Long, LayoutLine> offsetToLine) {
this.core = core;
this.planets = planets;
this.offsetToLine = offsetToLine;
}


Expand All @@ -56,11 +61,26 @@ private CaretCore(Caret core, List<Caret> planets) {
public static CaretMulti of(Function<Long, LayoutLine> offsetToLine) {
return new CaretCore(
new CaretImpl(offsetToLine),
new ArrayList<>()
new ArrayList<>(),
offsetToLine
);
}


@Override
public void add(long charOffset) {
Caret planet = new CaretImpl(offsetToLine);
planet.at(charOffset, true);
planets.add(planet);
}

@Override
public void clear() {
planets.clear();
}



@Override
public void draw(GraphicsContext gc, double margin, double hScrolled) {
core.draw(gc, margin, hScrolled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,15 +590,19 @@ public void vScrolled(int oldValue, int newValue) {

// <editor-fold defaultstate="collapsed" desc="mouse behavior">
@Override
public void click(double x, double y) {
public void click(double x, double y, boolean isShortcutDown) {
if (selection.isDragging()) {
selection.to(caret.caretPoint());
selection.endDragging();
return;
}
selection.clear();
long offset = texts.at(x - screen.textLeft(), y);
caret.at(offset, true);
if (isShortcutDown) {
caret.add(offset);
} else {
caret.at(offset, true);
}
}
@Override
public void clickDouble(double x, double y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void handleMouseClicked(MouseEvent e) {
canvas.requestFocus();
if (e.getButton() == MouseButton.PRIMARY && e.getTarget() == canvas) {
switch (e.getClickCount()) {
case 1 -> model.click(e.getX(), e.getY());
case 1 -> model.click(e.getX(), e.getY(), e.isShortcutDown());
case 2 -> model.clickDouble(e.getX(), e.getY());
case 3 -> model.clickTriple(e.getX(), e.getY());
}
Expand Down

0 comments on commit 2eeb015

Please sign in to comment.