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::values::specified::ui::CursorKind;
8use std::fmt::{self, Write};
9use style_traits::{CssWriter, ToCss};
10
11/// A generic value for the `cursor` property.
12///
13/// https://drafts.csswg.org/css-ui/#cursor
14#[derive(
15    Clone,
16    Debug,
17    MallocSizeOf,
18    PartialEq,
19    SpecifiedValueInfo,
20    ToComputedValue,
21    ToResolvedValue,
22    ToShmem,
23    ToTyped,
24)]
25#[repr(C)]
26pub struct GenericCursor<Image> {
27    /// The parsed images for the cursor.
28    pub images: crate::OwnedSlice<Image>,
29    /// The kind of the cursor [default | help | ...].
30    pub keyword: CursorKind,
31}
32
33pub use self::GenericCursor as Cursor;
34
35impl<Image> Cursor<Image> {
36    /// Set `cursor` to `auto`
37    #[inline]
38    pub fn auto() -> Self {
39        Self {
40            images: Default::default(),
41            keyword: CursorKind::Auto,
42        }
43    }
44}
45
46impl<Image: ToCss> ToCss for Cursor<Image> {
47    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
48    where
49        W: Write,
50    {
51        for image in &*self.images {
52            image.to_css(dest)?;
53            dest.write_str(", ")?;
54        }
55        self.keyword.to_css(dest)
56    }
57}
58
59/// A generic value for item of `image cursors`.
60#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
61#[repr(C)]
62pub struct GenericCursorImage<Image, Number> {
63    /// The url to parse images from.
64    pub image: Image,
65    /// Whether the image has a hotspot or not.
66    pub has_hotspot: bool,
67    /// The x coordinate.
68    pub hotspot_x: Number,
69    /// The y coordinate.
70    pub hotspot_y: Number,
71}
72
73pub use self::GenericCursorImage as CursorImage;
74
75impl<Image: ToCss, Number: ToCss> ToCss for CursorImage<Image, Number> {
76    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
77    where
78        W: Write,
79    {
80        self.image.to_css(dest)?;
81        if self.has_hotspot {
82            dest.write_char(' ')?;
83            self.hotspot_x.to_css(dest)?;
84            dest.write_char(' ')?;
85            self.hotspot_y.to_css(dest)?;
86        }
87        Ok(())
88    }
89}
90
91/// A generic value for `scrollbar-color` property.
92///
93/// https://drafts.csswg.org/css-scrollbars-1/#scrollbar-color
94#[derive(
95    Animate,
96    Clone,
97    ComputeSquaredDistance,
98    Copy,
99    Debug,
100    MallocSizeOf,
101    PartialEq,
102    SpecifiedValueInfo,
103    ToAnimatedValue,
104    ToAnimatedZero,
105    ToComputedValue,
106    ToCss,
107    ToResolvedValue,
108    ToShmem,
109    ToTyped,
110)]
111#[repr(C, u8)]
112pub enum GenericScrollbarColor<Color> {
113    /// `auto`
114    Auto,
115    /// `<color>{2}`
116    Colors {
117        /// First `<color>`, for color of the scrollbar thumb.
118        thumb: Color,
119        /// Second `<color>`, for color of the scrollbar track.
120        track: Color,
121    },
122}
123
124pub use self::GenericScrollbarColor as ScrollbarColor;
125
126impl<Color> Default for ScrollbarColor<Color> {
127    #[inline]
128    fn default() -> Self {
129        ScrollbarColor::Auto
130    }
131}