diff --git a/masonry/src/contexts.rs b/masonry/src/contexts.rs index ae60fddf0..ad249530d 100644 --- a/masonry/src/contexts.rs +++ b/masonry/src/contexts.rs @@ -10,12 +10,14 @@ use dpi::LogicalPosition; use parley::{FontContext, LayoutContext}; use tracing::{trace, warn}; use vello::kurbo::Vec2; +use vello::peniko::Color; use winit::window::ResizeDirection; use crate::action::Action; use crate::passes::layout::run_layout_on; use crate::render_root::{MutateCallback, RenderRootSignal, RenderRootState}; use crate::text::TextBrush; +use crate::theme::get_debug_color; use crate::tree_arena::{ArenaMutChildren, ArenaRefChildren}; use crate::widget::{WidgetMut, WidgetRef, WidgetState}; use crate::{AllowRawMut, BoxConstraints, Insets, Point, Rect, Size, Widget, WidgetId, WidgetPod}; @@ -226,6 +228,11 @@ impl_context_method!( self.widget_state.layout_rect() } + /// The offset of the baseline relative to the bottom of the widget. + pub fn baseline_offset(&self) -> f64 { + self.widget_state.baseline_offset + } + /// The origin of the widget in window coordinates, relative to the top left corner of the /// content area. pub fn window_origin(&self) -> Point { @@ -1123,6 +1130,29 @@ impl_context_method!(LayoutCtx<'_>, PaintCtx<'_>, { } }); +impl PaintCtx<'_> { + /// Whether debug paint is enabled. + /// + /// If this property is set, your widget may draw additional debug information + /// (such as the position of the text baseline). + /// These should normally use the [debug color][Self::debug_color] for this widget. + /// Please note that when debug painting is enabled, each widget's layout boundaries are + /// outlined by Masonry, so you should avoid duplicating that. + /// + /// Debug paint can be enabled by setting the environment variable `MASONRY_DEBUG_PAINT`. + pub fn debug_paint_enabled(&self) -> bool { + self.debug_paint + } + + /// A color used for debug painting in this widget. + /// + /// This is normally used to paint additional debugging information + /// when debug paint is enabled, see [`Self::debug_paint_enabled`]. + pub fn debug_color(&self) -> Color { + get_debug_color(self.widget_id().to_raw()) + } +} + // --- MARK: RAW WRAPPERS --- macro_rules! impl_get_raw { ($SomeCtx:tt) => { diff --git a/masonry/src/theme.rs b/masonry/src/theme.rs index 9241413b8..2af8034a6 100644 --- a/masonry/src/theme.rs +++ b/masonry/src/theme.rs @@ -79,6 +79,10 @@ static DEBUG_COLOR: &[Color] = &[ Color::rgb8(0, 0, 0), ]; +/// A color used for debug painting. +/// +/// The same color is always returned given the same id, usually the id of a widget. +/// When painting a widget, [`PaintCtx::debug_color`][crate::contexts::PaintCtx::debug_color] is typically used instead. pub fn get_debug_color(id: u64) -> Color { let color_num = id as usize % DEBUG_COLOR.len(); DEBUG_COLOR[color_num] diff --git a/masonry/src/widget/flex.rs b/masonry/src/widget/flex.rs index 5b709b253..dbde2ab8b 100644 --- a/masonry/src/widget/flex.rs +++ b/masonry/src/widget/flex.rs @@ -10,7 +10,6 @@ use vello::kurbo::common::FloatExt; use vello::kurbo::{Affine, Line, Stroke, Vec2}; use vello::Scene; -use crate::theme::get_debug_color; use crate::widget::WidgetMut; use crate::{ AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent, @@ -1178,9 +1177,9 @@ impl Widget for Flex { fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut Scene) { // paint the baseline if we're debugging layout - if ctx.debug_paint && ctx.widget_state.baseline_offset != 0.0 { - let color = get_debug_color(ctx.widget_id().to_raw()); - let my_baseline = ctx.size().height - ctx.widget_state.baseline_offset; + if ctx.debug_paint_enabled() && ctx.baseline_offset() != 0.0 { + let color = ctx.debug_color(); + let my_baseline = ctx.size().height - ctx.baseline_offset(); let line = Line::new((0.0, my_baseline), (ctx.size().width, my_baseline)); let stroke_style = Stroke::new(1.0).with_dashes(0., [4.0, 4.0]); diff --git a/masonry/src/widget/grid.rs b/masonry/src/widget/grid.rs index 02d7f6610..400b3a979 100644 --- a/masonry/src/widget/grid.rs +++ b/masonry/src/widget/grid.rs @@ -7,7 +7,6 @@ use tracing::{trace_span, Span}; use vello::kurbo::{Affine, Line, Stroke}; use vello::Scene; -use crate::theme::get_debug_color; use crate::widget::WidgetMut; use crate::{ AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent, @@ -287,9 +286,9 @@ impl Widget for Grid { fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut Scene) { // paint the baseline if we're debugging layout - if ctx.debug_paint && ctx.widget_state.baseline_offset != 0.0 { - let color = get_debug_color(ctx.widget_id().to_raw()); - let my_baseline = ctx.size().height - ctx.widget_state.baseline_offset; + if ctx.debug_paint_enabled() && ctx.baseline_offset() != 0.0 { + let color = ctx.debug_color(); + let my_baseline = ctx.size().height - ctx.baseline_offset(); let line = Line::new((0.0, my_baseline), (ctx.size().width, my_baseline)); let stroke_style = Stroke::new(1.0).with_dashes(0., [4.0, 4.0]);