envision 0.10.1

A ratatui framework for collaborative TUI development with headless testing support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! An accordion component with collapsible panels.
//!
//! [`Accordion`] provides a vertically stacked list of panels that can be
//! expanded or collapsed. Multiple panels can be open simultaneously,
//! and keyboard navigation is supported. State is stored in
//! [`AccordionState`], updated via [`AccordionMessage`], and produces
//! [`AccordionOutput`]. Panels are defined with [`AccordionPanel`].
//!
//! Implements [`Focusable`] and [`Disableable`].
//!
//! # Example
//!
//! ```rust
//! use envision::component::{Accordion, AccordionMessage, AccordionOutput, AccordionPanel, AccordionState, Component, Focusable};
//!
//! // Create panels
//! let panels = vec![
//!     AccordionPanel::new("Getting Started", "Welcome to the app..."),
//!     AccordionPanel::new("Configuration", "Set up your preferences..."),
//!     AccordionPanel::new("FAQ", "Frequently asked questions..."),
//! ];
//!
//! let mut state = AccordionState::new(panels);
//! Accordion::focus(&mut state);
//!
//! // Toggle first panel (expands it)
//! let output = Accordion::update(&mut state, AccordionMessage::Toggle);
//! assert_eq!(output, Some(AccordionOutput::Expanded(0)));
//! assert!(state.panels()[0].is_expanded());
//!
//! // Navigate to next panel and toggle
//! Accordion::update(&mut state, AccordionMessage::Down);
//! Accordion::update(&mut state, AccordionMessage::Toggle);
//! // Now panels 0 and 1 are both expanded
//! ```

use ratatui::prelude::*;
use ratatui::widgets::Paragraph;

use super::{Component, Disableable, Focusable, ViewContext};
use crate::input::{Event, KeyCode};
use crate::theme::Theme;

/// A single accordion panel with a title and content.
///
/// Panels can be created collapsed (default) or expanded using the builder method.
///
/// # Example
///
/// ```rust
/// use envision::component::AccordionPanel;
///
/// // Create a collapsed panel
/// let panel = AccordionPanel::new("Title", "Content here");
/// assert!(!panel.is_expanded());
///
/// // Create an expanded panel
/// let panel = AccordionPanel::new("Title", "Content").expanded();
/// assert!(panel.is_expanded());
/// ```
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct AccordionPanel {
    /// The panel header/title.
    title: String,
    /// The panel content.
    content: String,
    /// Whether this panel is expanded.
    expanded: bool,
}

impl AccordionPanel {
    /// Creates a new collapsed panel with the given title and content.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::AccordionPanel;
    ///
    /// let panel = AccordionPanel::new("Section 1", "Content for section 1");
    /// assert_eq!(panel.title(), "Section 1");
    /// assert_eq!(panel.content(), "Content for section 1");
    /// assert!(!panel.is_expanded());
    /// ```
    pub fn new(title: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            expanded: false,
        }
    }

    /// Sets the panel to be expanded (builder method).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::AccordionPanel;
    ///
    /// let panel = AccordionPanel::new("Title", "Content").expanded();
    /// assert!(panel.is_expanded());
    /// ```
    pub fn expanded(mut self) -> Self {
        self.expanded = true;
        self
    }

    /// Returns the panel title.
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Returns the panel content.
    pub fn content(&self) -> &str {
        &self.content
    }

    /// Returns whether the panel is expanded.
    pub fn is_expanded(&self) -> bool {
        self.expanded
    }
}

/// Messages that can be sent to an Accordion.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AccordionMessage {
    /// Move focus down to the next panel.
    Down,
    /// Move focus up to the previous panel.
    Up,
    /// Jump to the first panel.
    First,
    /// Jump to the last panel.
    Last,
    /// Toggle the currently focused panel.
    Toggle,
    /// Expand the currently focused panel.
    Expand,
    /// Collapse the currently focused panel.
    Collapse,
    /// Toggle a specific panel by index.
    ToggleIndex(usize),
    /// Expand all panels.
    ExpandAll,
    /// Collapse all panels.
    CollapseAll,
}

/// Output messages from an Accordion.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AccordionOutput {
    /// A panel was expanded (index).
    Expanded(usize),
    /// A panel was collapsed (index).
    Collapsed(usize),
    /// Focus moved to a panel (index).
    FocusChanged(usize),
}

