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::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)]
27#[typed(todo_derive_fields)]
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
61/// A generic value for item of `image cursors`.
62#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
63#[repr(C)]
64pub struct GenericCursorImage<Image, Number> {
65    /// The url to parse images from.
66    pub image: Image,
67    /// Whether the image has a hotspot or not.
68    pub has_hotspot: bool,
69    /// The x coordinate.
70    pub hotspot_x: Number,
71    /// The y coordinate.
72    pub hotspot_y: Number,
73}
74
75pub use self::GenericCursorImage as CursorImage;
76
77impl<Image: ToCss, Number: ToCss> ToCss for CursorImage<Image, Number> {
78    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
79    where
80        W: Write,
81    {
82        self.image.to_css(dest)?;
83        if self.has_hotspot {
84            dest.write_char(' ')?;
85            self.hotspot_x.to_css(dest)?;
86            dest.write_char(' ')?;
87            self.hotspot_y.to_css(dest)?;
88        }
89        Ok(())
90    }
91}
92
93/// A generic value for `scrollbar-color` property.
94///
95/// https://drafts.csswg.org/css-scrollbars-1/#scrollbar-color
96#[derive(
97    Animate,
98    Clone,
99    ComputeSquaredDistance,
100    Copy,
101    Debug,
102    MallocSizeOf,
103    PartialEq,
104    SpecifiedValueInfo,
105    ToAnimatedValue,
106    ToAnimatedZero,
107    ToComputedValue,
108    ToCss,
109    ToResolvedValue,
110    ToShmem,
111    ToTyped,
112)]
113#[repr(C, u8)]
114pub enum GenericScrollbarColor<Color> {
115    /// `auto`
116    Auto,
117    /// `<color>{2}`
118    Colors {
119        /// First `<color>`, for color of the scrollbar thumb.
120        thumb: Color,
121        /// Second `<color>`, for color of the scrollbar track.
122        track: Color,
123    },
124}
125
126pub use self::GenericScrollbarColor as ScrollbarColor;
127
128impl<Color> Default for ScrollbarColor<Color> {
129    #[inline]
130    fn default() -> Self {
131        ScrollbarColor::Auto
132    }
133}