mkgraphic 0.2.1

A Rust port of the cycfi/elements GUI framework
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Text input elements.

use std::any::Any;
use std::sync::RwLock;
use super::{Element, ViewLimits, ViewStretch, FocusRequest};
use super::context::{BasicContext, Context};
use crate::support::point::Point;
use crate::support::rect::Rect;
use crate::support::color::Color;
use crate::support::theme::get_theme;
use crate::view::{MouseButton, MouseButtonKind, KeyInfo, TextInfo, CursorTracking, KeyCode};

/// Text box state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextBoxState {
    #[default]
    Idle,
    Hover,
    Focused,
    Disabled,
}

/// Callback type for text changes.
pub type TextChangeCallback = Box<dyn Fn(&str) + Send + Sync>;
/// Callback type for enter key.
pub type EnterCallback = Box<dyn Fn(&str) + Send + Sync>;

/// A single-line text input element.
pub struct TextBox {
    text: RwLock<String>,
    placeholder: String,
    state: RwLock<TextBoxState>,
    cursor_pos: RwLock<usize>,
    selection_start: RwLock<Option<usize>>,
    background_color: Color,
    text_color: Color,
    placeholder_color: Color,
    highlight_color: Color,
    caret_color: Color,
    font_size: f32,
    width: f32,
    height: f32,
    padding: f32,
    corner_radius: f32,
    enabled: bool,
    password_mode: bool,
    on_change: Option<TextChangeCallback>,
    on_enter: Option<EnterCallback>,
    scroll_offset: RwLock<f32>,
}

impl TextBox {
    /// Creates a new text box.
    pub fn new() -> Self {
        let theme = get_theme();
        Self {
            text: RwLock::new(String::new()),
            placeholder: String::new(),
            state: RwLock::new(TextBoxState::Idle),
            cursor_pos: RwLock::new(0),
            selection_start: RwLock::new(None),
            background_color: theme.input_box_color,
            text_color: theme.text_box_font_color,
            placeholder_color: theme.text_box_idle_color,
            highlight_color: theme.text_box_hilite_color,
            caret_color: theme.text_box_caret_color,
            font_size: theme.text_box_font_size,
            width: 150.0,
            height: theme.text_box_font_size * 2.0,
            padding: 8.0,
            corner_radius: 4.0,
            enabled: true,
            password_mode: false,
            on_change: None,
            on_enter: None,
            scroll_offset: RwLock::new(0.0),
        }
    }

    /// Sets the initial text.
    pub fn text(self, text: impl Into<String>) -> Self {
        let s: String = text.into();
        let len = s.len();
        *self.text.write().unwrap() = s;
        *self.cursor_pos.write().unwrap() = len;
        self
    }

