From ef95dc6b439a3275579ba316231d350702809716 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Tue, 17 Dec 2024 01:03:16 -0500 Subject: [PATCH] Sized Box: Use a brush for the border This allows for a richer appearance by using any valid brush for the border. --- masonry/src/widget/sized_box.rs | 20 ++++++++++---------- xilem/src/view/sized_box.rs | 15 ++++++++------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/masonry/src/widget/sized_box.rs b/masonry/src/widget/sized_box.rs index af8f874d5..1154d06c1 100644 --- a/masonry/src/widget/sized_box.rs +++ b/masonry/src/widget/sized_box.rs @@ -7,7 +7,7 @@ use accesskit::{Node, Role}; use smallvec::{smallvec, SmallVec}; use tracing::{trace_span, warn, Span}; use vello::kurbo::{Affine, RoundedRectRadii}; -use vello::peniko::{Brush, Color, Fill}; +use vello::peniko::{Brush, Fill}; use vello::Scene; use crate::paint_scene_helpers::stroke; @@ -22,7 +22,7 @@ use crate::{ /// Something that can be used as the border for a widget. struct BorderStyle { width: f64, - color: Color, + brush: Brush, } /// Padding specifies the spacing between the edges of the box and the child view. @@ -292,10 +292,10 @@ impl SizedBox { self } - /// Builder-style method for painting a border around the widget with a color and width. - pub fn border(mut self, color: impl Into, width: impl Into) -> Self { + /// Builder-style method for painting a border around the widget with a brush and width. + pub fn border(mut self, brush: impl Into, width: impl Into) -> Self { self.border = Some(BorderStyle { - color: color.into(), + brush: brush.into(), width: width.into(), }); self @@ -386,14 +386,14 @@ impl SizedBox { this.ctx.request_paint_only(); } - /// Paint a border around the widget with a color and width. + /// Paint a border around the widget with a brush and width. pub fn set_border( this: &mut WidgetMut<'_, Self>, - color: impl Into, + brush: impl Into, width: impl Into, ) { this.widget.border = Some(BorderStyle { - color: color.into(), + brush: brush.into(), width: width.into(), }); this.ctx.request_layout(); @@ -547,7 +547,7 @@ impl Widget for SizedBox { .to_rect() .inset(border_width / -2.0) .to_rounded_rect(corner_radius); - stroke(scene, &border_rect, border.color, border_width); + stroke(scene, &border_rect, &border.brush, border_width); }; } @@ -574,7 +574,7 @@ impl Widget for SizedBox { #[cfg(test)] mod tests { use insta::assert_debug_snapshot; - use vello::peniko::Gradient; + use vello::peniko::{Color, Gradient}; use super::*; use crate::assert_render_snapshot; diff --git a/xilem/src/view/sized_box.rs b/xilem/src/view/sized_box.rs index 78c98f1ae..2bf9fece5 100644 --- a/xilem/src/view/sized_box.rs +++ b/xilem/src/view/sized_box.rs @@ -6,7 +6,7 @@ use std::marker::PhantomData; use masonry::widget; pub use masonry::widget::Padding; use vello::kurbo::RoundedRectRadii; -use vello::peniko::{Brush, Color}; +use vello::peniko::Brush; use crate::core::{DynMessage, Mut, View, ViewId, ViewMarker}; use crate::{Pod, ViewCtx, WidgetView}; @@ -92,16 +92,17 @@ impl SizedBox { /// This can be passed anything which can be represented by a [`Brush`]; /// notably, it can be any [`Color`], any gradient, or an [`Image`]. /// + /// [`Color`]: vello::peniko::Color /// [`Image`]: vello::peniko::Image pub fn background(mut self, brush: impl Into) -> Self { self.background = Some(brush.into()); self } - /// Builder-style method for painting a border around the widget with a color and width. - pub fn border(mut self, color: impl Into, width: impl Into) -> Self { + /// Builder-style method for painting a border around the widget with a brush and width. + pub fn border(mut self, brush: impl Into, width: impl Into) -> Self { self.border = Some(BorderStyle { - color: color.into(), + brush: brush.into(), width: width.into(), }); self @@ -141,7 +142,7 @@ where widget = widget.background(background.clone()); } if let Some(border) = &self.border { - widget = widget.border(border.color, border.width); + widget = widget.border(border.brush.clone(), border.width); } (ctx.new_pod(widget), child_state) } @@ -176,7 +177,7 @@ where if self.border != prev.border { match &self.border { Some(border) => { - widget::SizedBox::set_border(&mut element, border.color, border.width); + widget::SizedBox::set_border(&mut element, border.brush.clone(), border.width); } None => widget::SizedBox::clear_border(&mut element), } @@ -221,5 +222,5 @@ where #[derive(PartialEq)] struct BorderStyle { width: f64, - color: Color, + brush: Brush, }