Skip to content

Commit

Permalink
Sized Box: Use a brush for the border (#795)
Browse files Browse the repository at this point in the history
This allows for a richer appearance by using any valid brush for the
border.
  • Loading branch information
waywardmonkeys authored Dec 17, 2024
1 parent 692b080 commit c4d5950
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
20 changes: 10 additions & 10 deletions masonry/src/widget/sized_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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<Color>, width: impl Into<f64>) -> Self {
/// Builder-style method for painting a border around the widget with a brush and width.
pub fn border(mut self, brush: impl Into<Brush>, width: impl Into<f64>) -> Self {
self.border = Some(BorderStyle {
color: color.into(),
brush: brush.into(),
width: width.into(),
});
self
Expand Down Expand Up @@ -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<Color>,
brush: impl Into<Brush>,
width: impl Into<f64>,
) {
this.widget.border = Some(BorderStyle {
color: color.into(),
brush: brush.into(),
width: width.into(),
});
this.ctx.request_layout();
Expand Down Expand Up @@ -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);
};
}

Expand All @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions xilem/src/view/sized_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -92,16 +92,17 @@ impl<V, State, Action> SizedBox<V, State, Action> {
/// 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<Brush>) -> 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<Color>, width: impl Into<f64>) -> Self {
/// Builder-style method for painting a border around the widget with a brush and width.
pub fn border(mut self, brush: impl Into<Brush>, width: impl Into<f64>) -> Self {
self.border = Some(BorderStyle {
color: color.into(),
brush: brush.into(),
width: width.into(),
});
self
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -221,5 +222,5 @@ where
#[derive(PartialEq)]
struct BorderStyle {
width: f64,
color: Color,
brush: Brush,
}

0 comments on commit c4d5950

Please sign in to comment.