    /// Sets the placeholder text.
    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = placeholder.into();
        self
    }

    /// Sets the width.
    pub fn width(mut self, width: f32) -> Self {
        self.width = width;
        self
    }

    /// Sets password mode (displays dots instead of text).
    pub fn password(mut self, password: bool) -> Self {
        self.password_mode = password;
        self
    }

    /// Sets the background color.
    pub fn background_color(mut self, color: Color) -> Self {
        self.background_color = color;
        self
    }

    /// Sets the text color.
    pub fn text_color(mut self, color: Color) -> Self {
        self.text_color = color;
        self
    }

    /// Sets the change callback.
    pub fn on_change<F: Fn(&str) + Send + Sync + 'static>(mut self, callback: F) -> Self {
        self.on_change = Some(Box::new(callback));
        self
    }

    /// Sets the enter callback.
    pub fn on_enter<F: Fn(&str) + Send + Sync + 'static>(mut self, callback: F) -> Self {
        self.on_enter = Some(Box::new(callback));
        self
    }

    /// Returns the current text.
    pub fn get_text(&self) -> String {
        self.text.read().unwrap().clone()
    }

    /// Sets the text.
    pub fn set_text(&self, text: impl Into<String>) {
        let s: String = text.into();
        let len = s.len();
        *self.text.write().unwrap() = s;
        *self.cursor_pos.write().unwrap() = len;
        *self.selection_start.write().unwrap() = None;
    }

    /// Returns the display text (masked if password mode).
    fn display_text(&self) -> String {
        let text = self.text.read().unwrap();
        if self.password_mode {
            "•".repeat(text.chars().count())
        } else {
            text.clone()
        }
    }

    /// Inserts text at cursor position.
    fn insert_text(&self, s: &str) {
        let mut text = self.text.write().unwrap();
        let mut cursor_pos = self.cursor_pos.write().unwrap();
        let mut selection_start = self.selection_start.write().unwrap();

        // Delete selection if any
        if let Some(sel_start) = *selection_start {
            let start = sel_start.min(*cursor_pos);
            let end = sel_start.max(*cursor_pos);

            // Find byte indices
            let start_byte = text.char_indices().nth(start).map(|(i, _)| i).unwrap_or(text.len());
            let end_byte = text.char_indices().nth(end).map(|(i, _)| i).unwrap_or(text.len());

            text.replace_range(start_byte..end_byte, "");
            *cursor_pos = start;
            *selection_start = None;
        }

        // Insert new text
        let byte_pos = text.char_indices().nth(*cursor_pos).map(|(i, _)| i).unwrap_or(text.len());
        text.insert_str(byte_pos, s);
        *cursor_pos += s.chars().count();
    }

    /// Deletes character before cursor.
    fn delete_backward(&self) {
        let mut text = self.text.write().unwrap();
        let mut cursor_pos = self.cursor_pos.write().unwrap();
        let mut selection_start = self.selection_start.write().unwrap();

        if let Some(sel_start) = *selection_start {
            // Delete selection
            let start = sel_start.min(*cursor_pos);
            let end = sel_start.max(*cursor_pos);

            let start_byte = text.char_indices().nth(start).map(|(i, _)| i).unwrap_or(text.len());
            let end_byte = text.char_indices().nth(end).map(|(i, _)| i).unwrap_or(text.len());

            text.replace_range(start_byte..end_byte, "");
            *cursor_pos = start;
            *selection_start = None;
        } else if *cursor_pos > 0 {
            let prev_pos = *cursor_pos - 1;
            let start_byte = text.char_indices().nth(prev_pos).map(|(i, _)| i).unwrap_or(0);
            let end_byte = text.char_indices().nth(*cursor_pos).map(|(i, _)| i).unwrap_or(text.len());

            text.replace_range(start_byte..end_byte, "");
            *cursor_pos = prev_pos;
        }
    }

    /// Deletes character after cursor.
    fn delete_forward(&self) {
        let mut text = self.text.write().unwrap();
        let mut cursor_pos = self.cursor_pos.write().unwrap();
        let mut selection_start = self.selection_start.write().unwrap();

        if let Some(sel_start) = *selection_start {
            // Delete selection
            let start = sel_start.min(*cursor_pos);
            let end = sel_start.max(*cursor_pos);

            let start_byte = text.char_indices().nth(start).map(|(i, _)| i).unwrap_or(text.len());
            let end_byte = text.char_indices().nth(end).map(|(i, _)| i).unwrap_or(text.len());

            text.replace_range(start_byte..end_byte, "");
            *cursor_pos = start;
            *selection_start = None;
        } else {
            let char_count = text.chars().count();
            if *cursor_pos < char_count {
                let start_byte = text.char_indices().nth(*cursor_pos).map(|(i, _)| i).unwrap_or(text.len());
                let end_byte = text.char_indices().nth(*cursor_pos + 1).map(|(i, _)| i).unwrap_or(text.len());

                text.replace_range(start_byte..end_byte, "");
            }
        }
    }

    /// Moves cursor left.
    fn move_left(&self, select: bool) {
        let mut cursor_pos = self.cursor_pos.write().unwrap();
        let mut selection_start = self.selection_start.write().unwrap();

        if select {
            if selection_start.is_none() {
                *selection_start = Some(*cursor_pos);
            }
        } else {
            *selection_start = None;
        }

        if *cursor_pos > 0 {
            *cursor_pos -= 1;
        }
    }

    /// Moves cursor right.
    fn move_right(&self, select: bool) {
        let text = self.text.read().unwrap();
        let char_count = text.chars().count();
        drop(text);

        let mut cursor_pos = self.cursor_pos.write().unwrap();
        let mut selection_start = self.selection_start.write().unwrap();

        if select {
            if selection_start.is_none() {
                *selection_start = Some(*cursor_pos);
            }
        } else {
            *selection_start = None;
        }

        if *cursor_pos < char_count {
            *cursor_pos += 1;
        }
    }

    /// Moves cursor to start.
    fn move_home(&self, select: bool) {
        let mut cursor_pos = self.cursor_pos.write().unwrap();
        let mut selection_start = self.selection_start.write().unwrap();

        if select {
            if selection_start.is_none() {
                *selection_start = Some(*cursor_pos);
            }
        } else {
            *selection_start = None;
        }

        *cursor_pos = 0;
    }

    /// Moves cursor to end.
    fn move_end(&self, select: bool) {
        let text = self.text.read().unwrap();
        let char_count = text.chars().count();
        drop(text);

        let mut cursor_pos = self.cursor_pos.write().unwrap();
        let mut selection_start = self.selection_start.write().unwrap();

        if select {
            if selection_start.is_none() {
                *selection_start = Some(*cursor_pos);
            }
        } else {
            *selection_start = None;
        }

        *cursor_pos = char_count;
    }

    /// Selects all text.
    fn select_all(&self) {
        let text = self.text.read().unwrap();
        let char_count = text.chars().count();
        drop(text);

        *self.selection_start.write().unwrap() = Some(0);
        *self.cursor_pos.write().unwrap() = char_count;
    }

    fn draw_background(&self, ctx: &Context) {
        let mut canvas = ctx.canvas.borrow_mut();
        let state = *self.state.read().unwrap();

        let color = match state {
            TextBoxState::Idle => self.background_color,
            TextBoxState::Hover => self.background_color.level(1.1),
            TextBoxState::Focused => self.background_color.level(1.2),
            TextBoxState::Disabled => self.background_color.with_alpha(0.5),
        };

        canvas.fill_style(color);
        canvas.fill_round_rect(ctx.bounds, self.corner_radius);

        // Draw focus border
        if state == TextBoxState::Focused {
            let theme = get_theme();
            canvas.stroke_style(theme.frame_hilite_color);
            canvas.line_width(1.0);
            canvas.begin_path();
            canvas.add_round_rect(ctx.bounds, self.corner_radius);
            canvas.stroke();
        }
    }

    fn draw_text(&self, ctx: &Context) {
        let mut canvas = ctx.canvas.borrow_mut();
        let state = *self.state.read().unwrap();
        let display = self.display_text();

        let text_area = Rect::new(
            ctx.bounds.left + self.padding,
            ctx.bounds.top,
            ctx.bounds.right - self.padding,
            ctx.bounds.bottom,
        );

        canvas.font_size(self.font_size);

        if display.is_empty() && !self.placeholder.is_empty() {
            // Draw placeholder
            let color = if state == TextBoxState::Disabled {
                self.placeholder_color.with_alpha(0.3)
            } else {
                self.placeholder_color
            };
            canvas.fill_style(color);
            let y = text_area.center().y + self.font_size * 0.35;
            canvas.fill_text(&self.placeholder, Point::new(text_area.left, y));
        } else {
            // Draw text
            let color = if state == TextBoxState::Disabled {
                self.text_color.with_alpha(0.5)
            } else {
                self.text_color
            };
            canvas.fill_style(color);
            let y = text_area.center().y + self.font_size * 0.35;
            canvas.fill_text(&display, Point::new(text_area.left, y));
        }
    }

    fn draw_selection(&self, ctx: &Context) {
        let selection_start = *self.selection_start.read().unwrap();
        let cursor_pos = *self.cursor_pos.read().unwrap();

        if selection_start.is_none() {
            return;
        }

        let sel_start = selection_start.unwrap();
        if sel_start == cursor_pos {
            return;
        }

        let mut canvas = ctx.canvas.borrow_mut();
        let display = self.display_text();

        let start = sel_start.min(cursor_pos);
        let end = sel_start.max(cursor_pos);

        // Measure text width up to start and end positions
        canvas.font_size(self.font_size);
        let x1 = ctx.bounds.left + self.padding + canvas.text_width_to_position(&display, start);
        let x2 = ctx.bounds.left + self.padding + canvas.text_width_to_position(&display, end);

        let sel_rect = Rect::new(
            x1,
            ctx.bounds.top + 4.0,
            x2,
            ctx.bounds.bottom - 4.0,
        );

        canvas.fill_style(self.highlight_color);
        canvas.fill_rect(sel_rect);
    }

    fn draw_caret(&self, ctx: &Context) {
        let state = *self.state.read().unwrap();
        if state != TextBoxState::Focused {
            return;
        }

        let mut canvas = ctx.canvas.borrow_mut();
        let cursor_pos = *self.cursor_pos.read().unwrap();
        let display = self.display_text();

        // Measure text width up to cursor position
        canvas.font_size(self.font_size);
        let x = ctx.bounds.left + self.padding + canvas.text_width_to_position(&display, cursor_pos);
        let y1 = ctx.bounds.top + 4.0;
        let y2 = ctx.bounds.bottom - 4.0;

        canvas.stroke_style(self.caret_color);
        canvas.line_width(1.5);
        canvas.begin_path();
        canvas.move_to(Point::new(x, y1));
        canvas.line_to(Point::new(x, y2));
        canvas.stroke();
    }
}

