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::values::computed::{Context, ToComputedValue};
10use crate::values::specified;
11use crate::values::CSSFloat;
12use std::fmt::{self, Write};
13use style_traits::{CssWriter, ToCss};
14
15/// A computed `<resolution>`.
16#[repr(C)]
17#[derive(Animate, Clone, Debug, MallocSizeOf, PartialEq, ToResolvedValue, ToShmem)]
18pub struct Resolution(CSSFloat);
19
20impl Resolution {
21    /// Returns this resolution value as dppx.
22    #[inline]
23    pub fn dppx(&self) -> CSSFloat {
24        self.0
25    }
26
27    /// Return a computed `resolution` value from a dppx float value.
28    #[inline]
29    pub fn from_dppx(dppx: CSSFloat) -> Self {
30        Resolution(dppx)
31    }
32}
33
34impl ToComputedValue for specified::Resolution {
35    type ComputedValue = Resolution;
36
37    #[inline]
38    fn to_computed_value(&self, _: &Context) -> Self::ComputedValue {
39        Resolution(crate::values::normalize(self.dppx().max(0.0)))
40    }
41
42    #[inline]
43    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
44        specified::Resolution::from_dppx(computed.dppx())
45    }
46}
47
48impl ToCss for Resolution {
49    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
50    where
51        W: fmt::Write,
52    {
53        self.dppx().to_css(dest)?;
54        dest.write_str("dppx")
55    }
56}