From 83924c3ea00856fc46101d8959b3bb7c372b887d Mon Sep 17 00:00:00 2001 From: Dave Patrick Caberto Date: Thu, 31 Aug 2023 11:02:17 +0800 Subject: [PATCH] gsk: Add builder for Stroke --- gsk4/src/builders.rs | 3 +++ gsk4/src/lib.rs | 3 +++ gsk4/src/stroke.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 gsk4/src/stroke.rs diff --git a/gsk4/src/builders.rs b/gsk4/src/builders.rs index c9e9fb7dfa86..0f8ce108b58d 100644 --- a/gsk4/src/builders.rs +++ b/gsk4/src/builders.rs @@ -5,4 +5,7 @@ pub use crate::auto::builders::*; pub use crate::color_stop::ColorStopBuilder; +#[cfg(feature = "v4_14")] +#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))] +pub use crate::stroke::StrokeBuilder; pub use crate::ShaderArgsBuilder; diff --git a/gsk4/src/lib.rs b/gsk4/src/lib.rs index bf31d426f535..287116f0e842 100644 --- a/gsk4/src/lib.rs +++ b/gsk4/src/lib.rs @@ -79,6 +79,9 @@ mod rounded_clip_node; mod shadow_node; #[cfg(feature = "v4_14")] #[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))] +mod stroke; +#[cfg(feature = "v4_14")] +#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))] mod stroke_node; mod text_node; mod texture_node; diff --git a/gsk4/src/stroke.rs b/gsk4/src/stroke.rs new file mode 100644 index 000000000000..1eab71011246 --- /dev/null +++ b/gsk4/src/stroke.rs @@ -0,0 +1,49 @@ +use crate::{LineCap, LineJoin, Stroke}; + +impl Stroke { + // rustdoc-stripper-ignore-next + /// Creates a new builder-pattern struct instance to construct a [`Stroke`]. + /// + /// This method returns an instance of [`StrokeBuilder`](crate::builders::StrokeBuilder) which can be used to create a [`Stroke`]. + pub fn builder(line_width: f32) -> StrokeBuilder { + StrokeBuilder(Stroke::new(line_width)) + } +} + +// rustdoc-stripper-ignore-next +/// A [builder-pattern] type to construct a [`Stroke`]. +/// +/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html +#[must_use = "The builder must be built to be used"] +pub struct StrokeBuilder(Stroke); + +impl StrokeBuilder { + pub fn dash(self, dash: &[f32]) -> Self { + self.0.set_dash(dash); + self + } + + pub fn dash_offset(self, dash_offset: f32) -> Self { + self.0.set_dash_offset(dash_offset); + self + } + + pub fn line_cap(self, line_cap: LineCap) -> Self { + self.0.set_line_cap(line_cap); + self + } + + pub fn line_join(self, line_join: LineJoin) -> Self { + self.0.set_line_join(line_join); + self + } + + pub fn miter_limit(self, miter_limit: f32) -> Self { + self.0.set_miter_limit(miter_limit); + self + } + + pub fn build(self) -> Stroke { + self.0 + } +}