minui 0.3.2

A minimalist Rust framework for TUIs and terminal games.
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
//! # Panel Widget
//!
//! A structured panel widget that displays content with header and body sections.
//! The panel provides a bordered container with optional header text and supports
//! various content types including plain text and rich TextBlock widgets.
//!
//! ## Features
//!
//! - **Dual-section layout**: Separate header and body areas with independent styling
//! - **Flexible content**: Supports plain text or advanced TextBlock content
//! - **Auto-sizing**: Automatically adjusts dimensions to fit content
//! - **Rich styling**: Customizable borders, colors, and alignment options
//! - **Div-based positioning**: Uses modern container-based layout system
//!
//! ## Visual Structure
//!
//! ```text
//! ┌─────────────────┐
//! │  Header Text    │  ← Header section (optional)
//! ├─────────────────┤
//! │  Body content   │  ← Body section
//! │  goes here...   │
//! │                 │
//! └─────────────────┘
//! ```
//!
//! ## Basic Usage
//!
//! ```rust
//! use minui::{Panel, Widget};
//!
//! // Create a simple panel with header and body
//! let panel = Panel::new(30, 8)
//!     .with_header("Settings")
//!     .with_body("Configure application options");
//!
//! // Add the panel to a container for layout
//! let container = Container::vertical()
//!     .add_child(panel);
//! ```
//!
//! ## Advanced Usage
//!
//! ```rust
//! use minui::{Panel, Color, ColorPair, BorderChars};
//!
//! let panel = Panel::auto_sized()
//!     .with_header("Important Notice")
//!     .with_body("This is a critical system message\nthat requires your attention.")
//!     .with_header_style(BorderChars::double_line())
//!     .with_body_style(BorderChars::single_line())
//!     .with_header_color(Some(ColorPair::new(Color::Yellow, Color::Red)))
//!     .with_body_color(Some(ColorPair::new(Color::White, Color::Blue)))
//!     .with_padding(2);
//! ```
//!
//! ## Auto-sizing
//!
//! ```rust
//! use minui::Panel;
//!
//! // Panel will automatically size itself to fit content
//! let panel = Panel::auto_sized() // Auto-sizes to content
//!     .with_auto_size(true)
//!     .with_header("Dynamic Size")
//!     .with_body("This panel adjusts its size\nautomatically based on content");
//! ```
//!
//! ## Rich Content Support
//!
//! ```rust
//! use minui::{Panel, TextBlock, Alignment, VerticalAlignment, TextWrapMode};
//!
//! // Create a TextBlock with advanced formatting
//! let text_block = TextBlock::new(25, 8, "Complex formatted content here...")
//!     .with_wrap_mode(TextWrapMode::WordWrap)
//!     .with_vertical_alignment(VerticalAlignment::Center);
//!
//! let panel = Panel::new(30, 10)
//!     .with_header("Advanced Content")
//!     .with_body_block(text_block);
//! ```
//!
//! The panel widget integrates seamlessly with MinUI's container-based layout system,
//! automatically positioning and sizing itself within parent containers.

use crate::input::scroll::Scroller;
use crate::widgets::common::WindowView;
use crate::widgets::text::{Alignment, TextBlock};
use crate::widgets::{BorderChars, Widget};
use crate::{Color, ColorPair, Result, Window};

