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(from), generics::TrackSize::Breadth(to)) => {
53 animate_with_discrete_fallback(from, to, procedure)
54 .map(generics::TrackSize::Breadth)
55 },
56 (
57 generics::TrackSize::Minmax(from_min, from_max),
58 generics::TrackSize::Minmax(to_min, 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 (generics::TrackSize::FitContent(from), generics::TrackSize::FitContent(to)) => {
64 animate_with_discrete_fallback(from, to, procedure)
65 .map(generics::TrackSize::FitContent)
66 },
67 (_, _) => discrete(self, other, procedure),
68 }
69 }
70}
71
72impl Animate for generics::TrackRepeat<LengthPercentage, Integer> {
73 fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
74 // If the keyword, auto-fit/fill, is the same it can result in different
75 // number of tracks. For both auto-fit/fill, the number of columns isn't
76 // known until you do layout since it depends on the container size, item
77 // placement and other factors, so we cannot do the correct interpolation
78 // by computed values. Therefore, return Err(()) if it's keywords. If it
79 // is Number, we support animation only if the count is the same and the
80 // length of track_sizes is the same.
81 // https://github.com/w3c/csswg-drafts/issues/3503
82 match (&self.count, &other.count) {
83 (&generics::RepeatCount::Number(from), &generics::RepeatCount::Number(to))
84 if from == to => {},
85 (_, _) => return Err(()),
86 }
87
88 let count = self.count;
89 let track_sizes = super::lists::by_computed_value::animate(
90 &self.track_sizes,
91 &other.track_sizes,
92 procedure,
93 )?;
94
95 // The length of |line_names| is always 0 or N+1, where N is the length
96 // of |track_sizes|. Besides, <line-names> is always discrete.
97 let line_names = discrete(&self.line_names, &other.line_names, procedure)?;
98
99 Ok(generics::TrackRepeat {
100 count,
101 line_names,
102 track_sizes,
103 })
104 }
105}
106
107impl Animate for TrackList {
108 // Based on https://github.com/w3c/csswg-drafts/issues/3201:
109 // 1. Check interpolation type per track, so we need to handle discrete animations
110 // in TrackSize, so any Err(()) returned from TrackSize doesn't make all TrackSize
111 // fallback to discrete animation.
112 // 2. line-names is always discrete.
113 fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
114 if self.values.len() != other.values.len() {
115 return Err(());
116 }
117
118 if self.is_explicit() != other.is_explicit() {
119 return Err(());
120 }
121
122 // For now, repeat(auto-fill/auto-fit, ...) is not animatable.
123 // TrackRepeat will return Err(()) if we use keywords. Therefore, we can
124 // early return here to avoid traversing |values| in <auto-track-list>.
125 // This may be updated in the future.
126 // https://github.com/w3c/csswg-drafts/issues/3503
127 if self.has_auto_repeat() || other.has_auto_repeat() {
128 return Err(());
129 }
130
131 let values =
132 super::lists::by_computed_value::animate(&self.values, &other.values, procedure)?;
133
134 // The length of |line_names| is always 0 or N+1, where N is the length
135 // of |track_sizes|. Besides, <line-names> is always discrete.
136 let line_names = discrete(&self.line_names, &other.line_names, procedure)?;
137
138 Ok(TrackList {
139 values,
140 line_names,
141 auto_repeat_index: self.auto_repeat_index,
142 })
143 }
144}
145
146impl ComputeSquaredDistance for GridTemplateComponent {
147 #[inline]
148 fn compute_squared_distance(&self, _other: &Self) -> Result<SquaredDistance, ()> {
149 // TODO: Bug 1518585, we should implement ComputeSquaredDistance.
150 Err(())
151 }
152}
153
154impl ToAnimatedZero for GridTemplateComponent {
155 #[inline]
156 fn to_animated_zero(&self) -> Result<Self, ()> {
157 // It's not clear to get a zero grid track list based on the current definition
158 // of spec, so we return Err(()) directly.
159 Err(())
160 }
161}