freya-components 0.5.0-rc.13

Components for Freya apps
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use freya_core::prelude::*;
use torin::{
    content::Content,
    node::Node,
    prelude::{
        Area,
        Direction,
        Length,
    },
    size::Size,
};

use crate::{
    define_theme,
    get_theme,
};

define_theme! {
    %[component]
    pub ResizableHandle {
        %[fields]
        background: Color,
        hover_background: Color,
        corner_radius: CornerRadius,
    }
}

/// Sizing mode for a resizable panel.
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum PanelSize {
    /// Fixed pixel size.
    Pixels(Length),
    /// Proportional flex weight distributed among other percentage panels.
    Percentage(Length),
}

impl PanelSize {
    pub fn px(v: f32) -> Self {
        Self::Pixels(Length::new(v))
    }

    pub fn percent(v: f32) -> Self {
        Self::Percentage(Length::new(v))
    }

    pub fn value(&self) -> f32 {
        match self {
            Self::Pixels(v) | Self::Percentage(v) => v.get(),
        }
    }

    /// The upper bound for this sizing mode.
    fn max_size(&self) -> f32 {
        match self {
            Self::Pixels(_) => f32::MAX,
            Self::Percentage(_) => 100.,
        }
    }

    /// Scale factor to convert between pixels and this panel's unit system.
    fn flex_scale(&self, flex_factor: f32) -> f32 {
        match self {
            Self::Pixels(_) => 1.0,
            Self::Percentage(_) => flex_factor,
        }
    }
}

/// A panel's size and constraints.
#[derive(Clone, Copy, Debug)]
pub struct Panel {
    pub size: f32,
    pub initial_size: f32,
    pub min_size: f32,
    pub sizing: PanelSize,
}

impl Panel {
    /// Rebalances percentage panel sizes when this panel is added or removed,
    /// leaving fixed-size panels unchanged and respecting minimum sizes.
    fn redistribute<'a>(&self, panels: impl Iterator<Item = &'a mut Panel>, adding: bool) {
        if matches!(self.sizing, PanelSize::Pixels(_)) {
            return;
        }

        let mut buffer = self.size;
        for panel in panels.filter(|panel| matches!(panel.sizing, PanelSize::Percentage(_))) {
            let adjustment = (panel.initial_size - panel.size).min(buffer);
            if !adding || adjustment >= 0. {
                let adjustment = if adding { -adjustment } else { adjustment };
                panel.size = (panel.size + adjustment).max(panel.min_size);
                buffer -= panel.initial_size - panel.size;
            }
        }
    }
}

/// Resize state for a container. Panel membership and order come from its declarations.
#[derive(Default)]
pub struct ResizableContext {
    panels: Vec<(DiffKey, Panel)>,
    direction: Direction,
    dragging: Option<usize>,
}

impl ResizableContext {
    pub const HANDLE_SIZE: f32 = 4.0;

    pub fn direction(&self) -> Direction {
        self.direction
    }

    /// Returns the panels in declaration order.
    pub fn panels(&self) -> Vec<Panel> {
        self.panels.iter().map(|(_, panel)| *panel).collect()
    }

    fn reconcile(&mut self, mut panels: Vec<(DiffKey, Panel)>, direction: Direction) -> bool {
        let unchanged = self.panels.len() == panels.len()
            && self
                .panels
                .iter()
                .zip(&panels)
                .all(|((old_key, old), (key, new))| {
                    old_key == key && old.sizing == new.sizing && old.min_size == new.min_size
                });
        if unchanged && self.direction == direction {
            return false;
        }

        let mut previous = std::mem::take(&mut self.panels);
        let mut index = 0;
        while index < previous.len() {
            if panels.iter().any(|(key, _)| *key == previous[index].0) {
                index += 1;
            } else {
                let (_, removed) = previous.remove(index);
                removed.redistribute(previous.iter_mut().map(|(_, panel)| panel), false);
            }
        }

        for (key, panel) in &panels {
            if !previous.iter().any(|(old_key, _)| old_key == key) {
                panel.redistribute(previous.iter_mut().map(|(_, panel)| panel), true);
                previous.push((key.clone(), *panel));
            }
        }

        for (key, panel) in &mut panels {
            if let Some((_, old)) = previous.iter().find(|(old_key, old)| {
                old_key == key
                    && std::mem::discriminant(&old.sizing) == std::mem::discriminant(&panel.sizing)
            }) {
                panel.size = old.size;
            }
            panel.size = panel.size.clamp(panel.min_size, panel.sizing.max_size());
        }
        self.panels = panels;
        self.direction = direction;
        self.dragging = None;
        true
    }

