PhotoCraft: An open-source, photo editor written by scratch in pure Rust
feat(ui): bidirectional hover highlighting between Layers panel and canvas (#4718) (#4719)
safvanedge6af9e1d97789
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 7781085cd..ce97c240d 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -680,6 +680,10 @@ pub struct PhotocraftApp {pub(crate) clip_read_for_paste: bool,/// Pointer position over the canvas (document px), for the Info panel and status bar.pub(crate) hover_doc: Option<[f64; 2]>,+ /// Layer hovered in the Layers panel, outlined on the canvas.+ pub(crate) hover_layer: Option<photocraft_doc::LayerId>,+ /// Layer hovered on the canvas, highlighted in the Layers panel.+ pub(crate) canvas_hover_layer: Option<photocraft_doc::LayerId>,pub(crate) clone_preview: Option<crate::canvas::ClonePreviewCache>,/// Info panel sample cache: ((x, y, revision, Sample Size), composite RGBA).info_sample: Option<(InfoSampleKey, [f32; 4])>,@@ -822,6 +826,8 @@ impl PhotocraftApp {crop: Default::default(),pcrop: Default::default(),hover_doc: None,+ hover_layer: None,+ canvas_hover_layer: None,clone_preview: None,info_sample: None,os_clip_sig: None,@@ -1746,6 +1752,7 @@ impl eframe::App for PhotocraftApp {fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {let ctx = ui.ctx().clone();+ self.hover_layer = None;i18n::set_current(i18n::Lang::from_pref(&self.session.prefs().interface.language));// Fonts registered via set_fonts only take effect next frame; named families would panic now.if !self.fonts_ready {diff --git a/crates/ui-egui/src/move_hover.rs b/crates/ui-egui/src/move_hover.rsindex 01e6a40f3..0db1a5c80 100644--- a/crates/ui-egui/src/move_hover.rs+++ b/crates/ui-egui/src/move_hover.rs@@ -19,6 +19,7 @@ pub(crate) struct Hover {revision: u64,at: [i32; 2],group: bool,+ target_layer: Option<LayerId>,bounds: Option<Rect>,}@@ -36,18 +37,26 @@ pub(crate) fn target(app: &mut PhotocraftApp, at: Option<[f64; 2]>, command: boo|| app.ui.tool_options.move_auto_select == command|| crate::move_ui::moves_selected_pixels_with(app, Tool::Move){+ app.canvas_hover_layer = None;return None;}- let at = at.filter(|p| p[0].is_finite() && p[1].is_finite())?;+ let Some(at) = at.filter(|p| p[0].is_finite() && p[1].is_finite()) else {+ app.canvas_hover_layer = None;+ return None;+ };let at = [at[0].floor() as i32, at[1].floor() as i32];let group = app.ui.tool_options.move_target == "group";let st = app.session.active()?;let (doc, revision) = (st.doc.id, st.revision);if let Some(h) = app.move_hover.as_ref().filter(|h| h.doc == doc && h.revision == revision && h.at == at && h.group == group) {+ app.canvas_hover_layer = h.target_layer;return h.bounds;}- let bounds = photocraft_engine::pick_cmds::auto_select_target(&st.doc, at[0], at[1], group).and_then(|id| outline(&st.doc, id));- app.move_hover = Some(Hover { doc, revision, at, group, bounds });+ let target_layer = photocraft_engine::pick_cmds::auto_select_target(&st.doc, at[0], at[1], group);+ let bounds = target_layer.and_then(|id| outline(&st.doc, id));+ let target_layer = if bounds.is_some() { target_layer } else { None };+ app.move_hover = Some(Hover { doc, revision, at, group, target_layer, bounds });+ app.canvas_hover_layer = target_layer;bounds}@@ -79,15 +88,39 @@ fn bounds(doc: &Document, l: &Layer) -> Rect {}}+/// The document-space bounds to outline: either a layer hovered in the Layers panel, or what a+/// click at `at` would pick with the Move tool.+pub(crate) fn active_outline(app: &mut PhotocraftApp, at: Option<[f64; 2]>, command: bool, pressed: bool) -> Option<Rect> {+ if !app.session.prefs().tools.show_highlight_on_rollover {+ app.canvas_hover_layer = None;+ return None;+ }+ // A layer hovered in the Layers panel takes precedence over canvas rollover.+ if let Some(id) = app.hover_layer+ && app.drag.is_none()+ && app.guide_drag.is_none()+ && app.ui.transform.is_none()+ && let Some(st) = app.session.active()+ {+ return outline(&st.doc, id);+ }+ target(app, at, command, pressed)+}+/// Draws the highlight on the active document's canvas, in the theme's accent colour.pub(crate) fn draw(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, idx: usize, hover: Option<Pos2>) {if app.session.active_index() != Some(idx) {return;}+ let prev_canvas_hover = app.canvas_hover_layer;let (mods, pressed) = painter.ctx().input(|i| (i.modifiers, i.pointer.any_down()));let command = crate::workspace_ui::sticky_mods(app, mods).command;let at = hover.map(|p| xf.to_doc(p));- let Some(b) = target(app, at, command, pressed) else { return };+ let outline_rect = active_outline(app, at, command, pressed);+ if app.canvas_hover_layer != prev_canvas_hover {+ painter.ctx().request_repaint();+ }+ let Some(b) = outline_rect else { return };let accent = crate::theme::Tokens::get(painter.ctx()).accent;painter.rect_stroke(xf.doc_rect(b), 0.0, Stroke::new(1.5, accent), egui::StrokeKind::Outside);}@@ -125,4 +158,57 @@ mod tests {app.session.edit_prefs(|p| p.tools.show_highlight_on_rollover = false);assert_eq!(target(&mut app, Some([10.5, 12.0]), true, false), None);}++ #[test]+ fn layer_panel_hover_outlines_layer_on_canvas() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap();+ app.session.execute("layer.new.layer", json!({})).unwrap();+ app.session+ .edit("paint", |doc, a| {+ doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(8, 8, 24, 24), &[1.0, 0.0, 0.0, 1.0]);+ Ok(())+ })+ .unwrap();+ let target_layer = app.session.active().unwrap().active_layer.unwrap();+ let a = Rect::new(8, 8, 24, 24);++ // Even with a different tool (e.g. Brush) and no canvas hover, hovering the layer in the panel outlines it:+ app.ui.tool = Tool::Brush;+ assert_eq!(active_outline(&mut app, None, false, false), None);+ app.hover_layer = Some(target_layer);+ assert_eq!(active_outline(&mut app, None, false, false), Some(a));++ // When a drag or transform is in progress, no outline:+ app.guide_drag = Some(crate::rulers::GuideDrag { vertical: false, index: None, pos: 10.0 });+ assert_eq!(active_outline(&mut app, None, false, false), None);+ app.guide_drag = None;++ // When rollover preference is disabled, no outline:+ app.session.edit_prefs(|p| p.tools.show_highlight_on_rollover = false);+ assert_eq!(active_outline(&mut app, None, false, false), None);+ }++ #[test]+ fn canvas_hover_sets_canvas_hover_layer() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap();+ app.session.execute("layer.new.layer", json!({})).unwrap();+ app.session+ .edit("paint", |doc, a| {+ doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(8, 8, 24, 24), &[1.0, 0.0, 0.0, 1.0]);+ Ok(())+ })+ .unwrap();+ let target_layer = app.session.active().unwrap().active_layer.unwrap();+ app.ui.tool = Tool::Move;++ // Hovering over painted layer sets canvas_hover_layer:+ assert!(target(&mut app, Some([10.5, 12.0]), false, false).is_some());+ assert_eq!(app.canvas_hover_layer, Some(target_layer));++ // Hovering empty area clears canvas_hover_layer:+ assert!(target(&mut app, Some([40.0, 40.0]), false, false).is_none());+ assert_eq!(app.canvas_hover_layer, None);+ }}diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 5dafa2b58..702008363 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -2688,17 +2688,23 @@ fn layer_row(return rect;}let painter = ui.painter_at(rect.expand(1.0));+ let is_hovered = resp.hovered() || app.canvas_hover_layer == Some(l.id);+ if resp.hovered() {+ app.hover_layer = Some(l.id);+ }if t.pro {if selected {painter.rect_filled(rect, 0.0, t.row_selected);- } else if resp.hovered() {+ } else if is_hovered {painter.rect_filled(rect, 0.0, t.hover.gamma_multiply(0.5));+ painter.rect_filled(rect, 0.0, t.accent.gamma_multiply(0.12));}} else if selected {painter.rect_filled(rect, t.radius, t.hover);painter.rect_stroke(rect, t.radius, Stroke::new(1.0, t.field_border), StrokeKind::Inside);- } else if resp.hovered() {+ } else if is_hovered {painter.rect_filled(rect, t.radius, t.hover.gamma_multiply(0.5));+ painter.rect_filled(rect, t.radius, t.accent.gamma_multiply(0.12));}let eye_cell = Rect::from_min_max(rect.left_top(), pos2((rect.left() + 30.0).min(rect.right()), rect.bottom()));if let Some((_, background)) = t.layer_label_colors(l.label) {