/// State for an Accordion component.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct AccordionState {
    /// The accordion panels.
    panels: Vec<AccordionPanel>,
    /// Currently focused panel index.
    focused_index: usize,
    /// Whether the component is focused.
    focused: bool,
    /// Whether the component is disabled.
    disabled: bool,
}

impl AccordionState {
    /// Creates a new accordion with the given panels.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{AccordionPanel, AccordionState};
    ///
    /// let panels = vec![
    ///     AccordionPanel::new("Section 1", "Content 1"),
    ///     AccordionPanel::new("Section 2", "Content 2"),
    /// ];
    /// let state = AccordionState::new(panels);
    /// assert_eq!(state.len(), 2);
    /// assert_eq!(state.focused_index(), 0);
    /// ```
    pub fn new(panels: Vec<AccordionPanel>) -> Self {
        Self {
            panels,
            focused_index: 0,
            focused: false,
            disabled: false,
        }
    }

    /// Creates an accordion from title/content pairs.
    ///
    /// All panels start collapsed.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::AccordionState;
    ///
    /// let state = AccordionState::from_pairs(vec![
    ///     ("Section 1", "Content 1"),
    ///     ("Section 2", "Content 2"),
    /// ]);
    /// assert_eq!(state.len(), 2);
    /// ```
    pub fn from_pairs<S: Into<String>, T: Into<String>>(pairs: Vec<(S, T)>) -> Self {
        let panels = pairs
            .into_iter()
            .map(|(title, content)| AccordionPanel::new(title, content))
            .collect();
        Self::new(panels)
    }

    /// Returns the panels slice.
    ///
    /// # Examples
    ///
    /// ```
    /// use envision::prelude::*;
    ///
    /// let state = AccordionState::from_pairs(vec![("A", "1"), ("B", "2")]);
    /// assert_eq!(state.panels().len(), 2);
    /// assert_eq!(state.panels()[0].title(), "A");
    /// ```
    pub fn panels(&self) -> &[AccordionPanel] {
        &self.panels
    }

    /// Returns the number of panels.
    pub fn len(&self) -> usize {
        self.panels.len()
    }

    /// Returns true if there are no panels.
    pub fn is_empty(&self) -> bool {
        self.panels.is_empty()
    }

    /// Returns the currently focused panel index.
    pub fn focused_index(&self) -> usize {
        self.focused_index
    }

    /// Returns the currently focused panel.
    pub fn focused_panel(&self) -> Option<&AccordionPanel> {
        self.panels.get(self.focused_index)
    }

    /// Returns the currently focused panel index as an `Option`.
    ///
    /// This is a convenience alias for [`focused_index()`](Self::focused_index) that provides
    /// a consistent `Option<usize>` return type across all selection-based components.
    /// Returns `None` when the accordion has no panels.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{AccordionPanel, AccordionState};
    ///
    /// let panels = vec![
    ///     AccordionPanel::new("Section 1", "Content 1"),
    ///     AccordionPanel::new("Section 2", "Content 2"),
    /// ];
    /// let state = AccordionState::new(panels);
    /// assert_eq!(state.selected_index(), Some(0));
    /// ```
    pub fn selected_index(&self) -> Option<usize> {
        if self.panels.is_empty() {
            None
        } else {
            Some(self.focused_index)
        }
    }

    /// Returns the currently focused panel index as an `Option`.
    ///
    /// This is an alias for [`selected_index()`](Self::selected_index) that provides a
    /// consistent accessor name across all selection-based components.
    pub fn selected(&self) -> Option<usize> {
        self.selected_index()
    }

    /// Returns the currently focused panel.
    ///
    /// This is an alias for [`focused_panel()`](Self::focused_panel) that provides a
    /// consistent accessor name across all selection-based components.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{AccordionPanel, AccordionState};
    ///
    /// let panels = vec![
    ///     AccordionPanel::new("Section 1", "Content 1"),
    ///     AccordionPanel::new("Section 2", "Content 2"),
    /// ];
    /// let state = AccordionState::new(panels);
    /// let item = state.selected_item().unwrap();
    /// assert_eq!(item.title(), "Section 1");
    /// ```
    pub fn selected_item(&self) -> Option<&AccordionPanel> {
        self.focused_panel()
    }

    /// Returns whether the accordion is disabled.
    pub fn is_disabled(&self) -> bool {
        self.disabled
    }

