From d67796f4697ac366dcb50c4707b63451c6d96e25 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 09:20:09 +0000 Subject: [PATCH] focus: trials for the focus after a removed stop and after a closed canvas text editor Both fail on this branch: a stop that inherits the focus from a removed one is revealed, and a closed editor on a canvas with rulers hands the focus to the rulers' corner instead of the canvas. --- crates/text/tests/it/editor.rs | 48 ++++++++++++++++++++ crates/ui-contract/tests/it/runtime/focus.rs | 48 ++++++++++++++++++++ crates/ui-contract/tests/it/runtime/mod.rs | 1 + 3 files changed, 97 insertions(+) create mode 100644 crates/ui-contract/tests/it/runtime/focus.rs diff --git a/crates/text/tests/it/editor.rs b/crates/text/tests/it/editor.rs index f23eb21f1..7abc5e213 100644 --- a/crates/text/tests/it/editor.rs +++ b/crates/text/tests/it/editor.rs @@ -346,3 +346,51 @@ fn formatting_keys_follow_the_flow_and_fall_back_to_caret_moves() { assert!(!plain.host.borrow().styles.is_empty(), "the host was asked for the leading step"); assert_eq!(plain.selection.borrow().caret, before.saturating_sub(LINE), "the step the host did not take moved the caret a line up"); } + +/// #15555: a session the app ends (Cmd+Enter, then the editor is not shown) on a canvas with rulers hands the focus back +/// to the canvas it came from, not to the stop before the editor (the rulers' corner, whose arrows move the zero point), +/// and the view stays where it was. Without rulers the canvas is that stop anyway. +#[trial] +fn an_ended_session_returns_the_focus_to_the_canvas_and_the_view_stays() { + const EDITOR: craft_behavior::DocKey = craft_behavior::DocKey::new("trial.type-editor"); + use craft_ui::canvas::Rulers; + use craft_ui::contract::Props as _; + use craft_ui::runtime::focus::{FocusTarget, request}; + use craft_ui::testing::access::{focused_key, name}; + + for rulers in [true, false] { + let editing = Rc::new(Cell::new(false)); + let view = Rc::new(Cell::new(None::)); + let (shown_editing, shown_view) = (Rc::clone(&editing), Rc::clone(&view)); + let mut host = Host { text: "abc".to_owned(), ..Host::default() }; + let mut selection = TextSelection::caret_at(3); + let mut zero = Point::ZERO; + let mut fixture = Fixture::new(move |ui| { + let room = ui.available_rect_before_wrap(); + let given = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(100.0, 100.0), 1.0)); + let strips = rulers.then(|| Rulers::new("Rulers", Bind::bound(&mut zero))); + let canvas = Canvas::new("Document").view(given).with_rulers(strips).show(ui, |ui, body| { + if shown_editing.get() { + let bounds = Rect::from_min_size(body.rect.min + vec2(40.0, 40.0), vec2(200.0, 20.0)); + drop(TextEditor::new("Text", &mut host, Bind::bound(&mut selection)).doc_key(EDITOR).show_at(ui, bounds)); + } + }); + shown_view.set(Some(canvas.output.view.unwrap_or(given))); + }); + fixture.room(vec2(600.0, 400.0)).settle(); + let focused = |fixture: &Fixture| name(fixture.tree(), focused_key(fixture.tree())).map(str::to_owned); + // A press on the canvas focuses it, and the session that press starts asks for the editor's focus. + fixture.click_at(pos2(400.0, 300.0)).settle(); + assert_eq!(focused(&fixture).as_deref(), Some("Document"), "rulers {rulers}: the press focused the canvas"); + editing.set(true); + fixture.settle(); + request(fixture.ctx(), FocusTarget::Doc(EDITOR)); + fixture.settle(); + assert_eq!(focused(&fixture).as_deref(), Some("Text"), "rulers {rulers}: the editor took the focus"); + let before = view.get(); + editing.set(false); + fixture.settle(); + assert_eq!(focused(&fixture).as_deref(), Some("Document"), "rulers {rulers}: the focus went back to the canvas"); + assert_eq!(view.get(), before, "rulers {rulers}: the view stays"); + } +} diff --git a/crates/ui-contract/tests/it/runtime/focus.rs b/crates/ui-contract/tests/it/runtime/focus.rs new file mode 100644 index 000000000..760df0268 --- /dev/null +++ b/crates/ui-contract/tests/it/runtime/focus.rs @@ -0,0 +1,48 @@ +//! Focus after the focused stop goes away. + +use craft_ui::contract::{ContainerProps, Props as _, Scroll, Show as _, ShowContainer as _}; +use craft_ui::runtime::focus::{FocusTarget, request}; +use craft_ui::testing::layout::{Block, in_region}; +use craft_ui::testing::{Frames, trial}; +use craft_ui_test_kit::body::Slab; +use craft_ui_test_kit::contract::{Probe, Seen}; +use egui::{Pos2, Rect, Ui, Vec2}; +use std::cell::{Cell, RefCell}; + +const ORIGIN: Pos2 = Pos2::new(10.0, 10.0); +const VIEW: Vec2 = Vec2::new(200.0, 100.0); + +/// The focused stop goes away and hands the focus to the next stop in its scope, far below a scrolling body's view: +/// the stop takes the focus and the body stays where it is. A neighbour that inherits the focus was not shown by +/// anyone, so nothing reveals it (#15555: the rulers' corner panned the canvas when a closed text editor handed it the +/// focus). +#[trial] +fn a_stop_that_inherits_the_focus_from_a_removed_one_is_not_revealed() { + let (first, next) = (RefCell::new(Seen::default()), RefCell::new(Seen::default())); + let shown = Cell::new(true); + let top = Cell::new(0.0f32); + let mut show = |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| { + Slab::new(ContainerProps::new().scroll(Scroll::vertical())).id_salt("panel").show(ui, |ui, _| { + top.set(ui.next_widget_position().y); + if shown.get() { + Probe::new(&first).label("First").id_salt("first").show(ui); + } + Block::new(Vec2::new(VIEW.x, VIEW.y * 4.0)).show(ui); + Probe::new(&next).label("Next").id_salt("next").show(ui); + }); + }); + }; + let mut frames = Frames::new(); + frames.idle(3, &mut show); + let first_id = first.borrow().id.expect("the first stop ran"); + request(&frames.ctx, FocusTarget::Id(first_id)); + frames.idle(3, &mut show); + assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), Some(first_id), "the first stop took the focus"); + let start = top.get(); + shown.set(false); + frames.idle(6, &mut show); + let next_id = next.borrow().id.expect("the next stop ran"); + assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), Some(next_id), "the next stop inherits the focus"); + assert!((top.get() - start).abs() < 0.5, "the body keeps its offset: content top {} from {start}", top.get()); +} diff --git a/crates/ui-contract/tests/it/runtime/mod.rs b/crates/ui-contract/tests/it/runtime/mod.rs index 2567420c8..a662980d9 100644 --- a/crates/ui-contract/tests/it/runtime/mod.rs +++ b/crates/ui-contract/tests/it/runtime/mod.rs @@ -7,6 +7,7 @@ pub(crate) mod cursor; pub(crate) mod derived; pub(crate) mod elapsed; pub(crate) mod env; +pub(crate) mod focus; pub(crate) mod frame; pub(crate) mod latch; pub(crate) mod memory;