freya_code_editor/
editor_line.rs1use std::borrow::Cow;
2
3use freya_components::scrollviews::ScrollController;
4use freya_core::prelude::*;
5use freya_edit::{
6 EditableEvent,
7 EditorLine,
8 TextEditor,
9};
10use torin::size::Size;
11
12use crate::{
13 editor_data::CodeEditorData,
14 editor_theme::EditorTheme,
15 syntax::TextNode,
16};
17
18#[derive(Clone, PartialEq)]
19pub struct EditorLineUI {
20 pub(crate) editor: Writable<CodeEditorData>,
21 pub(crate) font_size: f32,
22 pub(crate) line_height: f32,
23 pub(crate) line_index: usize,
24 pub(crate) read_only: bool,
25 pub(crate) show_whitespace: bool,
26 pub(crate) font_family: Cow<'static, str>,
27 pub(crate) cursor_mode: CursorMode,
28 pub(crate) theme: EditorTheme,
29 pub(crate) a11y_id: AccessibilityId,
30 pub(crate) focus: Memo<Focus>,
31 pub(crate) scroll_controller: ScrollController,
32}
33
34impl Component for EditorLineUI {
35 fn render_key(&self) -> DiffKey {
36 DiffKey::from(&self.line_index)
37 }
38 fn render(&self) -> impl IntoElement {
39 let EditorLineUI {
40 mut editor,
41 font_size,
42 line_height,
43 line_index,
44 read_only,
45 show_whitespace,
46 font_family,
47 cursor_mode,
48 theme,
49 a11y_id,
50 focus,
51 scroll_controller,
52 } = self.clone();
53
54 let holder = use_state(ParagraphHolder::default);
55
56 let editor_data = editor.read_unchecked();
57
58 let longest_width = editor_data.metrics.longest_width;
59 let line = editor_data.metrics.syntax_blocks.get_line(line_index);
60 let highlights = editor_data.get_visible_selection(EditorLine::Paragraph(line_index));
61 let is_line_selected = editor_data.cursor_row() == line_index;
62
63 let on_tap = {
64 let mut editor = editor.clone();
65 let font_family = font_family.clone();
66 move |e: Event<FocusPressEventData>| {
67 let processed = editor.write_if(|mut editor| {
68 editor.process(
69 font_size,
70 &font_family,
71 EditableEvent::Down {
72 location: e.element_location(),
73 editor_line: EditorLine::Paragraph(line_index),
74 holder: &holder.read(),
75 },
76 )
77 });
78 if processed {
79 a11y_id.request_focus();
80 editor
81 .read()
82 .scroll_to_cursor(scroll_controller, line_height);
83 }
84 }
85 };
86
87 let on_pointer_move = {
88 let font_family = font_family.clone();
89 move |e: Event<PointerEventData>| {
90 let processed = editor.write_if(|mut editor| {
91 editor.process(
92 font_size,
93 &font_family,
94 EditableEvent::Move {
95 location: e.element_location(),
96 editor_line: EditorLine::Paragraph(line_index),
97 holder: &holder.read(),
98 },
99 )
100 });
101 if processed {
102 editor
103 .read()
104 .scroll_to_cursor(scroll_controller, line_height);
105 }
106 }
107 };
108
109 let focused = !read_only && is_line_selected && focus().is_focused();
110 let cursor_index = focused.then(|| editor_data.cursor_col());
111 let visible_selection = match editor_data.get_selection() {
112 None => false,
113 Some((s, e)) if s != e => true,
114 _ => false,
115 };
116 let line_background = if is_line_selected && !visible_selection {
117 theme.line_selected_background
118 } else {
119 Color::TRANSPARENT
120 };
121
122 rect()
123 .horizontal()
124 .height(Size::px(line_height))
125 .background(line_background)
126 .font_size(font_size)
127 .child(
128 paragraph()
129 .holder(holder.read().clone())
130 .on_pointer_move(on_pointer_move)
131 .on_focus_press(on_tap)
132 .cursor_color(theme.cursor)
133 .cursor_style(CursorStyle::Block)
134 .cursor_index(cursor_index)
135 .cursor_mode(cursor_mode)
136 .vertical_align(VerticalAlign::Center)
137 .highlights(highlights.map(|h| vec![h]))
138 .highlight_color(theme.highlight)
139 .width(Size::px(longest_width))
140 .min_width(Size::fill())
141 .height(Size::fill())
142 .font_family(font_family)
143 .max_lines(1)
144 .color(theme.text)
145 .spans_iter(line.iter().map(|span| {
146 let text: Cow<str> = match &span.1 {
147 TextNode::Range(word_pos) => {
148 editor_data.rope.slice(word_pos.clone()).into()
149 }
150 TextNode::LineOfChars { len, char } => {
151 if show_whitespace {
152 Cow::Owned(char.to_string().repeat(*len))
153 } else {
154 Cow::Owned(" ".repeat(*len))
155 }
156 }
157 };
158 Span::new(Cow::Owned(text.to_string())).color(span.0)
159 })),
160 )
161 }
162}
163
164#[derive(Clone, PartialEq)]
165pub(crate) struct EditorGutterLineUI {
166 pub(crate) editor: Writable<CodeEditorData>,
167 pub(crate) font_size: f32,
168 pub(crate) line_height: f32,
169 pub(crate) line_index: usize,
170 pub(crate) theme: EditorTheme,
171}
172
173impl Component for EditorGutterLineUI {
174 fn render_key(&self) -> DiffKey {
175 DiffKey::from(&self.line_index)
176 }
177
178 fn render(&self) -> impl IntoElement {
179 let EditorGutterLineUI {
180 editor,
181 font_size,
182 line_height,
183 line_index,
184 theme,
185 } = self.clone();
186 let editor_data = editor.read();
187 let is_line_selected = editor_data.cursor_row() == line_index;
188 let visible_selection = matches!(
189 editor_data.get_selection(),
190 Some((start, end)) if start != end
191 );
192 let line_background = if is_line_selected && !visible_selection {
193 theme.line_selected_background
194 } else {
195 Color::TRANSPARENT
196 };
197 let gutter_color = if is_line_selected {
198 theme.gutter_selected
199 } else {
200 theme.gutter_unselected
201 };
202
203 rect()
204 .height(Size::px(line_height))
205 .width(Size::fill())
206 .background(line_background)
207 .font_size(font_size)
208 .padding((0., 0., 0., 20.))
209 .main_align(torin::prelude::Alignment::Center)
210 .child(
211 label()
212 .color(gutter_color)
213 .text(format!("{} ", line_index + 1)),
214 )
215 }
216}