impl Default for TextBox {
    fn default() -> Self {
        Self::new()
    }
}

impl Element for TextBox {
    fn limits(&self, _ctx: &BasicContext) -> ViewLimits {
        ViewLimits::fixed(self.width, self.height)
    }

    fn stretch(&self) -> ViewStretch {
        ViewStretch::new(1.0, 0.0)
    }

    fn draw(&self, ctx: &Context) {
        self.draw_background(ctx);
        self.draw_selection(ctx);
        self.draw_text(ctx);
        self.draw_caret(ctx);
    }

    fn hit_test(&self, ctx: &Context, p: Point, _leaf: bool, _control: bool) -> Option<&dyn Element> {
        if ctx.bounds.contains(p) && self.enabled {
            Some(self)
        } else {
            None
        }
    }

    fn wants_control(&self) -> bool {
        self.enabled
    }

    fn wants_focus(&self) -> bool {
        self.enabled
    }

    fn begin_focus(&mut self, _req: FocusRequest) {
        *self.state.write().unwrap() = TextBoxState::Focused;
    }

    fn end_focus(&mut self) -> bool {
        *self.state.write().unwrap() = TextBoxState::Idle;
        true
    }

    fn clear_focus(&self) {
        let mut state = self.state.write().unwrap();
        if *state == TextBoxState::Focused {
            *state = TextBoxState::Idle;
        }
    }

