From 300503ab74a7912a14da72cb9cdc481c5c046f00 Mon Sep 17 00:00:00 2001 From: Olivier FAURE Date: Thu, 16 Jan 2025 16:54:19 +0100 Subject: [PATCH 1/7] Add screenshots to documentation Create include_screenshot macro --- masonry/src/testing/screenshots.rs | 36 ++++++++++++++++++++++++++++++ masonry/src/widget/align.rs | 3 +++ 2 files changed, 39 insertions(+) diff --git a/masonry/src/testing/screenshots.rs b/masonry/src/testing/screenshots.rs index 7d27efe6d..9a2479371 100644 --- a/masonry/src/testing/screenshots.rs +++ b/masonry/src/testing/screenshots.rs @@ -6,6 +6,42 @@ use image::{GenericImageView as _, RgbImage}; use nv_flip::{FlipImageRgb8, DEFAULT_PIXELS_PER_DEGREE}; +#[cfg(docsrs)] +#[doc(hidden)] +#[macro_export] +macro_rules! include_screenshot { + ($path:literal $(, $caption:literal)? $(,)?) => { + concat!( + "![", $($caption,)? "]", + "(", "https://raw.githubusercontent.com/linebender/xilem/", + "masonry-v", env!("CARGO_PKG_VERSION"), "/masonry/src/", $path, + ")", + ) + }; +} + +#[cfg(not(docsrs))] +#[doc(hidden)] +#[macro_export] +/// Macro used to create markdown img tag, with a different URL when uploading to docs.rs. +macro_rules! include_screenshot { + ($path:literal $(, $caption:literal)? $(,)?) => { + concat!( + "![", $($caption,)? "]", + "(", env!("CARGO_MANIFEST_DIR"), "/src/", $path, ")", + ) + }; +} + +// [!Image alt](link) + +// https://raw.githubusercontent.com/linebender/xilem/7f40266bd831c3f8e715bf7af325e3a53e046612/masonry/src/widget/screenshots/masonry__widget__align__tests__centered.png + +#[cfg(FALSE)] +include_screenshot!("hello"); + +#[cfg_attr(docsrs, doc = r" This is a doc comment.")] +#[cfg_attr(not(docsrs), doc = r" This is a doc comment.")] pub(crate) fn get_image_diff(ref_image: &RgbImage, new_image: &RgbImage) -> Option { assert_eq!( (ref_image.width(), ref_image.height()), diff --git a/masonry/src/widget/align.rs b/masonry/src/widget/align.rs index f6d7ec437..419002202 100644 --- a/masonry/src/widget/align.rs +++ b/masonry/src/widget/align.rs @@ -14,6 +14,7 @@ use tracing::{trace_span, Span}; use vello::Scene; use crate::contexts::AccessCtx; +use crate::include_screenshot; use crate::paint_scene_helpers::UnitPoint; use crate::widget::WidgetPod; use crate::{ @@ -24,6 +25,8 @@ use crate::{ // TODO - Have child widget type as generic argument /// A widget that aligns its child. +/// +#[doc = include_screenshot!("widget/screenshots/masonry__widget__align__tests__right.png", "Right-aligned label")] pub struct Align { align: UnitPoint, child: WidgetPod, From 2b45f6b087455048edee258904fe526c33dd893e Mon Sep 17 00:00:00 2001 From: Olivier FAURE Date: Thu, 23 Jan 2025 16:23:31 +0100 Subject: [PATCH 2/7] Add screenshots to all widgets Remove cruft --- masonry/src/testing/screenshots.rs | 12 +++++------- masonry/src/widget/align.rs | 3 +-- masonry/src/widget/button.rs | 2 ++ masonry/src/widget/checkbox.rs | 2 ++ masonry/src/widget/flex.rs | 2 ++ masonry/src/widget/grid.rs | 3 +++ masonry/src/widget/label.rs | 2 ++ masonry/src/widget/progress_bar.rs | 2 ++ masonry/src/widget/prose.rs | 2 ++ masonry/src/widget/scroll_bar.rs | 3 +++ masonry/src/widget/sized_box.rs | 2 ++ masonry/src/widget/spinner.rs | 2 ++ masonry/src/widget/split.rs | 2 ++ masonry/src/widget/zstack.rs | 2 ++ 14 files changed, 32 insertions(+), 9 deletions(-) diff --git a/masonry/src/testing/screenshots.rs b/masonry/src/testing/screenshots.rs index 9a2479371..127b2be9e 100644 --- a/masonry/src/testing/screenshots.rs +++ b/masonry/src/testing/screenshots.rs @@ -20,6 +20,11 @@ macro_rules! include_screenshot { }; } +// TODO: +// - Check whether the docs.rs trick actually works. +// - Check whether this method will work with cargo rdme. +// - Make better screenshots than the ones we currently use. + #[cfg(not(docsrs))] #[doc(hidden)] #[macro_export] @@ -33,13 +38,6 @@ macro_rules! include_screenshot { }; } -// [!Image alt](link) - -// https://raw.githubusercontent.com/linebender/xilem/7f40266bd831c3f8e715bf7af325e3a53e046612/masonry/src/widget/screenshots/masonry__widget__align__tests__centered.png - -#[cfg(FALSE)] -include_screenshot!("hello"); - #[cfg_attr(docsrs, doc = r" This is a doc comment.")] #[cfg_attr(not(docsrs), doc = r" This is a doc comment.")] pub(crate) fn get_image_diff(ref_image: &RgbImage, new_image: &RgbImage) -> Option { diff --git a/masonry/src/widget/align.rs b/masonry/src/widget/align.rs index 419002202..96d037b8a 100644 --- a/masonry/src/widget/align.rs +++ b/masonry/src/widget/align.rs @@ -14,7 +14,6 @@ use tracing::{trace_span, Span}; use vello::Scene; use crate::contexts::AccessCtx; -use crate::include_screenshot; use crate::paint_scene_helpers::UnitPoint; use crate::widget::WidgetPod; use crate::{ @@ -26,7 +25,7 @@ use crate::{ /// A widget that aligns its child. /// -#[doc = include_screenshot!("widget/screenshots/masonry__widget__align__tests__right.png", "Right-aligned label")] +#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__align__tests__right.png", "Right-aligned label")] pub struct Align { align: UnitPoint, child: WidgetPod, diff --git a/masonry/src/widget/button.rs b/masonry/src/widget/button.rs index 4d9a532e3..805cd772f 100644 --- a/masonry/src/widget/button.rs +++ b/masonry/src/widget/button.rs @@ -26,6 +26,8 @@ const LABEL_INSETS: Insets = Insets::uniform_xy(8., 2.); /// A button with a text label. /// /// Emits [`Action::ButtonPressed`] when pressed. +/// +#[doc = crate::include_screenshot!("widget/screenshots/masonry__widget__button__tests__hello.png", "Button with text label")] pub struct Button { label: WidgetPod