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
//! 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);
}
}