iced-core-kora 0.15.0

The essential ideas of iced (Playtron's Kora fork of iced_core)
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
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Operate on widgets that can be scrolled.
use crate::animation::Easing;
use crate::widget::operation::Outcome;
use crate::widget::{Id, Operation};
use crate::{Rectangle, Vector};

/// The internal state of a widget that can be scrolled.
pub trait Scrollable {
    /// Snaps the scroll of the widget to the given `percentage` along the horizontal & vertical axis.
    fn snap_to(&mut self, offset: RelativeOffset<Option<f32>>);

    /// Scroll the widget to the given [`AbsoluteOffset`] along the horizontal & vertical axis.
    fn scroll_to(&mut self, offset: AbsoluteOffset<Option<f32>>);

    /// Scroll the widget by the given [`AbsoluteOffset`] along the horizontal & vertical axis.
    fn scroll_by(&mut self, offset: AbsoluteOffset, bounds: Rectangle, content_bounds: Rectangle);

    /// Start an animated scroll to the given [`AbsoluteOffset`].
    ///
    /// The default implementation falls back to [`scroll_to`](Scrollable::scroll_to)
    /// (instant, no animation).
    fn scroll_to_animated(
        &mut self,
        offset: AbsoluteOffset<Option<f32>>,
        _animation: ScrollAnimation,
    ) {
        self.scroll_to(offset);
    }
}

/// Configuration for animated scrolling.
///
/// When attached to an [`EnsureVisibleConfig`], the scrollable will smoothly
/// animate to the target position using the configured [`Easing`] curve.
#[derive(Debug, Clone, Copy)]
pub struct ScrollAnimation {
    /// Duration of the animation in seconds.
    pub duration_secs: f32,
    /// The easing curve applied to the animation.
    pub easing: Easing,
}

impl Default for ScrollAnimation {
    /// Default animation: 0.3 seconds with ease-out cubic.
    fn default() -> Self {
        Self {
            duration_secs: 0.3,
            easing: Easing::EaseOutCubic,
        }
    }
}

impl ScrollAnimation {
    /// Creates a new animation with the given duration in seconds
    /// and the default easing ([`Easing::EaseOutCubic`]).
    #[must_use]
    pub fn new(duration_secs: f32) -> Self {
        Self {
            duration_secs: duration_secs.max(0.0),
            easing: Easing::EaseOutCubic,
        }
    }

    /// Sets the easing curve for this animation.
    #[must_use]
    pub const fn with_easing(mut self, easing: Easing) -> Self {
        self.easing = easing;
        self
    }
}

/// Produces an [`Operation`] that snaps the widget with the given [`Id`] to
/// the provided `percentage`.
pub fn snap_to<T>(target: Id, offset: RelativeOffset<Option<f32>>) -> impl Operation<T> {
    struct SnapTo {
        target: Id,
        offset: RelativeOffset<Option<f32>>,
    }

    impl<T> Operation<T> for SnapTo {
        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
            operate(self);
        }

        fn scrollable(
            &mut self,
            id: Option<&Id>,
            _bounds: Rectangle,
            _content_bounds: Rectangle,
            _translation: Vector,
            state: &mut dyn Scrollable,
        ) {
            if Some(&self.target) == id {
                state.snap_to(self.offset);
            }
        }
    }

    SnapTo { target, offset }
}

/// Produces an [`Operation`] that scrolls the widget with the given [`Id`] to
/// the provided [`AbsoluteOffset`].
pub fn scroll_to<T>(target: Id, offset: AbsoluteOffset<Option<f32>>) -> impl Operation<T> {
    struct ScrollTo {
        target: Id,
        offset: AbsoluteOffset<Option<f32>>,
    }

    impl<T> Operation<T> for ScrollTo {
        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
            operate(self);
        }

        fn scrollable(
            &mut self,
            id: Option<&Id>,
            _bounds: Rectangle,
            _content_bounds: Rectangle,
            _translation: Vector,
            state: &mut dyn Scrollable,
        ) {
            if Some(&self.target) == id {
                state.scroll_to(self.offset);
            }
        }
    }

    ScrollTo { target, offset }
}

/// Produces an [`Operation`] that animates the widget with the given [`Id`]
/// to the provided [`AbsoluteOffset`].
pub fn scroll_to_animated<T>(
    target: Id,
    offset: AbsoluteOffset<Option<f32>>,
    animation: ScrollAnimation,
) -> impl Operation<T> {
    struct ScrollToAnimated {
        target: Id,
        offset: AbsoluteOffset<Option<f32>>,
        animation: ScrollAnimation,
    }

    impl<T> Operation<T> for ScrollToAnimated {
        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
            operate(self);
        }

        fn scrollable(
            &mut self,
            id: Option<&Id>,
            _bounds: Rectangle,
            _content_bounds: Rectangle,
            _translation: Vector,
            state: &mut dyn Scrollable,
        ) {
            if Some(&self.target) == id {
                state.scroll_to_animated(self.offset, self.animation);
            }
        }
    }

    ScrollToAnimated {
        target,
        offset,
        animation,
    }
}

