From fac70bfc88eddbceda3803cae4235d47642f6ee9 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 03:49:21 +0000 Subject: [PATCH] Popup bodies are content-sized layer containers placed by this pass's measure OverlayUi::show shows its body closure through the container pipeline: a private content-sized layer container (popup surface, POPUP_PAD, no node of its own). Step 5 measures the body before the host places the layer, so a popup whose content changes is placed by its new size in the pass that shows it, and a filling child takes the width the popup settles on. The popup keeps its owner key: ContainerProps::owner (crate-private) makes the container's memory, presence and layer bookkeeping belong to the component that shows the popup (or the opener its overlay names), so body memory lasts across a close and KeepAnchor focus return finds the anchor. The host places a measured layer by at least its least width. The layer's least width at its anchor reaches a content-sized collection through the LayerFloor env value the layer container gives its regions in both runs, replacing the least width the layer memory carried from the last pass. --- crates/ui-contract/src/contract/body/hooks.rs | 8 +- .../ui-contract/src/contract/body/inherit.rs | 2 + crates/ui-contract/src/contract/body/open.rs | 2 +- crates/ui-contract/src/contract/body/size.rs | 2 +- crates/ui-contract/src/contract/container.rs | 4 + .../ui-contract/src/contract/items/hooks.rs | 2 +- crates/ui-contract/src/contract/mount.rs | 26 ++-- crates/ui-contract/src/contract/overlay.rs | 4 +- .../ui-contract/src/contract/overlay/host.rs | 33 ++--- .../src/contract/overlay/opener.rs | 30 +++- crates/ui-contract/src/contract/overlay/ui.rs | 129 ++++++++++++------ crates/ui-contract/src/contract/run.rs | 9 +- crates/ui-contract/src/contract/run/typed.rs | 16 ++- 13 files changed, 182 insertions(+), 85 deletions(-) diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 7b08a5230..994cc8910 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -78,6 +78,10 @@ impl ShapeHooks for BodyHooks<'_> { comp.props(Internal::new()).command_scope } + fn memory_owner(&self, comp: &C) -> Option { + comp.props(Internal::new()).owner + } + fn dismissed(&mut self, comp: &mut C, cx: &mut Cx<'_, C>, why: CloseReason) { self.close = true; self.open = false; @@ -427,9 +431,9 @@ impl ShapeHooks for BodyHooks<'_> { } None => self.measure_run(comp, at, cx, key, None), }; - // A layer hung from an opener is at least as wide as the opener its overlay asks for, in the measuring run too, + // A layer hung from an opener or a rect is at least as wide as its overlay asks for, in the measuring run too, // so the rows of a content-sized list inside it are placed at that width. - let took = Vec2::new(overlay_of(&props).map_or(took.x, |overlay| took.x.max(crate::contract::overlay::opener_floor(overlay))), took.y); + let took = Vec2::new(overlay_of(&props).map_or(took.x, |overlay| took.x.max(crate::contract::overlay::anchor_floor(overlay))), took.y); self.overflow = (took.x - self.room.x).max(0.0); // A body wider than the room overflows it, or scrolls along it; either way the container keeps to the room. Vec2::new(took.x.min(self.room.x), if vertical { took.y.min(self.room.y) } else { took.y }) diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index 8d0c00b6e..53772b857 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -53,6 +53,8 @@ impl BodyHooks<'_> { // opener's env as in the run under the layer's. if matches!(props.mount, Mount::Layer(_) | Mount::Viewport { .. } | Mount::FullWindow) { scoped.start_layer(); + let floor = super::placement::overlay_of(props).map_or(0.0, crate::contract::overlay::anchor_floor); + scoped.inherited_mut().values.set(crate::contract::overlay::LayerFloor(floor)); } // The container's own `enabled(false)` greys everything inside it, as a scope would. scoped.enabled &= cx.core.resolved.enabled; diff --git a/crates/ui-contract/src/contract/body/open.rs b/crates/ui-contract/src/contract/body/open.rs index fbb3cefd2..03b07eed3 100644 --- a/crates/ui-contract/src/contract/body/open.rs +++ b/crates/ui-contract/src/contract/body/open.rs @@ -50,7 +50,7 @@ pub(crate) fn unshown(comp: &mut C, ctx: &Context, id: Id, props: return false; } if !sizing { - let mut stage = MemoryStage::open_after(ctx, id, id); + let mut stage = MemoryStage::open_after(ctx, id, props.owner.unwrap_or(id)); let open = open_now(comp, &mut stage, id, props); debug_assert!(!open, "a peek and a real stage decide the same open state"); stage.write_back(); diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index e4cc09550..c5d682cb9 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -243,7 +243,7 @@ impl<'b> BodyHooks<'b> { self.measured_body.map(|body| { let (header, end) = (self.measured_header.unwrap_or(Vec2::ZERO), self.measured_end.unwrap_or(Vec2::ZERO)); let wanted = self.regions_size(props, body, header, self.measured_footer, end).x.max(self.least_width); - overlay_of(props).map_or(wanted, |overlay| wanted.max(crate::contract::overlay::opener_floor(overlay))) + overlay_of(props).map_or(wanted, |overlay| wanted.max(crate::contract::overlay::anchor_floor(overlay))) }) } diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 5696333d4..7dc25b892 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -766,6 +766,9 @@ pub struct ContainerProps { pub label_column: bool, /// How the labelled rows under the container show their labels; `None` keeps the scheme of the scope. pub labels: Option, + /// The component whose memory holds the container's shown memory and its layer's bookkeeping, when it is another: + /// the body of a popup an `overlay` hook shows belongs to the component that shows it. + pub(crate) owner: Option, } impl Default for ContainerProps { @@ -844,6 +847,7 @@ impl ContainerProps { collection_body: false, label_column: false, labels: None, + owner: None, } } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 4b234c126..2d3a37e23 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -203,7 +203,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { } // An unbounded room (a horizontally scrolling body) takes the widest row, as a content-sized collection does. (_, _) => { - let floor = overlay::drawing_parent(ui.ctx()).map_or(0.0, |layer| crate::contract::overlay::floor_width(ui.ctx(), layer)); + let floor = env.get::().0; let widest = layout_memory::measured(ui.ctx(), sub(id, "largest")).map_or(0.0, |measured| measured.took.x); // Columns side by side at their own widths: a `Content` column takes its widest cell, not the share of // the widest row it would get. diff --git a/crates/ui-contract/src/contract/mount.rs b/crates/ui-contract/src/contract/mount.rs index 46343dc73..0abbea9a6 100644 --- a/crates/ui-contract/src/contract/mount.rs +++ b/crates/ui-contract/src/contract/mount.rs @@ -32,7 +32,7 @@ pub(super) fn host(props: &ContainerProps, at: &HostCx<'_>, open: bool, title: & let presence = if at.ui.is_sizing_pass() { Presence { t: 1.0, visible: open, exiting: false } } else { - motion::layer_presence(ctx, sub(at.id, "presence"), at.id, at.env.motion, open, overlay.motion) + motion::layer_presence(ctx, sub(at.id, "presence"), props.owner.unwrap_or(at.id), at.env.motion, open, overlay.motion) }; (presence.visible.then_some(presence.t), !open) }; @@ -46,7 +46,7 @@ pub(super) fn host(props: &ContainerProps, at: &HostCx<'_>, open: bool, title: & Mount::Pane(pane) => Mounted { host: Box::new(PaneHost(*pane)), exiting: false }, Mount::Window { .. } => Mounted { host: Box::new(WindowContentHost), exiting: false }, Mount::Layer(overlay) => match layer(overlay) { - (Some(fade), exiting) => Mounted { host: Box::new(LayerHost::new(overlay.clone(), props.save.clone(), exiting, fade, at.size)), exiting }, + (Some(fade), exiting) => Mounted { host: Box::new(LayerHost::new(overlay.clone(), props, exiting, fade, at.size)), exiting }, (None, _) => closed(), }, Mount::FullWindow => full_window(props.focus), @@ -57,9 +57,7 @@ pub(super) fn host(props: &ContainerProps, at: &HostCx<'_>, open: bool, title: & full_window(fallback.focus) } else if !viewports { match layer(fallback) { - (Some(fade), exiting) => { - Mounted { host: Box::new(LayerHost::new(fallback.clone(), props.save.clone(), exiting, fade, at.size)), exiting } - } + (Some(fade), exiting) => Mounted { host: Box::new(LayerHost::new(fallback.clone(), props, exiting, fade, at.size)), exiting }, (None, _) => closed(), } } else if open { @@ -192,6 +190,8 @@ impl Host for PaneHost { struct LayerHost { overlay: Overlay, save: Option, + /// The component whose memory holds the layer's bookkeeping (`ContainerProps::owner`), else the container. + owner: Option, exiting: bool, fade: f32, /// The size the container measured for this pass. @@ -200,8 +200,8 @@ struct LayerHost { } impl LayerHost { - fn new(overlay: Overlay, save: Option, exiting: bool, fade: f32, size: Vec2) -> Self { - LayerHost { overlay, save, exiting, fade, size, dismissed: None } + fn new(overlay: Overlay, props: &ContainerProps, exiting: bool, fade: f32, size: Vec2) -> Self { + LayerHost { overlay, save: props.save.clone(), owner: props.owner, exiting, fade, size, dismissed: None } } } @@ -251,7 +251,17 @@ impl Host for LayerHost { ShowAt::Rect(rect) => rect.size(), ShowAt::Flow => self.size, }); - let layer = Layer { id, owner: id, overlay: &overlay, anchor, env, exiting: self.exiting, fade: self.fade, size: Some(size), region }; + let layer = Layer { + id, + owner: self.owner.unwrap_or(id), + overlay: &overlay, + anchor, + env, + exiting: self.exiting, + fade: self.fade, + size: Some(size), + region, + }; let out = overlay::host(&ctx, &layer, &mut |ui| { // A layer the user sized with no position of its own shows at that size where the host placed it from its // anchor, within the room the host leaves it. diff --git a/crates/ui-contract/src/contract/overlay.rs b/crates/ui-contract/src/contract/overlay.rs index bddc2dd8f..7622c5596 100644 --- a/crates/ui-contract/src/contract/overlay.rs +++ b/crates/ui-contract/src/contract/overlay.rs @@ -19,8 +19,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(crate) use opener::opener_floor; +pub(crate) use host::{Layer, forget_layer, host, host_window}; +pub(crate) use opener::{LayerFloor, anchor_floor}; pub use opener::{Opener, Trigger}; pub use place::{Anchor, FloatPlace, OverlayFit, Place, Resize, ResizeEdges, SizeLimits, ViewportShare}; pub use placement::PopupRegion; diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index e314255ce..0ad9f320f 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -29,8 +29,8 @@ pub(crate) struct Layer<'a> { pub exiting: bool, /// The opacity the open and close motion gives. pub fade: f32, - /// The size the layer's content takes in this pass, when its container measured it before the layer shows; - /// a layer without one is placed by the size it took last pass. + /// The size the layer's content takes in this pass, as its container measured it before the layer shows. A + /// toast or a tooltip, which has none, is placed by the size it took last pass. pub size: Option, /// The screen rect the layer is contained in (see [`region_for`]): placement, scrim, clip and input blocking stop at it. pub region: Option, @@ -60,14 +60,6 @@ pub(super) struct LayerMemory { family_closed: bool, /// The layer's focus scope has run in a pass that showed it: the scope takes its initial focus once per appearance. scope_entered: bool, - /// The least width the layer's body took in the last pass: a content-sized collection in the layer is at least - /// this wide, so its rows fill the layer. - min_width: f32, -} - -/// The least width the body of the layer `id` takes, as of the last pass; 0 before the layer's first pass. -pub(crate) fn floor_width(ctx: &Context, id: Id) -> f32 { - MemoryStage::peek::(ctx, memory_slot(id)).min_width } pub(super) fn memory_slot(id: Id) -> Id { @@ -136,20 +128,25 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U let bounds = layer.region.map_or_else(|| areas::safe_rect(ctx), |region| region.intersect(areas::safe_rect(ctx))); let anchor = anchor_rect(ctx, layer, bounds, &mut memory); let area = area_id(layer.id); - // A container's layer is placed by the size its container measured for this pass, any other by the size it took last - // pass. The layout memory entry under `size_id` marks that read, so a change of the size asks for - // the discard pass that places the layer by its real size, under the rule every measured size follows: no ask when - // the size also changed in the previous frame. A popup is placed by its anchor and opens from its opener's queued - // request, which a discard pass would replay. + let min_width = overlay.least_width(anchor.width()); + // A container's layer is placed by the size its container measured for this pass, at least its least width; a + // toast or a tooltip by the size it took last pass. For a layer that owns its memory, the layout memory entry + // under `size_id` marks that read, so a change of the shown size asks for the discard pass that places the layer + // by it, under the rule every measured size follows: no ask when the size also changed in the previous frame. A + // popup's layer, whose memory its opener owns, asks none: it opens from its opener's queued request, which a + // discard pass would replay. let size_id = sub(layer.id, "layer_size"); let sized = layer.owner == layer.id; if sized { let _ = layout_memory::read(ctx, size_id); } - let size = layer.size.or_else(|| areas::layer_rect(ctx, area).map(|rect| rect.size())); + let size = match layer.size { + Some(size) => Some(egui::vec2(size.x.max(min_width), size.y)), + None => areas::layer_rect(ctx, area).map(|rect| rect.size()), + }; let placed = placement(ctx, layer, size, bounds, anchor); let tier = overlay::layer_tier(ctx, overlay.rank); - if sized && areas::layer_rect(ctx, area).is_none() { + if (sized || layer.size.is_some()) && areas::layer_rect(ctx, area).is_none() { areas::seed_area(ctx, areas::layer_of(tier, area), placed.at, placed.room); } // A modal's cover: it takes the presses below the modal while the modal is open, and paints the scrim if it has one. @@ -168,7 +165,6 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U }; let parent = overlay::drawing_parent(ctx); let scope = (overlay.focus != FocusPolicy::NONE).then_some(overlay.focus); - let min_width = overlay.least_width(anchor.width()); let spec = LayerSpec { id: area, tier, at: placed.at, interactable: overlay.rank != Rank::Tooltip && !layer.exiting }; // The opener of the layer's scope is the stop focused when the layer first appears: egui's modal gate takes focus // from the widgets below a modal layer by the first pass that shows the layer. A dropdown that keeps its anchor's key @@ -222,7 +218,6 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U entry.scoped = scope.is_some(); entry.absorbs = overlay.dismiss.outside == Outside::Absorb; overlay::record(ctx, entry); - memory.min_width = min_width.min(placed.room.x); let dismissed = if layer.exiting { None } else { dismissal(ctx, layer, shown.rect, anchor, scope, &mut memory) }; close_family(ctx, layer, layer.exiting || dismissed.is_some(), &mut memory); remember(layer, ctx, memory); diff --git a/crates/ui-contract/src/contract/overlay/opener.rs b/crates/ui-contract/src/contract/overlay/opener.rs index 179c98022..ffec66732 100644 --- a/crates/ui-contract/src/contract/overlay/opener.rs +++ b/crates/ui-contract/src/contract/overlay/opener.rs @@ -151,17 +151,33 @@ pub(crate) enum Opening { } /// The least width a layer asks of its content: its `min_width` rule applied to the width of the opener it hangs -/// from, 0 for a layer with no opener or an anchor that is not a rect. -pub(crate) fn opener_floor(overlay: &Overlay) -> f32 { - let Some(opener) = &overlay.opener else { return 0.0 }; - let width = match &opener.anchor { - Some(Anchor::Rect(rect)) => rect.width(), - Some(Anchor::Response) | None => opener.rect.width(), - Some(_) => return 0.0, +/// from, else of the rect its place hangs it from; 0 for an anchor that is not a rect. +pub(crate) fn anchor_floor(overlay: &Overlay) -> f32 { + let width = match &overlay.opener { + Some(opener) => match &opener.anchor { + Some(Anchor::Rect(rect)) => rect.width(), + Some(Anchor::Response) | None => opener.rect.width(), + Some(_) => return 0.0, + }, + None => match &overlay.place { + Place::Anchored { anchor: Anchor::Rect(rect), .. } => rect.width(), + _ => return 0.0, + }, }; overlay.least_width(width) } +/// The least width of the layer the content runs in ([`anchor_floor`]), which a layer's container gives its regions: +/// a content-sized collection in the layer is at least this wide, so its rows fill the layer. 0 outside a layer. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub(crate) struct LayerFloor(pub(crate) f32); + +impl crate::runtime::env::EnvValue for LayerFloor { + fn root() -> Self { + LayerFloor(0.0) + } +} + /// The anchor a layer opened by `opener` hangs from: the opener's own, else the layer's `Pointer` or `Corner` /// place, else the pointer for a context request a secondary release made, else the rect a key's context request /// named, else the opener's rect. diff --git a/crates/ui-contract/src/contract/overlay/ui.rs b/crates/ui-contract/src/contract/overlay/ui.rs index 3618fbf7b..4468c1975 100644 --- a/crates/ui-contract/src/contract/overlay/ui.rs +++ b/crates/ui-contract/src/contract/overlay/ui.rs @@ -1,23 +1,23 @@ //! `OverlayUi`: what the `overlay` hook gets for each anchored popup. -use super::host::{Layer, forget_layer, host}; -use super::place::Anchor; -use super::placement::{PopupRegion, region_for, resolve_anchor}; +use super::host::forget_layer; +use super::place::{Anchor, Place}; +use super::placement::PopupRegion; use super::popups::{PopupHandle, PopupLayer, with_popups}; use super::props::Overlay; -use crate::contract::body::pad_of; -use crate::contract::{BodyCx, Inner, Internal, Mount, POPUP_PAD, Props as _, Resolved, ShowContainer}; -use crate::primitives::SurfaceSlot; +use crate::contract::{ + BodyCx, Common, Component, Container, ContainerProps, Cx, Inner, Internal, Mount, NoValue, Outcome, POPUP_PAD, Props as _, Resolved, Role, + ShowContainer, +}; use crate::runtime::access::PopupShown; -use crate::runtime::egui::alloc; use crate::runtime::egui::areas::{self}; use crate::runtime::env::Env; -use crate::runtime::memory::MemoryStage; +use crate::runtime::memory::sub; use crate::runtime::motion::{self}; use crate::runtime::overlay::{self, CloseReason}; -use crate::style::SurfaceKind; -use craft_behavior::PartRef; -use egui::{Context, Id, IdMap, Rect, Ui, UiBuilder}; +use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; +use craft_behavior::{Metrics, PartRef, Sense}; +use egui::{Context, Id, IdMap, Rect, Response, Ui}; /// What the `overlay` hook gets: one call per anchored popup of this component, built at step 10 with the open state /// of every popup `cx.popup` registered this pass. @@ -89,42 +89,32 @@ impl<'a> OverlayUi<'a> { /// Shows the popup while its handle is open, on a popup surface, inside `POPUP_PAD`; the content is a body closure. /// `None` while closed. - pub fn show(&mut self, popup: PopupHandle, overlay: Overlay, mut body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R) -> Option { + /// + /// The popup is a layer container sized from its content: its body is measured in this pass before the layer is + /// placed, so a popup whose content changes is placed by its new size in the pass that shows it. A child that fills + /// the width takes the width the popup settles on, not the window's. The popup's memory belongs to the component + /// that shows it (or to the opener its overlay names), so what its body keeps lasts while that component shows. + pub fn show(&mut self, popup: PopupHandle, overlay: Overlay, body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R) -> Option { let Some(open) = self.open.get(&popup.id).copied() else { debug_assert!(false, "popup {:?} is shown but was not registered with `cx.popup` this pass", popup.name); return None; }; - let ctx = self.ctx.clone(); - with_popups(&ctx, |popups| popups.anchors.push((self.owner, PopupShown { id: popup.id, kind: overlay.popup, open }))); let owner = overlay.opener.as_ref().map_or(self.owner, |opener| opener.id); - let presence = motion::layer_presence(&ctx, popup.id, owner, self.env.motion, open, overlay.motion); - if !presence.visible { - forget_layer(&ctx, popup.id, owner); + // A closed popup that no longer fades runs nothing: no container run for each closed popup of every pass. + if !open && !motion::layer_fading(self.ctx, sub(popup.id, "presence")) { + with_popups(self.ctx, |popups| popups.anchors.push((self.owner, PopupShown { id: popup.id, kind: overlay.popup, open }))); + forget_layer(self.ctx, popup.id, owner); return None; } - let anchor = resolve_anchor(&ctx, popup.id, &overlay, self.anchor, self.to_screen); - let region = region_for(&self.env, &overlay); - let layer = Layer { id: popup.id, owner, overlay: &overlay, anchor, env: &self.env, exiting: !open, fade: presence.t, size: None, region }; - let resolved = self.resolved; - let mut result = None; - let mut requested = false; - let out = host(&ctx, &layer, &mut |ui| { - let surface = SurfaceSlot::reserve(ui, true); - let mut stage = if ui.is_sizing_pass() { MemoryStage::open_peek(&ctx, popup.id) } else { MemoryStage::open_owned(&ctx, popup.id, owner) }; - let env = Env::of(ui); - let pad = pad_of(&env.tokens, POPUP_PAD); - let mut inner = alloc::child(ui, UiBuilder::new().max_rect(ui.available_rect_before_wrap().shrink(pad))); - inner.set_min_width((ui.min_rect().width() - 2.0 * pad).max(0.0)); - let mut shown_open = open; - let mut bcx = BodyCx::new(popup.id, env.view(), resolved, inner.max_rect(), &mut stage, &mut shown_open, None); - result = Some(body(&mut inner, &mut bcx)); - requested = bcx.close_requested().is_some(); - stage.write_back(); - alloc::advance_past(ui, inner.min_rect().expand(pad)); - surface.paint(&env.tokens, SurfaceKind::Popup, ui.min_rect(), env.direction); - }); - self.record_shown(popup, open, overlay.opener.is_none(), if requested { Some(CloseReason::Requested) } else { out.dismissed }); - result.filter(|_| open) + // A popup with no opener hangs from the owner's rect, which the container's layer reads as a rect anchor. + let overlay = match (&overlay.opener, &overlay.place) { + (None, Place::Anchored { anchor: Anchor::Response, .. }) => overlay.anchor(Anchor::Rect(self.anchor)), + _ => overlay, + }; + let label = self.resolved.label_value.clone(); + let props = ContainerProps::new().surface(SurfaceKind::Popup).pad(POPUP_PAD).mount(Mount::Layer(overlay)); + let container = PopupBody { common: Common::new(), props: ContainerProps { owner: Some(owner), ..props } }.label(label); + self.show_container(popup, container, body).and_then(|shown| shown.inner) } /// Shows `container` as the layer of `popup`, through the container pipeline: its body step with the move band @@ -180,3 +170,62 @@ impl<'a> OverlayUi<'a> { } } } + +/// The style of a popup body: its surface and padding come from the container. +#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)] +pub struct PopupBodyStyle { + #[style(metrics)] + metrics: Metrics, +} + +/// What a popup body reports: its response. +#[derive(Debug)] +pub struct PopupBodyOutput { + response: Response, +} + +impl Outcome for PopupBodyOutput { + type Value = NoValue; + + fn response(&self) -> &Response { + &self.response + } + + fn unshown(response: Response) -> Option { + Some(PopupBodyOutput { response }) + } +} + +/// The layer container [`OverlayUi::show`] shows a popup's body closure in: content-sized, on a popup surface inside +/// `POPUP_PAD`, its memory owned by the component that shows the popup. It adds no node of its own: the layer's node +/// is the popup's. +#[derive(crate::contract::Component)] +#[component(container, unregistered)] +pub struct PopupBody { + #[common] + common: Common, + #[container] + props: ContainerProps, +} + +impl Component for PopupBody { + const ROLE: Role = Role::None; + const PARTS: bool = false; + const STOPS: bool = false; + type Style = PopupBodyStyle; + type Output = PopupBodyOutput; + + fn style(_: &StyleCx<'_>, _: PopupBodyStyleKey) -> PopupBodyStyle { + PopupBodyStyle::default() + } + + fn sense(&self) -> Sense { + Sense::empty() + } + + fn finish(self, cx: Cx<'_, Self>) -> PopupBodyOutput { + PopupBodyOutput { response: cx.response.clone() } + } +} + +impl Container for PopupBody {} diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 9d0ee03d7..b2789b3c0 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -254,6 +254,12 @@ pub(crate) trait ShapeHooks { None } + /// The component whose memory holds this one's shown memory, when it is another: a popup body's memory belongs to + /// the component that shows the popup, so it lasts while that component shows, open or closed. + fn memory_owner(&self, _comp: &C) -> Option { + None + } + /// Step 7: the parts the shape declares itself (scroll parts, the item layout). fn parts(&mut self, _comp: &mut C, _rect: Rect, _pcx: &mut PartsCx<'_, C>) {} @@ -496,6 +502,7 @@ pub(crate) trait Erased { fn declares_parts(&self) -> bool; fn read(&mut self, stage: &mut MemoryStage, id: Id, env: &Env); fn instance_scope(&self) -> Option; + fn memory_owner(&self) -> Option; fn in_flow(&self) -> bool; /// Whether the shape fills the width of the room it is shown in (a body not sized from its content). fn fills_width(&self) -> bool; @@ -1309,7 +1316,7 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option Erased for Typed<'_, '_, C> { self.hooks.instance_scope(self.comp.get_ref()) } + fn memory_owner(&self) -> Option { + self.hooks.memory_owner(self.comp.get_ref()) + } + fn in_flow(&self) -> bool { self.hooks.in_flow(self.comp.get_ref()) } @@ -563,9 +567,15 @@ fn unshown(comp: &mut C, ui: &Ui) -> Option { if !sizing { steps::claim_id(ui, id, facts.name); } - if !sizing && crate::runtime::memory::owner_pass(ui.ctx(), id).is_some() { - crate::contract::overlay::forget_layer(ui.ctx(), id, id); - crate::runtime::memory::end_shown(ui.ctx(), id); + // A layer whose memory belongs to another component (`ContainerProps::owner`) forgets only its layer: the rest + // of its memory lasts while that component shows. + match props.owner { + Some(owner) if !sizing => crate::contract::overlay::forget_layer(ui.ctx(), id, owner), + None if !sizing && crate::runtime::memory::owner_pass(ui.ctx(), id).is_some() => { + crate::contract::overlay::forget_layer(ui.ctx(), id, id); + crate::runtime::memory::end_shown(ui.ctx(), id); + } + _ => {} } Some(output) }