FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#2430: a click in the text being edited on the monitor moves the caret
irgendwasmitfelix63a5a6bb38d9
diff --git a/crates/ui-egui/src/panels/graphics.rs b/crates/ui-egui/src/panels/graphics.rsindex 1b9e5438c..d6840f0dc 100644--- a/crates/ui-egui/src/panels/graphics.rs+++ b/crates/ui-egui/src/panels/graphics.rs@@ -831,7 +831,20 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fralet p = resp.interact_pointer_pos().unwrap_or(pic.center());let in_edit = edit_view.is_some_and(|v| v.hit(p));match tool {- _ if in_edit => {}+ // a click in the text being edited puts the caret there (Shift extends the selection),+ // as a press that becomes a drag already does (#2430)+ _ if in_edit => {+ if let (Some(v), Some(mut ed)) = (edit_view, editing.clone())+ && let Some(tp) = text_of(&v.spec)+ && let Some((x, y)) = v.to_local(p)+ {+ ed.caret = text_layout(tp).hit(x, y);+ if !ui.input(|i| i.modifiers.shift) {+ ed.anchor = ed.caret;+ }+ app.ui.gfx_edit = Some(ed);+ }+ }Tool::Type | Tool::VerticalType => {if let Some(v) = views.iter().find(|v| v.is_text() && v.hit(p)) {start_edit_at(app, ui, v, p, &mut actions);@@ -910,8 +923,8 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, frapainter.add(egui::Shape::convex_polygon(q.to_vec(), Color32::from_rgba_unmultiplied(80, 140, 255, 90), Stroke::NONE));}let (x, base, li) = l.caret(caret);- let line = &l.lines[li.min(l.lines.len() - 1)];- let (a, b) = (sp(&v.to_screen, x, base - line.ascent), sp(&v.to_screen, x, base + line.descent));+ let (ascent, descent) = l.lines.get(li.min(l.lines.len().saturating_sub(1))).map_or((0.0, 0.0), |line| (line.ascent, line.descent));+ let (a, b) = (sp(&v.to_screen, x, base - ascent), sp(&v.to_screen, x, base + descent));painter.line_segment([a, b], Stroke::new(1.5, Color32::WHITE));painter.line_segment([a + vec2(1.0, 0.0), b + vec2(1.0, 0.0)], Stroke::new(0.5, Color32::BLACK));let er = Rect::from_points(&v.quad()).union(Rect::from_two_pos(a, b));diff --git a/crates/ui-egui/tests/graphics_handles_ui.rs b/crates/ui-egui/tests/graphics_handles_ui.rsindex 5a7090fdd..f3c03fb51 100644--- a/crates/ui-egui/tests/graphics_handles_ui.rs+++ b/crates/ui-egui/tests/graphics_handles_ui.rs@@ -357,6 +357,26 @@ fn text_committed_by_an_input_method_is_typed_on_the_monitor() {assert_eq!(list["layers"][0]["text"], "A你好");}+/// #2430: a click (no drag) in the text being edited on the monitor puts the caret there; before,+/// only a press that moved a little did, and typing went on at the end.+#[test]+fn a_click_in_the_edited_text_moves_the_caret() {+ let mut d = Driver::new();+ d.ok("ui.set", json!({"tool": "Type"}));+ d.frames(3);+ let pic = d.rect("program.picture");+ d.ok("ui.click", json!({"x": pic[0] + pic[2] * 0.3, "y": pic[1] + pic[3] * 0.2}));+ d.ok("ui.type", json!({"text": "Title"}));+ d.frames(2);+ let text = d.rect("program.textEdit");+ // just inside the first letter: the caret goes before it+ d.ok("ui.click", json!({"x": text[0] + 1.0, "y": text[1] + text[3] / 2.0}));+ d.frames(2);+ d.ok("ui.type", json!({"text": "X"}));+ d.frames(2);+ assert_eq!(d.exec("graphics.list", json!({}))["layers"][0]["text"], "XTitle");+}+#[test]fn text_properties_switches_between_point_and_paragraph_text() {let mut d = Driver::new();