/// A structured panel widget with header and body sections.
///
/// Panel provides a bordered container that displays content in two distinct sections:
/// a header area for titles or captions, and a body area for main content. Both sections
/// support independent styling including borders, colors, and content alignment.
///
/// The panel uses automatic sizing and div-based positioning, integrating seamlessly
/// with MinUI's container layout system.
///
/// # Examples
///
/// ```rust
/// use minui::{Panel, Color, BorderChars, Alignment};
///
/// // Simple informational panel
/// let info_panel = Panel::new(30, 8)
///     .with_header("System Status")
///     .with_body("All systems operational\nMemory: 45% used\nCPU: 12% used")
///     .with_alignment(Alignment::Left);
///
/// // Styled warning panel
/// let warning_panel = Panel::new(40, 10)
///     .with_header("⚠️ Warning")
///     .with_body("Low disk space detected")
///     .with_header_style(BorderChars::double_line())
///     .with_header_color(Some(Color::Yellow.into()))
///     .with_body_color(Some(Color::Red.into()))
///     .with_padding(2);
/// ```
pub struct Panel {
    width: u16,
    height: u16,
    header_text: String,
    body_content: PanelContent,
    header_style: BorderChars,
    body_style: BorderChars,
    header_color: Option<ColorPair>,
    body_color: Option<ColorPair>,
    header_border_color: Option<ColorPair>,
    body_border_color: Option<ColorPair>,
    padding: u16,
    alignment: Alignment, // For the body only; header will always be centered
    auto_size: bool,
    /// Scroll state manager for body content
    scroller: Scroller,
    /// Whether scrolling is enabled for the body
    scrollable: bool,
    /// Whether to show scroll indicators
    show_scroll_indicators: bool,
}

/// The content type for a panel's body section.
///
/// `PanelContent` represents the different types of content that can be displayed
/// in a panel's body area. This allows panels to be flexible containers that can
/// display both simple text and more complex formatted content.
///
/// # Variants
///
/// - **Text**: Plain text content with basic line-by-line rendering
/// - **Block**: Advanced content using a [`TextBlock`] widget with features like
///   word wrapping, vertical alignment, and rich formatting
pub(crate) enum PanelContent {
    /// Plain text content rendered line by line with basic formatting
    Text(String),
    /// Advanced content using a TextBlock widget with rich formatting capabilities
    Block(Box<TextBlock>),
}

