iced-code-editor 0.6.0

A custom code editor widget for the Iced GUI framework with syntax highlighting, line numbers, and scrolling 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
//! Command palette UI: filter input plus the filtered command list.

use iced::mouse;
use iced::widget::canvas::{self, Canvas};
use iced::widget::{
    Space, Stack, button, column, container, row, scrollable, text, text_input,
};
use iced::{
    Background, Border, Color, Element, Event, Length, Rectangle, Renderer,
    Shadow, Theme, Vector, keyboard,
};

use super::{CommandPaletteState, PaletteEntry};
use crate::canvas_editor::Message;
use crate::canvas_editor::render::view::scrollable_rail;
use crate::i18n::Translations;

/// Height of a single command row, in pixels.
///
/// Fixed rather than content-driven because the geometry that keeps the
/// highlighted row visible is computed from it (see [`rows_to_pixels`] and
/// `CodeEditor::scroll_command_palette_to_selection`).
const ROW_HEIGHT: f32 = 26.0;

/// Vertical gap inserted between two consecutive rows.
const ROW_SPACING: f32 = 1.0;

/// Distance from the top of one row to the top of the next.
///
/// This — not [`ROW_HEIGHT`] — is the unit every scroll computation works in:
/// the rows are laid out in a `column` with [`ROW_SPACING`] between them, so a
/// list scrolled by `n * ROW_HEIGHT` would drift by one pixel per row.
const ROW_PITCH: f32 = ROW_HEIGHT + ROW_SPACING;

/// Number of command rows shown before the list starts scrolling.
pub(crate) const MAX_VISIBLE_ROWS: usize = 10;

/// Thickness of the line drawn under the recently-used block.
const SEPARATOR_HEIGHT: f32 = 1.0;

/// Distance the separator adds between the recent block and the row below it:
/// its own thickness plus the extra [`ROW_SPACING`] the column inserts around
/// it. Every scroll computation has to account for it, or the list would drift
/// by two pixels as soon as a command has been run.
const SEPARATOR_PITCH: f32 = SEPARATOR_HEIGHT + ROW_SPACING;

/// Converts a row count into the pixel distance it spans.
///
/// Used both for the list's fixed height and for the scroll offset, so the two
/// cannot disagree about how tall a row is.
///
/// # Arguments
///
/// * `rows` - Number of rows to measure
///
/// # Returns
///
/// The distance from the top of the first row to the top of row `rows`,
/// saturating on a row count no list could ever reach
pub(crate) fn rows_to_pixels(rows: usize) -> f32 {
    ROW_PITCH * f32::from(u16::try_from(rows).unwrap_or(u16::MAX))
}

/// Returns the number of rows the separator is drawn under, or `0` when the
/// list has no separator.
///
/// The recently-used commands are the leading rows of `entries`, flagged by
/// `promote_recent`. A list that is *only* recent commands gets no separator:
/// there is nothing under it to separate them from.
///
/// # Arguments
///
/// * `entries` - The rows about to be displayed, in display order
///
/// # Returns
///
/// The index of the first non-recent row, or `0` when no line should be drawn
pub(crate) fn separator_after(entries: &[PaletteEntry]) -> usize {
    let recent = entries.iter().take_while(|entry| entry.is_recent).count();
    if recent == entries.len() { 0 } else { recent }
}

/// Returns the vertical offset of the top of row `row`.
///
/// This is [`rows_to_pixels`] corrected for the separator: every row below it
/// sits [`SEPARATOR_PITCH`] lower than its index alone would suggest.
///
/// # Arguments
///
/// * `row` - Index of the row to locate
/// * `separator_after` - Result of [`separator_after`] for the same list
///
/// # Returns
///
/// The distance from the top of the list to the top of `row`
pub(crate) fn row_top(row: usize, separator_after: usize) -> f32 {
    let base = rows_to_pixels(row);
    if separator_after > 0 && row >= separator_after {
        base + SEPARATOR_PITCH
    } else {
        base
    }
}

/// Width of the palette, in pixels.
const PALETTE_WIDTH: f32 = 560.0;

/// Border radius shared by the palette frame and its scrollbar rail.
const BORDER_RADIUS: f32 = 6.0;

/// Transparent top layer that handles the keys the focused text input would
/// otherwise swallow: Escape (which would merely unfocus it) and the arrow
/// keys (which would move the caret inside the query instead of moving the
/// highlight through the results).
struct KeyListener;

impl canvas::Program<Message> for KeyListener {
    type State = ();

