diff --git a/crates/ui-egui/src/dev_mode_tests.rs b/crates/ui-egui/src/dev_mode_tests.rs index 0e8fa2726..b8bdad819 100644 --- a/crates/ui-egui/src/dev_mode_tests.rs +++ b/crates/ui-egui/src/dev_mode_tests.rs @@ -257,3 +257,124 @@ 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")); +} + +/// 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 { + 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 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()); + 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"); + // 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 ea9a27bf4..dbb6921ce 100644 --- a/crates/ui-egui/src/panels/dev_mode.rs +++ b/crates/ui-egui/src/panels/dev_mode.rs @@ -572,19 +572,30 @@ 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. 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 { + 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); @@ -593,6 +604,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. @@ -601,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); + }); } }