From 2a56d8a5f77fa12bfad3de699aa3b285cdb45ab5 Mon Sep 17 00:00:00 2001 From: Denis Gomes <6731020+denisg0mes@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:11:42 -0500 Subject: [PATCH] M7: a field left for another layer commits to the layer it was edited for Clicking into a fill's hex field and then clicking a section's row in the layers panel painted the section with the hex the field showed. The layers panel draws before the Design panel, so the row's click had already selected the section when the field let go: the field compared its text with the section's hex, found them different and committed it to the section. A colour typed and left with a press on the canvas went nowhere: the canvas selected and started a move before the field let go, the field committed into that move, and the unmoved release put the document back. The Design panel now shows the layers its field was edited for while the field has the keyboard and for the pass after, so whatever selects another layer first, the commit goes where it was typed. A press on the canvas takes the keyboard from a panel field before the panels draw, so the field commits before the press selects or starts a move. Rejected: keying the field by the layers it shows. The old field would no longer be drawn after the selection changed, so a typed colour would be dropped rather than committed. --- crates/ui-egui/src/a11y.rs | 5 + crates/ui-egui/src/app.rs | 2 + crates/ui-egui/src/canvas.rs | 1 + crates/ui-egui/src/canvas/input.rs | 22 ++++ crates/ui-egui/src/hex_field_target_tests.rs | 126 +++++++++++++++++++ crates/ui-egui/src/lib.rs | 6 + crates/ui-egui/src/panels/right.rs | 24 +++- 7 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 crates/ui-egui/src/hex_field_target_tests.rs diff --git a/crates/ui-egui/src/a11y.rs b/crates/ui-egui/src/a11y.rs index bc5d046e8..c1181a2ce 100644 --- a/crates/ui-egui/src/a11y.rs +++ b/crates/ui-egui/src/a11y.rs @@ -275,6 +275,11 @@ fn shared(ctx: &Context, f: impl FnOnce(&mut Shared) -> R) -> R { ctx.data_mut(|d| f(d.get_temp_mut_or_default::(id))) } +/// The canvas's widget id, once it has been drawn. +pub fn canvas_id(ctx: &Context) -> Option { + shared(ctx, |s| s.canvas) +} + /// Whether a widget other than the canvas has the keyboard (a field, a focused button): the /// canvas shortcuts stay off then. pub fn field_focused(ctx: &Context) -> bool { diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index a893d7ac0..216451194 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -292,6 +292,8 @@ impl FigApp { self.ui.narrow = narrow; // A narrow window shows the right panel when its button asks for it, a board's too. let right_open = (self.right_panel_shown(hidden) && !narrow && !right_minimized) || mobile == Some(MobilePanel::Right); + // A field left for the canvas commits as the panels draw, before the canvas takes the press. + canvas::release_fields_for_press(&ctx); // A highlighted fill, stroke or effect row goes with the panel it's in, before it draws. crate::property_rows::reconcile(self, &ctx, right_open); // A right-to-left UI language mirrors the window: the "left" rail and panel go on the diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 38e270b7a..70c7238cd 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -18,6 +18,7 @@ mod autopan; mod background; mod builder; mod input; +pub(crate) use input::release_fields_for_press; mod outline; mod overlays; mod render; diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index 013c20fa6..fbd1f9e65 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -189,6 +189,28 @@ fn pan_view(app: &mut FigApp, ctx: &egui::Context, pos: Pos2, mods: &egui::Modif } } +/// A press on the canvas takes the keyboard from a panel field before the panels draw, so the +/// field commits to the layers it was edited for. Drawn after the panels, the press would first +/// select another layer, and the field would commit to that one (or into the move the press +/// starts, which an unmoved release puts back). +pub(crate) fn release_fields_for_press(ctx: &egui::Context) { + let Some(canvas) = crate::a11y::canvas_id(ctx).and_then(|id| ctx.read_response(id)) else { return }; + let press = ctx.input(|i| { + i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: true, .. } => Some(*pos), + _ => None, + }) + }); + let on_canvas = press.is_some_and(|p| canvas.rect.contains(p) && ctx.layer_id_at(p) == Some(canvas.layer_id)); + if on_canvas && crate::a11y::field_focused(ctx) { + ctx.memory_mut(|m| { + if let Some(f) = m.focused() { + m.surrender_focus(f); + } + }); + } +} + /// The pointer and keys on the canvas. False when something on it with a cursor of its own has /// the pointer (a comment pin, a ruler guide, a gradient or padding handle): the tool's cursor /// then doesn't show. diff --git a/crates/ui-egui/src/hex_field_target_tests.rs b/crates/ui-egui/src/hex_field_target_tests.rs new file mode 100644 index 000000000..bd97e5157 --- /dev/null +++ b/crates/ui-egui/src/hex_field_target_tests.rs @@ -0,0 +1,126 @@ +//! A hex field in a fill row belongs to the layers the row showed: clicking another layer, on the +//! canvas or in the layers panel, ends the edit, and the colour goes to the layer it was typed +//! for (none when nothing was typed), not to the one clicked. + +use egui::accesskit::Role; +use egui::{Event, Pos2, pos2}; +use figcraft_doc::NodeId; +use figcraft_engine::Session; +use serde_json::json; + +use crate::layers_drag_tests::Ui; + +fn id(v: &serde_json::Value) -> NodeId { + NodeId(v["id"].as_u64().or_else(|| v["ids"][0].as_u64()).unwrap()) +} + +fn fill(u: &Ui, n: NodeId) -> Option { + u.app.session.doc().unwrap().node(n).unwrap().fills.first().and_then(|p| p.solid_color()).map(|c| c.to_css_hex()) +} + +/// The middle of the Design panel's hex field showing `hex`. +fn hex_field(u: &mut Ui, hex: &str) -> Pos2 { + u.frame(vec![]); + let out = u.frame_output(vec![]); + let nodes = out.platform_output.accesskit_update.expect("an AccessKit update").nodes; + let n = nodes + .iter() + .map(|(_, n)| n) + .find(|n| n.role() == Role::TextInput && n.value().is_some_and(|v| v.eq_ignore_ascii_case(hex.trim_start_matches('#')))) + .unwrap_or_else(|| panic!("no hex field showing {hex}")); + let b = n.bounds().expect("bounds"); + pos2(((b.x0 + b.x1) / 2.0) as f32, ((b.y0 + b.y1) / 2.0) as f32) +} + +/// Press at `p` and let go: within one frame (a quick click), or held for a few frames. +fn click(u: &mut Ui, p: Pos2, held: bool) { + let button = |pressed| Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + u.frame(vec![Event::PointerMoved(p)]); + if held { + u.frame(vec![button(true)]); + u.frame(vec![]); + u.frame(vec![button(false)]); + } else { + u.frame(vec![button(true), button(false)]); + } + u.frame(vec![]); +} + +/// A grey rectangle, a section and a second rectangle; the first rectangle is selected. +fn setup() -> (Ui, NodeId, NodeId, NodeId) { + let mut s = Session::new(); + let a = id(&s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 40, "height": 40})).unwrap()); + s.execute("fill.set", &json!({"ids": [a], "color": "#808080"})).unwrap(); + let sec = id(&s.execute("node.create", &json!({"type": "section", "x": 200, "y": 0, "width": 300, "height": 300})).unwrap()); + let b = id(&s.execute("shape.rectangle", &json!({"x": 0, "y": 200, "width": 40, "height": 40})).unwrap()); + s.execute("fill.set", &json!({"ids": [b], "color": "#00FF00"})).unwrap(); + s.execute("select.set", &json!({"ids": [a]})).unwrap(); + let mut u = Ui::new(s); + u.ctx.enable_accesskit(); + u.frame(vec![]); + (u, a, sec, b) +} + +/// Click into the first rectangle's hex field, type `typed` (nothing when empty) and click the +/// section, the second rectangle or empty canvas, quickly or `held`. +fn leave_hex_for(u: &mut Ui, typed: &str, target: &str, held: bool, sec: NodeId, b: NodeId) { + let field = hex_field(u, "808080"); + click(u, field, false); + if !typed.is_empty() { + u.frame(vec![Event::Text(typed.into())]); + } + u.frame(vec![]); + let (selects, x, y) = match target { + "section" | "section's row" => (vec![sec], 350.0, 200.0), + "rectangle" => (vec![b], 20.0, 220.0), + _ => (vec![], 100.0, 100.0), + }; + let at = if target == "section's row" { + layer_row(u, "Section 1") + } else { + let at = u.app.to_screen(x, y); + assert!(u.app.canvas.rect.contains(at), "{target} at {at:?} is on the canvas"); + at + }; + click(u, at, held); + u.frame(vec![]); + let selected: Vec = u.app.session.ds().unwrap().selection.clone(); + assert_eq!(selected, selects, "clicking the {target} selects it"); +} + +/// The middle of the layers panel row named `name`. +fn layer_row(u: &mut Ui, name: &str) -> Pos2 { + u.frame(vec![]); + let out = u.frame_output(vec![]); + let nodes = out.platform_output.accesskit_update.expect("an AccessKit update").nodes; + let n = nodes.iter().map(|(_, n)| n).find(|n| n.role() == Role::TreeItem && n.label() == Some(name)).unwrap_or_else(|| panic!("no row {name:?}")); + let b = n.bounds().expect("bounds"); + pos2(((b.x0 + b.x1) / 2.0) as f32, ((b.y0 + b.y1) / 2.0) as f32) +} + +/// A hex field clicked into and left for another layer doesn't paint that layer with the colour +/// it showed. +#[test] +fn a_hex_field_left_for_another_layer_leaves_that_layer_its_colour() { + let targets = ["section", "section's row", "rectangle"]; + for (target, held) in targets.iter().flat_map(|t| [(*t, false), (*t, true)]) { + let (mut u, a, sec, b) = setup(); + let (sec_before, b_before) = (fill(&u, sec), fill(&u, b)); + leave_hex_for(&mut u, "", target, held, sec, b); + assert_eq!((fill(&u, sec), fill(&u, b)), (sec_before, b_before), "the clicked {target} keeps its fill (held: {held})"); + assert_eq!(fill(&u, a).as_deref(), Some("#808080"), "the rectangle edited keeps its fill ({target}, held: {held})"); + } +} + +/// A hex typed and left for another layer or empty canvas colours the layer it was typed for. +#[test] +fn a_hex_typed_and_left_for_another_layer_colours_the_layer_it_was_typed_for() { + let targets = ["section", "section's row", "rectangle", "empty canvas"]; + for (target, held) in targets.iter().flat_map(|t| [(*t, false), (*t, true)]) { + let (mut u, a, sec, b) = setup(); + let (sec_before, b_before) = (fill(&u, sec), fill(&u, b)); + leave_hex_for(&mut u, "FF0000", target, held, sec, b); + assert_eq!((fill(&u, sec), fill(&u, b)), (sec_before, b_before), "the clicked {target} keeps its fill (held: {held})"); + assert_eq!(fill(&u, a).as_deref(), Some("#FF0000"), "the colour goes to the layer it was typed for ({target}, held: {held})"); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 2a13d7de1..88d62ed19 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -137,6 +137,8 @@ mod guide_snap_tests; #[cfg(test)] mod header_icon_size_tests; #[cfg(test)] +mod hex_field_target_tests; +#[cfg(test)] mod home_recents_tests; #[cfg(test)] mod home_tests; @@ -457,6 +459,9 @@ pub struct UiState { pub right_width: f32, /// The right sidebar as drawn this frame, after fitting the window. pub right_panel_rect: Option, + /// The layers the Design panel showed, and the pass it did, while one of its fields had the + /// keyboard (see `design_selection`). + pub field_selection: Option<(u64, Vec)>, pub right_tab: RightTab, /// The tab of the previous frame, to see the right panel change mode. pub last_right_tab: RightTab, @@ -683,6 +688,7 @@ impl Default for UiState { left_width: PANEL_DEFAULT, right_width: PANEL_DEFAULT, right_panel_rect: None, + field_selection: None, right_tab: RightTab::Design, last_right_tab: RightTab::Design, left_tab: LeftTab::File, diff --git a/crates/ui-egui/src/panels/right.rs b/crates/ui-egui/src/panels/right.rs index 41fc758f4..7484f2c47 100644 --- a/crates/ui-egui/src/panels/right.rs +++ b/crates/ui-egui/src/panels/right.rs @@ -259,14 +259,36 @@ pub fn right_panel(app: &mut FigApp, ui: &mut Ui) { }); } +/// Whether the widget with the keyboard is a field drawn inside `panel`. +fn field_focused_in(ctx: &egui::Context, panel: Rect) -> bool { + a11y::field_focused(ctx) && ctx.memory(|m| m.focused()).and_then(|f| ctx.read_response(f)).is_some_and(|r| panel.intersects(r.rect)) +} + +/// The layers the Design panel shows: the selection, except while one of its fields has the +/// keyboard and for the pass after, when it shows the layers that field was edited for (while +/// they are still there). A field commits when it lets go, and the click that takes the keyboard +/// may select another layer first (a layers panel row draws before this panel): the field's text +/// still goes to the layers it was typed for, not into the ones just selected. +fn design_selection(app: &mut FigApp, ctx: &egui::Context) -> Vec { + let pass = ctx.cumulative_pass_nr(); + let focused = app.ui.right_panel_rect.is_some_and(|panel| field_focused_in(ctx, panel)); + let Ok(ds) = app.session.ds() else { return Vec::new() }; + let held = app.ui.field_selection.as_ref().filter(|(at, ids)| at + 1 >= pass && !ids.is_empty() && ids.iter().all(|&n| ds.doc.node(n).is_some())); + let sel = held.map_or_else(|| ds.selection.clone(), |(_, ids)| ids.clone()); + if focused { + app.ui.field_selection = Some((pass, sel.clone())); + } + sel +} + fn design(app: &mut FigApp, ui: &mut Ui) { + let sel = design_selection(app, ui.ctx()); let Ok(ds) = app.session.ds() else { return }; // The panel shows the document itself, not a live text preview over it. let doc = ds.text_preview.clone().unwrap_or_else(|| ds.doc.clone()); // While a gesture only moves the selection, nothing the sections show changes but where it // is: they show the document it started from, so what they keep stays kept (#1571). let shown = crate::selection_cache::moved(ds).filter(|_| ds.text_preview.is_none()).map_or_else(|| doc.clone(), |(b, _)| b); - let sel = ds.selection.clone(); let vector_edit = ds.vector_edit.clone(); if sel.is_empty() { if app.session.tool.tool == figcraft_engine::ToolId::Scale {