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
153
154
155
156
157
158
159
160
161
162
//! # Input Method Editor (IME) State Management
//!
//! This module provides IME state management for the Tessera UI framework.
//!
//! ## Overview
//!
//! Input Method Editor (IME) support is essential for handling complex text input,
//! particularly for languages that require composition (such as Chinese, Japanese,
//! Korean, and others). The IME system allows users to input characters through
//! a multi-step process where intermediate composition states are displayed before
//! the final text is committed.
//!
//! ## Design
//!
//! The [`ImeState`] struct maintains a bounded queue of IME events to ensure:
//! - **Memory efficiency**: Old events are automatically discarded to prevent unbounded growth
//! - **Event ordering**: Events are processed in the order they were received
//! - **Performance**: The queue size is limited to prevent excessive memory usage
//!
//! ## Usage
//!
//! ```rust,ignore
//! use crate::ImeState;
//! use winit::event::Ime;
//!
//! let mut ime_state = ImeState::default();
//!
//! // Push IME events as they arrive
//! ime_state.push_event(Ime::Preedit("hello".to_string(), None));
//! ime_state.push_event(Ime::Commit("world".to_string()));
//!
//! // Process all pending events
//! let events = ime_state.take_events();
//! for event in events {
//! match event {
//! Ime::Preedit(text, cursor) => {
//! // Handle composition text with optional cursor position
//! }
//! Ime::Commit(text) => {
//! // Handle committed text
//! }
//! Ime::Enabled => {
//! // IME was enabled
//! }
//! Ime::Disabled => {
//! // IME was disabled
//! }
//! }
//! }
//! ```
use VecDeque;
/// Maximum number of IME events to keep in the queue.
///
/// This constant limits the size of the event queue to prevent unbounded memory growth.
/// When the queue exceeds this size, the oldest events are automatically removed.
/// The value of 10 provides a reasonable balance between memory usage and ensuring
/// that recent events are not lost during high-frequency input scenarios.
pub const KEEP_EVENTS_COUNT: usize = 10;
/// Manages the state and event queue for Input Method Editor (IME) operations.
///
/// The `ImeState` struct provides a bounded queue for storing IME events from the
/// windowing system. It automatically manages memory by discarding old events when
/// the queue becomes too large, ensuring consistent performance even during
/// intensive text input sessions.
///
/// ## Thread Safety
///
/// This struct is not thread-safe by itself. If you need to share IME state across
/// threads, wrap it in appropriate synchronization primitives like `Arc<Mutex<ImeState>>`.
///
/// ## Memory Management
///
/// The internal queue automatically maintains a maximum size of [`KEEP_EVENTS_COUNT`]
/// events. When new events are added beyond this limit, the oldest events are
/// automatically removed to prevent unbounded memory growth.
pub