    fn update(
        &self,
        _state: &mut Self::State,
        event: &Event,
        _bounds: Rectangle,
        _cursor: mouse::Cursor,
    ) -> Option<canvas::Action<Message>> {
        let Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) = event
        else {
            return None;
        };

        let message = match key {
            keyboard::Key::Named(keyboard::key::Named::Escape) => {
                Message::CloseCommandPalette
            }
            keyboard::Key::Named(keyboard::key::Named::ArrowDown) => {
                Message::CommandPaletteNavigate(true)
            }
            keyboard::Key::Named(keyboard::key::Named::ArrowUp) => {
                Message::CommandPaletteNavigate(false)
            }
            _ => return None,
        };

        Some(canvas::Action::publish(message).and_capture())
    }

    fn draw(
        &self,
        _state: &Self::State,
        _renderer: &Renderer,
        _theme: &Theme,
        _bounds: Rectangle,
        _cursor: mouse::Cursor,
    ) -> Vec<canvas::Geometry> {
        Vec::new()
    }
}

/// Builds the command palette shown over the editor.
///
/// # Arguments
///
/// * `state` - Current palette state
/// * `entries` - The commands matching the current query, in display order
/// * `translations` - Translations for the palette's own UI text
pub(crate) fn view<'a>(
    state: &CommandPaletteState,
    entries: &[PaletteEntry],
    translations: &Translations,
) -> Element<'a, Message> {
    if !state.is_open {
        return Space::new().into();
    }

    let query_input =
        text_input(&translations.command_palette_placeholder(), &state.query)
            .id(state.input_id.clone())
            .on_input(Message::CommandPaletteChanged)
            .on_submit(Message::SubmitCommandPalette)
            .padding(8)
            .size(14)
            .width(Length::Fill);

    let results: Element<'a, Message> = if entries.is_empty() {
        container(text(translations.command_palette_no_results()).size(13))
            .padding([6, 10])
            .width(Length::Fill)
            .into()
    } else {
        let separator_row = separator_after(entries);
        let mut rows: Vec<Element<'a, Message>> =
            Vec::with_capacity(entries.len() + 1);
        for (index, entry) in entries.iter().enumerate() {
            if separator_row > 0 && index == separator_row {
                rows.push(recent_separator());
            }
            rows.push(command_row(
                entry,
                index,
                index == state.selected,
                translations,
            ));
        }

        // The last row of the window is not followed by a gap, so the window
        // is one spacing shorter than the pitch it is made of. Getting this
        // exactly right is what lets the scroll offset land on a row boundary.
        // The separator only counts once it is strictly inside the window —
        // a window ending exactly on it stops above the line.
        let visible_rows = entries.len().min(MAX_VISIBLE_ROWS);
        let separator_extra =
            if separator_row > 0 && visible_rows > separator_row {
                SEPARATOR_PITCH
            } else {
                0.0
            };
        let list_height =
            rows_to_pixels(visible_rows) - ROW_SPACING + separator_extra;

        scrollable(column(rows).spacing(ROW_SPACING))
            .id(state.scrollable_id.clone())
            .height(Length::Fixed(list_height))
            .width(Length::Fill)
            .style(|theme: &Theme, _status| {
                let palette = theme.extended_palette();
                scrollable::Style {
                    container: container::Style::default(),
                    vertical_rail: scrollable_rail(
                        palette.background.weak.color,
                        palette.background.strong.color,
                        BORDER_RADIUS,
                    ),
                    horizontal_rail: scrollable_rail(
                        palette.background.weak.color,
                        palette.background.strong.color,
                        BORDER_RADIUS,
                    ),
                    gap: None,
                    auto_scroll: scrollable::AutoScroll {
                        background: Color::TRANSPARENT.into(),
                        border: Border::default(),
                        shadow: Shadow::default(),
                        icon: Color::TRANSPARENT,
                    },
                }
            })
            .into()
    };

    let dialog =
        container(column![query_input, results].spacing(6).width(Length::Fill))
            .padding(8)
            .width(Length::Fixed(PALETTE_WIDTH))
            .style(|theme: &Theme| {
                let palette = theme.extended_palette();
                container::Style {
                    background: Some(Background::Color(
                        palette.background.weak.color,
                    )),
                    text_color: Some(palette.background.weak.text),
                    border: Border {
                        color: palette.background.strong.color,
                        width: 1.0,
                        radius: BORDER_RADIUS.into(),
                    },
                    shadow: Shadow {
                        color: Color::BLACK.scale_alpha(0.35),
                        offset: Vector::new(0.0, 4.0),
                        blur_radius: 14.0,
                    },
                    ..container::Style::default()
                }
            });

    Stack::new()
        .push(dialog)
        .push(Canvas::new(KeyListener).width(Length::Fill).height(Length::Fill))
        .into()
}

