Skip to main content

iced_cube/layout/
accordion.rs

1//! Sections that expand and collapse under their headers.
2//!
3//! [`State`] tracks which sections are open. In [`Mode::Single`] opening a
4//! section closes the others; in [`Mode::Multiple`] each one is independent.
5
6use iced::keyboard::key::Named;
7use iced::widget::{self, button::Status, column, container, row, rule, text};
8use iced::{Alignment, Element, Length, Theme};
9
10use crate::icon::{opacity, themed};
11use crate::keys::{self, Chord, Keymap};
12use crate::theme::{Tokens, fade, mix, space, text_size};
13
14/// How many sections may be open at once.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
16pub enum Mode {
17    /// At most one section is open.
18    #[default]
19    Single,
20    /// Any number of sections are open.
21    Multiple,
22}
23
24impl Mode {
25    pub const ALL: [Mode; 2] = [Mode::Single, Mode::Multiple];
26}
27
28/// Opens or closes a section.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum Event<Id> {
31    Toggle(Id),
32    Open(Id),
33    Close(Id),
34}
35
36/// What changed after an [`Event`].
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum Output<Id> {
39    Opened(Id),
40    Closed(Id),
41}
42
43/// Which sections are open.
44#[derive(Debug, Clone, PartialEq, Eq, Default)]
45pub struct State<Id> {
46    mode: Mode,
47    open: Vec<Id>,
48}
49
50impl<Id: Copy + PartialEq> State<Id> {
51    /// Starts with every section closed.
52    pub fn new(mode: Mode) -> Self {
53        Self {
54            mode,
55            open: Vec::new(),
56        }
57    }
58
59    /// Starts with `id` open.
60    pub fn with_open(mut self, id: Id) -> Self {
61        let _ = self.update(Event::Open(id));
62        self
63    }
64
65    pub fn mode(&self) -> Mode {
66        self.mode
67    }
68
69    /// Switches mode. Going to single mode keeps only the first open section.
70    pub fn set_mode(&mut self, mode: Mode) {
71        self.mode = mode;
72        if mode == Mode::Single {
73            self.open.truncate(1);
74        }
75    }
76
77    pub fn is_open(&self, id: Id) -> bool {
78        self.open.contains(&id)
79    }
80
81    /// The open sections, in the order they were opened.
82    pub fn open(&self) -> &[Id] {
83        &self.open
84    }
85
86    pub fn update(&mut self, event: Event<Id>) -> Option<Output<Id>> {
87        match event {
88            Event::Toggle(id) if self.is_open(id) => self.update(Event::Close(id)),
89            Event::Toggle(id) | Event::Open(id) => {
90                if self.is_open(id) {
91                    return None;
92                }
93                if self.mode == Mode::Single {
94                    self.open.clear();
95                }
96                self.open.push(id);
97                Some(Output::Opened(id))
98            }
99            Event::Close(id) => {
100                let index = self.open.iter().position(|open| *open == id)?;
101                let _ = self.open.remove(index);
102                Some(Output::Closed(id))
103            }
104        }
105    }
106}
107
108/// What an accordion keyboard shortcut does.
109#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
110pub enum Action {
111    /// Moves the most recently opened section to the next one, wrapping.
112    Next,
113    /// Moves the most recently opened section to the previous one, wrapping.
114    Previous,
115    ExpandAll,
116    CollapseAll,
117}
118
119impl Action {
120    /// The events this action sends to `state`, given every section id in
121    /// display order. `ExpandAll` does nothing in [`Mode::Single`].
122    pub fn events<Id: Copy + PartialEq>(
123        self,
124        state: &State<Id>,
125        sections: &[Id],
126    ) -> Vec<Event<Id>> {
127        match self {
128            Action::Next => step(state, sections, true),
129            Action::Previous => step(state, sections, false),
130            Action::ExpandAll if state.mode == Mode::Single => Vec::new(),
131            Action::ExpandAll => sections
132                .iter()
133                .filter(|id| !state.is_open(**id))
134                .map(|id| Event::Open(*id))
135                .collect(),
136            Action::CollapseAll => state.open.iter().map(|id| Event::Close(*id)).collect(),
137        }
138    }
139}
140
141fn step<Id: Copy + PartialEq>(state: &State<Id>, sections: &[Id], forward: bool) -> Vec<Event<Id>> {
142    let len = sections.len();
143    if len == 0 {
144        return Vec::new();
145    }
146    let current = state.open.last().copied();
147    let index = current.and_then(|id| sections.iter().position(|section| *section == id));
148    let target = match (index, forward) {
149        (Some(index), true) => (index + 1) % len,
150        (Some(index), false) => (index + len - 1) % len,
151        (None, true) => 0,
152        (None, false) => len - 1,
153    };
154    let target = sections[target];
155    if current == Some(target) {
156        return Vec::new();
157    }
158    current
159        .map(Event::Close)
160        .into_iter()
161        .chain([Event::Open(target)])
162        .collect()
163}
164
165impl keys::Action for Action {
166    const ALL: &'static [Self] = &[
167        Action::Next,
168        Action::Previous,
169        Action::ExpandAll,
170        Action::CollapseAll,
171    ];
172
173    fn defaults() -> Keymap<Self> {
174        default_keymap()
175    }
176
177    fn name(self) -> &'static str {
178        match self {
179            Action::Next => "Next",
180            Action::Previous => "Previous",
181            Action::ExpandAll => "ExpandAll",
182            Action::CollapseAll => "CollapseAll",
183        }
184    }
185
186    fn description(self) -> &'static str {
187        match self {
188            Action::Next => {
189                "Opens the next section in place of the current one, wrapping at the end."
190            }
191            Action::Previous => {
192                "Opens the previous section in place of the current one, wrapping at the start."
193            }
194            Action::ExpandAll => "Opens every section. Only applies when several may be open.",
195            Action::CollapseAll => "Closes every open section.",
196        }
197    }
198}
199
200/// The default accordion shortcuts:
201///
202/// | Keys | Action |
203/// | --- | --- |
204/// | `ArrowDown` | [`Action::Next`] |
205/// | `ArrowUp` | [`Action::Previous`] |
206/// | `Ctrl+Shift+ArrowDown` | [`Action::ExpandAll`] |
207/// | `Ctrl+Shift+ArrowUp` | [`Action::CollapseAll`] |
208///
209/// Shortcuts are app-wide, so an app with scrolling content or other
210/// arrow-driven components may want to unbind the plain arrows.
211pub fn default_keymap() -> Keymap<Action> {
212    Keymap::new()
213        .bind(Chord::named(Named::ArrowDown), Action::Next)
214        .bind(Chord::named(Named::ArrowUp), Action::Previous)
215        .bind(
216            Chord::named(Named::ArrowDown).ctrl().shift(),
217            Action::ExpandAll,
218        )
219        .bind(
220            Chord::named(Named::ArrowUp).ctrl().shift(),
221            Action::CollapseAll,
222        )
223}
224
225/// An accordion builder. Convert it into an [`Element`] to render.
226pub struct Accordion<'a, Id, Message> {
227    state: &'a State<Id>,
228    items: Vec<(Id, text::Fragment<'a>, Element<'a, Message>)>,
229    on_event: Option<Box<dyn Fn(Event<Id>) -> Message + 'a>>,
230}
231
232/// Renders sections whose open state comes from `state`. Without
233/// [`on_event`](Accordion::on_event) the headers render disabled.
234pub fn accordion<Id, Message>(state: &State<Id>) -> Accordion<'_, Id, Message> {
235    Accordion {
236        state,
237        items: Vec::new(),
238        on_event: None,
239    }
240}
241
242impl<Id: std::fmt::Debug, Message> std::fmt::Debug for Accordion<'_, Id, Message> {
243    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
244        f.debug_struct("Accordion")
245            .field("state", self.state)
246            .field("items", &self.items.len())
247            .finish_non_exhaustive()
248    }
249}
250
251impl<'a, Id, Message> Accordion<'a, Id, Message> {
252    /// Adds a section with a header and the content shown while open.
253    /// Content inherits muted text unless it supplies its own colour.
254    pub fn item(
255        mut self,
256        id: Id,
257        title: impl text::IntoFragment<'a>,
258        content: impl Into<Element<'a, Message>>,
259    ) -> Self {
260        self.items.push((id, title.into_fragment(), content.into()));
261        self
262    }
263
264    pub fn on_event(mut self, on_event: impl Fn(Event<Id>) -> Message + 'a) -> Self {
265        self.on_event = Some(Box::new(on_event));
266        self
267    }
268}
269
270impl<'a, Id, Message> From<Accordion<'a, Id, Message>> for Element<'a, Message>
271where
272    Id: Copy + PartialEq + 'a,
273    Message: Clone + 'a,
274{
275    fn from(accordion: Accordion<'a, Id, Message>) -> Self {
276        let Accordion {
277            state,
278            items,
279            on_event,
280        } = accordion;
281
282        column(items.into_iter().map(|(id, title, content)| {
283            let open = state.is_open(id);
284            let message = on_event
285                .as_ref()
286                .map(|on_event| on_event(Event::Toggle(id)));
287            section(title, content, open, message)
288        }))
289        .width(Length::Fill)
290        .into()
291    }
292}
293
294fn section<'a, Message: Clone + 'a>(
295    title: text::Fragment<'a>,
296    content: Element<'a, Message>,
297    open: bool,
298    message: Option<Message>,
299) -> Element<'a, Message> {
300    let enabled = message.is_some();
301    #[cfg(feature = "icedwright")]
302    let (name, key) = (
303        title.clone(),
304        icedwright_probe::Key::from(format!("iced_cube::accordion/{title}")),
305    );
306    let chevron = if open {
307        crate::lucide!(ChevronUp)
308    } else {
309        crate::lucide!(ChevronDown)
310    };
311
312    let header = widget::button(
313        row![
314            text(title)
315                .font(crate::theme::semibold())
316                .size(text_size::SM)
317                .width(Length::Fill),
318            themed(chevron, 16.0, opacity(enabled), |theme| {
319                Tokens::of(theme).muted_foreground
320            }),
321        ]
322        .spacing(space::MD)
323        .align_y(Alignment::Center),
324    )
325    .width(Length::Fill)
326    .padding([space::MD, 0.0])
327    .on_press_maybe(message)
328    .style(|theme, status| style(&Tokens::of(theme), status));
329    #[cfg(feature = "icedwright")]
330    let header = {
331        let mut annotated = icedwright_probe::semantic(header)
332            .role(icedwright_probe::role::Button)
333            .name(name.clone())
334            .expanded(open)
335            .controls(key.clone())
336            .component("iced_cube::accordion");
337        if !enabled {
338            annotated = annotated.disabled(true);
339        }
340        annotated
341    };
342
343    let mut body = column![header].width(Length::Fill);
344    if open {
345        let panel = container(content)
346            .padding(iced::Padding::ZERO.bottom(space::LG).right(space::XL))
347            .width(Length::Fill)
348            .style(|theme: &Theme| container::Style {
349                text_color: Some(Tokens::of(theme).muted_foreground),
350                ..container::Style::default()
351            });
352        #[cfg(feature = "icedwright")]
353        let panel = icedwright_probe::semantic(panel)
354            .role(icedwright_probe::role::Region)
355            .name(name)
356            .key(key)
357            .component("iced_cube::accordion");
358        body = body.push(panel);
359    }
360    body.push(rule::horizontal(1).style(|theme: &Theme| {
361        let tokens = Tokens::of(theme);
362        rule::Style {
363            color: mix(tokens.background, tokens.border, 0.7),
364            radius: 0.0.into(),
365            fill_mode: rule::FillMode::Full,
366            snap: true,
367        }
368    }))
369    .into()
370}
371
372/// The style of a section header.
373pub fn style(tokens: &Tokens, status: Status) -> widget::button::Style {
374    let text_color = match status {
375        Status::Active => tokens.foreground,
376        Status::Hovered | Status::Pressed => tokens.muted_foreground,
377        Status::Disabled => fade(tokens.foreground, 0.5),
378    };
379    widget::button::Style {
380        background: None,
381        text_color,
382        ..widget::button::Style::default()
383    }
384}
385
386#[cfg(test)]
387mod tests {
388    use super::*;
389    use crate::theme::{dark, light};
390
391    #[test]
392    fn starts_closed_in_single_mode_by_default() {
393        let state: State<u8> = State::default();
394        assert_eq!(state.mode(), Mode::Single);
395        assert!(state.open().is_empty());
396    }
397
398    fn apply(state: &mut State<u8>, action: Action, sections: &[u8]) {
399        for event in action.events(state, sections) {
400            let _ = state.update(event);
401        }
402    }
403
404    #[test]
405    fn next_and_previous_move_the_open_section_and_wrap() {
406        let sections = [1, 2, 3];
407        let mut state = State::new(Mode::Single);
408        apply(&mut state, Action::Next, &sections);
409        assert_eq!(state.open(), [1]);
410        apply(&mut state, Action::Next, &sections);
411        assert_eq!(state.open(), [2]);
412        apply(&mut state, Action::Previous, &sections);
413        apply(&mut state, Action::Previous, &sections);
414        assert_eq!(state.open(), [3]);
415
416        let mut closed = State::new(Mode::Single);
417        apply(&mut closed, Action::Previous, &sections);
418        assert_eq!(closed.open(), [3]);
419    }
420
421    #[test]
422    fn next_in_multiple_mode_moves_only_the_latest_section() {
423        let mut state = State::new(Mode::Multiple).with_open(1).with_open(2);
424        apply(&mut state, Action::Next, &[1, 2, 3]);
425        assert_eq!(state.open(), [1, 3]);
426    }
427
428    #[test]
429    fn next_with_one_or_no_sections() {
430        let state = State::new(Mode::Single).with_open(1);
431        assert!(Action::Next.events(&state, &[1]).is_empty());
432        assert!(Action::Next.events(&state, &[]).is_empty());
433    }
434
435    #[test]
436    fn expand_and_collapse_all() {
437        let sections = [1, 2, 3];
438        let mut state = State::new(Mode::Multiple).with_open(2);
439        assert_eq!(
440            Action::ExpandAll.events(&state, &sections),
441            [Event::Open(1), Event::Open(3)]
442        );
443        apply(&mut state, Action::ExpandAll, &sections);
444        assert_eq!(state.open(), [2, 1, 3]);
445        apply(&mut state, Action::CollapseAll, &sections);
446        assert!(state.open().is_empty());
447
448        let single = State::new(Mode::Single).with_open(1);
449        assert!(Action::ExpandAll.events(&single, &sections).is_empty());
450        assert_eq!(
451            Action::CollapseAll.events(&single, &sections),
452            [Event::Close(1)]
453        );
454    }
455
456    #[test]
457    fn default_keymap_and_overrides() {
458        let keymap = default_keymap();
459        let resolve = |keymap: &Keymap<Action>, chord: &str| {
460            let chord: Chord = chord.parse().unwrap();
461            keymap.resolve(chord.key(), chord.modifiers())
462        };
463        assert_eq!(resolve(&keymap, "ArrowDown"), Some(Action::Next));
464        assert_eq!(resolve(&keymap, "ArrowUp"), Some(Action::Previous));
465        assert_eq!(
466            resolve(&keymap, "Ctrl+Shift+ArrowDown"),
467            Some(Action::ExpandAll)
468        );
469        assert_eq!(
470            resolve(&keymap, "Ctrl+Shift+ArrowUp"),
471            Some(Action::CollapseAll)
472        );
473        let custom = keymap
474            .unbind_action(&Action::Next)
475            .bind("N".parse().unwrap(), Action::Next);
476        assert_eq!(resolve(&custom, "ArrowDown"), None);
477        assert_eq!(resolve(&custom, "n"), Some(Action::Next));
478    }
479
480    #[test]
481    fn single_mode_keeps_one_section_open() {
482        let mut state = State::new(Mode::Single);
483        assert_eq!(state.update(Event::Toggle(1)), Some(Output::Opened(1)));
484        assert_eq!(state.update(Event::Toggle(2)), Some(Output::Opened(2)));
485        assert_eq!(state.open(), [2]);
486        assert_eq!(state.update(Event::Toggle(2)), Some(Output::Closed(2)));
487        assert!(state.open().is_empty());
488    }
489
490    #[test]
491    fn multiple_mode_opens_sections_independently() {
492        let mut state = State::new(Mode::Multiple);
493        let _ = state.update(Event::Toggle(1));
494        let _ = state.update(Event::Toggle(2));
495        assert!(state.is_open(1) && state.is_open(2));
496        let _ = state.update(Event::Toggle(1));
497        assert_eq!(state.open(), [2]);
498    }
499
500    #[test]
501    fn open_and_close_are_idempotent() {
502        let mut state = State::new(Mode::Multiple).with_open(1);
503        assert_eq!(state.update(Event::Open(1)), None);
504        assert_eq!(state.update(Event::Close(3)), None);
505        assert_eq!(state.update(Event::Close(1)), Some(Output::Closed(1)));
506        assert_eq!(state.update(Event::Close(1)), None);
507    }
508
509    #[test]
510    fn switching_to_single_keeps_the_first_open_section() {
511        let mut state = State::new(Mode::Multiple).with_open(3).with_open(1);
512        state.set_mode(Mode::Single);
513        assert_eq!(state.open(), [3]);
514        let _ = state.update(Event::Open(1));
515        assert_eq!(state.open(), [1]);
516    }
517
518    #[test]
519    fn header_style_in_every_status_and_theme() {
520        for theme in [light(), dark()] {
521            let tokens = Tokens::of(&theme);
522            assert_eq!(style(&tokens, Status::Active).text_color, tokens.foreground);
523            assert_eq!(
524                style(&tokens, Status::Hovered).text_color,
525                tokens.muted_foreground
526            );
527            assert_eq!(
528                style(&tokens, Status::Pressed).text_color,
529                tokens.muted_foreground
530            );
531            assert!(style(&tokens, Status::Disabled).text_color.a < 1.0);
532            assert!(style(&tokens, Status::Active).background.is_none());
533        }
534    }
535}