style/values/generics/
easing.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//! Generic types for CSS Easing Functions.
6//! https://drafts.csswg.org/css-easing/#timing-functions
7
8use crate::parser::ParserContext;
9
10/// A generic easing function.
11#[derive(
12    Clone,
13    Debug,
14    MallocSizeOf,
15    PartialEq,
16    SpecifiedValueInfo,
17    ToCss,
18    ToShmem,
19    Serialize,
20    Deserialize,
21)]
22#[value_info(ty = "TIMING_FUNCTION")]
23#[repr(u8, C)]
24pub enum TimingFunction<Integer, Number, LinearStops> {
25    /// `linear | ease | ease-in | ease-out | ease-in-out`
26    Keyword(TimingKeyword),
27    /// `cubic-bezier(<number>, <number>, <number>, <number>)`
28    #[allow(missing_docs)]
29    #[css(comma, function)]
30    CubicBezier {
31        x1: Number,
32        y1: Number,
33        x2: Number,
34        y2: Number,
35    },
36    /// `step-start | step-end | steps(<integer>, [ <step-position> ]?)`
37    /// `<step-position> = jump-start | jump-end | jump-none | jump-both | start | end`
38    #[css(comma, function)]
39    #[value_info(other_values = "step-start,step-end")]
40    Steps(Integer, #[css(skip_if = "is_end")] StepPosition),
41    /// linear([<linear-stop>]#)
42    /// <linear-stop> = <output> && <linear-stop-length>?
43    /// <linear-stop-length> = <percentage>{1, 2}
44    #[css(function = "linear")]
45    LinearFunction(LinearStops),
46}
47
48#[allow(missing_docs)]
49#[derive(
50    Clone,
51    Copy,
52    Debug,
53    Eq,
54    MallocSizeOf,
55    Parse,
56    PartialEq,
57    SpecifiedValueInfo,
58    ToComputedValue,
59    ToCss,
60    ToResolvedValue,
61    ToShmem,
62    Serialize,
63    Deserialize,
64)]
65#[repr(u8)]
66pub enum TimingKeyword {
67    Linear,
68    Ease,
69    EaseIn,
70    EaseOut,
71    EaseInOut,
72}
73
74/// Before flag, defined as per https://drafts.csswg.org/css-easing/#before-flag
75/// This flag is never user-specified.
76#[allow(missing_docs)]
77#[derive(PartialEq)]
78#[repr(u8)]
79pub enum BeforeFlag {
80    Unset,
81    Set,
82}
83
84#[cfg(feature = "gecko")]
85fn step_position_jump_enabled(_context: &ParserContext) -> bool {
86    true
87}
88
89#[cfg(feature = "servo")]
90fn step_position_jump_enabled(_context: &ParserContext) -> bool {
91    false
92}
93
94#[allow(missing_docs)]
95#[derive(
96    Clone,
97    Copy,
98    Debug,
99    Eq,
100    MallocSizeOf,
101    Parse,
102    PartialEq,
103    ToComputedValue,
104    ToCss,
105    ToResolvedValue,
106    ToShmem,
107    Serialize,
108    Deserialize,
109)]
110#[repr(u8)]
111pub enum StepPosition {
112    #[parse(condition = "step_position_jump_enabled")]
113    JumpStart,
114    #[parse(condition = "step_position_jump_enabled")]
115    JumpEnd,
116    #[parse(condition = "step_position_jump_enabled")]
117    JumpNone,
118    #[parse(condition = "step_position_jump_enabled")]
119    JumpBoth,
120    Start,
121    End,
122}
123
124#[inline]
125fn is_end(position: &StepPosition) -> bool {
126    *position == StepPosition::JumpEnd || *position == StepPosition::End
127}
128
129impl<Integer, Number, LinearStops> TimingFunction<Integer, Number, LinearStops> {
130    /// `ease`
131    #[inline]
132    pub fn ease() -> Self {
133        TimingFunction::Keyword(TimingKeyword::Ease)
134    }
135
136    /// Returns true if it is `ease`.
137    #[inline]
138    pub fn is_ease(&self) -> bool {
139        matches!(*self, TimingFunction::Keyword(TimingKeyword::Ease))
140    }
141}