/// Produces an [`Operation`] that scrolls the widget with the given [`Id`] by
/// the provided [`AbsoluteOffset`].
pub fn scroll_by<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> {
    struct ScrollBy {
        target: Id,
        offset: AbsoluteOffset,
    }

    impl<T> Operation<T> for ScrollBy {
        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
            operate(self);
        }

        fn scrollable(
            &mut self,
            id: Option<&Id>,
            bounds: Rectangle,
            content_bounds: Rectangle,
            _translation: Vector,
            state: &mut dyn Scrollable,
        ) {
            if Some(&self.target) == id {
                state.scroll_by(self.offset, bounds, content_bounds);
            }
        }
    }

    ScrollBy { target, offset }
}

/// The amount of absolute offset in each direction of a [`Scrollable`].
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct AbsoluteOffset<T = f32> {
    /// The amount of horizontal offset
    pub x: T,
    /// The amount of vertical offset
    pub y: T,
}

impl From<AbsoluteOffset> for AbsoluteOffset<Option<f32>> {
    fn from(offset: AbsoluteOffset) -> Self {
        Self {
            x: Some(offset.x),
            y: Some(offset.y),
        }
    }
}

/// The amount of relative offset in each direction of a [`Scrollable`].
///
/// A value of `0.0` means start, while `1.0` means end.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct RelativeOffset<T = f32> {
    /// The amount of horizontal offset
    pub x: T,
    /// The amount of vertical offset
    pub y: T,
}

impl RelativeOffset {
    /// A relative offset that points to the top-left of a [`Scrollable`].
    pub const START: Self = Self { x: 0.0, y: 0.0 };

    /// A relative offset that points to the bottom-right of a [`Scrollable`].
    pub const END: Self = Self { x: 1.0, y: 1.0 };
}

impl From<RelativeOffset> for RelativeOffset<Option<f32>> {
    fn from(offset: RelativeOffset) -> Self {
        Self {
            x: Some(offset.x),
            y: Some(offset.y),
        }
    }
}

/// Configuration for [`ensure_focused_visible`].
///
/// Controls how the focused widget is positioned within the scrollable
/// viewport after scrolling.
#[derive(Debug, Clone, Copy)]
pub struct EnsureVisibleConfig {
    /// Vertical alignment of the focused widget within the viewport.
    ///
    /// - `0.0` — align to the top edge
    /// - `0.35` — 35% from the top (Flutter Grid default)
    /// - `0.5` — center vertically
    /// - `1.0` — align to the bottom edge
    pub alignment_y: f32,

    /// Horizontal alignment of the focused widget within the viewport.
    ///
    /// - `0.0` — align to the left edge
    /// - `0.5` — center horizontally
    /// - `1.0` — align to the right edge
    pub alignment_x: f32,

    /// Optional animation. When `Some`, the scrollable animates smoothly
    /// to the target position. When `None`, it jumps instantly.
    pub animation: Option<ScrollAnimation>,
}

impl Default for EnsureVisibleConfig {
    /// Default: 35% alignment on both axes, animated with default duration.
    fn default() -> Self {
        Self {
            alignment_y: Self::DEFAULT_ALIGNMENT,
            alignment_x: Self::DEFAULT_ALIGNMENT,
            animation: Some(ScrollAnimation::default()),
        }
    }
}

impl EnsureVisibleConfig {
    /// Default alignment ratio (35% from the top/left of the viewport).
    pub const DEFAULT_ALIGNMENT: f32 = 0.35;

    /// Creates a config with custom vertical and horizontal alignment.
    /// Animated by default.
    #[must_use]
    pub fn new(alignment_y: f32, alignment_x: f32) -> Self {
        Self {
            alignment_y: alignment_y.clamp(0.0, 1.0),
            alignment_x: alignment_x.clamp(0.0, 1.0),
            animation: Some(ScrollAnimation::default()),
        }
    }

    /// Creates a config with a custom vertical alignment.
    ///
    /// Horizontal alignment defaults to [`Self::DEFAULT_ALIGNMENT`]. Animated by default.
    #[must_use]
    pub fn vertical(alignment_y: f32) -> Self {
        Self {
            alignment_y: alignment_y.clamp(0.0, 1.0),
            alignment_x: Self::DEFAULT_ALIGNMENT,
            animation: Some(ScrollAnimation::default()),
        }
    }

    /// Align the focused widget to the center of the viewport on both axes.
    /// Animated by default.
    pub const CENTER: Self = Self {
        alignment_y: 0.5,
        alignment_x: 0.5,
        animation: Some(ScrollAnimation {
            duration_secs: 0.3,
            easing: Easing::EaseOutCubic,
        }),
    };

    /// Sets the animation configuration.
    #[must_use]
    pub const fn with_animation(mut self, animation: ScrollAnimation) -> Self {
        self.animation = Some(animation);
        self
    }

    /// Disables animation — the scroll will jump instantly.
    #[must_use]
    pub const fn instant(mut self) -> Self {
        self.animation = None;
        self
    }
}

