tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

resolution.rs (1557B)


      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 
      9 use crate::derives::*;
     10 use crate::values::computed::{Context, ToComputedValue};
     11 use crate::values::specified;
     12 use crate::values::CSSFloat;
     13 use std::fmt::{self, Write};
     14 use style_traits::{CssWriter, ToCss};
     15 
     16 /// A computed `<resolution>`.
     17 #[repr(C)]
     18 #[derive(Animate, Clone, Debug, MallocSizeOf, PartialEq, ToResolvedValue, ToShmem)]
     19 pub struct Resolution(CSSFloat);
     20 
     21 impl 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 
     35 impl 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 
     49 impl 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 }