    /// Resizes panels around a divider at the given declaration index (1..panel count).
    pub fn apply_resize(
        &mut self,
        divider_index: usize,
        drag_delta: f32,
        container_size: f32,
    ) -> bool {
        if divider_index == 0 || divider_index >= self.panels.len() {
            return false;
        }

        let panels = &mut self.panels;
        let mut sizes_changed = false;

        let handle_space = panels.len().saturating_sub(1) as f32 * Self::HANDLE_SIZE;
        let (total_fixed_size, total_flex_weight) = panels.iter().fold(
            (0.0, 0.0),
            |(fixed_size, flex_weight): (f32, f32), (_, panel)| match panel.sizing {
                PanelSize::Pixels(_) => (fixed_size + panel.size, flex_weight),
                PanelSize::Percentage(_) => (fixed_size, flex_weight + panel.size),
            },
        );
        let flex_per_pixel =
            total_flex_weight / (container_size - total_fixed_size - handle_space).max(1.0);

        let shrink_amount = drag_delta.abs();
        let (growing_index, mut shrinking_range) = if drag_delta >= 0. {
            (divider_index - 1, divider_index..panels.len())
        } else {
            (divider_index, 0..divider_index)
        };

        let mut freed_space = 0.0;

        while let Some(index) = if drag_delta >= 0. {
            shrinking_range.next()
        } else {
            shrinking_range.next_back()
        } {
            let (_, panel) = &mut panels[index];
            let previous_size = panel.size;
            let units_per_pixel = panel.sizing.flex_scale(flex_per_pixel);
            let new_size = (panel.size - shrink_amount * units_per_pixel)
                .clamp(panel.min_size, panel.sizing.max_size());
            sizes_changed |= panel.size != new_size;
            panel.size = new_size;
            freed_space -= (new_size - previous_size) / units_per_pixel.max(f32::MIN_POSITIVE);

            if previous_size > panel.min_size {
                break;
            }
        }

        let (_, panel) = &mut panels[growing_index];
        let units_per_pixel = panel.sizing.flex_scale(flex_per_pixel);
        let new_size = (panel.size + freed_space * units_per_pixel)
            .clamp(panel.min_size, panel.sizing.max_size());
        sizes_changed |= panel.size != new_size;
        panel.size = new_size;

        sizes_changed
    }

    pub fn reset(&mut self) {
        for (_, panel) in &mut self.panels {
            panel.size = panel
                .initial_size
                .clamp(panel.min_size, panel.sizing.max_size());
        }
    }
}

/// A container with resizable panels.
///
/// # Example
///
/// ```rust
/// # use freya::prelude::*;
/// fn app() -> impl IntoElement {
///     ResizableContainer::new()
///         .panel(ResizablePanel::new(PanelSize::percent(50.)).child("Panel 1"))
///         .panel(ResizablePanel::new(PanelSize::percent(50.)).child("Panel 2"))
/// }
/// ```
///
/// See the [interactive components demo](https://freyaui.dev/demo).
#[derive(PartialEq, Clone)]
pub struct ResizableContainer {
    key: DiffKey,
    layout: LayoutData,
    panels: Vec<ResizablePanel>,
    controller: Option<Writable<ResizableContext>>,
}

impl Default for ResizableContainer {
    fn default() -> Self {
        Self::new()
    }
}

impl LayoutExt for ResizableContainer {
    fn layout_mut(&mut self) -> &mut LayoutData {
        &mut self.layout
    }
}

impl ContainerExt for ResizableContainer {}

impl KeyExt for ResizableContainer {
    fn key_mut(&mut self) -> &mut DiffKey {
        &mut self.key
    }
}

