uzor 1.0.20

Core UI engine — geometry, interaction, input state
Documentation
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Text input state - editing state management

use std::collections::HashMap;

// =============================================================================
// Trait for custom state implementations
// =============================================================================

/// State adapter for text input interaction (trait for custom backends)
pub trait TextInputStateTrait {
    fn is_focused(&self, input_id: &str) -> bool;
    fn cursor_position(&self, input_id: &str) -> usize;
    fn selection_range(&self, input_id: &str) -> Option<(usize, usize)>;
    fn set_focused(&mut self, input_id: &str, focused: bool);
    fn set_cursor_position(&mut self, input_id: &str, pos: usize);
    fn set_selection_range(&mut self, input_id: &str, range: Option<(usize, usize)>);
}

/// Simple HashMap-based implementation (prototyping only)
#[derive(Clone, Debug, Default)]
pub struct SimpleTextInputState {
    pub focus: HashMap<String, bool>,
    pub cursor: HashMap<String, usize>,
    pub selection: HashMap<String, Option<(usize, usize)>>,
}

impl SimpleTextInputState {
    pub fn new() -> Self {
        Self::default()
    }
}

impl TextInputStateTrait for SimpleTextInputState {
    fn is_focused(&self, input_id: &str) -> bool {
        self.focus.get(input_id).copied().unwrap_or(false)
    }

    fn cursor_position(&self, input_id: &str) -> usize {
        self.cursor.get(input_id).copied().unwrap_or(0)
    }

    fn selection_range(&self, input_id: &str) -> Option<(usize, usize)> {
        self.selection.get(input_id).copied().flatten()
    }

    fn set_focused(&mut self, input_id: &str, focused: bool) {
        if focused {
            for (id, is_focused) in self.focus.iter_mut() {
                if id != input_id {
                    *is_focused = false;
                }
            }
        }
        self.focus.insert(input_id.to_string(), focused);
    }

    fn set_cursor_position(&mut self, input_id: &str, pos: usize) {
        self.cursor.insert(input_id.to_string(), pos);
    }

    fn set_selection_range(&mut self, input_id: &str, range: Option<(usize, usize)>) {
        self.selection.insert(input_id.to_string(), range);
    }
}

// =============================================================================
// Concrete TextInputState (MOVED FROM APPLICATION)
// =============================================================================

/// State for an active text input field (standard implementation)
#[derive(Clone, Debug, Default)]
pub struct TextInputState {
    /// Is a text input currently active?
    pub is_active: bool,

    /// Field being edited (e.g., "price_input", "search_box")
    pub field_id: Option<String>,

    /// Current text content being edited
    pub text: String,

    /// Cursor position (character index, NOT byte index)
    pub cursor: usize,

    /// Selection start (if Some, text is selected from selection_start to cursor)
    pub selection_start: Option<usize>,

    /// Original text before editing started (for cancel/undo)
    pub original_text: String,

    /// Timestamp for cursor blink (milliseconds)
    pub blink_time: u64,
}

impl TextInputState {
    /// Create new text input state (inactive)
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if text input is active
    pub fn is_active(&self) -> bool {
        self.is_active
    }

    /// Check if this specific field is being edited
    pub fn is_editing(&self, field_id: &str) -> bool {
        self.is_active && self.field_id.as_deref() == Some(field_id)
    }

    /// Start editing a field with initial text
    pub fn start_editing(&mut self, field_id: &str, initial_text: &str) {
        self.is_active = true;
        self.field_id = Some(field_id.to_string());
        self.text = initial_text.to_string();
        self.cursor = initial_text.chars().count(); // Cursor at end
        self.selection_start = None;
        self.original_text = initial_text.to_string();
    }

    /// Start editing with timestamp for cursor blink
    pub fn start_editing_with_time(&mut self, field_id: &str, initial_text: &str, current_time_ms: u64) {
        self.start_editing(field_id, initial_text);
        self.blink_time = current_time_ms;
    }

    /// Reset blink timer (call when cursor moves or text changes)
    pub fn reset_blink(&mut self, current_time_ms: u64) {
        self.blink_time = current_time_ms;
    }

