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 std::sync::atomic::Ordering;
5
6use crate::canvas_editor::{CodeEditor, FOCUSED_EDITOR_ID};
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        FOCUSED_EDITOR_ID.store(self.editor_id, Ordering::Relaxed);
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.load(Ordering::Relaxed) == 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            FOCUSED_EDITOR_ID.store(0, Ordering::Relaxed);
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::CodeEditor;
114
115    #[test]
116    fn test_focus_for_test_grants_focus() {
117        let mut editor = CodeEditor::new("", "rs");
118        editor.focus_locked = true;
119
120        editor.focus_for_test();
121
122        assert!(editor.has_canvas_focus);
123        assert!(!editor.focus_locked);
124    }
125}