diff --git a/crates/ui-contract/src/contract/overlay.rs b/crates/ui-contract/src/contract/overlay.rs index 989d4fd41..3c02dd147 100644 --- a/crates/ui-contract/src/contract/overlay.rs +++ b/crates/ui-contract/src/contract/overlay.rs @@ -18,8 +18,8 @@ mod ui; pub use container_open::Popup; pub(crate) use container_open::{opener_open, set_opener_open}; -pub use host::paint_frame_layers; pub(crate) use host::{Layer, floor_width, forget_layer, host, host_window}; +pub use host::{close_beside_modal, paint_frame_layers, show_beside_modal}; pub(crate) use opener::opener_floor; pub use opener::{Opener, Trigger}; pub use place::{Anchor, FloatPlace, OverlayFit, Place, Resize, SizeLimits, ViewportShare}; diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index c13730664..32e43de4b 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -245,6 +245,43 @@ pub(crate) fn host_window(ctx: &Context, layer: &Layer<'_>, rect: Rect) -> Layer LayerOut { dismissed } } +/// The focus scope of a modal the app draws itself: Tab stays inside, focus stays where it is when it opens, and +/// Escape and Enter go on to the app, which answers them for its dialog and the egui popups in it. +const BESIDE: FocusPolicy = FocusPolicy { kind: craft_behavior::ScopeKind::Dialog, trap: true, initial: focus::Initial::Keep, ..FocusPolicy::NONE }; + +/// Shows `modal`, a modal dialog the app draws itself beside a dialog host's, as a modal layer of the layer stack: +/// its place in the order (inside a dialog host's `stack_place`, the stack's), the top modal and egui's modal +/// layer when it is on top, the keys of the framework controls inside it and below it, and the parent of the layers +/// opened from inside it. `id` names the layer: the dialog's id on the stack. The app answers its own Escape and +/// Enter; [`close_beside_modal`] closes the layers opened from inside it when the dialog closes. +pub fn show_beside_modal(ctx: &Context, id: Id, modal: egui::Modal, body: impl FnOnce(&mut Ui) -> T) -> egui::ModalResponse { + let parent = overlay::drawing_parent(ctx); + let response = modal.show(ctx, |ui| { + let real = !ui.is_sizing_pass(); + if real { + focus::push_scope(ctx, id, BESIDE, None); + overlay::begin_layer(ctx, id, false); + } + let out = body(ui); + if real { + overlay::end_layer(ctx); + focus::pop_scope(ctx); + } + out + }); + let shown = &response.response; + let mut entry = OpenEntry::new(id, Rank::modal(Backdrop::None), craft_behavior::OwnerClass::Dialog, shown.rect, shown.layer_id); + entry.parent = parent; + entry.scoped = true; + overlay::record(ctx, entry); + response +} + +/// The modal the app drew with [`show_beside_modal`] as `id` closed: the layers opened from inside it close too. +pub fn close_beside_modal(ctx: &Context, id: Id) { + overlay::close_descendants(ctx, id); +} + /// Paints the ghost egui layer from runtime data: the drag preview and drop mark, and the painted cursor the cursor /// service settled on. The end hook calls it once per pass, popup or no popup. pub fn paint_frame_layers(ctx: &Context) { diff --git a/crates/ui-contract/src/runtime/overlay.rs b/crates/ui-contract/src/runtime/overlay.rs index 42792b03b..0651458d8 100644 --- a/crates/ui-contract/src/runtime/overlay.rs +++ b/crates/ui-contract/src/runtime/overlay.rs @@ -154,6 +154,8 @@ pub struct OpenEntry { pub(crate) window: bool, /// The screen rect the layer is contained in: its scrim, placement, clip and input blocking stop at it. pub(crate) region: Option, + /// The layer's place on its dialog stack (`stack_place`): the modal families of one stack stack in its order. + pub(crate) place: Option, } impl OpenEntry { @@ -175,6 +177,7 @@ impl OpenEntry { absorbs: false, window: false, region: None, + place: None, } } @@ -209,6 +212,8 @@ pub(crate) struct Pass { pressed_outside: Vec, /// The popup a press that opened it ended on, and where, this pass. dragged_release: Option<(Id, Pos2)>, + /// The dialog stack place of the layers recorded now (`stack_place`). + place: Option, } /// The top modal layer of a viewport: its id and the scrim under it. @@ -263,6 +268,7 @@ fn with_pass(ctx: &Context, f: impl FnOnce(&mut Pass) -> R) -> R { pass.claimed.clear(); pass.pressed_outside.clear(); pass.dragged_release = None; + pass.place = None; }, f, ) @@ -557,12 +563,24 @@ pub(crate) fn note_peers(ctx: &Context, id: Id, peers: Vec) { }); } +/// Runs `show` with the layers it records at `place` on the dialog stack: a dialog host shows each open dialog +/// inside its stack place, bottom first, and the modal families of the stack then stack in that order whatever +/// opened, appeared or was pressed last. Families without a place keep their raise order. +#[doc(hidden)] +pub fn stack_place(ctx: &Context, place: usize, show: impl FnOnce() -> R) -> R { + let outer = with_pass(ctx, |pass| pass.place.replace(place)); + let out = show(); + with_pass(ctx, |pass| pass.place = outer); + out +} + /// Records the layer shown in this pass, parent first: a layer is recorded once its body ran, after the layers opened /// from inside it, so it goes in front of them, and every layer then stacks above the layer it was opened from. A /// layer shown again in a discarded pass replaces its entry. -pub(crate) fn record(ctx: &Context, entry: OpenEntry) { +pub(crate) fn record(ctx: &Context, mut entry: OpenEntry) { crate::runtime::replay::block(ctx); with_pass(ctx, |pass| { + entry.place = entry.place.or(pass.place); if let Some(at) = pass.current.iter().position(|recorded| recorded.id == entry.id) { pass.current[at] = entry; return; @@ -602,6 +620,16 @@ fn families(entries: &[OpenEntry], raised: &HashMap) -> Vec| members.iter().map(|member| member.rank.class()).max().unwrap_or(0); groups.sort_by_key(|(root, members)| (class(members), raised.get(root).copied().unwrap_or(0))); + // The modal families on a dialog stack take the slots the raise order gave them in the stack's order: a press + // on a lower dialog or a dialog replaced in place never reorders the stack's dialogs. + let place = |members: &Vec| members.iter().filter_map(|member| member.place).min(); + let slots: Vec = + (0..groups.len()).filter(|&at| class(&groups[at].1) == Rank::modal(Backdrop::None).class() && place(&groups[at].1).is_some()).collect(); + let mut stacked: Vec> = slots.iter().map(|&at| std::mem::take(&mut groups[at].1)).collect(); + stacked.sort_by_key(place); + for (at, members) in slots.into_iter().zip(stacked) { + groups[at].1 = members; + } groups.into_iter().map(|(_, members)| members).collect() }