iced-code-editor 0.3.11

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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Right-click context menu for editor actions.

use iced::widget::{Space, button, column, container, row, text};
use iced::{Background, Border, Color, Element, Length, Shadow, Theme, Vector};

use super::Message;
use crate::i18n::Translations;

const MENU_WIDTH: f32 = 224.0;

/// An actionable entry in the editor context menu.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContextMenuItem {
    /// Stable action identifier emitted when the item is selected.
    pub id: String,
    /// Text displayed for the item.
    pub label: String,
    /// Optional keyboard shortcut hint displayed beside the label.
    pub shortcut: Option<String>,
    /// Whether the item can be selected.
    pub enabled: bool,
}

impl ContextMenuItem {
    /// Creates an enabled context-menu item without a shortcut hint.
    pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            shortcut: None,
            enabled: true,
        }
    }

    /// Sets the keyboard shortcut hint displayed beside this item.
    #[must_use]
    pub fn with_shortcut(mut self, shortcut: impl Into<String>) -> Self {
        self.shortcut = Some(shortcut.into());
        self
    }

    /// Sets whether this item can be selected.
    #[must_use]
    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }
}

/// A custom editor context-menu entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContextMenuEntry {
    /// An actionable menu item.
    Item(ContextMenuItem),
    /// A visual separator between groups of items.
    Separator,
}

impl ContextMenuEntry {
    /// Creates an enabled action entry without a shortcut hint.
    pub fn item(id: impl Into<String>, label: impl Into<String>) -> Self {
        Self::Item(ContextMenuItem::new(id, label))
    }

    /// Creates a separator entry.
    pub const fn separator() -> Self {
        Self::Separator
    }

    /// Sets the keyboard shortcut hint when this is an action entry.
    #[must_use]
    pub fn with_shortcut(self, shortcut: impl Into<String>) -> Self {
        match self {
            Self::Item(item) => Self::Item(item.with_shortcut(shortcut)),
            Self::Separator => Self::Separator,
        }
    }

    /// Sets whether this entry can be selected when it is an action.
    #[must_use]
    pub fn with_enabled(self, enabled: bool) -> Self {
        match self {
            Self::Item(item) => Self::Item(item.with_enabled(enabled)),
            Self::Separator => Self::Separator,
        }
    }
}

impl From<ContextMenuItem> for ContextMenuEntry {
    fn from(item: ContextMenuItem) -> Self {
        Self::Item(item)
    }
}

#[cfg(target_os = "macos")]
const UNDO_SHORTCUT: &str = "⌘Z";
#[cfg(not(target_os = "macos"))]
const UNDO_SHORTCUT: &str = "Ctrl+Z";

#[cfg(target_os = "macos")]
const REDO_SHORTCUT: &str = "⇧⌘Z";
#[cfg(not(target_os = "macos"))]
const REDO_SHORTCUT: &str = "Ctrl+Y";

#[cfg(target_os = "macos")]
const CUT_SHORTCUT: &str = "⌘X";
#[cfg(not(target_os = "macos"))]
const CUT_SHORTCUT: &str = "Ctrl+X";

#[cfg(target_os = "macos")]
const COPY_SHORTCUT: &str = "⌘C";
#[cfg(not(target_os = "macos"))]
const COPY_SHORTCUT: &str = "Ctrl+C";

#[cfg(target_os = "macos")]
const PASTE_SHORTCUT: &str = "⌘V";
#[cfg(not(target_os = "macos"))]
const PASTE_SHORTCUT: &str = "Ctrl+V";

#[cfg(target_os = "macos")]
const SELECT_ALL_SHORTCUT: &str = "⌘A";
#[cfg(not(target_os = "macos"))]
const SELECT_ALL_SHORTCUT: &str = "Ctrl+A";

#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct MenuState {
    pub(crate) can_undo: bool,
    pub(crate) can_redo: bool,
    pub(crate) has_selection: bool,
    pub(crate) has_content: bool,
    pub(crate) reveal_in_file_manager_enabled: bool,
}

