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
//! Input method (IME) plumbing between winit and a window's [`Tree`], the
//! same on Windows and macOS: winit's `Ime` events become [`ImeEvent`]s, and
//! the window lets an input method compose only while a text input that takes
//! composition has focus, with its candidate window at that input's caret.
use winit::{
dpi::{LogicalPosition, LogicalSize},
event::Ime,
window::Window,
};
use crate::app::{
Tree,
events::{ImeCursorArea, ImeEvent},
};
pub(super) fn to_ime_event(ime: Ime) -> ImeEvent {
match ime {
Ime::Enabled => ImeEvent::Enabled,
Ime::Preedit(text, cursor) => ImeEvent::Preedit { text, cursor },
Ime::Commit(text) => ImeEvent::Commit(text),
Ime::Disabled => ImeEvent::Disabled,
}
}
/// What the window was last told, so it is told only of changes.
#[derive(Default)]
pub(super) struct ImeSync {
target: Option<usize>,
area: Option<ImeCursorArea>,
}
impl ImeSync {
pub(super) fn apply(&mut self, window: &Window, tree: &Tree) {
// On iOS the shell's own view takes the keyboard, with the input's traits; see `ios::TextInput`. winit's
// view would bring up a keyboard that knows nothing of the input.
#[cfg(target_os = "ios")]
let target = None;
#[cfg(not(target_os = "ios"))]
let target = tree.ime_target();
if target != self.target {
// Turning the input method off ends a composition left in the input
// that lost focus (macOS drops its marked text, Windows detaches the
// input context); it is turned on again for the newly focused input.
if self.target.is_some() {
window.set_ime_allowed(false);
}
if target.is_some() {
window.set_ime_allowed(true);
}
self.target = target;
self.area = None;
}
if target.is_none() {
return;
}
let area = tree.ime_cursor_area();
if area == self.area {
return;
}
if let Some(area) = area {
window.set_ime_cursor_area(
LogicalPosition::new(f64::from(area.x), f64::from(area.y)),
LogicalSize::new(f64::from(area.width), f64::from(area.height)),
);
}
self.area = area;
}
}