Skip to main content

blockworx_canvas2d/
cursor.rs

1//! The pointer shapes the tools ask for, in the browser's own vocabulary.
2
3use blockworx_paint::Cursor;
4
5/// The CSS `cursor` value for `cursor`, to set on the canvas element.
6#[must_use]
7pub fn css_cursor(cursor: Cursor) -> &'static str {
8    match cursor {
9        Cursor::Default => "default",
10        Cursor::Crosshair => "crosshair",
11        Cursor::Text => "text",
12        Cursor::PointingHand => "pointer",
13        Cursor::Move => "move",
14        Cursor::Grab => "grab",
15        Cursor::Grabbing => "grabbing",
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22
23    /// Every shape names a CSS keyword, and no two name the same one — a
24    /// duplicate would silently drop a tool's affordance.
25    #[test]
26    fn every_cursor_has_its_own_css_keyword() {
27        let shapes = [
28            Cursor::Default,
29            Cursor::Crosshair,
30            Cursor::Text,
31            Cursor::PointingHand,
32            Cursor::Move,
33            Cursor::Grab,
34            Cursor::Grabbing,
35        ];
36        let mut named: Vec<&str> = shapes.iter().map(|&c| css_cursor(c)).collect();
37        named.sort_unstable();
38        let count = named.len();
39        named.dedup();
40        assert_eq!(named.len(), count, "two shapes share a keyword: {named:?}");
41        assert_eq!(css_cursor(Cursor::PointingHand), "pointer");
42    }
43}