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                            .disabled(false)
1366                            .component("iced_cube::resizable_panel")
1367                    });
1368                }
1369                operation.focusable(id, *line, handle);
1370            }
1371            for ((pane, tree), layout) in self
1372                .panes
1373                .iter_mut()
1374                .zip(children.iter_mut())
1375                .zip(layout.children())
1376            {
1377                pane.as_widget_mut()
1378                    .operate(tree, layout, renderer, operation);
1379            }
1380        });
1381    }
1382
1383    fn update(
1384        &mut self,
1385        tree: &mut Tree,
1386        event: &Input,
1387        layout: Layout<'_>,
1388        cursor: mouse::Cursor,
1389        renderer: &Renderer,
1390        clipboard: &mut dyn Clipboard,
1391        shell: &mut Shell<'_, Message>,
1392        viewport: &Rectangle,
1393    ) {
1394        if let Some(on_event) = &self.on_event {
1395            let memory = tree.state.downcast_mut::<Memory>();
1396            if handle_event(
1397                self.state,
1398                &self.keymap,
1399                on_event,
1400                memory,
1401                event,
1402                layout,
1403                cursor,
1404                shell,
1405            ) {
1406                return;
1407            }
1408        }
1409
1410        for ((pane, tree), layout) in self
1411            .panes
1412            .iter_mut()
1413            .zip(&mut tree.children)
1414            .zip(layout.children())
1415        {
1416            pane.as_widget_mut().update(
1417                tree, event, layout, cursor, renderer, clipboard, shell, viewport,
1418            );
1419        }
1420    }
1421
1422    fn mouse_interaction(
1423        &self,
1424        tree: &Tree,
1425        layout: Layout<'_>,
1426        cursor: mouse::Cursor,
1427        viewport: &Rectangle,
1428        renderer: &Renderer,
1429    ) -> mouse::Interaction {
1430        let memory = tree.state.downcast_ref::<Memory>();
1431        if self.on_event.is_some() {
1432            let over = cursor
1433                .position()
1434                .and_then(|point| memory.handle_at(layout.bounds(), point));
1435            if self.state.dragging().is_some() || over.is_some() {
1436                return memory.axis.cursor();
1437            }
1438        }
1439        self.panes
1440            .iter()
1441            .zip(&tree.children)
1442            .zip(layout.children())
1443            .map(|((pane, tree), layout)| {
1444                pane.as_widget()
1445                    .mouse_interaction(tree, layout, cursor, viewport, renderer)
1446            })
1447            .max()
1448            .unwrap_or_default()
1449    }
1450
1451    fn draw(
1452        &self,
1453        tree: &Tree,
1454        renderer: &mut Renderer,
1455        theme: &Theme,
1456        style: &renderer::Style,
1457        layout: Layout<'_>,
1458        cursor: mouse::Cursor,
1459        viewport: &Rectangle,
1460    ) {
1461        use iced::advanced::Renderer as _;
1462
1463        let memory = tree.state.downcast_ref::<Memory>();
1464        let bounds = layout.bounds();
1465        for (index, ((pane, tree), layout)) in self
1466            .panes
1467            .iter()
1468            .zip(&tree.children)
1469            .zip(layout.children())
1470            .enumerate()
1471        {
1472            let Some(slot) = memory.slot(bounds, index) else {
1473                continue;
1474            };
1475            if slot.width < 1.0 || slot.height < 1.0 {
1476                continue;
1477            }
1478            let Some(clip) = slot.intersection(viewport) else {
1479                continue;
1480            };
1481            renderer.with_layer(clip, |renderer| {
1482                pane.as_widget()
1483                    .draw(tree, renderer, theme, style, layout, cursor, &clip);
1484            });
1485        }
1486
1487        let tokens = Tokens::of(theme);
1488        let focused = memory.visible();
1489        let over = cursor
1490            .position()
1491            .and_then(|point| memory.handle_at(bounds, point));
1492        for (index, line) in memory.lines(bounds).into_iter().enumerate() {
1493            let status = if self.on_event.is_none() {
1494                HandleStatus::Disabled
1495            } else if self.state.dragging() == Some(index) {
1496                HandleStatus::Dragged
1497            } else if focused == Some(index) {
1498                HandleStatus::Focused
1499            } else if over == Some(index) {
1500                HandleStatus::Hovered
1501            } else {
1502                HandleStatus::Idle
1503            };
1504            draw_handle(
1505                renderer,
1506                line,
1507                memory.axis,
1508                handle_style(&tokens, status),
1509                self.grip,
1510            );
1511        }
1512    }
1513
1514    fn overlay<'b>(
1515        &'b mut self,
1516        tree: &'b mut Tree,
1517        layout: Layout<'b>,
1518        renderer: &Renderer,
1519        viewport: &Rectangle,
1520        translation: Vector,
1521    ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
1522        overlay::from_children(
1523            &mut self.panes,
1524            tree,
1525            layout,
1526            renderer,
1527            viewport,
1528            translation,
1529        )
1530    }
1531}
1532
1533/// Handles pointer and key events on the handles. Returns whether the
1534/// event was used, so the panes never see it.
1535#[allow(clippy::too_many_arguments)]
1536fn handle_event<Message>(
1537    state: &State,
1538    keymap: &Keymap<Action>,
1539    on_event: &dyn Fn(Event) -> Message,
1540    memory: &mut Memory,
1541    event: &Input,
1542    layout: Layout<'_>,
1543    cursor: mouse::Cursor,
1544    shell: &mut Shell<'_, Message>,
1545) -> bool {
1546    if memory.total > 0.0 && (state.total() - memory.total).abs() > 0.5 {
1547        shell.publish(on_event(Event::Measure(memory.total)));
1548    }
1549    let axis = memory.axis;
1550
1551    match event {
1552        Input::Mouse(mouse::Event::CursorMoved { .. })
1553        | Input::Touch(touch::Event::FingerMoved { .. }) => {
1554            let Some(point) = pointer(event, cursor) else {
1555                return false;
1556            };
1557            if state.dragging().is_some() {
1558                shell.publish(on_event(Event::Drag(axis.along(point))));
1559                shell.capture_event();
1560                return true;
1561            }
1562            let hovered = memory.handle_at(layout.bounds(), point);
1563            if hovered != memory.hovered {
1564                memory.hovered = hovered;
1565                shell.request_redraw();
1566            }
1567            false
1568        }
1569        Input::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
1570        | Input::Touch(touch::Event::FingerPressed { .. }) => {
1571            let hit = pointer(event, cursor).and_then(|point| {
1572                memory
1573                    .handle_at(layout.bounds(), point)
1574                    .map(|handle| (handle, point))
1575            });
1576            let Some((handle, point)) = hit else {
1577                if memory.focused().is_some() {
1578                    memory.focus(None);
1579                    shell.request_redraw();
1580                }
1581                return false;
1582            };
1583            memory.focus(Some(handle));
1584            shell.publish(on_event(Event::DragStart {
1585                handle,
1586                position: axis.along(point),
1587            }));
1588            shell.capture_event();
1589            true
1590        }
1591        Input::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
1592        | Input::Touch(touch::Event::FingerLifted { .. } | touch::Event::FingerLost { .. }) => {
1593            if state.dragging().is_none() {
1594                return false;
1595            }
1596            shell.publish(on_event(Event::DragEnd));
1597            shell.capture_event();
1598            true
1599        }
1600        Input::Keyboard(keyboard::Event::KeyPressed { key, modifiers, .. }) => {
1601            let Some(handle) = memory.focused() else {
1602                return false;
1603            };
1604            let Some(action) = keymap.resolve(key, *modifiers) else {
1605                return false;
1606            };
1607            if action == Action::Release {
1608                memory.focus(None);
1609            } else {
1610                memory.show_focus();
1611                if let Some(event) = action.event(state, handle) {
1612                    shell.publish(on_event(event));
1613                }
1614            }
1615            shell.request_redraw();
1616            shell.capture_event();
1617            true
1618        }
1619        _ => false,
1620    }
1621}
1622
1623fn draw_handle(
1624    renderer: &mut Renderer,
1625    line: Rectangle,
1626    axis: Axis,
1627    style: HandleStyle,
1628    grip: bool,
1629) {
1630    use iced::advanced::Renderer as _;
1631
1632    let quad = |bounds: Rectangle, radius: f32, border: Option<Color>| renderer::Quad {
1633        bounds,
1634        border: Border {
1635            color: border.unwrap_or(Color::TRANSPARENT),
1636            width: if border.is_some() { 1.0 } else { 0.0 },
1637            radius: radius.into(),
1638        },
1639        shadow: Shadow::default(),
1640        snap: true,
1641    };
1642
1643    renderer.fill_quad(quad(line, 0.0, None), Background::Color(style.line));
1644
1645    if let Some(colour) = style.highlight {
1646        let grow = (HIGHLIGHT - HANDLE) / 2.0;
1647        let bar = match axis {
1648            Axis::Horizontal => Rectangle {
1649                x: line.x - grow,
1650                width: HIGHLIGHT,
1651                ..line
1652            },
1653            Axis::Vertical => Rectangle {
1654                y: line.y - grow,
1655                height: HIGHLIGHT,
1656                ..line
1657            },
1658        };
1659        renderer.fill_quad(quad(bar, 0.0, None), Background::Color(colour));
1660    }
1661
1662    if !grip {
1663        return;
1664    }
1665    let size = match axis {
1666        Axis::Horizontal => GRIP,
1667        Axis::Vertical => Size::new(GRIP.height, GRIP.width),
1668    };
1669    let centre = line.center();
1670    let bounds = Rectangle::new(
1671        Point::new(
1672            (centre.x - size.width / 2.0).round(),
1673            (centre.y - size.height / 2.0).round(),
1674        ),
1675        size,
1676    );
1677    renderer.fill_quad(
1678        quad(bounds, radius::SM, Some(style.grip_border)),
1679        Background::Color(style.grip),
1680    );
1681
1682    // Two rows of three dots, turned to follow the handle.
1683    let dot = 2.0;
1684    for column in [-1.0_f32, 1.0] {
1685        for row in [-1.0_f32, 0.0, 1.0] {
1686            let (dx, dy) = match axis {
1687                Axis::Horizontal => (column * 2.0, row * 4.0),
1688                Axis::Vertical => (row * 4.0, column * 2.0),
1689            };
1690            let position = Point::new(
1691                (centre.x + dx - dot / 2.0).round(),
1692                (centre.y + dy - dot / 2.0).round(),
1693            );
1694            renderer.fill_quad(
1695                quad(Rectangle::new(position, Size::new(dot, dot)), 1.0, None),
1696                Background::Color(style.grip_dots),
1697            );
1698        }
1699    }
1700}
1701
1702#[cfg(test)]
1703mod tests {
1704    use super::*;
1705    use crate::theme::{dark, light};
1706
1707    const TOTAL: f32 = 1000.0;
1708
1709    fn close(a: f32, b: f32) -> bool {
1710        (a - b).abs() < 0.05
1711    }
1712
1713    fn assert_sizes(actual: &[f32], expected: &[f32]) {
1714        assert_eq!(actual.len(), expected.len(), "{actual:?}");
1715        for (a, b) in actual.iter().zip(expected) {
1716            assert!(close(*a, *b), "{actual:?} != {expected:?}");
1717        }
1718    }
1719
1720    fn measured(panels: impl IntoIterator<Item = Panel>) -> State {
1721        let mut state = State::new(panels);
1722        let _ = state.update(Event::Measure(TOTAL));
1723        state
1724    }
1725
1726    fn pixels(state: &State) -> Vec<f32> {
1727        state.resolve(state.total())
1728    }
1729
1730    fn sidebar_and_content() -> [Panel; 2] {
1731        [
1732            panel(Extent::Fraction(0.3))
1733                .min(Extent::Pixels(200.0))
1734                .max(Extent::Fraction(0.5))
1735                .collapsible(true),
1736            panel(Extent::Fraction(0.7)).min(Extent::Pixels(300.0)),
1737        ]
1738    }
1739
1740    #[test]
1741    fn panel_defaults() {
1742        let panel = panel(Extent::Fraction(0.5));
1743        assert_eq!(panel.min, Extent::Pixels(0.0));
1744        assert_eq!(panel.max, Extent::Fraction(1.0));
1745        assert!(!panel.collapsible);
1746        assert_eq!(panel.collapsed_size, Extent::Pixels(0.0));
1747        assert_eq!(panel.collapse_below, None);
1748    }
1749
1750    #[test]
1751    fn extents_resolve_to_pixels() {
1752        assert_eq!(Extent::Fraction(0.25).resolve(800.0), 200.0);
1753        assert_eq!(Extent::Pixels(120.0).resolve(800.0), 120.0);
1754        assert_eq!(Extent::Pixels(-5.0).resolve(800.0), 0.0);
1755    }
1756
1757    #[test]
1758    fn limits_keep_max_above_min_and_threshold_between() {
1759        let limits = panel(Extent::Fraction(0.5))
1760            .min(Extent::Pixels(300.0))
1761            .max(Extent::Pixels(100.0))
1762            .collapsible(true)
1763            .collapsed_size(Extent::Pixels(40.0))
1764            .limits(TOTAL);
1765        assert_eq!(limits.min, 300.0);
1766        assert_eq!(limits.max, 300.0);
1767        assert_eq!(limits.collapsed, 40.0);
1768        assert_eq!(limits.threshold, 170.0);
1769
1770        let custom = panel(Extent::Fraction(0.5))
1771            .min(Extent::Pixels(200.0))
1772            .collapsible(true)
1773            .collapse_below(Extent::Pixels(500.0))
1774            .limits(TOTAL);
1775        assert_eq!(custom.threshold, 200.0, "never above the minimum");
1776    }
1777
1778    #[test]
1779    fn settle_clamps_and_snaps_at_the_threshold() {
1780        let plain = panel(Extent::Fraction(0.5))
1781            .min(Extent::Pixels(100.0))
1782            .max(Extent::Pixels(400.0));
1783        assert_eq!(plain.settle(50.0, TOTAL), 100.0);
1784        assert_eq!(plain.settle(250.0, TOTAL), 250.0);
1785        assert_eq!(plain.settle(900.0, TOTAL), 400.0);
1786
1787        let collapsible = plain.collapsible(true);
1788        assert_eq!(
1789            collapsible.settle(60.0, TOTAL),
1790            100.0,
1791            "above the threshold"
1792        );
1793        assert_eq!(collapsible.settle(49.0, TOTAL), 0.0, "below the threshold");
1794        assert!(collapsible.is_collapsed_at(0.0, TOTAL));
1795        assert!(!plain.is_collapsed_at(0.0, TOTAL));
1796    }
1797
1798    #[test]
1799    fn defaults_share_what_pixel_panels_leave() {
1800        let panels = [
1801            panel(Extent::Pixels(250.0)),
1802            panel(Extent::Fraction(1.0)),
1803            panel(Extent::Fraction(1.0)),
1804        ];
1805        assert_sizes(&defaults(&panels, TOTAL), &[250.0, 375.0, 375.0]);
1806    }
1807
1808    #[test]
1809    fn defaults_respect_limits_and_initial_collapse() {
1810        let panels = [
1811            panel(Extent::Fraction(0.1)).min(Extent::Pixels(200.0)),
1812            panel(Extent::Fraction(0.9)),
1813        ];
1814        assert_sizes(&defaults(&panels, TOTAL), &[200.0, 800.0]);
1815
1816        let collapsed = [
1817            panel(Extent::Fraction(0.3))
1818                .collapsible(true)
1819                .collapsed_size(Extent::Pixels(48.0))
1820                .min(Extent::Pixels(200.0))
1821                .collapsed(true),
1822            panel(Extent::Fraction(0.7)),
1823        ];
1824        assert_sizes(&defaults(&collapsed, TOTAL), &[48.0, 952.0]);
1825    }
1826
1827    #[test]
1828    fn fit_always_adds_up() {
1829        let panels = sidebar_and_content();
1830        for total in [1200.0, 600.0, 450.0, 320.0, 100.0] {
1831            let sizes = fit(
1832                &panels,
1833                &[false, false],
1834                vec![0.3 * total, 0.7 * total],
1835                total,
1836            );
1837            let sum: f32 = sizes.iter().sum();
1838            assert!(close(sum, total), "{total}: {sizes:?}");
1839        }
1840    }
1841
1842    #[test]
1843    fn fit_collapses_side_panels_when_minimums_do_not_fit() {
1844        let panels = sidebar_and_content();
1845        // 200 + 300 minimums do not fit in 360, so the sidebar collapses.
1846        let sizes = fit(&panels, &[false, false], vec![108.0, 252.0], 360.0);
1847        assert_sizes(&sizes, &[0.0, 360.0]);
1848    }
1849
1850    #[test]
1851    fn fit_scales_as_a_last_resort() {
1852        let panels = [
1853            panel(Extent::Fraction(0.5)).min(Extent::Pixels(300.0)),
1854            panel(Extent::Fraction(0.5)).min(Extent::Pixels(300.0)),
1855        ];
1856        let sizes = fit(&panels, &[false, false], vec![200.0, 200.0], 400.0);
1857        assert_sizes(&sizes, &[200.0, 200.0]);
1858    }
1859
1860    #[test]
1861    fn state_starts_from_defaults_once_measured() {
1862        let state = State::new(sidebar_and_content());
1863        assert_eq!(state.total(), 0.0);
1864        assert_sizes(&state.sizes(), &[0.3, 0.7]);
1865        assert_sizes(&state.resolve(TOTAL), &[300.0, 700.0]);
1866
1867        let state = measured(sidebar_and_content());
1868        assert_eq!(state.total(), TOTAL);
1869        assert_sizes(&state.sizes(), &[0.3, 0.7]);
1870    }
1871
1872    #[test]
1873    fn unmeasured_state_ignores_changes() {
1874        let mut state = State::new(sidebar_and_content());
1875        assert_eq!(
1876            state.update(Event::Resize {
1877                handle: 0,
1878                delta: 50.0
1879            }),
1880            None
1881        );
1882        assert_eq!(state.update(Event::Collapse(0)), None);
1883        let _ = state.update(Event::Measure(-1.0));
1884        let _ = state.update(Event::Measure(f32::INFINITY));
1885        assert_eq!(state.total(), 0.0);
1886    }
1887
1888    #[test]
1889    fn saved_sizes_round_trip_and_bad_ones_are_ignored() {
1890        let state = State::new(sidebar_and_content()).with_sizes([2.0, 6.0]);
1891        assert_sizes(&state.sizes(), &[0.25, 0.75]);
1892
1893        let wrong_length = State::new(sidebar_and_content()).with_sizes([1.0]);
1894        assert_sizes(&wrong_length.sizes(), &[0.3, 0.7]);
1895        let empty = State::new(sidebar_and_content()).with_sizes([0.0, 0.0]);
1896        assert_sizes(&empty.sizes(), &[0.3, 0.7]);
1897
1898        let collapsed = State::new(sidebar_and_content()).with_sizes([0.0, 1.0]);
1899        assert!(collapsed.is_collapsed(0));
1900    }
1901
1902    #[test]
1903    fn dragging_moves_the_handle_from_where_it_started() {
1904        let mut state = measured(sidebar_and_content());
1905        let _ = state.update(Event::DragStart {
1906            handle: 0,
1907            position: 300.0,
1908        });
1909        assert_eq!(state.dragging(), Some(0));
1910        assert_eq!(state.update(Event::Drag(340.0)), Some(Output::Resized));
1911        assert_sizes(&pixels(&state), &[340.0, 660.0]);
1912        assert_eq!(state.update(Event::Drag(320.0)), Some(Output::Resized));
1913        assert_sizes(&pixels(&state), &[320.0, 680.0]);
1914        let _ = state.update(Event::DragEnd);
1915        assert_eq!(state.dragging(), None);
1916        assert_eq!(state.update(Event::Drag(500.0)), None);
1917    }
1918
1919    #[test]
1920    fn drags_clamp_to_min_and_max() {
1921        let mut state = measured(sidebar_and_content());
1922        let _ = state.update(Event::DragStart {
1923            handle: 0,
1924            position: 300.0,
1925        });
1926        let _ = state.update(Event::Drag(900.0));
1927        assert_sizes(&pixels(&state), &[500.0, 500.0]);
1928        let _ = state.update(Event::Drag(160.0));
1929        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1930    }
1931
1932    #[test]
1933    fn collapsible_panels_snap_shut_below_the_threshold_and_open_past_it() {
1934        let mut state = measured(sidebar_and_content());
1935        let _ = state.update(Event::DragStart {
1936            handle: 0,
1937            position: 300.0,
1938        });
1939        // Threshold is 100: halfway between collapsed (0) and min (200).
1940        let _ = state.update(Event::Drag(110.0));
1941        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1942        assert_eq!(state.update(Event::Drag(90.0)), Some(Output::Collapsed(0)));
1943        assert!(state.is_collapsed(0));
1944        assert_sizes(&pixels(&state), &[0.0, 1000.0]);
1945        assert_eq!(state.update(Event::Drag(120.0)), Some(Output::Expanded(0)));
1946        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1947        let _ = state.update(Event::DragEnd);
1948
1949        let _ = state.update(Event::Collapse(0));
1950        let _ = state.update(Event::DragStart {
1951            handle: 0,
1952            position: 0.0,
1953        });
1954        assert_eq!(
1955            state.update(Event::Drag(60.0)),
1956            None,
1957            "still below the threshold"
1958        );
1959        assert!(state.is_collapsed(0));
1960        assert_eq!(state.update(Event::Drag(101.0)), Some(Output::Expanded(0)));
1961        assert_sizes(&pixels(&state), &[200.0, 800.0]);
1962    }
1963
1964    #[test]
1965    fn shrinking_carries_on_past_a_panel_at_its_minimum() {
1966        let mut state = measured([
1967            panel(Extent::Fraction(0.4)),
1968            panel(Extent::Fraction(0.3)).min(Extent::Pixels(200.0)),
1969            panel(Extent::Fraction(0.3)).min(Extent::Pixels(100.0)),
1970        ]);
1971        let _ = state.update(Event::Resize {
1972            handle: 0,
1973            delta: 250.0,
1974        });
1975        assert_sizes(&pixels(&state), &[650.0, 200.0, 150.0]);
1976        let _ = state.update(Event::Resize {
1977            handle: 0,
1978            delta: 500.0,
1979        });
1980        assert_sizes(&pixels(&state), &[700.0, 200.0, 100.0]);
1981    }
1982
1983    #[test]
1984    fn growth_past_a_maximum_passes_outwards() {
1985        let origin = [200.0, 300.0, 500.0];
1986        let panels = [
1987            panel(Extent::Fraction(0.2)),
1988            panel(Extent::Fraction(0.3)).max(Extent::Pixels(350.0)),
1989            panel(Extent::Fraction(0.5)),
1990        ];
1991        let sizes = resize(&panels, &origin, 1, 100.0, TOTAL);
1992        assert_sizes(&sizes, &[250.0, 350.0, 400.0]);
1993    }
1994
1995    #[test]
1996    fn resize_ignores_bad_handles_and_zero_moves() {
1997        let panels = sidebar_and_content();
1998        let origin = [300.0, 700.0];
1999        assert_eq!(resize(&panels, &origin, 1, 50.0, TOTAL), origin);
2000        assert_eq!(resize(&panels, &origin, 0, 0.0, TOTAL), origin);
2001        assert_eq!(resize(&panels, &[300.0], 0, 50.0, TOTAL), [300.0]);
2002    }
2003
2004    #[test]
2005    fn toggle_collapses_and_restores_the_previous_size() {
2006        let mut state = measured(sidebar_and_content());
2007        let _ = state.update(Event::Resize {
2008            handle: 0,
2009            delta: 100.0,
2010        });
2011        assert_eq!(state.update(Event::Toggle(0)), Some(Output::Collapsed(0)));
2012        assert_sizes(&pixels(&state), &[0.0, 1000.0]);
2013        assert_eq!(state.update(Event::Collapse(0)), None);
2014        assert_eq!(state.update(Event::Toggle(0)), Some(Output::Expanded(0)));
2015        assert_sizes(&pixels(&state), &[400.0, 600.0]);
2016        assert_eq!(state.update(Event::Expand(0)), None);
2017    }
2018
2019    #[test]
2020    fn the_last_panel_collapses_through_the_handle_before_it() {
2021        let mut state = measured([
2022            panel(Extent::Fraction(0.6)),
2023            panel(Extent::Fraction(0.4))
2024                .collapsible(true)
2025                .collapsed_size(Extent::Pixels(40.0))
2026                .min(Extent::Pixels(200.0)),
2027        ]);
2028        assert_eq!(state.update(Event::Collapse(1)), Some(Output::Collapsed(1)));
2029        assert_sizes(&pixels(&state), &[960.0, 40.0]);
2030        assert_eq!(state.update(Event::Expand(1)), Some(Output::Expanded(1)));
2031        assert_sizes(&pixels(&state), &[600.0, 400.0]);
2032    }
2033
2034    #[test]
2035    fn panels_that_cannot_collapse_ignore_it() {
2036        let mut state = measured(sidebar_and_content());
2037        assert_eq!(state.update(Event::Collapse(1)), None);
2038        assert_eq!(state.update(Event::Toggle(7)), None);
2039    }
2040
2041    #[test]
2042    fn reset_puts_the_defaults_back() {
2043        let mut state = measured(sidebar_and_content());
2044        let _ = state.update(Event::Collapse(0));
2045        assert_eq!(state.update(Event::Reset), Some(Output::Resized));
2046        assert!(!state.is_collapsed(0));
2047        assert_sizes(&pixels(&state), &[300.0, 700.0]);
2048        assert_eq!(state.update(Event::Reset), None);
2049    }
2050
2051    #[test]
2052    fn keyboard_steps_follow_the_step_sizes() {
2053        let state = measured(sidebar_and_content());
2054        let resize = |action: Action| action.event(&state, 0);
2055        assert_eq!(
2056            resize(Action::Increase),
2057            Some(Event::Resize {
2058                handle: 0,
2059                delta: 50.0
2060            })
2061        );
2062        assert_eq!(
2063            resize(Action::Decrease),
2064            Some(Event::Resize {
2065                handle: 0,
2066                delta: -50.0
2067            })
2068        );
2069        assert_eq!(
2070            resize(Action::IncreaseMore),
2071            Some(Event::Resize {
2072                handle: 0,
2073                delta: 200.0
2074            })
2075        );
2076        assert_eq!(
2077            resize(Action::DecreaseMore),
2078            Some(Event::Resize {
2079                handle: 0,
2080                delta: -200.0
2081            })
2082        );
2083
2084        let pixels = measured(sidebar_and_content())
2085            .step(Extent::Pixels(10.0))
2086            .large_step(Extent::Pixels(40.0));
2087        assert_eq!(
2088            Action::Increase.event(&pixels, 0),
2089            Some(Event::Resize {
2090                handle: 0,
2091                delta: 10.0
2092            })
2093        );
2094        assert_eq!(
2095            Action::DecreaseMore.event(&pixels, 0),
2096            Some(Event::Resize {
2097                handle: 0,
2098                delta: -40.0
2099            })
2100        );
2101    }
2102
2103    #[test]
2104    fn home_and_end_go_to_min_and_max() {
2105        let mut state = measured(sidebar_and_content());
2106        let home = Action::Min.event(&state, 0);
2107        assert_eq!(
2108            home,
2109            Some(Event::Resize {
2110                handle: 0,
2111                delta: -100.0
2112            })
2113        );
2114        if let Some(event) = home {
2115            let _ = state.update(event);
2116        }
2117        assert_sizes(&pixels(&state), &[200.0, 800.0]);
2118        if let Some(event) = Action::Max.event(&state, 0) {
2119            let _ = state.update(event);
2120        }
2121        assert_sizes(&pixels(&state), &[500.0, 500.0]);
2122    }
2123
2124    #[test]
2125    fn toggle_and_release_actions() {
2126        let state = measured(sidebar_and_content());
2127        assert_eq!(Action::Toggle.event(&state, 0), Some(Event::Toggle(0)));
2128        assert_eq!(Action::Release.event(&state, 0), None);
2129        assert_eq!(Action::Increase.event(&state, 1), None, "no such handle");
2130
2131        let neither = measured([panel(Extent::Fraction(0.5)), panel(Extent::Fraction(0.5))]);
2132        assert_eq!(Action::Toggle.event(&neither, 0), None);
2133        let after = measured([
2134            panel(Extent::Fraction(0.5)),
2135            panel(Extent::Fraction(0.5)).collapsible(true),
2136        ]);
2137        assert_eq!(Action::Toggle.event(&after, 0), Some(Event::Toggle(1)));
2138    }
2139
2140    fn press(keymap: &Keymap<Action>, chord: &str) -> Option<Action> {
2141        let chord: Chord = chord.parse().unwrap();
2142        keymap.resolve(chord.key(), chord.modifiers())
2143    }
2144
2145    #[test]
2146    fn default_keymap_covers_every_action() {
2147        let keymap = default_keymap();
2148        assert_eq!(press(&keymap, "ArrowLeft"), Some(Action::Decrease));
2149        assert_eq!(press(&keymap, "ArrowUp"), Some(Action::Decrease));
2150        assert_eq!(press(&keymap, "ArrowRight"), Some(Action::Increase));
2151        assert_eq!(press(&keymap, "ArrowDown"), Some(Action::Increase));
2152        assert_eq!(
2153            press(&keymap, "Shift+ArrowLeft"),
2154            Some(Action::DecreaseMore)
2155        );
2156        assert_eq!(
2157            press(&keymap, "Shift+ArrowDown"),
2158            Some(Action::IncreaseMore)
2159        );
2160        assert_eq!(press(&keymap, "Home"), Some(Action::Min));
2161        assert_eq!(press(&keymap, "End"), Some(Action::Max));
2162        assert_eq!(press(&keymap, "Enter"), Some(Action::Toggle));
2163        assert_eq!(press(&keymap, "Escape"), Some(Action::Release));
2164        for action in <Action as keys::Action>::ALL {
2165            assert!(!keymap.chords(action).is_empty(), "{action:?}");
2166        }
2167        assert_eq!(Keymap::<Action>::defaults(), keymap);
2168    }
2169
2170    #[test]
2171    fn keymap_overrides_and_unbinding() {
2172        let keymap = default_keymap()
2173            .unbind(&"Enter".parse().unwrap())
2174            .bind("Space".parse().unwrap(), Action::Toggle);
2175        assert_eq!(press(&keymap, "Enter"), None);
2176        assert_eq!(press(&keymap, "Space"), Some(Action::Toggle));
2177        assert!(keymap.clear().is_empty());
2178    }
2179
2180    #[test]
2181    fn stacking_only_turns_narrow_horizontal_groups() {
2182        let state = State::new(sidebar_and_content());
2183        let group: ResizablePanel<'_, ()> = resizable_panel(&state, []).stack_below(480.0);
2184        assert_eq!(group.axis_at(360.0), Axis::Vertical);
2185        assert_eq!(group.axis_at(720.0), Axis::Horizontal);
2186        let plain: ResizablePanel<'_, ()> = resizable_panel(&state, []);
2187        assert_eq!(plain.axis_at(360.0), Axis::Horizontal);
2188        let vertical: ResizablePanel<'_, ()> = resizable_panel(&state, [])
2189            .axis(Axis::Vertical)
2190            .stack_below(480.0);
2191        assert_eq!(vertical.axis_at(360.0), Axis::Vertical);
2192    }
2193
2194    #[test]
2195    fn builder_defaults() {
2196        let state = State::new(sidebar_and_content());
2197        let group: ResizablePanel<'_, ()> = resizable_panel(&state, []);
2198        assert_eq!(group.axis, Axis::Horizontal);
2199        assert!(!group.grip);
2200        assert_eq!(group.width, Length::Fill);
2201        assert_eq!(group.height, Length::Fill);
2202        assert!(group.on_event.is_none());
2203        assert_eq!(group.keymap, default_keymap());
2204    }
2205
2206    #[test]
2207    fn axes_pick_their_coordinate_and_cursor() {
2208        let point = Point::new(3.0, 7.0);
2209        assert_eq!(Axis::Horizontal.along(point), 3.0);
2210        assert_eq!(Axis::Vertical.along(point), 7.0);
2211        assert_eq!(
2212            Axis::Horizontal.cursor(),
2213            mouse::Interaction::ResizingColumn
2214        );
2215        assert_eq!(Axis::Vertical.cursor(), mouse::Interaction::ResizingRow);
2216    }
2217
2218    #[test]
2219    fn a_press_focuses_quietly_and_keys_or_operations_show_it() {
2220        let mut memory = Memory {
2221            handles: vec![HandleFocus::default(); 2],
2222            ..Memory::default()
2223        };
2224        memory.focus(Some(1));
2225        assert_eq!(memory.focused(), Some(1));
2226        assert_eq!(memory.visible(), None);
2227        memory.show_focus();
2228        assert_eq!(memory.visible(), Some(1));
2229        memory.focus(None);
2230        assert_eq!(memory.focused(), None);
2231
2232        memory.handles[0].focus();
2233        assert_eq!(memory.visible(), Some(0), "focus operations show it");
2234        memory.handles[0].unfocus();
2235        assert_eq!(memory.focused(), None);
2236    }
2237
2238    #[test]
2239    fn saved_sizes_below_the_threshold_restore_collapsed() {
2240        let panels = [
2241            panel(Extent::Pixels(240.0))
2242                .min(Extent::Pixels(180.0))
2243                .collapsible(true)
2244                .collapsed_size(Extent::Pixels(48.0)),
2245            panel(Extent::Fraction(1.0)),
2246        ];
2247        let mut state = State::new(panels).with_sizes([0.048, 0.952]);
2248        let _ = state.update(Event::Measure(TOTAL));
2249        assert!(state.is_collapsed(0));
2250        assert_sizes(&pixels(&state), &[48.0, 952.0]);
2251
2252        // A wider window keeps the rail at its pixel size.
2253        let _ = state.update(Event::Measure(2000.0));
2254        assert_sizes(&state.resolve(2000.0), &[48.0, 1952.0]);
2255        assert_eq!(state.update(Event::Expand(0)), Some(Output::Expanded(0)));
2256        assert_sizes(&state.resolve(2000.0), &[240.0, 1760.0]);
2257    }
2258
2259    #[test]
2260    fn handles_highlight_on_hover_drag_and_focus() {
2261        for theme in [light(), dark()] {
2262            let tokens = Tokens::of(&theme);
2263            let idle = handle_style(&tokens, HandleStatus::Idle);
2264            assert_eq!(idle.line, tokens.border);
2265            assert_eq!(idle.highlight, None);
2266            assert_eq!(
2267                handle_style(&tokens, HandleStatus::Disabled).highlight,
2268                None
2269            );
2270            assert_eq!(
2271                handle_style(&tokens, HandleStatus::Hovered).highlight,
2272                Some(tokens.border)
2273            );
2274            for status in [HandleStatus::Dragged, HandleStatus::Focused] {
2275                let style = handle_style(&tokens, status);
2276                assert_eq!(style.highlight, Some(tokens.ring));
2277                assert_eq!(style.grip_border, tokens.ring);
2278            }
2279        }
2280    }
2281}