style: Rust side plumbing work for linear easing function

Add LinearFunction to TimingFunction. Because the linear function is a
variable list of linear stops, the enum is no longer Copyable.

Differential Revision: https://phabricator.services.mozilla.com/D146837
This commit is contained in:
David Shin 2022-06-07 11:51:24 +00:00 committed by Martin Robinson
parent 898cafb5a5
commit 6326a384a8
6 changed files with 81 additions and 25 deletions

View file

@ -4,11 +4,14 @@
//! Computed types for CSS Easing functions.
use crate::values::computed::{Integer, Number};
use crate::values::computed::{Integer, Number, Percentage};
use crate::values::generics::easing;
/// A computed timing function.
pub type ComputedTimingFunction = easing::TimingFunction<Integer, Number>;
pub type ComputedTimingFunction = easing::TimingFunction<Integer, Number, Percentage>;
/// An alias of the computed timing function.
pub type TimingFunction = ComputedTimingFunction;
/// A computed linear easing entry.
pub type ComputedLinearStop = easing::LinearStop<Number, Percentage>;

View file

@ -6,8 +6,9 @@
//! https://drafts.csswg.org/css-easing/#timing-functions
use crate::parser::ParserContext;
use crate::values::generics::Optional;
/// A generic easing function.
/// An entry for linear easing function.
#[derive(
Clone,
Copy,
@ -20,9 +21,33 @@ use crate::parser::ParserContext;
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct LinearStop<Number, Percentage> {
/// Output of the function at the given point.
pub output: Number,
/// Playback progress at which this output starts.
#[css(skip_if = "Optional::is_none")]
pub input_start: Optional<Percentage>,
/// Playback progress at which this output ends.
#[css(skip_if = "Optional::is_none")]
pub input_end: Optional<Percentage>,
}
/// A generic easing function.
#[derive(
Clone,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[value_info(ty = "TIMING_FUNCTION")]
#[repr(u8, C)]
pub enum TimingFunction<Integer, Number> {
pub enum TimingFunction<Integer, Number, Percentage> {
/// `linear | ease | ease-in | ease-out | ease-in-out`
Keyword(TimingKeyword),
/// `cubic-bezier(<number>, <number>, <number>, <number>)`
@ -39,6 +64,11 @@ pub enum TimingFunction<Integer, Number> {
#[css(comma, function)]
#[value_info(other_values = "step-start,step-end")]
Steps(Integer, #[css(skip_if = "is_end")] StepPosition),
/// linear([<linear-stop>]#)
/// <linear-stop> = <output> && <linear-stop-length>?
/// <linear-stop-length> = <percentage>{1, 2}
#[css(comma, function = "linear")]
LinearFunction(#[css(iterable)] crate::OwnedSlice<LinearStop<Number, Percentage>>),
}
#[allow(missing_docs)]
@ -110,7 +140,7 @@ fn is_end(position: &StepPosition) -> bool {
*position == StepPosition::JumpEnd || *position == StepPosition::End
}
impl<Integer, Number> TimingFunction<Integer, Number> {
impl<Integer, Number, Percentage> TimingFunction<Integer, Number, Percentage> {
/// `ease`
#[inline]
pub fn ease() -> Self {

View file

@ -3,18 +3,20 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Specified types for CSS Easing functions.
use crate::parser::{Parse, ParserContext};
use crate::values::computed::easing::ComputedLinearStop;
use crate::values::computed::easing::TimingFunction as ComputedTimingFunction;
use crate::values::computed::Percentage as ComputedPercentage;
use crate::values::generics::easing::TimingFunction as GenericTimingFunction;
use crate::values::generics::easing::{StepPosition, TimingKeyword};
use crate::values::specified::{Integer, Number};
use crate::values::specified::{Integer, Number, Percentage};
use cssparser::Parser;
use selectors::parser::SelectorParseErrorKind;
use std::iter::FromIterator;
use style_traits::{ParseError, StyleParseErrorKind};
/// A specified timing function.
pub type TimingFunction = GenericTimingFunction<Integer, Number>;
pub type TimingFunction = GenericTimingFunction<Integer, Number, Percentage>;
impl Parse for TimingFunction {
fn parse<'i, 't>(
@ -88,9 +90,9 @@ impl Parse for TimingFunction {
impl TimingFunction {
/// Generate the ComputedTimingFunction without Context.
pub fn to_computed_value_without_context(&self) -> ComputedTimingFunction {
match *self {
match &self {
GenericTimingFunction::Steps(steps, pos) => {
GenericTimingFunction::Steps(steps.value(), pos)
GenericTimingFunction::Steps(steps.value(), *pos)
},
GenericTimingFunction::CubicBezier { x1, y1, x2, y2 } => {
GenericTimingFunction::CubicBezier {
@ -100,7 +102,23 @@ impl TimingFunction {
y2: y2.get(),
}
},
GenericTimingFunction::Keyword(keyword) => GenericTimingFunction::Keyword(keyword),
GenericTimingFunction::Keyword(keyword) => GenericTimingFunction::Keyword(*keyword),
GenericTimingFunction::LinearFunction(steps) => {
let iter = steps.iter().map(|e| ComputedLinearStop {
output: e.output.get(),
input_start: e
.input_start
.into_rust()
.map(|x| ComputedPercentage(x.get()))
.into(),
input_end: e
.input_end
.into_rust()
.map(|x| ComputedPercentage(x.get()))
.into(),
});
GenericTimingFunction::LinearFunction(crate::OwnedSlice::from_iter(iter))
},
}
}
}