PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): Type tool caret stays solid after typing, navigation or a click, then resumes blinking (#5511) (#5512)
safvanedgeaecac608b46a
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex acbf95063..ad2ddafa5 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -5498,6 +5498,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers){e.dragging = false;e.resize = None;+ app.ui.caret_blink_time = app.last_frame_time;}if tool == Tool::Pen {crate::vector_ui::pen_up(app);diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 4e82be710..e68a6d555 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -657,7 +657,7 @@ pub struct PhotocraftApp {/// The document each of `ui.views` belongs to, as of the last [`Self::sync_views`].view_docs: Vec<DocId>,pub fps: f32,- last_frame_time: f64,+ pub(crate) last_frame_time: f64,thumbs: HashMap<(photocraft_doc::LayerId, u8), (u64, egui::TextureHandle)>,/// The document area each layer thumbnail in `thumbs` frames (the canvas, or the layer's/// bounds with Clip Thumbnails to Layer Bounds; #4281).diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rsindex 373b6a479..d464cb3cc 100644--- a/crates/ui-egui/src/state.rs+++ b/crates/ui-egui/src/state.rs@@ -1076,6 +1076,9 @@ pub struct UiState {/// Inline type editing session, if any.#[serde(default)]pub text_edit: Option<TextEdit>,+ /// Clock time in seconds when type cursor blink was last reset (e.g. on keypress/caret move).+ #[serde(skip)]+ pub caret_blink_time: f64,/// Tool to restore when an inline type edit session commits or cancels (e.g. Move tool, #1132).#[serde(skip)]pub text_edit_return_tool: Option<Tool>,@@ -1310,6 +1313,7 @@ impl Default for UiState {spring: None,recent_files: Vec::new(),text_edit: None,+ caret_blink_time: 0.0,text_edit_return_tool: None,type_transform: None,type_transform_pivot: None,diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex d73bb2e2c..a8125201d 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -301,6 +301,7 @@ pub fn pointer_up(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {if let Some(e) = app.ui.text_edit.as_mut() {e.dragging = false;e.resize = None;+ app.ui.caret_blink_time = app.last_frame_time;return;}let (w, h) = ((end[0] - start[0]).abs(), (end[1] - start[1]).abs());@@ -340,6 +341,7 @@ pub fn pointer_up(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {}// Like Photoshop: the placeholder is selected, so typing replaces it.let n = text.chars().count();+ app.ui.caret_blink_time = app.last_frame_time;app.ui.text_edit = Some(TextEdit { layer: id, caret: n, anchor: 0, session: key, created: true, mask, dragging: false, resize: None, preedit: None });app.ui.text_edit_return_tool = None;}@@ -789,7 +791,11 @@ pub fn handle_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {e.caret = e.caret.min(n);e.anchor = e.anchor.min(n);}- ctx.request_repaint_after(std::time::Duration::from_millis(530)); // caret blink+ let time = ctx.input(|i| i.time);+ let elapsed = (time - app.ui.caret_blink_time).max(0.0);+ let period = 0.530;+ let remaining = period - (elapsed % period);+ ctx.request_repaint_after(std::time::Duration::from_secs_f64(remaining.clamp(0.010, period)));let events = ctx.input(|i| i.events.clone());let vertical_flow = is_vertical(app, id);let mut handled = vec![false; events.len()];@@ -942,6 +948,7 @@ pub fn handle_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {}}if handled.iter().any(|h| *h) {+ app.ui.caret_blink_time = ctx.input(|i| i.time);let mut k = 0;ctx.input_mut(|i| {i.events.retain(|_| {@@ -1000,6 +1007,9 @@ pub fn commit_with(app: &mut PhotocraftApp, mods: egui::Modifiers) {/// Selection highlight, caret and text frame over the canvas.pub fn draw_overlay(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {let Some(ed) = app.ui.text_edit.clone() else { return };+ if ed.dragging {+ app.ui.caret_blink_time = painter.ctx().input(|i| i.time);+ }let id = LayerId(ed.layer);let Some((l, aff, text)) = layout(app, id) else { return };if ed.mask {@@ -1083,9 +1093,10 @@ pub fn draw_overlay(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewX}}} else {- // Blinking caret (Photoshop's ~0.53 s rhythm), solid while dragging.+ // Blinking caret (Photoshop's ~0.53 s rhythm), solid while dragging or immediately after interaction.let time = painter.ctx().input(|i| i.time);- if ed.dragging || (time * 1000.0 / 530.0) as i64 % 2 == 0 {+ let elapsed = (time - app.ui.caret_blink_time).max(0.0);+ if ed.dragging || (elapsed * 1000.0 / 530.0) as i64 % 2 == 0 {let (x, top, bot) = l.caret(a);let (x, top, bot) = if l.lines.is_empty() { (0.0, -(12.0 * l.px_per_pt.max(1.0)), 3.0) } else { (x, top, bot) };let c = if crate::theme::Tokens::get(painter.ctx()).pro { Color32::WHITE } else { Color32::BLACK };@@ -2700,6 +2711,51 @@ mod tests {assert_eq!(word_boundary("hello big world", 7, false), 6);assert_eq!(word_boundary("hello big world", 15, false), 10);}+ #[test]+ fn arrow_keys_reset_caret_blink_phase() {+ let mut app = app();+ pointer_up(&mut app, [10.0, 50.0], [10.0, 50.0]);+ insert(&mut app, "Hello");+ app.ui.caret_blink_time = 0.0;+ let ctx = egui::Context::default();++ let has_caret = |app: &mut PhotocraftApp, ctx: &egui::Context, time: f64| -> bool {+ let mut out = ctx.run_ui(egui::RawInput { time: Some(time), ..Default::default() }, |ui| {+ let rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(400.0, 400.0));+ let painter = ui.ctx().layer_painter(egui::LayerId::background()).with_clip_rect(rect);+ let xf = ViewXform { rect, zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 };+ draw_overlay(app, &painter, &xf);+ });+ out.textures_delta.clear();+ out.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::LineSegment { stroke, .. } if (stroke.width - 1.5).abs() < 1e-4))+ };++ // Before resetting, the caret is in the off phase at t = 10.5 s (10500 / 530 = 19, odd phase)+ assert!(!has_caret(&mut app, &ctx, 10.5), "caret should be off at t = 10.5 with phase 0.0");++ ctx.begin_pass(egui::RawInput {+ time: Some(10.5),+ events: vec![egui::Event::Key { key: egui::Key::ArrowRight, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }],+ ..Default::default()+ });+ handle_keys(&mut app, &ctx);+ assert_eq!(app.ui.caret_blink_time, 10.5);++ // draw_overlay paints the solid caret in the new phase+ assert!(has_caret(&mut app, &ctx, 10.5), "caret should be painted right after arrow key");+ }++ #[test]+ fn pointer_up_click_resets_caret_blink_phase() {+ let mut app = app();+ pointer_up(&mut app, [10.0, 50.0], [10.0, 50.0]);+ insert(&mut app, "Hello");+ app.ui.caret_blink_time = 0.0;+ app.last_frame_time = 12.3;+ pointer_up(&mut app, [10.0, 50.0], [10.0, 50.0]);+ assert_eq!(app.ui.caret_blink_time, 12.3);+ }+#[test]fn double_click_from_move_tool_restores_move_tool_on_commit_and_cancel() {let (mut app, _) = variants();