Skip to content

Commit e6e980d

Browse files
Improve masonry docs (linebender#1059)
This fixes some things that have bitrotted due to API and other changes. It also removes usages of `masonry_winit` where `masonry` should be used instead. Dependencies like `kurbo`, `peniko`, `smallvec`, and `vello` should be referenced via their re-exports so that code outside of Masonry doesn't have to keep track of the versions and maintain their own dependencies.
1 parent bfa12b8 commit e6e980d

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

masonry/src/doc/02_implementing_widget.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ This widget has a size, a color, and will notify Masonry when the user left-clic
6161
First, let's create our struct:
6262

6363
```rust,ignore
64-
use vello::kurbo::Size;
65-
use vello::peniko::Color;
64+
use masonry::kurbo::Size;
65+
use masonry::peniko::Color;
6666
6767
struct ColorRectangle {
6868
size: Size,
@@ -85,15 +85,15 @@ Note that we store a size, and not a position: our widget's position is managed
8585
First we implement event methods:
8686

8787
```rust,ignore
88-
use masonry_winit::core::{
89-
Widget, EventCtx, PointerEvent, TextEvent, AccessEvent, Action
88+
use masonry::core::{
89+
AccessEvent, Action, EventCtx, PointerButton, PointerEvent, PropertiesMut, TextEvent, Widget
9090
};
9191
9292
impl Widget for ColorRectangle {
9393
fn on_pointer_event(&mut self, ctx: &mut EventCtx<'_>, _props: &mut PropertiesMut<'_>, event: &PointerEvent) {
9494
match event {
95-
PointerEvent::PointerDown(PointerButton::Primary, _) => {
96-
ctx.submit_action(Action::ButtonPressed(PointerButton::Primary));
95+
PointerEvent::Down { button: Some(PointerButton::Primary), .. } => {
96+
ctx.submit_action(Action::ButtonPressed(Some(PointerButton::Primary)));
9797
}
9898
_ => {},
9999
}
@@ -104,7 +104,7 @@ impl Widget for ColorRectangle {
104104
fn on_access_event(&mut self, ctx: &mut EventCtx<'_>, _props: &mut PropertiesMut<'_>, event: &AccessEvent) {
105105
match event.action {
106106
accesskit::Action::Click => {
107-
ctx.submit_action(Action::ButtonPressed(PointerButton::Primary));
107+
ctx.submit_action(Action::ButtonPressed(Some(PointerButton::Primary)));
108108
}
109109
_ => {}
110110
}
@@ -129,8 +129,8 @@ We don't handle any text events.
129129
Since our widget isn't animated and doesn't react to changes in status, we can leave the `on_anim_frame` and `update` implementations empty:
130130

131131
```rust,ignore
132-
use masonry_winit::core::{
133-
UpdateCtx, Update,
132+
use masonry::core::{
133+
PropertiesMut, UpdateCtx, Update, Widget
134134
};
135135
136136
impl Widget for ColorRectangle {
@@ -148,9 +148,10 @@ impl Widget for ColorRectangle {
148148
Next we implement layout:
149149

150150
```rust,ignore
151-
use masonry_winit::core::{
152-
LayoutCtx, BoxConstraints
151+
use masonry::core::{
152+
BoxConstraints, LayoutCtx, PropertiesMut, Widget
153153
};
154+
use masonry::kurbo::Size;
154155
155156
impl Widget for ColorRectangle {
156157
// ...
@@ -173,11 +174,13 @@ We return our stored size, clamped between the min and max constraints.
173174
Next we write our render methods:
174175

175176
```rust,ignore
176-
use masonry_winit::core::{
177-
PaintCtx, AccessCtx
177+
use masonry::accesskit::{Node, Role};
178+
use masonry::core::{
179+
AccessCtx, PaintCtx, PropertiesRef, Widget
178180
};
179-
use vello::Scene;
180-
use accesskit::{Node, Role};
181+
use masonry::kurbo::Affine;
182+
use masonry::peniko::Fill;
183+
use masonry::vello::Scene;
181184
182185
impl Widget for ColorRectangle {
183186
// ...
@@ -239,13 +242,14 @@ Pragmatically, if you're not sure about what a certain value means or how to imp
239242
We also write a `make_trace_span()` method, which is useful for debugging with the [tracing](https://docs.rs/tracing/latest/tracing/) framework.
240243

241244
```rust,ignore
245+
use masonry::core::{QueryCtx, Widget};
242246
use tracing::{trace_span, Span};
243247
244248
impl Widget for ColorRectangle {
245249
// ...
246250
247-
fn make_trace_span(&self) -> Span {
248-
trace_span!("ColorRectangle")
251+
fn make_trace_span(&self, ctx: &QueryCtx<'_>) -> Span {
252+
trace_span!("ColorRectangle", id = ctx.widget_id().trace())
249253
}
250254
251255
// ...
@@ -255,10 +259,10 @@ impl Widget for ColorRectangle {
255259
And last, we stub in some additional methods:
256260

257261
```rust,ignore
258-
use masonry_winit::core::{
259-
RegisterCtx, WidgetId
262+
use masonry::core::{
263+
RegisterCtx, Widget, WidgetId
260264
};
261-
use smallvec::SmallVec;
265+
use masonry::smallvec::SmallVec;
262266
263267
impl Widget for ColorRectangle {
264268
// ...

masonry/src/doc/03_implementing_container_widget.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ A container widget needs to pay special attention to these methods:
5050
trait Widget {
5151
// ...
5252
53-
fn layout(&mut self, ctx: &mut LayoutCtx<'_>) -> Size;
53+
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &mut PropertiesMut<'_>, bc: &BoxConstraints) -> Size;
5454
fn compose(&mut self, ctx: &mut ComposeCtx<'_>);
5555
5656
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>);
@@ -78,9 +78,10 @@ When debug assertions are on, Masonry will actively try to detect cases where yo
7878
For our `VerticalStack`, we'll lay out our children in a vertical line, with a gap between each child; we give each child an equal share of the available height:
7979

8080
```rust,ignore
81-
use masonry_winit::core::{
82-
LayoutCtx, BoxConstraints
81+
use masonry::core::{
82+
BoxConstraints, LayoutCtx, PropertiesMut, Widget
8383
};
84+
use masonry::kurbo::{Point, Size};
8485
8586
impl Widget for VerticalStack {
8687
// ...
@@ -131,8 +132,8 @@ For instance, if a widget in a list changes size, its siblings and parents must
131132
In the case of our `VerticalStack`, we don't implement any transform-only changes, so we don't need to do anything in compose:
132133

133134
```rust,ignore
134-
use masonry_winit::core::{
135-
LayoutCtx, BoxConstraints
135+
use masonry::core::{
136+
BoxConstraints, ComposeCtx, Widget
136137
};
137138
138139
impl Widget for VerticalStack {
@@ -147,8 +148,8 @@ impl Widget for VerticalStack {
147148
The `register_children` method must call [`RegisterCtx::register_child`] for each child:
148149

149150
```rust,ignore
150-
use masonry_winit::{
151-
Widget, RegisterCtx
151+
use masonry::core::{
152+
RegisterCtx, Widget
152153
};
153154
154155
impl Widget for VerticalStack {

masonry/src/doc/04_testing_widget.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ First, let's write a test module with a first unit test:
3131
#[cfg(test)]
3232
mod tests {
3333
use insta::assert_debug_snapshot;
34-
use masonry_winit::testing::{widget_ids, TestHarness, TestWidgetExt};
35-
use masonry_winit::theme::default_property_set;
34+
use masonry::testing::{widget_ids, TestHarness, TestWidgetExt};
35+
use masonry::theme::default_property_set;
3636
3737
use super::*;
3838
@@ -78,7 +78,7 @@ Let's add a visual test:
7878

7979
```rust,ignore
8080
// ...
81-
use masonry_winit::assert_render_snapshot;
81+
use masonry::assert_render_snapshot;
8282
8383
#[test]
8484
fn simple_rect() {
@@ -191,7 +191,7 @@ Since our `WidgetRectangle` doesn't emit actions, let's look at a unit test for
191191
harness.mouse_click_on(button_id);
192192
assert_eq!(
193193
harness.pop_action(),
194-
Some((Action::ButtonPressed(PointerButton::Primary), button_id))
194+
Some((Action::ButtonPressed(Some(PointerButton::Primary)), button_id))
195195
);
196196
}
197197
```

masonry/src/doc/04b_widget_properties.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ Properties should *not* be used to represent an individual widget's state. The f
8383
With that in mind, let's rewrite our `ColorRectangle` widget to use properties:
8484

8585
```rust,ignore
86-
use masonry_winit::properties::BackgroundColor;
86+
use masonry::properties::BackgroundColor;
8787
8888
impl Widget for ColorRectangle {
8989
// ...
9090
9191
fn paint(&mut self, ctx: &mut PaintCtx<'_>, props: &PropertiesRef<'_>, scene: &mut Scene) {
92-
let color = props.get::<BackgroundColor>().unwrap_or(masonry_winit::palette::css::WHITE);
92+
let color = props.get::<BackgroundColor>().unwrap_or(masonry::palette::css::WHITE);
9393
let rect = ctx.size().to_rect();
9494
scene.fill(
9595
Fill::NonZero,
@@ -111,7 +111,7 @@ The most idiomatic way to set properties is through `WidgetMut`:
111111
```rust,ignore
112112
let color_rectangle_mut: WidgetMut<ColorRectangle> = ...;
113113
114-
let bg = BackgroundColor { color: masonry_winit::palette::css::BLUE };
114+
let bg = BackgroundColor { color: masonry::palette::css::BLUE };
115115
116116
color_rectangle_mut.insert_prop(bg);
117117
```

0 commit comments

Comments
 (0)