    /// Sets new panels, resetting the focused index if needed.
    pub fn set_panels(&mut self, panels: Vec<AccordionPanel>) {
        self.panels = panels;
        if self.focused_index >= self.panels.len() && !self.panels.is_empty() {
            self.focused_index = 0;
        }
    }

    /// Adds a panel to the accordion.
    pub fn add_panel(&mut self, panel: AccordionPanel) {
        self.panels.push(panel);
    }

    /// Removes a panel by index.
    ///
    /// If the index is out of bounds, this is a no-op.
    /// Adjusts the focused index after removal so it remains valid.
    /// If the accordion becomes empty, the focused index is reset to 0.
    pub fn remove_panel(&mut self, index: usize) {
        if index >= self.panels.len() {
            return;
        }
        self.panels.remove(index);
        if self.panels.is_empty() {
            self.focused_index = 0;
        } else if self.focused_index >= self.panels.len() {
            self.focused_index = self.panels.len() - 1;
        }
    }

    /// Sets the disabled state.
    pub fn set_disabled(&mut self, disabled: bool) {
        self.disabled = disabled;
    }

    /// Sets the focused panel index (builder method).
    ///
    /// If the index is out of bounds, it will be clamped to the valid range.
    /// Has no effect on an empty accordion.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::AccordionState;
    ///
    /// let state = AccordionState::from_pairs(vec![("A", "1"), ("B", "2"), ("C", "3")])
    ///     .with_focused_index(2);
    /// assert_eq!(state.focused_index(), 2);
    /// ```
    pub fn with_focused_index(mut self, index: usize) -> Self {
        if !self.panels.is_empty() {
            self.focused_index = index.min(self.panels.len() - 1);
        }
        self
    }

    /// Sets the disabled state (builder method).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::AccordionState;
    ///
    /// let state = AccordionState::from_pairs(vec![("A", "1")])
    ///     .with_disabled(true);
    /// assert!(state.is_disabled());
    /// ```
    pub fn with_disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Returns the count of expanded panels.
    ///
    /// # Examples
    ///
    /// ```
    /// use envision::prelude::*;
    ///
    /// let panels = vec![
    ///     AccordionPanel::new("A", "1").expanded(),
    ///     AccordionPanel::new("B", "2"),
    /// ];
    /// let state = AccordionState::new(panels);
    /// assert_eq!(state.expanded_count(), 1);
    /// ```
    pub fn expanded_count(&self) -> usize {
        self.panels.iter().filter(|p| p.expanded).count()
    }

    /// Returns true if any panel is expanded.
    ///
    /// # Examples
    ///
    /// ```
    /// use envision::prelude::*;
    ///
    /// let panels = vec![
    ///     AccordionPanel::new("A", "1"),
    ///     AccordionPanel::new("B", "2").expanded(),
    /// ];
    /// let state = AccordionState::new(panels);
    /// assert!(state.is_any_expanded());
    /// ```
    pub fn is_any_expanded(&self) -> bool {
        self.panels.iter().any(|p| p.expanded)
    }

    /// Returns true if all panels are expanded.
    ///
    /// # Examples
    ///
    /// ```
    /// use envision::prelude::*;
    ///
    /// let panels = vec![
    ///     AccordionPanel::new("A", "1").expanded(),
    ///     AccordionPanel::new("B", "2").expanded(),
    /// ];
    /// let state = AccordionState::new(panels);
    /// assert!(state.is_all_expanded());
    /// ```
    pub fn is_all_expanded(&self) -> bool {
        !self.panels.is_empty() && self.panels.iter().all(|p| p.expanded)
    }

    /// Returns true if the accordion is focused.
    ///
    /// # Examples
    ///
    /// ```
    /// use envision::prelude::*;
    ///
    /// let state = AccordionState::from_pairs(vec![("A", "1")]);
    /// assert!(!state.is_focused());
    /// ```
    pub fn is_focused(&self) -> bool {
        self.focused
    }

    /// Sets the focus state.
    ///
    /// # Examples
    ///
    /// ```
    /// use envision::prelude::*;
    ///
    /// let mut state = AccordionState::from_pairs(vec![("A", "1")]);
    /// state.set_focused(true);
    /// assert!(state.is_focused());
    /// ```
    pub fn set_focused(&mut self, focused: bool) {
        self.focused = focused;
    }

