yororen_ui 0.2.0

Reusable UI components and widgets built on top of gpui.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Easing functions for animations.
//!
//! Provides a collection of easing functions for smooth animations.
//! Each function takes a normalized time value (0.0 to 1.0) and returns
//! the eased progress value.

/// A function that maps linear progress to eased progress.
///
/// Note: `gpui::Animation::with_easing` expects the easing output to stay within
/// `[0.0, 1.0]`. Some classic easing curves (e.g. back/elastic) overshoot this
/// range by design. Prefer using the `*_clamped` variants in this module when
/// passing an easing function to gpui.
pub type EasingFn = fn(f32) -> f32;

#[inline]
fn clamp01(t: f32) -> f32 {
    t.clamp(0.0, 1.0)
}

/// Clamp an easing function's output to `[0.0, 1.0]`.
#[inline]
pub fn clamp_easing(easing: EasingFn, t: f32) -> f32 {
    clamp01(easing(t))
}

// ============================================================================
// Linear
// ============================================================================

/// Linear easing (no easing).
pub const fn ease_linear(t: f32) -> f32 {
    t
}

// ============================================================================
// Sine
// ============================================================================

/// Ease in with sine function.
pub fn ease_in_sine(t: f32) -> f32 {
    1.0 - (t * std::f32::consts::FRAC_PI_2).cos()
}

/// Clamped version of [`ease_in_sine`].
pub fn ease_in_sine_clamped(t: f32) -> f32 {
    clamp01(ease_in_sine(t))
}

/// Ease out with sine function.
pub fn ease_out_sine(t: f32) -> f32 {
    (t * std::f32::consts::FRAC_PI_2).sin()
}

/// Clamped version of [`ease_out_sine`].
pub fn ease_out_sine_clamped(t: f32) -> f32 {
    clamp01(ease_out_sine(t))
}

/// Ease in-out with sine function.
pub fn ease_in_out_sine(t: f32) -> f32 {
    -(t * std::f32::consts::PI).cos() / 2.0 + 0.5
}

/// Clamped version of [`ease_in_out_sine`].
pub fn ease_in_out_sine_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_sine(t))
}

// ============================================================================
// Quadratic (Power 2)
// ============================================================================

/// Ease in with quadratic function (t^2).
pub const fn ease_in_quad(t: f32) -> f32 {
    t * t
}

pub fn ease_in_quad_clamped(t: f32) -> f32 {
    clamp01(ease_in_quad(t))
}

/// Ease out with quadratic function (t^2).
pub const fn ease_out_quad(t: f32) -> f32 {
    1.0 - (1.0 - t) * (1.0 - t)
}

pub fn ease_out_quad_clamped(t: f32) -> f32 {
    clamp01(ease_out_quad(t))
}

/// Ease in-out with quadratic function (t^2).
pub fn ease_in_out_quad(t: f32) -> f32 {
    if t < 0.5 {
        2.0 * t * t
    } else {
        1.0 - (2.0 - 2.0 * t) * (2.0 - 2.0 * t) / 2.0
    }
}

pub fn ease_in_out_quad_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_quad(t))
}

// ============================================================================
// Cubic (Power 3)
// ============================================================================

/// Ease in with cubic function (t^3).
pub const fn ease_in_cubic(t: f32) -> f32 {
    t * t * t
}

pub fn ease_in_cubic_clamped(t: f32) -> f32 {
    clamp01(ease_in_cubic(t))
}

/// Ease out with cubic function (t^3).
pub const fn ease_out_cubic(t: f32) -> f32 {
    let t = 1.0 - t;
    1.0 - t * t * t
}

pub fn ease_out_cubic_clamped(t: f32) -> f32 {
    clamp01(ease_out_cubic(t))
}

/// Ease in-out with cubic function (t^3).
pub const fn ease_in_out_cubic(t: f32) -> f32 {
    if t < 0.5 {
        4.0 * t * t * t
    } else {
        let t = 2.0 * t - 2.0;
        (t * t * t + 2.0) / 2.0
    }
}

pub fn ease_in_out_cubic_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_cubic(t))
}

// ============================================================================
// Quartic (Power 4)
// ============================================================================

/// Ease in with quartic function (t^4).
pub const fn ease_in_quart(t: f32) -> f32 {
    t * t * t * t
}

pub fn ease_in_quart_clamped(t: f32) -> f32 {
    clamp01(ease_in_quart(t))
}

/// Ease out with quartic function (t^4).
pub const fn ease_out_quart(t: f32) -> f32 {
    let t = 1.0 - t;
    1.0 - t * t * t * t
}

pub fn ease_out_quart_clamped(t: f32) -> f32 {
    clamp01(ease_out_quart(t))
}

