Skip to main content

iced_cube/layout/
resizable_panel.rs

1//! Two or more panels in a row or column, separated by handles that resize
2//! them.
3//!
4//! [`State`] holds the panels' constraints and their sizes, as fractions of
5//! the space the panels share, so an app can persist them with
6//! [`State::sizes`] and restore them with [`State::with_sizes`]. Its pure
7//! [`update`](State::update) applies drags, keyboard steps and collapsing,
8//! enforcing each panel's [min, max and collapse threshold](Panel).
9//!
10//! Dragging a handle moves the panels next to it; when one reaches its
11//! limit the change carries on to the panels beyond it. A
12//! [collapsible](Panel::collapsible) panel snaps shut when dragged below its
13//! threshold and snaps open again when dragged past it.
14//!
15//! # Keyboard
16//!
17//! iced 0.14 has no general focus model, but it does let widgets take part
18//! in its focus operations. Each handle is focusable: pressing it focuses
19//! it, as a text field is, and `iced::widget::operation::focus_next()` or
20//! `focus(id)` reach it too. While a handle has focus the group resolves
21//! its own [`Keymap`] of [`Action`]s and captures the keys it uses, so an
22//! app needs no subscription; see [`default_keymap`]. A press anywhere
23//! else, or Escape, takes the focus away.
24
25use iced::Event as Input;
26use iced::advanced::layout::{self, Layout};
27use iced::advanced::widget::operation::Focusable;
28use iced::advanced::widget::{Operation, Tree, Widget, tree};
29use iced::advanced::{Clipboard, Shell, overlay, renderer};
30use iced::keyboard::{self, key::Named};
31use iced::widget;
32use iced::{
33    Background, Border, Color, Element, Length, Point, Rectangle, Renderer, Shadow, Size, Theme,
34    Vector, mouse, touch,
35};
36
37use crate::keys::{self, Chord, Keymap};
38use crate::theme::{Tokens, radius};
39
40/// Space a handle takes up between two panels, in logical pixels.
41pub const HANDLE: f32 = 1.0;
42
43/// Width of the area around a handle that responds to the pointer.
44pub const HIT_AREA: f32 = 9.0;
45
46/// Width of the bar drawn over a highlighted handle.
47const HIGHLIGHT: f32 = 3.0;
48
49/// Size of the grip across and along the handle.
50const GRIP: Size = Size::new(12.0, 18.0);
51
52/// Sizes closer than this count as equal, in logical pixels.
53const EPSILON: f32 = 0.01;
54
55/// Whether panels sit side by side or one above the other.
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
57pub enum Axis {
58    /// A row of panels with upright handles between them.
59    #[default]
60    Horizontal,
61    /// A column of panels with level handles between them.
62    Vertical,
63}
64
65impl Axis {
66    pub const ALL: [Axis; 2] = [Axis::Horizontal, Axis::Vertical];
67
68    /// The coordinate of `point` along this axis.
69    pub fn along(self, point: Point) -> f32 {
70        match self {
71            Axis::Horizontal => point.x,
72            Axis::Vertical => point.y,
73        }
74    }
75
76    /// The pointer shape over a handle.
77    pub fn cursor(self) -> mouse::Interaction {
78        match self {
79            Axis::Horizontal => mouse::Interaction::ResizingColumn,
80            Axis::Vertical => mouse::Interaction::ResizingRow,
81        }
82    }
83
84    fn main(self, size: Size) -> f32 {
85        match self {
86            Axis::Horizontal => size.width,
87            Axis::Vertical => size.height,
88        }
89    }
90
91    fn cross(self, size: Size) -> f32 {
92        match self {
93            Axis::Horizontal => size.height,
94            Axis::Vertical => size.width,
95        }
96    }
97
98    fn size(self, main: f32, cross: f32) -> Size {
99        match self {
100            Axis::Horizontal => Size::new(main, cross),
101            Axis::Vertical => Size::new(cross, main),
102        }
103    }
104
105    fn point(self, main: f32) -> Point {
106        match self {
107            Axis::Horizontal => Point::new(main, 0.0),
108            Axis::Vertical => Point::new(0.0, main),
109        }
110    }
111}
112
113/// A length along the group: a share of the space the panels have, or a
114/// fixed number of logical pixels.
115#[derive(Debug, Clone, Copy, PartialEq)]
116pub enum Extent {
117    /// A share of the space, from 0.0 to 1.0.
118    Fraction(f32),
119    Pixels(f32),
120}
121
122impl Extent {
123    /// The length in pixels, out of `total` pixels.
124    pub fn resolve(self, total: f32) -> f32 {
125        let pixels = match self {
126            Extent::Fraction(share) => share * total,
127            Extent::Pixels(pixels) => pixels,
128        };
129        pixels.max(0.0)
130    }
131}
132
133/// The constraints of one panel.
134#[derive(Debug, Clone, Copy, PartialEq)]
135pub struct Panel {
136    /// The starting size. Fractions share out the space the pixel panels
137    /// leave.
138    pub default: Extent,
139    pub min: Extent,
140    pub max: Extent,
141    /// Whether the panel snaps shut below its threshold.
142    pub collapsible: bool,
143    /// The size of the panel while collapsed.
144    pub collapsed_size: Extent,
145    /// Below this size a collapsible panel snaps shut. Defaults to halfway
146    /// between the collapsed size and the minimum.
147    pub collapse_below: Option<Extent>,
148    /// Whether the panel starts collapsed.
149    pub collapsed: bool,
150}
151
152/// A panel starting at `default`, with no minimum or maximum.
153pub fn panel(default: Extent) -> Panel {
154    Panel {
155        default,
156        min: Extent::Pixels(0.0),
157        max: Extent::Fraction(1.0),
158        collapsible: false,
159        collapsed_size: Extent::Pixels(0.0),
160        collapse_below: None,
161        collapsed: false,
162    }
163}
164
165impl Panel {
166    pub fn min(mut self, min: Extent) -> Self {
167        self.min = min;
168        self
169    }
170
171    pub fn max(mut self, max: Extent) -> Self {
172        self.max = max;
173        self
174    }
175
176    pub fn collapsible(mut self, collapsible: bool) -> Self {
177        self.collapsible = collapsible;
178        self
179    }
180
181    /// The size while collapsed, such as the width of an icon rail.
182    pub fn collapsed_size(mut self, size: Extent) -> Self {
183        self.collapsed_size = size;
184        self
185    }
186
187    pub fn collapse_below(mut self, threshold: Extent) -> Self {
188        self.collapse_below = Some(threshold);
189        self
190    }
191
192    /// Starts the panel collapsed. Only applies to a collapsible panel.
193    pub fn collapsed(mut self, collapsed: bool) -> Self {
194        self.collapsed = collapsed;
195        self
196    }
197
198    /// The minimum, maximum and collapsed sizes in pixels. The maximum is
199    /// never below the minimum.
200    pub fn limits(&self, total: f32) -> Limits {
201        let min = self.min.resolve(total);
202        let collapsed = self.collapsed_size.resolve(total).min(min);
203        Limits {
204            min,
205            max: self.max.resolve(total).max(min),
206            collapsed,
207            threshold: self
208                .collapse_below
209                .map_or((collapsed + min) / 2.0, |threshold| {
210                    threshold.resolve(total).clamp(collapsed, min)
211                }),
212        }
213    }
214
215    /// The nearest size this panel can take to `size`: collapsed below the
216    /// threshold when collapsible, otherwise within its minimum and maximum.
217    pub fn settle(&self, size: f32, total: f32) -> f32 {
218        let limits = self.limits(total);
219        if self.collapsible && size < limits.threshold {
220            return limits.collapsed;
221        }
222        size.clamp(limits.min, limits.max)
223    }
224
225    /// Whether a panel of `size` pixels is collapsed.
226    pub fn is_collapsed_at(&self, size: f32, total: f32) -> bool {
227        let limits = self.limits(total);
228        self.collapsible && size <= limits.collapsed + EPSILON && limits.collapsed < limits.min
229    }
230}
231
232/// A panel's constraints resolved to pixels.
233#[derive(Debug, Clone, Copy, PartialEq)]
234pub struct Limits {
235    pub min: f32,
236    pub max: f32,
237    pub collapsed: f32,
238    pub threshold: f32,
239}
240
241/// The starting sizes of `panels` in pixels: pixel defaults as given, and
242/// fraction defaults sharing out what is left in proportion, then fitted to
243/// the panels' constraints.
244pub fn defaults(panels: &[Panel], total: f32) -> Vec<f32> {
245    let fixed: f32 = panels
246        .iter()
247        .filter_map(|panel| match panel.default {
248            Extent::Pixels(pixels) => Some(pixels.max(0.0)),
249            Extent::Fraction(_) => None,
250        })
251        .sum();
252    let shares: f32 = panels
253        .iter()
254        .filter_map(|panel| match panel.default {
255            Extent::Fraction(share) => Some(share.max(0.0)),
256            Extent::Pixels(_) => None,
257        })
258        .sum();
259    let left = (total - fixed).max(0.0);
260    let sizes = panels
261        .iter()
262        .map(|panel| match panel.default {
263            Extent::Pixels(pixels) => pixels.max(0.0),
264            Extent::Fraction(share) if shares > 0.0 => left * share.max(0.0) / shares,
265            Extent::Fraction(_) => 0.0,
266        })
267        .collect();
268    let collapsed: Vec<bool> = panels
269        .iter()
270        .map(|panel| panel.collapsible && panel.collapsed)
271        .collect();
272    fit(panels, &collapsed, sizes, total)
273}
274
275/// Fits `sizes` into `total` pixels within each panel's limits.
276///
277/// Collapsed panels take their collapsed size. The rest grow or shrink in
278/// proportion to their size until the sizes add up. When the minimums do
279/// not fit, collapsible panels collapse, last first, and as a last resort
280/// every panel scales down, so the sizes always add up to `total`.
281pub fn fit(panels: &[Panel], collapsed: &[bool], sizes: Vec<f32>, total: f32) -> Vec<f32> {
282    let mut shut: Vec<bool> = (0..panels.len())
283        .map(|index| collapsed.get(index).copied().unwrap_or(false))
284        .collect();
285    let mut fitted = constrain(panels, &shut, &sizes, total);
286    distribute(panels, &shut, &mut fitted, total);
287
288    while fitted.iter().sum::<f32>() > total + EPSILON {
289        let Some(index) = (0..panels.len())
290            .rev()
291            .find(|&index| panels[index].collapsible && !shut[index])
292        else {
293            break;
294        };
295        shut[index] = true;
296        fitted = constrain(panels, &shut, &sizes, total);
297        distribute(panels, &shut, &mut fitted, total);
298    }
299
300    let sum: f32 = fitted.iter().sum();
301    if sum > EPSILON && (sum - total).abs() > EPSILON {
302        for size in &mut fitted {
303            *size *= total / sum;
304        }
305    }
306    fitted
307}
308
309fn constrain(panels: &[Panel], shut: &[bool], sizes: &[f32], total: f32) -> Vec<f32> {
310    panels
311        .iter()
312        .enumerate()
313        .map(|(index, panel)| {
314            let limits = panel.limits(total);
315            if shut[index] && panel.collapsible {
316                return limits.collapsed;
317            }
318            sizes
319                .get(index)
320                .copied()
321                .unwrap_or(0.0)
322                .clamp(limits.min, limits.max)
323        })
324        .collect()
325}
326
327/// Spreads the difference between `total` and the sizes over the open
328/// panels that have room, in proportion to their size.
329fn distribute(panels: &[Panel], shut: &[bool], sizes: &mut [f32], total: f32) {
330    for _ in 0..=panels.len() {
331        let gap = total - sizes.iter().sum::<f32>();
332        if gap.abs() <= EPSILON {
333            return;
334        }
335        let room = |index: usize| {
336            let limits = panels[index].limits(total);
337            let open = !(shut[index] && panels[index].collapsible);
338            open && if gap > 0.0 {
339                sizes[index] < limits.max - EPSILON
340            } else {
341                sizes[index] > limits.min + EPSILON
342            }
343        };
344        let flexible: Vec<usize> = (0..panels.len()).filter(|&index| room(index)).collect();
345        if flexible.is_empty() {
346            return;
347        }
348        let weight: f32 = flexible.iter().map(|&index| sizes[index]).sum();
349        for &index in &flexible {
350            let share = if weight > EPSILON {
351                sizes[index] / weight
352            } else {
353                1.0 / flexible.len() as f32
354            };
355            let limits = panels[index].limits(total);
356            sizes[index] = (sizes[index] + gap * share).clamp(limits.min, limits.max);
357        }
358    }
359}
360
361/// The sizes after moving handle `handle` by `delta` pixels from `origin`.
362///
363/// The panel on the side the handle moves towards shrinks, and once it
364/// reaches its minimum the panels beyond it shrink in turn. The panel on
365/// the other side grows, passing any growth past its maximum on outwards.
366/// Collapsible panels snap shut and open at their threshold. When the move
367/// cannot be balanced, `origin` comes back unchanged.
368pub fn resize(panels: &[Panel], origin: &[f32], handle: usize, delta: f32, total: f32) -> Vec<f32> {
369    let count = panels.len();
370    if handle + 1 >= count || origin.len() != count || delta.abs() < EPSILON {
371        return origin.to_vec();
372    }
373    let before: Vec<usize> = (0..=handle).rev().collect();
374    let after: Vec<usize> = (handle + 1..count).collect();
375    let (grow, shrink) = if delta > 0.0 {
376        (before, after)
377    } else {
378        (after, before)
379    };
380
381    let mut request = delta.abs();
382    for _ in 0..3 {
383        let (shrunk, given) = shrink_by(panels, origin, &shrink, request, total);
384        if given < EPSILON {
385            return origin.to_vec();
386        }
387        let (grown, taken) = grow_by(panels, shrunk, &grow, given, total);
388        if (taken - given).abs() < EPSILON {
389            return grown;
390        }
391        // Snapping made one side move more than the other: try again with
392        // what the growing side accepts.
393        request = taken;
394    }
395    origin.to_vec()
396}
397
398fn shrink_by(
399    panels: &[Panel],
400    origin: &[f32],
401    order: &[usize],
402    request: f32,
403    total: f32,
404) -> (Vec<f32>, f32) {
405    let mut sizes = origin.to_vec();
406    let mut given = 0.0;
407    for &index in order {
408        let remaining = request - given;
409        if remaining < EPSILON {
410            break;
411        }
412        let current = sizes[index];
413        let settled = panels[index].settle(current - remaining, total);
414        if settled < current {
415            given += current - settled;
416            sizes[index] = settled;
417        }
418    }
419    (sizes, given)
420}
421
422fn grow_by(
423    panels: &[Panel],
424    mut sizes: Vec<f32>,
425    order: &[usize],
426    amount: f32,
427    total: f32,
428) -> (Vec<f32>, f32) {
429    let mut taken = 0.0;
430    for &index in order {
431        let remaining = amount - taken;
432        if remaining < EPSILON {
433            break;
434        }
435        let current = sizes[index];
436        let settled = panels[index].settle(current + remaining, total);
437        if settled > current {
438            taken += settled - current;
439            sizes[index] = settled;
440        } else if panels[index].is_collapsed_at(current, total) {
441            // A collapsed panel that stays shut holds the handle in place.
442            break;
443        }
444    }
445    (sizes, taken)
446}
447
448/// Changes to a panel group.
449#[derive(Debug, Clone, Copy, PartialEq)]
450pub enum Event {
451    /// The group measured the space its panels share, in pixels. The
452    /// widget sends it whenever that changes.
453    Measure(f32),
454    /// The pointer pressed `handle` at `position` along the axis.
455    DragStart {
456        handle: usize,
457        position: f32,
458    },
459    /// The pointer moved to `position` along the axis while dragging.
460    Drag(f32),
461    DragEnd,
462    /// Moves `handle` by `delta` pixels, towards the end when positive.
463    Resize {
464        handle: usize,
465        delta: f32,
466    },
467    Collapse(usize),
468    Expand(usize),
469    /// Collapses an open panel or expands a collapsed one.
470    Toggle(usize),
471    /// Puts every panel back to its default size.
472    Reset,
473}
474
475/// What changed after an event.
476#[derive(Debug, Clone, Copy, PartialEq, Eq)]
477pub enum Output {
478    /// The sizes changed. Read them with [`State::sizes`].
479    Resized,
480    /// This panel collapsed.
481    Collapsed(usize),
482    /// This panel expanded.
483    Expanded(usize),
484}
485
486/// A drag in progress: the handle, where the pointer started and the
487/// sizes at that moment.
488#[derive(Debug, Clone, PartialEq)]
489struct Drag {
490    handle: usize,
491    start: f32,
492    origin: Vec<f32>,
493}
494
495/// The panels, their sizes and any drag in progress.
496#[derive(Debug, Clone, PartialEq)]
497pub struct State {
498    panels: Vec<Panel>,
499    /// Fractions of the shared space, adding up to 1. Empty until the
500    /// group is first measured or sizes are restored.
501    sizes: Vec<f32>,
502    collapsed: Vec<bool>,
503    /// The size each collapsed panel had before it collapsed.
504    restore: Vec<Option<f32>>,
505    total: f32,
506    /// Sizes came from [`State::with_sizes`], so which panels are collapsed
507    /// is worked out on the first measurement.
508    restored: bool,
509    drag: Option<Drag>,
510    step: Extent,
511    large_step: Extent,
512}
513
514impl State {
515    /// A group of `panels` at their default sizes.
516    pub fn new(panels: impl IntoIterator<Item = Panel>) -> Self {
517        let panels: Vec<Panel> = panels.into_iter().collect();
518        let collapsed = panels
519            .iter()
520            .map(|panel| panel.collapsible && panel.collapsed)
521            .collect();
522        Self {
523            restore: vec![None; panels.len()],
524            panels,
525            sizes: Vec::new(),
526            collapsed,
527            total: 0.0,
528            restored: false,
529            drag: None,
530            step: Extent::Fraction(0.05),
531            large_step: Extent::Fraction(0.2),
532        }
533    }
534
535    /// Starts from saved sizes, as returned by [`State::sizes`]. Sizes of
536    /// the wrong length or adding up to nothing are ignored; the rest are
537    /// scaled to add up to 1.
538    pub fn with_sizes(mut self, sizes: impl IntoIterator<Item = f32>) -> Self {
539        self.set_sizes(sizes);
540        self
541    }
542
543    /// Replaces the sizes, as [`State::with_sizes`] does, fitting them to
544    /// the panels' limits once the group has been measured.
545    pub fn set_sizes(&mut self, sizes: impl IntoIterator<Item = f32>) {
546        let sizes: Vec<f32> = sizes.into_iter().map(|size| size.max(0.0)).collect();
547        let sum: f32 = sizes.iter().sum();
548        if sizes.len() != self.panels.len() || sum <= 0.0 {
549            return;
550        }
551        self.sizes = sizes.iter().map(|size| size / sum).collect();
552        self.collapsed = self
553            .panels
554            .iter()
555            .zip(&self.sizes)
556            .map(|(panel, size)| panel.collapsible && *size <= EPSILON / 100.0)
557            .collect();
558        self.restored = true;
559        if self.total > 0.0 {
560            self.settle_restored();
561        }
562    }
563
564    /// How far the arrow keys move a handle. Defaults to 5% of the space.
565    pub fn step(mut self, step: Extent) -> Self {
566        self.step = step;
567        self
568    }
569
570    /// How far Shift and an arrow key move a handle. Defaults to 20%.
571    pub fn large_step(mut self, step: Extent) -> Self {
572        self.large_step = step;
573        self
574    }
575
576    pub fn panels(&self) -> &[Panel] {
577        &self.panels
578    }
579
580    /// Each panel's share of the space, adding up to 1, for saving and
581    /// restoring with [`State::with_sizes`]. Before the group is first
582    /// measured this is the defaults as fractions, or empty when a default
583    /// is in pixels.
584    pub fn sizes(&self) -> Vec<f32> {
585        if !self.sizes.is_empty() {
586            return self.sizes.clone();
587        }
588        let fractions: Option<Vec<f32>> = self
589            .panels
590            .iter()
591            .map(|panel| match panel.default {
592                Extent::Fraction(share) => Some(share),
593                Extent::Pixels(_) => None,
594            })
595            .collect();
596        fractions.unwrap_or_default()
597    }
598
599    /// The pixels the panels share, as last measured. 0 before the first
600    /// measurement.
601    pub fn total(&self) -> f32 {
602        self.total
603    }
604
605    pub fn is_collapsed(&self, panel: usize) -> bool {
606        self.collapsed.get(panel).copied().unwrap_or(false)
607    }
608
609    /// The handle being dragged, if any.
610    pub fn dragging(&self) -> Option<usize> {
611        self.drag.as_ref().map(|drag| drag.handle)
612    }
613
614    /// Each panel's size in pixels, out of `total`.
615    pub fn resolve(&self, total: f32) -> Vec<f32> {
616        if self.sizes.is_empty() {
617            let mut sizes = defaults(&self.panels, total);
618            self.collapse_marked(&mut sizes, total);
619            return sizes;
620        }
621        let sizes = self.sizes.iter().map(|size| size * total).collect();
622        fit(&self.panels, &self.collapsed, sizes, total)
623    }
624
625    fn collapse_marked(&self, sizes: &mut [f32], total: f32) {
626        let marked = self.collapsed.iter().any(|collapsed| *collapsed);
627        if marked {
628            let fitted = fit(&self.panels, &self.collapsed, sizes.to_vec(), total);
629            sizes.copy_from_slice(&fitted);
630        }
631    }
632
633    /// The pixel sizes for the current measurement.
634    fn current(&self) -> Vec<f32> {
635        self.resolve(self.total)
636    }
637
638    /// Applies an event and says what changed.
639    pub fn update(&mut self, event: Event) -> Option<Output> {
640        if let Event::Measure(total) = event {
641            self.measure(total);
642            return None;
643        }
644        if self.total <= 0.0 {
645            return None;
646        }
647        match event {
648            Event::Measure(_) => None,
649            Event::DragStart { handle, position } => {
650                if handle + 1 >= self.panels.len() {
651                    return None;
652                }
653                self.drag = Some(Drag {
654                    handle,
655                    start: position,
656                    origin: self.current(),
657                });
658                None
659            }
660            Event::Drag(position) => {
661                let drag = self.drag.clone()?;
662                let sizes = resize(
663                    &self.panels,
664                    &drag.origin,
665                    drag.handle,
666                    position - drag.start,
667                    self.total,
668                );
669                self.apply(sizes, &drag.origin)
670            }
671            Event::DragEnd => {
672                self.drag = None;
673                None
674            }
675            Event::Resize { handle, delta } => {
676                let origin = self.current();
677                let sizes = resize(&self.panels, &origin, handle, delta, self.total);
678                self.apply(sizes, &origin)
679            }
680            Event::Collapse(panel) => self.collapse(panel),
681            Event::Expand(panel) => self.expand(panel),
682            Event::Toggle(panel) if self.is_collapsed(panel) => self.expand(panel),
683            Event::Toggle(panel) => self.collapse(panel),
684            Event::Reset => {
685                let origin = self.current();
686                self.collapsed = self
687                    .panels
688                    .iter()
689                    .map(|panel| panel.collapsible && panel.collapsed)
690                    .collect();
691                self.restore = vec![None; self.panels.len()];
692                self.sizes.clear();
693                let sizes = self.current();
694                self.store(&sizes);
695                (!same(&sizes, &origin)).then_some(Output::Resized)
696            }
697        }
698    }
699
700    fn measure(&mut self, total: f32) {
701        if total <= 0.0 || !total.is_finite() {
702            return;
703        }
704        if self.sizes.is_empty() {
705            let sizes = self.resolve(total);
706            self.total = total;
707            self.store(&sizes);
708            return;
709        }
710        self.total = total;
711        if self.restored {
712            self.settle_restored();
713        }
714    }
715
716    /// Fits restored sizes to the panels. A size below a collapsible
717    /// panel's threshold would snap shut, so that panel starts collapsed.
718    fn settle_restored(&mut self) {
719        self.restored = false;
720        let total = self.total;
721        self.collapsed = self
722            .panels
723            .iter()
724            .zip(&self.sizes)
725            .map(|(panel, size)| panel.collapsible && size * total < panel.limits(total).threshold)
726            .collect();
727        let sizes = self.current();
728        self.store(&sizes);
729    }
730
731    /// The handle to move to resize `panel`, and whether growing the panel
732    /// moves it towards the end.
733    fn handle_for(&self, panel: usize) -> Option<(usize, bool)> {
734        if panel + 1 < self.panels.len() {
735            return Some((panel, true));
736        }
737        (panel > 0 && panel < self.panels.len()).then(|| (panel - 1, false))
738    }
739
740    fn collapse(&mut self, panel: usize) -> Option<Output> {
741        let settings = self.panels.get(panel)?;
742        if !settings.collapsible || self.is_collapsed(panel) {
743            return None;
744        }
745        let (handle, forward) = self.handle_for(panel)?;
746        let origin = self.current();
747        let shrink = origin[panel] - settings.limits(self.total).collapsed;
748        let delta = if forward { -shrink } else { shrink };
749        let sizes = resize(&self.panels, &origin, handle, delta, self.total);
750        self.apply(sizes, &origin)
751    }
752
753    fn expand(&mut self, panel: usize) -> Option<Output> {
754        let settings = self.panels.get(panel)?;
755        if !self.is_collapsed(panel) {
756            return None;
757        }
758        let (handle, forward) = self.handle_for(panel)?;
759        let origin = self.current();
760        let limits = settings.limits(self.total);
761        let wanted = self
762            .restore
763            .get(panel)
764            .copied()
765            .flatten()
766            .map_or_else(
767                || settings.default.resolve(self.total),
768                |size| size * self.total,
769            )
770            .clamp(limits.min, limits.max);
771        let grow = wanted - origin[panel];
772        let delta = if forward { grow } else { -grow };
773        let sizes = resize(&self.panels, &origin, handle, delta, self.total);
774        self.apply(sizes, &origin)
775    }
776
777    /// Stores new pixel sizes and reports what changed against `origin`.
778    fn apply(&mut self, sizes: Vec<f32>, origin: &[f32]) -> Option<Output> {
779        if same(&sizes, &self.current()) {
780            return None;
781        }
782        let was = self.collapsed.clone();
783        self.store(&sizes);
784        let changed = (0..self.panels.len()).find(|&index| was[index] != self.collapsed[index]);
785        match changed {
786            Some(index) if self.collapsed[index] => {
787                if let (Some(slot), Some(size)) = (self.restore.get_mut(index), origin.get(index)) {
788                    *slot = Some(size / self.total);
789                }
790                Some(Output::Collapsed(index))
791            }
792            Some(index) => Some(Output::Expanded(index)),
793            None => Some(Output::Resized),
794        }
795    }
796
797    fn store(&mut self, sizes: &[f32]) {
798        let sum: f32 = sizes.iter().sum();
799        if sum <= 0.0 {
800            return;
801        }
802        self.sizes = sizes.iter().map(|size| size / sum).collect();
803        self.collapsed = self
804            .panels
805            .iter()
806            .zip(sizes)
807            .map(|(panel, size)| panel.is_collapsed_at(*size, self.total.max(sum)))
808            .collect();
809    }
810}
811
812fn same(a: &[f32], b: &[f32]) -> bool {
813    a.len() == b.len() && a.iter().zip(b).all(|(a, b)| (a - b).abs() < EPSILON)
814}
815
816/// What a handle keyboard shortcut does.
817#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
818pub enum Action {
819    /// Moves the handle towards the start by one step.
820    Decrease,
821    /// Moves the handle towards the end by one step.
822    Increase,
823    DecreaseMore,
824    IncreaseMore,
825    /// Shrinks the panel before the handle to its minimum.
826    Min,
827    /// Grows the panel before the handle to its maximum.
828    Max,
829    /// Collapses or expands the collapsible panel next to the handle.
830    Toggle,
831    /// Takes the focus away from the handle.
832    Release,
833}
834
835impl Action {
836    /// The [`Event`] this action sends for `handle`, or `None` when it has
837    /// nothing to do, as for [`Action::Release`], which the group handles
838    /// itself, or [`Action::Toggle`] beside panels that cannot collapse.
839    pub fn event(self, state: &State, handle: usize) -> Option<Event> {
840        if handle + 1 >= state.panels.len() {
841            return None;
842        }
843        let total = state.total;
844        let size = || state.current().get(handle).copied().unwrap_or(0.0);
845        let limits = || state.panels[handle].limits(total);
846        let delta = match self {
847            Action::Decrease => -state.step.resolve(total),
848            Action::Increase => state.step.resolve(total),
849            Action::DecreaseMore => -state.large_step.resolve(total),
850            Action::IncreaseMore => state.large_step.resolve(total),
851            Action::Min => limits().min - size(),
852            Action::Max => limits().max - size(),
853            Action::Toggle => {
854                return [handle, handle + 1]
855                    .into_iter()
856                    .find(|&panel| state.panels[panel].collapsible)
857                    .map(Event::Toggle);
858            }
859            Action::Release => return None,
860        };
861        Some(Event::Resize { handle, delta })
862    }
863}
864
865impl keys::Action for Action {
866    const ALL: &'static [Self] = &[
867        Action::Decrease,
868        Action::Increase,
869        Action::DecreaseMore,
870        Action::IncreaseMore,
871        Action::Min,
872        Action::Max,
873        Action::Toggle,
874        Action::Release,
875    ];
876
877    fn defaults() -> Keymap<Self> {
878        default_keymap()
879    }
880
881    fn name(self) -> &'static str {
882        match self {
883            Action::Decrease => "Decrease",
884            Action::Increase => "Increase",
885            Action::DecreaseMore => "DecreaseMore",
886            Action::IncreaseMore => "IncreaseMore",
887            Action::Min => "Min",
888            Action::Max => "Max",
889            Action::Toggle => "Toggle",
890            Action::Release => "Release",
891        }
892    }
893
894    fn description(self) -> &'static str {
895        match self {
896            Action::Decrease => "Moves the focused handle one step towards the start.",
897            Action::Increase => "Moves the focused handle one step towards the end.",
898            Action::DecreaseMore => "Moves the focused handle a large step towards the start.",
899            Action::IncreaseMore => "Moves the focused handle a large step towards the end.",
900            Action::Min => "Shrinks the panel before the handle to its minimum.",
901            Action::Max => "Grows the panel before the handle to its maximum.",
902            Action::Toggle => "Collapses or expands the collapsible panel next to the handle.",
903            Action::Release => "Takes the focus away from the handle.",
904        }
905    }
906}
907
908/// The default handle shortcuts, active while a handle has focus:
909///
910/// | Keys | Action |
911/// | --- | --- |
912/// | `ArrowLeft`, `ArrowUp` | [`Action::Decrease`] |
913/// | `ArrowRight`, `ArrowDown` | [`Action::Increase`] |
914/// | `Shift+ArrowLeft`, `Shift+ArrowUp` | [`Action::DecreaseMore`] |
915/// | `Shift+ArrowRight`, `Shift+ArrowDown` | [`Action::IncreaseMore`] |
916/// | `Home` | [`Action::Min`] |
917/// | `End` | [`Action::Max`] |
918/// | `Enter` | [`Action::Toggle`] |
919/// | `Escape` | [`Action::Release`] |
920///
921/// The group resolves these itself and captures them, so they never reach
922/// `keys::subscription` while a handle has focus.
923pub fn default_keymap() -> Keymap<Action> {
924    Keymap::new()
925        .bind(Chord::named(Named::ArrowLeft), Action::Decrease)
926        .bind(Chord::named(Named::ArrowUp), Action::Decrease)
927        .bind(Chord::named(Named::ArrowRight), Action::Increase)
928        .bind(Chord::named(Named::ArrowDown), Action::Increase)
929        .bind(Chord::named(Named::ArrowLeft).shift(), Action::DecreaseMore)
930        .bind(Chord::named(Named::ArrowUp).shift(), Action::DecreaseMore)
931        .bind(
932            Chord::named(Named::ArrowRight).shift(),
933            Action::IncreaseMore,
934        )
935        .bind(Chord::named(Named::ArrowDown).shift(), Action::IncreaseMore)
936        .bind(Chord::named(Named::Home), Action::Min)
937        .bind(Chord::named(Named::End), Action::Max)
938        .bind(Chord::named(Named::Enter), Action::Toggle)
939        .bind(Chord::named(Named::Escape), Action::Release)
940}
941
942/// How a handle is drawn.
943#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
944pub enum HandleStatus {
945    #[default]
946    Idle,
947    /// Under the pointer.
948    Hovered,
949    Dragged,
950    /// Has keyboard focus.
951    Focused,
952    /// The group has no message, so the handle does nothing.
953    Disabled,
954}
955
956impl HandleStatus {
957    pub const ALL: [HandleStatus; 5] = [
958        HandleStatus::Idle,
959        HandleStatus::Hovered,
960        HandleStatus::Dragged,
961        HandleStatus::Focused,
962        HandleStatus::Disabled,
963    ];
964}
965
966/// The colours of a handle.
967#[derive(Debug, Clone, Copy, PartialEq)]
968pub struct HandleStyle {
969    /// The one pixel line between the panels.
970    pub line: Color,
971    /// The wider bar drawn over the line while highlighted.
972    pub highlight: Option<Color>,
973    pub grip: Color,
974    pub grip_border: Color,
975    pub grip_dots: Color,
976}
977
978/// Resolves a handle's colours for a status.
979pub fn handle_style(tokens: &Tokens, status: HandleStatus) -> HandleStyle {
980    let highlight = match status {
981        HandleStatus::Idle | HandleStatus::Disabled => None,
982        HandleStatus::Hovered => Some(tokens.border),
983        HandleStatus::Dragged | HandleStatus::Focused => Some(tokens.ring),
984    };
985    HandleStyle {
986        line: tokens.border,
987        highlight,
988        grip: tokens.background,
989        grip_border: match status {
990            HandleStatus::Focused | HandleStatus::Dragged => tokens.ring,
991            _ => tokens.border,
992        },
993        grip_dots: tokens.muted_foreground,
994    }
995}
996
997/// A panel group builder. Convert it into an [`Element`] to render.
998pub struct ResizablePanel<'a, Message> {
999    state: &'a State,
1000    panes: Vec<Element<'a, Message>>,
1001    axis: Axis,
1002    stack_below: Option<f32>,
1003    grip: bool,
1004    width: Length,
1005    height: Length,
1006    id: Option<widget::Id>,
1007    keymap: Keymap<Action>,
1008    on_event: Option<Box<dyn Fn(Event) -> Message + 'a>>,
1009}
1010
1011impl<Message> std::fmt::Debug for ResizablePanel<'_, Message> {
1012    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1013        f.debug_struct("ResizablePanel")
1014            .field("state", self.state)
1015            .field("panes", &self.panes.len())
1016            .field("axis", &self.axis)
1017            .field("stack_below", &self.stack_below)
1018            .field("grip", &self.grip)
1019            .field("keymap", &self.keymap)
1020            .finish_non_exhaustive()
1021    }
1022}
1023
1024/// Lays out `panes`, one per panel of `state`, with handles between them.
1025/// Without [`on_event`](ResizablePanel::on_event) the handles do nothing.
1026pub fn resizable_panel<'a, Message>(
1027    state: &'a State,
1028    panes: impl IntoIterator<Item = Element<'a, Message>>,
1029) -> ResizablePanel<'a, Message> {
1030    ResizablePanel {
1031        state,
1032        panes: panes.into_iter().collect(),
1033        axis: Axis::default(),
1034        stack_below: None,
1035        grip: false,
1036        width: Length::Fill,
1037        height: Length::Fill,
1038        id: None,
1039        keymap: default_keymap(),
1040        on_event: None,
1041    }
1042}
1043
1044impl<'a, Message> ResizablePanel<'a, Message> {
1045    pub fn axis(mut self, axis: Axis) -> Self {
1046        self.axis = axis;
1047        self
1048    }
1049
1050    /// Stacks a horizontal group into a column while it is narrower than
1051    /// `width`, as on a phone.
1052    pub fn stack_below(mut self, width: f32) -> Self {
1053        self.stack_below = Some(width);
1054        self
1055    }
1056
1057    /// Draws a grip on each handle so it is easier to find.
1058    pub fn grip(mut self, grip: bool) -> Self {
1059        self.grip = grip;
1060        self
1061    }
1062
1063    /// Defaults to [`Length::Fill`].
1064    pub fn width(mut self, width: impl Into<Length>) -> Self {
1065        self.width = width.into();
1066        self
1067    }
1068
1069    /// Defaults to [`Length::Fill`].
1070    pub fn height(mut self, height: impl Into<Length>) -> Self {
1071        self.height = height.into();
1072        self
1073    }
1074
1075    /// The id of the first handle, for focusing it with
1076    /// `iced::widget::operation::focus`.
1077    pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
1078        self.id = Some(id.into());
1079        self
1080    }
1081
1082    /// Replaces the [`default_keymap`] a focused handle resolves keys with.
1083    pub fn keymap(mut self, keymap: Keymap<Action>) -> Self {
1084        self.keymap = keymap;
1085        self
1086    }
1087
1088    pub fn on_event(mut self, on_event: impl Fn(Event) -> Message + 'a) -> Self {
1089        self.on_event = Some(Box::new(on_event));
1090        self
1091    }
1092
1093    /// The axis panels are laid out along in `width` pixels.
1094    pub fn axis_at(&self, width: f32) -> Axis {
1095        match self.stack_below {
1096            Some(breakpoint) if self.axis == Axis::Horizontal && width < breakpoint => {
1097                Axis::Vertical
1098            }
1099            _ => self.axis,
1100        }
1101    }
1102}
1103
1104impl<'a, Message: 'a> From<ResizablePanel<'a, Message>> for Element<'a, Message> {
1105    fn from(group: ResizablePanel<'a, Message>) -> Self {
1106        Element::new(group)
1107    }
1108}
1109
1110/// Whether one handle has keyboard focus, and whether to show it. A press
1111/// focuses a handle without showing it; a key or a focus operation shows
1112/// it.
1113#[derive(Debug, Clone, Copy, Default)]
1114struct HandleFocus {
1115    focused: bool,
1116    visible: bool,
1117}
1118
1119impl Focusable for HandleFocus {
1120    fn is_focused(&self) -> bool {
1121        self.focused
1122    }
1123
1124    fn focus(&mut self) {
1125        self.focused = true;
1126        self.visible = true;
1127    }
1128
1129    fn unfocus(&mut self) {
1130        self.focused = false;
1131        self.visible = false;
1132    }
1133}
1134
1135/// What the widget remembers between events.
1136#[derive(Debug, Default)]
1137struct Memory {
1138    axis: Axis,
1139    total: f32,
1140    /// Where each panel starts along the axis, and its length, relative
1141    /// to the group.
1142    slots: Vec<(f32, f32)>,
1143    hovered: Option<usize>,
1144    handles: Vec<HandleFocus>,
1145}
1146
1147impl Memory {
1148    fn focused(&self) -> Option<usize> {
1149        self.handles.iter().position(|handle| handle.focused)
1150    }
1151
1152    /// The focused handle, if its focus should be drawn.
1153    fn visible(&self) -> Option<usize> {
1154        self.handles
1155            .iter()
1156            .position(|handle| handle.focused && handle.visible)
1157    }
1158
1159    /// Focuses one handle, or none, without showing the focus.
1160    fn focus(&mut self, index: Option<usize>) {
1161        for (i, handle) in self.handles.iter_mut().enumerate() {
1162            handle.focused = Some(i) == index;
1163            handle.visible = false;
1164        }
1165    }
1166
1167    fn show_focus(&mut self) {
1168        for handle in &mut self.handles {
1169            handle.visible = handle.focused;
1170        }
1171    }
1172
1173    /// The space panel `index` has, in window coordinates.
1174    fn slot(&self, bounds: Rectangle, index: usize) -> Option<Rectangle> {
1175        let (start, length) = *self.slots.get(index)?;
1176        Some(match self.axis {
1177            Axis::Horizontal => Rectangle {
1178                x: bounds.x + start,
1179                width: length,
1180                ..bounds
1181            },
1182            Axis::Vertical => Rectangle {
1183                y: bounds.y + start,
1184                height: length,
1185                ..bounds
1186            },
1187        })
1188    }
1189
1190    /// The line of each handle, after every panel but the last.
1191    /// Where each handle sits, as a percentage of the panels' total length.
1192    #[cfg(feature = "icedwright")]
1193    fn shares(&self) -> Vec<f32> {
1194        let total: f32 = self.slots.iter().map(|(_, length)| length).sum();
1195        if total <= 0.0 {
1196            return vec![0.0; self.slots.len().saturating_sub(1)];
1197        }
1198        let count = self.slots.len().saturating_sub(1);
1199        self.slots
1200            .iter()
1201            .take(count)
1202            .scan(0.0, |before, (_, length)| {
1203                *before += length;
1204                Some((*before / total * 100.0).round())
1205            })
1206            .collect()
1207    }
1208
1209    fn lines(&self, bounds: Rectangle) -> Vec<Rectangle> {
1210        let count = self.slots.len().saturating_sub(1);
1211        self.slots
1212            .iter()
1213            .take(count)
1214            .map(|(start, length)| match self.axis {
1215                Axis::Horizontal => Rectangle {
1216                    x: bounds.x + start + length,
1217                    width: HANDLE,
1218                    ..bounds
1219                },
1220                Axis::Vertical => Rectangle {
1221                    y: bounds.y + start + length,
1222                    height: HANDLE,
1223                    ..bounds
1224                },
1225            })
1226            .collect()
1227    }
1228
1229    fn handle_at(&self, bounds: Rectangle, point: Point) -> Option<usize> {
1230        self.lines(bounds)
1231            .into_iter()
1232            .position(|line| hit_area(line, self.axis).contains(point))
1233    }
1234}
1235
1236/// The area around a handle's line that responds to the pointer.
1237fn hit_area(line: Rectangle, axis: Axis) -> Rectangle {
1238    let grow = (HIT_AREA - HANDLE) / 2.0;
1239    match axis {
1240        Axis::Horizontal => Rectangle {
1241            x: line.x - grow,
1242            width: HIT_AREA,
1243            ..line
1244        },
1245        Axis::Vertical => Rectangle {
1246            y: line.y - grow,
1247            height: HIT_AREA,
1248            ..line
1249        },
1250    }
1251}
1252
1253fn pointer(event: &Input, cursor: mouse::Cursor) -> Option<Point> {
1254    match event {
1255        Input::Touch(
1256            touch::Event::FingerPressed { position, .. }
1257            | touch::Event::FingerMoved { position, .. },
1258        ) => Some(*position),
1259        _ => cursor.position(),
1260    }
1261}
1262
1263impl<Message> Widget<Message, Theme, Renderer> for ResizablePanel<'_, Message> {
1264    fn tag(&self) -> tree::Tag {
1265        tree::Tag::of::<Memory>()
1266    }
1267
1268    fn state(&self) -> tree::State {
1269        tree::State::new(Memory::default())
1270    }
1271
1272    fn size(&self) -> Size<Length> {
1273        Size::new(self.width, self.height)
1274    }
1275
1276    fn children(&self) -> Vec<Tree> {
1277        self.panes.iter().map(Tree::new).collect()
1278    }
1279
1280    fn diff(&self, tree: &mut Tree) {
1281        tree.diff_children(&self.panes);
1282    }
1283
1284    fn layout(
1285        &mut self,
1286        tree: &mut Tree,
1287        renderer: &Renderer,
1288        limits: &layout::Limits,
1289    ) -> layout::Node {
1290        let limits = limits.width(self.width).height(self.height);
1291        let max = limits.max();
1292        let axis = self.axis_at(max.width);
1293        let count = self.panes.len();
1294        let handles = count.saturating_sub(1) as f32 * HANDLE;
1295        let main = axis.main(max);
1296        let total = if main.is_finite() {
1297            (main - handles).max(0.0)
1298        } else {
1299            0.0
1300        };
1301        let cross = axis.cross(max);
1302        let sizes = self.state.resolve(total);
1303
1304        let mut start = 0.0_f32;
1305        let mut used = 0.0_f32;
1306        let mut widest = 0.0_f32;
1307        let mut nodes = Vec::with_capacity(count);
1308        let mut slots = Vec::with_capacity(count);
1309        for (index, (pane, tree)) in self.panes.iter_mut().zip(&mut tree.children).enumerate() {
1310            used += sizes.get(index).copied().unwrap_or(0.0);
1311            let end = used.round();
1312            let length = (end - start).max(0.0);
1313            let offset = start + index as f32 * HANDLE;
1314            let bounds = axis.size(length, cross);
1315            let node = pane
1316                .as_widget_mut()
1317                .layout(tree, renderer, &layout::Limits::new(Size::ZERO, bounds))
1318                .move_to(axis.point(offset));
1319            widest = widest.max(axis.cross(node.size()));
1320            nodes.push(node);
1321            slots.push((offset, length));
1322            start = end;
1323        }
1324
1325        let cross = if cross.is_finite() { cross } else { widest };
1326        let memory = tree.state.downcast_mut::<Memory>();
1327        memory.axis = axis;
1328        memory.total = total;
1329        memory.slots = slots;
1330        memory
1331            .handles
1332            .resize(count.saturating_sub(1), HandleFocus::default());
1333
1334        let size = limits.resolve(self.width, self.height, axis.size(total + handles, cross));
1335        layout::Node::with_children(size, nodes)
1336    }
1337
1338    fn operate(
1339        &mut self,
1340        tree: &mut Tree,
1341        layout: Layout<'_>,
1342        renderer: &Renderer,
1343        operation: &mut dyn Operation,
1344    ) {
1345        operation.container(None, layout.bounds());
1346        let Tree {
1347            state, children, ..
1348        } = tree;
1349        let memory = state.downcast_mut::<Memory>();
1350        let lines = memory.lines(layout.bounds());
1351        let id = self.id.as_ref();
1352        #[cfg(feature = "icedwright")]
1353        let shares = memory.shares();
1354        operation.traverse(&mut |operation| {
1355            for (index, (handle, line)) in memory.handles.iter_mut().zip(&lines).enumerate() {
1356                let id = id.filter(|_| index == 0);
1357                // Reported just before the focusable so the two merge.
1358                #[cfg(feature = "icedwright")]
1359                {
1360                    let share = shares.get(index).copied().unwrap_or_default();
1361                    icedwright_probe::report(operation, *line, || {
1362                        icedwright_probe::Annotation::new()
1363                            .role(icedwright_probe::role::Splitter)
1364                            .range(f64::from(share), 0.0..=100.0)
1365                            .component("iced_cube::resizable_panel")
1366                    });
1367                }
1368                operation.focusable(id, *line, handle);
1369            }
1370            for ((pane, tree), layout) in self
1371                .panes
1372                .iter_mut()
1373                .zip(children.iter_mut())
1374                .zip(layout.children())
1375            {
1376                pane.as_widget_mut()
1377                    .operate(tree, layout, renderer, operation);
1378            }
1379        });
1380    }
1381
1382    fn update(
1383        &mut self,
1384        tree: &mut Tree,
1385        event: &Input,
1386        layout: Layout<'_>,
1387        cursor: mouse::Cursor,
1388        renderer: &Renderer,
1389        clipboard: &mut dyn Clipboard,
1390        shell: &mut Shell<'_, Message>,
1391        viewport: &Rectangle,
1392    ) {
1393        if let Some(on_event) = &self.on_event {
1394            let memory = tree.state.downcast_mut::<Memory>();
1395            if handle_event(
1396                self.state,
1397                &self.keymap,
1398                on_event,
1399                memory,
1400                event,
1401                layout,
1402                cursor,
1403                shell,
1404            ) {
1405                return;
1406            }
1407        }
1408
1409        for ((pane, tree), layout) in self
1410            .panes
1411            .iter_mut()
1412            .zip(&mut tree.children)
1413            .zip(layout.children())
1414        {
1415            pane.as_widget_mut().update(
1416                tree, event, layout, cursor, renderer, clipboard, shell, viewport,
1417            );
1418        }
1419    }
1420
1421    fn mouse_interaction(
1422        &self,
1423        tree: &Tree,
1424        layout: Layout<'_>,
1425        cursor: mouse::Cursor,
1426        viewport: &Rectangle,
1427        renderer: &Renderer,
1428    ) -> mouse::Interaction {
1429        let memory = tree.state.downcast_ref::<Memory>();
1430        if self.on_event.is_some() {
1431            let over = cursor
1432                .position()
1433                .and_then(|point| memory.handle_at(layout.bounds(), point));
1434            if self.state.dragging().is_some() || over.is_some() {
1435                return memory.axis.cursor();
1436            }
1437        }
1438        self.panes
1439            .iter()
1440            .zip(&tree.children)
1441            .zip(layout.children())
1442            .map(|((pane, tree), layout)| {
1443                pane.as_widget()
1444                    .mouse_interaction(tree, layout, cursor, viewport, renderer)
1445            })
1446            .max()
1447            .unwrap_or_default()
1448    }
1449
1450    fn draw(
1451        &self,
1452        tree: &Tree,
1453        renderer: &mut Renderer,
1454        theme: &Theme,
1455        style: &renderer::Style,
1456        layout: Layout<'_>,
1457        cursor: mouse::Cursor,
1458        viewport: &Rectangle,
1459    ) {
1460        use iced::advanced::Renderer as _;
1461
1462        let memory = tree.state.downcast_ref::<Memory>();
1463        let bounds = layout.bounds();
1464        for (index, ((pane, tree), layout)) in self
1465            .panes
1466            .iter()
1467            .zip(&tree.children)
1468            .zip(layout.children())
1469            .enumerate()
1470        {
1471            let Some(slot) = memory.slot(bounds, index) else {
1472                continue;
1473            };
1474            if slot.width < 1.0 || slot.height < 1.0 {
1475                continue;
1476            }
1477            let Some(clip) = slot.intersection(viewport) else {
1478                continue;
1479            };
1480            renderer.with_layer(clip, |renderer| {
1481                pane.as_widget()
1482                    .draw(tree, renderer, theme, style, layout, cursor, &clip);
1483            });
1484        }
1485
1486        let tokens = Tokens::of(theme);
1487        let focused = memory.visible();
1488        let over = cursor
1489            .position()
1490            .and_then(|point| memory.handle_at(bounds, point));
1491        for (index, line) in memory.lines(bounds).into_iter().enumerate() {
1492            let status = if self.on_event.is_none() {
1493                HandleStatus::Disabled
1494            } else if self.state.dragging() == Some(index) {
1495                HandleStatus::Dragged
1496            } else if focused == Some(index) {
1497                HandleStatus::Focused
1498            } else if over == Some(index) {
1499                HandleStatus::Hovered
1500            } else {
1501                HandleStatus::Idle
1502            };
1503            draw_handle(
1504                renderer,
1505                line,
1506                memory.axis,
1507                handle_style(&tokens, status),
1508                self.grip,
1509            );
1510        }
1511    }
1512
1513    fn overlay<'b>(
1514        &'b mut self,
1515        tree: &'b mut Tree,
1516        layout: Layout<'b>,
1517        renderer: &Renderer,
1518        viewport: &Rectangle,
1519        translation: Vector,
1520    ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
1521        overlay::from_children(
1522            &mut self.panes,
1523            tree,
1524            layout,
1525            renderer,
1526            viewport,
1527            translation,
1528        )
1529    }
1530}
1531
1532/// Handles pointer and key events on the handles. Returns whether the
1533/// event was used, so the panes never see it.
1534#[allow(clippy::too_many_arguments)]
1535fn handle_event<Message>(
1536    state: &State,
1537    keymap: &Keymap<Action>,
1538    on_event: &dyn Fn(Event) -> Message,
1539    memory: &mut Memory,
1540    event: &Input,
1541    layout: Layout<'_>,
1542    cursor: mouse::Cursor,
1543    shell: &mut Shell<'_, Message>,
1544) -> bool {
1545    if memory.total > 0.0 && (state.total() - memory.total).abs() > 0.5 {
1546        shell.publish(on_event(Event::Measure(memory.total)));
1547    }
1548    let axis = memory.axis;
1549
1550    match event {
1551        Input::Mouse(mouse::Event::CursorMoved { .. })
1552        | Input::Touch(touch::Event::FingerMoved { .. }) => {
1553            let Some(point) = pointer(event, cursor) else {
1554                return false;
1555            };
1556            if state.dragging().is_some() {
1557                shell.publish(on_event(Event::Drag(axis.along(point))));
1558                shell.capture_event();
1559                return true;
1560            }
1561            let hovered = memory.handle_at(layout.bounds(), point);
1562            if hovered != memory.hovered {
1563                memory.hovered = hovered;
1564                shell.request_redraw();
1565            }
1566            false
1567        }
1568        Input::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
1569        | Input::Touch(touch::Event::FingerPressed { .. }) => {
1570            let hit = pointer(event, cursor).and_then(|point| {
1571                memory
1572                    .handle_at(layout.bounds(), point)
1573                    .map(|handle| (handle, point))
1574            });
1575            let Some((handle, point)) = hit else {
1576                if memory.focused().is_some() {
1577                    memory.focus(None);
1578                    shell.request_redraw();
1579                }
1580                return false;
1581            };
1582            memory.focus(Some(handle));
1583            shell.publish(on_event(Event::DragStart {
1584                handle,
1585                position: axis.along(point),
1586            }));
1587            shell.capture_event();
1588            true
1589        }
1590        Input::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
1591        | Input::Touch(touch::Event::FingerLifted { .. } | touch::Event::FingerLost { .. }) => {
1592            if state.dragging().is_none() {
1593                return false;
1594            }
1595            shell.publish(on_event(Event::DragEnd));
1596            shell.capture_event();
1597            true
1598        }
1599        Input::Keyboard(keyboard::Event::KeyPressed { key, modifiers, .. }) => {
1600            let Some(handle) = memory.focused() else {
1601                return false;
1602            };
1603            let Some(action) = keymap.resolve(key, *modifiers) else {
1604                return false;
1605            };
1606            if action == Action::Release {
1607                memory.focus(None);
1608            } else {
1609                memory.show_focus();
1610                if let Some(event) = action.event(state, handle) {
1611                    shell.publish(on_event(event));
1612                }
1613            }
1614            shell.request_redraw();
1615            shell.capture_event();
1616            true
1617        }
1618        _ => false,
1619    }
1620}
1621
1622fn draw_handle(
1623    renderer: &mut Renderer,
1624    line: Rectangle,
1625    axis: Axis,
1626    style: HandleStyle,
1627    grip: bool,
1628) {
1629    use iced::advanced::Renderer as _;
1630
1631    let quad = |bounds: Rectangle, radius: f32, border: Option<Color>| renderer::Quad {
1632        bounds,
1633        border: Border {
1634            color: border.unwrap_or(Color::TRANSPARENT),
1635            width: if border.is_some() { 1.0 } else { 0.0 },
1636            radius: radius.into(),
1637        },
1638        shadow: Shadow::default(),
1639        snap: true,
1640    };
1641
1642    renderer.fill_quad(quad(line, 0.0, None), Background::Color(style.line));
1643
1644    if let Some(colour) = style.highlight {
1645        let grow = (HIGHLIGHT - HANDLE) / 2.0;
1646        let bar = match axis {
1647            Axis::Horizontal => Rectangle {
1648                x: line.x - grow,
1649                width: HIGHLIGHT,
1650                ..line
1651            },
1652            Axis::Vertical => Rectangle {
1653                y: line.y - grow,
1654                height: HIGHLIGHT,
1655                ..line
1656            },
1657        };
1658        renderer.fill_quad(quad(bar, 0.0, None), Background::Color(colour));
1659    }
1660
1661    if !grip {
1662        return;
1663    }
1664    let size = match axis {
1665        Axis::Horizontal => GRIP,
1666        Axis::Vertical => Size::new(GRIP.height, GRIP.width),
1667    };
1668    let centre = line.center();
1669    let bounds = Rectangle::new(
1670        Point::new(
1671            (centre.x - size.width / 2.0).round(),
1672            (centre.y - size.height / 2.0).round(),
1673        ),
1674        size,
1675    );
1676    renderer.fill_quad(
1677        quad(bounds, radius::SM, Some(style.grip_border)),
1678        Background::Color(style.grip),
1679    );
1680
1681    // Two rows of three dots, turned to follow the handle.
1682    let dot = 2.0;
1683    for column in [-1.0_f32, 1.0] {
1684        for row in [-1.0_f32, 0.0, 1.0] {
1685            let (dx, dy) = match axis {
1686                Axis::Horizontal => (column * 2.0, row * 4.0),
1687                Axis::Vertical => (row * 4.0, column * 2.0),
1688            };
1689            let position = Point::new(
1690                (centre.x + dx - dot / 2.0).round(),
1691                (centre.y + dy - dot / 2.0).round(),
1692            );
1693            renderer.fill_quad(
1694                quad(Rectangle::new(position, Size::new(dot, dot)), 1.0, None),
1695                Background::Color(style.grip_dots),
1696            );
1697        }
1698    }
1699}
1700
1701#[cfg(test)]
1702mod tests {
1703    use super::*;
1704    use crate::theme::{dark, light};
1705
1706    const TOTAL: f32 = 1000.0;
1707
1708    fn close(a: f32, b: f32) -> bool {
1709        (a - b).abs() < 0.05
1710    }
1711
1712    fn assert_sizes(actual: &[f32], expected: &[f32]) {
1713        assert_eq!(actual.len(), expected.len(), "{actual:?}");
1714        for (a, b) in actual.iter().zip(expected) {
1715            assert!(close(*a, *b), "{actual:?} != {expected:?}");
1716        }
1717    }
1718
1719    fn measured(panels: impl IntoIterator<Item = Panel>) -> State {
1720        let mut state = State::new(panels);
1721        let _ = state.update(Event::Measure(TOTAL));
1722        state
1723    }
1724
1725    fn pixels(state: &State) -> Vec<f32> {
1726        state.resolve(state.total())
1727    }
1728
1729    fn sidebar_and_content() -> [Panel; 2] {
1730        [
1731            panel(Extent::Fraction(0.3))
1732                .min(Extent::Pixels(200.0))
1733                .max(Extent::Fraction(0.5))
1734                .collapsible(true),
1735            panel(Extent::Fraction(0.7)).min(Extent::Pixels(300.0)),
1736        ]
1737    }
1738
1739    #[test]
1740    fn panel_defaults() {
1741        let panel = panel(Extent::Fraction(0.5));
1742        assert_eq!(panel.min, Extent::Pixels(0.0));
1743        assert_eq!(panel.max, Extent::Fraction(1.0));
1744        assert!(!panel.collapsible);
1745        assert_eq!(panel.collapsed_size, Extent::Pixels(0.0));
1746        assert_eq!(panel.collapse_below, None);
1747    }
1748
1749    #[test]
1750    fn extents_resolve_to_pixels() {
1751        assert_eq!(Extent::Fraction(0.25).resolve(800.0), 200.0);
1752        assert_eq!(Extent::Pixels(120.0).resolve(800.0), 120.0);
1753        assert_eq!(Extent::Pixels(-5.0).resolve(800.0), 0.0);
1754    }
1755
1756    #[test]
1757    fn limits_keep_max_above_min_and_threshold_between() {
1758        let limits = panel(Extent::Fraction(0.5))
1759            .min(Extent::Pixels(300.0))
1760            .max(Extent::Pixels(100.0))
1761            .collapsible(true)
1762            .collapsed_size(Extent::Pixels(40.0))
1763            .limits(TOTAL);
1764        assert_eq!(limits.min, 300.0);
1765        assert_eq!(limits.max, 300.0);
1766        assert_eq!(limits.collapsed, 40.0);
1767        assert_eq!(limits.threshold, 170.0);
1768
1769        let custom = panel(Extent::Fraction(0.5))
1770            .min(Extent::Pixels(200.0))
1771            .collapsible(true)
1772            .collapse_below(Extent::Pixels(500.0))
1773            .limits(TOTAL);
1774        assert_eq!(custom.threshold, 200.0, "never above the minimum");
1775    }
1776
1777    #[test]
1778    fn settle_clamps_and_snaps_at_the_threshold() {
1779        let plain = panel(Extent::Fraction(0.5))
1780            .min(Extent::Pixels(100.0))
1781            .max(Extent::Pixels(400.0));
1782        assert_eq!(plain.settle(50.0, TOTAL), 100.0);
1783        assert_eq!(plain.settle(250.0, TOTAL), 250.0);
1784        assert_eq!(plain.settle(900.0, TOTAL), 400.0);
1785
1786        let collapsible = plain.collapsible(true);
1787        assert_eq!(
1788            collapsible.settle(60.0, TOTAL),
1789            100.0,
1790            "above the threshold"
1791        );
1792        assert_eq!(collapsible.settle(49.0, TOTAL), 0.0, "below the threshold");
1793        assert!(collapsible.is_collapsed_at(0.0, TOTAL));
1794        assert!(!plain.is_collapsed_at(0.0, TOTAL));
1795    }
1796
1797    #[test]
1798    fn defaults_share_what_pixel_panels_leave() {
1799        let panels = [
1800            panel(Extent::Pixels(250.0)),
1801            panel(Extent::Fraction(1.0)),
1802            panel(Extent::Fraction(1.0)),
1803        ];
1804        assert_sizes(&defaults(&panels, TOTAL), &[250.0, 375.0, 375.0]);
1805    }
1806
1807    #[test]
1808    fn defaults_respect_limits_and_initial_collapse() {
1809        let panels = [
1810            panel(Extent::Fraction(0.1)).min(Extent::Pixels(200.0)),
1811            panel(Extent::Fraction(0.9)),
1812        ];
1813        assert_sizes(&defaults(&panels, TOTAL), &[200.0, 800.0]);
1814
1815        let collapsed = [
1816            panel(Extent::Fraction(0.3))
1817                .collapsible(true)
1818                .collapsed_size(Extent::Pixels(48.0))
1819                .min(Extent::Pixels(200.0))
1820                .collapsed(true),
1821            panel(Extent::Fraction(0.7)),
1822        ];
1823        assert_sizes(&defaults(&collapsed, TOTAL), &[48.0, 952.0]);
1824    }
1825
1826    #[test]
1827    fn fit_always_adds_up() {
1828        let panels = sidebar_and_content();
1829        for total in [1200.0, 600.0, 450.0, 320.0, 100.0] {
1830            let sizes = fit(
1831                &panels,
1832                &[false, false],
1833                vec![0.3 * total, 0.7 * total],
1834                total,
1835            );
1836            let sum: f32 = sizes.iter().sum();
1837            assert!(close(sum, total), "{total}: {sizes:?}");
1838        }
1839    }
1840
1841    #[test]
1842    fn fit_collapses_side_panels_when_minimums_do_not_fit() {
1843        let panels = sidebar_and_content();
1844        // 200 + 300 minimums do not fit in 360, so the sidebar collapses.
1845        let sizes = fit(&panels, &[false, false], vec![108.0, 252.0], 360.0);
1846        assert_sizes(&sizes, &[0.0, 360.0]);
1847    }
1848
1849    #[test]
1850    fn fit_scales_as_a_last_resort() {
1851        let panels = [
1852            panel(Extent::Fraction(0.5)).min(Extent::Pixels(300.0)),
1853            panel(Extent::Fraction(0.5)).min(Extent::Pixels(300.0)),
1854        ];
1855        let sizes = fit(&panels, &[false, false], vec![200.0, 200.0], 400.0);
1856        assert_sizes(&sizes, &[200.0, 200.0]);
1857    }
1858
1859    #[test]
1860    fn state_starts_from_defaults_once_measured() {
1861        let state = State::new(sidebar_and_content());
1862        assert_eq!(state.total(), 0.0);
1863        assert_sizes(&state.sizes(), &[0.3, 0.7]);
1864        assert_sizes(&state.resolve(TOTAL), &[300.0, 700.0]);
1865
1866        let state = measured(sidebar_and_content());
1867        assert_eq!(state.total(), TOTAL);
1868        assert_sizes(&state.sizes(), &[0.3, 0.7]);
1869    }
1870
1871    #[test]
1872    fn unmeasured_state_ignores_changes() {
1873        let mut state = State::new(sidebar_and_content());
1874        assert_eq!(
1875            state.update(Event::Resize {
1876                handle: 0,
1877                delta: 50.0
1878            }),
1879            None
1880        );
1881        assert_eq!(state.update(Event::Collapse(0)), None);
1882        let _ = state.update(Event::Measure(-1.0));
1883        let _ = state.update(Event::Measure(f32::INFINITY));
1884        assert_eq!(state.total(), 0.0);
1885    }
1886
1887    #[test]
1888    fn saved_sizes_round_trip_and_bad_ones_are_ignored() {
1889        let state = State::new(sidebar_and_content()).with_sizes([2.0, 6.0]);
1890        assert_sizes(&state.sizes(), &[0.25, 0.75]);
1891
1892        let wrong_length = State::new(sidebar_and_content()).with_sizes([1.0]);
1893        assert_sizes(&wrong_length.sizes(), &[0.3, 0.7]);
1894        let empty = State::new(sidebar_and_content()).with_sizes([0.0, 0.0]);
1895        assert_sizes(&empty.sizes(), &[0.3, 0.7]);
1896
1897        let collapsed = State::new(sidebar_and_content()).with_sizes([0.0, 1.0]);
1898        assert!(collapsed.is_collapsed(0));
1899    }
1900
1901    #[test]
1902    fn dragging_moves_the_handle_from_where_it_started() {
1903        let mut state = measured(sidebar_and_content());
1904        let _ = state.update(Event::DragStart {
1905            handle: 0,
1906            position: 300.0,
1907        });
1908        assert_eq!(state.dragging(), Some(0));
1909        assert_eq!(state.update(Event::Drag(340.0)), Some(Output::Resized));
1910        assert_sizes(&pixels(&state), &[340.0, 660.0]);
1911        assert_eq!(state.update(Event::Drag(320.0)), Some(Output::Resized));
1912        assert_sizes(&pixels(&state), &[320.0, 680.0]);
1913        let _ = state.update(Event::DragEnd);
1914        assert_eq!(state.dragging(), None);
1915        assert_eq!(state.update(Event::Drag(500.0)), None);
1916    }
1917
1918    #[test]
1919    fn drags_clamp_to_min_and_max() {
1920        let mut state = measured(sidebar_and_content());
1921        let _ = state.update(Event::DragStart {
1922            handle: 0,
1923            position: 300.0,
1924        });
1925        let _ = state.update(Event::Drag(900.0));
1926        assert_sizes(&pixels(&state), &[500.0, 500.0]);
1927        let _ = state.update(Event::Drag(160.0));
1928        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1929    }
1930
1931    #[test]
1932    fn collapsible_panels_snap_shut_below_the_threshold_and_open_past_it() {
1933        let mut state = measured(sidebar_and_content());
1934        let _ = state.update(Event::DragStart {
1935            handle: 0,
1936            position: 300.0,
1937        });
1938        // Threshold is 100: halfway between collapsed (0) and min (200).
1939        let _ = state.update(Event::Drag(110.0));
1940        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1941        assert_eq!(state.update(Event::Drag(90.0)), Some(Output::Collapsed(0)));
1942        assert!(state.is_collapsed(0));
1943        assert_sizes(&pixels(&state), &[0.0, 1000.0]);
1944        assert_eq!(state.update(Event::Drag(120.0)), Some(Output::Expanded(0)));
1945        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1946        let _ = state.update(Event::DragEnd);
1947
1948        let _ = state.update(Event::Collapse(0));
1949        let _ = state.update(Event::DragStart {
1950            handle: 0,
1951            position: 0.0,
1952        });
1953        assert_eq!(
1954            state.update(Event::Drag(60.0)),
1955            None,
1956            "still below the threshold"
1957        );
1958        assert!(state.is_collapsed(0));
1959        assert_eq!(state.update(Event::Drag(101.0)), Some(Output::Expanded(0)));
1960        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1961    }
1962
1963    #[test]
1964    fn shrinking_carries_on_past_a_panel_at_its_minimum() {
1965        let mut state = measured([
1966            panel(Extent::Fraction(0.4)),
1967            panel(Extent::Fraction(0.3)).min(Extent::Pixels(200.0)),
1968            panel(Extent::Fraction(0.3)).min(Extent::Pixels(100.0)),
1969        ]);
1970        let _ = state.update(Event::Resize {
1971            handle: 0,
1972            delta: 250.0,
1973        });
1974        assert_sizes(&pixels(&state), &[650.0, 200.0, 150.0]);
1975        let _ = state.update(Event::Resize {
1976            handle: 0,
1977            delta: 500.0,
1978        });
1979        assert_sizes(&pixels(&state), &[700.0, 200.0, 100.0]);
1980    }
1981
1982    #[test]
1983    fn growth_past_a_maximum_passes_outwards() {
1984        let origin = [200.0, 300.0, 500.0];
1985        let panels = [
1986            panel(Extent::Fraction(0.2)),
1987            panel(Extent::Fraction(0.3)).max(Extent::Pixels(350.0)),
1988            panel(Extent::Fraction(0.5)),
1989        ];
1990        let sizes = resize(&panels, &origin, 1, 100.0, TOTAL);
1991        assert_sizes(&sizes, &[250.0, 350.0, 400.0]);
1992    }
1993
1994    #[test]
1995    fn resize_ignores_bad_handles_and_zero_moves() {
1996        let panels = sidebar_and_content();
1997        let origin = [300.0, 700.0];
1998        assert_eq!(resize(&panels, &origin, 1, 50.0, TOTAL), origin);
1999        assert_eq!(resize(&panels, &origin, 0, 0.0, TOTAL), origin);
2000        assert_eq!(resize(&panels, &[300.0], 0, 50.0, TOTAL), [300.0]);
2001    }
2002
2003    #[test]
2004    fn toggle_collapses_and_restores_the_previous_size() {
2005        let mut state = measured(sidebar_and_content());
2006        let _ = state.update(Event::Resize {
2007            handle: 0,
2008            delta: 100.0,
2009        });
2010        assert_eq!(state.update(Event::Toggle(0)), Some(Output::Collapsed(0)));
2011        assert_sizes(&pixels(&state), &[0.0, 1000.0]);
2012        assert_eq!(state.update(Event::Collapse(0)), None);
2013        assert_eq!(state.update(Event::Toggle(0)), Some(Output::Expanded(0)));
2014        assert_sizes(&pixels(&state), &[400.0, 600.0]);
2015        assert_eq!(state.update(Event::Expand(0)), None);
2016    }
2017
2018    #[test]
2019    fn the_last_panel_collapses_through_the_handle_before_it() {
2020        let mut state = measured([
2021            panel(Extent::Fraction(0.6)),
2022            panel(Extent::Fraction(0.4))
2023                .collapsible(true)
2024                .collapsed_size(Extent::Pixels(40.0))
2025                .min(Extent::Pixels(200.0)),
2026        ]);
2027        assert_eq!(state.update(Event::Collapse(1)), Some(Output::Collapsed(1)));
2028        assert_sizes(&pixels(&state), &[960.0, 40.0]);
2029        assert_eq!(state.update(Event::Expand(1)), Some(Output::Expanded(1)));
2030        assert_sizes(&pixels(&state), &[600.0, 400.0]);
2031    }
2032
2033    #[test]
2034    fn panels_that_cannot_collapse_ignore_it() {
2035        let mut state = measured(sidebar_and_content());
2036        assert_eq!(state.update(Event::Collapse(1)), None);
2037        assert_eq!(state.update(Event::Toggle(7)), None);
2038    }
2039
2040    #[test]
2041    fn reset_puts_the_defaults_back() {
2042        let mut state = measured(sidebar_and_content());
2043        let _ = state.update(Event::Collapse(0));
2044        assert_eq!(state.update(Event::Reset), Some(Output::Resized));
2045        assert!(!state.is_collapsed(0));
2046        assert_sizes(&pixels(&state), &[300.0, 700.0]);
2047        assert_eq!(state.update(Event::Reset), None);
2048    }
2049
2050    #[test]
2051    fn keyboard_steps_follow_the_step_sizes() {
2052        let state = measured(sidebar_and_content());
2053        let resize = |action: Action| action.event(&state, 0);
2054        assert_eq!(
2055            resize(Action::Increase),
2056            Some(Event::Resize {
2057                handle: 0,
2058                delta: 50.0
2059            })
2060        );
2061        assert_eq!(
2062            resize(Action::Decrease),
2063            Some(Event::Resize {
2064                handle: 0,
2065                delta: -50.0
2066            })
2067        );
2068        assert_eq!(
2069            resize(Action::IncreaseMore),
2070            Some(Event::Resize {
2071                handle: 0,
2072                delta: 200.0
2073            })
2074        );
2075        assert_eq!(
2076            resize(Action::DecreaseMore),
2077            Some(Event::Resize {
2078                handle: 0,
2079                delta: -200.0
2080            })
2081        );
2082
2083        let pixels = measured(sidebar_and_content())
2084            .step(Extent::Pixels(10.0))
2085            .large_step(Extent::Pixels(40.0));
2086        assert_eq!(
2087            Action::Increase.event(&pixels, 0),
2088            Some(Event::Resize {
2089                handle: 0,
2090                delta: 10.0
2091            })
2092        );
2093        assert_eq!(
2094            Action::DecreaseMore.event(&pixels, 0),
2095            Some(Event::Resize {
2096                handle: 0,
2097                delta: -40.0
2098            })
2099        );
2100    }
2101
2102    #[test]
2103    fn home_and_end_go_to_min_and_max() {
2104        let mut state = measured(sidebar_and_content());
2105        let home = Action::Min.event(&state, 0);
2106        assert_eq!(
2107            home,
2108            Some(Event::Resize {
2109                handle: 0,
2110                delta: -100.0
2111            })
2112        );
2113        if let Some(event) = home {
2114            let _ = state.update(event);
2115        }
2116        assert_sizes(&pixels(&state), &[200.0, 800.0]);
2117        if let Some(event) = Action::Max.event(&state, 0) {
2118            let _ = state.update(event);
2119        }
2120        assert_sizes(&pixels(&state), &[500.0, 500.0]);
2121    }
2122
2123    #[test]
2124    fn toggle_and_release_actions() {
2125        let state = measured(sidebar_and_content());
2126        assert_eq!(Action::Toggle.event(&state, 0), Some(Event::Toggle(0)));
2127        assert_eq!(Action::Release.event(&state, 0), None);
2128        assert_eq!(Action::Increase.event(&state, 1), None, "no such handle");
2129
2130        let neither = measured([panel(Extent::Fraction(0.5)), panel(Extent::Fraction(0.5))]);
2131        assert_eq!(Action::Toggle.event(&neither, 0), None);
2132        let after = measured([
2133            panel(Extent::Fraction(0.5)),
2134            panel(Extent::Fraction(0.5)).collapsible(true),
2135        ]);
2136        assert_eq!(Action::Toggle.event(&after, 0), Some(Event::Toggle(1)));
2137    }
2138
2139    fn press(keymap: &Keymap<Action>, chord: &str) -> Option<Action> {
2140        let chord: Chord = chord.parse().unwrap();
2141        keymap.resolve(chord.key(), chord.modifiers())
2142    }
2143
2144    #[test]
2145    fn default_keymap_covers_every_action() {
2146        let keymap = default_keymap();
2147        assert_eq!(press(&keymap, "ArrowLeft"), Some(Action::Decrease));
2148        assert_eq!(press(&keymap, "ArrowUp"), Some(Action::Decrease));
2149        assert_eq!(press(&keymap, "ArrowRight"), Some(Action::Increase));
2150        assert_eq!(press(&keymap, "ArrowDown"), Some(Action::Increase));
2151        assert_eq!(
2152            press(&keymap, "Shift+ArrowLeft"),
2153            Some(Action::DecreaseMore)
2154        );
2155        assert_eq!(
2156            press(&keymap, "Shift+ArrowDown"),
2157            Some(Action::IncreaseMore)
2158        );
2159        assert_eq!(press(&keymap, "Home"), Some(Action::Min));
2160        assert_eq!(press(&keymap, "End"), Some(Action::Max));
2161        assert_eq!(press(&keymap, "Enter"), Some(Action::Toggle));
2162        assert_eq!(press(&keymap, "Escape"), Some(Action::Release));
2163        for action in <Action as keys::Action>::ALL {
2164            assert!(!keymap.chords(action).is_empty(), "{action:?}");
2165        }
2166        assert_eq!(Keymap::<Action>::defaults(), keymap);
2167    }
2168
2169    #[test]
2170    fn keymap_overrides_and_unbinding() {
2171        let keymap = default_keymap()
2172            .unbind(&"Enter".parse().unwrap())
2173            .bind("Space".parse().unwrap(), Action::Toggle);
2174        assert_eq!(press(&keymap, "Enter"), None);
2175        assert_eq!(press(&keymap, "Space"), Some(Action::Toggle));
2176        assert!(keymap.clear().is_empty());
2177    }
2178
2179    #[test]
2180    fn stacking_only_turns_narrow_horizontal_groups() {
2181        let state = State::new(sidebar_and_content());
2182        let group: ResizablePanel<'_, ()> = resizable_panel(&state, []).stack_below(480.0);
2183        assert_eq!(group.axis_at(360.0), Axis::Vertical);
2184        assert_eq!(group.axis_at(720.0), Axis::Horizontal);
2185        let plain: ResizablePanel<'_, ()> = resizable_panel(&state, []);
2186        assert_eq!(plain.axis_at(360.0), Axis::Horizontal);
2187        let vertical: ResizablePanel<'_, ()> = resizable_panel(&state, [])
2188            .axis(Axis::Vertical)
2189            .stack_below(480.0);
2190        assert_eq!(vertical.axis_at(360.0), Axis::Vertical);
2191    }
2192
2193    #[test]
2194    fn builder_defaults() {
2195        let state = State::new(sidebar_and_content());
2196        let group: ResizablePanel<'_, ()> = resizable_panel(&state, []);
2197        assert_eq!(group.axis, Axis::Horizontal);
2198        assert!(!group.grip);
2199        assert_eq!(group.width, Length::Fill);
2200        assert_eq!(group.height, Length::Fill);
2201        assert!(group.on_event.is_none());
2202        assert_eq!(group.keymap, default_keymap());
2203    }
2204
2205    #[test]
2206    fn axes_pick_their_coordinate_and_cursor() {
2207        let point = Point::new(3.0, 7.0);
2208        assert_eq!(Axis::Horizontal.along(point), 3.0);
2209        assert_eq!(Axis::Vertical.along(point), 7.0);
2210        assert_eq!(
2211            Axis::Horizontal.cursor(),
2212            mouse::Interaction::ResizingColumn
2213        );
2214        assert_eq!(Axis::Vertical.cursor(), mouse::Interaction::ResizingRow);
2215    }
2216
2217    #[test]
2218    fn a_press_focuses_quietly_and_keys_or_operations_show_it() {
2219        let mut memory = Memory {
2220            handles: vec![HandleFocus::default(); 2],
2221            ..Memory::default()
2222        };
2223        memory.focus(Some(1));
2224        assert_eq!(memory.focused(), Some(1));
2225        assert_eq!(memory.visible(), None);
2226        memory.show_focus();
2227        assert_eq!(memory.visible(), Some(1));
2228        memory.focus(None);
2229        assert_eq!(memory.focused(), None);
2230
2231        memory.handles[0].focus();
2232        assert_eq!(memory.visible(), Some(0), "focus operations show it");
2233        memory.handles[0].unfocus();
2234        assert_eq!(memory.focused(), None);
2235    }
2236
2237    #[test]
2238    fn saved_sizes_below_the_threshold_restore_collapsed() {
2239        let panels = [
2240            panel(Extent::Pixels(240.0))
2241                .min(Extent::Pixels(180.0))
2242                .collapsible(true)
2243                .collapsed_size(Extent::Pixels(48.0)),
2244            panel(Extent::Fraction(1.0)),
2245        ];
2246        let mut state = State::new(panels).with_sizes([0.048, 0.952]);
2247        let _ = state.update(Event::Measure(TOTAL));
2248        assert!(state.is_collapsed(0));
2249        assert_sizes(&pixels(&state), &[48.0, 952.0]);
2250
2251        // A wider window keeps the rail at its pixel size.
2252        let _ = state.update(Event::Measure(2000.0));
2253        assert_sizes(&state.resolve(2000.0), &[48.0, 1952.0]);
2254        assert_eq!(state.update(Event::Expand(0)), Some(Output::Expanded(0)));
2255        assert_sizes(&state.resolve(2000.0), &[240.0, 1760.0]);
2256    }
2257
2258    #[test]
2259    fn handles_highlight_on_hover_drag_and_focus() {
2260        for theme in [light(), dark()] {
2261            let tokens = Tokens::of(&theme);
2262            let idle = handle_style(&tokens, HandleStatus::Idle);
2263            assert_eq!(idle.line, tokens.border);
2264            assert_eq!(idle.highlight, None);
2265            assert_eq!(
2266                handle_style(&tokens, HandleStatus::Disabled).highlight,
2267                None
2268            );
2269            assert_eq!(
2270                handle_style(&tokens, HandleStatus::Hovered).highlight,
2271                Some(tokens.border)
2272            );
2273            for status in [HandleStatus::Dragged, HandleStatus::Focused] {
2274                let style = handle_style(&tokens, status);
2275                assert_eq!(style.highlight, Some(tokens.ring));
2276                assert_eq!(style.grip_border, tokens.ring);
2277            }
2278        }
2279    }
2280}