    /// Maps an input event to an accordion message.
    pub fn handle_event(&self, event: &Event) -> Option<AccordionMessage> {
        Accordion::handle_event(self, event)
    }

    /// Dispatches an event, updating state and returning any output.
    pub fn dispatch_event(&mut self, event: &Event) -> Option<AccordionOutput> {
        Accordion::dispatch_event(self, event)
    }

    /// Updates the accordion state with a message, returning any output.
    pub fn update(&mut self, msg: AccordionMessage) -> Option<AccordionOutput> {
        Accordion::update(self, msg)
    }
}

/// An accordion component with collapsible panels.
///
/// The accordion displays a vertical list of panels. Each panel has a header
/// that can be clicked (or toggled via keyboard) to expand or collapse its
/// content. Multiple panels can be expanded simultaneously.
///
/// # Keyboard Navigation
///
/// The accordion itself doesn't handle keyboard events directly. Your application
/// should map:
/// - Down arrow to [`AccordionMessage::Down`]
/// - Up arrow to [`AccordionMessage::Up`]
/// - Enter/Space to [`AccordionMessage::Toggle`]
/// - Home to [`AccordionMessage::First`]
/// - End to [`AccordionMessage::Last`]
///
/// # Visual Layout
///
/// ```text
/// ▼ Section 1            ← Focused, expanded
///   Content for section 1...
///   More content here.
/// ▶ Section 2            ← Collapsed
/// ▼ Section 3            ← Expanded
///   Content for section 3...
/// ```
///
/// # Example
///
/// ```rust
/// use envision::component::{Accordion, AccordionMessage, AccordionPanel, AccordionState, Component};
///
/// let panels = vec![
///     AccordionPanel::new("FAQ", "Frequently asked questions..."),
///     AccordionPanel::new("Help", "How to get help..."),
/// ];
///
/// let mut state = AccordionState::new(panels);
///
/// // Toggle first panel
/// Accordion::update(&mut state, AccordionMessage::Toggle);
/// assert!(state.panels()[0].is_expanded());
///
/// // Navigate and toggle second
/// Accordion::update(&mut state, AccordionMessage::Down);
/// Accordion::update(&mut state, AccordionMessage::Toggle);
/// // Both panels are now expanded
/// assert_eq!(state.expanded_count(), 2);
/// ```
pub struct Accordion;

impl Component for Accordion {
    type State = AccordionState;
    type Message = AccordionMessage;
    type Output = AccordionOutput;

    fn init() -> Self::State {
        AccordionState::default()
    }

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        if state.disabled {
            return None;
        }

