Skip to main content

style/values/animated/
text.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//! Animation implementations for text-related types.
6
7use super::{Animate, Procedure, ToAnimatedZero};
8use crate::values::computed::LengthPercentage;
9use crate::values::{ComputeSquaredDistance, MallocSizeOf, SquaredDistance};
10
11/// A `text-decoration-inset` value for animation: we resolve `auto` to a pair of lengths,
12/// but record whether the original value was actually `auto`.
13#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
14pub struct TextDecorationInset {
15    /// The inset at the start of the decoration.
16    pub start: LengthPercentage,
17    /// The inset at the end of the decoration.
18    pub end: LengthPercentage,
19    /// Whether this represents a resolved `auto` value.
20    pub is_auto: bool,
21}
22
23impl Animate for TextDecorationInset {
24    fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
25        Ok(if self.is_auto && other.is_auto {
26            self.clone()
27        } else {
28            Self {
29                start: self.start.animate(&other.start, procedure)?,
30                end: self.end.animate(&other.end, procedure)?,
31                is_auto: false,
32            }
33        })
34    }
35}
36
37impl ComputeSquaredDistance for TextDecorationInset {
38    #[inline]
39    fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
40        Ok(if self.is_auto && other.is_auto {
41            SquaredDistance::from_sqrt(0.)
42        } else {
43            self.start.compute_squared_distance(&other.start)?
44                + self.end.compute_squared_distance(&other.end)?
45        })
46    }
47}
48
49impl ToAnimatedZero for TextDecorationInset {
50    #[inline]
51    fn to_animated_zero(&self) -> Result<Self, ()> {
52        Ok(Self {
53            start: self.start.to_animated_zero()?,
54            end: self.end.to_animated_zero()?,
55            is_auto: false,
56        })
57    }
58}