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    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#[allow(missing_docs)]
85#[derive(
86    Clone,
87    Copy,
88    Debug,
89    Eq,
90    MallocSizeOf,
91    Parse,
92    PartialEq,
93    ToComputedValue,
94    ToCss,
95    ToResolvedValue,
96    ToShmem,
97    Serialize,
98    Deserialize,
99)]
100#[repr(u8)]
101pub enum StepPosition {
102    JumpStart,
103    JumpEnd,
104    JumpNone,
105    JumpBoth,
106    Start,
107    End,
108}
109
110#[inline]
111fn is_end(position: &StepPosition) -> bool {
112    *position == StepPosition::JumpEnd || *position == StepPosition::End
113}
114
115impl<Integer, Number, LinearStops> TimingFunction<Integer, Number, LinearStops> {
116    /// `ease`
117    #[inline]
118    pub fn ease() -> Self {
119        TimingFunction::Keyword(TimingKeyword::Ease)
120    }
121
122    /// Returns true if it is `ease`.
123    #[inline]
124    pub fn is_ease(&self) -> bool {
125        matches!(*self, TimingFunction::Keyword(TimingKeyword::Ease))
126    }
127}