diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 956401601..6483ae76b 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -387,6 +387,8 @@ impl<'a, C: Component> ErasedPass<'a> for TypedPass<'_, 'a, C> { } fn access(&self) -> (Access, Option) { + #[cfg(feature = "testing")] + crate::runtime::measure::count_access_build(&self.cx.core.ctx); let mut access = Access::default(); self.comp.access(&mut access, self.cx); (access, self.comp.value_text(self.cx)) diff --git a/crates/ui-contract/src/contract/steps/report.rs b/crates/ui-contract/src/contract/steps/report.rs index c53047145..7e178b9de 100644 --- a/crates/ui-contract/src/contract/steps/report.rs +++ b/crates/ui-contract/src/contract/steps/report.rs @@ -87,7 +87,10 @@ 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); - let (mut access, value_text) = p.access(); + // `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(); + 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 { Some(numeric) => numeric.text = Some(text.clone()), @@ -115,7 +118,6 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_> return; } scope::finish(ctx, id, None); - let carried = scope::carried_name(ctx, id); // Step 8 created the node by the type's role; the role this pass reports may be none (`Access::role`). The node // 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. @@ -130,6 +132,7 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_> return; } // Facts that only a node shows are built after the node check. + let carried = scope::carried_name(ctx, id); for node in &mut access.nodes { if let Some(toggle) = core.toggles.iter().find(|toggle| toggle.part == node.part) { node.expanded = Some(toggle.open); diff --git a/crates/ui-contract/src/contract/tooltip.rs b/crates/ui-contract/src/contract/tooltip.rs index c0da2e83a..bb0f5cb5c 100644 --- a/crates/ui-contract/src/contract/tooltip.rs +++ b/crates/ui-contract/src/contract/tooltip.rs @@ -82,9 +82,9 @@ fn spec_text(env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled /// A canvas control (`canvas_control`) never shows its label: its rect is the canvas it is shown over, not a face of /// 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) -> Option { - if message.is_some() { - return message; +fn text_of(core: &super::CxCore<'_>, shows_label: bool, canvas_control: bool, message: Option<&str>) -> Option { + 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()) { @@ -162,18 +162,14 @@ pub(crate) fn run(core: &super::CxCore<'_>, input: TipIn<'_>) { 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 own_spec = core.resolved.tooltip.map(|tooltip| tooltip.spec().clone()); - let (text, spec, rect, hovered, subject) = match &part { - Some(part) => ( - Some(spec_text(&core.env, &part.spec, core.resolved.enabled)), - Some(part.spec.clone()), - part.rect, - part.hovered || keyboard, - Some(part.part), - ), - None => { - (text_of(core, input.shows_label, input.canvas_control, input.message), own_spec, core.rect, core.response.hovered() || keyboard, None) - } + let (spec, rect, hovered, subject) = match &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)), + None => text_of(core, shows_label, canvas_control, message), }; let call = TipCall { ctx: &core.ctx, @@ -182,7 +178,7 @@ pub(crate) fn run(core: &super::CxCore<'_>, input: TipIn<'_>) { env: input.env, layer: input.ui.layer_id(), rect, - text, + text: &text, rich: if part.is_some() { None } else { input.rich }, spec, wants_input: hovered, @@ -199,8 +195,8 @@ pub fn attach(response: &egui::Response, tooltip: &Tooltip) { let ctx = &response.ctx; let Some(env) = Env::root_of(ctx) else { return }; memory::register_owner(ctx, response.id); - let spec = tooltip.spec().clone(); - let text = spec_text(&env.view(), &spec, response.enabled()); + let spec = tooltip.spec(); + let text = || Some(spec_text(&env.view(), spec, response.enabled())); let call = TipCall { ctx, owner: response.id, @@ -208,7 +204,7 @@ pub fn attach(response: &egui::Response, tooltip: &Tooltip) { env: &env, layer: response.layer_id, rect: response.rect, - text: Some(text), + text: &text, rich: None, spec: Some(spec), wants_input: response.hovered() || response.has_focus(), @@ -232,7 +228,7 @@ pub fn at_pointer(ui: &Ui, owner: egui::Id, tip: Option<&Tooltip>, at: Option, at: Option { layer: areas::Layer, /// The rect the tooltip hangs from, in the layer's space. rect: Rect, - text: Option, + /// Builds the text; `show_tip` calls it only for a tooltip that is wanted or shown. + text: &'a dyn Fn() -> Option, rich: Option>, - spec: Option, + spec: Option<&'a TooltipSpec>, /// The pointer rests on the subject or the keyboard is on it. wants_input: bool, pressed: bool, @@ -274,11 +271,22 @@ struct TipCall<'a> { } fn show_tip(call: TipCall<'_>) { - let TipCall { ctx, owner, id, env, layer: rect_layer, rect, text, rich, spec, wants_input, pressed, subject, describes } = call; + let TipCall { ctx, owner, id, env, layer: rect_layer, rect, text: build, rich, spec, wants_input, pressed, subject, describes } = call; let state_id = sub(id, "tip_state"); - let has_content = text.is_some() || rich.is_some(); - // A component with no tooltip content and no timer or fading layer left from before has nothing to advance. - if !(has_content && wants_input) && !store::exists::(ctx, state_id) { + 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 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. + if !wants && !store::exists::(ctx, state_id) { return; } let now = frame::clock(ctx); @@ -289,8 +297,7 @@ fn show_tip(call: TipCall<'_>) { if state.subject != subject { state = TipState { subject, ..TipState::default() }; } - let when_now = spec.as_ref().is_some_and(|spec| spec.when == TooltipWhen::Now); - let wants = has_content && wants_input; + let when_now = spec.is_some_and(|spec| spec.when == TooltipWhen::Now); let screen = areas::layer_to_screen(ctx, rect_layer) * rect; let suppressed = pressed || overlay_blocks(ctx, screen.center()) || pointer::press_captured(ctx) || drag::running(ctx); if wants { @@ -320,7 +327,7 @@ fn show_tip(call: TipCall<'_>) { overlay::forget_layer(ctx, tip_id, owner); return; } - let at_pointer = spec.as_ref().is_some_and(|spec| spec.at == TooltipAt::Pointer); + let 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 { @@ -342,7 +349,7 @@ fn show_tip(call: TipCall<'_>) { overlay::host(ctx, &layer, &mut |ui| match rich.take() { Some(content) => content(ui), None => { - if let Some(text) = &text { + if let Some(text) = text() { let tip = TipText { common: Common::default(), text }.id_salt(tip_id).show(ui); if let Some(node) = describes { // The described control's node was written earlier in the pass; the relation joins it. diff --git a/crates/ui-contract/src/runtime/measure.rs b/crates/ui-contract/src/runtime/measure.rs index cc9010ece..d64d0beef 100644 --- a/crates/ui-contract/src/runtime/measure.rs +++ b/crates/ui-contract/src/runtime/measure.rs @@ -360,6 +360,30 @@ pub fn chord_texts(ctx: &Context) -> u32 { last_totals(ctx, |log| log.chord_texts, 0) } +/// Counts a component's `Access` built for its report in this pass. +#[cfg(feature = "testing")] +pub(crate) fn count_access_build(ctx: &Context) { + totals(ctx, |log| log.access_builds += 1); +} + +/// The `Access` values the reports of the last pass built: none for a component whose report has no reader of them. +#[cfg(feature = "testing")] +pub fn last_pass_access_builds(ctx: &Context) -> u32 { + last_totals(ctx, |log| log.access_builds, 0) +} + +/// Counts a tooltip text built in this pass. +#[cfg(feature = "testing")] +pub(crate) fn count_tip_text(ctx: &Context) { + totals(ctx, |log| log.tip_texts += 1); +} + +/// The tooltip texts built in the last pass: only for a tooltip the pointer or the keyboard wants, or one still shown. +#[cfg(feature = "testing")] +pub fn last_pass_tip_texts(ctx: &Context) -> u32 { + last_totals(ctx, |log| log.tip_texts, 0) +} + /// What a command resolution was for. #[cfg(feature = "testing")] #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -488,7 +512,8 @@ pub fn last_frame_memo_runs(ctx: &Context) -> Vec<(&'static str, Id, MemoRun)> { pass_state::with::(ctx, |log| if log.frame == last { log.frame_runs.clone() } else { Vec::new() }) } -/// Bytes fed into memo keys in one pass, and the layouts, measure asks and chord texts of that pass. +/// Bytes fed into memo keys in one pass, and the layouts, measure asks, chord texts, `Access` values and tooltip texts +/// of that pass. #[cfg(feature = "testing")] #[derive(Default)] pub(crate) struct Totals { @@ -496,6 +521,8 @@ pub(crate) struct Totals { key_bytes: usize, text_layouts: u32, chord_texts: u32, + access_builds: u32, + tip_texts: u32, resolves: [u32; 3], wake_walks: u32, asks: Vec<(&'static str, u32)>, diff --git a/crates/ui-contract/src/testing/mod.rs b/crates/ui-contract/src/testing/mod.rs index fab30e3d4..c2a06851e 100644 --- a/crates/ui-contract/src/testing/mod.rs +++ b/crates/ui-contract/src/testing/mod.rs @@ -55,9 +55,9 @@ pub use crate::runtime::layout_memory::forget_kept as forget_measurements; pub use crate::runtime::layout_memory::kept_leaves as kept_measurement_leaves; pub use crate::runtime::measure::{ MemoRun, active as is_measuring, body_runs as measuring_runs, contributions as measured_contributions, last_frame_derived_builds, - last_frame_memo_runs, last_pass_body_runs as last_pass_measuring_runs, last_pass_derived_builds, last_pass_measure_asks, - last_pass_memo_key_bytes, last_pass_memo_runs, last_pass_runs, last_pass_text_layouts, least_runs as shrink_least_runs, row_builds, row_measures, - run as measuring_run, take_effects, widest_runs, + last_frame_memo_runs, last_pass_access_builds, last_pass_body_runs as last_pass_measuring_runs, last_pass_derived_builds, last_pass_measure_asks, + last_pass_memo_key_bytes, last_pass_memo_runs, last_pass_runs, last_pass_text_layouts, last_pass_tip_texts, least_runs as shrink_least_runs, + row_builds, row_measures, run as measuring_run, take_effects, widest_runs, }; pub use craft_ui_macros::{story, trial}; pub use fixture::{Fixture, Impure, Room, Target}; diff --git a/crates/ui-widgets/tests/it/runtime/mod.rs b/crates/ui-widgets/tests/it/runtime/mod.rs index 3063263e7..24b162a8c 100644 --- a/crates/ui-widgets/tests/it/runtime/mod.rs +++ b/crates/ui-widgets/tests/it/runtime/mod.rs @@ -1,2 +1,3 @@ pub(crate) mod bypass; pub(crate) mod inspect; +pub(crate) mod report_costs; diff --git a/crates/ui-widgets/tests/it/runtime/report_costs.rs b/crates/ui-widgets/tests/it/runtime/report_costs.rs new file mode 100644 index 000000000..bafd4ffb1 --- /dev/null +++ b/crates/ui-widgets/tests/it/runtime/report_costs.rs @@ -0,0 +1,59 @@ +//! What the report step builds for a component in each pass: its AccessKit data only for a reader (a node, a merged +//! 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_ui::buttons::IconButton; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::form::Field; +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 std::cell::RefCell; + +/// The rows of each scene. +const ROWS: usize = 40; + +/// 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. +#[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| { + 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"); +} + +/// 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. +#[trial] +fn tooltip_texts_are_built_only_for_a_wanted_tooltip() { + let labels: Vec = (0..ROWS).map(|row| format!("Tool {row}")).collect(); + let rects = RefCell::new(Vec::new()); + let show = |ui: &mut Ui| { + let mut shown = rects.borrow_mut(); + shown.clear(); + for label in &labels { + shown.push(IconButton::new(IconId::new("play")).label(label.clone()).show(ui).response.rect); + } + }; + let mut frames = Frames::new(); + 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); + frames.idle(3, &show); + assert_eq!(last_pass_tip_texts(&frames.ctx), 1, "the shown tooltip's text is built once a pass"); +}