From a0b445d944168151f9dd71daef19f20e9605a87f Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Wed, 18 Nov 2026 18:18:25 +0000 Subject: [PATCH] dialogs-w1: net change of 3 commits - dialogs-w1: finished by an agent - dialogs-w1: finished by an agent - dialogs-w1: review fixes (review-1) --- crates/ui-contract/src/contract/body/hooks.rs | 19 +- crates/ui-contract/src/contract/container.rs | 3 +- .../ui-contract/src/contract/overlay/host.rs | 113 ++++++- crates/ui-panels/src/dialog/held.rs | 2 +- crates/ui-panels/src/dialog/host.rs | 20 +- crates/ui-panels/src/dialog/modal.rs | 30 +- crates/ui-panels/src/dialog/stories.rs | 27 ++ crates/ui-panels/tests/it/dialogs.rs | 311 +++++++++++++++++- migration/api-map/dialog-shells.toml | 2 +- 9 files changed, 502 insertions(+), 25 deletions(-) diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 3c562dd3b3..7587ba2e5a 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -880,6 +880,9 @@ impl ShapeHooks for BodyHooks<'_> { let (layout, body) = (&props.layout, &mut *self.body); // The room a scrolling body's bars take under or beside its content this run: no part of the line. let mut bars_inset = Vec2::ZERO; + // The body-end hook runs in the body's own column below its layout and scroll region, outside the shared label + // columns, so it lies under the body and above the footer in every footer place: beside an `EndTop` footer + // column too. let mut run = |parent: &mut Ui, at: Rect, bcx: &mut BodyCx<'_>| -> Option { let mut body_ui = alloc::child(parent, { let builder = UiBuilder::new().max_rect(at).layout(child::column_layout(rtl)).scope_id(body_id); @@ -938,7 +941,9 @@ impl ShapeHooks for BodyHooks<'_> { let view_env = body_env.derive_local(|scoped| scoped.lazy = Some(view)); let region_env = Env::install_for(®ion_ui, view_env.clone()); layout::run_body_measured(&mut region_ui, &view_env, layout_id, layout, gap, line, |ui| { - shared_labels(ui, id, columns, room.x, self.settle.clone(), &mut labels, |ui| body(ui, bcx)) + shared_labels(ui, id, columns, room.x, self.settle.clone(), &mut labels, |ui| { + body(ui, bcx); + }) }); Env::restore(&ctx, region_env); if lays_out { @@ -977,11 +982,21 @@ impl ShapeHooks for BodyHooks<'_> { // A row that places its block itself asks for its children's width, not the room it spans. None => { let (_, natural) = layout::run_body_measured(&mut body_ui, &body_env, layout_id, layout, gap, line, |ui| { - shared_labels(ui, id, columns, room.x, self.settle.clone(), &mut labels, |ui| body(ui, bcx)) + shared_labels(ui, id, columns, room.x, self.settle.clone(), &mut labels, |ui| { + body(ui, bcx); + }) }); natural.map(|width| Vec2::new(width, body_ui.min_rect().height())) } }; + // A scrolling body's region allocates nothing in `body_ui`, so the hook starts below the part it shows. + if scroll.is_some() { + body_ui.allocate_space(Vec2::new(0.0, took.map_or(0.0, |size| size.y))); + } + let before = body_ui.min_rect().height(); + comp.body_end(&mut body_ui, bcx); + let ended = body_ui.min_rect().height() - before; + let took = took.map(|size| Vec2::new(size.x, size.y + ended)); Env::restore(&ctx, installed); let took = took.unwrap_or_else(|| body_ui.min_rect().size()); // A content-sized column of rows takes, in a measuring run, the width they take at its group's widest diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 7dc25b8928..a0559703ad 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -414,7 +414,8 @@ impl FooterRegion { } } -/// Where a footer region sits. +/// Where a footer region sits. In every place, `Container::body_end` fills the body's column under its content, so +/// a notice it shows lies under the body and above the footer, or beside a footer in the end region. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[non_exhaustive] pub enum FooterPlace { diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index 0ad9f320f1..15e3d599fe 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -11,8 +11,10 @@ use crate::runtime::env::Env; use crate::runtime::focus::{self, FocusPolicy}; use crate::runtime::layout_memory; use crate::runtime::memory::{MemoryStage, sub}; +use crate::runtime::motion::{self, Driver}; use crate::runtime::overlay::{self, Backdrop, CloseReason, OpenEntry, Rank}; -use egui::{Context, Id, Pos2, Rect, Ui}; +use craft_behavior::{MotionKind, Playback}; +use egui::{Context, Id, Pos2, Rect, Ui, Vec2}; /// What the host reads for one layer. pub(crate) struct Layer<'a> { @@ -108,7 +110,8 @@ fn area_id(id: Id) -> Id { pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut Ui)) -> LayerOut { // A layer that appears starts with nothing it remembered while shown before, also when it opens again during its // exit fade or its host never saw it hide: the focus that was inside it does not close it as `focus_left`. - let mut memory = if overlay::appears(ctx, layer.id, layer.exiting) { LayerMemory::default() } else { recall(layer, ctx) }; + let appears = overlay::appears(ctx, layer.id, layer.exiting); + let mut memory = if appears { LayerMemory::default() } else { recall(layer, ctx) }; // A right-click on a context opener while its layer is open hangs the layer at the new pointer. let pointed = layer .overlay @@ -144,7 +147,11 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U 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 mut placed = placement(ctx, layer, size, bounds, anchor); + let nudge = Nudge::of(ctx, layer, appears); + if let Some(nudge) = &nudge { + placed.at += nudge.offset(); + } let tier = overlay::layer_tier(ctx, overlay.rank); 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); @@ -177,6 +184,11 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U let recorded = overlay.opener.as_ref().and_then(|first| focus::stop_rec(ctx, (first.id, None))).map(|rec| rec.stop.key()); owned.map(|rec| rec.stop.key()).or(pressed).or(recorded).or_else(|| focus::focused_stop(ctx)) }); + // A modal closes the popups beneath it in the pass it first shows; `appears` is false in a discard pass that repeats + // the frame, so the close is queued once. A popup opened above it later is not touched. + if overlay.rank.is_modal() && appears { + overlay::close_popups_outside(ctx, &[layer.id]); + } overlay::begin_layer(ctx, layer.id, tier, layer.exiting, placed.side); let shown = areas::show_layer(ctx, &spec, |ui| { if let Some(region) = layer.region { @@ -219,11 +231,106 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U entry.absorbs = overlay.dismiss.outside == Outside::Absorb; overlay::record(ctx, entry); let dismissed = if layer.exiting { None } else { dismissal(ctx, layer, shown.rect, anchor, scope, &mut memory) }; + if let Some(nudge) = &nudge { + // `dismissal` above records the press beside the layer (`dismissal.rs`, `note_pressed_outside`), so the nudge + // starts after it and moves the layer from the next pass on. + nudge.start_on_press(ctx, layer); + nudge.flash(ctx, &shown, layer.env); + } close_family(ctx, layer, layer.exiting || dismissed.is_some(), &mut memory); remember(layer, ctx, memory); LayerOut { dismissed } } +/// The motion slot of a layer's nudge, under the layer's id. +// PLAN: the decision names a `NUDGE_SPEC`; `MotionSpec::new` (`runtime/motion.rs:39`) is a private `const fn` of a file outside this set, so +// the nudge runs on `spec::OPEN_FADE` (a fade, which reduced motion still runs) over the open token's duration. +const NUDGE: &str = "nudge"; + +/// The swings of a nudge's shake either way, dying away. +const NUDGE_SWINGS: f32 = 3.0; + +/// The edge flashes of a nudge under reduced motion. +const NUDGE_FLASHES: f32 = 2.0; + +/// A press beside a modal layer that stays open (`Outside::Keep`) nudges it, so the user sees why nothing behind it +/// reacts: a short damped shake, or two accent edge flashes when the motion policy snaps travel. `left` runs from 1 at +/// the press to 0 at rest on the motion clock, in the layer's motion memory, which goes with the layer; a layer that +/// appears starts at rest. With motion off the fade the nudge steps by snaps too, so nothing shows. +struct Nudge { + /// How much of the nudge is left: 1 at the press, 0 at rest. + left: f32, + /// The layer shakes; otherwise its edge flashes. + shakes: bool, + /// The widest swing of the shake: the gap between neighbouring controls. + amplitude: f32, +} + +impl Nudge { + /// The nudge of `layer` in this pass, stepped on the pass clock; `None` for a layer that never nudges. A second + /// pass of one frame reads the same clock, so it repeats the first pass's value. + fn of(ctx: &Context, layer: &Layer<'_>, appears: bool) -> Option { + if !layer.overlay.rank.is_modal() || layer.overlay.dismiss.outside != Outside::Keep || layer.exiting { + return None; + } + let policy = layer.env.motion; + let left = Nudge::driven(ctx, layer, |driver| { + if appears { + driver.jump(NUDGE, 0.0_f32); + } + driver.anim(NUDGE, 0.0_f32, motion::spec::OPEN_FADE) + }); + let shakes = policy.mode.playback(MotionKind::Travel) == Playback::Run; + Some(Nudge { left, shakes, amplitude: layer.env.tokens.space.gap.item }) + } + + /// Runs `f` with the motion driver of `layer`'s nudge, in a stage its owner keeps. + fn driven(ctx: &Context, layer: &Layer<'_>, f: impl FnOnce(&Driver<'_>) -> R) -> R { + let stage = MemoryStage::open_owned(ctx, sub(layer.id, NUDGE), layer.owner); + let result = f(&Driver { stage: &stage, base: layer.id, policy: layer.env.motion }); + stage.write_back(); + result + } + + /// How far through the nudge it is, 0 at the press to 1 at rest. + fn through(&self) -> f32 { + 1.0 - self.left.clamp(0.0, 1.0) + } + + /// The layer's sideways offset: the shake's swing, none at rest and none when the edge flashes instead. + fn offset(&self) -> Vec2 { + if !self.shakes || self.left <= 0.0 { + return Vec2::ZERO; + } + let swing = (self.through() * NUDGE_SWINGS * std::f32::consts::TAU).sin() * self.left; + Vec2::new(self.amplitude * swing, 0.0) + } + + /// Starts the nudge when a press landed beside the layer in this pass and none runs; a press while one runs leaves + /// it alone. A later pass of the frame gets no press (`pressed_outside` is per pass), so it starts nothing. + fn start_on_press(&self, ctx: &Context, layer: &Layer<'_>) { + if self.left > 0.0 || !overlay::pressed_outside(ctx, layer.id) { + return; + } + Nudge::driven(ctx, layer, |driver| { + driver.jump(NUDGE, 1.0_f32); + // Aims it at rest from this pass's clock, so it runs and asks for the passes that step it. + driver.anim(NUDGE, 0.0_f32, motion::spec::OPEN_FADE); + }); + } + + /// Paints the edge flash on the layer's rect while the nudge runs without the shake: on in the first and third + /// quarters, so it flashes twice. + fn flash(&self, ctx: &Context, shown: &areas::LayerShown, env: &Env) { + if self.shakes || self.left <= 0.0 || (self.through() * NUDGE_FLASHES * 2.0).floor() % 2.0 != 0.0 { + return; + } + let stroke = egui::Stroke::new(env.tokens.stroke.medium, crate::style::rgba_color(env.tokens.color.accent.solid)); + let radius = egui::CornerRadius::from(env.tokens.shape.radius.window); + ctx.layer_painter(shown.layer).rect_stroke(shown.rect, radius, stroke, egui::StrokeKind::Inside); + } +} + /// The layer-stack side of an OS-window mount: records the window as an entry of its viewport's stack (rank, key /// class, scope; no egui layer) and decides its dismissal after the body ran. `rect` is the window's inner rect. pub(crate) fn host_window(ctx: &Context, layer: &Layer<'_>, rect: Rect) -> LayerOut { diff --git a/crates/ui-panels/src/dialog/held.rs b/crates/ui-panels/src/dialog/held.rs index f30f91db52..6aed222003 100644 --- a/crates/ui-panels/src/dialog/held.rs +++ b/crates/ui-panels/src/dialog/held.rs @@ -21,7 +21,7 @@ pub enum HeldOk { /// hands the answer to the app once ([`take`](HeldAnswer::take)), and closes with [`finish`](HeldAnswer::finish) once /// the app accepted it; a refusal keeps the refused answer beside its reason, so the dialog can show the reason until /// its values change. -#[derive(Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct HeldAnswer { filed: Option, /// `take` has not handed out `filed` yet. diff --git a/crates/ui-panels/src/dialog/host.rs b/crates/ui-panels/src/dialog/host.rs index ce4d47cca6..051dd931e1 100644 --- a/crates/ui-panels/src/dialog/host.rs +++ b/crates/ui-panels/src/dialog/host.rs @@ -212,7 +212,6 @@ impl std::fmt::Debug for ChildDialog { /// Type-erased [`StackDialog`] the host stores. trait AnyStackDialog: Send { fn id(&self) -> DialogId; - #[allow(dead_code)] // P3(dialogs-w1): dg.F31 fn modeless(&self) -> bool; fn result_type(&self) -> TypeId; fn key(&self) -> Option; @@ -224,6 +223,8 @@ trait AnyStackDialog: Send { fn json_fields(&self) -> Option; fn set_json_field(&mut self, ctx: &Context, field: &str, value: Value) -> Result<(), String>; fn show_erased(&mut self, ui: &mut Ui, stack: &DialogStack) -> Option>; + /// [`StackDialog::take_child`], unwrapped. + fn take_child_erased(&mut self) -> Option>; /// `DialogResult::::Cancel`, boxed, for a dialog closed from outside. fn cancelled(&self) -> Box; /// [`StackDialog::cancelled`]. @@ -282,6 +283,10 @@ impl AnyStackDialog for D { self.show(ui, stack).map(|result| Box::new(result) as Box) } + fn take_child_erased(&mut self) -> Option> { + self.take_child().map(|ChildDialog(child)| child) + } + fn release(&mut self, ctx: &Context) { StackDialog::cancelled(self, ctx); } @@ -389,8 +394,9 @@ impl fmt::Debug for DialogHost { impl DialogHost { /// Whether its open stack holds a dialog that is not modeless; current state, so a dialog opened this pass counts. + /// A queued startup dialog is not shown yet, so it does not count. pub fn shows_modal(&self) -> bool { - todo!("dg.F3") + self.open.iter().any(|dialog| !dialog.modeless()) } /// An empty host with no open or queued dialogs. @@ -462,9 +468,13 @@ impl DialogHost { let Some(at) = self.position_by_id(id) else { continue }; let stack = &self.stack; let dialog = &mut self.open[at]; - let Some(result) = crate::runtime::overlay::stack_place(&ctx, place, id, || dialog.show_erased(ui, stack)) else { - // The dialog stays open: a child it asks for opens over it and shows from the next pass. - if let Some(ChildDialog(child)) = self.open[at].take_child_erased() { + let shown = crate::runtime::overlay::stack_place(&ctx, place, id, || dialog.show_erased(ui, stack)); + // The child is taken after every show, so a request is never left for a later pass; a dialog that just + // finished would close its child with it, so its request is dropped. + let child = dialog.take_child_erased(); + let Some(result) = shown else { + if let Some(child) = child { + // The child is not on this run's `ids`, so it shows from the next pass, as any open during the run. self.open_with_parent(child, Some(id)); } continue; diff --git a/crates/ui-panels/src/dialog/modal.rs b/crates/ui-panels/src/dialog/modal.rs index c9163897b7..c7af4e7db5 100644 --- a/crates/ui-panels/src/dialog/modal.rs +++ b/crates/ui-panels/src/dialog/modal.rs @@ -5,9 +5,10 @@ use super::frame::{self, DialogOutput, FOOTER, FooterCx, FooterFn, FooterHooks, use crate::contract::overlay::{FloatPlace, OverlayFit, Place}; use crate::contract::{ Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, DragBand, FooterPlace, FooterRegion, HeaderRegion, Mount, Overlay, Pad, - Props as _, Role, SaveKey, ShowContainer as _, + Props as _, Role, SaveKey, Show as _, ShowContainer as _, }; use crate::edit::outer::{self, OuterEnd}; +use crate::notices::{Notice, NoticeLayout}; use crate::runtime::access::Access; use crate::runtime::env::Label; use crate::runtime::focus::Initial; @@ -18,6 +19,7 @@ use crate::sections::{DialogRole, FooterButton}; use crate::style::{ComponentStyle, StyleCx}; use craft_behavior::{CancelRule, Metrics, Sense}; use craft_host::target::WindowState; +use craft_theme::variant::Tone; use egui::{Align2, Context, Id, Pos2, Rangef, Rect, Response, Ui, ViewportId}; /// The style of a dialog: the surface and padding come from the container. @@ -34,6 +36,8 @@ type OnPickFn<'a> = Box) + 'a>; type SettleFn<'a> = Box, &mut [FooterButton]) + 'a>; +type RefusalFn<'a> = Box Option