Skip to main content

ferroui_base/media/immutable/
immutable_dash_style.rs

1use crate::media::IDashStyle;
2use std::any::Any;
3use std::rc::Rc;
4
5/// Represents the sequence of dashes and gaps that will be applied by an
6/// [`ImmutablePen`](crate::media::immutable::ImmutablePen).
7#[derive(Clone, Debug)]
8pub struct ImmutableDashStyle {
9    dashes: Box<[f64]>,
10    offset: f64,
11}
12
13impl ImmutableDashStyle {
14    /// Creates a dash style. `None` dashes produce an empty sequence.
15    pub fn new(dashes: Option<&[f64]>, offset: f64) -> Self {
16        Self { dashes: dashes.unwrap_or(&[]).into(), offset }
17    }
18
19    /// The length of alternating dashes and gaps.
20    #[inline]
21    pub fn dashes(&self) -> &[f64] {
22        &self.dashes
23    }
24
25    /// How far in the dash sequence the stroke will start.
26    #[inline]
27    pub fn offset(&self) -> f64 {
28        self.offset
29    }
30
31    fn sequence_equal(left: &[f64], right: Option<&[f64]>) -> bool {
32        match right {
33            Some(right) => left == right,
34            None => false,
35        }
36    }
37}
38
39impl PartialEq for ImmutableDashStyle {
40    fn eq(&self, other: &Self) -> bool {
41        self.offset == other.offset && self.dashes == other.dashes
42    }
43}
44
45impl IDashStyle for ImmutableDashStyle {
46    fn dashes(&self) -> Option<Vec<f64>> {
47        Some(self.dashes.to_vec())
48    }
49
50    #[inline]
51    fn offset(&self) -> f64 {
52        self.offset
53    }
54
55    fn as_any(&self) -> &dyn Any {
56        self
57    }
58
59    fn into_immutable_dash_style(self: Rc<Self>) -> Rc<ImmutableDashStyle> {
60        self
61    }
62
63    fn equals(&self, other: &dyn IDashStyle) -> bool {
64        if self.reference_id() == other.reference_id() {
65            return true;
66        }
67
68        if let Some(other) = other.as_any().downcast_ref::<ImmutableDashStyle>() {
69            return self == other;
70        }
71
72        self.offset == other.offset() && Self::sequence_equal(&self.dashes, other.dashes().as_deref())
73    }
74}