iced_code_editor/canvas_editor/
view.rs

1//! Iced UI view and rendering logic.
2
3use iced::widget::canvas::Canvas;
4use iced::widget::{Scrollable, container, scrollable};
5use iced::{Border, Color, Element, Length, Shadow};
6
7use super::{CodeEditor, LINE_HEIGHT, Message};
8
9impl CodeEditor {
10    /// Creates the view element with scrollable wrapper.
11    pub fn view(&self) -> Element<'_, Message> {
12        // Calculate total content height
13        let total_lines = self.buffer.line_count();
14        let content_height = total_lines as f32 * LINE_HEIGHT;
15
16        // Create canvas with fixed height based on content
17        let canvas = Canvas::new(self)
18            .width(Length::Fill)
19            .height(Length::Fixed(content_height));
20
21        // Capture style colors for the scrollbar style closure
22        let scrollbar_bg = self.style.scrollbar_background;
23        let scroller_color = self.style.scroller_color;
24
25        // Wrap in scrollable for automatic scrollbar display with custom style
26        Scrollable::new(canvas)
27            .id(self.scrollable_id.clone())
28            .width(Length::Fill)
29            .height(Length::Fill)
30            .on_scroll(Message::Scrolled)
31            .style(move |_theme, _status| scrollable::Style {
32                container: container::Style::default(),
33                vertical_rail: scrollable::Rail {
34                    background: Some(scrollbar_bg.into()),
35                    border: Border {
36                        radius: 4.0.into(),
37                        width: 0.0,
38                        color: Color::TRANSPARENT,
39                    },
40                    scroller: scrollable::Scroller {
41                        background: scroller_color.into(),
42                        border: Border {
43                            radius: 4.0.into(),
44                            width: 0.0,
45                            color: Color::TRANSPARENT,
46                        },
47                    },
48                },
49                horizontal_rail: scrollable::Rail {
50                    background: Some(scrollbar_bg.into()),
51                    border: Border {
52                        radius: 4.0.into(),
53                        width: 0.0,
54                        color: Color::TRANSPARENT,
55                    },
56                    scroller: scrollable::Scroller {
57                        background: scroller_color.into(),
58                        border: Border {
59                            radius: 4.0.into(),
60                            width: 0.0,
61                            color: Color::TRANSPARENT,
62                        },
63                    },
64                },
65                gap: None,
66                auto_scroll: scrollable::AutoScroll {
67                    background: Color::TRANSPARENT.into(),
68                    border: Border::default(),
69                    shadow: Shadow::default(),
70                    icon: Color::TRANSPARENT,
71                },
72            })
73            .into()
74    }
75}