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