From 73701aca9d27928a5cb5919b9491aa2b1679f2bc Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 02:01:20 +0700 Subject: [PATCH] ui-contract: a run too large to show (past MAX_EXTENT) declares no parts and is not parted, so its parts read idle, as a canvas on a very wide window reads its background --- crates/ui-contract/src/contract/cx/context.rs | 6 +-- crates/ui-contract/src/contract/run.rs | 2 +- .../ui-contract/tests/it/contract/pointer.rs | 48 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/crates/ui-contract/src/contract/cx/context.rs b/crates/ui-contract/src/contract/cx/context.rs index c83fa0094..8845e93c3 100644 --- a/crates/ui-contract/src/contract/cx/context.rs +++ b/crates/ui-contract/src/contract/cx/context.rs @@ -101,9 +101,9 @@ pub struct CxCore<'a> { pub(crate) state: StateSet, /// True once step 12 wrote `state` in this pass. pub(crate) stated: bool, - /// True once the run reached step 7. A measuring run, a sizing pass of the parent or host `Ui`, and a host that - /// shows nothing (a layer in its parent's sizing pass) stop before it: their core is blank and every part reads - /// idle. + /// True once the run reached step 7 with something to show. A measuring run, a sizing pass of the parent or host + /// `Ui`, and a host that shows nothing (a layer in its parent's sizing pass) stop before it, and an inert run (a + /// size the guard refuses) declares no parts: their core is blank and every part reads idle. pub(crate) parted: bool, /// Read with `Styled::ink`, valid from step 12. pub(crate) ink: Ink, diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 346f236ce..b1cc16d13 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -960,7 +960,7 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e p.real = true; let inspected = p.trace.recording() && inspect::open_element(ui, id, facts.name); let e = &mut *p.p; - e.core().parted = true; + e.core().parted = !inert; // 7 parts p.trace.mark(Step::Parts); let flow = Flow::new(rect, rtl); diff --git a/crates/ui-contract/tests/it/contract/pointer.rs b/crates/ui-contract/tests/it/contract/pointer.rs index 1d5f358cd..d1961a9e1 100644 --- a/crates/ui-contract/tests/it/contract/pointer.rs +++ b/crates/ui-contract/tests/it/contract/pointer.rs @@ -47,3 +47,51 @@ fn canvas_tool_release_in_a_discarded_pass_reaches_the_tool_once() { assert_eq!(passes, 2, "the release frame ran twice"); assert_eq!(releases(&canvas.borrow()), 1, "the tool saw its release once: {:?}", canvas.borrow().press); } + +/// A leaf with one part across its rect that reads the part's press in `finish`, as a canvas reads its background. +#[derive(craft_ui::contract::Component)] +struct ReadsPart<'a> { + #[common] + common: craft_ui::contract::Common, + read: &'a Cell>, +} + +const READ_PART: craft_behavior::PartKey = craft_behavior::PartKey::new("background"); + +impl craft_ui::contract::Component for ReadsPart<'_> { + const ROLE: craft_ui::contract::Role = craft_ui::contract::Role::GROUP; + type Style = craft_ui_test_kit::contract::PlainStyle; + type Output = egui::Response; + + fn style(_: &craft_ui::style::StyleCx<'_>, _: ReadsPartStyleKey) -> Self::Style { + craft_ui_test_kit::contract::PlainStyle::default() + } + + fn sense(&self) -> Sense { + Sense::empty() + } + + fn parts(&mut self, rect: Rect, cx: &mut craft_ui::contract::PartsCx<'_, Self>) { + cx.part(READ_PART, rect).sense(Sense::CLICK).pointer_only("a test surface"); + } + + fn finish(self, cx: craft_ui::contract::Cx<'_, Self>) -> egui::Response { + use craft_ui::contract::Pointer as _; + self.read.set(Some(cx.part(READ_PART).press.phase)); + cx.response.clone() + } +} + +/// A component shown at a size past the extent limit shows nothing and declares no parts, so its parts read idle, +/// as in a run that stopped before the parts step (a canvas on a window wider than the limit reads its background). +/// Guards against such a run counting as parted and its part read failing. +#[trial] +fn a_part_read_in_a_run_too_large_to_show_is_idle() { + let mut frames = Frames::new(); + let read = Cell::new(None); + let wide = rect(0.0, 0.0, craft_ui::contract::MAX_EXTENT * 2.0, 32.0); + frames.idle(2, &mut |ui: &mut Ui| { + ReadsPart { common: craft_ui::contract::Common::new(), read: &read }.id_salt("wide").show_at(ui, wide); + }); + assert!(matches!(read.get(), Some(PressPhase::Idle)), "the part reads idle: {:?}", read.get()); +}