    /// Check if cursor should be visible based on blink timing
    pub fn is_cursor_visible(&self, current_time_ms: u64) -> bool {
        let elapsed = current_time_ms.wrapping_sub(self.blink_time);
        (elapsed / 500).is_multiple_of(2)
    }

    /// Stop editing and return final text
    pub fn finish_editing(&mut self) -> Option<String> {
        if !self.is_active {
            return None;
        }
        let result = self.text.clone();
        self.clear();
        Some(result)
    }

    /// Cancel editing and restore original text
    pub fn cancel_editing(&mut self) -> Option<String> {
        if !self.is_active {
            return None;
        }
        let result = self.original_text.clone();
        self.clear();
        Some(result)
    }

    /// Clear state (stop editing)
    pub fn clear(&mut self) {
        self.is_active = false;
        self.field_id = None;
        self.text.clear();
        self.cursor = 0;
        self.selection_start = None;
        self.original_text.clear();
        self.blink_time = 0;
    }

    /// Get current text
    pub fn get_text(&self) -> &str {
        &self.text
    }

    /// Get field being edited
    pub fn get_field(&self) -> Option<&str> {
        self.field_id.as_deref()
    }

    // =========================================================================
    // Text manipulation
    // =========================================================================

    /// Insert character at cursor position
    pub fn insert_char(&mut self, c: char) {
        self.delete_selection();
        let byte_pos = self.char_to_byte_pos(self.cursor);
        self.text.insert(byte_pos, c);
        self.cursor += 1;
    }

    /// Insert string at cursor position
    pub fn insert_str(&mut self, s: &str) {
        self.delete_selection();
        let byte_pos = self.char_to_byte_pos(self.cursor);
        self.text.insert_str(byte_pos, s);
        self.cursor += s.chars().count();
    }

    /// Delete character before cursor (backspace)
    pub fn backspace(&mut self) {
        if self.has_selection() {
            self.delete_selection();
        } else if self.cursor > 0 {
            let byte_pos = self.char_to_byte_pos(self.cursor - 1);
            let next_byte_pos = self.char_to_byte_pos(self.cursor);
            self.text.drain(byte_pos..next_byte_pos);
            self.cursor -= 1;
        }
    }

    /// Delete character at cursor (delete key)
    pub fn delete(&mut self) {
        if self.has_selection() {
            self.delete_selection();
        } else {
            let char_count = self.text.chars().count();
            if self.cursor < char_count {
                let byte_pos = self.char_to_byte_pos(self.cursor);
                let next_byte_pos = self.char_to_byte_pos(self.cursor + 1);
                self.text.drain(byte_pos..next_byte_pos);
            }
        }
    }

    /// Delete selected text (if any)
    fn delete_selection(&mut self) {
        if let Some(sel_start) = self.selection_start {
            let (start, end) = if sel_start < self.cursor {
                (sel_start, self.cursor)
            } else {
                (self.cursor, sel_start)
            };

            let start_byte = self.char_to_byte_pos(start);
            let end_byte = self.char_to_byte_pos(end);
            self.text.drain(start_byte..end_byte);
            self.cursor = start;
            self.selection_start = None;
        }
    }

    /// Check if there's a selection
    pub fn has_selection(&self) -> bool {
        self.selection_start.is_some() && self.selection_start != Some(self.cursor)
    }

    /// Get selection range (start, end) in character indices
    pub fn get_selection(&self) -> Option<(usize, usize)> {
        self.selection_start.map(|sel_start| {
            if sel_start < self.cursor {
                (sel_start, self.cursor)
            } else {
                (self.cursor, sel_start)
            }
        })
    }

    // =========================================================================
    // Cursor movement
    // =========================================================================

