Skip to content

Commit cae3b5a

Browse files
authored
feat: add CSS touch-action support (#35)
* feat: add CSS touch-action support * feat: better touch-action property parsing
1 parent ea616f2 commit cae3b5a

4 files changed

Lines changed: 108 additions & 1 deletion

File tree

float-pigment-css/src/property.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ property_list! (PropertyValueWithGlobal, {
174174
0xa9 GridAutoRows: GridAutoType as Initial default GridAuto::List(vec![TrackSize::Length(Length::Auto)].into());
175175
0xaa GridAutoColumns: GridAutoType as Initial default GridAuto::List(vec![TrackSize::Length(Length::Auto)].into());
176176

177-
177+
// misc
178178
0xd0 ListStyleType: ListStyleTypeType as Inherit default ListStyleType::Disc;
179179
0xd1 ListStyleImage: ListStyleImageType as Inherit default ListStyleImage::None;
180180
0xd2 ListStylePosition: ListStylePositionType as Inherit default ListStylePosition::Outside;
@@ -185,6 +185,7 @@ property_list! (PropertyValueWithGlobal, {
185185
0xd7 AspectRatio: AspectRatioType as Initial default AspectRatio::Auto;
186186
0xd8 Contain: ContainType as Initial default Contain::None;
187187
0xd9 Content: ContentType as Initial default Content::None;
188+
0xda TouchAction: TouchActionType as Initial default TouchAction::Auto;
188189

189190
// wx-spec special properties
190191
0xe0 WxScrollbarX: ScrollbarType as Initial default Scrollbar::Auto;
@@ -1729,6 +1730,32 @@ property_value_format! (PropertyValueWithGlobal, {
17291730
};
17301731
}};
17311732

1733+
<touch_action_pan_x: u8>:
1734+
"pan-x" -> |_| 3;
1735+
| "pan-left" -> |_| 1;
1736+
| "pan-right" -> |_| 2;
1737+
;
1738+
<touch_action_pan_y: u8>:
1739+
"pan-y" -> |_| 3;
1740+
| "pan-up" -> |_| 1;
1741+
| "pan-down" -> |_| 2;
1742+
;
1743+
touch_action: {{ TouchAction
1744+
= "auto" => TouchActionType::Auto
1745+
| "none" => TouchActionType::None
1746+
| "manipulation" => TouchActionType::Manipulation
1747+
| [<touch_action_pan_x> || <touch_action_pan_y>] -> |(pan_x, pan_y): (Option<u8>, Option<u8>)| {
1748+
let pan_x = pan_x.unwrap_or(0);
1749+
let pan_y = pan_y.unwrap_or(0);
1750+
let ges = TouchActionGestures {
1751+
pan_left: (pan_x & 1) > 0,
1752+
pan_right: (pan_x & 2) > 0,
1753+
pan_up: (pan_y & 1) > 0,
1754+
pan_down: (pan_y & 2) > 0,
1755+
};
1756+
TouchActionType::Gestures(ges)
1757+
};
1758+
}}
17321759
});
17331760

17341761
pub(crate) fn split_hv<T: Clone>(x: Vec<T>) -> (T, T) {

float-pigment-css/src/typing.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,3 +1905,32 @@ pub enum GridAutoFlow {
19051905
pub enum GridAuto {
19061906
List(Array<TrackSize>),
19071907
}
1908+
1909+
#[allow(missing_docs)]
1910+
#[repr(C)]
1911+
#[property_value_type(PropertyValueWithGlobal for TouchActionType)]
1912+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ResolveFontSize)]
1913+
#[cfg_attr(debug_assertions, derive(CompatibilityEnumCheck))]
1914+
pub enum TouchAction {
1915+
Auto,
1916+
None,
1917+
Manipulation,
1918+
Gestures(TouchActionGestures),
1919+
}
1920+
1921+
#[allow(missing_docs)]
1922+
#[repr(C)]
1923+
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
1924+
#[cfg_attr(debug_assertions, derive(CompatibilityStructCheck))]
1925+
pub struct TouchActionGestures {
1926+
pub pan_left: bool,
1927+
pub pan_right: bool,
1928+
pub pan_up: bool,
1929+
pub pan_down: bool,
1930+
}
1931+
1932+
impl ResolveFontSize for TouchActionGestures {
1933+
fn resolve_font_size(&mut self, _: f32) {
1934+
// empty
1935+
}
1936+
}

float-pigment-css/src/typing_stringify.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,3 +2434,40 @@ impl fmt::Display for GridAuto {
24342434
}
24352435
}
24362436
}
2437+
2438+
impl fmt::Display for TouchAction {
2439+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2440+
match self {
2441+
TouchAction::Auto => write!(f, "auto"),
2442+
TouchAction::None => write!(f, "none"),
2443+
TouchAction::Manipulation => write!(f, "manipulation"),
2444+
TouchAction::Gestures(ges) => write!(f, "{}", ges),
2445+
}
2446+
}
2447+
}
2448+
2449+
impl fmt::Display for TouchActionGestures {
2450+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2451+
let Self { pan_left, pan_right, pan_up, pan_down } = self;
2452+
let pan_x = if *pan_left && *pan_right {
2453+
"pan-x"
2454+
} else if *pan_left {
2455+
"pan-left"
2456+
} else if *pan_right {
2457+
"pan-right"
2458+
} else {
2459+
""
2460+
};
2461+
let pan_y = if *pan_up && *pan_down {
2462+
"pan-y"
2463+
} else if *pan_up {
2464+
"pan-up"
2465+
} else if *pan_down {
2466+
"pan-down"
2467+
} else {
2468+
""
2469+
};
2470+
let s: Vec<_> = [pan_x, pan_y].into_iter().filter(|x| !x.is_empty()).collect();
2471+
write!(f, "{}", s.join(" "))
2472+
}
2473+
}

float-pigment-css/tests/property.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7289,6 +7289,20 @@ mod other {
72897289
)
72907290
);
72917291
}
7292+
7293+
// 0xda
7294+
#[test]
7295+
fn touch_action() {
7296+
test_parse_property!(touch_action, "touch-action", "auto", TouchAction::Auto);
7297+
test_parse_property!(touch_action, "touch-action", "none", TouchAction::None);
7298+
test_parse_property!(touch_action, "touch-action", "manipulation", TouchAction::Manipulation);
7299+
test_parse_property!(touch_action, "touch-action", "pan-x", TouchAction::Gestures(TouchActionGestures { pan_left: true, pan_right: true, pan_up: false, pan_down: false }));
7300+
test_parse_property!(touch_action, "touch-action", "pan-y", TouchAction::Gestures(TouchActionGestures { pan_left: false, pan_right: false, pan_up: true, pan_down: true }));
7301+
test_parse_property!(touch_action, "touch-action", "pan-left", TouchAction::Gestures(TouchActionGestures { pan_left: true, pan_right: false, pan_up: false, pan_down: false }));
7302+
test_parse_property!(touch_action, "touch-action", "pan-right", TouchAction::Gestures(TouchActionGestures { pan_left: false, pan_right: true, pan_up: false, pan_down: false }));
7303+
test_parse_property!(touch_action, "touch-action", "pan-up", TouchAction::Gestures(TouchActionGestures { pan_left: false, pan_right: false, pan_up: true, pan_down: false }));
7304+
test_parse_property!(touch_action, "touch-action", "pan-down", TouchAction::Gestures(TouchActionGestures { pan_left: false, pan_right: false, pan_up: false, pan_down: true }));
7305+
}
72927306
}
72937307

72947308
mod wx_special {

0 commit comments

Comments
 (0)