        match msg {
            AccordionMessage::Down => {
                if !state.panels.is_empty() {
                    state.focused_index = (state.focused_index + 1) % state.panels.len();
                    Some(AccordionOutput::FocusChanged(state.focused_index))
                } else {
                    None
                }
            }
            AccordionMessage::Up => {
                if !state.panels.is_empty() {
                    if state.focused_index == 0 {
                        state.focused_index = state.panels.len() - 1;
                    } else {
                        state.focused_index -= 1;
                    }
                    Some(AccordionOutput::FocusChanged(state.focused_index))
                } else {
                    None
                }
            }
            AccordionMessage::First => {
                if !state.panels.is_empty() && state.focused_index != 0 {
                    state.focused_index = 0;
                    Some(AccordionOutput::FocusChanged(0))
                } else {
                    None
                }
            }
            AccordionMessage::Last => {
                if !state.panels.is_empty() {
                    let last = state.panels.len() - 1;
                    if state.focused_index != last {
                        state.focused_index = last;
                        Some(AccordionOutput::FocusChanged(last))
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            AccordionMessage::Toggle => {
                if let Some(panel) = state.panels.get_mut(state.focused_index) {
                    panel.expanded = !panel.expanded;
                    if panel.expanded {
                        Some(AccordionOutput::Expanded(state.focused_index))
                    } else {
                        Some(AccordionOutput::Collapsed(state.focused_index))
                    }
                } else {
                    None
                }
            }
            AccordionMessage::Expand => {
                if let Some(panel) = state.panels.get_mut(state.focused_index) {
                    if !panel.expanded {
                        panel.expanded = true;
                        Some(AccordionOutput::Expanded(state.focused_index))
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            AccordionMessage::Collapse => {
                if let Some(panel) = state.panels.get_mut(state.focused_index) {
                    if panel.expanded {
                        panel.expanded = false;
                        Some(AccordionOutput::Collapsed(state.focused_index))
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            AccordionMessage::ToggleIndex(index) => {
                if let Some(panel) = state.panels.get_mut(index) {
                    panel.expanded = !panel.expanded;
                    if panel.expanded {
                        Some(AccordionOutput::Expanded(index))
                    } else {
                        Some(AccordionOutput::Collapsed(index))
                    }
                } else {
                    None
                }
            }
            AccordionMessage::ExpandAll => {
                let mut any_changed = false;
                for (i, panel) in state.panels.iter_mut().enumerate() {
                    if !panel.expanded {
                        panel.expanded = true;
                        any_changed = true;
                        // Return the first one that was expanded
                        if !any_changed {
                            return Some(AccordionOutput::Expanded(i));
                        }
                    }
                }
                if any_changed {
                    // Return a general expanded signal for the first panel
                    Some(AccordionOutput::Expanded(0))
                } else {
                    None
                }
            }
            AccordionMessage::CollapseAll => {
                let mut any_changed = false;
                for (i, panel) in state.panels.iter_mut().enumerate() {
                    if panel.expanded {
                        panel.expanded = false;
                        any_changed = true;
                        if !any_changed {
                            return Some(AccordionOutput::Collapsed(i));
                        }
                    }
                }
                if any_changed {
                    Some(AccordionOutput::Collapsed(0))
                } else {
                    None
                }
            }
        }
    }

    fn handle_event(state: &Self::State, event: &Event) -> Option<Self::Message> {
        if !state.focused || state.disabled {
            return None;
        }
        if let Some(key) = event.as_key() {
            match key.code {
                KeyCode::Up | KeyCode::Char('k') => Some(AccordionMessage::Up),
                KeyCode::Down | KeyCode::Char('j') => Some(AccordionMessage::Down),
                KeyCode::Enter | KeyCode::Char(' ') => Some(AccordionMessage::Toggle),
                KeyCode::Home => Some(AccordionMessage::First),
                KeyCode::End => Some(AccordionMessage::Last),
                _ => None,
            }
        } else {
            None
        }
    }

    fn view(state: &Self::State, frame: &mut Frame, area: Rect, theme: &Theme, ctx: &ViewContext) {
        if state.panels.is_empty() {
            return;
        }

        crate::annotation::with_registry(|reg| {
            reg.register(
                area,
                crate::annotation::Annotation::accordion("accordion")
                    .with_focus(ctx.focused)
                    .with_disabled(ctx.disabled),
            );
        });

        let mut y = area.y;

        for (i, panel) in state.panels.iter().enumerate() {
            if y >= area.bottom() {
                break;
            }

            // Header line
            let is_focused_panel = ctx.focused && i == state.focused_index;
            let icon = if panel.expanded { "" } else { "" };
            let header = format!("{} {}", icon, panel.title);

            let header_style = if ctx.disabled {
                theme.disabled_style()
            } else if is_focused_panel {
                theme.focused_bold_style()
            } else {
                theme.normal_style()
            };

            let header_area = Rect::new(area.x, y, area.width, 1);
            frame.render_widget(Paragraph::new(header).style(header_style), header_area);
            y += 1;

            // Content (if expanded)
            if panel.expanded && y < area.bottom() {
                let content_lines = panel.content.lines().count().max(1) as u16;
                let available_height = area.bottom().saturating_sub(y);
                let content_height = content_lines.min(available_height);

                if content_height > 0 {
                    let content_area =
                        Rect::new(area.x + 2, y, area.width.saturating_sub(2), content_height);
                    let content_style = if ctx.disabled {
                        theme.disabled_style()
                    } else {
                        theme.placeholder_style()
                    };
                    frame.render_widget(
                        Paragraph::new(panel.content.as_str()).style(content_style),
                        content_area,
                    );
                    y += content_height;
                }
            }
        }
    }
}

impl Focusable for Accordion {
    fn is_focused(state: &Self::State) -> bool {
        state.focused
    }

    fn set_focused(state: &mut Self::State, focused: bool) {
        state.focused = focused;
    }
}

impl Disableable for Accordion {
    fn is_disabled(state: &Self::State) -> bool {
        state.disabled
    }

    fn set_disabled(state: &mut Self::State, disabled: bool) {
        state.disabled = disabled;
    }
}

#[cfg(test)]
mod tests;