rust_widgets 1.0.0

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 60+ widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
//! InplaceEditor widget — an in-place text editing control for table/cell editing.
//!
//! Displays text normally, and when activated (double-click), switches to an
//! edit mode with a blinking cursor. Enter/Tab accepts changes, Escape cancels.

use crate::core::{Color, Font, HorizontalAlignment, Point, Rect};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::{GenericSignal, Signal1};
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};

/// A text editor that switches between display mode and edit mode in-place.
///
/// In display mode, the text is drawn as plain text with a background.
/// In edit mode, it draws a text input area with a blinking cursor.
pub struct InplaceEditor {
    base: BaseWidget,
    text: String,
    is_editing: bool,
    original_text: String,
    font_size: f32,
    padding: i32,
    cursor_position: usize,
    /// Emitted when the edit is accepted (Enter/Tab). Carries the final text.
    pub edit_accepted: Signal1<String>,
    /// Emitted when the edit is cancelled (Escape).
    pub edit_cancelled: GenericSignal,
}

impl InplaceEditor {
    /// Creates a new InplaceEditor widget with the given text and geometry.
    pub fn new(text: &str, geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::InplaceEditor, geometry, "InplaceEditor"),
            text: text.to_string(),
            is_editing: false,
            original_text: text.to_string(),
            font_size: 14.0,
            padding: 4,
            cursor_position: text.len(),
            edit_accepted: Signal1::new(),
            edit_cancelled: GenericSignal::new(),
        }
    }

    /// Starts editing mode.
    pub fn start_edit(&mut self) {
        if !self.is_editing {
            self.is_editing = true;
            self.original_text = self.text.clone();
            self.cursor_position = self.text.len();
            self.base.request_redraw();
        }
    }

    /// Finishes editing mode. If `accept` is true, the current text is kept;
    /// otherwise it reverts to the original text.
    pub fn finish_edit(&mut self, accept: bool) {
        if !self.is_editing {
            return;
        }
        self.is_editing = false;
        if accept {
            self.edit_accepted.emit(self.text.clone());
        } else {
            self.text = self.original_text.clone();
            self.edit_cancelled.emit();
        }
        self.base.request_redraw();
    }

    /// Returns whether the editor is in edit mode.
    pub fn is_editing(&self) -> bool {
        self.is_editing
    }

    /// Returns the current text content.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Sets the text content.
    pub fn set_text(&mut self, text: &str) {
        self.text = text.to_string();
        self.cursor_position = self.text.len();
        self.base.request_redraw();
    }

    /// Sets the font size.
    pub fn set_font_size(&mut self, size: f32) {
        self.font_size = size.max(4.0);
        self.base.request_redraw();
    }

    /// Returns the font size.
    pub fn font_size(&self) -> f32 {
        self.font_size
    }

    /// Sets the padding around the text.
    pub fn set_padding(&mut self, padding: i32) {
        self.padding = padding.max(0);
        self.base.request_redraw();
    }

    /// Returns the padding.
    pub fn padding(&self) -> i32 {
        self.padding
    }

    /// Inserts a character at the cursor position.
    fn insert_char(&mut self, c: char) {
        if c == '\u{7f}' {
            // Delete (backward)
            if self.cursor_position > 0 {
                let mut chars: Vec<char> = self.text.chars().collect();
                chars.remove(self.cursor_position - 1);
                self.text = chars.into_iter().collect();
                self.cursor_position = self.cursor_position.saturating_sub(1);
                self.base.request_redraw();
            }
        } else if c == '\u{ffff}' {
            // Forward delete
            if self.cursor_position < self.text.chars().count() {
                let mut chars: Vec<char> = self.text.chars().collect();
                chars.remove(self.cursor_position);
                self.text = chars.into_iter().collect();
                self.base.request_redraw();
            }
        } else {
            let mut chars: Vec<char> = self.text.chars().collect();
            chars.insert(self.cursor_position, c);
            self.text = chars.into_iter().collect();
            self.cursor_position += 1;
            self.base.request_redraw();
        }
    }

    /// Moves the cursor left by one character.
    fn cursor_left(&mut self) {
        if self.cursor_position > 0 {
            self.cursor_position -= 1;
            self.base.request_redraw();
        }
    }

    /// Moves the cursor right by one character.
    fn cursor_right(&mut self) {
        let char_count = self.text.chars().count();
        if self.cursor_position < char_count {
            self.cursor_position += 1;
            self.base.request_redraw();
        }
    }
}

