Skip to main content

style/values/computed/
resolution.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//! Resolution values:
6//!
7//! https://drafts.csswg.org/css-values/#resolution
8
9use crate::derives::*;
10use crate::typed_om::{NumericValue, ToTyped, TypedValue, UnitValue};
11use crate::values::CSSFloat;
12use std::{
13    fmt::{self, Write},
14    ops::AddAssign,
15};
16use style_traits::{CssString, CssWriter, ToCss};
17use thin_vec::ThinVec;
18
19/// A computed `<resolution>`.
20#[repr(C)]
21#[derive(
22    Animate,
23    Copy,
24    Clone,
25    Debug,
26    Deserialize,
27    MallocSizeOf,
28    PartialEq,
29    PartialOrd,
30    Serialize,
31    ToAnimatedZero,
32    ToResolvedValue,
33    ToShmem,
34)]
35pub struct Resolution(CSSFloat);
36
37impl Resolution {
38    /// Returns this resolution value as dppx.
39    #[inline]
40    pub fn dppx(&self) -> CSSFloat {
41        self.0
42    }
43
44    /// Return a computed `resolution` value from a dppx float value.
45    #[inline]
46    pub fn from_dppx(dppx: CSSFloat) -> Self {
47        Resolution(dppx)
48    }
49}
50
51impl ToCss for Resolution {
52    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
53    where
54        W: fmt::Write,
55    {
56        self.dppx().to_css(dest)?;
57        dest.write_str("dppx")
58    }
59}
60
61impl ToTyped for Resolution {
62    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
63        dest.push(TypedValue::Numeric(NumericValue::Unit(UnitValue {
64            value: self.dppx(),
65            unit: CssString::from("dppx"),
66        })));
67        Ok(())
68    }
69}
70
71impl AddAssign for Resolution {
72    fn add_assign(&mut self, rhs: Self) {
73        self.0 += rhs.0
74    }
75}