Repository navigation
Expand file tree
/
Copy pathtext_input.rs
More file actions
1353 lines (1214 loc) · 49.3 KB
/
Copy pathtext_input.rs
File metadata and controls
1353 lines (1214 loc) · 49.3 KB
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Made by: Mathew Dusome
Jul 31, 2026
Adds a text input object
================================================================================
CLIPBOARD SUPPORT
================================================================================
No WEB Support
Ctrl+C, Ctrl+V, and Ctrl+X are implemented on windows, linux and macOS using system clipboard utilities
(wl-copy/wl-paste for Wayland, xclip for X11, pbcopy/pbpaste for macOS, and clip/Get-Clipboard for Windows).
Running the following command in terminal:
cargo add arboard@3.6 --features wayland-data-control --target 'cfg(not(target_arch = "wasm32"))'
Or to manunal add if adding to Cargo.toml:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
arboard = { version = "3.6", features = ["wayland-data-control"] }
In your src/ui.rs file add the following to the end of the file
pub mod text_input;
Add the following in the screen you want to use the textinput in with the other use statements
use crate::ui::text_input::TextInput;
Then to use this you would put the following above the loop:
let mut txt_input = TextInput::new(100.0, 100.0, 300.0, 40.0, 25.0);
Where the parameters are x, y, width, height, font size
You can customize the text box using various methods:
LIMITS AND MULTILINE:
// Set a maximum number of characters
txt_input.set_max_chars(50);
// Restrict input to specific characters
txt_input.set_allowed_chars("0123456789");
// Enable multiline mode (text wraps within the box)
txt_input.set_multiline(true);
APPEARANCE CUSTOMIZATION:
// Set colors (text, border, background, cursor)
txt_input.with_colors(WHITE, BLUE, DARKGRAY, RED);
// Set individual colors
txt_input.set_text_color(WHITE)
.set_border_color(BLUE)
.set_background_color(DARKGRAY)
.set_cursor_color(RED);
// Set custom font
txt_input.with_font(my_font.clone());
// Change position and dimensions
txt_input.set_position(150.0, 150.0);
txt_input.set_dimensions(250.0, 50.0);
// Set prompt text and color (shown when input is empty)
txt_input.set_prompt("Enter your name...");
txt_input.set_prompt_color(DARKGRAY);
// Enable or disable the text input
txt_input.set_enabled(false); // Disable the text input (becomes read-only)
txt_input.set_enabled(true); // Enable the text input
txt_input.set_disabled_color(Color::new(0.7, 0.7, 0.7, 0.5)); // Customize disabled appearance
TEXT MANIPULATION:
// Get current text
let current_text = txt_input.get_text();
// Set text content
txt_input.set_text("Hello World");
// Check active state
if txt_input.is_active() {
// Do something when textbox is active
}
// Set cursor position
txt_input.set_cursor_index(5);
// Customize key repeat behavior (for arrow keys, backspace, delete)
txt_input.set_key_repeat_delay(0.3); // Initial delay before key repeat starts (seconds)
txt_input.set_key_repeat_rate(0.03); // Time between repeats after initial delay (seconds)
// Or set both at once
txt_input.with_key_repeat_settings(0.3, 0.03);
Then in the main loop you would use:
// Update and draw the textbox in one step
txt_input.draw();
*/
#[cfg(feature = "scale")]
use crate::utils::scale::mouse_position_world as mouse_position;
use macroquad::prelude::*;
#[cfg(target_arch = "wasm32")]
#[allow(dead_code)]
#[link(wasm_import_module = "env")]
unsafe extern "C" {
pub fn mq_request_paste();
pub fn mq_get_paste_len() -> usize;
pub fn mq_fill_paste_buffer(ptr: *mut u8);
pub fn mq_clear_paste();
pub fn mq_copy_to_clipboard(ptr: *const u8, len: usize);
}
pub fn copy_to_clipboard(text: String) {
// =========================
// 🌐 WEB (using JavaScript clipboard API)
// =========================
#[cfg(target_arch = "wasm32")]
{
let text_bytes = text.as_bytes();
unsafe {
mq_copy_to_clipboard(text_bytes.as_ptr(), text_bytes.len());
}
return;
}
// =========================
// 🐧 Everything else
// =========================
#[cfg(not(target_arch = "wasm32"))]
{
use arboard::Clipboard;
let clipboard = Clipboard::new();
if let Ok(mut clipboard) = clipboard {
clipboard.set_text(text).unwrap();
}
return;
}
}
#[allow(unused)]
pub fn paste_from_clipboard() -> String {
// =========================
// 🌐 WEB (using JavaScript clipboard API)
// =========================
#[cfg(target_arch = "wasm32")]
{
// Request paste from JavaScript (async)
// JavaScript handles browser-specific logic (disabled for Firefox)
unsafe {
mq_request_paste();
}
// Return empty - actual content comes via async polling
return String::new();
}
// =========================
// Everything else
// =========================
#[cfg(not(target_arch = "wasm32"))]
{
use arboard::Clipboard;
let clipboard = Clipboard::new();
if let Ok(mut clipboard) = clipboard {
return clipboard.get_text().unwrap_or_default();
}
return String::new();
}
}
pub struct TextInput {
// For vertical navigation, store the preferred column
preferred_col: Option<usize>,
#[cfg(target_arch = "wasm32")]
paste_requested: bool,
// Make all fields private for complete encapsulation
x: f32,
y: f32,
width: f32,
height: f32,
text: String,
active: bool,
cursor_index: usize,
cursor_timer: f32,
cursor_visible: bool,
font_size: f32,
text_color: Color,
border_color: Color,
background_color: Color,
cursor_color: Color,
font: Option<Font>,
prompt: Option<String>, // New field for prompt text
prompt_color: Color, // Color for the prompt text
// Add key repeat functionality
key_repeat_delay: f32, // Initial delay before key starts repeating (in seconds)
key_repeat_rate: f32, // How often the key repeats after initial delay (in seconds)
key_repeat_timer: f32, // Timer to track key repeat
last_key: Option<KeyCode>, // Track the last key that was pressed
enabled: bool, // Controls whether the text input can be interacted with
disabled_color: Color, // Color used when the text input is disabled
// New: Multiline and max chars support
multiline: bool, // If true, wraps text to next line within box
max_chars: Option<usize>, // Optional maximum number of characters
allowed_chars: Option<String>, // Optional whitelist of allowed typed characters
selection_anchor: Option<usize>, // Selection anchor byte index for range selection
is_dragging_selection: bool, // Tracks active mouse drag selection
drag_start_position: Option<(f32, f32)>, // Track where drag started to distinguish click from drag
}
impl TextInput {
fn is_char_allowed(&self, c: char) -> bool {
self.allowed_chars.as_ref().map_or(true, |allowed| allowed.contains(c))
}
fn apply_text_constraints(&self, text: &str) -> String {
let mut constrained = String::new();
for c in text.chars() {
if c == '\n' && self.multiline {
constrained.push(c);
} else if self.is_char_allowed(c) {
constrained.push(c);
}
if self.max_chars.is_some_and(|max| constrained.chars().count() >= max) {
break;
}
}
constrained
}
fn can_insert_char(&self, c: char) -> bool {
if c == '\n' && !self.multiline {
return false;
}
if c != '\n' && !self.is_char_allowed(c) {
return false;
}
self.max_chars.map_or(true, |max| self.text.chars().count() < max)
}
/// Returns (wrapped_lines, mapping) where mapping[byte_idx] = (line, col)
fn get_wrapped_lines_and_mapping(&self) -> (Vec<String>, Vec<(usize, usize)>) {
if !self.multiline {
// Single line: mapping is just (0, col) for each char
let mut mapping = Vec::new();
let mut col = 0;
for (i, c) in self.text.char_indices() {
mapping.resize(i + c.len_utf8(), (0, col));
col += 1;
}
mapping.resize(self.text.len() + 1, (0, col));
return (vec![self.text.clone()], mapping);
}
let mut lines = Vec::new();
let mut mapping = vec![(0, 0); self.text.len() + 1];
let padding = 5.0;
let max_width = self.width - 2.0 * padding;
let font = self.font.as_ref();
let mut line_idx: usize = 0;
let mut col_idx = 0;
let mut current_line = String::new();
let mut current_width = 0.0;
let mut byte_idx = 0;
let mut chars = self.text.chars().peekable();
while let Some(c) = chars.next() {
let c_width = measure_text(&c.to_string(), font, self.font_size as u16, 1.0).width;
let is_newline = c == '\n';
// If wrapping needed (but not for newline)
if !is_newline && current_width + c_width > max_width && !current_line.is_empty() {
lines.push(current_line.clone());
current_line.clear();
current_width = 0.0;
line_idx += 1;
col_idx = 0;
}
if is_newline {
lines.push(current_line.clone());
current_line.clear();
current_width = 0.0;
line_idx += 1;
col_idx = 0;
mapping[byte_idx] = (line_idx, col_idx); // Map the newline byte
byte_idx += c.len_utf8();
continue;
}
current_line.push(c);
// Map every byte of this char to the current (line, col)
let char_bytes = c.len_utf8();
for i in 0..char_bytes {
if byte_idx + i < mapping.len() {
mapping[byte_idx + i] = (line_idx, col_idx);
}
}
byte_idx += char_bytes;
current_width += c_width;
col_idx += 1;
}
if !current_line.is_empty() {
lines.push(current_line);
line_idx += 1;
}
// Map the end of the text
if byte_idx < mapping.len() {
mapping[byte_idx] = (line_idx.saturating_sub(1), lines.last().map(|l| l.chars().count()).unwrap_or(0));
}
(lines, mapping)
}
/// Ensure the cursor index and selection anchor are always at a valid UTF-8 boundary and in bounds
fn ensure_indices_validity(&mut self) {
// Ensure cursor_index is valid
if self.cursor_index > self.text.len() {
self.cursor_index = self.text.len();
}
// Clamp to char boundary
while self.cursor_index > 0 && !self.text.is_char_boundary(self.cursor_index) {
self.cursor_index -= 1;
}
// Ensure selection_anchor is valid if it exists
if let Some(ref mut anchor) = self.selection_anchor {
if *anchor > self.text.len() {
*anchor = self.text.len();
}
// Clamp to char boundary
while *anchor > 0 && !self.text.is_char_boundary(*anchor) {
*anchor -= 1;
}
}
}
fn get_selection_range(&self) -> Option<(usize, usize)> {
let anchor = self.selection_anchor?;
let mut start = anchor.min(self.cursor_index);
let mut end = anchor.max(self.cursor_index);
while start > 0 && !self.text.is_char_boundary(start) {
start -= 1;
}
while end > 0 && end < self.text.len() && !self.text.is_char_boundary(end) {
end -= 1;
}
if start == end { None } else { Some((start, end)) }
}
fn clear_selection(&mut self) {
self.selection_anchor = None;
}
fn delete_selection(&mut self) -> bool {
if let Some((start, end)) = self.get_selection_range() {
self.text.replace_range(start..end, "");
self.cursor_index = start;
self.ensure_indices_validity();
self.clear_selection();
return true;
}
false
}
fn index_from_local_point(&self, local_x: f32, local_y: f32) -> usize {
if self.text.is_empty() {
return 0;
}
let (wrapped_lines, mapping) = self.get_wrapped_lines_and_mapping();
if wrapped_lines.is_empty() {
return self.text.len();
}
let line_height = self.font_size + 2.0;
let mut clicked_line = (local_y / line_height).floor() as isize;
if clicked_line < 0 {
clicked_line = 0;
}
let clicked_line = (clicked_line as usize).min(wrapped_lines.len().saturating_sub(1));
let mut col = 0usize;
let mut x_offset = 0.0;
let font = self.font.as_ref();
let line = &wrapped_lines[clicked_line];
for (i, c) in line.chars().enumerate() {
let c_width = measure_text(&c.to_string(), font, self.font_size as u16, 1.0).width;
if x_offset + c_width / 2.0 > local_x {
break;
}
x_offset += c_width;
col = i + 1;
}
let mut last_match = None;
for (byte_idx, &(line_idx, ccol)) in mapping.iter().enumerate() {
if line_idx == clicked_line && ccol == col {
last_match = Some(byte_idx);
}
}
last_match.unwrap_or(self.text.len())
}
fn horizontal_offset_for_col(&self, line: &str, col: usize) -> f32 {
let font = self.font.as_ref();
line.chars()
.take(col)
.map(|c| measure_text(&c.to_string(), font, self.font_size as u16, 1.0).width)
.sum()
}
fn line_col_for_index(&self, mapping: &[(usize, usize)], wrapped_lines: &[String], index: usize) -> (usize, usize) {
let idx = index.min(self.text.len());
if idx < mapping.len() {
mapping[idx]
} else {
(
wrapped_lines.len().saturating_sub(1),
wrapped_lines.last().map(|l| l.chars().count()).unwrap_or(0),
)
}
}
pub fn new(x: f32, y: f32, width: f32, height: f32, font_size: f32) -> Self {
Self {
preferred_col: None,
#[cfg(target_arch = "wasm32")]
paste_requested: false,
x,
y,
width,
height,
text: String::new(),
active: false,
cursor_index: 0,
cursor_timer: 0.0,
cursor_visible: true,
font_size,
text_color: BLACK, // Default color for text
border_color: DARKGRAY, // Default color for border
background_color: LIGHTGRAY, // Default color for background
cursor_color: BLACK, // Default color for cursor
font: None, // Default to None (use system font)
prompt: None, // Default to None (no prompt text)
prompt_color: GRAY, // Default color for prompt text
// Initialize key repeat values
key_repeat_delay: 0.4, // 400ms initial delay before repeat
key_repeat_rate: 0.05, // 50ms between repeats after initial delay
key_repeat_timer: 0.0,
last_key: None,
enabled: true, // Default to enabled
disabled_color: Color::new(0.7, 0.7, 0.7, 0.5), // Semi-transparent gray for disabled state
multiline: false,
max_chars: None,
allowed_chars: None,
selection_anchor: None,
is_dragging_selection: false,
drag_start_position: None,
}
}
/// Enable or disable multiline mode (wrapping within box)
#[allow(unused)]
pub fn set_multiline(&mut self, multiline: bool) -> &mut Self {
self.multiline = multiline;
self
}
#[allow(unused)]
pub fn is_multiline(&self) -> bool {
self.multiline
}
// Position and dimension getters/setters
#[allow(unused)]
pub fn get_x(&self) -> f32 {
self.x
}
#[allow(unused)]
pub fn set_x(&mut self, x: f32) -> &mut Self {
self.x = x;
self
}
#[allow(unused)]
pub fn get_y(&self) -> f32 {
self.y
}
#[allow(unused)]
pub fn set_y(&mut self, y: f32) -> &mut Self {
self.y = y;
self
}
#[allow(unused)]
pub fn get_width(&self) -> f32 {
self.width
}
#[allow(unused)]
pub fn set_width(&mut self, width: f32) -> &mut Self {
self.width = width;
self
}
#[allow(unused)]
pub fn get_height(&self) -> f32 {
self.height
}
#[allow(unused)]
pub fn set_height(&mut self, height: f32) -> &mut Self {
self.height = height;
self
}
// Position convenience methods
#[allow(unused)]
pub fn get_position(&self) -> (f32, f32) {
(self.x, self.y)
}
#[allow(unused)]
pub fn set_position(&mut self, x: f32, y: f32) -> &mut Self {
self.x = x;
self.y = y;
self
}
// Dimension convenience methods
#[allow(unused)]
pub fn get_dimensions(&self) -> (f32, f32) {
(self.width, self.height)
}
#[allow(unused)]
pub fn set_dimensions(&mut self, width: f32, height: f32) -> &mut Self {
self.width = width;
self.height = height;
self
}
// Add a method to change colors
#[allow(unused)]
pub fn with_colors(&mut self, text_color: Color, border_color: Color, background_color: Color, cursor_color: Color) -> &mut Self {
self.text_color = text_color;
self.border_color = border_color;
self.background_color = background_color;
self.cursor_color = cursor_color;
self
}
// Method to set custom font
#[allow(unused)]
pub fn with_font(&mut self, font: Font) -> &mut Self {
self.font = Some(font);
self
}
// Get the current text content
#[allow(unused)]
pub fn get_text(&self) -> String {
self.text.clone()
}
// Set the text content - now accepts both String and &str
#[allow(unused)]
pub fn set_text<T: Into<String>>(&mut self, text: T) -> &mut Self {
self.text = self.apply_text_constraints(&text.into());
self.ensure_indices_validity();
self.clear_selection();
self
}
// Active state getters/setters
#[allow(unused)]
pub fn is_active(&self) -> bool {
self.active
}
#[allow(unused)]
pub fn set_active(&mut self, active: bool) -> &mut Self {
self.active = active;
if !active {
self.clear_selection();
self.is_dragging_selection = false;
}
self
}
// Cursor index getters/setters
#[allow(unused)]
pub fn get_cursor_index(&self) -> usize {
self.cursor_index
}
#[allow(unused)]
pub fn set_cursor_index(&mut self, index: usize) -> &mut Self {
let mut clamped = index.min(self.text.len());
while clamped > 0 && !self.text.is_char_boundary(clamped) {
clamped -= 1;
}
self.cursor_index = clamped;
self.clear_selection();
self
}
// Font size getters/setters
#[allow(unused)]
pub fn get_font_size(&self) -> f32 {
self.font_size
}
#[allow(unused)]
pub fn set_font_size(&mut self, size: f32) -> &mut Self {
self.font_size = size;
self
}
// Color getters/setters
#[allow(unused)]
pub fn get_text_color(&self) -> Color {
self.text_color
}
#[allow(unused)]
pub fn set_text_color(&mut self, color: Color) -> &mut Self {
self.text_color = color;
self
}
#[allow(unused)]
pub fn get_border_color(&self) -> Color {
self.border_color
}
#[allow(unused)]
pub fn set_border_color(&mut self, color: Color) -> &mut Self {
self.border_color = color;
self
}
#[allow(unused)]
pub fn get_background_color(&self) -> Color {
self.background_color
}
#[allow(unused)]
pub fn set_background_color(&mut self, color: Color) -> &mut Self {
self.background_color = color;
self
}
#[allow(unused)]
pub fn get_cursor_color(&self) -> Color {
self.cursor_color
}
#[allow(unused)]
pub fn set_cursor_color(&mut self, color: Color) -> &mut Self {
self.cursor_color = color;
self
}
// Font getter/setter
#[allow(unused)]
pub fn get_font(&self) -> Option<&Font> {
self.font.as_ref()
}
// Prompt text getters/setters
#[allow(unused)]
pub fn get_prompt(&self) -> Option<&String> {
self.prompt.as_ref()
}
#[allow(unused)]
pub fn set_prompt<T: Into<String>>(&mut self, prompt: T) -> &mut Self {
self.prompt = Some(prompt.into());
self
}
#[allow(unused)]
pub fn get_prompt_color(&self) -> Color {
self.prompt_color
}
#[allow(unused)]
pub fn set_prompt_color(&mut self, color: Color) -> &mut Self {
self.prompt_color = color;
self
}
// Key repeat settings getters/setters
#[allow(unused)]
pub fn get_key_repeat_delay(&self) -> f32 {
self.key_repeat_delay
}
#[allow(unused)]
pub fn set_key_repeat_delay(&mut self, delay: f32) -> &mut Self {
self.key_repeat_delay = delay;
self
}
#[allow(unused)]
pub fn get_key_repeat_rate(&self) -> f32 {
self.key_repeat_rate
}
#[allow(unused)]
pub fn set_key_repeat_rate(&mut self, rate: f32) -> &mut Self {
self.key_repeat_rate = rate;
self
}
// Convenience method to set both key repeat values at once
#[allow(unused)]
pub fn with_key_repeat_settings(&mut self, delay: f32, rate: f32) -> &mut Self {
self.key_repeat_delay = delay;
self.key_repeat_rate = rate;
self
}
// Enable/disable functionality
#[allow(unused)]
pub fn is_enabled(&self) -> bool {
self.enabled
}
#[allow(unused)]
pub fn set_enabled(&mut self, enabled: bool) -> &mut Self {
self.enabled = enabled;
if !enabled {
self.active = false; // Deactivate if disabled
self.clear_selection();
self.is_dragging_selection = false;
}
self
}
#[allow(unused)]
pub fn get_disabled_color(&self) -> Color {
self.disabled_color
}
#[allow(unused)]
pub fn set_disabled_color(&mut self, color: Color) -> &mut Self {
self.disabled_color = color;
self
}
/// Set the maximum number of characters allowed in the text input.
#[allow(unused)]
pub fn set_max_chars(&mut self, max: usize) -> &mut Self {
self.max_chars = Some(max);
self
}
/// Remove the character limit (unlimited input).
#[allow(unused)]
pub fn clear_max_chars(&mut self) -> &mut Self {
self.max_chars = None;
self
}
/// Restrict text input to only the characters in the provided whitelist.
#[allow(unused)]
pub fn set_allowed_chars<T: Into<String>>(&mut self, allowed_chars: T) -> &mut Self {
self.allowed_chars = Some(allowed_chars.into());
self.text = self.apply_text_constraints(&self.text);
self.ensure_indices_validity();
self
}
/// Remove any character whitelist and allow all characters again.
#[allow(unused)]
pub fn clear_allowed_chars(&mut self) -> &mut Self {
self.allowed_chars = None;
self
}
#[allow(unused)]
pub fn get_allowed_chars(&self) -> Option<&str> {
self.allowed_chars.as_deref()
}
// Primary method - both updates and draws the textbox
#[allow(unused)]
pub fn draw(&mut self) {
self.update_internal();
self.draw_internal();
}
// For cases when only drawing is needed without updating
#[allow(unused)]
pub fn draw_only(&self) {
self.draw_internal();
}
// For cases when only updating is needed without drawing
#[allow(unused)]
pub fn update_only(&mut self) {
self.update_internal();
}
// Now private - internal implementation only
fn update_internal(&mut self) {
// Guard against stale invalid UTF-8 byte indices before any string slicing.
self.ensure_indices_validity();
// Skip all interaction if disabled
if !self.enabled {
self.active = false;
self.cursor_visible = false;
self.clear_selection();
self.is_dragging_selection = false;
return;
}
if is_mouse_button_pressed(MouseButton::Left) {
let (mx, my) = mouse_position();
self.active = mx >= self.x && mx <= self.x + self.width && my >= self.y && my <= self.y + self.height;
if self.active {
let text_x = self.x + 5.0;
let text_y = self.y + 5.0;
let new_cursor = self.index_from_local_point(mx - text_x, my - text_y);
self.cursor_index = new_cursor;
self.ensure_indices_validity();
self.clear_selection();
self.is_dragging_selection = true;
self.drag_start_position = Some((mx, my));
self.cursor_visible = true;
self.cursor_timer = 0.0;
} else {
self.clear_selection();
self.is_dragging_selection = false;
}
}
if self.active && self.is_dragging_selection && is_mouse_button_down(MouseButton::Left) {
let (mx, my) = mouse_position();
let text_x = self.x + 5.0;
let text_y = self.y + 5.0;
// Only start selection if we've actually dragged
if let Some((start_x, start_y)) = self.drag_start_position {
let drag_distance = ((mx - start_x).powi(2) + (my - start_y).powi(2)).sqrt();
if drag_distance > 3.0 && self.selection_anchor.is_none() {
self.selection_anchor = Some(self.cursor_index);
}
}
self.cursor_index = self.index_from_local_point(mx - text_x, my - text_y);
self.ensure_indices_validity();
self.cursor_visible = true;
self.cursor_timer = 0.0;
}
if is_mouse_button_released(MouseButton::Left) {
self.is_dragging_selection = false;
self.drag_start_position = None;
}
if self.active {
let shortcut_mod_down = is_key_down(KeyCode::LeftControl)
|| is_key_down(KeyCode::RightControl)
|| is_key_down(KeyCode::LeftSuper)
|| is_key_down(KeyCode::RightSuper)
|| is_key_pressed(KeyCode::LeftControl)
|| is_key_pressed(KeyCode::RightControl)
|| is_key_pressed(KeyCode::LeftSuper)
|| is_key_pressed(KeyCode::RightSuper);
let mut consumed_shortcut = false;
if shortcut_mod_down {
if is_key_pressed(KeyCode::A) {
self.selection_anchor = Some(0);
self.cursor_index = self.text.len();
self.ensure_indices_validity();
consumed_shortcut = true;
} else if is_key_pressed(KeyCode::C) {
// Copy to clipboard
if let Some((s, e)) = self.get_selection_range() {
if s != e {
let selected_text = self.text[s..e].to_string();
copy_to_clipboard(selected_text);
}
}
consumed_shortcut = true;
} else if is_key_pressed(KeyCode::X) {
// Cut to clipboard
if let Some((s, e)) = self.get_selection_range() {
if s != e {
let selected_text = self.text[s..e].to_string();
copy_to_clipboard(selected_text);
self.delete_selection();
}
}
consumed_shortcut = true;
} else if is_key_pressed(KeyCode::V) {
#[cfg(target_arch = "wasm32")]
{
self.paste_requested = true;
paste_from_clipboard(); // Request async paste
consumed_shortcut = true;
}
#[cfg(not(target_arch = "wasm32"))]
{
let clipboard_text = paste_from_clipboard();
self.delete_selection();
for c in clipboard_text.chars() {
if self.can_insert_char(c) {
self.text.insert(self.cursor_index, c);
self.cursor_index += c.len_utf8();
}
}
self.ensure_indices_validity();
self.clear_selection();
consumed_shortcut = true;
}
}
}
if consumed_shortcut {
self.cursor_visible = true;
self.cursor_timer = 0.0;
self.last_key = None;
}
// Handle typing
if shortcut_mod_down {
// Avoid leaking Ctrl/Cmd shortcut letters into the input buffer.
while get_char_pressed().is_some() {}
} else if !consumed_shortcut {
while let Some(c) = get_char_pressed() {
if !c.is_control() {
self.delete_selection();
}
if !c.is_control() && self.can_insert_char(c) {
self.text.insert(self.cursor_index, c);
self.cursor_index += c.len_utf8();
self.ensure_indices_validity();
self.clear_selection();
}
}
}
// Handle Enter key for multiline
if self.multiline && is_key_pressed(KeyCode::Enter) {
self.delete_selection();
}
if self.multiline && is_key_pressed(KeyCode::Enter) && self.can_insert_char('\n') {
self.text.insert(self.cursor_index, '\n');
self.cursor_index += 1;
self.ensure_indices_validity();
self.clear_selection();
}
// Initial key presses
let key_delete_pressed = is_key_pressed(KeyCode::Delete);
let key_backspace_pressed = is_key_pressed(KeyCode::Backspace);
let key_left_pressed = is_key_pressed(KeyCode::Left);
let key_right_pressed = is_key_pressed(KeyCode::Right);
let key_up_pressed = is_key_pressed(KeyCode::Up);
let key_down_pressed = is_key_pressed(KeyCode::Down);
let shift_down = is_key_down(KeyCode::LeftShift) || is_key_down(KeyCode::RightShift);
// Handle initial key presses
if key_delete_pressed {
if !self.delete_selection() && self.cursor_index < self.text.len() {
if let Some((_, c)) = self.text[self.cursor_index..].char_indices().next() {
let char_len = c.len_utf8();
self.text.replace_range(self.cursor_index..self.cursor_index + char_len, "");
self.ensure_indices_validity();
}
}
self.last_key = Some(KeyCode::Delete);
self.key_repeat_timer = 0.0;
} else if key_backspace_pressed {
if !self.delete_selection() && self.cursor_index > 0 {
if let Some((prev_offset, _c)) = self.text[..self.cursor_index].char_indices().rev().next() {
self.text.replace_range(prev_offset..self.cursor_index, "");
self.cursor_index = prev_offset;
self.ensure_indices_validity();
}
}
self.last_key = Some(KeyCode::Backspace);
self.key_repeat_timer = 0.0;
} else if key_left_pressed && self.cursor_index > 0 {
let mut collapse_only = false;
if shift_down {
if self.selection_anchor.is_none() {
self.selection_anchor = Some(self.cursor_index);
}
} else if let Some((start, _end)) = self.get_selection_range() {