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