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
//! Iced UI view and rendering logic.
use iced::Size;
use iced::advanced::input_method;
use iced::widget::canvas::Canvas;
use iced::widget::{Column, Row, Scrollable, Space, container, scrollable};
use iced::{Background, Border, Color, Element, Length, Rectangle, Shadow};
use super::ime_requester::ImeRequester;
use super::search_dialog;
use super::wrapping::{self, WrappingCalculator};
use super::{CodeEditor, GUTTER_WIDTH, Message};
use std::rc::Rc;
impl CodeEditor {
/// Calculates visual lines and canvas height for the editor.
///
/// Returns a tuple of (visual_lines, canvas_height) where:
/// - visual_lines: The visual line mapping with wrapping applied
/// - canvas_height: The total height needed for the canvas
fn calculate_canvas_height(&self) -> (Rc<Vec<wrapping::VisualLine>>, f32) {
// Reuse memoized visual lines so view layout (canvas height + IME cursor rect)
// does not trigger repeated wrapping computation.
let visual_lines = self.visual_lines_cached(self.viewport_width);
let total_visual_lines = visual_lines.len();
let content_height = total_visual_lines as f32 * self.line_height;
// Use max of content height and viewport height to ensure the canvas
// always covers the visible area (prevents visual artifacts when
// content is shorter than viewport after reset/file change)
let canvas_height = content_height.max(self.viewport_height);
(visual_lines, canvas_height)
}
/// Creates the scrollable style function with custom colors.
///
/// Returns a style function that configures the scrollbar appearance.
fn create_scrollable_style(
&self,
) -> impl Fn(&iced::Theme, scrollable::Status) -> scrollable::Style {
let scrollbar_bg = self.style.scrollbar_background;
let scroller_color = self.style.scroller_color;
move |_theme, _status| scrollable::Style {
container: container::Style {
background: Some(Background::Color(Color::TRANSPARENT)),
..container::Style::default()
},
vertical_rail: scrollable::Rail {
background: Some(scrollbar_bg.into()),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
scroller: scrollable::Scroller {
background: scroller_color.into(),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
},
},
horizontal_rail: scrollable::Rail {
background: Some(scrollbar_bg.into()),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
scroller: scrollable::Scroller {
background: scroller_color.into(),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
},
},
gap: None,
auto_scroll: scrollable::AutoScroll {
background: Color::TRANSPARENT.into(),
border: Border::default(),
shadow: Shadow::default(),
icon: Color::TRANSPARENT,
},
}
}
/// Creates the canvas widget wrapped in a scrollable container.
///
/// # Arguments
///
/// * `canvas_height` - The total height of the canvas
///
/// # Returns
///
/// A configured scrollable widget containing the canvas
fn create_canvas_with_scrollable(
&self,
canvas_height: f32,
) -> Scrollable<'_, Message> {
let canvas = Canvas::new(self)
.width(Length::Fill)
.height(Length::Fixed(canvas_height));
Scrollable::new(canvas)
.id(self.scrollable_id.clone())
.width(Length::Fill)
.height(Length::Fill)
.on_scroll(Message::Scrolled)
.style(self.create_scrollable_style())
}
/// Creates the horizontal scrollbar element when wrap is disabled and content overflows.
///
/// # Arguments
///
/// * `max_content_width` - The total pixel width of the widest line
///
/// # Returns
///
/// `Some(element)` if a horizontal scrollbar is needed, `None` otherwise
fn create_horizontal_scrollbar(
&self,
max_content_width: f32,
) -> Option<Element<'_, Message>> {
if self.wrap_enabled || max_content_width <= self.viewport_width {
return None;
}
let scrollbar_bg = self.style.scrollbar_background;
let scroller_color = self.style.scroller_color;
let h_scrollable = Scrollable::new(
Space::new().width(Length::Fixed(max_content_width)).height(0.0),
)
.id(self.horizontal_scrollable_id.clone())
.width(Length::Fill)
.height(Length::Fixed(12.0))
.direction(scrollable::Direction::Horizontal(
scrollable::Scrollbar::new(),
))
.on_scroll(Message::HorizontalScrolled)
.style(move |_theme, _status| scrollable::Style {
container: container::Style {
background: Some(Background::Color(Color::TRANSPARENT)),
..container::Style::default()
},
vertical_rail: scrollable::Rail {
background: Some(scrollbar_bg.into()),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
scroller: scrollable::Scroller {
background: scroller_color.into(),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
},
},
horizontal_rail: scrollable::Rail {
background: Some(scrollbar_bg.into()),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
scroller: scrollable::Scroller {
background: scroller_color.into(),
border: Border {
radius: 4.0.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
},
},
gap: None,
auto_scroll: scrollable::AutoScroll {
background: Color::TRANSPARENT.into(),
border: Border::default(),
shadow: Shadow::default(),
icon: Color::TRANSPARENT,
},
});
Some(h_scrollable.into())
}
/// Creates the gutter background container if line numbers are enabled.
///
/// # Returns
///
/// Some(container) if line numbers are enabled, None otherwise
fn create_gutter_container(
&self,
) -> Option<container::Container<'_, Message>> {
if self.line_numbers_enabled {
let gutter_background = self.style.gutter_background;
Some(
container(
Space::new().width(Length::Fill).height(Length::Fill),
)
.width(Length::Fixed(GUTTER_WIDTH))
.height(Length::Fill)
.style(move |_| container::Style {
background: Some(Background::Color(gutter_background)),
..container::Style::default()
}),
)
} else {
None
}
}
/// Creates the code area background container.
///
/// # Returns
///
/// The code background container widget
fn create_code_background_container(
&self,
) -> container::Container<'_, Message> {
let background_color = self.style.background;
container(Space::new().width(Length::Fill).height(Length::Fill))
.width(Length::Fill)
.height(Length::Fill)
.style(move |_| container::Style {
background: Some(Background::Color(background_color)),
..container::Style::default()
})
}
/// Creates the background layer combining gutter and code backgrounds.
///
/// # Returns
///
/// A row containing the background elements
fn create_background_layer(&self) -> Row<'_, Message> {
let gutter_container = self.create_gutter_container();
let code_background_container = self.create_code_background_container();
if let Some(gutter) = gutter_container {
Row::new().push(gutter).push(code_background_container)
} else {
Row::new().push(code_background_container)
}
}
/// Calculates the IME cursor rectangle for the current cursor position.
///
/// # Arguments
///
/// * `visual_lines` - The visual line mapping
///
/// # Returns
///
/// A rectangle representing the cursor position for IME
fn calculate_ime_cursor_rect(
&self,
visual_lines: &[wrapping::VisualLine],
) -> Rectangle {
let ime_enabled = self.is_focused() && self.has_canvas_focus;
if !ime_enabled {
return Rectangle::new(
iced::Point::new(0.0, 0.0),
Size::new(0.0, 0.0),
);
}
if let Some(cursor_visual) = WrappingCalculator::logical_to_visual(
visual_lines,
self.cursors.primary_position().0,
self.cursors.primary_position().1,
) {
let vl = &visual_lines[cursor_visual];
let line_content = self.buffer.line(vl.logical_line);
let prefix_len =
self.cursors.primary_position().1.saturating_sub(vl.start_col);
let prefix_text: String = line_content
.chars()
.skip(vl.start_col)
.take(prefix_len)
.collect();
let cursor_x = self.gutter_width()
+ 5.0
+ super::measure_text_width(
&prefix_text,
self.full_char_width,
self.char_width,
)
- self.horizontal_scroll_offset;
// Calculate visual Y position relative to the viewport
// We subtract viewport_scroll because the content is scrolled up/down
// but the cursor position sent to IME must be relative to the visible area
let cursor_y = (cursor_visual as f32 * self.line_height)
- self.viewport_scroll;
Rectangle::new(
iced::Point::new(cursor_x, cursor_y + 2.0),
Size::new(2.0, self.line_height - 4.0),
)
} else {
Rectangle::new(iced::Point::new(0.0, 0.0), Size::new(0.0, 0.0))
}
}
/// Creates the IME (Input Method Editor) layer widget.
///
/// # Arguments
///
/// * `cursor_rect` - The rectangle representing the cursor position
///
/// # Returns
///
/// An element containing the IME requester widget
fn create_ime_layer(&self, cursor_rect: Rectangle) -> Element<'_, Message> {
let ime_enabled = self.is_focused() && self.has_canvas_focus;
let preedit =
self.ime_preedit.as_ref().map(|p| input_method::Preedit {
content: p.content.clone(),
selection: p.selection.clone(),
text_size: None,
});
let ime_layer = ImeRequester::new(ime_enabled, cursor_rect, preedit);
iced::Element::new(ime_layer)
}
/// Creates the view element with scrollable wrapper.
///
/// The backgrounds (editor and gutter) are handled by container styles
/// to ensure proper clipping when the pane is resized.
pub fn view(&self) -> Element<'_, Message> {
// Calculate canvas height and visual lines
let (visual_lines, canvas_height) = self.calculate_canvas_height();
// Create scrollable containing the canvas
let scrollable = self.create_canvas_with_scrollable(canvas_height);
// Create background layer with gutter and code backgrounds
let background_row = self.create_background_layer();
// Build editor stack: backgrounds + scrollable
let mut editor_stack =
iced::widget::Stack::new().push(background_row).push(scrollable);
// Add IME layer for input method support.
// The IME requester needs the cursor rect in viewport coordinates, which
// depends on the current logical↔visual mapping.
let cursor_rect = self.calculate_ime_cursor_rect(visual_lines.as_ref());
let ime_layer = self.create_ime_layer(cursor_rect);
editor_stack = editor_stack.push(ime_layer);
// Add search dialog overlay if open
if self.search_state.is_open {
let search_dialog =
search_dialog::view(&self.search_state, &self.translations);
// Position the dialog in top-right corner with 20px margin
let positioned_dialog = container(
Row::new()
.push(Space::new().width(Length::Fill))
.push(search_dialog),
)
.padding(20)
.width(Length::Fill)
.height(Length::Shrink);
editor_stack = editor_stack.push(positioned_dialog);
}
// Wrap the editor stack in a container with clip
let editor_container = container(editor_stack)
.width(Length::Fill)
.height(Length::Fill)
.clip(true);
// When wrap is disabled, add a horizontal scrollbar below the editor
let max_content_width = self.max_content_width();
if let Some(h_scrollbar) =
self.create_horizontal_scrollbar(max_content_width)
{
Column::new().push(editor_container).push(h_scrollbar).into()
} else {
editor_container.into()
}
}
}