impl Panel {
    /// Creates a new panel with the specified dimensions.
    ///
    /// Use 0 for width or height to enable auto-sizing based on content.
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            width,
            height,
            header_text: "".to_string(),
            body_content: PanelContent::Text(String::new()),
            header_style: BorderChars::single_line(),
            body_style: BorderChars::single_line(),
            header_color: None,
            body_color: None,
            header_border_color: None,
            body_border_color: None,
            padding: 1,
            alignment: Alignment::Left,
            auto_size: width == 0 || height == 0, // Auto sizes when dimensions are 0
            scroller: Scroller::new(),
            scrollable: false,
            show_scroll_indicators: false,
        }
    }

    /// Creates a new auto-sizing panel that adjusts to fit its content.
    pub fn auto_sized() -> Self {
        Self::new(0, 0)
    }

    /// Sets the panel's header text. Header text is always centered.
    pub fn with_header(mut self, text: impl Into<String>) -> Self {
        self.header_text = text.into();
        if self.auto_size {
            self.adjust_size();
        }
        self
    }

    /// Sets the panel's body content to plain text. Supports multi-line text.
    pub fn with_body(mut self, text: impl Into<String>) -> Self {
        self.body_content = PanelContent::Text(text.into());
        if self.auto_size {
            self.adjust_size();
        }
        self
    }

    /// Sets the panel's body content to a TextBlock widget for advanced formatting.
    pub fn with_body_block(mut self, text_block: TextBlock) -> Self {
        self.body_content = PanelContent::Block(Box::new(text_block));
        if self.auto_size {
            self.adjust_size();
        }
        self
    }

    /// Sets the border style for the header section.
    pub fn with_header_style(mut self, style: BorderChars) -> Self {
        self.header_style = style;
        self
    }

    /// Sets the border style for the body section.
    pub fn with_body_style(mut self, style: BorderChars) -> Self {
        self.body_style = style;
        self
    }

    /// Sets the text color for the header.
    pub fn with_header_color(mut self, color: Option<ColorPair>) -> Self {
        self.header_color = color;
        self
    }

    /// Sets the text color for the body.
    pub fn with_body_color(mut self, color: Option<ColorPair>) -> Self {
        self.body_color = color;
        self
    }

    /// Sets the border color for the header section.
    pub fn with_header_border_color(mut self, color: Color) -> Self {
        self.header_border_color = Some(ColorPair::new(color, Color::Transparent));
        self
    }

    /// Sets the border color for the body section.
    pub fn with_body_border_color(mut self, color: Color) -> Self {
        self.body_border_color = Some(ColorPair::new(color, Color::Transparent));
        self
    }

    /// Sets the internal padding for the panel content.
    pub fn with_padding(mut self, padding: u16) -> Self {
        self.padding = padding;
        if self.auto_size {
            self.adjust_size();
        }
        self
    }

    /// Sets the text alignment for the body content. Header text is always centered.
    pub fn with_alignment(mut self, alignment: Alignment) -> Self {
        self.alignment = alignment;
        self
    }

    /// Updates the header text of an existing panel.
    pub fn set_header(&mut self, text: impl Into<String>) {
        self.header_text = text.into();
        if self.auto_size {
            self.adjust_size();
        }
    }

    /// Updates the body text of an existing panel.
    pub fn set_body(&mut self, text: impl Into<String>) {
        self.body_content = PanelContent::Text(text.into());
        if self.auto_size {
            self.adjust_size();
        }
        // Reset scroll when content changes
        self.scroller.scroll_to_top();
    }

    /// Updates the body content using a TextBlock.
    pub fn set_body_block(&mut self, text_block: TextBlock) {
        self.body_content = PanelContent::Block(Box::new(text_block));
        if self.auto_size {
            self.adjust_size();
        }
        // Reset scroll when content changes
        self.scroller.scroll_to_top();
    }

    /// Gets a mutable reference to the body TextBlock if it exists.
    /// Returns None if the body contains plain text instead of a TextBlock.
    pub fn body_block_mut(&mut self) -> Option<&mut TextBlock> {
        match &mut self.body_content {
            PanelContent::Block(block) => Some(block.as_mut()),
            PanelContent::Text(_) => None,
        }
    }

    /// Gets a reference to the body TextBlock if it exists.
    pub fn body_block(&self) -> Option<&TextBlock> {
        match &self.body_content {
            PanelContent::Block(block) => Some(block.as_ref()),
            PanelContent::Text(_) => None,
        }
    }

    /// Enables scrolling for the panel body.
    pub fn with_scrollable(mut self, scrollable: bool) -> Self {
        self.scrollable = scrollable;
        self
    }

    /// Enables or disables scroll indicators.
    pub fn with_scroll_indicators(mut self, show: bool) -> Self {
        self.show_scroll_indicators = show;
        self
    }

    /// Sets the scroll direction for the panel.
    ///
    /// # Arguments
    /// * `natural` - `true` for natural scrolling (default), `false` for inverted scrolling
    ///
    /// # Examples
    ///
    /// ```rust
    /// use minui::Panel;
    ///
    /// let panel = Panel::new(40, 20)
    ///     .with_scrollable(true)
    ///     .with_scroll_direction(false); // Inverted scrolling
    /// ```
    pub fn with_scroll_direction(mut self, natural: bool) -> Self {
        self.scroller.set_invert_scroll_vertical(!natural);
        self
    }

    /// Sets whether scroll direction is inverted for the panel.
    ///
    /// When enabled, scrolling direction is reversed (e.g., up becomes down).
    pub fn set_invert_scroll(&mut self, invert: bool) {
        self.scroller.set_invert_scroll_vertical(invert);
    }

    /// Returns whether scroll direction is inverted for the panel.
    pub fn is_scroll_inverted(&self) -> bool {
        self.scroller.is_scroll_vertical_inverted()
    }

    /// Scrolls the body content by a relative amount.
    /// Positive values scroll down, negative values scroll up.
    pub fn scroll_by(&mut self, delta: i16) {
        if !self.scrollable {
            return;
        }

        let max_offset = self.max_scroll_offset();
        self.scroller.scroll_by(delta, max_offset);
    }

    /// Scrolls to a specific line in the body content.
    pub fn scroll_to(&mut self, line: u16) {
        if !self.scrollable {
            return;
        }
        let max_offset = self.max_scroll_offset();
        self.scroller.scroll_to(line, max_offset);
    }

    /// Scrolls to the top of the body content.
    pub fn scroll_to_top(&mut self) {
        self.scroller.scroll_to_top();
    }

    /// Scrolls to the bottom of the body content.
    pub fn scroll_to_bottom(&mut self) {
        if self.scrollable {
            let max_offset = self.max_scroll_offset();
            self.scroller.scroll_to_bottom(max_offset);
        }
    }

    /// Returns the current scroll offset.
    pub fn scroll_offset(&self) -> u16 {
        self.scroller.offset()
    }

    /// Returns the maximum valid scroll offset based on content.
    pub fn max_scroll_offset(&self) -> u16 {
        let (_, inner_height) = self.get_inner_dimensions();
        let content_lines = self.get_content_line_count();
        content_lines.saturating_sub(inner_height)
    }

    /// Returns whether the panel can be scrolled.
    pub fn can_scroll(&self) -> bool {
        if !self.scrollable {
            return false;
        }
        let (_, inner_height) = self.get_inner_dimensions();
        self.get_content_line_count() > inner_height
    }

    /// Returns whether scrolling up is possible.
    pub fn can_scroll_up(&self) -> bool {
        self.scrollable && self.scroller.can_scroll_up()
    }

    /// Returns whether scrolling down is possible.
    pub fn can_scroll_down(&self) -> bool {
        self.scrollable && self.scroller.can_scroll_down(self.max_scroll_offset())
    }

    /// Handles a mouse scroll event and updates the scroll position.
    /// Returns true if the scroll position changed.
    pub fn handle_scroll_event(&mut self, delta: i8) -> bool {
        if !self.scrollable {
            return false;
        }
        let max_offset = self.max_scroll_offset();
        self.scroller.handle_scroll_event(delta, max_offset)
    }

    /// Handles a mouse drag event on the panel.
    /// If the drag is on or near the scrollbar, scrolls to that position.
    /// Returns true if the scroll position changed.
    ///
    /// # Arguments
    /// * `drag_x` - X coordinate of drag relative to panel's top-left
    /// * `drag_y` - Y coordinate of drag relative to panel's top-left
    pub fn handle_drag_event(&mut self, drag_x: u16, drag_y: u16) -> bool {
        if !self.scrollable || !self.can_scroll() {
            return false;
        }

        let scrollbar_x = self.width - 2;
        let (_, inner_height) = self.get_inner_dimensions();

        // Calculate content area start
        let content_start_y = if !self.header_text.is_empty() {
            3 + self.padding // header top + header text + separator + padding
        } else {
            1 + self.padding // body top border + padding
        };

        let max_scroll = self.max_scroll_offset();
        self.scroller.handle_drag_event(
            drag_x,
            drag_y,
            scrollbar_x,
            inner_height,
            content_start_y,
            max_scroll,
        )
    }

    /// Handles a mouse click event on the panel.
    /// If the click is on or near the scrollbar, scrolls to that position.
    /// Returns true if the scroll position changed.
    ///
    /// # Arguments
    /// * `click_x` - X coordinate of click relative to panel's top-left
    /// * `click_y` - Y coordinate of click relative to panel's top-left
    pub fn handle_click_event(&mut self, click_x: u16, click_y: u16) -> bool {
        if !self.scrollable || !self.can_scroll() {
            return false;
        }

        let scrollbar_x = self.width - 2;
        let (_, inner_height) = self.get_inner_dimensions();

        // Calculate content area start
        let content_start_y = if !self.header_text.is_empty() {
            3 + self.padding // header top + header text + separator + padding
        } else {
            1 + self.padding // body top border + padding
        };

        let max_scroll = self.max_scroll_offset();
        self.scroller.handle_scrollbar_event(
            click_x,
            click_y,
            scrollbar_x,
            inner_height,
            content_start_y,
            max_scroll,
        )
    }

    /// Returns the number of lines in the body content.
    fn get_content_line_count(&self) -> u16 {
        match &self.body_content {
            PanelContent::Text(text) => {
                if text.is_empty() {
                    0
                } else {
                    text.lines().count() as u16
                }
            }
            PanelContent::Block(block) => block.line_count() as u16,
        }
    }

    /// Enables or disables automatic sizing based on content.
    pub fn with_auto_size(mut self, auto_size: bool) -> Self {
        self.auto_size = auto_size;
        if auto_size {
            self.adjust_size();
        }
        self
    }

    /// Calculates and sets panel dimensions based on content size.
    fn adjust_size(&mut self) {
        if !self.auto_size {
            return;
        }

        let mut content_width = 0u16;
        let mut content_height = 0u16;

        // Calculate header dimensions
        if !self.header_text.is_empty() {
            content_width = content_width.max(self.header_text.len() as u16);
            content_height += 1; // Header takes 1 line
        }

        // Calculate body dimensions
        match &self.body_content {
            PanelContent::Text(text) => {
                if !text.is_empty() {
                    let lines: Vec<&str> = text.lines().collect();
                    let max_line_width =
                        lines.iter().map(|line| line.len()).max().unwrap_or(0) as u16;
                    content_width = content_width.max(max_line_width);
                    content_height += lines.len() as u16;
                }
            }
            PanelContent::Block(text_block) => {
                let (block_width, block_height) = text_block.get_size();
                content_width = content_width.max(block_width);
                content_height += block_height;
            }
        }

        // Add padding and borders
        self.width = content_width + (self.padding * 2) + 2; // 2 for left and right borders
        self.height = content_height + (self.padding * 2) + 2; // 2 for top and bottom borders

        // Add separator line if we have both header and body
        if !self.header_text.is_empty() && content_height > 1 {
            self.height += 1; // Separator line
        }
    }

    /// Returns the available content area dimensions after borders and padding.
    fn get_inner_dimensions(&self) -> (u16, u16) {
        let inner_width = self.width.saturating_sub(2 + (self.padding * 2));
        let mut inner_height = self.height.saturating_sub(2 + (self.padding * 2));

        // Subtract header space if present
        if !self.header_text.is_empty() {
            inner_height = inner_height.saturating_sub(2); // Header + separator
        }

        (inner_width, inner_height)
    }
}

