From b69121232d03065ad4001879fbd2d1e67d1b9929 Mon Sep 17 00:00:00 2001 From: B-uchi Date: Fri, 27 Nov 2026 10:33:25 +0100 Subject: [PATCH 1/4] M8: the Screen sizes section keeps a typed rule and its group field per document The section kept its rule draft under the same id as the text field's editing state, which the field removes when it loses focus, returning nothing because the text equals the value it was given: a rule typed and then left by clicking anywhere but Add rule was gone. The draft and the editor now have ids of their own; the draft is updated when the field commits, and Add rule uses what the field holds while it has focus. The group field's id held only the layer id, which repeats across open documents, so an unfinished edit could land on another document's layer. It now includes the document, as the rule draft does. --- crates/ui-egui/src/dev_mode_tests.rs | 29 +++++++++++++++++++++++++++ crates/ui-egui/src/panels/dev_mode.rs | 25 ++++++++++++++++++----- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dev_mode_tests.rs b/crates/ui-egui/src/dev_mode_tests.rs index 0e8fa2726..bfc5c6ceb 100644 --- a/crates/ui-egui/src/dev_mode_tests.rs +++ b/crates/ui-egui/src/dev_mode_tests.rs @@ -257,3 +257,32 @@ fn a_rule_draft_belongs_to_its_document_and_outlives_a_refusal() { u.run("file.new", json!({})); assert_ne!(crate::dev_mode::rule_draft(&u.app, f), field); } + +#[test] +fn a_typed_rule_stays_when_the_field_loses_focus() { + let mut s = Session::new(); + let f = id(&s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 300, "height": 400, "name": "Home"})).unwrap()); + s.execute("select.set", &json!({"ids": [f]})).unwrap(); + let mut u = crate::layers_drag_tests::Ui::new(s); + u.app.ui.right_tab = crate::RightTab::Inspect; + u.ctx.enable_accesskit(); + u.frame(vec![]); + let te = crate::dev_mode::rule_editor(&u.app, f).with("te"); + u.ctx.memory_mut(|m| m.request_focus(te)); + u.frame(vec![]); + u.frame(vec![Event::Text("Cards stack".into())]); + // Click something else in the panel: the field loses focus as it does in use. + let mobile = tree(&mut u).into_iter().find(|n| n.role() == Role::Button && n.label() == Some("Mobile")).expect("the Mobile button"); + click(&mut u, centre(&mobile)); + u.frame(vec![]); + let draft = u.ctx.data(|d| d.get_temp::(crate::dev_mode::rule_draft(&u.app, f))); + assert_eq!(draft.as_deref(), Some("Cards stack")); +} + +#[test] +fn the_group_field_belongs_to_its_document() { + let (mut u, f, _) = setup(); + let here = crate::dev_mode::group_field(&u.app, f); + u.run("file.new", json!({})); + assert_ne!(crate::dev_mode::group_field(&u.app, f), here, "another document's layer with the same id has its own field"); +} diff --git a/crates/ui-egui/src/panels/dev_mode.rs b/crates/ui-egui/src/panels/dev_mode.rs index ea9a27bf4..5a7950fd9 100644 --- a/crates/ui-egui/src/panels/dev_mode.rs +++ b/crates/ui-egui/src/panels/dev_mode.rs @@ -572,19 +572,22 @@ pub fn responsive_section(app: &mut FigApp, ui: &mut Ui, doc: &Document, id: Nod let group = responsive::group_of(n); row(ui, 32.0, |ui| { let width = ui.available_width() - 2.0 * PAD; - if let Some(v) = widgets::text_field(ui, Id::unique(("responsive_group", id)), group, width, tr("Design it's a version of")) { + if let Some(v) = widgets::text_field(ui, group_field(app, id), group, width, tr("Design it's a version of")) { // Emptied: back to the frame's name. let group = if v.trim().is_empty() { json!(null) } else { json!(v) }; app.run("responsive.set", json!({"id": id, "viewport": current, "group": group})); } }); } - let field = rule_draft(app, id); - let draft = ui.data(|d| d.get_temp::(field)).unwrap_or_default(); + let (field, editor) = (rule_draft(app, id), rule_editor(app, id)); + let mut draft = ui.data(|d| d.get_temp::(field)).unwrap_or_default(); widgets::sublabels(ui, tr("Add a rule"), None); - if let Some(v) = widgets::multiline_field(ui, field, &draft, tr("Sidebar becomes a drawer below 768 px"), 3) { - ui.data_mut(|d| d.insert_temp(field, v)); + if let Some(v) = widgets::multiline_field(ui, editor, &draft, tr("Sidebar becomes a drawer below 768 px"), 3) { + ui.data_mut(|d| d.insert_temp(field, v.clone())); + draft = v; } + // While the field has focus, what it holds is the rule. + let draft = ui.data(|d| d.get_temp::(editor)).unwrap_or(draft); row(ui, 32.0, |ui| { if widgets::button(ui, tr("Add rule"), false, !draft.trim().is_empty()).clicked() { add_rule(app, ui.ctx(), id, &draft); @@ -593,6 +596,18 @@ pub fn responsive_section(app: &mut FigApp, ui: &mut Ui, doc: &Document, id: Nod widgets::note(ui, tr("Rules appear under Annotations, in the Responsive category."), t.text_secondary); } +/// The Screen sizes section's field for the design layer `id` is a version of. +pub(crate) fn group_field(app: &FigApp, id: NodeId) -> Id { + // Layer ids repeat across open documents. + Id::unique(("responsive_group", app.session.ds().ok().map(|d| d.uid), id)) +} + +/// The rule field's own editing state for layer `id`. +pub(crate) fn rule_editor(app: &FigApp, id: NodeId) -> Id { + // Apart from the draft: the field drops its editing state when it loses focus. + rule_draft(app, id).with("editor") +} + /// Where the Screen sizes section keeps the rule being typed for layer `id`. pub(crate) fn rule_draft(app: &FigApp, id: NodeId) -> Id { // Layer ids repeat across open documents. From ce5b5f686f4aded993cf313eca5da4b836e305c4 Mon Sep 17 00:00:00 2001 From: B-uchi Date: Fri, 27 Nov 2026 14:05:52 +0100 Subject: [PATCH 2/4] M8: Add rule sends only what the rule field shows Add rule used the rule editor's stored text whenever there was some. When the Screen sizes section went away while the field had focus (the selection changed), that text stayed stored but nothing committed it, so when the section came back the field showed the empty draft while Add rule would send the hidden text. It now reads the editor's text only while the field has focus. --- crates/ui-egui/src/dev_mode_tests.rs | 38 +++++++++++++++++++++++++++ crates/ui-egui/src/panels/dev_mode.rs | 6 +++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/dev_mode_tests.rs b/crates/ui-egui/src/dev_mode_tests.rs index bfc5c6ceb..f02584578 100644 --- a/crates/ui-egui/src/dev_mode_tests.rs +++ b/crates/ui-egui/src/dev_mode_tests.rs @@ -286,3 +286,41 @@ fn the_group_field_belongs_to_its_document() { u.run("file.new", json!({})); assert_ne!(crate::dev_mode::group_field(&u.app, f), here, "another document's layer with the same id has its own field"); } + +/// `n` frames with no input. +fn idle(u: &mut crate::layers_drag_tests::Ui, n: usize) { + for _ in 0..n { + u.frame(vec![]); + } +} + +/// A frame named `name`, marked desktop and selected, with the Inspect tab drawn. +fn marked_frame(u: &mut crate::layers_drag_tests::Ui, name: &str) -> NodeId { + let f = id(&u.app.run("frame.create", json!({"x": 0, "y": 0, "width": 300, "height": 400, "name": name})).unwrap()); + u.app.run("responsive.set", json!({"ids": [f], "viewport": "desktop"})); + u.app.run("select.set", json!({"ids": [f]})); + idle(u, 3); + f +} + +#[test] +fn a_rule_left_typed_when_the_section_hid_isnt_added_unseen() { + let mut u = crate::layers_drag_tests::Ui::new(Session::new()); + u.app.ui.right_tab = crate::RightTab::Inspect; + u.ctx.enable_accesskit(); + let f = marked_frame(&mut u, "Home"); + let te = crate::dev_mode::rule_editor(&u.app, f).with("te"); + u.ctx.memory_mut(|m| m.request_focus(te)); + u.frame(vec![]); + u.frame(vec![Event::Text("Stale".into())]); + // The section goes while the field has focus, then comes back. + u.app.run("select.set", json!({"ids": []})); + idle(&mut u, 3); + u.app.run("select.set", json!({"ids": [f]})); + idle(&mut u, 3); + let add = tree(&mut u).into_iter().find(|n| n.role() == Role::Button && n.label() == Some("Add rule")).expect("Add rule"); + click(&mut u, centre(&add)); + idle(&mut u, 2); + let rules = u.app.session.doc().unwrap().node(f).unwrap().annotations.len(); + assert_eq!(rules, 0, "nothing the field doesn't show is added"); +} diff --git a/crates/ui-egui/src/panels/dev_mode.rs b/crates/ui-egui/src/panels/dev_mode.rs index 5a7950fd9..f35c0d299 100644 --- a/crates/ui-egui/src/panels/dev_mode.rs +++ b/crates/ui-egui/src/panels/dev_mode.rs @@ -586,8 +586,10 @@ pub fn responsive_section(app: &mut FigApp, ui: &mut Ui, doc: &Document, id: Nod ui.data_mut(|d| d.insert_temp(field, v.clone())); draft = v; } - // While the field has focus, what it holds is the rule. - let draft = ui.data(|d| d.get_temp::(editor)).unwrap_or(draft); + // While the field has focus, what it holds is the rule. Only then: text it kept when the + // section went away mid-edit isn't shown, so it mustn't be what Add rule sends. + let focused = ui.memory(|m| m.has_focus(editor.with("te"))); + let draft = if focused { ui.data(|d| d.get_temp::(editor)).unwrap_or(draft) } else { draft }; row(ui, 32.0, |ui| { if widgets::button(ui, tr("Add rule"), false, !draft.trim().is_empty()).clicked() { add_rule(app, ui.ctx(), id, &draft); From 44f88c54f91604d85e82bb334b52924ccdb62d60 Mon Sep 17 00:00:00 2001 From: B-uchi Date: Fri, 27 Nov 2026 14:06:04 +0100 Subject: [PATCH 3/4] M8: the group field's per-document test checks what lands in the document `the_group_field_belongs_to_its_document` compared the field ids, which only shows the wiring. `an_unfinished_group_edit_stays_with_its_document` leaves an edit unfinished in one document, opens another with a layer of the same id, presses Enter there and checks the edit didn't land on it; with the old id it does. --- crates/ui-egui/src/dev_mode_tests.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/dev_mode_tests.rs b/crates/ui-egui/src/dev_mode_tests.rs index f02584578..de397ca2a 100644 --- a/crates/ui-egui/src/dev_mode_tests.rs +++ b/crates/ui-egui/src/dev_mode_tests.rs @@ -279,14 +279,6 @@ fn a_typed_rule_stays_when_the_field_loses_focus() { assert_eq!(draft.as_deref(), Some("Cards stack")); } -#[test] -fn the_group_field_belongs_to_its_document() { - let (mut u, f, _) = setup(); - let here = crate::dev_mode::group_field(&u.app, f); - u.run("file.new", json!({})); - assert_ne!(crate::dev_mode::group_field(&u.app, f), here, "another document's layer with the same id has its own field"); -} - /// `n` frames with no input. fn idle(u: &mut crate::layers_drag_tests::Ui, n: usize) { for _ in 0..n { @@ -303,6 +295,26 @@ fn marked_frame(u: &mut crate::layers_drag_tests::Ui, name: &str) -> NodeId { f } +#[test] +fn an_unfinished_group_edit_stays_with_its_document() { + let mut u = crate::layers_drag_tests::Ui::new(Session::new()); + u.app.ui.right_tab = crate::RightTab::Inspect; + u.ctx.enable_accesskit(); + let a = marked_frame(&mut u, "Home"); + let field = tree(&mut u).into_iter().find(|n| n.label() == Some("Design it's a version of")).expect("the group field"); + click(&mut u, centre(&field)); + u.frame(vec![Event::Text("Alpha".into())]); + // Left unfinished: another document opens, with a layer of the same id. + u.app.run("file.new", json!({})); + let b = marked_frame(&mut u, "Pricing"); + assert_eq!(a, b, "the same layer id in both documents"); + let enter = |pressed| Event::Key { key: Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Default::default() }; + u.frame(vec![enter(true), enter(false)]); + idle(&mut u, 2); + let group = u.app.session.doc().unwrap().node(b).unwrap().plugin_data.get("figcraft.viewportGroup").cloned(); + assert_ne!(group.as_deref(), Some("Alpha"), "the other document's edit didn't land here"); +} + #[test] fn a_rule_left_typed_when_the_section_hid_isnt_added_unseen() { let mut u = crate::layers_drag_tests::Ui::new(Session::new()); From efb8d7eed2158861df8fd97dcfe51f7e2d8df33f Mon Sep 17 00:00:00 2001 From: B-uchi Date: Fri, 27 Nov 2026 17:48:27 +0100 Subject: [PATCH 4/4] M8: an added rule clears the field even when it keeps focus Add rule cleared the rule's draft but not the field's own editing buffer. An assistive Click activates the button without moving focus, so the focused field reloaded the rule it had just sent and Add rule could send it again. Both are cleared on success; a refused rule keeps both, so nothing typed is lost. Text the field kept when the section went away mid-edit is dropped while the field has no focus, so editing again starts from what the field shows instead of bringing that text back. --- crates/ui-egui/src/dev_mode_tests.rs | 42 +++++++++++++++++++++++++++ crates/ui-egui/src/panels/dev_mode.rs | 20 +++++++++---- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dev_mode_tests.rs b/crates/ui-egui/src/dev_mode_tests.rs index de397ca2a..b8bdad819 100644 --- a/crates/ui-egui/src/dev_mode_tests.rs +++ b/crates/ui-egui/src/dev_mode_tests.rs @@ -279,6 +279,13 @@ fn a_typed_rule_stays_when_the_field_loses_focus() { assert_eq!(draft.as_deref(), Some("Cards stack")); } +/// The text the field whose editor is `te` shows, as assistive technology reads it. +fn shown(u: &mut crate::layers_drag_tests::Ui, te: egui::Id) -> String { + let nodes = u.frame_output(vec![]).platform_output.accesskit_update.unwrap().nodes; + let field = nodes.into_iter().find(|(at, _)| *at == te.accesskit_id()).expect("the field").1; + field.value().unwrap_or_default().to_string() +} + /// `n` frames with no input. fn idle(u: &mut crate::layers_drag_tests::Ui, n: usize) { for _ in 0..n { @@ -335,4 +342,39 @@ fn a_rule_left_typed_when_the_section_hid_isnt_added_unseen() { idle(&mut u, 2); let rules = u.app.session.doc().unwrap().node(f).unwrap().annotations.len(); assert_eq!(rules, 0, "nothing the field doesn't show is added"); + // Nor does it come back when the field is edited again. + u.ctx.memory_mut(|m| m.request_focus(te)); + idle(&mut u, 2); + assert_eq!(shown(&mut u, te), "", "the field starts from what it showed"); +} + +#[test] +fn a_rule_added_while_its_field_keeps_focus_is_added_once() { + let mut u = crate::layers_drag_tests::Ui::new(Session::new()); + u.app.ui.right_tab = crate::RightTab::Inspect; + u.ctx.enable_accesskit(); + let f = marked_frame(&mut u, "Home"); + let te = crate::dev_mode::rule_editor(&u.app, f).with("te"); + u.ctx.memory_mut(|m| m.request_focus(te)); + u.frame(vec![]); + u.frame(vec![Event::Text("Cards stack".into())]); + // An assistive Click activates the button without taking focus from the field. + let nodes = u.frame_output(vec![]).platform_output.accesskit_update.unwrap().nodes; + let add = nodes.iter().find(|(_, n)| n.role() == Role::Button && n.label() == Some("Add rule")).expect("Add rule").0; + let activate = || { + Event::AccessKitActionRequest(egui::accesskit::ActionRequest { + action: egui::accesskit::Action::Click, + target_tree: egui::accesskit::TreeId::ROOT, + target_node: add, + data: None, + }) + }; + u.frame(vec![activate()]); + idle(&mut u, 2); + assert!(u.ctx.memory(|m| m.has_focus(te)), "the field kept focus"); + assert_eq!(u.app.session.doc().unwrap().node(f).unwrap().annotations.len(), 1); + assert_eq!(shown(&mut u, te), "", "the field clears once the rule is added"); + u.frame(vec![activate()]); + idle(&mut u, 2); + assert_eq!(u.app.session.doc().unwrap().node(f).unwrap().annotations.len(), 1, "a second activation adds nothing"); } diff --git a/crates/ui-egui/src/panels/dev_mode.rs b/crates/ui-egui/src/panels/dev_mode.rs index f35c0d299..dbb6921ce 100644 --- a/crates/ui-egui/src/panels/dev_mode.rs +++ b/crates/ui-egui/src/panels/dev_mode.rs @@ -586,10 +586,16 @@ pub fn responsive_section(app: &mut FigApp, ui: &mut Ui, doc: &Document, id: Nod ui.data_mut(|d| d.insert_temp(field, v.clone())); draft = v; } - // While the field has focus, what it holds is the rule. Only then: text it kept when the - // section went away mid-edit isn't shown, so it mustn't be what Add rule sends. + // While the field has focus, what it holds is the rule. Without focus, text it kept when the + // section went away mid-edit was never shown: it isn't sent, and it's dropped so the next + // edit starts from the draft the field shows. let focused = ui.memory(|m| m.has_focus(editor.with("te"))); - let draft = if focused { ui.data(|d| d.get_temp::(editor)).unwrap_or(draft) } else { draft }; + let draft = if focused { + ui.data(|d| d.get_temp::(editor)).unwrap_or(draft) + } else { + ui.data_mut(|d| d.remove_temp::(editor)); + draft + }; row(ui, 32.0, |ui| { if widgets::button(ui, tr("Add rule"), false, !draft.trim().is_empty()).clicked() { add_rule(app, ui.ctx(), id, &draft); @@ -618,9 +624,13 @@ pub(crate) fn rule_draft(app: &FigApp, id: NodeId) -> Id { /// Add `text` as a rule on `id`. The draft stays when the rule is refused, so nothing typed is lost. pub(crate) fn add_rule(app: &mut FigApp, ctx: &egui::Context, id: NodeId, text: &str) { - let field = rule_draft(app, id); + let (field, editor) = (rule_draft(app, id), rule_editor(app, id)); if app.run("responsive.rule", json!({"id": id, "text": text})).is_some() { - ctx.data_mut(|d| d.remove_temp::(field)); + // Both: a field that kept focus (an assistive Click doesn't move it) would bring the rule back. + ctx.data_mut(|d| { + d.remove_temp::(field); + d.remove_temp::(editor); + }); } }