From 4ed94f62c03a11f571c01bc2b415fd1fe192614c Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sat, 21 Nov 2026 21:26:41 +0700 Subject: [PATCH] chrome-w5: net change of 2 commits - chrome-w5: written from its sheet - chrome-w5: repair 1 --- .../ui-contract/src/contract/body/presses.rs | 52 +++++--- crates/ui-contract/src/contract/container.rs | 1 + crates/ui-contract/src/contract/run.rs | 21 +++- crates/ui-contract/src/layout/body.rs | 16 ++- crates/ui-contract/tests/it/body/mod.rs | 1 + crates/ui-contract/tests/it/body/surface.rs | 112 ++++++++++++++++++ crates/ui-macros/src/props.rs | 1 + crates/ui-widgets/src/display/text.rs | 20 +++- .../ui-widgets/src/layout_components/mod.rs | 10 +- 9 files changed, 197 insertions(+), 37 deletions(-) create mode 100644 crates/ui-contract/tests/it/body/surface.rs diff --git a/crates/ui-contract/src/contract/body/presses.rs b/crates/ui-contract/src/contract/body/presses.rs index 98296dceb..42e30a3ad 100644 --- a/crates/ui-contract/src/contract/body/presses.rs +++ b/crates/ui-contract/src/contract/body/presses.rs @@ -7,19 +7,19 @@ use crate::contract::{Container, Cx}; use crate::runtime::egui::areas; use crate::runtime::memory::sub; use crate::runtime::pointer; -use craft_behavior::{Cursor, CursorPriority, PartRef, PressState}; +use craft_behavior::{Cursor, CursorPriority, PartRef, PointerButton, PressState}; +use egui::Pos2; /// Whether the second press of a double click went down on the container's move band in this pass. #[derive(Clone, Default)] struct BandDouble(bool); -/// Whether the primary press of this pass went down on the container's own surface (`Sense::SURFACE`), and whether it -/// is the second of a double click. +/// Whether the primary press of this pass went down on the container's own surface (`Sense::SURFACE`), whether it +/// is the second of a double click, and whether this pass's release completes a click that began there. #[derive(Clone, Default)] struct SurfacePress { pressed: bool, double: bool, - #[allow(dead_code)] // P3(chrome-w5): ch.K32 clicked: bool, } @@ -59,7 +59,7 @@ impl BodyCx<'_> { /// than `PointerFrame::slop` and was released inside. A drag, a cancel, a release elsewhere and a measuring run /// give false. It takes no focus and is no Tab stop. pub fn surface_clicked(&self) -> bool { - todo!("ch.K32") + !self.memory.is_peek() && self.memory.recall::(sub(self.id, "surface_press")).clicked } /// Requests `cursor` at `priority` for this pass, in the cursor service the parts' and components' requests go @@ -77,24 +77,46 @@ impl BodyHooks<'_> { /// Records for the container's hooks whether this pass's primary press went down on its own surface /// (`BodyCx::surface_pressed`): no shape of last pass under the press takes it, and the press lies in the /// container's rect on its layer; and whether that press is the second of a double click - /// (`BodyCx::surface_double_pressed`), read from the same press's click count. + /// (`BodyCx::surface_double_pressed`), read from the same press's click count; and whether this pass's primary + /// release ends a press that went down there (`BodyCx::surface_clicked`). The press origin waits in the press slot + /// until the release; Escape and a blur clear it, and so does any pass with no button held. pub(super) fn surface_press(&self, sensed: bool, cx: &mut Cx<'_, C>) { let ctx = cx.core.ctx.clone(); + let pass = pointer::pass_pointer(&ctx); + let (layer, rect) = (cx.core.response.layer_id, cx.core.rect); + // Whether `pos` lies in the container's rect, on its layer. + let inside = |pos: Pos2| { + let at = areas::layer_map(&ctx, layer).map_or(pos, |map| map.to_layer(pos)); + areas::layer_at(&ctx, pos) == Some(layer) && rect.contains(at) + }; let surface = if sensed { - let pass = pointer::pass_pointer(&ctx); - pass.frame.as_ref().and_then(|frame| frame.press).filter(|press| press.button == craft_behavior::PointerButton::Primary).filter(|press| { - let layer = cx.core.response.layer_id; - let at = areas::layer_map(&ctx, layer).map_or(press.origin, |map| map.to_layer(press.origin)); - areas::layer_at(&ctx, press.origin) == Some(layer) - && cx.core.rect.contains(at) - && pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none() - }) + pass.frame + .as_ref() + .and_then(|frame| frame.press) + .filter(|press| press.button == PointerButton::Primary) + .filter(|press| inside(press.origin) && pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none()) } else { None }; - let shown = cx.core.memory.shown::(sub(sub(cx.core.id, "body"), "surface_press")); + let state_id = sub(sub(cx.core.id, "body"), "surface_press"); + let mut clicked = false; + if sensed { + let origin = cx.core.memory.press::(state_id); + if let Some(press) = surface { + *origin = Some(press.origin); + } + match pass.frame.as_ref() { + Some(frame) if frame.release == Some(PointerButton::Primary) => { + clicked = origin.take().is_some_and(|from| frame.pos.is_some_and(|pos| inside(pos) && (pos - from).length() < frame.slop)); + } + _ if surface.is_none() && (!pass.primary_down || pointer::cancel_of_pass(&ctx).is_some()) => *origin = None, + _ => {} + } + } + let shown = cx.core.memory.shown::(state_id); shown.pressed = surface.is_some(); shown.double = surface.is_some_and(|press| press.clicks >= 2); + shown.clicked = clicked; } /// Records for the container's header hooks whether the second press of a double click went down on the move diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 1de376030..0edb687ed 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -812,6 +812,7 @@ impl ContainerProps { && self.sizing == other.sizing && self.content_in_bar == other.content_in_bar && self.room_height == other.room_height + && self.fit_rank == other.fit_rank && self.layout == other.layout && self.header == other.header && self.scroll == other.scroll diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 2be5b841f..cd8fe1a4e 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -259,6 +259,11 @@ pub(crate) trait ShapeHooks { None } + /// The rank by which the component leaves a shrinking row whole (`ContainerProps::fit_rank`). + fn fit_rank(&self, _comp: &C) -> Option { + 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 { @@ -508,6 +513,11 @@ pub(crate) trait Erased { fn read(&mut self, stage: &mut MemoryStage, id: Id, env: &Env); fn instance_scope(&self) -> Option; fn theme_scope(&self) -> Option; + /// The rank by which the component leaves a shrinking row whole. + fn fit_rank(&self) -> Option { + // PLAN: `typed.rs` (`Erased for Typed`) and `body/hooks.rs` (`ShapeHooks::fit_rank` reading `ContainerProps::fit_rank`) are not in chrome-w5's Files table; their owner must forward the rank, or no body reports one. + None + } 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). @@ -1560,11 +1570,12 @@ fn room_step(t: &mut dyn Erased, ui: &Ui, at: ShowAt, begun: &Begun) -> Room { let fill_body = shape == ShapeKind::Body && in_flow && t.in_flow() && t.fills_width() && crate::layout::flow_room(ui).x.is_finite(); let shrink = env.shrink.filter(|_| in_flow && (shape == ShapeKind::Leaf || (shape == ShapeKind::Body && t.in_flow() && !fill_body))); let given = match shrink { - Some(crate::layout::Shrink::Apply(row)) if shape == ShapeKind::Leaf => crate::layout::shrink_cap(ctx, row, id), + Some(crate::layout::Shrink::Apply(row)) => crate::layout::shrink_cap(ctx, row, id), _ => None, }; let dropped = given.is_some_and(|given| given.dropped); - let cap = given.map(|given| given.width); + // A body takes its width from its own size step; the cap only tells it whether it was dropped. + let cap = given.filter(|_| shape == ShapeKind::Leaf).map(|given| given.width); let room = match cap { Some(cap) => Some(room.map_or(cap, |room| room.min(cap))), None => room, @@ -1694,9 +1705,9 @@ fn measure_step( let ideal = steps::measure(style, &input, content, Vec2::ZERO, resolved).x; let whole = steps::whole(style, content, Vec2::ZERO, resolved); let least = ask(&mut *t, Proposal::Min, None, Vec2::ZERO).0.x; - crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), t.cut_first(), None); // P3(chrome-w5): ch.K30 + crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), t.cut_first(), None); } - Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false, None), // P3(chrome-w5): ch.K30 + Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false, t.fit_rank()), Some(crate::layout::Shrink::Least(row)) if shape == ShapeKind::Body => crate::layout::report_least(ctx, row, id, size.x), _ => {} } @@ -1704,7 +1715,7 @@ fn measure_step( // the units narrow to leave it, and it takes what they leave. if let Some(crate::layout::Shrink::Count(row)) = env.shrink.filter(|_| fill_body) { let content = measured.x + t.overflow(); - crate::layout::report_shrink(ctx, row, id, size.x.min(content), content, Some((content, content)), false, None); // P3(chrome-w5): ch.K30 + crate::layout::report_shrink(ctx, row, id, size.x.min(content), content, Some((content, content)), false, t.fit_rank()); } // A component shown at a rect fills it: the rect is its size, and a leaf that measures nothing (a content // painter) still shows there. diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 6d1e55fc9..f5327dc87 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -449,6 +449,9 @@ fn shrink_in_turns(units: &[ShrinkUnit], whole: &[f32], first: &[bool], fixed: f /// to be cut (`first`) drop out from the end, each with one `gap`, until the rest fits at their least. A unit that /// drops gets no width and takes no place, so nothing overflows into a neighbour; controls never drop. A row that /// scrolls keeps every unit and scrolls instead. +/// +/// Before those drops, the units that carry a rank (`ranks`) leave whole, scrolling row or not: the lowest rank first, +/// the last of a rank first, until the kept units fit at their ideal widths. A unit with no rank never leaves by rank. fn shrink_or_drop( units: &[ShrinkUnit], whole: &[f32], @@ -459,12 +462,19 @@ fn shrink_or_drop( order: ShrinkOrder, drops: bool, ) -> Vec { - let _ = ranks; // P3(chrome-w5): ch.K30 let mut kept = vec![true; units.len()]; let mut fixed = fixed; // Half a point: less than a pixel shows at any scale the box runs. - let fits = - |kept: &[bool], fixed: f32| fixed + units.iter().zip(kept).filter(|(_, kept)| **kept).map(|(unit, _)| unit.least).sum::() - room < 0.5; + let fits_at = |kept: &[bool], fixed: f32, width: fn(&ShrinkUnit) -> f32| { + fixed + units.iter().zip(kept).filter(|(_, kept)| **kept).map(|(unit, _)| width(unit)).sum::() - room < 0.5 + }; + let fits = |kept: &[bool], fixed: f32| fits_at(kept, fixed, |unit| unit.least); + while !fits_at(&kept, fixed, |unit| unit.ideal) { + let Some(rank) = (0..units.len()).filter(|at| kept[*at]).filter_map(|at| ranks[at]).min() else { break }; + let Some(at) = (0..units.len()).rev().find(|at| kept[*at] && ranks[*at] == Some(rank)) else { break }; + kept[at] = false; + fixed -= gap; + } while drops && !fits(&kept, fixed) { let Some(at) = (0..units.len()).rev().find(|at| kept[*at] && first[*at]) else { break }; kept[at] = false; diff --git a/crates/ui-contract/tests/it/body/mod.rs b/crates/ui-contract/tests/it/body/mod.rs index ef7344a76..2a39d2c2b 100644 --- a/crates/ui-contract/tests/it/body/mod.rs +++ b/crates/ui-contract/tests/it/body/mod.rs @@ -3,3 +3,4 @@ mod mounts; mod nesting; mod placement; +mod surface; diff --git a/crates/ui-contract/tests/it/body/surface.rs b/crates/ui-contract/tests/it/body/surface.rs new file mode 100644 index 000000000..527322348 --- /dev/null +++ b/crates/ui-contract/tests/it/body/surface.rs @@ -0,0 +1,112 @@ +//! The click on a container's own surface: it reports in the pass of its primary release. + +use craft_behavior::Sense; +use craft_ui::contract::Show as _; +use craft_ui::testing::input::key_press; +use craft_ui::testing::layout::Block; +use craft_ui::testing::{Frames, pointer, trial}; +use craft_ui_test_kit::contract::{Panel, Probe, Seen}; +use egui::{Key, Modifiers, Pos2, Rect, Ui, Vec2}; +use std::cell::{Cell, RefCell}; + +const PROBE: Vec2 = Vec2::new(40.0, 40.0); +const BLOCK: Vec2 = Vec2::new(100.0, 60.0); + +/// What the shown container leaves for the trial to read. +#[derive(Default)] +struct Shown { + rect: Cell, + clicked: Cell, + seen: RefCell, +} + +impl Shown { + /// A panel that senses its surface, with a child that takes clicks above a block that senses nothing. + fn show(&self, ui: &mut Ui) { + let out = Panel::new().sense(Sense::SURFACE).show(ui, |ui, cx| { + self.clicked.set(self.clicked.get() | cx.surface_clicked()); + Probe::new(&self.seen).size(PROBE).sense(Sense::CLICK).show(ui); + Block::new(BLOCK).show(ui); + }); + self.rect.set(out.response.rect); + } +} + +struct Rig { + frames: Frames, + shown: Shown, +} + +impl Rig { + fn new() -> Self { + let mut rig = Rig { frames: Frames::new(), shown: Shown::default() }; + rig.frames.idle(3, |ui| rig.shown.show(ui)); + rig + } + + /// Runs one frame with `events`; whether the container reported a click in it. + fn frame(&mut self, events: Vec) -> bool { + self.shown.clicked.set(false); + self.frames.frame(events, |ui| self.shown.show(ui)); + self.shown.clicked.get() + } + + /// A point on the block, which senses nothing: the container's own surface. + fn surface(&self) -> Pos2 { + self.shown.rect.get().min + Vec2::new(20.0, PROBE.y + 30.0) + } + + /// Moves to `at` and presses there; whether the press pass reported a click. + fn press(&mut self, at: Pos2) -> bool { + self.frame(vec![pointer::moved(at)]); + self.frame(vec![pointer::press(at)]) + } +} + +#[trial] +fn a_surface_click_reports_on_release_and_a_drag_a_cancel_or_a_release_elsewhere_report_none() { + // A click reports in the release pass, not the press pass, and a click again reports again. + let mut rig = Rig::new(); + let at = rig.surface(); + for _ in 0..2 { + assert!(!rig.press(at), "the press pass reports no click"); + assert!(rig.frame(vec![pointer::release(at)]), "the release pass reports the click"); + assert!(!rig.frame(Vec::new()), "the next pass reports none"); + rig.frames.advance(1.0); + } + + // A drag past the slop reports none. + let mut rig = Rig::new(); + let at = rig.surface(); + rig.press(at); + let far = at + Vec2::new(40.0, 0.0); + rig.frame(vec![pointer::moved(far)]); + assert!(!rig.frame(vec![pointer::release(far)]), "a drag is no click"); + + // Escape cancels the press. + let mut rig = Rig::new(); + let at = rig.surface(); + rig.press(at); + rig.frame(vec![key_press(Key::Escape, Modifiers::NONE)]); + assert!(!rig.frame(vec![pointer::release(at)]), "a cancelled press is no click"); + + // A blur cancels it too. + let mut rig = Rig::new(); + let at = rig.surface(); + rig.press(at); + rig.frame(vec![pointer::window_blurred()]); + assert!(!rig.frame(vec![pointer::release(at)]), "a blurred press is no click"); + + // A release outside the rect, within the slop of the press, is no click. + let mut rig = Rig::new(); + let corner = rig.shown.rect.get().max - Vec2::splat(0.5); + rig.press(corner); + let outside = corner + Vec2::splat(1.5); + assert!(!rig.frame(vec![pointer::release(outside)]), "a release outside is no click"); + + // A press a child takes is no surface click. + let mut rig = Rig::new(); + let on_child = rig.shown.rect.get().min + Vec2::splat(20.0); + rig.press(on_child); + assert!(!rig.frame(vec![pointer::release(on_child)]), "a press the child took is no surface click"); +} diff --git a/crates/ui-macros/src/props.rs b/crates/ui-macros/src/props.rs index 158382be1..7be7f4637 100644 --- a/crates/ui-macros/src/props.rs +++ b/crates/ui-macros/src/props.rs @@ -222,6 +222,7 @@ const CONTAINER: &[Forwarded] = &[ ("end", "$ui::contract::EndRegion", "The region at the line's end edge, outside the body scroll."), ("position", "$ui::contract::SetPosition", "The component's place in its set, for assistive technology."), ("collection_body", "", "The body holds one collection that owns the scroll: a layer's own body scroll is off."), + ("fit_rank", "u8", "A member of a shrinking row that leaves it whole, lowest rank first, when the row is short."), ]; /// The builders a `#[container(a, b)]` field names; a bare `#[collection]` or `#[container]` forwards them all. diff --git a/crates/ui-widgets/src/display/text.rs b/crates/ui-widgets/src/display/text.rs index 60c124ab4..316d13309 100644 --- a/crates/ui-widgets/src/display/text.rs +++ b/crates/ui-widgets/src/display/text.rs @@ -76,6 +76,8 @@ pub struct Text { elide: Elide, /// The styled spans that replace the label's text; empty for plain text. spans: Vec>, + /// The text whose width stands for the label's ideal and least width. + sample: Option