Skip to main content

leptos_css/
touch_action.rs

1use std::fmt;
2
3use crate::checked_value::impl_checked_value;
4use crate::serialization::CssWriteTo;
5
6/// A horizontal panning component of the `touch-action` grammar.
7///
8/// [Pointer Events Level 4 `touch-action` grammar]: https://www.w3.org/TR/pointerevents4/#the-touch-action-css-property
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
10#[non_exhaustive]
11pub enum TouchActionHorizontalPan {
12    /// `pan-x`.
13    PanX,
14    /// `pan-left`.
15    PanLeft,
16    /// `pan-right`.
17    PanRight,
18}
19
20impl CssWriteTo for TouchActionHorizontalPan {
21    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
22        w.write_str(match self {
23            Self::PanX => "pan-x",
24            Self::PanLeft => "pan-left",
25            Self::PanRight => "pan-right",
26        })
27    }
28}
29
30/// A vertical panning component of the `touch-action` grammar.
31///
32/// [Pointer Events Level 4 `touch-action` grammar]: https://www.w3.org/TR/pointerevents4/#the-touch-action-css-property
33#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
34#[non_exhaustive]
35pub enum TouchActionVerticalPan {
36    /// `pan-y`.
37    PanY,
38    /// `pan-up`.
39    PanUp,
40    /// `pan-down`.
41    PanDown,
42}
43
44impl CssWriteTo for TouchActionVerticalPan {
45    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
46        w.write_str(match self {
47            Self::PanY => "pan-y",
48            Self::PanUp => "pan-up",
49            Self::PanDown => "pan-down",
50        })
51    }
52}
53
54/// A non-empty composition of panning and pinch-zoom gestures for `touch-action`.
55///
56/// Horizontal and vertical panning come from Pointer Events Level 4. The WHATWG Compatibility
57/// Standard augments that grammar with the independently composable `pinch-zoom` keyword.
58///
59/// [Pointer Events Level 4 `touch-action` grammar]: https://www.w3.org/TR/pointerevents4/#the-touch-action-css-property
60/// [WHATWG Compatibility `touch-action` extension]: https://compat.spec.whatwg.org/#touch-action
61#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
62pub struct TouchActionGestures {
63    horizontal: Option<TouchActionHorizontalPan>,
64    vertical: Option<TouchActionVerticalPan>,
65    pinch_zoom: bool,
66}
67
68impl TouchActionGestures {
69    /// Start a gesture composition with horizontal panning.
70    pub const fn horizontal(horizontal: TouchActionHorizontalPan) -> Self {
71        Self {
72            horizontal: Some(horizontal),
73            vertical: None,
74            pinch_zoom: false,
75        }
76    }
77
78    /// Start a gesture composition with vertical panning.
79    pub const fn vertical(vertical: TouchActionVerticalPan) -> Self {
80        Self {
81            horizontal: None,
82            vertical: Some(vertical),
83            pinch_zoom: false,
84        }
85    }
86
87    /// Start a gesture composition with pinch zooming.
88    pub const fn pinch_zoom() -> Self {
89        Self {
90            horizontal: None,
91            vertical: None,
92            pinch_zoom: true,
93        }
94    }
95
96    /// Add or replace the horizontal panning component.
97    #[must_use]
98    pub const fn with_horizontal(mut self, horizontal: TouchActionHorizontalPan) -> Self {
99        self.horizontal = Some(horizontal);
100        self
101    }
102
103    /// Add or replace the vertical panning component.
104    #[must_use]
105    pub const fn with_vertical(mut self, vertical: TouchActionVerticalPan) -> Self {
106        self.vertical = Some(vertical);
107        self
108    }
109
110    /// Add pinch zooming to the gesture composition.
111    #[must_use]
112    pub const fn with_pinch_zoom(mut self) -> Self {
113        self.pinch_zoom = true;
114        self
115    }
116}
117
118impl CssWriteTo for TouchActionGestures {
119    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
120        let mut has_previous = false;
121
122        if let Some(horizontal) = self.horizontal {
123            horizontal.css_fmt(w)?;
124            has_previous = true;
125        }
126        if let Some(vertical) = self.vertical {
127            if has_previous {
128                w.write_char(' ')?;
129            }
130            vertical.css_fmt(w)?;
131            has_previous = true;
132        }
133        if self.pinch_zoom {
134            if has_previous {
135                w.write_char(' ')?;
136            }
137            w.write_str("pinch-zoom")?;
138        }
139
140        Ok(())
141    }
142}
143
144/// A standards-valid `touch-action` value.
145///
146/// Pointer Events Level 4 defines the property keywords and the horizontal and vertical panning
147/// components. The WHATWG Compatibility Standard adds `pinch-zoom` to the composable gesture
148/// branch represented by [`TouchActionGestures`].
149///
150/// [Pointer Events Level 4 `touch-action` grammar]: https://www.w3.org/TR/pointerevents4/#the-touch-action-css-property
151/// [WHATWG Compatibility `touch-action` extension]: https://compat.spec.whatwg.org/#touch-action
152#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
153#[non_exhaustive]
154pub enum TouchAction {
155    /// `auto`.
156    Auto,
157    /// `none`.
158    None,
159    /// `manipulation`.
160    Manipulation,
161    /// A non-empty combination of horizontal panning, vertical panning, and pinch zooming.
162    Gestures(TouchActionGestures),
163}
164
165impl From<TouchActionGestures> for TouchAction {
166    fn from(value: TouchActionGestures) -> Self {
167        Self::Gestures(value)
168    }
169}
170
171impl CssWriteTo for TouchAction {
172    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
173        match self {
174            Self::Auto => w.write_str("auto"),
175            Self::None => w.write_str("none"),
176            Self::Manipulation => w.write_str("manipulation"),
177            Self::Gestures(value) => value.css_fmt(w),
178        }
179    }
180}
181
182impl_checked_value!(
183    TouchActionHorizontalPan,
184    TouchActionVerticalPan,
185    TouchActionGestures,
186    TouchAction,
187);