#[derive(Debug, Clone)]
enum MenuEntry {
    Item { label: String, shortcut: String, message: Option<Message> },
    Separator,
}

impl MenuEntry {
    #[cfg(test)]
    fn label(&self) -> Option<&str> {
        match self {
            Self::Item { label, .. } => Some(label),
            Self::Separator => None,
        }
    }
}

fn custom_entries(entries: &[ContextMenuEntry]) -> Vec<MenuEntry> {
    entries
        .iter()
        .map(|entry| match entry {
            ContextMenuEntry::Item(item) => MenuEntry::Item {
                label: item.label.clone(),
                shortcut: item.shortcut.clone().unwrap_or_default(),
                message: item
                    .enabled
                    .then(|| Message::CustomContextMenuAction(item.id.clone())),
            },
            ContextMenuEntry::Separator => MenuEntry::Separator,
        })
        .collect()
}

fn default_entries(
    state: MenuState,
    translations: &Translations,
) -> Vec<MenuEntry> {
    let mut entries = if state.reveal_in_file_manager_enabled {
        vec![
            MenuEntry::Item {
                label: translations.context_menu_reveal_in_file_manager(),
                shortcut: String::new(),
                message: Some(Message::RevealInFileManager),
            },
            MenuEntry::Separator,
        ]
    } else {
        Vec::new()
    };
    entries.extend([
        MenuEntry::Item {
            label: translations.context_menu_undo(),
            shortcut: UNDO_SHORTCUT.to_string(),
            message: state.can_undo.then_some(Message::Undo),
        },
        MenuEntry::Item {
            label: translations.context_menu_redo(),
            shortcut: REDO_SHORTCUT.to_string(),
            message: state.can_redo.then_some(Message::Redo),
        },
        MenuEntry::Separator,
        MenuEntry::Item {
            label: translations.context_menu_cut(),
            shortcut: CUT_SHORTCUT.to_string(),
            message: state.has_selection.then_some(Message::Cut),
        },
        MenuEntry::Item {
            label: translations.context_menu_copy(),
            shortcut: COPY_SHORTCUT.to_string(),
            message: state.has_selection.then_some(Message::Copy),
        },
        MenuEntry::Item {
            label: translations.context_menu_paste(),
            shortcut: PASTE_SHORTCUT.to_string(),
            message: Some(Message::Paste(String::new())),
        },
        MenuEntry::Separator,
        MenuEntry::Item {
            label: translations.context_menu_select_all(),
            shortcut: SELECT_ALL_SHORTCUT.to_string(),
            message: state.has_content.then_some(Message::SelectAll),
        },
    ]);
    entries
}

fn build_entries(
    custom: &[ContextMenuEntry],
    default_context_menu_enabled: bool,
    state: MenuState,
    translations: &Translations,
) -> Vec<MenuEntry> {
    let mut entries = custom_entries(custom);
    if default_context_menu_enabled {
        if !entries.is_empty() {
            entries.push(MenuEntry::Separator);
        }
        entries.extend(default_entries(state, translations));
    }
    entries
}

/// Builds the context-menu contents.
pub(crate) fn view(
    custom: &[ContextMenuEntry],
    default_context_menu_enabled: bool,
    state: MenuState,
    translations: Translations,
) -> Element<'static, Message> {
    let items = build_entries(
        custom,
        default_context_menu_enabled,
        state,
        &translations,
    )
    .into_iter()
    .map(|entry| match entry {
        MenuEntry::Item { label, shortcut, message } => {
            menu_item(label, shortcut, message)
        }
        MenuEntry::Separator => separator(),
    })
    .collect::<Vec<_>>();

    container(column(items).spacing(1).padding(4))
        .width(Length::Fixed(MENU_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: 6.0.into(),
                },
                shadow: Shadow {
                    color: Color::BLACK.scale_alpha(0.35),
                    offset: Vector::new(0.0, 4.0),
                    blur_radius: 14.0,
                },
                ..container::Style::default()
            }
        })
        .into()
}

