-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
262 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
modules/ui-model/src/main/java/com/mammb/code/editor/ui/model/SelectionDraw.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mammb.code.editor.ui.model; | ||
|
||
import com.mammb.code.editor.javafx.layout.FxFonts; | ||
import com.mammb.code.editor.model.layout.TextRun; | ||
import com.mammb.code.editor.model.text.OffsetPoint; | ||
import javafx.scene.canvas.GraphicsContext; | ||
import javafx.scene.paint.Color; | ||
import java.util.Objects; | ||
|
||
/** | ||
* SelectionDraw. | ||
* @author Naotsugu Kobayashi | ||
*/ | ||
public interface SelectionDraw { | ||
|
||
/** The color. */ | ||
Color color = new Color(0.6784314F, 0.84705883F, 0.9019608F, 0.3); | ||
|
||
/** | ||
* Get the min select offset. | ||
* @return the min select offset | ||
*/ | ||
OffsetPoint min(); | ||
|
||
/** | ||
* Get the max select offset. | ||
* @return the max select offset | ||
*/ | ||
OffsetPoint max(); | ||
|
||
|
||
/** | ||
* Draw the selection. | ||
* @param gc the graphics context | ||
* @param run the text run | ||
* @param offsetY the position y | ||
* @param left the left position of run(margin included) | ||
*/ | ||
default void draw(GraphicsContext gc, TextRun run, double offsetY, double left) { | ||
|
||
final OffsetPoint min = min(); | ||
final OffsetPoint max = max(); | ||
|
||
if (min == null || Objects.equals(min, max)) { | ||
return; | ||
} | ||
|
||
long runStart = run.offset(); | ||
long runEnd = runStart + run.length(); | ||
|
||
if (max().offset() >= runStart && min().offset() < runEnd) { | ||
|
||
final String text = run.text(); | ||
|
||
if (runEnd <= max().offset() && | ||
((text.length() == 1 && text.charAt(0) == '\n') || | ||
(text.length() == 2 && text.charAt(0) == '\r' && text.charAt(1) == '\n'))) { | ||
|
||
gc.setFill(color); | ||
gc.fillRect(run.layout().x() + left, offsetY, FxFonts.uppercaseLetterWidth(gc.getFont()), run.textLine().leadingHeight()); | ||
|
||
} else { | ||
|
||
double x1 = run.offsetToX().apply(Math.toIntExact(Math.max(min().offset(), runStart) - runStart)); | ||
double x2 = run.offsetToX().apply(Math.toIntExact(Math.min(max().offset(), runEnd) - runStart)); | ||
|
||
gc.setFill(color); | ||
gc.fillRect(x1 + left, offsetY, x2 - x1, run.textLine().leadingHeight()); | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
modules/ui-model/src/main/java/com/mammb/code/editor/ui/model/impl/CaretSelection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mammb.code.editor.ui.model.impl; | ||
|
||
import com.mammb.code.editor.model.text.OffsetPoint; | ||
import com.mammb.code.editor.ui.model.SelectionDraw; | ||
|
||
/** | ||
* CaretSelection. | ||
* @author Naotsugu Kobayashi | ||
*/ | ||
public class CaretSelection implements SelectionDraw { | ||
|
||
/** The selection open offset. */ | ||
private final OffsetPoint start; | ||
|
||
/** The selection close offset caret. */ | ||
private final CaretLine caretLine; | ||
|
||
|
||
/** | ||
* Constructor. | ||
* @param caretLine the caret | ||
*/ | ||
public CaretSelection(CaretLine caretLine) { | ||
if (caretLine == null) { | ||
this.start = null; | ||
this.caretLine = null; | ||
} else { | ||
this.start = caretLine.point(); | ||
this.caretLine = caretLine; | ||
} | ||
} | ||
|
||
|
||
public boolean isInvalid() { | ||
return start == null || caretLine.getLine() == null; | ||
} | ||
|
||
|
||
@Override | ||
public OffsetPoint min() { | ||
if (isInvalid()) { | ||
return null; | ||
} | ||
return (start.offset() <= caretLine.getBar().offset()) | ||
? start | ||
: caretLine.point(); | ||
} | ||
|
||
|
||
@Override | ||
public OffsetPoint max() { | ||
if (isInvalid()) { | ||
return null; | ||
} | ||
return (start.offset() <= caretLine.getBar().offset()) | ||
? caretLine.point() | ||
: start; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
modules/ui-model/src/main/java/com/mammb/code/editor/ui/model/impl/CaretSelections.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mammb.code.editor.ui.model.impl; | ||
|
||
import com.mammb.code.editor.model.layout.TextRun; | ||
import javafx.scene.canvas.GraphicsContext; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* CaretSelections. | ||
* @author Naotsugu Kobayashi | ||
*/ | ||
public class CaretSelections { | ||
|
||
private final List<CaretSelection> carets; | ||
|
||
|
||
public CaretSelections(List<CaretSelection> carets) { | ||
this.carets = carets; | ||
} | ||
|
||
|
||
public static CaretSelections of(List<CaretLine> caretLines) { | ||
return new CaretSelections(caretLines.stream() | ||
.map(CaretSelection::new) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
public void draw(GraphicsContext gc, TextRun run, double offsetY, double left) { | ||
carets.removeIf(CaretSelection::isInvalid); | ||
carets.forEach(c -> c.draw(gc, run, offsetY, left)); | ||
} | ||
|
||
|
||
public void clear() { | ||
carets.clear(); | ||
} | ||
|
||
|
||
private void ensureValid() { | ||
carets.removeIf(CaretSelection::isInvalid); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.