Skip to content

Commit 92caeeb

Browse files
committed
Display whitespace for selected newlines
1 parent 4307d3f commit 92caeeb

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

parley/src/layout/cursor.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,7 @@ impl Selection {
799799
/// This avoids allocation if the intent is to render the rectangles
800800
/// immediately.
801801
pub fn geometry_with<B: Brush>(&self, layout: &Layout<B>, mut f: impl FnMut(Rect)) {
802-
// Ensure we add some visual indicator for selected empty
803-
// lines.
804-
// Make this configurable?
805-
const MIN_RECT_WIDTH: f64 = 8.0;
802+
const NEWLINE_WHITESPACE_WIDTH_RATIO: f64 = 0.25;
806803
if self.is_collapsed() {
807804
return;
808805
}
@@ -824,6 +821,17 @@ impl Selection {
824821
let metrics = line.metrics();
825822
let line_min = metrics.min_coord as f64;
826823
let line_max = metrics.max_coord as f64;
824+
// Trailing whitespace to indicate that the newline character at the
825+
// end of this line is selected. It's based on the ascent and
826+
// descent so it doesn't change with the line height.
827+
//
828+
// TODO: the width of this whitespace should be the width of a space
829+
// (U+0020) character.
830+
let newline_whitespace = if line.break_reason() == BreakReason::Explicit {
831+
(metrics.ascent as f64 + metrics.descent as f64) * NEWLINE_WHITESPACE_WIDTH_RATIO
832+
} else {
833+
0.0
834+
};
827835
if line_ix == line_start_ix || line_ix == line_end_ix {
828836
// We only need to run the expensive logic on the first and
829837
// last lines
@@ -838,22 +846,29 @@ impl Selection {
838846
cur_x += advance;
839847
} else {
840848
if cur_x != start_x {
841-
let width = (cur_x - start_x).max(MIN_RECT_WIDTH);
842-
f(Rect::new(start_x, line_min, start_x + width, line_max));
849+
f(Rect::new(start_x, line_min, cur_x, line_max));
843850
}
844851
cur_x += advance;
845852
start_x = cur_x;
846853
}
847854
}
848855
}
849-
if cur_x != start_x || (cluster_count != 0 && line.metrics().advance == 0.0) {
850-
let width = (cur_x - start_x).max(MIN_RECT_WIDTH);
851-
f(Rect::new(start_x, line_min, start_x + width, line_max));
856+
let mut end_x = cur_x;
857+
if line_ix != line_end_ix || (cluster_count != 0 && metrics.advance == 0.0) {
858+
end_x += newline_whitespace;
859+
}
860+
if end_x != start_x {
861+
f(Rect::new(start_x, line_min, end_x, line_max));
852862
}
853863
} else {
854864
let x = metrics.offset as f64;
855-
let width = (metrics.advance as f64).max(MIN_RECT_WIDTH);
856-
f(Rect::new(x, line_min, x + width, line_max));
865+
let width = metrics.advance as f64;
866+
f(Rect::new(
867+
x,
868+
line_min,
869+
x + width + newline_whitespace,
870+
line_max,
871+
));
857872
}
858873
}
859874
}

0 commit comments

Comments
 (0)