fn menu_item(
    label: String,
    shortcut: String,
    message: Option<Message>,
) -> Element<'static, Message> {
    let enabled = message.is_some();
    let content = row![
        text(label).size(13),
        Space::new().width(Length::Fill),
        text(shortcut).size(12),
    ]
    .align_y(iced::Alignment::Center);

    button(content)
        .width(Length::Fill)
        .padding([6, 9])
        .on_press_maybe(message)
        .style(move |theme: &Theme, status| {
            let palette = theme.extended_palette();
            let text_color = if enabled {
                palette.background.weak.text
            } else {
                palette.background.weak.text.scale_alpha(0.35)
            };
            let background = matches!(
                status,
                button::Status::Hovered | button::Status::Pressed
            )
            .then_some(Background::Color(palette.background.strong.color));

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

fn separator() -> Element<'static, Message> {
    let line =
        container(Space::new().width(Length::Fill).height(Length::Fixed(1.0)))
            .style(|theme: &Theme| {
                let palette = theme.extended_palette();
                container::Style {
                    background: Some(Background::Color(
                        palette.background.strong.color,
                    )),
                    ..container::Style::default()
                }
            });

    container(line).padding([3, 7]).into()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ContextMenuEntry, ContextMenuItem, Language, Translations};

    #[test]
    fn test_custom_context_menu_action_message_preserves_id() {
        let entries = build_entries(
            &[ContextMenuEntry::Item(ContextMenuItem::new(
                "refactor.extract",
                "Extract function",
            ))],
            false,
            MenuState::default(),
            &Translations::default(),
        );

        assert!(matches!(
            &entries[0],
            MenuEntry::Item {
                message: Some(Message::CustomContextMenuAction(id)),
                ..
            }
                if id == "refactor.extract"
        ));
    }

    #[test]
    fn test_custom_entries_precede_default_entries() {
        let entries = build_entries(
            &[ContextMenuEntry::item("custom.format", "Format document")],
            true,
            MenuState::default(),
            &Translations::default(),
        );

        assert_eq!(entries[0].label(), Some("Format document"));
        assert!(matches!(entries[1], MenuEntry::Separator));
        assert_eq!(entries[2].label(), Some("Undo"));
    }

    #[test]
    fn test_context_menu_uses_selected_language() {
        let translations = Translations::new(Language::ChineseSimplified);
        let entries = default_entries(MenuState::default(), &translations);

        assert_eq!(entries[0].label(), Some("撤消"));
        assert_eq!(entries[1].label(), Some("恢复"));
        assert_eq!(entries[3].label(), Some("剪切"));
        assert_eq!(entries[4].label(), Some("复制"));
        assert_eq!(entries[5].label(), Some("粘贴"));
        assert_eq!(entries[7].label(), Some("选择全部"));

        let custom = custom_entries(&[ContextMenuEntry::item(
            "custom.format",
            "Format document",
        )]);
        assert_eq!(custom[0].label(), Some("Format document"));
    }

    #[test]
    fn test_reveal_in_file_manager_entry_emits_request() {
        let translations = Translations::new(Language::English);
        let entries = build_entries(
            &[],
            true,
            MenuState {
                reveal_in_file_manager_enabled: true,
                ..MenuState::default()
            },
            &translations,
        );

        assert!(matches!(
            &entries[0],
            MenuEntry::Item { label, shortcut, message }
                if label == &translations.context_menu_reveal_in_file_manager()
                    && shortcut.is_empty()
                    && matches!(message, Some(Message::RevealInFileManager))
        ));
        assert!(matches!(entries[1], MenuEntry::Separator));
        assert_eq!(entries[2].label(), Some("Undo"));
    }

    #[test]
    fn test_reveal_in_file_manager_respects_default_menu_toggle() {
        let entries = build_entries(
            &[],
            false,
            MenuState {
                reveal_in_file_manager_enabled: true,
                ..MenuState::default()
            },
            &Translations::default(),
        );

        assert!(entries.is_empty());
    }
}