/// A self-contained operation that finds the focused widget inside the nearest
/// scrollable ancestor and scrolls it into view — all in one step, without
/// requiring the scrollable to have an explicit [`Id`].
///
/// Phase 1 walks the tree to find the focused widget and its scrollable
/// context. Phase 2 (via [`Outcome::Chain`]) walks the tree again and
/// calls [`Scrollable::scroll_to`] on the matching scrollable.
pub fn ensure_focused_visible_op<T>(config: EnsureVisibleConfig) -> impl Operation<T> {
    EnsureFocusedVisibleOp {
        config,
        scrollable_ctx: None,
        scrollable_index: 0,
        target: None,
    }
}

struct EnsureFocusedVisibleOp {
    config: EnsureVisibleConfig,
    /// The most recently encountered scrollable (tracked by index).
    scrollable_ctx: Option<(usize, ScrollableContext)>,
    scrollable_index: usize,
    /// The computed scroll target: (scrollable tree index, offset).
    target: Option<(usize, AbsoluteOffset<Option<f32>>)>,
}

#[derive(Clone)]
struct ScrollableContext {
    bounds: Rectangle,
    content_bounds: Rectangle,
}

impl<T> Operation<T> for EnsureFocusedVisibleOp {
    fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
        operate(self);
    }

    fn scrollable(
        &mut self,
        _id: Option<&Id>,
        bounds: Rectangle,
        content_bounds: Rectangle,
        _translation: Vector,
        _state: &mut dyn Scrollable,
    ) {
        if self.target.is_some() {
            return;
        }

        self.scrollable_ctx = Some((
            self.scrollable_index,
            ScrollableContext {
                bounds,
                content_bounds,
            },
        ));
        self.scrollable_index += 1;
    }

    fn focusable(&mut self, _id: Option<&Id>, bounds: Rectangle, state: &mut dyn super::Focusable) {
        if self.target.is_some() || !state.is_focused() {
            return;
        }

        let Some((idx, ctx)) = &self.scrollable_ctx else {
            return;
        };

        if let Some(offset) =
            compute_scroll_offset(bounds, ctx.bounds, ctx.content_bounds, self.config)
        {
            self.target = Some((*idx, offset));
        }
    }

    fn finish(&self) -> Outcome<T> {
        if let Some((idx, offset)) = &self.target {
            Outcome::Chain(Box::new(ApplyScrollAtIndex {
                target_index: *idx,
                current: 0,
                offset: *offset,
                animation: self.config.animation,
            }))
        } else {
            Outcome::None
        }
    }
}

/// Phase 2: walks the tree counting scrollables and scrolls the one
/// at the recorded index.
struct ApplyScrollAtIndex {
    target_index: usize,
    current: usize,
    offset: AbsoluteOffset<Option<f32>>,
    animation: Option<ScrollAnimation>,
}

impl<T> Operation<T> for ApplyScrollAtIndex {
    fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
        operate(self);
    }

    fn scrollable(
        &mut self,
        _id: Option<&Id>,
        _bounds: Rectangle,
        _content_bounds: Rectangle,
        _translation: Vector,
        state: &mut dyn Scrollable,
    ) {
        if self.current == self.target_index {
            if let Some(animation) = self.animation {
                state.scroll_to_animated(self.offset, animation);
            } else {
                state.scroll_to(self.offset);
            }
        }
        self.current += 1;
    }
}

/// Computes the absolute scroll offset needed to position a widget
/// at the configured alignment within a scrollable viewport.
///
/// Layout bounds inside a scrollable are **not** translated by the scroll
/// offset — translation is only applied during rendering. So the
/// content-relative position is simply `bounds.origin - scrollable.origin`.
pub fn compute_scroll_offset(
    focused_bounds: Rectangle,
    scrollable_bounds: Rectangle,
    content_bounds: Rectangle,
    config: EnsureVisibleConfig,
) -> Option<AbsoluteOffset<Option<f32>>> {
    // --- Vertical axis ---
    let target_y = if content_bounds.height > scrollable_bounds.height {
        let focused_y = focused_bounds.y - scrollable_bounds.y;
        let focused_center_y = focused_y + focused_bounds.height / 2.0;
        let viewport_target_y = scrollable_bounds.height * config.alignment_y;
        let desired = focused_center_y - viewport_target_y;
        let max_scroll = content_bounds.height - scrollable_bounds.height;
        Some(desired.clamp(0.0, max_scroll))
    } else {
        None
    };

    // --- Horizontal axis ---
    let target_x = if content_bounds.width > scrollable_bounds.width {
        let focused_x = focused_bounds.x - scrollable_bounds.x;
        let focused_center_x = focused_x + focused_bounds.width / 2.0;
        let viewport_target_x = scrollable_bounds.width * config.alignment_x;
        let desired = focused_center_x - viewport_target_x;
        let max_scroll = content_bounds.width - scrollable_bounds.width;
        Some(desired.clamp(0.0, max_scroll))
    } else {
        None
    };

    if target_x.is_some() || target_y.is_some() {
        Some(AbsoluteOffset {
            x: target_x,
            y: target_y,
        })
    } else {
        None
    }
}