/// Builds the line closing the recently-used block.
fn recent_separator<'a>() -> Element<'a, Message> {
    container(Space::new())
        .width(Length::Fill)
        .height(Length::Fixed(SEPARATOR_HEIGHT))
        .style(|theme: &Theme| {
            let palette = theme.extended_palette();
            container::Style {
                background: Some(Background::Color(
                    palette.background.strong.color,
                )),
                ..container::Style::default()
            }
        })
        .into()
}

/// Builds one command row: the label on the left, then the toggle badge and
/// the shortcut hint on the right.
fn command_row<'a>(
    entry: &PaletteEntry,
    index: usize,
    is_selected: bool,
    translations: &Translations,
) -> Element<'a, Message> {
    // A toggle shows what it is switching *from*, so the user knows what
    // running it does without having to run it first.
    let status: Element<'a, Message> = match entry.status {
        Some(true) => {
            text(translations.command_palette_status_on()).size(12).into()
        }
        Some(false) => {
            text(translations.command_palette_status_off()).size(12).into()
        }
        None => Space::new().into(),
    };

    let content = row![
        text(entry.label.clone()).size(13),
        Space::new().width(Length::Fill),
        status,
        text(entry.shortcut.clone()).size(12),
    ]
    .spacing(10)
    .align_y(iced::Alignment::Center);

    button(content)
        .width(Length::Fill)
        .height(Length::Fixed(ROW_HEIGHT))
        .padding([4, 8])
        .on_press(Message::CommandPaletteSelected(index))
        .style(move |theme: &Theme, status| {
            let palette = theme.extended_palette();
            let hovered = matches!(
                status,
                button::Status::Hovered | button::Status::Pressed
            );
            let background = if is_selected {
                Some(Background::Color(palette.primary.weak.color))
            } else if hovered {
                Some(Background::Color(palette.background.strong.color))
            } else {
                None
            };
            let text_color = if is_selected {
                palette.primary.weak.text
            } else {
                palette.background.weak.text
            };

            button::Style {
                background,
                text_color,
                border: Border { radius: 4.0.into(), ..Border::default() },
                ..button::Style::default()
            }
        })
        .into()
}

#[cfg(test)]
mod tests {
    use std::cmp::Ordering;

    use super::super::PaletteAction;
    use super::*;
    use crate::canvas_editor::compare_floats;

    /// Builds a row that is recent or not, with no other distinguishing state.
    fn entry(is_recent: bool) -> PaletteEntry {
        PaletteEntry {
            label: String::new(),
            shortcut: String::new(),
            status: None,
            is_recent,
            action: PaletteAction::Custom(String::new()),
        }
    }

    /// Asserts two offsets are equal, since `float_cmp` is denied crate-wide.
    fn assert_same_offset(measured: f32, expected: f32) {
        assert_eq!(
            compare_floats(measured, expected),
            Ordering::Equal,
            "expected {expected}, measured {measured}"
        );
    }

    #[test]
    fn test_separator_sits_under_the_leading_recent_rows() {
        assert_eq!(separator_after(&[]), 0);
        assert_eq!(separator_after(&[entry(false), entry(false)]), 0);
        assert_eq!(
            separator_after(&[entry(true), entry(true), entry(false)]),
            2
        );
    }

    #[test]
    fn test_a_list_of_only_recent_rows_gets_no_separator() {
        assert_eq!(separator_after(&[entry(true), entry(true)]), 0);
    }

    #[test]
    fn test_row_top_shifts_every_row_below_the_separator() {
        // Without a separator the offset is the plain row pitch.
        assert_same_offset(row_top(3, 0), rows_to_pixels(3));

        // With one under row 2, the rows above it are unaffected...
        assert_same_offset(row_top(0, 2), rows_to_pixels(0));
        assert_same_offset(row_top(1, 2), rows_to_pixels(1));

        // ...and the first row under it, like every row after, moves down.
        assert_same_offset(row_top(2, 2), rows_to_pixels(2) + SEPARATOR_PITCH);
        assert_same_offset(row_top(5, 2), rows_to_pixels(5) + SEPARATOR_PITCH);
    }
}