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
//! Editor focus management: programmatic focus/blur and the canvas focus
//! lock used to suppress input during focus transitions.

use crate::canvas_editor::{
    CodeEditor, focused_editor_id, set_focused_editor_id,
};

impl CodeEditor {
    /// Requests focus for this editor.
    ///
    /// This method programmatically sets the focus to this editor instance,
    /// allowing it to receive keyboard events. Other editors will automatically
    /// lose focus.
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor1 = CodeEditor::new("fn main() {}", "rs");
    /// let mut editor2 = CodeEditor::new("fn test() {}", "rs");
    ///
    /// // Give focus to editor2
    /// editor2.request_focus();
    /// ```
    pub fn request_focus(&self) {
        set_focused_editor_id(self.editor_id);
    }

    /// Checks if this editor currently has focus.
    ///
    /// Returns `true` if this editor will receive keyboard events,
    /// `false` otherwise.
    ///
    /// # Returns
    ///
    /// `true` if focused, `false` otherwise
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let editor = CodeEditor::new("fn main() {}", "rs");
    /// if editor.is_focused() {
    ///     println!("Editor has focus");
    /// }
    /// ```
    pub fn is_focused(&self) -> bool {
        focused_editor_id() == self.editor_id
    }

    /// Removes canvas focus from this editor.
    ///
    /// This method programmatically removes focus from the canvas, preventing
    /// it from receiving keyboard events. The cursor will be hidden, but the
    /// selection will remain visible.
    ///
    /// Call this when focus should move to another widget (e.g., text input).
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.lose_focus();
    /// ```
    pub fn lose_focus(&mut self) {
        if self.is_focused() {
            set_focused_editor_id(0);
        }
        self.has_canvas_focus = false;
        self.show_cursor = false;
        self.ime_preedit = None;
    }

    /// Resets the focus lock state.
    ///
    /// This method can be called to manually unlock focus processing
    /// after a focus transition has completed. This is useful when
    /// you want to allow the editor to process input again after
    /// programmatic focus changes.
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.reset_focus_lock();
    /// ```
    pub fn reset_focus_lock(&mut self) {
        self.focus_locked = false;
    }

    /// Gives this editor full keyboard focus, for tests only.
    ///
    /// Sets the process-global focused editor ID, the canvas focus flag and
    /// clears the focus lock, so `has_focus()` returns `true` and
    /// focus-gated messages (`CharacterInput`, `Backspace`, `Delete`, ...)
    /// are processed.
    #[cfg(test)]
    pub(crate) fn focus_for_test(&mut self) {
        self.request_focus();
        self.has_canvas_focus = true;
        self.focus_locked = false;
    }
}

#[cfg(test)]
mod tests {
    use crate::canvas_editor::{
        CodeEditor, focused_editor_id, set_focused_editor_id,
    };

    #[test]
    fn test_set_focused_editor_id_round_trips() {
        set_focused_editor_id(42);
        assert_eq!(focused_editor_id(), 42);
        set_focused_editor_id(0);
        assert_eq!(focused_editor_id(), 0);
    }

    #[test]
    fn test_focus_is_isolated_between_test_threads() {
        let editor = CodeEditor::new("", "rs");
        editor.request_focus();

        let other_thread_sees = std::thread::spawn(|| {
            let thief = CodeEditor::new("", "rs");
            thief.request_focus();
            focused_editor_id()
        })
        .join()
        .unwrap_or_default();

        assert_ne!(other_thread_sees, 0);
        assert!(editor.is_focused());
    }

    #[test]
    fn test_focus_for_test_grants_focus() {
        let mut editor = CodeEditor::new("", "rs");
        editor.focus_locked = true;

        editor.focus_for_test();

        assert!(editor.has_canvas_focus);
        assert!(!editor.focus_locked);
    }
}