ui.rs (7549B)
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 //! Specified types for UI properties. 6 7 use crate::derives::*; 8 use crate::parser::{Parse, ParserContext}; 9 use crate::values::generics::ui as generics; 10 use crate::values::specified::color::Color; 11 use crate::values::specified::image::Image; 12 use crate::values::specified::Number; 13 use cssparser::Parser; 14 use std::fmt::{self, Write}; 15 use style_traits::{ 16 CssWriter, KeywordsCollectFn, ParseError, SpecifiedValueInfo, StyleParseErrorKind, ToCss, 17 }; 18 19 /// A specified value for the `cursor` property. 20 pub type Cursor = generics::GenericCursor<CursorImage>; 21 22 /// A specified value for item of `image cursors`. 23 pub type CursorImage = generics::GenericCursorImage<Image, Number>; 24 25 impl Parse for Cursor { 26 /// cursor: [<url> [<number> <number>]?]# [auto | default | ...] 27 fn parse<'i, 't>( 28 context: &ParserContext, 29 input: &mut Parser<'i, 't>, 30 ) -> Result<Self, ParseError<'i>> { 31 let mut images = vec![]; 32 loop { 33 match input.try_parse(|input| CursorImage::parse(context, input)) { 34 Ok(image) => images.push(image), 35 Err(_) => break, 36 } 37 input.expect_comma()?; 38 } 39 Ok(Self { 40 images: images.into(), 41 keyword: CursorKind::parse(input)?, 42 }) 43 } 44 } 45 46 impl Parse for CursorImage { 47 fn parse<'i, 't>( 48 context: &ParserContext, 49 input: &mut Parser<'i, 't>, 50 ) -> Result<Self, ParseError<'i>> { 51 use crate::Zero; 52 53 let image = Image::parse_only_url(context, input)?; 54 let mut has_hotspot = false; 55 let mut hotspot_x = Number::zero(); 56 let mut hotspot_y = Number::zero(); 57 58 if let Ok(x) = input.try_parse(|input| Number::parse(context, input)) { 59 has_hotspot = true; 60 hotspot_x = x; 61 hotspot_y = Number::parse(context, input)?; 62 } 63 64 Ok(Self { 65 image, 66 has_hotspot, 67 hotspot_x, 68 hotspot_y, 69 }) 70 } 71 } 72 73 // This trait is manually implemented because we don't support the whole <image> 74 // syntax for cursors 75 impl SpecifiedValueInfo for CursorImage { 76 fn collect_completion_keywords(f: KeywordsCollectFn) { 77 f(&["url", "image-set"]); 78 } 79 } 80 /// Specified value of `-moz-force-broken-image-icon` 81 #[derive( 82 Clone, 83 Copy, 84 Debug, 85 MallocSizeOf, 86 PartialEq, 87 SpecifiedValueInfo, 88 ToComputedValue, 89 ToResolvedValue, 90 ToShmem, 91 ToTyped, 92 )] 93 #[repr(transparent)] 94 pub struct BoolInteger(pub bool); 95 96 impl BoolInteger { 97 /// Returns 0 98 #[inline] 99 pub fn zero() -> Self { 100 Self(false) 101 } 102 } 103 104 impl Parse for BoolInteger { 105 fn parse<'i, 't>( 106 _context: &ParserContext, 107 input: &mut Parser<'i, 't>, 108 ) -> Result<Self, ParseError<'i>> { 109 // We intentionally don't support calc values here. 110 match input.expect_integer()? { 111 0 => Ok(Self(false)), 112 1 => Ok(Self(true)), 113 _ => Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)), 114 } 115 } 116 } 117 118 impl ToCss for BoolInteger { 119 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result 120 where 121 W: Write, 122 { 123 dest.write_str(if self.0 { "1" } else { "0" }) 124 } 125 } 126 127 /// A specified value for `scrollbar-color` property 128 pub type ScrollbarColor = generics::ScrollbarColor<Color>; 129 130 impl Parse for ScrollbarColor { 131 fn parse<'i, 't>( 132 context: &ParserContext, 133 input: &mut Parser<'i, 't>, 134 ) -> Result<Self, ParseError<'i>> { 135 if input.try_parse(|i| i.expect_ident_matching("auto")).is_ok() { 136 return Ok(generics::ScrollbarColor::Auto); 137 } 138 Ok(generics::ScrollbarColor::Colors { 139 thumb: Color::parse(context, input)?, 140 track: Color::parse(context, input)?, 141 }) 142 } 143 } 144 145 /// The specified value for the `user-select` property. 146 /// 147 /// https://drafts.csswg.org/css-ui-4/#propdef-user-select 148 #[allow(missing_docs)] 149 #[derive( 150 Clone, 151 Copy, 152 Debug, 153 Eq, 154 MallocSizeOf, 155 Parse, 156 PartialEq, 157 SpecifiedValueInfo, 158 ToComputedValue, 159 ToCss, 160 ToResolvedValue, 161 ToShmem, 162 ToTyped, 163 )] 164 #[repr(u8)] 165 pub enum UserSelect { 166 Auto, 167 Text, 168 #[parse(aliases = "-moz-none")] 169 None, 170 /// Force selection of all children. 171 All, 172 } 173 174 /// The keywords allowed in the Cursor property. 175 /// 176 /// https://drafts.csswg.org/css-ui-4/#propdef-cursor 177 #[allow(missing_docs)] 178 #[derive( 179 Clone, 180 Copy, 181 Debug, 182 Eq, 183 FromPrimitive, 184 MallocSizeOf, 185 Parse, 186 PartialEq, 187 SpecifiedValueInfo, 188 ToComputedValue, 189 ToCss, 190 ToResolvedValue, 191 ToShmem, 192 )] 193 #[repr(u8)] 194 pub enum CursorKind { 195 None, 196 Default, 197 Pointer, 198 ContextMenu, 199 Help, 200 Progress, 201 Wait, 202 Cell, 203 Crosshair, 204 Text, 205 VerticalText, 206 Alias, 207 Copy, 208 Move, 209 NoDrop, 210 NotAllowed, 211 #[parse(aliases = "-moz-grab")] 212 Grab, 213 #[parse(aliases = "-moz-grabbing")] 214 Grabbing, 215 EResize, 216 NResize, 217 NeResize, 218 NwResize, 219 SResize, 220 SeResize, 221 SwResize, 222 WResize, 223 EwResize, 224 NsResize, 225 NeswResize, 226 NwseResize, 227 ColResize, 228 RowResize, 229 AllScroll, 230 #[parse(aliases = "-moz-zoom-in")] 231 ZoomIn, 232 #[parse(aliases = "-moz-zoom-out")] 233 ZoomOut, 234 Auto, 235 } 236 237 /// The keywords allowed in the -moz-theme property. 238 #[derive( 239 Clone, 240 Copy, 241 Debug, 242 Eq, 243 FromPrimitive, 244 MallocSizeOf, 245 Parse, 246 PartialEq, 247 SpecifiedValueInfo, 248 ToComputedValue, 249 ToCss, 250 ToResolvedValue, 251 ToShmem, 252 ToTyped, 253 )] 254 #[repr(u8)] 255 pub enum MozTheme { 256 /// Choose the default (maybe native) rendering. 257 Auto, 258 /// Choose the non-native rendering. 259 NonNative, 260 } 261 262 /// The pointer-events property 263 /// https://svgwg.org/svg2-draft/interact.html#PointerEventsProperty 264 #[allow(missing_docs)] 265 #[derive( 266 Clone, 267 Copy, 268 Debug, 269 Eq, 270 FromPrimitive, 271 MallocSizeOf, 272 Parse, 273 PartialEq, 274 SpecifiedValueInfo, 275 ToComputedValue, 276 ToCss, 277 ToResolvedValue, 278 ToShmem, 279 ToTyped, 280 )] 281 #[repr(u8)] 282 pub enum PointerEvents { 283 Auto, 284 None, 285 #[cfg(feature = "gecko")] 286 Visiblepainted, 287 #[cfg(feature = "gecko")] 288 Visiblefill, 289 #[cfg(feature = "gecko")] 290 Visiblestroke, 291 #[cfg(feature = "gecko")] 292 Visible, 293 #[cfg(feature = "gecko")] 294 Painted, 295 #[cfg(feature = "gecko")] 296 Fill, 297 #[cfg(feature = "gecko")] 298 Stroke, 299 #[cfg(feature = "gecko")] 300 All, 301 } 302 303 /// Internal property to represent the inert attribute state: 304 /// https://html.spec.whatwg.org/multipage/#inert-subtrees 305 #[allow(missing_docs)] 306 #[derive( 307 Clone, 308 Copy, 309 Debug, 310 Eq, 311 FromPrimitive, 312 MallocSizeOf, 313 Parse, 314 PartialEq, 315 SpecifiedValueInfo, 316 ToComputedValue, 317 ToCss, 318 ToResolvedValue, 319 ToShmem, 320 ToTyped, 321 )] 322 #[repr(u8)] 323 pub enum Inert { 324 None, 325 Inert, 326 } 327 328 /// Internal -moz-user-focus property. 329 /// https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-user-focus 330 #[allow(missing_docs)] 331 #[derive( 332 Clone, 333 Copy, 334 Debug, 335 Eq, 336 FromPrimitive, 337 MallocSizeOf, 338 Parse, 339 PartialEq, 340 SpecifiedValueInfo, 341 ToComputedValue, 342 ToCss, 343 ToResolvedValue, 344 ToShmem, 345 ToTyped, 346 )] 347 #[repr(u8)] 348 pub enum UserFocus { 349 Normal, 350 None, 351 Ignore, 352 }