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