impl Widget for Panel {
    /// Renders the panel starting at (0,0) within the window.
    fn draw(&self, window: &mut dyn Window) -> Result<()> {
        if self.width == 0 || self.height == 0 {
            return Ok(()); // Nothing to draw
        }

        let mut current_y = 0u16;

        // Draw header section if we have header text
        if !self.header_text.is_empty() {
            // Header top border
            self.draw_header_top_border(window, current_y)?;
            current_y += 1;

            // Header text (centered)
            self.draw_header_text(window, current_y)?;
            current_y += 1;

            // Header separator line
            self.draw_header_separator(window, current_y)?;
            current_y += 1;
        } else {
            // Just draw top border
            self.draw_body_top_border(window, current_y)?;
            current_y += 1;
        }

        // Draw body content area
        self.draw_body_content(window, current_y)?;

        // Draw bottom border
        self.draw_bottom_border(window, self.height - 1)?;

        Ok(())
    }

    fn get_size(&self) -> (u16, u16) {
        (self.width, self.height)
    }

    fn get_position(&self) -> (u16, u16) {
        (0, 0) // Position is managed by parent containers
    }
}

// Helper methods for drawing different sections
impl Panel {
    fn draw_header_top_border(&self, window: &mut dyn Window, y: u16) -> Result<()> {
        let color = self.header_border_color;
        self.draw_horizontal_line(window, y, &self.header_style, true, color)
    }

