diff --git a/crates/ui-egui/src/a11y.rs b/crates/ui-egui/src/a11y.rs index d09cb5165..934fc57a8 100644 --- a/crates/ui-egui/src/a11y.rs +++ b/crates/ui-egui/src/a11y.rs @@ -535,7 +535,7 @@ pub fn end(app: &mut FigApp, ctx: &Context) { } /// Dialogs, the palette, the presenter and the home screen have no regions. -fn blocked(app: &FigApp) -> bool { +pub(crate) fn blocked(app: &FigApp) -> bool { let u = &app.ui; u.palette.is_some() || u.about diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 451a40d16..200139617 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -167,6 +167,9 @@ impl FigApp { self.ui_frame_stamp = Some(stamp); self.frame_stats.record(&ctx); self.ui_setup(&ctx); + // The root `Ui` copied the style before the setup set this frame's theme: this pass + // draws with the theme it just set. + ui.set_style(ctx.global_style()); } windows::begin(self, &ctx); let (files, prefs) = (self.session.docs.len(), self.session.prefs.clone()); diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index db3efcfdc..bd76d9922 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -253,7 +253,9 @@ pub fn show(app: &mut FigApp, ui: &mut Ui, worker: &mut Worker) { let ppp = ctx.pixels_per_point(); let resp = ui.interact(rect, ui.scope_id().with("canvas"), Sense::click_and_drag()); // The window's document canvas: with nothing focused its keys stay in egui for the shortcuts. - crate::framework::host_canvas(&ctx, resp.id); + // Tab is the canvas's (pick select) only while FigCraft's regions run; over a dialog, the + // palette or the presenter it is egui's own focus move. + crate::framework::host_canvas(&ctx, resp.id, !crate::a11y::blocked(app)); let key_box = app.ui.key_box; crate::a11y::canvas(&mut app.ui.nav, &resp, || key_box.map_or_else(|| describe(&app.session), describe_box)); if let Ok(ds) = app.session.ds_mut() { diff --git a/crates/ui-egui/src/field_tests.rs b/crates/ui-egui/src/field_tests.rs index 4d8b5cf62..9a22f7e89 100644 --- a/crates/ui-egui/src/field_tests.rs +++ b/crates/ui-egui/src/field_tests.rs @@ -1350,43 +1350,37 @@ fn a_deferred_field_save_does_not_retarget_a_closed_moved_or_foreign_owner() { } /// A save prepared for A must not force its cached X editor into B when routing changes the -/// presented document before the fields draw, even though both panels use the same widget ID. +/// presented document before the fields draw, even though both panels use the same widget ID. The +/// control channel is the route that presents B in the save's pass: a click on B's tab cannot, as +/// the runtime ends a pass at a pointer press and the click lands in the next one. #[test] fn a_prepared_field_save_cannot_commit_into_a_same_pass_replacement_document() { - for route in ["control", "tab"] { - for save_as in [false, true] { - for discard in [false, true] { - prepared_save_cannot_enter_a_replacement(route, save_as, discard); - } + for save_as in [false, true] { + for discard in [false, true] { + prepared_save_cannot_enter_a_replacement(save_as, discard); } } } -/// The save shortcut's key events (and, routing by tab, a click on the tab at `tab_at`), with the modifiers held. -fn save_events(route: &str, save_as: bool, tab_at: egui::Pos2) -> (Vec, Modifiers) { +/// The save shortcut's key events, with the modifiers held. +fn save_events(save_as: bool) -> (Vec, Modifiers) { let mut modifiers = if cfg!(target_os = "macos") { Modifiers::COMMAND } else { Modifiers::COMMAND.plus(Modifiers::CTRL) }; if save_as { modifiers = modifiers.plus(Modifiers::SHIFT); } - let mut events = vec![ + let events = vec![ Event::Key { key: Key::S, physical_key: Some(Key::S), pressed: true, repeat: false, modifiers }, Event::Key { key: Key::S, physical_key: Some(Key::S), pressed: false, repeat: false, modifiers }, ]; - if route == "tab" { - events.push(Event::PointerMoved(tab_at)); - for pressed in [true, false] { - events.push(Event::PointerButton { pos: tab_at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }); - } - } (events, modifiers) } -/// One route/save-as/discard combination of the same-pass replacement test: a save prepared for A -/// must not commit A's cached X editor into B when routing presents B before the fields draw, -/// even though both panels use the same widget ID. -fn prepared_save_cannot_enter_a_replacement(route: &str, save_as: bool, discard: bool) { +/// One save-as/discard combination of the same-pass replacement test: a save prepared for A must +/// not commit A's cached X editor into B when routing presents B before the fields draw, even +/// though both panels use the same widget ID. +fn prepared_save_cannot_enter_a_replacement(save_as: bool, discard: bool) { let (mut u, a, _) = setup(); - let dir = std::env::temp_dir().join(format!("figcraft-field-save-owner-{}-{route}-{save_as}-{discard}", std::process::id())); + let dir = std::env::temp_dir().join(format!("figcraft-field-save-owner-{}-{save_as}-{discard}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let (path, other, copy) = (dir.join("a.figcraft"), dir.join("b.figcraft"), dir.join("copy.figcraft")); u.app.session.execute("file.saveAs", &json!({"path": path.to_string_lossy()})).unwrap(); @@ -1399,7 +1393,6 @@ fn prepared_save_cannot_enter_a_replacement(route: &str, save_as: bool, discard: u.app.session.execute("select.set", &json!({"ids": [b]})).unwrap(); let other_index = u.app.session.active; let other_uid = u.app.session.ds().unwrap().uid; - let other_name = u.app.session.doc().unwrap().name.clone(); let other_model = u.app.session.ds().unwrap().doc.clone(); let before_switch_revision = u.app.session.ds().unwrap().revision; let other_history = u.app.session.undo_depth(); @@ -1414,15 +1407,7 @@ fn prepared_save_cannot_enter_a_replacement(route: &str, save_as: bool, discard: assert_eq!(other_revision, before_switch_revision + 1, "the setup switch advances only B's command revision"); let asked = std::rc::Rc::new(std::cell::Cell::new(0)); u.app.services = Box::new(SaveAsPicker { path: Some(copy.clone()), asked: asked.clone() }); - let output = u.frame_output(vec![]); - let nodes = output.platform_output.accesskit_update.unwrap().nodes; - let tab = nodes - .iter() - .map(|(_, node)| node) - .find(|node| node.role() == Role::Tab && node.label() == Some(other_name.as_str())) - .expect("the actual B tab"); - let bounds = tab.bounds().unwrap(); - let tab_at = pos2(((bounds.x0 + bounds.x1) / 2.0) as f32, ((bounds.y0 + bounds.y1) / 2.0) as f32); + u.frame(vec![]); let (at, _) = field(&mut u, "X"); click(&mut u, at); assert_eq!(u.ctx.memory(|m| m.focused()), Some(Id::unique("pos_x").with("te"))); @@ -1431,20 +1416,15 @@ fn prepared_save_cannot_enter_a_replacement(route: &str, save_as: bool, discard: assert_eq!(u.ctx.data(|d| d.get_temp::(Id::unique("pos_x").with("editing"))), Some("77".into())); assert_eq!(geom(&u, a).0, 10.0, "A's raw draft is not committed yet"); assert_eq!(u.app.session.ds().unwrap().uid, owner, "the save begins with A presented"); - let reply = if route == "control" { - let (reply, rx) = std::sync::mpsc::channel(); - u.app.control_queue.push(crate::ControlRequest { - id: json!("same-pass-switch"), - method: "engine.execute".into(), - params: json!({"command": "doc.switch", "params": {"index": other_index}, "window": 0}), - reply, - state: Default::default(), - }); - Some(rx) - } else { - None - }; - let (events, modifiers) = save_events(route, save_as, tab_at); + let (reply, rx) = std::sync::mpsc::channel(); + u.app.control_queue.push(crate::ControlRequest { + id: json!("same-pass-switch"), + method: "engine.execute".into(), + params: json!({"command": "doc.switch", "params": {"index": other_index}, "window": 0}), + reply, + state: Default::default(), + }); + let (events, modifiers) = save_events(save_as); if discard { let mut events = events; events.insert(0, Event::ModifiersChanged(modifiers)); @@ -1452,13 +1432,11 @@ fn prepared_save_cannot_enter_a_replacement(route: &str, save_as: bool, discard: } else { u.frame_with(events, modifiers); } - if let Some(reply) = reply { - let reply = reply.try_recv().expect("real control::drain acknowledged doc.switch in this pass"); - assert!(reply.result.is_ok(), "{:?}", reply.result); - } + let reply = rx.try_recv().expect("real control::drain acknowledged doc.switch in this pass"); + assert!(reply.result.is_ok(), "{:?}", reply.result); let unchanged = |u: &Ui| { assert_eq!(u.app.session.ds().unwrap().uid, other_uid, "the real same-pass route presents B"); - assert_eq!(geom(u, b), (200.0, 40.0, 30.0, 30.0), "A's draft cannot enter B's field: route={route}, save_as={save_as}, discard={discard}"); + assert_eq!(geom(u, b), (200.0, 40.0, 30.0, 30.0), "A's draft cannot enter B's field: save_as={save_as}, discard={discard}"); assert!(std::sync::Arc::ptr_eq(&u.app.session.ds().unwrap().doc, &other_model), "B's model is untouched"); assert_eq!(u.app.session.ds().unwrap().revision, other_revision); assert_eq!(u.app.session.undo_depth(), other_history, "B's undo/redo history is unchanged"); diff --git a/crates/ui-egui/src/framework.rs b/crates/ui-egui/src/framework.rs index 95805c72d..e9dc985ad 100644 --- a/crates/ui-egui/src/framework.rs +++ b/crates/ui-egui/src/framework.rs @@ -139,10 +139,13 @@ impl FigApp { } /// Declares the canvas `id`, drawn in this pass, as its window's document canvas. With nothing focused it is the -/// window's key target: Tab, the edit chords, the clipboard events and the Alt+digit panel keys stay in egui's input -/// for FigCraft's shortcuts, as do the arrows, Enter and the page keys. -pub(crate) fn host_canvas(ctx: &egui::Context, id: egui::Id) { - let keys = KeyUse::CANVAS.with_keep(KeepKeys::Tab).with_edits(EditSet::ALL).with_claims(alt_digit_claims()); +/// window's key target: the edit chords, the clipboard events and the Alt+digit panel keys stay in egui's input for +/// FigCraft's shortcuts, as do the arrows, Enter and the page keys. With `keeps_tab` Tab stays there too, for the +/// canvas and FigCraft's region moves; without it, a Tab from nothing focused is egui's own focus move, which the +/// runtime cancels for a Tab the canvas keeps. +pub(crate) fn host_canvas(ctx: &egui::Context, id: egui::Id, keeps_tab: bool) { + let keys = KeyUse::CANVAS.with_edits(EditSet::ALL).with_claims(alt_digit_claims()); + let keys = if keeps_tab { keys.with_keep(KeepKeys::Tab) } else { keys }; craft_ui::runtime::focus::host_canvas(ctx, id, keys); } diff --git a/crates/ui-egui/src/idle_tests.rs b/crates/ui-egui/src/idle_tests.rs index 6955b0410..c2b12c2d0 100644 --- a/crates/ui-egui/src/idle_tests.rs +++ b/crates/ui-egui/src/idle_tests.rs @@ -217,7 +217,7 @@ fn a_toast_raised_by_a_slow_frame_still_shows_for_3_s() { #[test] fn a_finished_background_render_wakes_the_ui() { - let ctx = crate::framework::headless_context(); + let ctx = egui::Context::default(); let wakes = Arc::new(AtomicUsize::new(0)); let w = wakes.clone(); ctx.set_request_repaint_callback(move |_| { diff --git a/crates/ui-egui/src/palette_tests.rs b/crates/ui-egui/src/palette_tests.rs index eb395e818..9eb927414 100644 --- a/crates/ui-egui/src/palette_tests.rs +++ b/crates/ui-egui/src/palette_tests.rs @@ -181,13 +181,15 @@ fn arrows_move_the_highlight_and_enter_runs_it() { } /// A trackpad tap sends the press and the release in one frame; the press must not close a -/// context menu the release just opened, but a later outside press still does. +/// context menu the release just opened, but a later outside press still does. The runtime ends a +/// pass at its press, so the release, which opens the menu, arrives in the next frame. #[test] fn context_menu_survives_a_same_frame_tap() { let mut u = Ui::new(Session::new()); let p = Pos2::new(1000.0, 600.0); let b = |pressed| Event::PointerButton { pos: p, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() }; u.frame(vec![Event::PointerMoved(p), b(true), b(false)]); + u.frame(vec![]); assert!(u.app.ui.context_menu.is_some(), "the opening tap's press is spared"); u.frame(vec![]); assert!(u.app.ui.context_menu.is_some(), "the menu stays open");