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
//! Input Method Editor (IME) composition tracking for East Asian CJK input.
/// Lifecycle states of an active IME text composition session.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ImeState {
#[default]
Inactive,
/// Actively composing pre-edit candidate text (e.g. pinyin or hiragana).
Composing {
preedit_text: String,
cursor_offset: usize,
selection_range: Option<(usize, usize)>,
},
/// Text was finalized and committed to the document buffer.
Committed(String),
}
/// Tracks inline IME pre-edit composition states and cursor positioning.
#[derive(Debug, Clone, Default)]
pub struct ImeComposition {
state: ImeState,
}
impl ImeComposition {
pub fn new() -> Self {
Self {
state: ImeState::Inactive,
}
}
/// Returns the current IME composition state.
pub fn state(&self) -> &ImeState {
&self.state
}
/// Returns whether the user is actively composing an IME candidate string.
pub fn is_composing(&self) -> bool {
matches!(self.state, ImeState::Composing { .. })
}
/// Starts an IME composition session.
pub fn start(&mut self) {
self.state = ImeState::Composing {
preedit_text: String::new(),
cursor_offset: 0,
selection_range: None,
};
}
/// Updates the active pre-edit text and caret position during candidate composition.
pub fn update(&mut self, text: &str, cursor_offset: usize) {
self.state = ImeState::Composing {
preedit_text: text.to_string(),
cursor_offset,
selection_range: None,
};
}
/// Commits the composed string and resets IME state to inactive.
pub fn commit(&mut self) -> Option<String> {
match std::mem::replace(&mut self.state, ImeState::Inactive) {
ImeState::Composing { preedit_text, .. } => {
if !preedit_text.is_empty() {
Some(preedit_text)
} else {
None
}
}
ImeState::Committed(s) => Some(s),
ImeState::Inactive => None,
}
}
/// Cancels active composition without committing text.
pub fn cancel(&mut self) {
self.state = ImeState::Inactive;
}
/// Accesses the active pre-edit string if composing.
pub fn preedit_text(&self) -> Option<&str> {
match &self.state {
ImeState::Composing { preedit_text, .. } => Some(preedit_text.as_str()),
_ => None,
}
}
}