    fn draw_header_text(&self, window: &mut dyn Window, y: u16) -> Result<()> {
        // Draw left border
        if let Some(color) = self.header_border_color {
            window.write_str_colored(y, 0, &self.header_style.vertical.to_string(), color)?;
        } else {
            window.write_str(y, 0, &self.header_style.vertical.to_string())?;
        }

        // Draw centered header text
        let inner_width = self.width.saturating_sub(2);
        let text_len = self.header_text.len() as u16;
        let start_x = if text_len < inner_width {
            1 + (inner_width - text_len) / 2
        } else {
            1
        };

        if let Some(color) = self.header_color {
            window.write_str_colored(y, start_x, &self.header_text, color)?;
        } else {
            window.write_str(y, start_x, &self.header_text)?;
        }

        // Draw right border
        if let Some(color) = self.header_border_color {
            window.write_str_colored(
                y,
                self.width - 1,
                &self.header_style.vertical.to_string(),
                color,
            )?;
        } else {
            window.write_str(y, self.width - 1, &self.header_style.vertical.to_string())?;
        }

        Ok(())
    }

    fn draw_header_separator(&self, window: &mut dyn Window, y: u16) -> Result<()> {
        let color = self.header_border_color;

        // Left T-junction
        if let Some(color) = color {
            window.write_str_colored(y, 0, &self.header_style.intersect_left.to_string(), color)?;
        } else {
            window.write_str(y, 0, &self.header_style.intersect_left.to_string())?;
        }

        // Horizontal line
        for x in 1..self.width - 1 {
            if let Some(color) = color {
                window.write_str_colored(y, x, &self.header_style.horizontal.to_string(), color)?;
            } else {
                window.write_str(y, x, &self.header_style.horizontal.to_string())?;
            }
        }

        // Right T-junction
        if let Some(color) = color {
            window.write_str_colored(
                y,
                self.width - 1,
                &self.header_style.intersect_right.to_string(),
                color,
            )?;
        } else {
            window.write_str(
                y,
                self.width - 1,
                &self.header_style.intersect_right.to_string(),
            )?;
        }

        Ok(())
    }

