tor-browser

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

animation.rs (2945B)


      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 //! Computed values for properties related to animations and transitions
      6 
      7 use crate::derives::*;
      8 use crate::values::computed::{Context, LengthPercentage, Time, ToComputedValue};
      9 use crate::values::generics::animation as generics;
     10 use crate::values::specified::animation as specified;
     11 use crate::values::CSSFloat;
     12 use std::fmt::{self, Write};
     13 use style_traits::{CssWriter, ToCss};
     14 
     15 pub use crate::values::specified::animation::{
     16    AnimationComposition, AnimationDirection, AnimationFillMode, AnimationName, AnimationPlayState,
     17    ScrollAxis, TimelineName, TransitionBehavior, TransitionProperty, ViewTransitionClass,
     18    ViewTransitionName,
     19 };
     20 
     21 /// A computed value for the `animation-duration` property.
     22 pub type AnimationDuration = generics::GenericAnimationDuration<Time>;
     23 
     24 impl AnimationDuration {
     25    /// Returns the amount of seconds this time represents.
     26    #[inline]
     27    pub fn seconds(&self) -> CSSFloat {
     28        match *self {
     29            Self::Auto => 0.0,
     30            Self::Time(ref t) => t.seconds(),
     31        }
     32    }
     33 }
     34 
     35 /// A computed value for the `animation-iteration-count` property.
     36 #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToResolvedValue, ToShmem)]
     37 #[repr(C)]
     38 pub struct AnimationIterationCount(pub f32);
     39 
     40 impl ToComputedValue for specified::AnimationIterationCount {
     41    type ComputedValue = AnimationIterationCount;
     42 
     43    #[inline]
     44    fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
     45        AnimationIterationCount(match *self {
     46            specified::AnimationIterationCount::Number(n) => n.to_computed_value(context).0,
     47            specified::AnimationIterationCount::Infinite => f32::INFINITY,
     48        })
     49    }
     50 
     51    #[inline]
     52    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
     53        use crate::values::specified::NonNegativeNumber;
     54        if computed.0.is_infinite() {
     55            specified::AnimationIterationCount::Infinite
     56        } else {
     57            specified::AnimationIterationCount::Number(NonNegativeNumber::new(computed.0))
     58        }
     59    }
     60 }
     61 
     62 impl AnimationIterationCount {
     63    /// Returns the value `1.0`.
     64    #[inline]
     65    pub fn one() -> Self {
     66        Self(1.0)
     67    }
     68 }
     69 
     70 impl ToCss for AnimationIterationCount {
     71    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
     72    where
     73        W: Write,
     74    {
     75        if self.0.is_infinite() {
     76            dest.write_str("infinite")
     77        } else {
     78            self.0.to_css(dest)
     79        }
     80    }
     81 }
     82 
     83 /// A computed value for the `animation-timeline` property.
     84 pub type AnimationTimeline = generics::GenericAnimationTimeline<LengthPercentage>;
     85 
     86 /// A computed value for the `view-timeline-inset` property.
     87 pub type ViewTimelineInset = generics::GenericViewTimelineInset<LengthPercentage>;