diff --git a/crates/ui-contract/src/runtime/env/shell.rs b/crates/ui-contract/src/runtime/env/shell.rs index 3dd178554..8cdc66a61 100644 --- a/crates/ui-contract/src/runtime/env/shell.rs +++ b/crates/ui-contract/src/runtime/env/shell.rs @@ -2,10 +2,12 @@ //! carries them to the slots and the body. use super::{AppValues, EnvValue}; +use crate::runtime::egui::pass; +use crate::runtime::frame::{PerViewport, Stamped, Stored}; use egui::Rect; use std::fmt; +use std::sync::Arc; use std::sync::atomic::{AtomicU32, Ordering}; -use std::sync::{Arc, Mutex}; /// The stable key of a slot. pub type SlotKey = &'static str; @@ -123,19 +125,28 @@ struct Inner { regions: ShellRegions, float_bounds: Rect, region: Option, - revealed_rects: RevealedRects, + /// The key of the shell's revealed rects: the same in every pass of the shell, so a replayed view registers its + /// rects for the shell that shows it. + reveals: egui::Id, dialog: DialogRecord, } -/// The rects that body content registered this pass for the edge it was revealed from. The registry is shared by the -/// clones of one `ShellCx`. -#[derive(Clone, Debug, Default)] -struct RevealedRects(Arc>>); +/// The rects that content registered on the running pass of a viewport for the edge it was revealed from, each with +/// the key of its shell. +#[derive(Default)] +struct RevealedRects(Vec<(egui::Id, RevealEdge, Rect)>); -impl PartialEq for RevealedRects { - fn eq(&self, other: &Self) -> bool { - Arc::ptr_eq(&self.0, &other.0) - } +impl Stored for RevealedRects {} + +fn with_revealed(ctx: &egui::Context, f: impl FnOnce(&mut RevealedRects) -> R) -> R { + let viewport = pass::current_viewport(ctx); + PerViewport::>::with_pass(ctx, viewport, pass::pass_nr(ctx, viewport), |rects, _| rects.0.clear(), f) +} + +/// Registers `rect` for `edge` with the shell keyed `reveals`, on the running pass. A replayed view registers it again. +fn note_reveal(ctx: &egui::Context, reveals: egui::Id, edge: RevealEdge, rect: Rect) { + crate::runtime::replay::journal(ctx, || (reveals, edge, rect), |ctx, &(reveals, edge, rect)| note_reveal(ctx, reveals, edge, rect)); + with_revealed(ctx, |rects| rects.0.push((reveals, edge, rect))); } /// How many dialogs, modals and windows the body showed on this pass. The record is shared by the clones of one @@ -154,6 +165,7 @@ impl ShellCx { /// mode allows and floating frames may go over the body. pub fn new(window: WindowKey, mode: BodyMode, regions: ShellRegions) -> Self { let float_bounds = regions.body; + let reveals = crate::runtime::memory::global("shell reveals", &window); let inner = Inner { window, mode, @@ -164,7 +176,7 @@ impl ShellCx { regions, float_bounds, region: None, - revealed_rects: RevealedRects::default(), + reveals, dialog: DialogRecord::default(), }; ShellCx { inner: Arc::new(inner) } @@ -255,17 +267,28 @@ impl ShellCx { !self.inner.panels_hidden && (self.inner.mode == BodyMode::Normal || self.inner.tab_reveal) } + /// The same skeleton with its revealed rects registered under `id`, which stays the same across passes. Without + /// it they are keyed by the window; `AppShell` passes its own id, so an embedded shell keeps its rects apart from + /// the shell of the window around it. + #[must_use] + pub fn with_reveals_key(mut self, id: egui::Id) -> Self { + Arc::make_mut(&mut self.inner).reveals = id; + self + } + /// Registers `rect`, a revealed panel the body drew on this pass, as part of the revealed `edge`: the edge reveal - /// lasts while the pointer is over it, as it does over a revealed slot. Call it in the pass that draws the panel. - pub fn reveal_rect(&self, edge: RevealEdge, rect: Rect) { - if let Ok(mut rects) = self.inner.revealed_rects.0.lock() { - rects.push((edge, rect)); + /// lasts while the pointer is over it, as it does over a revealed slot. Call it in the pass that draws the panel; a + /// memoized view that replays registers it again. A measuring run registers nothing. + pub fn reveal_rect(&self, ctx: &egui::Context, edge: RevealEdge, rect: Rect) { + if crate::runtime::measure::effect(ctx, "shell::reveal_rect") { + note_reveal(ctx, self.inner.reveals, edge, rect); } } /// The rects registered with [`reveal_rect`](Self::reveal_rect) on this pass, in registration order. - pub fn revealed_rects(&self) -> Vec<(RevealEdge, Rect)> { - self.inner.revealed_rects.0.lock().map(|rects| rects.clone()).unwrap_or_default() + pub fn revealed_rects(&self, ctx: &egui::Context) -> Vec<(RevealEdge, Rect)> { + let reveals = self.inner.reveals; + with_revealed(ctx, |rects| rects.0.iter().filter(|(shell, _, _)| *shell == reveals).map(|&(_, edge, rect)| (edge, rect)).collect()) } /// Records that a container in this shell shows as a dialog, a modal, a full-window layer or an OS window. Every @@ -318,11 +341,12 @@ impl ShellCx { pub struct ShellEntry(#[doc(hidden)] pub Option); /// The shell's input fields: the window, the body's mode, the reveal and visibility flags, the regions and the float -/// bounds. The registries the body writes into (revealed rects, the dialog count) are its output, not an input. +/// bounds. The registries the body writes into (revealed rects, the dialog count) are its output, not an input; the +/// revealed rects' key is fixed for the shell's life. impl crate::contract::MemoProps for ShellEntry { fn key(&self, key: &mut crate::contract::MemoKey) { let Some(shell) = &self.0 else { return key.write_u32(0) }; - let Inner { window, mode, tab_reveal, edges, tabs, panels_hidden, regions, float_bounds, region, revealed_rects: _, dialog: _ } = + let Inner { window, mode, tab_reveal, edges, tabs, panels_hidden, regions, float_bounds, region, reveals: _, dialog: _ } = &*shell.inner; key.write_u32(1); key.feed_hash(window); diff --git a/crates/ui-contract/src/runtime/layout_memory.rs b/crates/ui-contract/src/runtime/layout_memory.rs index 3229ba943..c06521a03 100644 --- a/crates/ui-contract/src/runtime/layout_memory.rs +++ b/crates/ui-contract/src/runtime/layout_memory.rs @@ -1218,6 +1218,7 @@ pub(crate) mod guard { ("runtime::windows::Shared", "window shells: read"), ("runtime::windows::Record", "window records: read"), ("runtime::windows::hit::Exclusive", "exclusive windows: read"), + ("runtime::env::shell::RevealedRects", "revealed rects: read; registered in real runs only"), ("runtime::focus::State", "focus: read; stops register in real runs only"), ("runtime::keys::Stage", "key stage: read"), ("primitives::sensing::Arenas", "arenas: read; members join in real runs only"), diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 4af84f572..4401d77be 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -589,8 +589,8 @@ fn show_area( let Some(path) = scene.layout.dock_path(edge) else { continue }; let Some(Node::Dock { columns, .. }) = scene.layout.root.as_ref().and_then(|root| node_at(root, &path)) else { continue }; leaves.push((path.clone(), rect)); - if let Some(shell) = shell.as_ref().filter(|shell| shell.revealed() && crate::runtime::measure::effect(ctx, "shell::reveal_rect")) { - shell.reveal_rect(if edge == DockEdge::Start { RevealEdge::Start } else { RevealEdge::End }, rect); + if let Some(shell) = shell.as_ref().filter(|shell| shell.revealed()) { + shell.reveal_rect(ctx, if edge == DockEdge::Start { RevealEdge::Start } else { RevealEdge::End }, rect); } pass.dock(ui, split, DockPlan { edge, columns, rect, flow, open: open.clone() }); } diff --git a/crates/ui-shell/src/shell/app_shell.rs b/crates/ui-shell/src/shell/app_shell.rs index 16d64c203..f067ae344 100644 --- a/crates/ui-shell/src/shell/app_shell.rs +++ b/crates/ui-shell/src/shell/app_shell.rs @@ -23,6 +23,7 @@ use crate::runtime::egui::pass; use crate::runtime::env::{AppValues, BodyMode, Env, Label, RevealEdge, ShellCx, ShellEntry, ShellRegions, WindowKey}; use crate::runtime::focus::FocusPolicy; use crate::runtime::frame::{note_shell, take_chrome_toggles}; +use crate::runtime::memory::sub; use crate::runtime::overlay::{OpenFilter, any_open}; use crate::runtime::pointer; use crate::runtime::windows::{WindowAction, WindowHost}; @@ -493,7 +494,8 @@ impl<'a, S> AppShell<'a, S> { let mut shell = ShellCx::new(window.clone(), if only { BodyMode::Only } else { BodyMode::Normal }, regions.clone()) .with_tabs(tabs) .with_tab_reveal(kept.reveal.tab()) - .with_panels_hidden(kept.reveal.panels_hidden()); + .with_panels_hidden(kept.reveal.panels_hidden()) + .with_reveals_key(sub(cx.id, "reveals")); // The room an embedded shell shows: its rect less what a scroll or a parent clips. Its dialogs are contained in // it. let region = cx.rect.intersect(ui.clip_rect()); @@ -579,7 +581,7 @@ impl<'a, S> AppShell<'a, S> { slots.iter().find(|placed| placed.slot.key() == *key).and_then(|placed| placed.edge.reveal_edge()).map(|edge| (edge, *rect)) }) .collect(); - edges.extend(scope.revealed_rects()); + edges.extend(scope.revealed_rects(ui.ctx())); kept.reveal.record(edges); let revealed = only && !regions.slots.is_empty(); *cx.keep::() = kept; diff --git a/crates/ui-shell/tests/it/shell/app_shell.rs b/crates/ui-shell/tests/it/shell/app_shell.rs index 0f890aba4..be95b0ccb 100644 --- a/crates/ui-shell/tests/it/shell/app_shell.rs +++ b/crates/ui-shell/tests/it/shell/app_shell.rs @@ -6,7 +6,7 @@ use craft_ui::buttons::Button; use craft_ui::contract::{MemoProps, MemoView, Show as _, ShowContainer as _}; use craft_ui::display::Text; use craft_ui::layout_components::{Row, Stack}; -use craft_ui::runtime::env::WindowKey; +use craft_ui::runtime::env::{Env, RevealEdge, ShellEntry, WindowKey}; use craft_ui::shell::{AppShell, Slot, Title, TitleBar}; use craft_ui::testing::pointer::{press, release}; use craft_ui::testing::{Fixture, Frames, trial}; @@ -166,3 +166,48 @@ fn memoized_slots_run_only_when_their_props_change() { assert_eq!(frame(&mut frames, vec![]).0, vec!["status"], "a new status runs the status slot alone, once"); assert_eq!(frame(&mut frames, vec![]).0, Vec::<&str>::new(), "and it replays at it"); } + +/// What a slot that registers a revealed rect shows: the rect. +#[derive(MemoProps)] +struct Revealing { + at: Rect, +} + +fn revealing(props: &mut Revealing, ui: &mut Ui) { + SLOT_RUNS.with(|runs| runs.borrow_mut().push("revealing")); + if let Some(shell) = Env::of(ui).view().get::().0 { + shell.reveal_rect(ui.ctx(), RevealEdge::Start, props.at); + } + Text::new("Tools").show(ui); +} + +/// A memoized view in a slot that registers a revealed rect registers it again when it replays: the shell reads the +/// same rects in a replayed pass as in the run. +#[trial] +fn a_replayed_view_in_a_slot_registers_its_revealed_rects() { + let at = Rect::from_min_size(egui::pos2(0.0, 40.0), Vec2::new(40.0, 120.0)); + let seen = RefCell::new(Vec::new()); + let frame = |frames: &mut Frames| { + frames.frame(vec![], |ui| { + AppShell::new(WindowKey::main()) + .start(Slot::new("tools"), |ui, _, _: &mut ()| { + let _ = MemoView::new("tools", Revealing { at }, revealing).show(ui); + }) + .body(|ui, bcx, _| { + *seen.borrow_mut() = bcx.shell().map(|shell| shell.revealed_rects(ui.ctx())).unwrap_or_default(); + }) + .show(ui, &mut ()); + }); + (slot_runs(), seen.borrow().clone()) + }; + let mut frames = Frames::new(); + let (ran, first) = frame(&mut frames); + assert!(ran.contains(&"revealing"), "the slot runs in its first frame"); + assert_eq!(first, vec![(RevealEdge::Start, at)], "its run registers the rect"); + for _ in 0..30 { + frame(&mut frames); + } + let (ran, replayed) = frame(&mut frames); + assert_eq!(ran, Vec::<&str>::new(), "the slot replays"); + assert_eq!(replayed, first, "the replay registers the same rects as the run"); +}