diff --git a/masonry/examples/calc_masonry.rs b/masonry/examples/calc_masonry.rs index 543903d74..a2385e2f0 100644 --- a/masonry/examples/calc_masonry.rs +++ b/masonry/examples/calc_masonry.rs @@ -17,8 +17,8 @@ use masonry::dpi::LogicalSize; use masonry::widget::{Align, CrossAxisAlignment, Flex, Label, RootWidget, SizedBox}; use masonry::{ AccessCtx, AccessEvent, Action, AppDriver, BoxConstraints, Color, DriverCtx, EventCtx, - LayoutCtx, PaintCtx, Point, PointerEvent, RegisterCtx, Size, TextEvent, Update, UpdateCtx, - Widget, WidgetId, WidgetPod, + LayoutCtx, PaintCtx, Point, PointerEvent, QueryCtx, RegisterCtx, Size, TextEvent, Update, + UpdateCtx, Widget, WidgetId, WidgetPod, }; use smallvec::{smallvec, SmallVec}; use tracing::{trace, trace_span, Span}; @@ -244,8 +244,8 @@ impl Widget for CalcButton { smallvec![self.inner.id()] } - fn make_trace_span(&self) -> Span { - trace_span!("CalcButton") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("CalcButton", id = ctx.widget_id().trace()) } } diff --git a/masonry/examples/custom_widget.rs b/masonry/examples/custom_widget.rs index 7d3508b57..49b52e09f 100644 --- a/masonry/examples/custom_widget.rs +++ b/masonry/examples/custom_widget.rs @@ -15,7 +15,8 @@ use masonry::kurbo::{BezPath, Stroke}; use masonry::widget::{ObjectFit, RootWidget}; use masonry::{ AccessCtx, AccessEvent, Action, Affine, AppDriver, BoxConstraints, Color, DriverCtx, EventCtx, - LayoutCtx, PaintCtx, Point, PointerEvent, Rect, RegisterCtx, Size, TextEvent, Widget, WidgetId, + LayoutCtx, PaintCtx, Point, PointerEvent, QueryCtx, Rect, RegisterCtx, Size, TextEvent, Widget, + WidgetId, }; use parley::layout::Alignment; use parley::style::{FontFamily, FontStack, StyleProperty}; @@ -139,8 +140,8 @@ impl Widget for CustomWidget { SmallVec::new() } - fn make_trace_span(&self) -> Span { - trace_span!("CustomWidget") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("CustomWidget", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/passes/accessibility.rs b/masonry/src/passes/accessibility.rs index cb8d82924..ef6a81756 100644 --- a/masonry/src/passes/accessibility.rs +++ b/masonry/src/passes/accessibility.rs @@ -10,6 +10,8 @@ use crate::render_root::{RenderRoot, RenderRootState}; use crate::tree_arena::ArenaMut; use crate::{AccessCtx, Widget, WidgetState}; +use super::enter_span_if; + // --- MARK: BUILD TREE --- fn build_accessibility_tree( global_state: &mut RenderRootState, @@ -19,10 +21,12 @@ fn build_accessibility_tree( rebuild_all: bool, scale_factor: f64, ) { - let _span = global_state - .trace - .access - .then(|| widget.item.make_trace_span().entered()); + let _span = enter_span_if( + global_state.trace.access, + global_state, + widget.reborrow(), + state.reborrow(), + ); let id = state.item.id; if !rebuild_all && !state.item.needs_accessibility { diff --git a/masonry/src/passes/anim.rs b/masonry/src/passes/anim.rs index f670e86b5..cd34e8ee7 100644 --- a/masonry/src/passes/anim.rs +++ b/masonry/src/passes/anim.rs @@ -3,7 +3,7 @@ use tracing::info_span; -use crate::passes::recurse_on_children; +use crate::passes::{enter_span_if, recurse_on_children}; use crate::render_root::{RenderRoot, RenderRootState}; use crate::tree_arena::ArenaMut; use crate::{UpdateCtx, Widget, WidgetState}; @@ -15,11 +15,12 @@ fn update_anim_for_widget( mut state: ArenaMut<'_, WidgetState>, elapsed_ns: u64, ) { - let _span = global_state - .trace - .anim - .then(|| widget.item.make_trace_span().entered()); - + let _span = enter_span_if( + global_state.trace.anim, + global_state, + widget.reborrow(), + state.reborrow(), + ); if !state.item.needs_anim { return; } diff --git a/masonry/src/passes/compose.rs b/masonry/src/passes/compose.rs index 06f3c37cc..5ae018f80 100644 --- a/masonry/src/passes/compose.rs +++ b/masonry/src/passes/compose.rs @@ -4,7 +4,7 @@ use tracing::info_span; use vello::kurbo::Vec2; -use crate::passes::recurse_on_children; +use crate::passes::{enter_span_if, recurse_on_children}; use crate::render_root::{RenderRoot, RenderRootSignal, RenderRootState}; use crate::tree_arena::ArenaMut; use crate::{ComposeCtx, Widget, WidgetState}; @@ -17,10 +17,12 @@ fn compose_widget( parent_moved: bool, parent_translation: Vec2, ) { - let _span = global_state - .trace - .compose - .then(|| widget.item.make_trace_span().entered()); + let _span = enter_span_if( + global_state.trace.compose, + global_state, + widget.reborrow(), + state.reborrow(), + ); let moved = parent_moved || state.item.translation_changed; let translation = parent_translation + state.item.translation + state.item.origin.to_vec2(); diff --git a/masonry/src/passes/event.rs b/masonry/src/passes/event.rs index 41be58e8f..3a9e3c23a 100644 --- a/masonry/src/passes/event.rs +++ b/masonry/src/passes/event.rs @@ -6,7 +6,7 @@ use tracing::{debug, info_span, trace}; use winit::event::ElementState; use winit::keyboard::{KeyCode, PhysicalKey}; -use crate::passes::merge_state_up; +use crate::passes::{enter_span, merge_state_up}; use crate::render_root::RenderRoot; use crate::{AccessEvent, EventCtx, Handled, PointerEvent, TextEvent, Widget, WidgetId}; @@ -46,21 +46,24 @@ fn run_event_pass( let mut is_handled = false; while let Some(widget_id) = target_widget_id { let parent_id = root.widget_arena.parent_of(widget_id); - let (widget_mut, state_mut) = root.widget_arena.get_pair_mut(widget_id); - - let mut ctx = EventCtx { - global_state: &mut root.global_state, - widget_state: state_mut.item, - widget_state_children: state_mut.children, - widget_children: widget_mut.children, - target: original_target.unwrap(), - allow_pointer_capture, - is_handled: false, - }; - let widget = widget_mut.item; + let (mut widget_mut, mut state_mut) = root.widget_arena.get_pair_mut(widget_id); if !is_handled { - let _span = widget.make_trace_span().entered(); + let _span = enter_span( + &root.global_state, + widget_mut.reborrow(), + state_mut.reborrow(), + ); + let mut ctx = EventCtx { + global_state: &mut root.global_state, + widget_state: state_mut.item, + widget_state_children: state_mut.children, + widget_children: widget_mut.children, + target: original_target.unwrap(), + allow_pointer_capture, + is_handled: false, + }; + let widget = widget_mut.item; if trace { trace!( "Widget '{}' {} visited", diff --git a/masonry/src/passes/layout.rs b/masonry/src/passes/layout.rs index 37f672a85..00f78fb7a 100644 --- a/masonry/src/passes/layout.rs +++ b/masonry/src/passes/layout.rs @@ -10,7 +10,7 @@ use smallvec::SmallVec; use tracing::{info_span, trace}; use vello::kurbo::{Point, Rect, Size}; -use crate::passes::recurse_on_children; +use crate::passes::{enter_span_if, recurse_on_children}; use crate::render_root::{RenderRoot, RenderRootSignal, WindowSizePolicy}; use crate::widget::WidgetState; use crate::{BoxConstraints, LayoutCtx, Widget, WidgetPod}; @@ -28,7 +28,12 @@ pub(crate) fn run_layout_on( let mut state = parent_ctx.widget_state_children.get_child_mut(id).unwrap(); let trace = parent_ctx.global_state.trace.layout; - let _span = trace.then(|| widget.item.make_trace_span().entered()); + let _span = enter_span_if( + trace, + parent_ctx.global_state, + widget.reborrow(), + state.reborrow(), + ); let mut children_ids = SmallVec::new(); if cfg!(debug_assertions) { diff --git a/masonry/src/passes/mod.rs b/masonry/src/passes/mod.rs index 6d3e6d67f..d152b1529 100644 --- a/masonry/src/passes/mod.rs +++ b/masonry/src/passes/mod.rs @@ -1,9 +1,12 @@ // Copyright 2024 the Xilem Authors // SPDX-License-Identifier: Apache-2.0 -use crate::tree_arena::{ArenaMut, ArenaMutChildren}; +use tracing::span::EnteredSpan; + +use crate::render_root::RenderRootState; +use crate::tree_arena::{ArenaMut, ArenaMutChildren, ArenaRef}; use crate::widget::WidgetArena; -use crate::{Widget, WidgetId, WidgetState}; +use crate::{QueryCtx, Widget, WidgetId, WidgetState}; pub(crate) mod accessibility; pub(crate) mod anim; @@ -14,6 +17,35 @@ pub(crate) mod mutate; pub(crate) mod paint; pub(crate) mod update; +#[must_use = "Span will be immediately closed if dropped"] +pub(crate) fn enter_span_if( + enabled: bool, + global_state: &RenderRootState, + widget: ArenaRef<'_, Box>, + state: ArenaRef<'_, WidgetState>, +) -> Option { + if enabled { + Some(enter_span(global_state, widget, state)) + } else { + None + } +} + +#[must_use = "Span will be immediately closed if dropped"] +pub(crate) fn enter_span( + global_state: &RenderRootState, + widget: ArenaRef<'_, Box>, + state: ArenaRef<'_, WidgetState>, +) -> EnteredSpan { + let ctx = QueryCtx { + global_state, + widget_state: state.item, + widget_state_children: state.children, + widget_children: widget.children, + }; + widget.item.make_trace_span(&ctx).entered() +} + pub(crate) fn recurse_on_children( id: WidgetId, mut widget: ArenaMut<'_, Box>, @@ -68,6 +100,12 @@ pub(crate) fn merge_state_up(arena: &mut WidgetArena, widget_id: WidgetId) { /// `MASONRY_TRACE_PASSES` environment variable. /// /// Note that passes which are bounded by depth (rather than absolute size) are never filtered out here. +/// +/// Ideally, we'd cache the spans, which would make a lot (but not all) of this unnecessary. +/// However, each pass uses a different parent span (with the individual pass's name), so it's +/// (at best) non-trivial to make that work. +/// +/// We could *maybe* use a global parent span called "Pass", with a name field, but that's extremely ugly. pub(crate) struct PassTracing { pub(crate) update_tree: bool, pub(crate) anim: bool, diff --git a/masonry/src/passes/paint.rs b/masonry/src/passes/paint.rs index a5d05dafb..0842a35d9 100644 --- a/masonry/src/passes/paint.rs +++ b/masonry/src/passes/paint.rs @@ -8,7 +8,7 @@ use vello::kurbo::{Affine, Stroke}; use vello::peniko::Mix; use vello::Scene; -use crate::passes::recurse_on_children; +use crate::passes::{enter_span_if, recurse_on_children}; use crate::render_root::{RenderRoot, RenderRootState}; use crate::theme::get_debug_color; use crate::tree_arena::ArenaMut; @@ -24,7 +24,8 @@ fn paint_widget( debug_paint: bool, ) { let trace = global_state.trace.paint; - let _span = trace.then(|| widget.item.make_trace_span().entered()); + let _span = enter_span_if(trace, global_state, widget.reborrow(), state.reborrow()); + let id = state.item.id; // TODO - Handle invalidation regions diff --git a/masonry/src/passes/update.rs b/masonry/src/passes/update.rs index 655bc6185..3a71a90e6 100644 --- a/masonry/src/passes/update.rs +++ b/masonry/src/passes/update.rs @@ -7,7 +7,7 @@ use cursor_icon::CursorIcon; use tracing::{info_span, trace}; use crate::passes::event::run_on_pointer_event_pass; -use crate::passes::{merge_state_up, recurse_on_children}; +use crate::passes::{enter_span, enter_span_if, merge_state_up, recurse_on_children}; use crate::render_root::{RenderRoot, RenderRootSignal, RenderRootState}; use crate::tree_arena::ArenaMut; use crate::{ @@ -82,7 +82,7 @@ fn update_widget_tree( mut state: ArenaMut<'_, WidgetState>, ) { let trace = global_state.trace.update_tree; - let _span = trace.then(|| widget.item.make_trace_span().entered()); + let _span = enter_span_if(trace, global_state, widget.reborrow(), state.reborrow()); let id = state.item.id; if !state.item.children_changed { @@ -196,7 +196,7 @@ fn update_disabled_for_widget( mut state: ArenaMut<'_, WidgetState>, parent_disabled: bool, ) { - let _span = widget.item.make_trace_span().entered(); + let _span = enter_span(global_state, widget.reborrow(), state.reborrow()); let id = state.item.id; let disabled = state.item.is_explicitly_disabled || parent_disabled; @@ -260,7 +260,7 @@ fn update_stashed_for_widget( mut state: ArenaMut<'_, WidgetState>, parent_stashed: bool, ) { - let _span = widget.item.make_trace_span().entered(); + let _span = enter_span(global_state, widget.reborrow(), state.reborrow()); let id = state.item.id; let stashed = state.item.is_explicitly_stashed || parent_stashed; @@ -327,10 +327,10 @@ pub(crate) fn run_update_stashed_pass(root: &mut RenderRoot) { fn update_focus_chain_for_widget( global_state: &mut RenderRootState, mut widget: ArenaMut<'_, Box>, - state: ArenaMut<'_, WidgetState>, + mut state: ArenaMut<'_, WidgetState>, parent_focus_chain: &mut Vec, ) { - let _span = widget.item.make_trace_span().entered(); + let _span = enter_span(global_state, widget.reborrow(), state.reborrow()); let id = state.item.id; if !state.item.update_focus_chain { diff --git a/masonry/src/testing/helper_widgets.rs b/masonry/src/testing/helper_widgets.rs index dbaba17ee..870cd6716 100644 --- a/masonry/src/testing/helper_widgets.rs +++ b/masonry/src/testing/helper_widgets.rs @@ -381,8 +381,8 @@ impl Widget for ModularWidget { self.accepts_text_input } - fn make_trace_span(&self) -> tracing::Span { - trace_span!("ModularWidget") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> tracing::Span { + trace_span!("ModularWidget", id = ctx.widget_id().trace()) } fn get_debug_text(&self) -> Option { @@ -581,8 +581,8 @@ impl Widget for Recorder { self.child.accepts_text_input() } - fn make_trace_span(&self) -> tracing::Span { - self.child.make_trace_span() + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> tracing::Span { + self.child.make_trace_span(ctx) } fn get_debug_text(&self) -> Option { diff --git a/masonry/src/widget/align.rs b/masonry/src/widget/align.rs index fa72d837c..ad20734d4 100644 --- a/masonry/src/widget/align.rs +++ b/masonry/src/widget/align.rs @@ -17,8 +17,8 @@ use crate::contexts::AccessCtx; use crate::paint_scene_helpers::UnitPoint; use crate::widget::WidgetPod; use crate::{ - AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, Rect, RegisterCtx, - Size, TextEvent, Widget, WidgetId, + AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, QueryCtx, Rect, + RegisterCtx, Size, TextEvent, Widget, WidgetId, }; // TODO - Have child widget type as generic argument @@ -148,8 +148,8 @@ impl Widget for Align { smallvec![self.child.id()] } - fn make_trace_span(&self) -> Span { - trace_span!("Align") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Align", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/button.rs b/masonry/src/widget/button.rs index 2b187a307..ee49bce05 100644 --- a/masonry/src/widget/button.rs +++ b/masonry/src/widget/button.rs @@ -15,7 +15,7 @@ use crate::text::ArcStr; use crate::widget::{Label, WidgetMut, WidgetPod}; use crate::{ theme, AccessCtx, AccessEvent, BoxConstraints, EventCtx, Insets, LayoutCtx, PaintCtx, - PointerEvent, Size, TextEvent, Update, UpdateCtx, Widget, WidgetId, + PointerEvent, QueryCtx, Size, TextEvent, Update, UpdateCtx, Widget, WidgetId, }; // the minimum padding added to a button. @@ -204,8 +204,8 @@ impl Widget for Button { smallvec![self.label.id()] } - fn make_trace_span(&self) -> Span { - trace_span!("Button") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Button", id = ctx.widget_id().trace()) } // FIXME diff --git a/masonry/src/widget/checkbox.rs b/masonry/src/widget/checkbox.rs index 8e2aea5b8..13d47ca98 100644 --- a/masonry/src/widget/checkbox.rs +++ b/masonry/src/widget/checkbox.rs @@ -15,7 +15,7 @@ use crate::text::ArcStr; use crate::widget::{Label, WidgetMut}; use crate::{ theme, AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, - RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, WidgetPod, + QueryCtx, RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, WidgetPod, }; /// A checkbox that can be toggled. @@ -213,8 +213,8 @@ impl Widget for Checkbox { smallvec![self.label.id()] } - fn make_trace_span(&self) -> Span { - trace_span!("Checkbox") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Checkbox", id = ctx.widget_id().trace()) } fn get_debug_text(&self) -> Option { diff --git a/masonry/src/widget/flex.rs b/masonry/src/widget/flex.rs index 78a7e34e3..5b709b253 100644 --- a/masonry/src/widget/flex.rs +++ b/masonry/src/widget/flex.rs @@ -14,7 +14,7 @@ use crate::theme::get_debug_color; use crate::widget::WidgetMut; use crate::{ AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent, - Rect, Size, TextEvent, Widget, WidgetId, WidgetPod, + QueryCtx, Rect, Size, TextEvent, Widget, WidgetId, WidgetPod, }; /// A container with either horizontal or vertical layout. @@ -1202,8 +1202,8 @@ impl Widget for Flex { .collect() } - fn make_trace_span(&self) -> Span { - trace_span!("Flex") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Flex", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/grid.rs b/masonry/src/widget/grid.rs index 7e5849610..02d7f6610 100644 --- a/masonry/src/widget/grid.rs +++ b/masonry/src/widget/grid.rs @@ -11,7 +11,7 @@ use crate::theme::get_debug_color; use crate::widget::WidgetMut; use crate::{ AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent, - Size, TextEvent, Widget, WidgetId, WidgetPod, + QueryCtx, Size, TextEvent, Widget, WidgetId, WidgetPod, }; pub struct Grid { @@ -311,8 +311,8 @@ impl Widget for Grid { .collect() } - fn make_trace_span(&self) -> Span { - trace_span!("Grid") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Grid", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/image.rs b/masonry/src/widget/image.rs index d044e9caa..31e09a8d5 100644 --- a/masonry/src/widget/image.rs +++ b/masonry/src/widget/image.rs @@ -13,7 +13,7 @@ use vello::Scene; use crate::widget::{ObjectFit, WidgetMut}; use crate::{ - AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, + AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, QueryCtx, RegisterCtx, Size, TextEvent, Update, UpdateCtx, Widget, WidgetId, }; @@ -134,8 +134,8 @@ impl Widget for Image { SmallVec::new() } - fn make_trace_span(&self) -> Span { - trace_span!("Image") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Image", id = ctx.widget_id().trace()) } } @@ -163,14 +163,20 @@ mod tests { #[test] fn tall_paint() { - #[rustfmt::skip] let image_data = ImageBuf::new( - vec![ - 255, 255, 255, 255, - 0, 0, 0, 255, - 0, 0, 0, 255, - 255, 255, 255, 255, - ].into(), + // This could have a more concise chain, but previously used versions either + // had unreadable formatting or used `rustfmt::skip`, which broke formatting + // across large parts of the file. + [ + [255, 255, 255, 255], + [000, 000, 000, 255], + [000, 000, 000, 255], + [255, 255, 255, 255], + ] + .into_iter() + .flatten() + .collect::>() + .into(), Format::Rgba8, 2, 2, diff --git a/masonry/src/widget/label.rs b/masonry/src/widget/label.rs index f0ca9e752..0a46f5f26 100644 --- a/masonry/src/widget/label.rs +++ b/masonry/src/widget/label.rs @@ -16,7 +16,7 @@ use vello::Scene; use crate::text::{ArcStr, TextBrush, TextLayout}; use crate::widget::WidgetMut; use crate::{ - AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, + AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, QueryCtx, RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, }; @@ -249,8 +249,8 @@ impl Widget for Label { false } - fn make_trace_span(&self) -> Span { - trace_span!("Label") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Label", id = ctx.widget_id().trace()) } fn get_debug_text(&self) -> Option { diff --git a/masonry/src/widget/portal.rs b/masonry/src/widget/portal.rs index 38c64ba47..d496cc7cb 100644 --- a/masonry/src/widget/portal.rs +++ b/masonry/src/widget/portal.rs @@ -14,7 +14,7 @@ use vello::Scene; use crate::widget::{Axis, ScrollBar, WidgetMut}; use crate::{ AccessCtx, AccessEvent, BoxConstraints, ComposeCtx, EventCtx, LayoutCtx, PaintCtx, - PointerEvent, RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, WidgetPod, + PointerEvent, QueryCtx, RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, WidgetPod, }; // TODO - refactor - see https://github.com/linebender/xilem/issues/366 @@ -467,8 +467,8 @@ impl Widget for Portal { ] } - fn make_trace_span(&self) -> Span { - trace_span!("Portal") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Portal", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/progress_bar.rs b/masonry/src/widget/progress_bar.rs index 2e09f1f04..c4b4f3933 100644 --- a/masonry/src/widget/progress_bar.rs +++ b/masonry/src/widget/progress_bar.rs @@ -14,7 +14,7 @@ use crate::text::{ArcStr, TextLayout}; use crate::widget::WidgetMut; use crate::{ theme, AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, - PointerEvent, RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, + PointerEvent, QueryCtx, RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, }; /// A progress bar @@ -191,8 +191,8 @@ impl Widget for ProgressBar { smallvec![] } - fn make_trace_span(&self) -> Span { - trace_span!("ProgressBar") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("ProgressBar", id = ctx.widget_id().trace()) } fn get_debug_text(&self) -> Option { diff --git a/masonry/src/widget/prose.rs b/masonry/src/widget/prose.rs index aaae1fb63..a1609b04f 100644 --- a/masonry/src/widget/prose.rs +++ b/masonry/src/widget/prose.rs @@ -283,8 +283,8 @@ impl Widget for Prose { SmallVec::new() } - fn make_trace_span(&self) -> Span { - trace_span!("Prose") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Prose", id = ctx.widget_id().trace()) } fn get_debug_text(&self) -> Option { diff --git a/masonry/src/widget/root_widget.rs b/masonry/src/widget/root_widget.rs index 12f379fc0..3cd75c522 100644 --- a/masonry/src/widget/root_widget.rs +++ b/masonry/src/widget/root_widget.rs @@ -9,7 +9,7 @@ use vello::Scene; use crate::widget::{WidgetMut, WidgetPod}; use crate::{ - AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, + AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, QueryCtx, RegisterCtx, Size, TextEvent, Widget, WidgetId, }; @@ -65,7 +65,7 @@ impl Widget for RootWidget { smallvec![self.pod.id()] } - fn make_trace_span(&self) -> Span { - trace_span!("RootWidget") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("RootWidget", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/scroll_bar.rs b/masonry/src/widget/scroll_bar.rs index 9cfc4d975..a8dd2ca39 100644 --- a/masonry/src/widget/scroll_bar.rs +++ b/masonry/src/widget/scroll_bar.rs @@ -13,7 +13,8 @@ use crate::paint_scene_helpers::{fill_color, stroke}; use crate::widget::{Axis, WidgetMut}; use crate::{ theme, AccessCtx, AccessEvent, AllowRawMut, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, - Point, PointerEvent, RegisterCtx, Size, TextEvent, Update, UpdateCtx, Widget, WidgetId, + Point, PointerEvent, QueryCtx, RegisterCtx, Size, TextEvent, Update, UpdateCtx, Widget, + WidgetId, }; // RULES @@ -223,8 +224,8 @@ impl Widget for ScrollBar { SmallVec::new() } - fn make_trace_span(&self) -> Span { - trace_span!("ScrollBar") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("ScrollBar", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/sized_box.rs b/masonry/src/widget/sized_box.rs index a7c2019f0..a2d3c1de6 100644 --- a/masonry/src/widget/sized_box.rs +++ b/masonry/src/widget/sized_box.rs @@ -14,7 +14,7 @@ use crate::paint_scene_helpers::stroke; use crate::widget::{WidgetMut, WidgetPod}; use crate::{ AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent, - RegisterCtx, Size, TextEvent, Widget, WidgetId, + QueryCtx, RegisterCtx, Size, TextEvent, Widget, WidgetId, }; // FIXME - Improve all doc in this module ASAP. @@ -401,8 +401,8 @@ impl Widget for SizedBox { } } - fn make_trace_span(&self) -> Span { - trace_span!("SizedBox") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("SizedBox", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/spinner.rs b/masonry/src/widget/spinner.rs index 5dcc90e6e..9fa374837 100644 --- a/masonry/src/widget/spinner.rs +++ b/masonry/src/widget/spinner.rs @@ -14,7 +14,8 @@ use vello::Scene; use crate::widget::WidgetMut; use crate::{ theme, AccessCtx, AccessEvent, BoxConstraints, Color, EventCtx, LayoutCtx, PaintCtx, Point, - PointerEvent, RegisterCtx, Size, TextEvent, Update, UpdateCtx, Vec2, Widget, WidgetId, + PointerEvent, QueryCtx, RegisterCtx, Size, TextEvent, Update, UpdateCtx, Vec2, Widget, + WidgetId, }; /// An animated spinner widget for showing a loading state. @@ -148,8 +149,8 @@ impl Widget for Spinner { SmallVec::new() } - fn make_trace_span(&self) -> Span { - trace_span!("Spinner") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Spinner", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/split.rs b/masonry/src/widget/split.rs index f82833c0b..40e20f2f6 100644 --- a/masonry/src/widget/split.rs +++ b/masonry/src/widget/split.rs @@ -540,8 +540,8 @@ impl Widget for Split { smallvec![self.child1.id(), self.child2.id()] } - fn make_trace_span(&self) -> Span { - trace_span!("Split") + fn make_trace_span(&self, ctx: &QueryCtx) -> Span { + trace_span!("Split", id = ctx.widget_id().trace()) } } diff --git a/masonry/src/widget/textbox.rs b/masonry/src/widget/textbox.rs index 9268ba572..7efc4712d 100644 --- a/masonry/src/widget/textbox.rs +++ b/masonry/src/widget/textbox.rs @@ -332,8 +332,8 @@ impl Widget for Textbox { true } - fn make_trace_span(&self) -> Span { - trace_span!("Textbox") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("Textbox", id = ctx.widget_id().trace()) } fn get_debug_text(&self) -> Option { diff --git a/masonry/src/widget/variable_label.rs b/masonry/src/widget/variable_label.rs index 4589a7d55..1afa6f6db 100644 --- a/masonry/src/widget/variable_label.rs +++ b/masonry/src/widget/variable_label.rs @@ -18,7 +18,7 @@ use vello::Scene; use crate::text::{ArcStr, Hinting, TextBrush, TextLayout}; use crate::widget::{LineBreaking, WidgetMut}; use crate::{ - AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, + AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, PointerEvent, QueryCtx, RegisterCtx, TextEvent, Update, UpdateCtx, Widget, WidgetId, }; @@ -411,8 +411,8 @@ impl Widget for VariableLabel { SmallVec::new() } - fn make_trace_span(&self) -> Span { - trace_span!("VariableLabel") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("VariableLabel", id = ctx.widget_id().trace()) } fn get_debug_text(&self) -> Option { diff --git a/masonry/src/widget/widget.rs b/masonry/src/widget/widget.rs index efb927705..1e5f62089 100644 --- a/masonry/src/widget/widget.rs +++ b/masonry/src/widget/widget.rs @@ -10,6 +10,7 @@ use std::sync::atomic::{AtomicU64, Ordering}; use accesskit::{NodeBuilder, Role}; use cursor_icon::CursorIcon; use smallvec::SmallVec; +use tracing::field::DisplayValue; use tracing::{trace_span, Span}; use vello::Scene; @@ -45,6 +46,12 @@ use crate::{ #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] pub struct WidgetId(pub(crate) NonZeroU64); +impl WidgetId { + pub fn trace(self) -> DisplayValue { + tracing::field::display(self) + } +} + // TODO - Add tutorial: implementing a widget - See https://github.com/linebender/xilem/issues/376 /// The trait implemented by all widgets. /// @@ -208,8 +215,12 @@ pub trait Widget: AsAny { /// widget visited, and popped when control flow goes back to the parent. This method /// returns a static span (that you can use to filter traces and logs). // TODO: Make include the widget's id? - fn make_trace_span(&self) -> Span { - trace_span!("Widget", r#type = self.short_type_name()) + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!( + "Widget", + r#type = self.short_type_name(), + id = ctx.widget_id().trace() + ) } /// Return a small string representing important info about this widget instance. @@ -469,8 +480,8 @@ impl Widget for Box { self.deref().accepts_text_input() } - fn make_trace_span(&self) -> Span { - self.deref().make_trace_span() + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + self.deref().make_trace_span(ctx) } fn get_debug_text(&self) -> Option { diff --git a/xilem/src/any_view.rs b/xilem/src/any_view.rs index d2bd06f34..a2e32700b 100644 --- a/xilem/src/any_view.rs +++ b/xilem/src/any_view.rs @@ -5,7 +5,7 @@ use accesskit::{NodeBuilder, Role}; use masonry::widget::WidgetMut; use masonry::{ AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent, - RegisterCtx, Size, TextEvent, Widget, WidgetId, WidgetPod, + QueryCtx, RegisterCtx, Size, TextEvent, Widget, WidgetId, WidgetPod, }; use smallvec::{smallvec, SmallVec}; use tracing::{trace_span, Span}; @@ -100,7 +100,7 @@ impl Widget for DynWidget { smallvec![self.inner.id()] } - fn make_trace_span(&self) -> Span { - trace_span!("DynWidget") + fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span { + trace_span!("DynWidget", id = ctx.widget_id().trace()) } }