mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
Layer stack: dialogs stack at their stack place; app-drawn modals are modal layers of the stack
overlay::stack_place gives the layers a dialog host shows their place on its stack, and the modal families that carry one stack in the stack's order in the slots the raise order gave them: a press on a lower dialog or a dialog replaced in place no longer reorders them. contract::overlay::show_beside_modal shows an egui::Modal the app draws itself as a modal layer (order, top modal, egui's modal layer, key blocking, a focus scope that leaves Escape and Enter to the app, parent of the layers opened inside it); close_beside_modal closes those layers when the dialog closes.
This commit is contained in:
@@ -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};
|
||||
|
||||
@@ -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<T>(ctx: &Context, id: Id, modal: egui::Modal, body: impl FnOnce(&mut Ui) -> T) -> egui::ModalResponse<T> {
|
||||
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) {
|
||||
|
||||
@@ -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<Rect>,
|
||||
/// The layer's place on its dialog stack (`stack_place`): the modal families of one stack stack in its order.
|
||||
pub(crate) place: Option<usize>,
|
||||
}
|
||||
|
||||
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<Id>,
|
||||
/// 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<usize>,
|
||||
}
|
||||
|
||||
/// The top modal layer of a viewport: its id and the scrim under it.
|
||||
@@ -263,6 +268,7 @@ fn with_pass<R>(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<Rect>) {
|
||||
});
|
||||
}
|
||||
|
||||
/// 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<R>(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<Id, u64>) -> Vec<Vec<OpenEnt
|
||||
// A modal opened from a floating panel lifts the whole family: the family sorts by its highest member.
|
||||
let class = |members: &Vec<OpenEntry>| 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<OpenEntry>| members.iter().filter_map(|member| member.place).min();
|
||||
let slots: Vec<usize> =
|
||||
(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<Vec<OpenEntry>> = 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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user