    fn handle_click(&self, ctx: &Context, btn: MouseButton) -> bool {
        if !self.enabled || btn.button != MouseButtonKind::Left {
            return false;
        }

        if btn.down {
            *self.state.write().unwrap() = TextBoxState::Focused;

            // Set cursor position based on click location
            let char_width = self.font_size * 0.6;
            let text = self.text.read().unwrap();
            let char_count = text.chars().count();
            drop(text);

            let rel_x = btn.pos.x - ctx.bounds.left - self.padding;
            let pos = ((rel_x / char_width).round() as usize).min(char_count);

            *self.cursor_pos.write().unwrap() = pos;
            *self.selection_start.write().unwrap() = None;
        }

        true
    }

    fn key(&mut self, _ctx: &Context, k: KeyInfo) -> bool {
        self.handle_key(_ctx, k)
    }

    fn handle_key(&self, _ctx: &Context, k: KeyInfo) -> bool {
        if !self.enabled {
            return false;
        }

        let state = *self.state.read().unwrap();
        if state != TextBoxState::Focused {
            return false;
        }

        if k.action != crate::view::KeyAction::Press && k.action != crate::view::KeyAction::Repeat {
            return true;
        }

        let shift = k.modifiers & crate::view::modifiers::SHIFT != 0;
        let ctrl = k.modifiers & (crate::view::modifiers::CONTROL | crate::view::modifiers::SUPER) != 0;

        match k.key {
            KeyCode::Left => {
                self.move_left(shift);
                return true;
            }
            KeyCode::Right => {
                self.move_right(shift);
                return true;
            }
            KeyCode::Home => {
                self.move_home(shift);
                return true;
            }
            KeyCode::End => {
                self.move_end(shift);
                return true;
            }
            KeyCode::Backspace => {
                self.delete_backward();
                if let Some(ref callback) = self.on_change {
                    callback(&self.get_text());
                }
                return true;
            }
            KeyCode::Delete => {
                self.delete_forward();
                if let Some(ref callback) = self.on_change {
                    callback(&self.get_text());
                }
                return true;
            }
            KeyCode::Enter => {
                if let Some(ref callback) = self.on_enter {
                    callback(&self.get_text());
                }
                return true;
            }
            KeyCode::A if ctrl => {
                self.select_all();
                return true;
            }
            _ => {}
        }

        false
    }

    fn text(&mut self, _ctx: &Context, info: TextInfo) -> bool {
        self.handle_text(_ctx, info)
    }

    fn handle_text(&self, _ctx: &Context, info: TextInfo) -> bool {
        if !self.enabled {
            return false;
        }

        let state = *self.state.read().unwrap();
        if state != TextBoxState::Focused {
            return false;
        }

        // Filter control characters
        let c = info.codepoint;
        if !c.is_control() {
            let s = c.to_string();
            self.insert_text(&s);
            if let Some(ref callback) = self.on_change {
                callback(&self.get_text());
            }
        }

        true
    }

    fn cursor(&mut self, _ctx: &Context, _p: Point, status: CursorTracking) -> bool {
        if !self.enabled {
            return false;
        }

        let mut state = self.state.write().unwrap();
        if *state == TextBoxState::Focused {
            return true;
        }

        match status {
            CursorTracking::Entering | CursorTracking::Hovering => {
                *state = TextBoxState::Hover;
            }
            CursorTracking::Leaving => {
                *state = TextBoxState::Idle;
            }
        }

        true
    }

    fn enable(&mut self, state: bool) {
        self.enabled = state;
        let mut box_state = self.state.write().unwrap();
        if !state {
            *box_state = TextBoxState::Disabled;
        } else if *box_state == TextBoxState::Disabled {
            *box_state = TextBoxState::Idle;
        }
    }

    fn is_enabled(&self) -> bool {
        self.enabled
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Creates a text box.
pub fn text_box() -> TextBox {
    TextBox::new()
}

/// Creates a text box with initial text.
pub fn text_box_with_text(text: impl Into<String>) -> TextBox {
    TextBox::new().text(text)
}

/// Creates a password input field.
pub fn password_box() -> TextBox {
    TextBox::new().password(true)
}