Skip to main content

style/values/generics/
ui.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Generic values for UI properties.
6
7use crate::derives::*;
8use crate::typed_om::{ToTyped, TypedValue};
9use crate::values::specified::ui::CursorKind;
10use std::fmt::{self, Write};
11use style_traits::{CssWriter, ToCss};
12use thin_vec::ThinVec;
13
14/// A generic value for the `cursor` property.
15///
16/// https://drafts.csswg.org/css-ui/#cursor
17#[derive(
18    Clone,
19    Debug,
20    MallocSizeOf,
21    PartialEq,
22    SpecifiedValueInfo,
23    ToComputedValue,
24    ToResolvedValue,
25    ToShmem,
26)]
27#[repr(C)]
28pub struct GenericCursor<Image> {
29    /// The parsed images for the cursor.
30    pub images: crate::OwnedSlice<Image>,
31    /// The kind of the cursor [default | help | ...].
32    pub keyword: CursorKind,
33}
34
35pub use self::GenericCursor as Cursor;
36
37impl<Image> Cursor<Image> {
38    /// Set `cursor` to `auto`
39    #[inline]
40    pub fn auto() -> Self {
41        Self {
42            images: Default::default(),
43            keyword: CursorKind::Auto,
44        }
45    }
46}
47
48impl<Image: ToCss> ToCss for Cursor<Image> {
49    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
50    where
51        W: Write,
52    {
53        for image in &*self.images {
54            image.to_css(dest)?;
55            dest.write_str(", ")?;
56        }
57        self.keyword.to_css(dest)
58    }
59}
60
61impl<Image> ToTyped for Cursor<Image> {
62    // Note: The specification does not currently define how cursor should be
63    // reified into Typed OM. The current behavior follows existing WPT
64    // coverage (cursor.html). Syncing spec with UA/WPT behavior tracked in
65    // https://github.com/w3c/csswg-drafts/issues/13907
66    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
67        if self.images.len() != 0 {
68            return Err(());
69        }
70
71        self.keyword.to_typed(dest)
72    }
73}
74
75/// A generic value for item of `image cursors`.
76#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
77#[repr(C)]
78pub struct GenericCursorImage<Image, Number> {
79    /// The url to parse images from.
80    pub image: Image,
81    /// Whether the image has a hotspot or not.
82    pub has_hotspot: bool,
83    /// The x coordinate.
84    pub hotspot_x: Number,
85    /// The y coordinate.
86    pub hotspot_y: Number,
87}
88
89pub use self::GenericCursorImage as CursorImage;
90
91impl<Image: ToCss, Number: ToCss> ToCss for CursorImage<Image, Number> {
92    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
93    where
94        W: Write,
95    {
96        self.image.to_css(dest)?;
97        if self.has_hotspot {
98            dest.write_char(' ')?;
99            self.hotspot_x.to_css(dest)?;
100            dest.write_char(' ')?;
101            self.hotspot_y.to_css(dest)?;
102        }
103        Ok(())
104    }
105}
106
107/// A generic value for `scrollbar-color` property.
108///
109/// https://drafts.csswg.org/css-scrollbars-1/#scrollbar-color
110#[derive(
111    Animate,
112    Clone,
113    ComputeSquaredDistance,
114    Copy,
115    Debug,
116    MallocSizeOf,
117    PartialEq,
118    SpecifiedValueInfo,
119    ToAnimatedValue,
120    ToAnimatedZero,
121    ToComputedValue,
122    ToCss,
123    ToResolvedValue,
124    ToShmem,
125    ToTyped,
126)]
127#[repr(C, u8)]
128pub enum GenericScrollbarColor<Color> {
129    /// `auto`
130    Auto,
131    /// `<color>{2}`
132    Colors {
133        /// First `<color>`, for color of the scrollbar thumb.
134        thumb: Color,
135        /// Second `<color>`, for color of the scrollbar track.
136        track: Color,
137    },
138}
139
140pub use self::GenericScrollbarColor as ScrollbarColor;
141
142impl<Color> Default for ScrollbarColor<Color> {
143    #[inline]
144    fn default() -> Self {
145        ScrollbarColor::Auto
146    }
147}