Skip to content

Commit 72530b2

Browse files
authored
Add getters to masonry widget contexts (#735)
This fixes #728. And update `flex` and `grid` examples to use them. This allows external crates to access these fields for custom widgets.
1 parent 6db696d commit 72530b2

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

masonry/src/contexts.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ use dpi::LogicalPosition;
1010
use parley::{FontContext, LayoutContext};
1111
use tracing::{trace, warn};
1212
use vello::kurbo::Vec2;
13+
use vello::peniko::Color;
1314
use winit::window::ResizeDirection;
1415

1516
use crate::action::Action;
1617
use crate::passes::layout::run_layout_on;
1718
use crate::render_root::{MutateCallback, RenderRootSignal, RenderRootState};
1819
use crate::text::TextBrush;
20+
use crate::theme::get_debug_color;
1921
use crate::tree_arena::{ArenaMutChildren, ArenaRefChildren};
2022
use crate::widget::{WidgetMut, WidgetRef, WidgetState};
2123
use crate::{AllowRawMut, BoxConstraints, Insets, Point, Rect, Size, Widget, WidgetId, WidgetPod};
@@ -226,6 +228,11 @@ impl_context_method!(
226228
self.widget_state.layout_rect()
227229
}
228230

231+
/// The offset of the baseline relative to the bottom of the widget.
232+
pub fn baseline_offset(&self) -> f64 {
233+
self.widget_state.baseline_offset
234+
}
235+
229236
/// The origin of the widget in window coordinates, relative to the top left corner of the
230237
/// content area.
231238
pub fn window_origin(&self) -> Point {
@@ -1123,6 +1130,29 @@ impl_context_method!(LayoutCtx<'_>, PaintCtx<'_>, {
11231130
}
11241131
});
11251132

1133+
impl PaintCtx<'_> {
1134+
/// Whether debug paint is enabled.
1135+
///
1136+
/// If this property is set, your widget may draw additional debug information
1137+
/// (such as the position of the text baseline).
1138+
/// These should normally use the [debug color][Self::debug_color] for this widget.
1139+
/// Please note that when debug painting is enabled, each widget's layout boundaries are
1140+
/// outlined by Masonry, so you should avoid duplicating that.
1141+
///
1142+
/// Debug paint can be enabled by setting the environment variable `MASONRY_DEBUG_PAINT`.
1143+
pub fn debug_paint_enabled(&self) -> bool {
1144+
self.debug_paint
1145+
}
1146+
1147+
/// A color used for debug painting in this widget.
1148+
///
1149+
/// This is normally used to paint additional debugging information
1150+
/// when debug paint is enabled, see [`Self::debug_paint_enabled`].
1151+
pub fn debug_color(&self) -> Color {
1152+
get_debug_color(self.widget_id().to_raw())
1153+
}
1154+
}
1155+
11261156
// --- MARK: RAW WRAPPERS ---
11271157
macro_rules! impl_get_raw {
11281158
($SomeCtx:tt) => {

masonry/src/theme.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ static DEBUG_COLOR: &[Color] = &[
7979
Color::rgb8(0, 0, 0),
8080
];
8181

82+
/// A color used for debug painting.
83+
///
84+
/// The same color is always returned given the same id, usually the id of a widget.
85+
/// When painting a widget, [`PaintCtx::debug_color`][crate::contexts::PaintCtx::debug_color] is typically used instead.
8286
pub fn get_debug_color(id: u64) -> Color {
8387
let color_num = id as usize % DEBUG_COLOR.len();
8488
DEBUG_COLOR[color_num]

masonry/src/widget/flex.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use vello::kurbo::common::FloatExt;
1010
use vello::kurbo::{Affine, Line, Stroke, Vec2};
1111
use vello::Scene;
1212

13-
use crate::theme::get_debug_color;
1413
use crate::widget::WidgetMut;
1514
use crate::{
1615
AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent,
@@ -1178,9 +1177,9 @@ impl Widget for Flex {
11781177

11791178
fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut Scene) {
11801179
// paint the baseline if we're debugging layout
1181-
if ctx.debug_paint && ctx.widget_state.baseline_offset != 0.0 {
1182-
let color = get_debug_color(ctx.widget_id().to_raw());
1183-
let my_baseline = ctx.size().height - ctx.widget_state.baseline_offset;
1180+
if ctx.debug_paint_enabled() && ctx.baseline_offset() != 0.0 {
1181+
let color = ctx.debug_color();
1182+
let my_baseline = ctx.size().height - ctx.baseline_offset();
11841183
let line = Line::new((0.0, my_baseline), (ctx.size().width, my_baseline));
11851184

11861185
let stroke_style = Stroke::new(1.0).with_dashes(0., [4.0, 4.0]);

masonry/src/widget/grid.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use tracing::{trace_span, Span};
77
use vello::kurbo::{Affine, Line, Stroke};
88
use vello::Scene;
99

10-
use crate::theme::get_debug_color;
1110
use crate::widget::WidgetMut;
1211
use crate::{
1312
AccessCtx, AccessEvent, BoxConstraints, EventCtx, LayoutCtx, PaintCtx, Point, PointerEvent,
@@ -287,9 +286,9 @@ impl Widget for Grid {
287286

288287
fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut Scene) {
289288
// paint the baseline if we're debugging layout
290-
if ctx.debug_paint && ctx.widget_state.baseline_offset != 0.0 {
291-
let color = get_debug_color(ctx.widget_id().to_raw());
292-
let my_baseline = ctx.size().height - ctx.widget_state.baseline_offset;
289+
if ctx.debug_paint_enabled() && ctx.baseline_offset() != 0.0 {
290+
let color = ctx.debug_color();
291+
let my_baseline = ctx.size().height - ctx.baseline_offset();
293292
let line = Line::new((0.0, my_baseline), (ctx.size().width, my_baseline));
294293

295294
let stroke_style = Stroke::new(1.0).with_dashes(0., [4.0, 4.0]);

0 commit comments

Comments
 (0)