/// Ease in-out with quartic function (t^4).
#[allow(dead_code)]
pub const fn ease_in_out_quart(t: f32) -> f32 {
    if t < 0.5 {
        8.0 * t * t * t * t
    } else {
        let t = 1.0 - t;
        1.0 - 8.0 * t * t * t * t
    }
}

pub fn ease_in_out_quart_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_quart(t))
}

// ============================================================================
// Quintic (Power 5)
// ============================================================================

/// Ease in with quintic function (t^5).
pub const fn ease_in_quint(t: f32) -> f32 {
    t * t * t * t * t
}

pub fn ease_in_quint_clamped(t: f32) -> f32 {
    clamp01(ease_in_quint(t))
}

/// Ease out with quintic function (t^5).
pub const fn ease_out_quint(t: f32) -> f32 {
    let t = 1.0 - t;
    1.0 - t * t * t * t * t
}

pub fn ease_out_quint_clamped(t: f32) -> f32 {
    clamp01(ease_out_quint(t))
}

/// Ease in-out with quintic function (t^5).
pub const fn ease_in_out_quint(t: f32) -> f32 {
    if t < 0.5 {
        16.0 * t * t * t * t * t
    } else {
        let t = 2.0 * t - 2.0;
        (t * t * t * t * t + 2.0) / 2.0
    }
}

pub fn ease_in_out_quint_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_quint(t))
}

// ============================================================================
// Exponential
// ============================================================================

/// Ease in with exponential function (2^10*(t-1)).
pub fn ease_in_expo(t: f32) -> f32 {
    if t == 0.0 {
        0.0
    } else {
        2.0_f32.powf(10.0 * t - 10.0)
    }
}

pub fn ease_in_expo_clamped(t: f32) -> f32 {
    clamp01(ease_in_expo(t))
}

/// Ease out with exponential function (-2^(-10*t) + 1).
pub fn ease_out_expo(t: f32) -> f32 {
    if t == 1.0 {
        1.0
    } else {
        1.0 - 2.0_f32.powf(-10.0 * t)
    }
}

pub fn ease_out_expo_clamped(t: f32) -> f32 {
    clamp01(ease_out_expo(t))
}

/// Ease in-out with exponential function.
pub fn ease_in_out_expo(t: f32) -> f32 {
    if t == 0.0 {
        0.0
    } else if t == 1.0 {
        1.0
    } else if t < 0.5 {
        2.0_f32.powf(20.0 * t - 10.0) / 2.0
    } else {
        (2.0 - 2.0_f32.powf(-20.0 * t + 10.0)) / 2.0
    }
}

pub fn ease_in_out_expo_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_expo(t))
}

// ============================================================================
// Circular
// ============================================================================

/// Ease in with circular function.
pub fn ease_in_circ(t: f32) -> f32 {
    1.0 - (1.0 - t * t).sqrt()
}

pub fn ease_in_circ_clamped(t: f32) -> f32 {
    clamp01(ease_in_circ(t))
}

/// Ease out with circular function.
pub fn ease_out_circ(t: f32) -> f32 {
    let t = t - 1.0;
    (1.0 - t * t).sqrt()
}

pub fn ease_out_circ_clamped(t: f32) -> f32 {
    clamp01(ease_out_circ(t))
}

/// Ease in-out with circular function.
#[allow(dead_code)]
pub fn ease_in_out_circ(t: f32) -> f32 {
    if t < 0.5 {
        (1.0 - (1.0 - 4.0 * t * t).sqrt()) / 2.0
    } else {
        ((4.0 * t * t - 4.0 * t + 1.0).sqrt() + 1.0) / 2.0
    }
}

pub fn ease_in_out_circ_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_circ(t))
}

// ============================================================================
// Back (Overshoot)
// ============================================================================

/// Ease in with back/overshoot effect.
#[allow(dead_code)]
pub fn ease_in_back(t: f32) -> f32 {
    let c1 = 1.70158;
    let c3 = c1 + 1.0;
    c3 * t * t * t - c1 * t * t
}

/// Clamped version of [`ease_in_back`].
pub fn ease_in_back_clamped(t: f32) -> f32 {
    clamp01(ease_in_back(t))
}

/// Ease out with back/overshoot effect.
pub fn ease_out_back(t: f32) -> f32 {
    let c1 = 1.70158;
    let c3 = c1 + 1.0;
    1.0 + c3 * (t - 1.0).powi(3) + c1 * (t - 1.0).powi(2)
}

/// Clamped version of [`ease_out_back`].
pub fn ease_out_back_clamped(t: f32) -> f32 {
    clamp01(ease_out_back(t))
}

/// Ease in-out with back/overshoot effect.
pub fn ease_in_out_back(t: f32) -> f32 {
    let c1 = 1.70158;
    let c2 = c1 * 1.525;
    if t < 0.5 {
        ((2.0 * t).powi(2) * ((c2 + 1.0) * 2.0 * t - c2)) / 2.0
    } else {
        ((2.0 * t - 2.0).powi(2) * ((c2 + 1.0) * (t * 2.0 - 2.0) + c2) + 2.0) / 2.0
    }
}

