Skip to main content

iced_code_editor/canvas_editor/
focus.rs

1//! Editor focus management: programmatic focus/blur and the canvas focus
2//! lock used to suppress input during focus transitions.
3
4use crate::canvas_editor::{
5    CodeEditor, focused_editor_id, set_focused_editor_id,
6};
7
8impl CodeEditor {
9    /// Requests focus for this editor.
10    ///
11    /// This method programmatically sets the focus to this editor instance,
12    /// allowing it to receive keyboard events. Other editors will automatically
13    /// lose focus.
14    ///
15    /// # Example
16    ///
17    /// ```
18    /// use iced_code_editor::CodeEditor;
19    ///
20    /// let mut editor1 = CodeEditor::new("fn main() {}", "rs");
21    /// let mut editor2 = CodeEditor::new("fn test() {}", "rs");
22    ///
23    /// // Give focus to editor2
24    /// editor2.request_focus();
25    /// ```
26    pub fn request_focus(&self) {
27        set_focused_editor_id(self.editor_id);
28    }
29
30    /// Checks if this editor currently has focus.
31    ///
32    /// Returns `true` if this editor will receive keyboard events,
33    /// `false` otherwise.
34    ///
35    /// # Returns
36    ///
37    /// `true` if focused, `false` otherwise
38    ///
39    /// # Example
40    ///
41    /// ```
42    /// use iced_code_editor::CodeEditor;
43    ///
44    /// let editor = CodeEditor::new("fn main() {}", "rs");
45    /// if editor.is_focused() {
46    ///     println!("Editor has focus");
47    /// }
48    /// ```
49    pub fn is_focused(&self) -> bool {
50        focused_editor_id() == self.editor_id
51    }
52
53    /// Removes canvas focus from this editor.
54    ///
55    /// This method programmatically removes focus from the canvas, preventing
56    /// it from receiving keyboard events. The cursor will be hidden, but the
57    /// selection will remain visible.
58    ///
59    /// Call this when focus should move to another widget (e.g., text input).
60    ///
61    /// # Example
62    ///
63    /// ```
64    /// use iced_code_editor::CodeEditor;
65    ///
66    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
67    /// editor.lose_focus();
68    /// ```
69    pub fn lose_focus(&mut self) {
70        if self.is_focused() {
71            set_focused_editor_id(0);
72        }
73        self.has_canvas_focus = false;
74        self.show_cursor = false;
75        self.ime_preedit = None;
76    }
77
78    /// Resets the focus lock state.
79    ///
80    /// This method can be called to manually unlock focus processing
81    /// after a focus transition has completed. This is useful when
82    /// you want to allow the editor to process input again after
83    /// programmatic focus changes.
84    ///
85    /// # Example
86    ///
87    /// ```
88    /// use iced_code_editor::CodeEditor;
89    ///
90    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
91    /// editor.reset_focus_lock();
92    /// ```
93    pub fn reset_focus_lock(&mut self) {
94        self.focus_locked = false;
95    }
96
97    /// Gives this editor full keyboard focus, for tests only.
98    ///
99    /// Sets the process-global focused editor ID, the canvas focus flag and
100    /// clears the focus lock, so `has_focus()` returns `true` and
101    /// focus-gated messages (`CharacterInput`, `Backspace`, `Delete`, ...)
102    /// are processed.
103    #[cfg(test)]
104    pub(crate) fn focus_for_test(&mut self) {
105        self.request_focus();
106        self.has_canvas_focus = true;
107        self.focus_locked = false;
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use crate::canvas_editor::{
114        CodeEditor, focused_editor_id, set_focused_editor_id,
115    };
116
117    #[test]
118    fn test_set_focused_editor_id_round_trips() {
119        set_focused_editor_id(42);
120        assert_eq!(focused_editor_id(), 42);
121        set_focused_editor_id(0);
122        assert_eq!(focused_editor_id(), 0);
123    }
124
125    #[test]
126    fn test_focus_is_isolated_between_test_threads() {
127        let editor = CodeEditor::new("", "rs");
128        editor.request_focus();
129
130        let other_thread_sees = std::thread::spawn(|| {
131            let thief = CodeEditor::new("", "rs");
132            thief.request_focus();
133            focused_editor_id()
134        })
135        .join()
136        .unwrap_or_default();
137
138        assert_ne!(other_thread_sees, 0);
139        assert!(editor.is_focused());
140    }
141
142    #[test]
143    fn test_focus_for_test_grants_focus() {
144        let mut editor = CodeEditor::new("", "rs");
145        editor.focus_locked = true;
146
147        editor.focus_for_test();
148
149        assert!(editor.has_canvas_focus);
150        assert!(!editor.focus_locked);
151    }
152}