Skip to main content

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