style/values/animated/grid.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 implementation for various grid-related types.
6
7// Note: we can implement Animate on their generic types directly, but in this case we need to
8// make sure two trait bounds, L: Clone and I: PartialEq, are satisfied on almost all the
9// grid-related types and their other trait implementations because Animate needs them. So in
10// order to avoid adding these two trait bounds (or maybe more..) everywhere, we implement
11// Animate for the computed types, instead of the generic types.
12
13use super::{Animate, Procedure, ToAnimatedZero};
14use crate::values::computed::Integer;
15use crate::values::computed::LengthPercentage;
16use crate::values::computed::{GridTemplateComponent, TrackList, TrackSize};
17use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
18use crate::values::generics::grid as generics;
19
20fn discrete<T: Clone>(from: &T, to: &T, procedure: Procedure) -> Result<T, ()> {
21 if let Procedure::Interpolate { progress } = procedure {
22 Ok(if progress < 0.5 {
23 from.clone()
24 } else {
25 to.clone()
26 })
27 } else {
28 // The discrete animation is not additive, so per spec [1] we should use the |from|, which
29 // is the underlying value. However this mismatches our animation mechanism (see
30 // composite_endpoint() in servo/ports/geckolib/glues.rs), which uses the effect value
31 // (i.e. |to| value here) [2]. So in order to match the behavior of other properties and
32 // other browsers, we use |to| value for addition and accumulation, i.e. Vresult = Vb.
33 //
34 // [1] https://drafts.csswg.org/css-values-4/#not-additive
35 // [2] https://github.com/w3c/csswg-drafts/issues/9070
36 Ok(to.clone())
37 }
38}
39
40fn animate_with_discrete_fallback<T: Animate + Clone>(
41 from: &T,
42 to: &T,
43 procedure: Procedure,
44) -> Result<T, ()> {
45 from.animate(to, procedure)
46 .or_else(|_| discrete(from, to, procedure))
47}
48
49impl Animate for TrackSize {
50 fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
51 match (self, other) {
52 (&generics::TrackSize::Breadth(ref from), &generics::TrackSize::Breadth(ref to)) => {
53 animate_with_discrete_fallback(from, to, procedure)
54 .map(generics::TrackSize::Breadth)
55 },
56 (
57 &generics::TrackSize::Minmax(ref from_min, ref from_max),
58 &generics::TrackSize::Minmax(ref to_min, ref to_max),
59 ) => Ok(generics::TrackSize::Minmax(
60 animate_with_discrete_fallback(from_min, to_min, procedure)?,
61 animate_with_discrete_fallback(from_max, to_max, procedure)?,
62 )),
63 (
64 &generics::TrackSize::FitContent(ref from),
65 &generics::TrackSize::FitContent(ref to),
66 ) => animate_with_discrete_fallback(from, to, procedure)
67 .map(generics::TrackSize::FitContent),
68 (_, _) => discrete(self, other, procedure),
69 }
70 }
71}
72
73impl Animate for generics::TrackRepeat<LengthPercentage, Integer> {
74 fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
75 // If the keyword, auto-fit/fill, is the same it can result in different
76 // number of tracks. For both auto-fit/fill, the number of columns isn't
77 // known until you do layout since it depends on the container size, item
78 // placement and other factors, so we cannot do the correct interpolation
79 // by computed values. Therefore, return Err(()) if it's keywords. If it
80 // is Number, we support animation only if the count is the same and the
81 // length of track_sizes is the same.
82 // https://github.com/w3c/csswg-drafts/issues/3503
83 match (&self.count, &other.count) {
84 (&generics::RepeatCount::Number(from), &generics::RepeatCount::Number(to))
85 if from == to =>
86 {
87 ()
88 },
89 (_, _) => return Err(()),
90 }
91
92 let count = self.count;
93 let track_sizes = super::lists::by_computed_value::animate(
94 &self.track_sizes,
95 &other.track_sizes,
96 procedure,
97 )?;
98
99 // The length of |line_names| is always 0 or N+1, where N is the length
100 // of |track_sizes|. Besides, <line-names> is always discrete.
101 let line_names = discrete(&self.line_names, &other.line_names, procedure)?;
102
103 Ok(generics::TrackRepeat {
104 count,
105 line_names,
106 track_sizes,
107 })
108 }
109}
110
111impl Animate for TrackList {
112 // Based on https://github.com/w3c/csswg-drafts/issues/3201:
113 // 1. Check interpolation type per track, so we need to handle discrete animations
114 // in TrackSize, so any Err(()) returned from TrackSize doesn't make all TrackSize
115 // fallback to discrete animation.
116 // 2. line-names is always discrete.
117 fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
118 if self.values.len() != other.values.len() {
119 return Err(());
120 }
121
122 if self.is_explicit() != other.is_explicit() {
123 return Err(());
124 }
125
126 // For now, repeat(auto-fill/auto-fit, ...) is not animatable.
127 // TrackRepeat will return Err(()) if we use keywords. Therefore, we can
128 // early return here to avoid traversing |values| in <auto-track-list>.
129 // This may be updated in the future.
130 // https://github.com/w3c/csswg-drafts/issues/3503
131 if self.has_auto_repeat() || other.has_auto_repeat() {
132 return Err(());
133 }
134
135 let values =
136 super::lists::by_computed_value::animate(&self.values, &other.values, procedure)?;
137
138 // The length of |line_names| is always 0 or N+1, where N is the length
139 // of |track_sizes|. Besides, <line-names> is always discrete.
140 let line_names = discrete(&self.line_names, &other.line_names, procedure)?;
141
142 Ok(TrackList {
143 values,
144 line_names,
145 auto_repeat_index: self.auto_repeat_index,
146 })
147 }
148}
149
150impl ComputeSquaredDistance for GridTemplateComponent {
151 #[inline]
152 fn compute_squared_distance(&self, _other: &Self) -> Result<SquaredDistance, ()> {
153 // TODO: Bug 1518585, we should implement ComputeSquaredDistance.
154 Err(())
155 }
156}
157
158impl ToAnimatedZero for GridTemplateComponent {
159 #[inline]
160 fn to_animated_zero(&self) -> Result<Self, ()> {
161 // It's not clear to get a zero grid track list based on the current definition
162 // of spec, so we return Err(()) directly.
163 Err(())
164 }
165}