Files
tubestation/servo/components/style/values/computed/ratio.rs
Emilio Cobos Álvarez f49da1c698 Bug 1909153 - Fix interaction of transitions and zoom. r=firefox-style-system-reviewers,layout-reviewers,boris
The main issue here is that we transition the zoomed value, which is
generally wrong because the animated value gets injected back into the
cascade (and thus zoomed again).

We happen to cancel the transition in [1], which is kinda nice because
otherwise we would've just transitioned to a completely wrong value (and
maybe indefinitely, since values would keep getting bigger and
bigger...).

We need to do something similar to to_resolved_value, and unzoom lengths
in to_animated_value, that is, interpolate "unzoomed" values.

The extra test to test_transitions_per_property caught some existing
issues with calc() and zoom which are fixed too for the test to pass.

Same for the ToResolvedValue for Au, that is needed for properties like
column-rule-width to return the correct resolved values.

Main thing I left unfixed is bug 1909280, but that deserves a more
subtle test and a bit more thought because only matrix components need
to be zoomed.

While at it, I simplified the animation setup a little bit, removing the
special animation_value_type="ComputedValue", which means that we need
to add a few ToAnimatedValue calls.

Now the only values are "none", "discrete", and "normal", and given it's
not a value type anymore I called it just "animation_type".

This got a bit bigger than I would've liked, but also it fixes more bugs
that what I was originally expecting, so... :)

[1]: https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/layout/style/nsTransitionManager.cpp#168-171

Differential Revision: https://phabricator.services.mozilla.com/D217308
2024-07-23 21:54:01 +00:00

91 lines
3.4 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! `<ratio>` computed values.
use crate::values::animated::{Animate, Procedure};
use crate::values::computed::NonNegativeNumber;
use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
use crate::values::generics::ratio::Ratio as GenericRatio;
use crate::Zero;
use std::cmp::Ordering;
/// A computed <ratio> value.
pub type Ratio = GenericRatio<NonNegativeNumber>;
impl PartialOrd for Ratio {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
f64::partial_cmp(
&((self.0).0 as f64 * (other.1).0 as f64),
&((self.1).0 as f64 * (other.0).0 as f64),
)
}
}
impl GenericRatio<f32> {
/// Returns the f32 value by dividing the first value by the second one.
#[inline]
fn to_f32(&self) -> f32 {
debug_assert!(!self.is_degenerate());
self.0 / self.1
}
}
/// https://drafts.csswg.org/css-values/#combine-ratio
impl Animate for GenericRatio<f32> {
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
// If either <ratio> is degenerate, the values cannot be interpolated.
if self.is_degenerate() || other.is_degenerate() {
return Err(());
}
// Addition of <ratio>s is not possible, and based on
// https://drafts.csswg.org/css-values-4/#not-additive,
// we simply use the first value as the result value.
// Besides, the procedure for accumulation should be identical to addition here.
if matches!(procedure, Procedure::Add | Procedure::Accumulate { .. }) {
return Ok(self.clone());
}
// The interpolation of a <ratio> is defined by converting each <ratio> to a number by
// dividing the first value by the second (so a ratio of 3 / 2 would become 1.5), taking
// the logarithm of that result (so the 1.5 would become approximately 0.176), then
// interpolating those values.
//
// The result during the interpolation is converted back to a <ratio> by inverting the
// logarithm, then interpreting the result as a <ratio> with the result as the first value
// and 1 as the second value.
let start = self.to_f32().ln();
let end = other.to_f32().ln();
let e = std::f32::consts::E;
let result = e.powf(start.animate(&end, procedure)?);
// The range of the result is [0, inf), based on the easing function.
if result.is_zero() || result.is_infinite() {
return Err(());
}
Ok(GenericRatio(result, 1.0))
}
}
impl ComputeSquaredDistance for GenericRatio<f32> {
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
if self.is_degenerate() || other.is_degenerate() {
return Err(());
}
// Use the distance of their logarithm values. (This is used by testing, so don't
// need to care about the base. Here we use the same base as that in animate().)
self.to_f32()
.ln()
.compute_squared_distance(&other.to_f32().ln())
}
}
impl Ratio {
/// Returns a new Ratio.
#[inline]
pub fn new(a: f32, b: f32) -> Self {
GenericRatio(a.into(), b.into())
}
}