aboutsummaryrefslogtreecommitdiff
path: root/src/text.rs
diff options
context:
space:
mode:
authorPolesznyák Márk <contact@pml68.dev>2025-10-26 20:22:20 +0100
committerPolesznyák Márk <contact@pml68.dev>2025-10-26 20:22:20 +0100
commit0dcf63d06e17aa64caa64d26392d3187f9b730ea (patch)
tree1800e235cb33167fedc270742c16eef2ffb0c926 /src/text.rs
parentfix(wip): multi line text selection box drawing (works for non-wrapped) (diff)
downloadiced_selection-0dcf63d06e17aa64caa64d26392d3187f9b730ea.tar.gz
feat: clean up debugging code, add multi-line fix to `Rich`
Diffstat (limited to 'src/text.rs')
-rw-r--r--src/text.rs92
1 files changed, 30 insertions, 62 deletions
diff --git a/src/text.rs b/src/text.rs
index 82e9c2c..cd066c3 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -199,11 +199,11 @@ impl State {
.grapheme_position(end.line, end.index)
.unwrap_or_default();
- dbg!((
+ (
end_row.saturating_sub(start_row) + 1,
start_position,
end_position,
- ))
+ )
}
fn grapheme_position(
@@ -241,15 +241,8 @@ impl State {
})
});
- let (run_idx, glyph, _) = glyphs
+ let (_, glyph, _) = glyphs
.find(|(run_idx, glyph, text)| {
- let new_run_index = if Some(*run_idx) != last_run_index {
- last_run_graphemes = 0;
- Some(*run_idx)
- } else {
- last_run_index
- };
-
if Some(glyph.start) != last_start {
last_grapheme_count =
text[glyph.start..glyph.end].graphemes(false).count();
@@ -258,17 +251,20 @@ impl State {
last_run_graphemes += last_grapheme_count;
real_index += last_grapheme_count;
- if new_run_index != last_run_index && graphemes_seen < index
+ if Some(*run_idx) != last_run_index
+ && graphemes_seen < index
{
real_index = last_grapheme_count;
+ last_run_graphemes = last_grapheme_count;
}
- } else if new_run_index != last_run_index
+ } else if Some(*run_idx) != last_run_index
&& graphemes_seen < index
{
real_index = 0;
+ last_run_graphemes = 0;
}
- last_run_index = new_run_index;
+ last_run_index = Some(*run_idx);
graphemes_seen >= index
})
@@ -276,38 +272,28 @@ impl State {
real_index -= graphemes_seen.saturating_sub(index);
real_index =
- dbg!(real_index.saturating_sub(last_run_index? - first_run_index?));
+ real_index.saturating_sub(last_run_index? - first_run_index?);
last_run_graphemes = last_run_graphemes
- .saturating_sub(last_run_index? - first_run_index? - 1);
-
- let advance = if real_index == 0 {
- 0.0
+ .saturating_sub(last_run_index? - first_run_index?);
+
+ let advance = if last_run_index? - first_run_index? <= 1 {
+ if real_index == 0 {
+ 0.0
+ } else {
+ glyph.w
+ * (1.0
+ - last_run_graphemes.saturating_sub(real_index) as f32
+ / last_grapheme_count.max(1) as f32)
+ - glyph.w * (last_run_index? - first_run_index?) as f32
+ }
} else {
- glyph.w
+ -(glyph.w
* (1.0
- - last_run_graphemes.saturating_sub(real_index) as f32
- / last_grapheme_count.max(1) as f32)
+ + last_run_graphemes.saturating_sub(real_index) as f32
+ / last_grapheme_count.max(1) as f32))
};
- // println!(
- // "text: total: {} index: {}\nreal: total: {} index: {}\nlast_seen: {}\n\n",
- // graphemes_seen,
- // index,
- // last_run_graphemes,
- // real_index,
- // last_grapheme_count
- // );
-
- // println!(
- // "{}, {}: {}, {} (index {})",
- // line,
- // index,
- // glyph.x + glyph.x_offset * glyph.font_size + advance,
- // glyph.y - glyph.y_offset * glyph.font_size,
- // run_idx
- // );
-
Some((
last_run_index?,
Point::new(
@@ -407,12 +393,12 @@ where
state.last_click,
);
+ let (line, index) = state
+ .grapheme_line_and_index(position)
+ .unwrap_or((0, 0));
+
match click.kind() {
click::Kind::Single => {
- let (line, index) = state
- .grapheme_line_and_index(position)
- .unwrap_or((0, 0));
-
let new_end = SelectionEnd { line, index };
if state.keyboard_modifiers.shift() {
@@ -424,23 +410,15 @@ where
state.dragging = Some(Dragging::Grapheme);
}
click::Kind::Double => {
- let (line, index) = state
- .grapheme_line_and_index(position)
- .unwrap_or((0, 0));
-
state.selection.select_word(
line,
index,
&state.paragraph,
);
- state.dragging = None;
+ state.dragging = Some(Dragging::Word);
}
click::Kind::Triple => {
- let (line, _) = state
- .grapheme_line_and_index(position)
- .unwrap_or((0, 0));
-
state.selection.select_line(line, &state.paragraph);
state.dragging = Some(Dragging::Line);
}
@@ -652,16 +630,6 @@ where
.floor()
* line_height;
- // The correct code, uncomment when glyphs report a correct `y` value.
- //
- //let height = dbg!(end.y) - baseline_y - 0.5;
- //let rows =
- // usize::max(dbg!((height / line_height).ceil() as usize), 1);
- //
- // Temporary solution
- //let rows =
- // state.selection.end.line - state.selection.start.line + 1;
-
for row in 0..rows {
let (x, width) = if row == 0 {
(