diff --git a/crates/ui-contract/src/contract/body.rs b/crates/ui-contract/src/contract/body.rs index d984f7b64..ac6e40a93 100644 --- a/crates/ui-contract/src/contract/body.rs +++ b/crates/ui-contract/src/contract/body.rs @@ -25,7 +25,7 @@ mod size; pub(crate) use size::EnvFacts; mod window; -pub(crate) use open::unshown; +pub(crate) use open::{forget_open, unshown}; pub(crate) use placement::shown_placement; #[doc(hidden)] pub(crate) use regions::HeaderSlots; diff --git a/crates/ui-contract/src/contract/body/open.rs b/crates/ui-contract/src/contract/body/open.rs index 03b07eed3..a383855ad 100644 --- a/crates/ui-contract/src/contract/body/open.rs +++ b/crates/ui-contract/src/contract/body/open.rs @@ -19,6 +19,13 @@ fn open_slot<'s>(stage: &'s mut MemoryStage, id: Id, save: Option<&SaveKey>) -> } } +/// Forgets the open state the container `id` keeps for the session, so its next show starts at its initial state. A +/// dialog host calls it for each layer of a dialog it removed (`overlay::end_stack_dialog`): a dialog that closed +/// itself and opens again under the same id opens. A binding or a save key keeps the state elsewhere and is untouched. +pub(crate) fn forget_open(ctx: &Context, id: Id) { + crate::runtime::memory::forget(ctx, sub(id, "open")); +} + /// Whether the container is open this pass: its binding, else its opener's state, else its slot. pub(super) fn open_now(comp: &mut C, stage: &mut MemoryStage, id: Id, props: &ContainerProps) -> bool { // A popup's layer (`OverlayUi::show_container`) is open while the popup is: the popup's slot is the one owner. diff --git a/crates/ui-contract/src/runtime/memory.rs b/crates/ui-contract/src/runtime/memory.rs index 3b2ddfd5f..763870ec7 100644 --- a/crates/ui-contract/src/runtime/memory.rs +++ b/crates/ui-contract/src/runtime/memory.rs @@ -283,7 +283,6 @@ fn take_shown(ctx: &Context, reg: &mut Registry, ended: impl Fn(Id) -> bool) -> static QUEUED: AtomicUsize = AtomicUsize::new(0); /// Removes the session slots under `id` (all types). The only way a session slot goes. -#[cfg_attr(not(feature = "testing"), expect(dead_code, reason = "used by the pipeline (F3)"))] pub(crate) fn forget(ctx: &Context, id: Id) { forget_tier(ctx, id, SESSION); } diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index ec0c1c0db..052eb29b6 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -641,14 +641,16 @@ 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, so the next open of the -/// dialog is a first pass. +/// 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 (`body::forget_open`), so the next open of the dialog is a first pass and starts open: the +/// close that ended the dialog wrote that state closed. #[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()); for id in shown { crate::runtime::memory::end_shown(ctx, id); + crate::contract::body::forget_open(ctx, id); } } diff --git a/crates/ui-panels/src/dialog/host.rs b/crates/ui-panels/src/dialog/host.rs index 541637bd0..63f24e63b 100644 --- a/crates/ui-panels/src/dialog/host.rs +++ b/crates/ui-panels/src/dialog/host.rs @@ -346,7 +346,8 @@ impl DialogHost { } /// Opens `dialog` on top. Its id goes on the stack now; the dialog shows from the next - /// [`run`](DialogHost::run). Returns the id. + /// [`run`](DialogHost::run), open: a dialog of the same id that closed itself before starts open again, since the + /// host forgets a removed dialog's open state (`overlay::end_stack_dialog`). Returns the id. pub fn open(&mut self, dialog: D) -> DialogId { self.open_with_parent(Box::new(dialog), None) } diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index ad32a5fba..d0186d59f 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -3,13 +3,14 @@ use craft_behavior::{Metrics, PartKey, Sense}; use craft_host::target::{Os, WindowChrome}; use craft_quit::{CloseAnswer, CloseScope}; -use craft_ui::contract::{Common, Component, Cx, MeasureCx, PartsCx, Props as _, Role, Show as _}; +use craft_ui::contract::{Common, Component, Cx, MeasureCx, PartsCx, Props as _, Role, Show as _, ShowContainer as _}; use craft_ui::dialog::stories::hosted; use craft_ui::dialog::{ - Alert, DialogHost, DialogId, DialogResult, DialogStack, HostedAlert, PromptKind, StackDialog, StandardPrompt, StartupStage, UnsavedChangesDialog, + Alert, Dialog, DialogHost, DialogId, DialogResult, DialogStack, HostedAlert, PromptKind, StackDialog, StandardPrompt, StartupStage, UnsavedChangesDialog, dialog_id, }; use craft_ui::runtime::windows::{WindowHit, WindowHost}; +use craft_ui::sections::{DialogRole, FooterButton}; use craft_ui::style::{ComponentStyle, StyleCx}; use craft_ui::testing::{Fixture, Frames, Story, StoryData, StoryOutput, trial}; use egui::{Modifiers, Rect, Response, Ui, Vec2, ViewportId, vec2}; @@ -127,6 +128,40 @@ fn a_hosted_alert_answers_ok_however_it_closes_and_opens_again() { } } +/// A hosted dialog that leaves its open state to the dialog shell (no `opened` binding); its OK closes it. +struct SelfClosing; + +impl StackDialog for SelfClosing { + type Result = (); + + fn id(&self) -> DialogId { + dialog_id("self-closing") + } + + fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { + let shown = Dialog::new("Reopen probe").buttons([FooterButton::new("OK", DialogRole::Accept)]).show(ui, |_, _| {}); + shown.output.closed.map(|_| DialogResult::Ok(())) + } +} + +/// A dialog whose shell closed itself (its OK wrote its own open state closed) shows again when the host opens it +/// again under the same id: the host forgets the open state of a dialog it removes. +#[trial] +fn a_dialog_that_closed_itself_shows_when_opened_again() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut fixture = running(&host); + for round in 0..2 { + host.borrow_mut().open(SelfClosing); + fixture.idle(6); + assert!(fixture.by_label("Reopen probe").is_some(), "open {round}: the dialog shows"); + let ok = fixture.by_label("OK").expect("the OK button is shown"); + fixture.click(&ok).idle(4); + assert!(host.borrow().is_empty(), "open {round}: OK closes the dialog"); + assert_eq!(host.borrow_mut().take::(), Some(DialogResult::Ok(())), "open {round}: OK answers"); + assert!(fixture.by_label("Reopen probe").is_none(), "open {round}: the closed dialog is gone"); + } +} + const BAND: PartKey = PartKey::new("band"); /// A style with no look. diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index 2c96ea111..c8942dd41 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -773,7 +773,6 @@ type Build = dyn for<'b> Fn(&'b mut DialogFields) -> ParamDialog<'b> + Send; /// A [`ParamDialog`] the [`DialogHost`](crate::dialog::DialogHost) owns: the host keeps the values, and `build` makes /// the dialog over them each pass. /// -/// - The host keeps the dialog's open state, so a dialog opened again under the same key opens. /// - The host keeps the Preview check box of a dialog that [`previews`](ParamDialog::previews): it starts on, so /// [`is_previewing`](HostedParamDialog::is_previewing) holds from construction, before the first pass. /// - The preview picture comes from [`set_preview`](HostedParamDialog::set_preview) between passes, since `build` cannot @@ -799,9 +798,6 @@ pub struct HostedParamDialog { /// The last range alert a row raised, with its title and the env the dialog showed in (which words it), until the /// host takes it. range_alert: Option<(Label, RangeAlert, Env)>, - /// Whether the dialog is open: bound to the dialog's `opened` prop, so the dialog's own open state, kept under - /// its id, cannot hold a dialog closed that the host opened again. - open: bool, /// The values the dialog opened with, passed to the form as the values Reset restores. opening: DialogFields, /// Whether the dialog has the Preview check box ([`ParamDialog::previews`] of `build`). @@ -853,7 +849,6 @@ impl HostedParamDialog { build: Box::new(build), requests: HostRequests::new(), range_alert: None, - open: true, previews, preview_on: true, picture: None, @@ -1126,7 +1121,6 @@ impl StackDialog for HostedParamDialog { .preview_on(Bind::bound(&mut self.preview_on)) .values_opened(self.opening.clone()) .answers(answers) - .opened(Bind::bound(&mut self.open)) .hold_ok(self.hold) .finish_held(finish) .preview_colors(self.requests.previewed_colors().to_vec());