Skip to content

Commit c4d5950

Browse files
Sized Box: Use a brush for the border (linebender#795)
This allows for a richer appearance by using any valid brush for the border.
1 parent 692b080 commit c4d5950

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

masonry/src/widget/sized_box.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use accesskit::{Node, Role};
77
use smallvec::{smallvec, SmallVec};
88
use tracing::{trace_span, warn, Span};
99
use vello::kurbo::{Affine, RoundedRectRadii};
10-
use vello::peniko::{Brush, Color, Fill};
10+
use vello::peniko::{Brush, Fill};
1111
use vello::Scene;
1212

1313
use crate::paint_scene_helpers::stroke;
@@ -22,7 +22,7 @@ use crate::{
2222
/// Something that can be used as the border for a widget.
2323
struct BorderStyle {
2424
width: f64,
25-
color: Color,
25+
brush: Brush,
2626
}
2727

2828
/// Padding specifies the spacing between the edges of the box and the child view.
@@ -292,10 +292,10 @@ impl SizedBox {
292292
self
293293
}
294294

295-
/// Builder-style method for painting a border around the widget with a color and width.
296-
pub fn border(mut self, color: impl Into<Color>, width: impl Into<f64>) -> Self {
295+
/// Builder-style method for painting a border around the widget with a brush and width.
296+
pub fn border(mut self, brush: impl Into<Brush>, width: impl Into<f64>) -> Self {
297297
self.border = Some(BorderStyle {
298-
color: color.into(),
298+
brush: brush.into(),
299299
width: width.into(),
300300
});
301301
self
@@ -386,14 +386,14 @@ impl SizedBox {
386386
this.ctx.request_paint_only();
387387
}
388388

389-
/// Paint a border around the widget with a color and width.
389+
/// Paint a border around the widget with a brush and width.
390390
pub fn set_border(
391391
this: &mut WidgetMut<'_, Self>,
392-
color: impl Into<Color>,
392+
brush: impl Into<Brush>,
393393
width: impl Into<f64>,
394394
) {
395395
this.widget.border = Some(BorderStyle {
396-
color: color.into(),
396+
brush: brush.into(),
397397
width: width.into(),
398398
});
399399
this.ctx.request_layout();
@@ -547,7 +547,7 @@ impl Widget for SizedBox {
547547
.to_rect()
548548
.inset(border_width / -2.0)
549549
.to_rounded_rect(corner_radius);
550-
stroke(scene, &border_rect, border.color, border_width);
550+
stroke(scene, &border_rect, &border.brush, border_width);
551551
};
552552
}
553553

@@ -574,7 +574,7 @@ impl Widget for SizedBox {
574574
#[cfg(test)]
575575
mod tests {
576576
use insta::assert_debug_snapshot;
577-
use vello::peniko::Gradient;
577+
use vello::peniko::{Color, Gradient};
578578

579579
use super::*;
580580
use crate::assert_render_snapshot;

xilem/src/view/sized_box.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::marker::PhantomData;
66
use masonry::widget;
77
pub use masonry::widget::Padding;
88
use vello::kurbo::RoundedRectRadii;
9-
use vello::peniko::{Brush, Color};
9+
use vello::peniko::Brush;
1010

1111
use crate::core::{DynMessage, Mut, View, ViewId, ViewMarker};
1212
use crate::{Pod, ViewCtx, WidgetView};
@@ -92,16 +92,17 @@ impl<V, State, Action> SizedBox<V, State, Action> {
9292
/// This can be passed anything which can be represented by a [`Brush`];
9393
/// notably, it can be any [`Color`], any gradient, or an [`Image`].
9494
///
95+
/// [`Color`]: vello::peniko::Color
9596
/// [`Image`]: vello::peniko::Image
9697
pub fn background(mut self, brush: impl Into<Brush>) -> Self {
9798
self.background = Some(brush.into());
9899
self
99100
}
100101

101-
/// Builder-style method for painting a border around the widget with a color and width.
102-
pub fn border(mut self, color: impl Into<Color>, width: impl Into<f64>) -> Self {
102+
/// Builder-style method for painting a border around the widget with a brush and width.
103+
pub fn border(mut self, brush: impl Into<Brush>, width: impl Into<f64>) -> Self {
103104
self.border = Some(BorderStyle {
104-
color: color.into(),
105+
brush: brush.into(),
105106
width: width.into(),
106107
});
107108
self
@@ -141,7 +142,7 @@ where
141142
widget = widget.background(background.clone());
142143
}
143144
if let Some(border) = &self.border {
144-
widget = widget.border(border.color, border.width);
145+
widget = widget.border(border.brush.clone(), border.width);
145146
}
146147
(ctx.new_pod(widget), child_state)
147148
}
@@ -176,7 +177,7 @@ where
176177
if self.border != prev.border {
177178
match &self.border {
178179
Some(border) => {
179-
widget::SizedBox::set_border(&mut element, border.color, border.width);
180+
widget::SizedBox::set_border(&mut element, border.brush.clone(), border.width);
180181
}
181182
None => widget::SizedBox::clear_border(&mut element),
182183
}
@@ -221,5 +222,5 @@ where
221222
#[derive(PartialEq)]
222223
struct BorderStyle {
223224
width: f64,
224-
color: Color,
225+
brush: Brush,
225226
}

0 commit comments

Comments
 (0)