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