Skip to content

Commit df3107e

Browse files
authored
Add screenshots to documentation (#832)
Create include_screenshot macro
1 parent 8342d8f commit df3107e

26 files changed

+103
-9
lines changed

masonry/examples/calc_masonry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ mod tests {
414414
fn screenshot_test() {
415415
let mut harness = TestHarness::create(build_calc());
416416
assert_debug_snapshot!(harness.root_widget());
417-
assert_render_snapshot!(harness, "calc");
417+
assert_render_snapshot!(harness, "initial_screenshot");
418418

419419
// TODO - Test clicking buttons
420420
}

masonry/examples/custom_widget.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,6 @@ mod tests {
193193

194194
let mut harness = TestHarness::create(CustomWidget(my_string));
195195
assert_debug_snapshot!(harness.root_widget());
196-
assert_render_snapshot!(harness, "custom_widget");
196+
assert_render_snapshot!(harness, "initial_screenshot");
197197
}
198198
}

masonry/examples/grid_masonry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ mod tests {
140140
fn screenshot_test() {
141141
let mut harness = TestHarness::create(make_grid(1.0));
142142
assert_debug_snapshot!(harness.root_widget());
143-
assert_render_snapshot!(harness, "grid_masonry");
143+
assert_render_snapshot!(harness, "initial_screenshot");
144144

145145
// TODO - Test clicking buttons
146146
}

masonry/examples/readme.md

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
# Masonry examples
22

3-
TODO:
3+
## `hello_masonry`
44

5-
- List examples in this folder
6-
- Add screenshot test to each example
7-
- Use the screenshot in this list
5+
Simples possible Masonry app.
6+
7+
## `two_textboxes`
8+
9+
Example used to test text input and text focus.
10+
11+
12+
## `calc_masonry`
13+
14+
![](screenshots/calc_masonry__tests__initial_screenshot.png)
15+
16+
Calculator app.
17+
18+
19+
## `to_do_list`
20+
21+
![](screenshots/to_do_list__tests__initial_screenshot.png)
22+
23+
To-do list app.
24+
25+
26+
## `custom_widget`
27+
28+
![](screenshots/custom_widget__tests__initial_screenshot.png)
29+
30+
Static render showing off Vello's capabilities.
31+
32+
33+
## `grid_masonry`
34+
35+
![](screenshots/grid_masonry__tests__initial_screenshot.png)
36+
37+
Demonstration of the grid layout.
38+
39+
40+
## `simple_image`
41+
42+
![](screenshots/simple_image__tests__initial_screenshot.png)
43+
44+
Simple image example.

masonry/examples/simple_image.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ mod tests {
6363
fn screenshot_test() {
6464
let mut harness = TestHarness::create(make_image());
6565
assert_debug_snapshot!(harness.root_widget());
66-
assert_render_snapshot!(harness, "grid_masonry");
66+
assert_render_snapshot!(harness, "initial_screenshot");
6767
}
6868
}

masonry/examples/to_do_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mod tests {
8888
fn screenshot_test() {
8989
let mut harness = TestHarness::create(make_widget_tree());
9090
assert_debug_snapshot!(harness.root_widget());
91-
assert_render_snapshot!(harness, "to_do_list");
91+
assert_render_snapshot!(harness, "initial_screenshot");
9292

9393
// TODO - Test clicking buttons
9494
// TODO - Test typing text

masonry/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
//! [Druid]: https://crates.io/crates/druid
9797
//! [Xilem]: https://crates.io/crates/xilem
9898
//! [tracing_tracy]: https://crates.io/crates/tracing-tracy
99+
// TODO: Add screenshot. This can't use include_screenshot as that doesn't work with cargo-rdme
100+
// See https://github.com/linebender/xilem/issues/851
99101

100102
// LINEBENDER LINT SET - lib.rs - v1
101103
// See https://linebender.org/wiki/canonical-lints/

masonry/src/testing/screenshots.rs

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66
use image::{GenericImageView as _, RgbImage};
77
use nv_flip::{FlipImageRgb8, DEFAULT_PIXELS_PER_DEGREE};
88

9+
#[cfg(docsrs)]
10+
#[doc(hidden)]
11+
#[macro_export]
12+
macro_rules! include_screenshot {
13+
($path:literal $(, $caption:literal)? $(,)?) => {
14+
concat!(
15+
"![", $($caption,)? "]",
16+
"(", "https://media.githubusercontent.com/media/linebender/xilem/",
17+
"masonry-v", env!("CARGO_PKG_VERSION"), "/masonry/src/", $path,
18+
")",
19+
)
20+
};
21+
}
22+
23+
#[cfg(not(docsrs))]
24+
#[doc(hidden)]
25+
#[macro_export]
26+
/// Macro used to create markdown img tag, with a different URL when uploading to docs.rs.
27+
macro_rules! include_screenshot {
28+
($path:literal $(, $caption:literal)? $(,)?) => {
29+
concat!(
30+
"![", $($caption,)? "]",
31+
"(", env!("CARGO_MANIFEST_DIR"), "/src/", $path, ")",
32+
)
33+
};
34+
}
35+
936
pub(crate) fn get_image_diff(ref_image: &RgbImage, new_image: &RgbImage) -> Option<RgbImage> {
1037
assert_eq!(
1138
(ref_image.width(), ref_image.height()),

masonry/src/widget/align.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use crate::{
2424
// TODO - Have child widget type as generic argument
2525

2626
/// A widget that aligns its child.
27+
///
28+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__align__tests__right.png", "Right-aligned label.")]
2729
pub struct Align {
2830
align: UnitPoint,
2931
child: WidgetPod<dyn Widget>,

masonry/src/widget/button.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const LABEL_INSETS: Insets = Insets::uniform_xy(8., 2.);
2626
/// A button with a text label.
2727
///
2828
/// Emits [`Action::ButtonPressed`] when pressed.
29+
///
30+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__button__tests__hello.png", "Button with text label.")]
2931
pub struct Button {
3032
label: WidgetPod<Label>,
3133
}

masonry/src/widget/checkbox.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::{
1919
};
2020

2121
/// A checkbox that can be toggled.
22+
///
23+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__checkbox__tests__hello_checked.png", "Checkbox with checked state.")]
2224
pub struct Checkbox {
2325
checked: bool,
2426
label: WidgetPod<Label>,

masonry/src/widget/flex.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::{
1919
/// A container with either horizontal or vertical layout.
2020
///
2121
/// This widget is the foundation of most layouts, and is highly configurable.
22+
///
23+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__flex__tests__col_main_axis_spaceAround.png", "Flex column with multiple labels.")]
2224
pub struct Flex {
2325
direction: Axis,
2426
cross_alignment: CrossAxisAlignment,

masonry/src/widget/grid.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use crate::{
1313
QueryCtx, Size, TextEvent, Widget, WidgetId, WidgetPod,
1414
};
1515

16+
/// A widget that arranges its children in a grid.
17+
///
18+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__grid__tests__with_changed_spacing.png", "Grid with buttons of various sizes.")]
1619
pub struct Grid {
1720
children: Vec<Child>,
1821
grid_width: i32,

masonry/src/widget/label.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub enum LineBreaking {
4242
///
4343
/// This is useful for creating interactive widgets which internally
4444
/// need support for displaying text, such as a button.
45+
///
46+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__label__tests__styled_label.png", "Styled label.")]
4547
pub struct Label {
4648
text_layout: Layout<BrushIndex>,
4749
accessibility: LayoutAccessibility,

masonry/src/widget/progress_bar.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::{
1818
};
1919

2020
/// A progress bar.
21+
///
22+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__progress_bar__tests__25_percent_progressbar.png", "25% progress bar.")]
2123
pub struct ProgressBar {
2224
/// A value in the range `[0, 1]` inclusive, where 0 is 0% and 1 is 100% complete.
2325
///

masonry/src/widget/prose.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const PROSE_PADDING: Padding = Padding::horizontal(2.0);
3636
/// as it enables users to copy/paste from the text.
3737
///
3838
/// This widget has no actions.
39+
///
40+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__prose__tests__prose_alignment_flex.png", "Multiple lines with different alignments.")]
3941
pub struct Prose {
4042
text: WidgetPod<TextArea<false>>,
4143

masonry/src/widget/scroll_bar.rs

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use crate::{
2424
// - Document names
2525
// - Document invariants
2626

27+
/// A scrollbar.
28+
///
29+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__scroll_bar__tests__scrollbar_default.png", "Vertical scrollbar.")]
2730
pub struct ScrollBar {
2831
axis: Axis,
2932
pub(crate) cursor_progress: f64,

masonry/src/widget/sized_box.rs

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub struct Padding {
5656
/// If not given a child, `SizedBox` will try to size itself as close to the specified height
5757
/// and width as possible given the parent's constraints. If height or width is not set,
5858
/// it will be treated as zero.
59+
///
60+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__sized_box__tests__label_box_with_outer_padding.png", "Box with blue border, pink background and a child label.")]
5961
pub struct SizedBox {
6062
child: Option<WidgetPod<dyn Widget>>,
6163
width: Option<f64>,

masonry/src/widget/spinner.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use crate::{
2424
/// that has a fixed width and height.
2525
///
2626
/// [`SizedBox`]: crate::widget::SizedBox
27+
///
28+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__spinner__tests__spinner_init.png", "Spinner frame.")]
2729
pub struct Spinner {
2830
t: f64,
2931
color: Color,

masonry/src/widget/split.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use crate::{
2121
// TODO - Have child widget type as generic argument
2222

2323
/// A container containing two other widgets, splitting the area either horizontally or vertically.
24+
///
25+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__split__tests__columns.png", "Split panel with two labels.")]
2426
pub struct Split {
2527
split_axis: Axis,
2628
split_point_chosen: f64,

masonry/src/widget/zstack.rs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub enum ChildAlignment {
3232
///
3333
/// The alignment of how the children are placed can be specified globally using [`with_alignment`][Self::with_alignment].
3434
/// Each child can additionally override the global alignment using [`ChildAlignment::SelfAligned`].
35+
///
36+
#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__zstack__tests__zstack_alignment_default.png", "Red foreground widget on top of blue background widget.")]
3537
#[derive(Default)]
3638
pub struct ZStack {
3739
children: Vec<Child>,

0 commit comments

Comments
 (0)