impl Widget for InplaceEditor {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn size_hint(&self) -> crate::core::Size {
        crate::core::Size::new(200, 28)
    }
}

impl Draw for InplaceEditor {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        let font = Font::new("sans-serif", self.font_size, false, false);

        if self.is_editing {
            // Draw editing mode
            context.fill_rect(rect, Color::rgba(255, 255, 255, 255));
            context.draw_rect_stroke(rect, Color::rgba(0, 120, 255, 255), 2);

            // Draw text
            let text_x = rect.x + self.padding;
            let text_y = rect.y + self.padding + self.font_size as i32;
            context.draw_text(
                Point::new(text_x, text_y),
                &self.text,
                &font,
                Color::BLACK,
                HorizontalAlignment::Left,
            );

            // Draw cursor (blinking vertical line)
            let cursor_x = text_x + self.cursor_position as i32 * 8;
            context.draw_line(
                Point::new(cursor_x, rect.y + self.padding),
                Point::new(cursor_x, rect.y + rect.height as i32 - self.padding),
                Color::rgba(0, 0, 0, 200),
            );
        } else {
            // Draw display mode
            context.fill_rect(rect, Color::rgba(245, 245, 245, 255));
            context.draw_rect_stroke(rect, Color::rgba(200, 200, 200, 255), 1);

            let text_x = rect.x + self.padding;
            let text_y = rect.y + self.padding + self.font_size as i32;
            context.draw_text(
                Point::new(text_x, text_y),
                &self.text,
                &font,
                Color::rgba(50, 50, 50, 255),
                HorizontalAlignment::Left,
            );
        }
    }
}

