diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index 337e46fcc..660349218 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -254,6 +254,7 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U } let mut entry = OpenEntry::new(layer.id, overlay.rank, overlay.keys, shown.rect, shown.layer); entry.parent = parent; + entry.role = overlay.role; entry.anchor = matches!(overlay.place, Place::Anchored { .. }).then_some(anchor); entry.side = placed.side; entry.asked = placed.asked; @@ -334,6 +335,7 @@ fn host_in_popup(ctx: &Context, layer: &Layer<'_>, popup: PopupRoom, body: &mut } let mut entry = OpenEntry::new(layer.id, overlay.rank, overlay.keys, rect, areas::layer_of(tier, area_id(layer.id))); entry.parent = parent; + entry.role = overlay.role; entry.anchor = matches!(overlay.place, Place::Anchored { .. }).then_some(anchor); entry.side = placed.side; entry.asked = placed.asked; @@ -418,6 +420,7 @@ fn show_in_popup( }); overlay::end_layer(&ctx); let mut entry = OpenEntry::new(layer.id, overlay.rank, overlay.keys, shown.rect, shown.layer); + entry.role = overlay.role; entry.side = placed.side; entry.asked = placed.asked; entry.exiting = layer.exiting; @@ -529,6 +532,7 @@ pub(crate) fn host_window(ctx: &Context, layer: &Layer<'_>, rect: Rect) -> Layer let scope = (overlay.focus != FocusPolicy::NONE).then_some(overlay.focus); let mut entry = OpenEntry::new(layer.id, overlay.rank, overlay.keys, rect, areas::layer_of(overlay::layer_tier(ctx, overlay.rank), area_id(layer.id))); + entry.role = overlay.role; entry.scoped = scope.is_some(); entry.window = true; overlay::record(ctx, entry); diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index d067bc936..f89811f96 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -115,7 +115,8 @@ pub enum LayerRole { /// An ordinary layer. #[default] Plain, - /// A progress window: `any_open` leaves it out, as it blocks nothing the user waits on. + /// A progress window: `any_open` with `OpenFilter::Asks` leaves it out, as it asks the user nothing. The other + /// filters count it, since it still covers the window. Progress, /// A field over its owner's own content that edits it in place (a page's rename field): it is part of the owner, /// so the owner reports no open or expanded state and no popup it controls while it shows. @@ -132,6 +133,8 @@ pub enum OpenFilter { Modal, /// Layers that take keys: popups and modals. Keys, + /// Modal layers that wait for the user's answer: every modal layer except a progress window. + Asks, } /// One shown egui layer. @@ -442,10 +445,11 @@ pub(crate) fn peek_close(ctx: &Context, id: Id) -> Option { pub fn any_open(ctx: &Context, filter: OpenFilter) -> bool { crate::runtime::replay::note_input(ctx, crate::runtime::inputs::Input::Overlays); with_pass(ctx, |pass| { - pass.last.iter().filter(|entry| !entry.exiting && entry.role != LayerRole::Progress).any(|entry| match filter { + pass.last.iter().filter(|entry| !entry.exiting).any(|entry| match filter { OpenFilter::Any => true, OpenFilter::Modal => entry.blocks_window(), OpenFilter::Keys => entry.takes_keys(), + OpenFilter::Asks => entry.blocks_window() && entry.role != LayerRole::Progress, }) }) } diff --git a/crates/ui-panels/src/dialog/job_progress.rs b/crates/ui-panels/src/dialog/job_progress.rs index 134c5bcae..8c16efc5f 100644 --- a/crates/ui-panels/src/dialog/job_progress.rs +++ b/crates/ui-panels/src/dialog/job_progress.rs @@ -6,13 +6,15 @@ use crate::contract::{Bind, Show as _, ShowContainer as _}; use crate::display::{Progress, Shade, Text}; use crate::layout_components::Row; use crate::runtime::env::{Env, Label}; +use crate::runtime::overlay::LayerRole; use crate::runtime::words::CommonWords; use crate::sections::DialogRole; use crate::style::TypeRole; use egui::{Response, Ui}; /// Modal progress window for a running job: a progress bar (or an indeterminate one), counts and the percentage, the -/// queued line, and Cancel (and Hide, when `can_hide`). Escape and the close action give Cancel. +/// queued line, and Cancel (and Hide, when `can_hide`). Escape and the close action give Cancel. Its layer has the +/// progress role, so `any_open` with `OpenFilter::Asks` leaves it out. // PLAN: `#[pattern(dialog)]` needs `StackDialog` (`dialog_probe`); the app draws this window directly, so it registers as a plain `Pattern` #[derive(crate::contract::Pattern)] pub struct JobProgress { @@ -72,6 +74,7 @@ impl JobProgress { // leave a session slot closed for the next job. let mut open = true; let shown = Prompt::new(title, step) + .layer_role(LayerRole::Progress) .answers(buttons(&env, replies)) .extras(|ui| { match fraction { diff --git a/crates/ui-panels/src/dialog/prompt.rs b/crates/ui-panels/src/dialog/prompt.rs index 67c28cdd7..9ae51f814 100644 --- a/crates/ui-panels/src/dialog/prompt.rs +++ b/crates/ui-panels/src/dialog/prompt.rs @@ -10,7 +10,7 @@ use crate::notices::{Notice, NoticeLayout}; use crate::primitives::IconSpec; use crate::runtime::env::{Env, Label}; use crate::runtime::focus::Initial; -use crate::runtime::overlay::CloseReason; +use crate::runtime::overlay::{CloseReason, LayerRole}; use crate::sections::{DialogRole, FooterButton, Section}; use craft_behavior::{IconId, Mixed}; use craft_keymap::{Chord, mnemonic}; @@ -227,6 +227,8 @@ pub struct Prompt<'a> { escape: Option, /// Whether the dialog reports the alert-dialog role, described by the message and any tone. alert: bool, + /// What the dialog's layer is for. + layer_role: LayerRole, } impl<'a> Prompt<'a> { @@ -241,6 +243,7 @@ impl<'a> Prompt<'a> { open: None, escape: None, alert: false, + layer_role: LayerRole::Plain, } } @@ -252,6 +255,13 @@ impl<'a> Prompt<'a> { self } + /// What the dialog's layer is for: `LayerRole::Progress` for a job's progress window. + #[must_use] + pub(super) fn layer_role(mut self, role: LayerRole) -> Self { + self.layer_role = role; + self + } + /// The answers, one footer button each. #[must_use] pub fn answers(mut self, answers: impl IntoIterator) -> Self { @@ -303,12 +313,16 @@ impl<'a> Prompt<'a> { /// Shows the prompt. pub fn show(self, ui: &mut Ui) -> PromptOutput { - let Prompt { title, message, answers, extras, icon, open, escape, alert } = self; + let Prompt { title, message, answers, extras, icon, open, escape, alert, layer_role } = self; let (escape_at, enter_at) = (escape_answer(&answers, escape), default_answer(&answers)); // With no available default answer (none, or a blocked one) focus stays off the buttons: landing on the first // other one would let Enter answer with it (Discard under a blocked Recover, Cancel in a job window). let initial = if enter_at.is_some() { Initial::Default } else { Initial::Keep }; - let mut dialog = Dialog::prompt(title).buttons(answers).initial_focus(initial).close_on_enter(enter_at.is_some()); + let mut dialog = Dialog::prompt(title) + .buttons(answers) + .initial_focus(initial) + .close_on_enter(enter_at.is_some()) + .overlay(|overlay| overlay.role(layer_role)); if alert { let env = Env::of(ui); let view = env.view(); diff --git a/crates/ui-panels/tests/it/dialogs.rs b/crates/ui-panels/tests/it/dialogs.rs index 453dc081e..1cc136919 100644 --- a/crates/ui-panels/tests/it/dialogs.rs +++ b/crates/ui-panels/tests/it/dialogs.rs @@ -3,12 +3,13 @@ use craft_behavior::{Metrics, MotionMode, PartKey, Sense}; use craft_host::target::{Os, WindowChrome}; use craft_quit::{CloseAnswer, CloseScope}; -use craft_ui::contract::overlay::{Dismiss, Outside}; +use craft_ui::contract::overlay::{Dismiss, OpenFilter, Outside, any_open}; use craft_ui::contract::{Bind, Common, Component, Cx, MeasureCx, PartsCx, Props as _, Role, Show as _, ShowContainer as _}; use craft_ui::dialog::stories::hosted; use craft_ui::dialog::{ - Alert, ChildDialog, Dialog, DialogHost, DialogId, DialogResult, DialogStack, HostedAlert, JobAnswer, JobProgress, PromptChoice, PromptKind, - RecoveredDocument, RecoveryAnswer, RecoveryChoice, RecoveryDialog, StackDialog, StandardPrompt, StartupStage, UnsavedChangesDialog, dialog_id, + Alert, ChildDialog, Dialog, DialogHost, DialogId, DialogResult, DialogStack, HostedAlert, JobAnswer, JobProgress, Prompt, PromptChoice, + PromptKind, RecoveredDocument, RecoveryAnswer, RecoveryChoice, RecoveryDialog, StackDialog, StandardPrompt, StartupStage, UnsavedChangesDialog, + dialog_id, }; use craft_ui::display::Text; use craft_ui::popover::Popover; @@ -302,6 +303,32 @@ fn a_prompt_without_a_default_answer_opens_with_no_button_focused() { assert_eq!(answered.get(), Some(JobAnswer::Cancel)); } +/// A job window is not a dialog that asks: an app that draws it only while no such dialog is open draws it in every +/// pass, and the window still counts as a modal layer. A prompt with an answer counts as asking. +#[trial] +fn a_job_window_is_a_modal_layer_that_asks_nothing() { + let (passes, drawn) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0))); + let (all, shown) = (Rc::clone(&passes), Rc::clone(&drawn)); + let mut fixture = Fixture::new(move |ui| { + all.set(all.get() + 1); + if !any_open(ui.ctx(), OpenFilter::Asks) { + JobProgress::new("Exporting", "Page 1 of 3").show(ui); + shown.set(shown.get() + 1); + } + }); + fixture.idle(6); + assert_eq!(drawn.get(), passes.get(), "the window is drawn in every pass"); + assert!(fixture.by_label("Exporting").is_some(), "the window shows"); + assert!(any_open(fixture.ctx(), OpenFilter::Modal), "the window is a modal layer"); + assert!(!any_open(fixture.ctx(), OpenFilter::Asks), "the window asks nothing"); + + let mut fixture = Fixture::new(|ui| { + Prompt::new("Replace the file?", "A file with this name exists.").answers([FooterButton::new("Replace", DialogRole::Accept)]).show(ui); + }); + fixture.idle(6); + assert!(any_open(fixture.ctx(), OpenFilter::Asks), "a prompt with an answer asks"); +} + const BAND: PartKey = PartKey::new("band"); /// A style with no look. diff --git a/docs/contract-api.md b/docs/contract-api.md index 3128890a6..754789c5d 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -4422,7 +4422,7 @@ The layer stack (15.2): the record of the egui layers shown, their order and the - `craft_ui::runtime::overlay::Backdrop`: `pub enum Backdrop { None, Scrim }`. A modal's backdrop. - `craft_ui::runtime::overlay::CloseReason`: `pub enum CloseReason { Requested, Escape, Outside, FocusLeft(crate::runtime::focus::FocusMove), Activated, Enter, Timeout, AnchorGone }`. Why a layer closes. - `craft_ui::runtime::overlay::LayerRole`: `pub enum LayerRole { Plain, Progress, InPlaceEditor }`. What a layer is for, beyond its rank. -- `craft_ui::runtime::overlay::OpenFilter`: `pub enum OpenFilter { Any, Modal, Keys }`. What `any_open` counts. +- `craft_ui::runtime::overlay::OpenFilter`: `pub enum OpenFilter { Any, Modal, Keys, Asks }`. What `any_open` counts. - `craft_ui::runtime::overlay::Rank`: `pub enum Rank { Window, Toast, Popup, Modal { backdrop: Backdrop }, ModalChrome, Tooltip }`. An overlay's place in the order. `Rank` is the only place a layer's egui order is chosen. - `pub const fn modal(backdrop: Backdrop) -> Rank`. A modal rank with `backdrop`. - `craft_ui::runtime::overlay::any_open`: `pub fn any_open(ctx: &Context, filter: OpenFilter) -> bool`. Whether any layer of the running viewport that `filter` counts was shown last frame.