    fn draw_body_top_border(&self, window: &mut dyn Window, y: u16) -> Result<()> {
        let color = self.body_border_color;
        self.draw_horizontal_line(window, y, &self.body_style, true, color)
    }

    fn draw_body_content(&self, window: &mut dyn Window, start_y: u16) -> Result<()> {
        let (inner_width, inner_height) = self.get_inner_dimensions();

        // Draw vertical borders for each content line
        for y_offset in 0..inner_height + (self.padding * 2) {
            let y = start_y + y_offset;

            // Left border
            if let Some(color) = self.body_border_color {
                window.write_str_colored(y, 0, &self.body_style.vertical.to_string(), color)?;
            } else {
                window.write_str(y, 0, &self.body_style.vertical.to_string())?;
            }

            // Right border
            if let Some(color) = self.body_border_color {
                window.write_str_colored(
                    y,
                    self.width - 1,
                    &self.body_style.vertical.to_string(),
                    color,
                )?;
            } else {
                window.write_str(y, self.width - 1, &self.body_style.vertical.to_string())?;
            }
        }

        // Draw scroll indicators if enabled and scrollable
        if self.show_scroll_indicators && self.scrollable {
            let indicator_color = ColorPair::new(Color::DarkGray, Color::Transparent);

            // Draw scrollbar on the right side
            if self.can_scroll() {
                let max_scroll = self.max_scroll_offset();
                if max_scroll > 0 {
                    let scrollbar_height = inner_height;
                    let scrollbar_x = self.width - 2;
                    let scrollbar_start_y = start_y + self.padding;

                    // Calculate thumb position and size
                    let content_lines = self.get_content_line_count();
                    let thumb_size = ((inner_height as f32 / content_lines as f32)
                        * scrollbar_height as f32)
                        .max(1.0) as u16;
                    let thumb_pos = ((self.scroller.offset() as f32 / max_scroll as f32)
                        * (scrollbar_height - thumb_size) as f32)
                        as u16;

                    // Draw scrollbar track and thumb
                    for i in 0..scrollbar_height {
                        let y = scrollbar_start_y + i;
                        if i >= thumb_pos && i < thumb_pos + thumb_size {
                            // Thumb
                            window.write_str_colored(y, scrollbar_x, "", indicator_color)?;
                        } else {
                            // Track
                            window.write_str_colored(
                                y,
                                scrollbar_x,
                                "",
                                ColorPair::new(Color::DarkGray, Color::Transparent),
                            )?;
                        }
                    }

                    // Draw scroll indicators at top and bottom of scrollbar
                    if self.can_scroll_up() {
                        window.write_str_colored(
                            scrollbar_start_y,
                            scrollbar_x,
                            "",
                            indicator_color,
                        )?;
                    }

                    if self.can_scroll_down() {
                        let scrollbar_end_y =
                            scrollbar_start_y + scrollbar_height.saturating_sub(1);
                        window.write_str_colored(
                            scrollbar_end_y,
                            scrollbar_x,
                            "",
                            indicator_color,
                        )?;
                    }
                }
            }
        }

        // Draw actual content with padding
        let content_start_x = 1 + self.padding;
        let content_start_y = start_y + self.padding;
        let content_width = inner_width;
        let content_height = inner_height;

        if content_width > 0 && content_height > 0 {
            let mut content_window = WindowView {
                window,
                x_offset: content_start_x,
                y_offset: content_start_y,
                width: content_width,
                height: content_height,
            };

            match &self.body_content {
                PanelContent::Text(text) => {
                    self.draw_text_content(&mut content_window, text)?;
                }
                PanelContent::Block(text_block) => {
                    text_block.draw(&mut content_window)?;
                }
            }
        }

        Ok(())
    }