impl EventHandler for InplaceEditor {
    fn handle_event(&mut self, event: &Event) {
        if !self.base.is_enabled() {
            return;
        }
        match event {
            Event::MouseDoubleClick { pos: _, button } if *button == 1 => {
                self.start_edit();
            }
            Event::KeyPress { key, modifiers: _ } => {
                if !self.is_editing {
                    self.base.handle_event(event);
                    return;
                }
                match *key {
                    0x1B => {
                        // Escape - cancel
                        self.finish_edit(false);
                    }
                    0x0D | 0x09 => {
                        // Enter or Tab - accept
                        self.finish_edit(true);
                    }
                    0x08 => {
                        // Backspace
                        self.insert_char('\u{7f}');
                    }
                    0x2E => {
                        // Delete (forward)
                        self.insert_char('\u{ffff}');
                    }
                    0x25 => {
                        // Left arrow
                        self.cursor_left();
                    }
                    0x27 => {
                        // Right arrow
                        self.cursor_right();
                    }
                    _ => {
                        // Printable characters
                        if let Some(c) = char::from_u32(*key) {
                            if c.is_alphanumeric() || c.is_whitespace() || c.is_ascii_punctuation()
                            {
                                self.insert_char(c);
                            }
                        }
                    }
                }
            }
            _ => {
                self.base.handle_event(event);
            }
        }
    }
}

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

    #[test]
    fn inplace_editor_initial_state() {
        let ie = InplaceEditor::new("Hello", Rect::new(0, 0, 200, 30));
        assert_eq!(ie.text(), "Hello");
        assert!(!ie.is_editing());
        assert!((ie.font_size() - 14.0).abs() < 0.01);
        assert_eq!(ie.padding(), 4);
        assert_eq!(ie.kind(), WidgetKind::InplaceEditor);
    }

    #[test]
    fn inplace_editor_set_text() {
        let mut ie = InplaceEditor::new("Hello", Rect::new(0, 0, 200, 30));
        ie.set_text("World");
        assert_eq!(ie.text(), "World");
    }

    #[test]
    fn inplace_editor_start_and_finish_edit_accept() {
        let mut ie = InplaceEditor::new("Hello", Rect::new(0, 0, 200, 30));

        let accepted = std::sync::Arc::new(std::sync::Mutex::new(None));
        let accepted_clone = accepted.clone();
        ie.edit_accepted.connect(move |text| {
            *accepted_clone.lock().unwrap() = Some((*text).clone());
        });

        ie.start_edit();
        assert!(ie.is_editing());

        ie.set_text("Hello World");
        ie.finish_edit(true);

        assert!(!ie.is_editing());
        assert_eq!(ie.text(), "Hello World");
        assert_eq!(*accepted.lock().unwrap(), Some("Hello World".to_string()));
    }

    #[test]
    fn inplace_editor_start_and_finish_edit_cancel() {
        let mut ie = InplaceEditor::new("Hello", Rect::new(0, 0, 200, 30));

        let cancelled = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let cancelled_clone = cancelled.clone();
        ie.edit_cancelled.connect(move || {
            cancelled_clone.store(true, std::sync::atomic::Ordering::SeqCst);
        });

        ie.start_edit();
        assert!(ie.is_editing());

        ie.set_text("Modified Text");
        ie.finish_edit(false);

        assert!(!ie.is_editing());
        assert_eq!(ie.text(), "Hello"); // Should revert to original
        assert!(cancelled.load(std::sync::atomic::Ordering::SeqCst));
    }

    #[test]
    fn inplace_editor_double_click_starts_edit() {
        let mut ie = InplaceEditor::new("Hello", Rect::new(0, 0, 200, 30));
        ie.handle_event(&Event::MouseDoubleClick { pos: Point::new(50, 15), button: 1 });
        assert!(ie.is_editing());
    }

    #[test]
    fn inplace_editor_escape_cancels_edit() {
        let mut ie = InplaceEditor::new("Hello", Rect::new(0, 0, 200, 30));
        ie.start_edit();
        ie.set_text("Changed");

        ie.handle_event(&Event::KeyPress {
            key: 0x1B, // Escape
            modifiers: 0,
        });

        assert!(!ie.is_editing());
        assert_eq!(ie.text(), "Hello");
    }

    #[test]
    fn inplace_editor_enter_accepts_edit() {
        let mut ie = InplaceEditor::new("Hello", Rect::new(0, 0, 200, 30));
        ie.start_edit();
        ie.set_text("Accepted");

        ie.handle_event(&Event::KeyPress {
            key: 0x0D, // Enter
            modifiers: 0,
        });

        assert!(!ie.is_editing());
        assert_eq!(ie.text(), "Accepted");
    }

    #[test]
    fn inplace_editor_set_font_size_and_padding() {
        let mut ie = InplaceEditor::new("Test", Rect::new(0, 0, 200, 30));

        ie.set_font_size(18.0);
        assert!((ie.font_size() - 18.0).abs() < 0.01);

        ie.set_font_size(0.0); // Should clamp
        assert!((ie.font_size() - 4.0).abs() < 0.01);

        ie.set_padding(8);
        assert_eq!(ie.padding(), 8);

        ie.set_padding(-5); // Should clamp to 0
        assert_eq!(ie.padding(), 0);
    }

    #[test]
    fn inplace_editor_insert_characters() {
        let mut ie = InplaceEditor::new("", Rect::new(0, 0, 200, 30));
        ie.start_edit();

        // Simulate typing
        ie.insert_char('A');
        ie.insert_char('B');
        ie.insert_char('C');
        assert_eq!(ie.text(), "ABC");
        assert_eq!(ie.cursor_position, 3);

        // Backspace
        ie.insert_char('\u{7f}');
        assert_eq!(ie.text(), "AB");
        assert_eq!(ie.cursor_position, 2);
    }
}