dioxus_leaflet/types/
path_options.rs

1use serde::{Deserialize, Serialize};
2use color::{OpaqueColor, Srgb};
3
4pub type Color = OpaqueColor<Srgb>;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum LineCap {
9    Butt,
10    Round,
11    Square,
12}
13
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "lowercase")]
16pub enum LineJoin {
17    Arcs,
18    Bevel,
19    Miter,
20    #[serde(rename = "miter-clip")]
21    MiterClip,
22    Round,
23}
24
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub struct PathOptions {
28    /// Whether to draw stroke along the path. Set it to false to disable borders on polygons or circles.
29    pub stroke: bool,
30
31    /// Stroke color
32    #[serde(with = "color_format")]
33    pub color: Color,
34
35    /// Stroke width in pixels
36    pub weight: u32,
37
38    /// Stroke opacity
39    pub opacity: f32,
40
41    /// Defines shape to be used at the end of the stroke.
42    pub line_cap: LineCap,
43
44    /// Defines shape to be used at the corners of the stroke.
45    pub line_join: LineJoin,
46
47    /// Whether to fill the path with color.
48    pub fill: bool,
49
50    /// Fill color. Defaults to the value of the color option.
51    #[serde(with = "color_format")]
52    pub fill_color: Color,
53
54    /// Fill opacity.
55    pub fill_opacity: f32,
56}
57
58impl Default for PathOptions {
59    fn default() -> Self {
60        Self {
61            stroke: true,
62            color: Color::from_rgb8(0x33, 0x88, 0xff),
63            weight: 3,
64            opacity: 1.0,
65            line_cap: LineCap::Round,
66            line_join: LineJoin::Round,
67            fill: true,
68            fill_color: Color::from_rgb8(0x33, 0x88, 0xff),
69            fill_opacity: 0.2,
70        }
71    }
72}
73
74mod color_format {
75    use super::Color;
76    use serde::{Deserializer, Serializer};
77
78    pub fn serialize<S>(color: &Color, serializer: S) -> Result<S::Ok, S::Error>
79    where S : Serializer {
80        let color = color.to_rgba8();
81        serializer.serialize_str(&format!("rgb({r}, {g}, {b})",
82            r = color.r,
83            g = color.g,
84            b = color.b))
85    }
86
87    pub fn deserialize<'de, D>(_: D) -> Result<Color, D::Error>
88    where D : Deserializer<'de> {
89        panic!("Deserializing colors not (yet) supported")
90    }
91}