/// Clamped version of [`ease_in_out_back`].
pub fn ease_in_out_back_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_back(t))
}

// ============================================================================
// Elastic (Spring-like)
// ============================================================================

/// Ease in with elastic/spring effect.
pub fn ease_in_elastic(t: f32) -> f32 {
    if t == 0.0 {
        0.0
    } else if t == 1.0 {
        1.0
    } else {
        let c4 = (2.0 * std::f32::consts::PI) / 3.0;
        -(2.0_f32.powf(10.0 * t - 10.0)) * ((t * 10.0 - 10.75) * c4).sin()
    }
}

/// Clamped version of [`ease_in_elastic`].
pub fn ease_in_elastic_clamped(t: f32) -> f32 {
    clamp01(ease_in_elastic(t))
}

/// Ease out with elastic/spring effect.
pub fn ease_out_elastic(t: f32) -> f32 {
    if t == 0.0 {
        0.0
    } else if t == 1.0 {
        1.0
    } else {
        let c4 = (2.0 * std::f32::consts::PI) / 3.0;
        2.0_f32.powf(-10.0 * t) * ((t * 10.0 - 0.75) * c4).sin() + 1.0
    }
}

/// Clamped version of [`ease_out_elastic`].
pub fn ease_out_elastic_clamped(t: f32) -> f32 {
    clamp01(ease_out_elastic(t))
}

/// Ease in-out with elastic/spring effect.
pub fn ease_in_out_elastic(t: f32) -> f32 {
    if t == 0.0 {
        0.0
    } else if t == 1.0 {
        1.0
    } else {
        let c5 = (2.0 * std::f32::consts::PI) / 4.5;
        if t < 0.5 {
            -(2.0_f32.powf(20.0 * t - 10.0) * ((20.0 * t - 11.125) * c5).sin()) / 2.0
        } else {
            (2.0_f32.powf(-20.0 * t + 10.0) * ((20.0 * t - 11.125) * c5).sin()) / 2.0 + 1.0
        }
    }
}

/// Clamped version of [`ease_in_out_elastic`].
pub fn ease_in_out_elastic_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_elastic(t))
}

// ============================================================================
// Bounce
// ============================================================================

/// Ease in with bounce effect.
pub fn ease_in_bounce(t: f32) -> f32 {
    1.0 - ease_out_bounce(1.0 - t)
}

pub fn ease_in_bounce_clamped(t: f32) -> f32 {
    clamp01(ease_in_bounce(t))
}

/// Ease out with bounce effect.
pub fn ease_out_bounce(t: f32) -> f32 {
    let n1 = 7.5625;
    let d1 = 2.75;

    if t < 1.0 / d1 {
        n1 * t * t
    } else if t < 2.0 / d1 {
        let t = t - 1.5 / d1;
        n1 * t * t + 0.75
    } else if t < 2.5 / d1 {
        let t = t - 2.25 / d1;
        n1 * t * t + 0.9375
    } else {
        let t = t - 2.625 / d1;
        n1 * t * t + 0.984375
    }
}

pub fn ease_out_bounce_clamped(t: f32) -> f32 {
    clamp01(ease_out_bounce(t))
}

/// Ease in-out with bounce effect.
pub fn ease_in_out_bounce(t: f32) -> f32 {
    if t < 0.5 {
        (1.0 - ease_out_bounce(1.0 - 2.0 * t)) / 2.0
    } else {
        (1.0 + ease_out_bounce(2.0 * t - 1.0)) / 2.0
    }
}

pub fn ease_in_out_bounce_clamped(t: f32) -> f32 {
    clamp01(ease_in_out_bounce(t))
}

// ============================================================================
// Basic easing (matching gpui)
// ============================================================================

/// Ease in (simple quadratic).
pub const fn ease_in(t: f32) -> f32 {
    t * t
}

#[allow(dead_code)]
pub fn ease_in_clamped(t: f32) -> f32 {
    clamp01(ease_in(t))
}

/// Ease out (simple quadratic).
pub const fn ease_out(t: f32) -> f32 {
    1.0 - (1.0 - t) * (1.0 - t)
}

#[allow(dead_code)]
pub fn ease_out_clamped(t: f32) -> f32 {
    clamp01(ease_out(t))
}

/// Ease in-out (simple quadratic).
pub fn ease_in_out(t: f32) -> f32 {
    if t < 0.5 {
        2.0 * t * t
    } else {
        1.0 - (2.0 - 2.0 * t) * (2.0 - 2.0 * t) / 2.0
    }
}

#[allow(dead_code)]
pub fn ease_in_out_clamped(t: f32) -> f32 {
    clamp01(ease_in_out(t))
}