diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 44e38d6e20..941f27be9e 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -702,6 +702,17 @@ fn opener(state: &State, id: Id, noted: Option, focused: Option) -> } } +/// The layer `id` ended its shown life in the running pass after its body ran (a dialog host removed the dialog it +/// showed): its scope plays its exit from now on, so the end hook of this pass moves the focus inside it to its opener, +/// or nowhere, as for a scope that went away. A scope this pass did not show is left as it is. +pub(crate) fn end_scope(ctx: &Context, id: Id) { + with(ctx, |state| { + if let Some(scope) = state.scopes.get_mut(&id) { + scope.closing = true; + } + }); +} + /// The innermost scope open in the running pass: the scope whose body is being shown. #[doc(hidden)] pub fn open_scope(ctx: &Context) -> Option { diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index 420b12633a..9cab5e77bf 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -835,14 +835,20 @@ pub fn take_keys_for_new_dialog(ctx: &Context) { /// A dialog host removed `dialog`: the layers it showed for it (`stack_place`) in the running or the last pass end /// their shown life at once (`memory::end_shown`), as a container that shows nothing does, and forget the open state /// they kept for the session (`memory::open_slot`), so the next open of the dialog is a first pass and starts open: the -/// close that ended the dialog wrote that state closed. +/// close that ended the dialog wrote that state closed. The layers leave this pass's stack, so its end hook records no +/// modal and no top layer for them, and their focus scopes close (`focus::end_scope`): the end hook of this pass moves +/// the focus inside them back to the opener, and the next frame's keys go there. #[doc(hidden)] pub fn end_stack_dialog(ctx: &Context, dialog: Id) { - let shown: Vec = - with_pass(ctx, |pass| pass.current.iter().chain(&pass.last).filter(|entry| entry.dialog == Some(dialog)).map(|entry| entry.id).collect()); + let shown: Vec = with_pass(ctx, |pass| { + let shown = pass.current.iter().chain(&pass.last).filter(|entry| entry.dialog == Some(dialog)).map(|entry| entry.id).collect(); + pass.current.retain(|entry| entry.dialog != Some(dialog)); + shown + }); for id in shown { crate::runtime::memory::end_shown(ctx, id); crate::runtime::memory::forget(ctx, crate::runtime::memory::open_slot(id)); + crate::runtime::focus::end_scope(ctx, id); } } diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index 5059c84c0e..70a22db8d8 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -1358,3 +1358,56 @@ fn an_alert_moves_by_its_title_row() { fixture.drag_at(at, vec2(40.0, 30.0)).idle(4); assert_eq!(dialog.get().min - before.min, vec2(40.0, 30.0), "the alert moved with the drag"); } + +/// A modal dialog with a name field that finishes in the pass after `finish` is set, as a held OK the app accepted does. +struct Renaming { + name: String, + finish: Arc, +} + +impl StackDialog for Renaming { + type Result = (); + + fn id(&self) -> DialogId { + dialog_id("renaming") + } + + fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { + Dialog::new("Rename").id_salt("renaming").show(ui, |ui, _| { + TextField::new("New name", Bind::bound(&mut self.name)).show(ui); + }); + self.finish.load(Ordering::Relaxed).then_some(DialogResult::Ok(())) + } +} + +/// A dialog the host removes in the pass its body ran gives up the keyboard in that pass: the next frame's typing goes +/// to the field the dialog opened from, and that frame sees no modal layer. +#[trial] +fn a_dialog_that_finishes_after_its_pass_ran_hands_the_next_keys_to_its_opener() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let below = Rc::new(RefCell::new(String::new())); + let modal_seen = Rc::new(RefCell::new(Vec::new())); + let (shown, field, seen) = (Rc::clone(&host), Rc::clone(&below), Rc::clone(&modal_seen)); + let mut fixture = Fixture::new(move |ui| { + seen.borrow_mut().push(any_open(ui.ctx(), OpenFilter::Modal)); + TextField::new("Layer name", Bind::bound(&mut *field.borrow_mut())).show(ui); + shown.borrow_mut().run(ui); + }); + fixture.settle(); + let opener = fixture.by_label("Layer name").expect("the window's field shows"); + fixture.click(&opener).settle(); + let finish = Arc::new(AtomicBool::new(false)); + host.borrow_mut().open(Renaming { name: String::new(), finish: Arc::clone(&finish) }); + fixture.settle(); + let in_dialog = fixture.by_label("New name").expect("the dialog's field shows"); + assert_eq!(fixture.tree().focused_key(), in_dialog.node, "the dialog's field has the keyboard"); + finish.store(true, Ordering::Relaxed); + fixture.frame(Vec::new()); + assert!(!host.borrow().is_open::(), "the dialog finished"); + assert_eq!(fixture.passes(), 1, "the frame the dialog finished in ran one pass"); + modal_seen.borrow_mut().clear(); + fixture.frame(vec![egui::Event::Text("x".to_owned())]); + assert_eq!(modal_seen.borrow().first(), Some(&false), "the frame after the close sees no modal layer"); + fixture.idle(2); + assert_eq!(*below.borrow(), "x", "the typing of the frame after the close reached the opener's field"); +}