impl ResizableContainer {
    pub fn new() -> Self {
        Self {
            key: DiffKey::None,
            layout: Node {
                width: Size::fill(),
                height: Size::fill(),
                content: Content::flex(),
                ..Default::default()
            }
            .into(),
            panels: vec![],
            controller: None,
        }
    }

    pub fn direction(mut self, direction: Direction) -> Self {
        self.layout.direction = direction;
        self
    }

    pub fn panel(mut self, panel: impl Into<Option<ResizablePanel>>) -> Self {
        if let Some(panel) = panel.into() {
            self.panels.push(panel);
        }
        self
    }

    pub fn panels_iter(mut self, panels: impl Iterator<Item = ResizablePanel>) -> Self {
        self.panels.extend(panels);
        self
    }

    pub fn controller(mut self, controller: impl Into<Writable<ResizableContext>>) -> Self {
        self.controller = Some(controller.into());
        self
    }
}

impl Component for ResizableContainer {
    fn render(&self) -> impl IntoElement {
        let mut size = use_state(Area::default);
        use_provide_context(|| size);

        let mut controller = use_provide_context(|| {
            self.controller
                .clone()
                .unwrap_or_else(|| State::create(ResizableContext::default()).into_writable())
        });
        let declarations: Vec<_> = self
            .panels
            .iter()
            .enumerate()
            .map(|(index, panel)| {
                let initial_size = panel.initial_size.value();
                (
                    panel.key.clone().or(DiffKey::DefaultU64(index as u64)),
                    Panel {
                        size: initial_size,
                        initial_size,
                        min_size: panel.min_size.unwrap_or(initial_size * 0.25),
                        sizing: panel.initial_size,
                    },
                )
            })
            .collect();
        controller.write_if(|mut context| context.reconcile(declarations, self.layout.direction));
        let context = controller.read();

        rect()
            .layout(self.layout.clone())
            .on_sized(move |e: Event<SizedEventData>| size.set(e.area))
            .on_global_pointer_up({
                let mut controller = controller.clone();
                move |_: Event<PointerEventData>| {
                    controller.write_if(|mut context| context.dragging.take().is_some());
                }
            })
            .children(
                context
                    .panels
                    .iter()
                    .zip(&self.panels)
                    .enumerate()
                    .flat_map(|(index, ((key, state), panel))| {
                        let main_size = match state.sizing {
                            PanelSize::Pixels(_) => Size::px(state.size),
                            PanelSize::Percentage(_) => Size::flex(state.size),
                        };
                        let (width, height) = match context.direction {
                            Direction::Horizontal => (main_size, Size::fill()),
                            Direction::Vertical => (Size::fill(), main_size),
                        };

                        [
                            (index > 0)
                                .then(|| ResizableHandle::new(index).key(index).into_element()),
                            Some(
                                rect()
                                    .key(key)
                                    .a11y_role(AccessibilityRole::Pane)
                                    .width(width)
                                    .height(height)
                                    .overflow(panel.overflow)
                                    .children(panel.children.clone())
                                    .into_element(),
                            ),
                        ]
                        .into_iter()
                        .flatten()
                    }),
            )
    }

    fn render_key(&self) -> DiffKey {
        self.key.clone().or(self.default_key())
    }
}

/// A panel declaration. Its position in the container determines its order.
/// Use a key to preserve its size and child state when moving it.
#[derive(PartialEq, Clone)]
pub struct ResizablePanel {
    key: DiffKey,
    initial_size: PanelSize,
    min_size: Option<f32>,
    children: Vec<Element>,
    overflow: Overflow,
}

impl KeyExt for ResizablePanel {
    fn key_mut(&mut self) -> &mut DiffKey {
        &mut self.key
    }
}

impl ChildrenExt for ResizablePanel {
    fn children_mut(&mut self) -> &mut Vec<Element> {
        &mut self.children
    }
}

impl ResizablePanel {
    pub fn new(initial_size: PanelSize) -> Self {
        Self {
            key: DiffKey::None,
            initial_size,
            min_size: None,
            children: vec![],
            overflow: Overflow::Clip,
        }
    }

