From c10002e09fa833ea67a72c630b26dc43af51a259 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 13:43:10 +0000 Subject: [PATCH] Frame cost: one Access rule for components, members and faces Collection members build their Access and sub-node only when the collection has a node, and faces build their record's facts only for a reported face of a host with a node: the component's node flag (step 8) is one signal on its core that the report, members and faces read. Without a node, a component builds Access only when its roles include a merged one or both a role with a node and none. Tooltip texts are counted where text_of and spec_text build them; part, member and pointer tooltips borrow their spec. The tooltip trial checks that the tip shows its text and builds it once a pass while it fades, and none after; the Access trial adds an open menu and a tab strip's members. --- crates/ui-contract/src/contract/cx/context.rs | 3 + .../ui-contract/src/contract/items/members.rs | 3 +- crates/ui-contract/src/contract/paint_ui.rs | 11 +-- crates/ui-contract/src/contract/run.rs | 8 ++- crates/ui-contract/src/contract/run/typed.rs | 42 +++++++---- .../ui-contract/src/contract/steps/report.rs | 18 ++--- crates/ui-contract/src/contract/tooltip.rs | 67 ++++++++++-------- .../tests/it/runtime/report_costs.rs | 69 +++++++++++++++---- 8 files changed, 147 insertions(+), 74 deletions(-) diff --git a/crates/ui-contract/src/contract/cx/context.rs b/crates/ui-contract/src/contract/cx/context.rs index 72ddd0283..cca483c64 100644 --- a/crates/ui-contract/src/contract/cx/context.rs +++ b/crates/ui-contract/src/contract/cx/context.rs @@ -187,6 +187,9 @@ pub struct CxCore<'a> { /// Not enabled, with `DisabledLook::Faded`: the component and its faces paint their enabled look, which step 13 /// fades. pub(crate) faded: bool, + /// The component has an AccessKit node this pass (step 8), or as a collection member a sub-node under its + /// collection's: its access data, its members' and its faces' have a reader. + pub(crate) node: bool, /// Where the faded component's shapes start and the ground its veil takes (`steps::fade`). pub(crate) fade: Option, /// The focus ring shape each part declared (`focus_shape`), in which step 13 draws the active part's ring. diff --git a/crates/ui-contract/src/contract/items/members.rs b/crates/ui-contract/src/contract/items/members.rs index 57f68e22b..b1aea858e 100644 --- a/crates/ui-contract/src/contract/items/members.rs +++ b/crates/ui-contract/src/contract/items/members.rs @@ -46,6 +46,7 @@ impl<'r, C: Collection> ItemsHooks<'_, 'r, C> { shown_rows: &mut [ShownRow], ) { let enabled = cx.core.resolved.enabled; + let reported = cx.core.node; let parent = access::fold::owner_of(ctx, id).unwrap_or(id); let hover = sensing::read_outcome(ctx, ArenaId::new(id)).hover; let mut prepared: Vec>> = members @@ -75,7 +76,7 @@ impl<'r, C: Collection> ItemsHooks<'_, 'r, C> { let clip = ui.clip_rect(); for ((member, member_id, on, item_sense, prep), sensed) in prepared.drain(..).zip(sensed) { let Member { at, key, row, clip: row_clip, flags, position, renaming, openers, shown, .. } = member; - let node = run::MemberNode { owner: id, part: ITEM_NODE.at(key_hash(&key)), parent, position, flags, enabled: on, item_sense }; + let node = run::MemberNode { owner: id, part: ITEM_NODE.at(key_hash(&key)), parent, position, flags, enabled: on, item_sense, reported }; ui.set_clip_rect(row_clip); let run::MemberShown { output, tip } = run::member_show(ui, env, (member_id, row), prep, sensed, &node); ui.set_clip_rect(clip); diff --git a/crates/ui-contract/src/contract/paint_ui.rs b/crates/ui-contract/src/contract/paint_ui.rs index 498f64829..f60372b59 100644 --- a/crates/ui-contract/src/contract/paint_ui.rs +++ b/crates/ui-contract/src/contract/paint_ui.rs @@ -52,6 +52,8 @@ pub struct PaintUi<'a> { /// The fade `steps::fade` began (`DisabledLook::Faded`): the faces paint their enabled look, and the children /// shown from here lie under the component's veil. pub(crate) fade: Option, + /// The composite has a node (`CxCore::node`): its step 16 fills its parts' sub-nodes from the faces' records. + pub(crate) node: bool, /// The children's `Ui`, built on the first child under `sub(id, "children")`. pub(crate) kids: Option, } @@ -249,15 +251,14 @@ impl PaintUi<'_> { flags: self.flags, enabled: self.enabled, faded: self.fade.is_some(), + reported: reported && self.node, }; - let record = run::paint_face(self.ui, self.env, &source, part, face); + let (baseline, record) = run::paint_face(self.ui, self.env, &source, part, face); let mut own = self.record.borrow_mut(); - if let Some(baseline) = record.baseline { + if let Some(baseline) = baseline { own.baseline.get_or_insert(baseline); } - if reported { - own.faces.push(record); - } + own.faces.extend(record); } /// Reserves an empty shape at the end of the component's layer, clipped to `clip`, for shapes added later by code diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index f495e805f..3df3d7571 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -409,6 +409,7 @@ fn blank_core<'a>( texts: None, selections: Vec::new(), faded: false, + node: false, fade: None, focus_shapes: Vec::new(), surface_slot: None, @@ -619,6 +620,7 @@ pub(super) fn paint_ui<'a>(ui: &'a mut Ui, env: &'a Env, core: &'a CxCore<'_>, s size: core.resolved.size, selections: &core.selections, fade: core.fade, + node: core.node, kids: None, } } @@ -891,7 +893,6 @@ struct HostStep { text_keys: Option, own: Sense, member: Option, - node: bool, /// The bar line whose rules wait for this member. line: Option, /// A bar's view cut the member: it shows nothing until the clip is restored. @@ -1015,6 +1016,7 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e } let session = member.filter(|_| e.keys().session); let core = e.core(); + core.node = node; // A hovered part's declared cursor, else the role's over the root's own shape. let own_hovered = enabled && response.hovered() && !results.values().any(|result| result.hovered); core.part_cursors = @@ -1094,7 +1096,6 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e text_keys, own, member, - node, line, cut, shown_clip, @@ -1287,7 +1288,6 @@ fn report_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) e, facts, &steps::ReportIn { - node: h.node, decls: &h.parts.decls, sense: h.own, live: e.live(), @@ -1925,4 +1925,6 @@ pub(crate) struct FaceSource<'a> { pub enabled: bool, /// The host is faded (`DisabledLook::Faded`): its faces paint their enabled look inside its fade. pub faded: bool, + /// The host's step 16 fills the part's sub-node from the face's record: a reported face of a host with a node. + pub reported: bool, } diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 6483ae76b..b7a9d2fa5 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -682,8 +682,15 @@ pub(crate) fn measured_extent( /// A face run is steps 1, 2, 4 (read only), 12 and 13 of the face component, inside the composite's step 13, under /// the composite's `env`. It registers no id, senses nothing, opens no memory stage and fires nothing. Returns the -/// record step 16 fills the part's sub-node from, and the facts the face's `access` declared. -pub(crate) fn paint_face(ui: &mut Ui, env: &Env, source: &super::FaceSource<'_>, part: PartRef, mut face: C) -> FaceRecord { +/// face's first text baseline and, for a reported face (`FaceSource::reported`), the record step 16 fills the part's +/// sub-node from, with the facts the face's `access` declared. +pub(crate) fn paint_face( + ui: &mut Ui, + env: &Env, + source: &super::FaceSource<'_>, + part: PartRef, + mut face: C, +) -> (Option, Option) { let ctx = ui.ctx().clone(); let common = std::mem::take(face.common_mut(Internal::new())); debug_assert!(common.tooltip.is_none(), "a face instance carries no tooltip; use `PartBuilder::tooltip`"); @@ -742,19 +749,22 @@ pub(crate) fn paint_face(ui: &mut Ui, env: &Env, source: &super::FaceSo size: step1.size, selections: &[], fade: None, + node: false, kids: None, }; face.paint(&mut pui, &cx); } - let (elided, baseline) = { - let paint = cx.core.paint.borrow(); - (paint.elided.clone(), paint.baseline) - }; - let name = elided.unwrap_or_else(|| step1.label.as_str().to_owned()); + let baseline = cx.core.paint.borrow().baseline; + if !source.reported { + return (baseline, None); + } + let name = cx.core.paint.borrow().elided.clone().unwrap_or_else(|| step1.label.as_str().to_owned()); // The face's own `access` adds its facts; the composite's step 16 fills the part's sub-node from the record. let mut access = Access::default(); + #[cfg(feature = "testing")] + crate::runtime::measure::count_access_build(&ctx); face.access(&mut access, &cx); - FaceRecord { + let record = FaceRecord { part, name, flags, @@ -765,7 +775,8 @@ pub(crate) fn paint_face(ui: &mut Ui, env: &Env, source: &super::FaceSo baseline, value: access.value, color: access.color, - } + }; + (baseline, Some(record)) } /// A member of a collection's run (`Collection::MEMBERS`) after steps 1, 2, 4 (read only) and 7: what the batch join @@ -836,6 +847,8 @@ pub(crate) struct MemberNode { pub enabled: bool, /// The member's sense as an item: whether its sub-node takes AccessKit focus and clicks, which the collection serves. pub item_sense: Sense, + /// The collection has a node this pass: the member's sub-node is written under it. + pub reported: bool, } /// What a member's show leaves for the collection: its output, and its tooltip while the pointer rests on it. @@ -887,6 +900,7 @@ pub(crate) fn member_show( core.results = sensed.parts; core.press = sensed.press; core.member_of = Some(node.owner); + core.node = node.reported; core.intents = crate::runtime::keys::intents_of(&ctx, Some(id), rtl); let mut cx = Cx:: { core: &mut core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok }; let flags = part_state.flags | steps::value_flags(&item, &cx); @@ -906,10 +920,12 @@ pub(crate) fn member_show( item.paint_over(&mut super::paint_ui(ui, env, cx.core, &*style), &cx); steps::unfade(ui, cx.core, fade); ui.set_clip_rect(outer_clip); - let mut access = Access::default(); - item.access(&mut access, &cx); - let member_node = report_member(cx.core, &access, node, C::ROLE); - { + if node.reported { + let mut access = Access::default(); + #[cfg(feature = "testing")] + crate::runtime::measure::count_access_build(&ctx); + item.access(&mut access, &cx); + let member_node = report_member(cx.core, &access, node, C::ROLE); let paint = cx.core.paint.borrow(); steps::report_parts(cx.core, &decls, &access.nodes, &paint.faces, member_node); } diff --git a/crates/ui-contract/src/contract/steps/report.rs b/crates/ui-contract/src/contract/steps/report.rs index 7e178b9de..274045bcc 100644 --- a/crates/ui-contract/src/contract/steps/report.rs +++ b/crates/ui-contract/src/contract/steps/report.rs @@ -14,10 +14,8 @@ use crate::style::Flags; use craft_behavior::Sense; use egui::Id; -/// What step 16 reads that the pass computed before: whether step 8 made the node and the part declarations. +/// What step 16 reads that the pass computed before: the part declarations, the sense and the container facts. pub(crate) struct ReportIn<'a> { - /// The node step 8 created, `None` for a role with no node or while AccessKit is off. - pub node: bool, pub decls: &'a Decls, pub sense: Sense, /// The container's `ContainerProps.live`. @@ -87,9 +85,13 @@ fn register_validity(p: &dyn ErasedPass<'_>) -> Option<(String, bool)> { fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>, validity: Option) { let core = p.core_ref(); let (ctx, id) = (&core.ctx, core.id); - // `Access` is read by the node, by a merged role's facts, and for its role when the type has alternatives. Without - // any of them (AccessKit off, or a role with no node) the component's access data is never built. - let reads_access = input.node || facts.role == Role::Merged || !facts.alt_roles.is_empty(); + // Without a node, `Access` is read only for a merged role's facts, and for its role when the roles the type may + // report include both a role with a node and none, which leave the label scope differently. Otherwise (AccessKit + // off, or a role with no node) the component's access data is never built. + let roles = || std::iter::once(&facts.role).chain(facts.alt_roles); + let merges = roles().any(|role| *role == Role::Merged); + let node_or_none = roles().any(|role| matches!(role, Role::Node(_))) && roles().any(|role| *role == Role::None); + let reads_access = core.node || merges || node_or_none; let (mut access, value_text) = if reads_access { p.access() } else { (Access::default(), p.value_text()) }; if let Some(text) = &value_text { match &mut access.numeric { @@ -122,13 +124,13 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_> // then stays only as the place of the children shown inside it, which assistive technology passes over: left // unwritten it would be reported as a node of unknown role. let Role::Node(role) = role else { - if input.node { + if core.node { access::pass_through(ctx, id); } scope::leave(ctx, id); return; }; - if !input.node { + if !core.node { return; } // Facts that only a node shows are built after the node check. diff --git a/crates/ui-contract/src/contract/tooltip.rs b/crates/ui-contract/src/contract/tooltip.rs index bb0f5cb5c..be95ee5f5 100644 --- a/crates/ui-contract/src/contract/tooltip.rs +++ b/crates/ui-contract/src/contract/tooltip.rs @@ -65,7 +65,21 @@ pub(crate) struct TipIn<'a> { } /// The text a tooltip shows for `spec`: the disabled text while disabled. -fn spec_text(env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled: bool) -> String { +fn spec_text(ctx: &egui::Context, env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled: bool) -> String { + count_text(ctx); + resolve_spec(env, spec, enabled) +} + +/// Counts a tooltip text built in this pass. +fn count_text(ctx: &egui::Context) { + #[cfg(feature = "testing")] + crate::runtime::measure::count_tip_text(ctx); + #[cfg(not(feature = "testing"))] + let _ = ctx; +} + +/// [`spec_text`] without the count, for a builder that counted its text already. +fn resolve_spec(env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled: bool) -> String { let label = match (&spec.disabled_text, enabled) { (Some(disabled), false) => disabled, _ => &spec.text, @@ -83,12 +97,13 @@ fn spec_text(env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled /// its own, so the label would show wherever the pointer rests on the page ("Rulers margins" over a whole page). Its /// parts name themselves with their own tooltips (a margin edge's "Left Margin"). fn text_of(core: &super::CxCore<'_>, shows_label: bool, canvas_control: bool, message: Option<&str>) -> Option { + count_text(&core.ctx); if let Some(message) = message { return Some(message.to_owned()); } let hint = core.command.as_deref().and_then(|command| command.hint.as_deref()).filter(|hint| !hint.is_empty()); let base = match core.resolved.tooltip.map(|tooltip| tooltip.spec()) { - Some(spec) => spec_text(&core.env, spec, core.resolved.enabled), + Some(spec) => resolve_spec(&core.env, spec, core.resolved.enabled), None if canvas_control => return None, None => { let paint = core.paint.borrow(); @@ -126,19 +141,19 @@ pub(crate) struct MemberTip { } /// A part's tooltip, for the part the pointer is over or the keyboard is on. -struct PartTip { +struct PartTip<'a> { part: PartRef, - spec: TooltipSpec, + spec: &'a TooltipSpec, rect: Rect, hovered: bool, } /// The part whose tooltip shows: the hovered one with a tooltip, else the keyboard's active part. -fn part_tip(core: &super::CxCore<'_>, parts: &Decls) -> Option { - let tip = |decl: &PartDecl, extras: &PartExtras, hovered: bool| { +fn part_tip<'a>(core: &super::CxCore<'_>, parts: &'a Decls) -> Option> { + let tip = |decl: &PartDecl, extras: &'a PartExtras, hovered: bool| { let part = PartRef { key: decl.key, index: decl.index }; let rect = core.results.get(&(part.key, part.index))?.rect; - Some(PartTip { part, spec: extras.tooltip.as_ref()?.spec().clone(), rect, hovered }) + Some(PartTip { part, spec: extras.tooltip.as_ref()?.spec(), rect, hovered }) }; let hovered = parts.iter().find_map(|(decl, extras)| { let result = core.results.get(&(decl.key, decl.index))?; @@ -156,19 +171,19 @@ pub(crate) fn run(core: &super::CxCore<'_>, input: TipIn<'_>) { // A hovered part's tooltip, else the hovered member's, else the keyboard's active part's. let part = match (part_tip(core, input.parts), &core.member_tip) { (Some(tip), _) if tip.hovered => Some(tip), - (_, Some(member)) => Some(PartTip { part: member.part, spec: member.spec.clone(), rect: member.rect, hovered: true }), + (_, Some(member)) => Some(PartTip { part: member.part, spec: &member.spec, rect: member.rect, hovered: true }), (tip, None) => tip, }; let member_pressed = core.member_tip.as_ref().filter(|member| part.as_ref().is_some_and(|tip| tip.part == member.part)).map(|member| member.pressed); let keyboard = core.state.flags.contains(Flags::FOCUS_VISIBLE); let (spec, rect, hovered, subject) = match &part { - Some(part) => (Some(&part.spec), part.rect, part.hovered || keyboard, Some(part.part)), + Some(part) => (Some(part.spec), part.rect, part.hovered || keyboard, Some(part.part)), None => (core.resolved.tooltip.map(Tooltip::spec), core.rect, core.response.hovered() || keyboard, None), }; let (shows_label, canvas_control, message) = (input.shows_label, input.canvas_control, input.message.as_deref()); let text = || match &part { - Some(part) => Some(spec_text(&core.env, &part.spec, core.resolved.enabled)), + Some(part) => Some(spec_text(&core.ctx, &core.env, part.spec, core.resolved.enabled)), None => text_of(core, shows_label, canvas_control, message), }; let call = TipCall { @@ -181,6 +196,7 @@ pub(crate) fn run(core: &super::CxCore<'_>, input: TipIn<'_>) { text: &text, rich: if part.is_some() { None } else { input.rich }, spec, + at_pointer: false, wants_input: hovered, pressed: member_pressed.unwrap_or_else(|| core.press.phase.is_press()), subject, @@ -196,7 +212,7 @@ pub fn attach(response: &egui::Response, tooltip: &Tooltip) { let Some(env) = Env::root_of(ctx) else { return }; memory::register_owner(ctx, response.id); let spec = tooltip.spec(); - let text = || Some(spec_text(&env.view(), spec, response.enabled())); + let text = || Some(spec_text(ctx, &env.view(), spec, response.enabled())); let call = TipCall { ctx, owner: response.id, @@ -207,6 +223,7 @@ pub fn attach(response: &egui::Response, tooltip: &Tooltip) { text: &text, rich: None, spec: Some(spec), + at_pointer: false, wants_input: response.hovered() || response.has_focus(), pressed: crate::runtime::egui::response::is_pressed(response), subject: None, @@ -223,12 +240,8 @@ pub fn attach(response: &egui::Response, tooltip: &Tooltip) { pub fn at_pointer(ui: &Ui, owner: egui::Id, tip: Option<&Tooltip>, at: Option, pressed: bool) { let ctx = ui.ctx(); let env = Env::of(ui); - let spec = tip.map(|tip| { - let mut spec = tip.spec().clone(); - spec.at = TooltipAt::Pointer; - spec - }); - let text = || spec.as_ref().map(|spec| spec_text(&env.view(), spec, true)); + let spec = tip.map(Tooltip::spec); + let text = || spec.map(|spec| spec_text(ctx, &env.view(), spec, true)); let call = TipCall { ctx, owner, @@ -238,7 +251,8 @@ pub fn at_pointer(ui: &Ui, owner: egui::Id, tip: Option<&Tooltip>, at: Option { text: &'a dyn Fn() -> Option, rich: Option>, spec: Option<&'a TooltipSpec>, + /// The tooltip shows at the pointer whatever its spec's `at` says. + at_pointer: bool, /// The pointer rests on the subject or the keyboard is on it. wants_input: bool, pressed: bool, @@ -271,18 +287,11 @@ struct TipCall<'a> { } fn show_tip(call: TipCall<'_>) { - let TipCall { ctx, owner, id, env, layer: rect_layer, rect, text: build, rich, spec, wants_input, pressed, subject, describes } = call; + let TipCall { ctx, owner, id, env, layer: rect_layer, rect, text: build, rich, spec, at_pointer, wants_input, pressed, subject, describes } = + call; let state_id = sub(id, "tip_state"); let built = std::cell::OnceCell::new(); - let text = || { - built - .get_or_init(|| { - #[cfg(feature = "testing")] - crate::runtime::measure::count_tip_text(ctx); - build() - }) - .as_deref() - }; + let text = || built.get_or_init(build).as_deref(); let wants = wants_input && (rich.is_some() || text().is_some()); // A component no pointer or keyboard wants a tooltip of, with no timer or fading layer left from before, has // nothing to advance. @@ -327,7 +336,7 @@ fn show_tip(call: TipCall<'_>) { overlay::forget_layer(ctx, tip_id, owner); return; } - let at_pointer = spec.is_some_and(|spec| spec.at == TooltipAt::Pointer); + let at_pointer = at_pointer || spec.is_some_and(|spec| spec.at == TooltipAt::Pointer); let place = if at_pointer { Place::anchored(Anchor::Pointer, Side::Below, Align::Start) } else { diff --git a/crates/ui-widgets/tests/it/runtime/report_costs.rs b/crates/ui-widgets/tests/it/runtime/report_costs.rs index bafd4ffb1..6f5cd5534 100644 --- a/crates/ui-widgets/tests/it/runtime/report_costs.rs +++ b/crates/ui-widgets/tests/it/runtime/report_costs.rs @@ -2,39 +2,67 @@ //! role, a role picked from alternatives), and its tooltip text only for a tooltip the pointer or keyboard wants or //! one still shown. -use craft_behavior::IconId; +use craft_behavior::{IconId, keyed}; use craft_ui::buttons::IconButton; -use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::contract::tooltip::DELAY; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowCollection as _, ShowContainer as _}; use craft_ui::form::Field; +use craft_ui::menu::{Menu, MenuItem}; +use craft_ui::tabs::{Tab, TabStrip}; use craft_ui::testing::pointer::moved; use craft_ui::testing::{Frames, last_pass_access_builds, last_pass_tip_texts, trial}; use craft_ui::text::NumberField; -use egui::{Rect, Ui}; +use egui::{Rect, Ui, Vec2, pos2, vec2}; use std::cell::RefCell; /// The rows of each scene. const ROWS: usize = 40; +/// A screen every row of every scene fits in, the menu's and the tab strip's included. +const ROOM: Vec2 = vec2(8000.0, 1400.0); -/// Count relation: with AccessKit on, every Field row's node reads its control's access data; with it off, a pass of -/// the same rows builds none. +/// The `Access` values the last idle pass of `show` built with AccessKit on, and with it off. +fn access_builds(show: impl Fn(&mut Ui)) -> (u32, u32) { + let mut frames = Frames::new(); + frames.screen(ROOM); + frames.idle(3, &show); + let on = last_pass_access_builds(&frames.ctx); + frames.ctx.disable_accesskit(); + frames.idle(3, &show); + (on, last_pass_access_builds(&frames.ctx)) +} + +/// Count relation: with AccessKit on, every row's node reads its access data; with it off, a pass builds none, for +/// component rows (Field rows, an open menu's items) and for a collection's members (a tab strip's tabs) alike. #[trial] fn reports_build_access_data_only_for_a_reader() { let labels: Vec = (0..ROWS).map(|row| format!("Row {row}")).collect(); - let show = |ui: &mut Ui| { + let fields = |ui: &mut Ui| { for label in &labels { Field::new(label.clone()).show(ui, |ui, _| NumberField::new(label.clone(), Bind::given(Some(1.0))).show(ui)); } }; - let mut frames = Frames::new(); - frames.idle(3, &show); - assert!(last_pass_access_builds(&frames.ctx) >= ROWS as u32, "with AccessKit on, each row's node reads its access data"); - frames.ctx.disable_accesskit(); - frames.idle(3, &show); - assert_eq!(last_pass_access_builds(&frames.ctx), 0, "with AccessKit off, no row builds access data"); + let menu = |ui: &mut Ui| { + let mut open = true; + let _ = Menu::new("Items").open(Bind::bound(&mut open)).id_salt("menu").show(ui, |ui, _| { + for label in &labels { + let _ = MenuItem::new(label.clone()).id_salt(label.as_str()).show(ui); + } + }); + }; + let keys: Vec = (0..ROWS).collect(); + let tabs = |ui: &mut Ui| { + let source = keyed(&keys, |key: &usize| *key); + let _ = TabStrip::new("Documents").shown(0).show_items(ui, &source, &mut |key, _| Tab::new(labels[*key].clone())); + }; + for (scene, (on, off)) in [("Field rows", access_builds(fields)), ("menu items", access_builds(menu)), ("tabs", access_builds(tabs))] { + assert!(on >= ROWS as u32, "with AccessKit on, each of the {scene} reads its access data ({on} built)"); + assert_eq!(off, 0, "with AccessKit off, none of the {scene} builds access data"); + } } -/// Count relation: icon buttons whose label is their tooltip build no tooltip text while the pointer is away; the one -/// the pointer rests on builds its own once a pass, before and after its tooltip shows, and the others still none. +/// Count relation: icon buttons whose label is their tooltip build no tooltip text while the pointer is away. The one +/// the pointer rests on builds its own once a pass, before and while its tooltip shows it; once the pointer leaves, the +/// fading tooltip builds it once a pass, and nothing builds it after the fade. #[trial] fn tooltip_texts_are_built_only_for_a_wanted_tooltip() { let labels: Vec = (0..ROWS).map(|row| format!("Tool {row}")).collect(); @@ -46,14 +74,25 @@ fn tooltip_texts_are_built_only_for_a_wanted_tooltip() { shown.push(IconButton::new(IconId::new("play")).label(label.clone()).show(ui).response.rect); } }; + let painted = |frames: &Frames| frames.texts().iter().any(|run| run.text == labels[ROWS / 2]); let mut frames = Frames::new(); + frames.screen(ROOM); frames.idle(3, &show); assert_eq!(last_pass_tip_texts(&frames.ctx), 0, "no tooltip is wanted, so no text is built"); let rested: Rect = rects.borrow()[ROWS / 2]; frames.frame(vec![moved(rested.center())], &show); frames.idle(2, &show); assert_eq!(last_pass_tip_texts(&frames.ctx), 1, "only the button under the pointer builds its text"); - frames.advance(craft_ui::contract::tooltip::DELAY); + assert!(!painted(&frames), "the tooltip waits for its delay"); + frames.advance(DELAY); frames.idle(3, &show); + assert!(painted(&frames), "the tooltip shows the button's label"); assert_eq!(last_pass_tip_texts(&frames.ctx), 1, "the shown tooltip's text is built once a pass"); + frames.frame(vec![moved(pos2(ROOM.x - 10.0, ROOM.y - 10.0))], &show); + assert!(painted(&frames), "the tooltip fades out after the pointer leaves"); + assert_eq!(last_pass_tip_texts(&frames.ctx), 1, "the fading tooltip's text is built once a pass"); + frames.advance(1.0); + frames.idle(3, &show); + assert!(!painted(&frames), "the faded tooltip is gone"); + assert_eq!(last_pass_tip_texts(&frames.ctx), 0, "nothing builds a text after the fade"); }