    fn draw_text_content(&self, window: &mut dyn Window, text: &str) -> Result<()> {
        let lines: Vec<&str> = text.lines().collect();
        let (window_width, window_height) = window.get_size();

        // Apply scroll offset if scrolling is enabled
        let start_line = if self.scrollable {
            self.scroller.offset() as usize
        } else {
            0
        };

        let visible_lines = lines.iter().skip(start_line).take(window_height as usize);

        for (i, line) in visible_lines.enumerate() {
            let x_pos = match self.alignment {
                Alignment::Left => 0,
                Alignment::Center => {
                    let line_len = line.len() as u16;
                    if line_len < window_width {
                        (window_width - line_len) / 2
                    } else {
                        0
                    }
                }
                Alignment::Right => {
                    let line_len = line.len() as u16;
                    if line_len < window_width {
                        window_width - line_len
                    } else {
                        0
                    }
                }
            };

            if let Some(color) = self.body_color {
                window.write_str_colored(i as u16, x_pos, line, color)?;
            } else {
                window.write_str(i as u16, x_pos, line)?;
            }
        }

        Ok(())
    }

    fn draw_bottom_border(&self, window: &mut dyn Window, y: u16) -> Result<()> {
        let color = self.body_border_color;
        self.draw_horizontal_line(window, y, &self.body_style, false, color)
    }

    fn draw_horizontal_line(
        &self,
        window: &mut dyn Window,
        y: u16,
        style: &BorderChars,
        is_top: bool,
        color: Option<ColorPair>,
    ) -> Result<()> {
        // Left corner
        let left_char = if is_top {
            style.top_left
        } else {
            style.bottom_left
        };
        let right_char = if is_top {
            style.top_right
        } else {
            style.bottom_right
        };

        if let Some(color) = color {
            window.write_str_colored(y, 0, &left_char.to_string(), color)?;
        } else {
            window.write_str(y, 0, &left_char.to_string())?;
        }

        // Horizontal line
        for x in 1..self.width - 1 {
            if let Some(color) = color {
                window.write_str_colored(y, x, &style.horizontal.to_string(), color)?;
            } else {
                window.write_str(y, x, &style.horizontal.to_string())?;
            }
        }

        // Right corner
        if let Some(color) = color {
            window.write_str_colored(y, self.width - 1, &right_char.to_string(), color)?;
        } else {
            window.write_str(y, self.width - 1, &right_char.to_string())?;
        }

        Ok(())
    }
}