    /// Sets the initial size, also used by the controller's reset operation.
    /// Existing resized sizes are retained unless the sizing mode changes.
    pub fn initial_size(mut self, initial_size: PanelSize) -> Self {
        self.initial_size = initial_size;
        self
    }

    /// Set the minimum size for this panel (in the same units as the panel's sizing mode).
    pub fn min_size(mut self, min_size: f32) -> Self {
        self.min_size = Some(min_size);
        self
    }

    /// Sets how content is clipped inside the panel bounds.
    pub fn overflow(mut self, overflow: impl Into<Overflow>) -> Self {
        self.overflow = overflow.into();
        self
    }
}

/// Describes the current status of the Handle.
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum HandleStatus {
    /// Default state.
    #[default]
    Idle,
    /// Mouse is hovering the handle.
    Hovering,
}

#[derive(PartialEq)]
pub struct ResizableHandle {
    key: DiffKey,
    panel_index: usize,
    /// Theme override.
    pub(crate) theme: Option<ResizableHandleThemePartial>,
}

impl ResizableHandle {
    pub fn new(panel_index: usize) -> Self {
        Self {
            key: DiffKey::None,
            panel_index,
            theme: None,
        }
    }
}

impl KeyExt for ResizableHandle {
    fn key_mut(&mut self) -> &mut DiffKey {
        &mut self.key
    }
}

impl Component for ResizableHandle {
    fn render(&self) -> impl IntoElement {
        let ResizableHandleTheme {
            background,
            hover_background,
            corner_radius,
        } = get_theme!(
            &self.theme,
            ResizableHandleThemePreference,
            "resizable_handle"
        );
        let mut size = use_state(Area::default);
        let mut status = use_state(HandleStatus::default);
        let mut controller = use_consume::<Writable<ResizableContext>>();
        let container_size = use_consume::<State<Area>>();
        let mut allow_resizing = use_state(|| false);

        let panel_index = self.panel_index;
        let (direction, dragging) = {
            let context = controller.read();
            (context.direction, context.dragging == Some(panel_index))
        };

        let cursor = match direction {
            Direction::Horizontal => CursorIcon::ColResize,
            _ => CursorIcon::RowResize,
        };

        let on_capture_global_pointer_move = {
            let mut controller = controller.clone();
            move |e: Event<PointerEventData>| {
                if controller.peek().dragging == Some(panel_index) {
                    e.prevent_default();

                    if !*allow_resizing.read() {
                        return;
                    }

                    let coords = e.global_location();
                    let handle = size.read();
                    let container = container_size.read();
                    let mut context = controller.write();

                    let (drag_delta, container_axis_size) = match context.direction {
                        Direction::Horizontal => {
                            (coords.x as f32 - handle.min_x(), container.width())
                        }
                        Direction::Vertical => {
                            (coords.y as f32 - handle.min_y(), container.height())
                        }
                    };

                    let sizes_changed =
                        context.apply_resize(panel_index, drag_delta, container_axis_size);

                    if sizes_changed {
                        allow_resizing.set(false);
                    }
                }
            }
        };

        let handle_size = Size::px(ResizableContext::HANDLE_SIZE);
        let (width, height) = match direction {
            Direction::Horizontal => (handle_size, Size::fill()),
            Direction::Vertical => (Size::fill(), handle_size),
        };

        let background = match *status.read() {
            HandleStatus::Idle if !dragging => background,
            _ => hover_background,
        };

        rect()
            .width(width)
            .height(height)
            .background(background)
            .corner_radius(corner_radius)
            .cursor(cursor)
            .on_sized(move |e: Event<SizedEventData>| {
                size.set(e.area);
                allow_resizing.set(true);
            })
            .on_pointer_down(move |e: Event<PointerEventData>| {
                if !e.data().is_primary() {
                    return;
                }
                e.stop_propagation();
                e.prevent_default();
                controller.write().dragging = Some(panel_index);
            })
            .on_pointer_enter(move |_| status.set(HandleStatus::Hovering))
            .on_capture_global_pointer_move(on_capture_global_pointer_move)
            .on_pointer_leave(move |_| status.set(HandleStatus::Idle))
    }

    fn render_key(&self) -> DiffKey {
        self.key.clone().or(self.default_key())
    }
}