    /// Move cursor left
    pub fn move_left(&mut self, with_selection: bool) {
        if with_selection {
            if self.selection_start.is_none() {
                self.selection_start = Some(self.cursor);
            }
        } else {
            self.selection_start = None;
        }

        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// Move cursor right
    pub fn move_right(&mut self, with_selection: bool) {
        if with_selection {
            if self.selection_start.is_none() {
                self.selection_start = Some(self.cursor);
            }
        } else {
            self.selection_start = None;
        }

        let char_count = self.text.chars().count();
        if self.cursor < char_count {
            self.cursor += 1;
        }
    }

    /// Move cursor to start
    pub fn move_home(&mut self, with_selection: bool) {
        if with_selection {
            if self.selection_start.is_none() {
                self.selection_start = Some(self.cursor);
            }
        } else {
            self.selection_start = None;
        }
        self.cursor = 0;
    }

    /// Move cursor to end
    pub fn move_end(&mut self, with_selection: bool) {
        if with_selection {
            if self.selection_start.is_none() {
                self.selection_start = Some(self.cursor);
            }
        } else {
            self.selection_start = None;
        }
        self.cursor = self.text.chars().count();
    }

    /// Select all text
    pub fn select_all(&mut self) {
        self.selection_start = Some(0);
        self.cursor = self.text.chars().count();
    }

    /// Set cursor position at character index (clears selection)
    pub fn set_cursor(&mut self, pos: usize) {
        let char_count = self.text.chars().count();
        self.cursor = pos.min(char_count);
        self.selection_start = None;
    }

    /// Set cursor position based on click X within the text field
    pub fn set_cursor_from_click(&mut self, click_x_offset: f64, char_width: f64) -> usize {
        if char_width <= 0.0 {
            return self.cursor;
        }
        let char_count = self.text.chars().count();
        let clicked_pos = ((click_x_offset / char_width).round() as usize).min(char_count);
        self.cursor = clicked_pos;
        self.selection_start = None;
        clicked_pos
    }

    // =========================================================================
    // Clipboard operations
    // =========================================================================

    /// Get selected text for copy operation
    pub fn get_selected_text(&self) -> Option<String> {
        self.get_selection().map(|(start, end)| {
            let start_byte = self.char_to_byte_pos(start);
            let end_byte = self.char_to_byte_pos(end);
            self.text[start_byte..end_byte].to_string()
        })
    }

    /// Cut selected text (returns text and deletes selection)
    pub fn cut(&mut self) -> Option<String> {
        let text = self.get_selected_text();
        if text.is_some() {
            self.delete_selection();
        }
        text
    }

    /// Paste text at cursor
    pub fn paste(&mut self, text: &str) {
        self.insert_str(text);
    }

    // =========================================================================
    // Helper methods
    // =========================================================================

    /// Convert character index to byte position
    fn char_to_byte_pos(&self, char_idx: usize) -> usize {
        self.text
            .char_indices()
            .nth(char_idx)
            .map(|(pos, _)| pos)
            .unwrap_or(self.text.len())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_editing() {
        let mut state = TextInputState::new();
        state.start_editing("test", "hello");

        assert!(state.is_active());
        assert_eq!(state.get_text(), "hello");
        assert_eq!(state.cursor, 5);

        state.insert_char('!');
        assert_eq!(state.get_text(), "hello!");

        state.backspace();
        assert_eq!(state.get_text(), "hello");
    }

    #[test]
    fn test_cursor_movement() {
        let mut state = TextInputState::new();
        state.start_editing("test", "abc");

        assert_eq!(state.cursor, 3);

        state.move_left(false);
        assert_eq!(state.cursor, 2);

        state.move_home(false);
        assert_eq!(state.cursor, 0);

        state.move_end(false);
        assert_eq!(state.cursor, 3);
    }

    #[test]
    fn test_selection() {
        let mut state = TextInputState::new();
        state.start_editing("test", "hello");

        state.select_all();
        assert_eq!(state.get_selection(), Some((0, 5)));
        assert_eq!(state.get_selected_text(), Some("hello".to_string()));

        state.delete_selection();
        assert_eq!(state.get_text(), "");
    }

    #[test]
    fn test_cancel() {
        let mut state = TextInputState::new();
        state.start_editing("test", "original");

        state.insert_str(" modified");
        assert_eq!(state.get_text(), "original modified");

        let restored = state.cancel_editing();
        assert_eq!(restored, Some("original".to_string()));
        assert!(!state.is_active());
    }
}