forked from vlang/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textbox.v
646 lines (605 loc) · 14.9 KB
/
textbox.v
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
// Copyright (c) 2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module ui
import gx
import gg
import strings
import time
enum SelectionDirection {
nil = 0
left_to_right
right_to_left
}
const (
placeholder_cfg = gx.TextCfg{
color: gx.gray
size: gg.default_font_size
align: gx.align_left
}
text_border_color = gx.rgb(177, 177, 177)
text_inner_border_color = gx.rgb(240, 240, 240)
text_border_accentuated_color = gx.rgb(255, 0, 0)
textbox_padding = 5
// selection_color = gx.rgb(226, 233, 241)
selection_color = gx.rgb(186, 214, 251)
)
type KeyDownFn fn(voidptr, voidptr, u32)
type KeyUpFn fn(voidptr, voidptr, u32)
type TextBoxChangeFn fn(string, voidptr)
[ref_only]
pub struct TextBox {
pub mut:
height int
width int
x int
y int
parent Layout
is_focused bool
// gg &gg.GG
ui &UI
//text string
text &string = voidptr(0)
max_len int
is_multi bool
placeholder string
cursor_pos int
is_numeric bool
is_password bool
sel_start int
sel_end int
last_x int
read_only bool
borderless bool
on_key_down KeyDownFn=KeyDownFn(0)
on_key_up KeyUpFn=KeyUpFn(0)
dragging bool
sel_direction SelectionDirection
border_accentuated bool
is_error &bool = voidptr(0)
on_change TextBoxChangeFn = TextBoxChangeFn(0)
mut:
is_typing bool
}
/*
struct Rect {
x int
y int
width int
height int
}
*/
pub struct TextBoxConfig {
width int
height int=22
min int
max int
val int
placeholder string
max_len int
is_numeric bool
is_password bool
read_only bool
is_multi bool
text &string = voidptr(0)
is_error &bool = voidptr(0)
is_focused bool
//is_error bool
borderless bool
on_key_down KeyDownFn
on_key_up KeyUpFn
on_change voidptr
on_return voidptr
border_accentuated bool=false
}
fn (mut tb TextBox) init(parent Layout) {
tb.parent = parent
ui := parent.get_ui()
tb.ui = ui
// return widget
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, tb_click, tb)
subscriber.subscribe_method(events.on_key_down, tb_key_down, tb)
subscriber.subscribe_method(events.on_key_up, tb_key_up, tb)
subscriber.subscribe_method(events.on_mouse_move, tb_mouse_move, tb)
}
pub fn textbox(c TextBoxConfig) &TextBox {
tb := &TextBox{
height: c.height
width: if c.width < 30 { 30 } else { c.width }
// sel_start: 0
placeholder: c.placeholder
// TODO is_focused: !c.parent.has_textbox // focus on the first textbox in the window by default
is_numeric: c.is_numeric
is_password: c.is_password
max_len: c.max_len
read_only: c.read_only
borderless: c.borderless
on_key_down: c.on_key_down
on_key_up: c.on_key_up
on_change: c.on_change
border_accentuated: c.border_accentuated
ui: 0
text:c.text
is_focused: c.is_focused
is_error: c.is_error
}
if c.text == 0 {
panic('textbox.text binding is not set')
}
return tb
}
//fn (t &TextBox) draw_inner_border() {
fn draw_inner_border(border_accentuated bool, gg &gg.Context, x, y, width, height int, is_error bool) {
if !border_accentuated {
color := if is_error { gx.rgb(255,0,0) } else { text_border_color }
gg.draw_empty_rect(x, y, width, height, color)
//gg.draw_empty_rect(t.x, t.y, t.width, t.height, color) //text_border_color)
// TODO this should be +-1, not 0.5, a bug in gg/opengl
gg.draw_empty_rect(0.5 + f32(x), 0.5 + f32(y), width - 1, height - 1, text_inner_border_color) // inner lighter border
}
else {
gg.draw_empty_rect(x, y, width, height, text_border_accentuated_color)
gg.draw_empty_rect(1.5 + f32(x), 1.5 + f32(y), width - 3, height - 3, text_border_accentuated_color) // inner lighter border
}
}
fn (mut b TextBox) set_pos(x, y int) {
b.x = x
b.y = y
}
fn (mut b TextBox) size() (int,int) {
return b.width,b.height
}
fn (mut b TextBox) propose_size(w, h int) (int,int) {
return b.width,b.height
}
fn (mut t TextBox) draw() {
text:=*(t.text)
t.ui.gg.draw_rect(t.x, t.y, t.width, t.height, gx.white)
if !t.borderless {
draw_inner_border(t.border_accentuated, t.ui.gg, t.x, t.y, t.width, t.height, t.is_error != 0 && *t.is_error)
}
width := if text.len == 0 { 0 } else { t.ui.gg.text_width(text) }
text_y := t.y + 2 // TODO off by 1px
mut skip_idx := 0
// Placeholder
if text == '' && t.placeholder != '' {
t.ui.gg.draw_text(t.x + textbox_padding, text_y, t.placeholder, placeholder_cfg)
}
// Text
else {
// Selection box
// if t.sel_start != 0 {
ustr := text.ustring()
if t.sel_start < t.sel_end && t.sel_start < ustr.len {
left := ustr.left(t.sel_start)
right := ustr.right(t.sel_end)
sel_width := width - t.ui.gg.text_width(right) - t.ui.gg.text_width(left)
x := t.ui.gg.text_width(left) + t.x + textbox_padding
t.ui.gg.draw_rect(x, t.y + 3, sel_width, t.height - 6, selection_color)
/*
sel_width := t.ui.gg.text_width(right) + 1
*/
}
// The text doesn't fit, find the largest substring we can draw
if width > t.width {
for i := text.len - 1; i >= 0; i-- {
if i >= text.len {
continue
}
if t.ui.gg.text_width(text[i..]) > t.width {
skip_idx = i + 3
break
}
}
t.ui.gg.draw_text_def(t.x + textbox_padding, text_y, text[skip_idx..])
}
else {
if t.is_password {
/*
for i in 0..t.text.len {
// TODO drawing multiple circles is broken
//t.ui.gg.draw_image(t.x + 5 + i * 12, t.y + 5, 8, 8, t.ui.circle_image)
}
*/
t.ui.gg.draw_text_def(t.x + textbox_padding, text_y, strings.repeat(`*`, text.len))
}
else {
t.ui.gg.draw_text_def(t.x + textbox_padding, text_y, text)
}
}
}
// Draw the cursor
if t.is_focused && !t.read_only && t.ui.show_cursor && t.sel_start == 0 &&
t.sel_end == 0 {
// no cursor in sel mode
mut cursor_x := t.x + textbox_padding
if t.is_password {
cursor_x += t.ui.gg.text_width(strings.repeat(`*`, t.cursor_pos))
}
else if skip_idx > 0 {
cursor_x += t.ui.gg.text_width(text[skip_idx..])
}
else if text.len > 0 {
// left := t.text[..t.cursor_pos]
left := text.ustring().left(t.cursor_pos)
cursor_x += t.ui.gg.text_width(left)
}
if text.len == 0 {
cursor_x = t.x + textbox_padding
}
// t.ui.gg.draw_line(cursor_x, t.y+2, cursor_x, t.y-2+t.height-1)//, gx.Black)
t.ui.gg.draw_rect(cursor_x, t.y + 3, 1, t.height - 6, gx.black) // , gx.Black)
}
}
fn tb_key_up(mut t TextBox, e &KeyEvent, window &Window) {
if !t.is_focused {
return
}
if t.on_key_up != voidptr(0) {
t.on_key_up(window.state, t, e.codepoint)
}
}
fn tb_key_down(mut t TextBox, e &KeyEvent, window &Window) {
text := *t.text
if !t.is_focused {
//println('textbox.key_down on an unfocused textbox, this should never happen')
return
}
if t.is_error != voidptr(0) {
unsafe {
*t.is_error = false
}
}
t.is_typing = true
if t.on_key_down != voidptr(0) {
t.on_key_down(window.state, t, e.codepoint)
}
t.ui.last_type_time = time.ticks() // TODO perf?
if e.codepoint != 0 {
if t.read_only {
return
}
if t.max_len > 0 && text.len >= t.max_len {
return
}
if byte(e.codepoint) in [`\t`, 127] {
// Do not print the tab character, delete etc
return
}
s := utf32_to_str(e.codepoint)
// if (t.is_numeric && (s.len > 1 || !s[0].is_digit() ) {
if t.is_numeric && (s.len > 1 || (!s[0].is_digit() && ((s[0] != `-`) || ((text.len > 0) && (t.cursor_pos > 0))))) {
return
}
t.insert(s)
if t.on_change != TextBoxChangeFn(0) {
t.on_change(*t.text, window.state)
}
//println('T "$s " $t.cursor_pos')
// t.text += s
// t.cursor_pos ++//= utf8_char_len(s[0])// s.le-112
return
}
// println(e.key)
// println('mods=$e.mods')
match e.key {
.backspace {
t.ui.show_cursor = true
if text != '' {
if t.cursor_pos == 0 {
return
}
u := text.ustring()
// Delete the entire selection
if t.sel_start < t.sel_end {
unsafe {
*t.text = u.left(t.sel_start) + u.right(t.sel_end)
}
t.cursor_pos = t.sel_start
t.sel_start = 0
t.sel_end = 0
}
else if e.mods in [.super, .ctrl] {
// Delete until previous whitespace
mut i := t.cursor_pos
for {
if i > 0 {
i--
}
if text[i].is_space() || i == 0 {
unsafe {
//*t.text = u.left(i) + u.right(t.cursor_pos)
}
break
}
}
t.cursor_pos = i
}
else {
// Delete just one character
unsafe {
*t.text = u.left(t.cursor_pos - 1) + u.right(t.cursor_pos)
}
t.cursor_pos--
}
// u.free() // TODO remove
// t.text = t.text[..t.cursor_pos - 1] + t.text[t.cursor_pos..]
}
if t.on_change != TextBoxChangeFn(0) {
//t.on_change(*t.text, window.state)
}
}
.delete {
t.ui.show_cursor = true
if t.cursor_pos == text.len || text == '' {
return
}
u := text.ustring()
unsafe {
*t.text = u.left(t.cursor_pos) + u.right(t.cursor_pos + 1)
}
// t.text = t.text[..t.cursor_pos] + t.text[t.cursor_pos + 1..]
// u.free() // TODO remove
if t.on_change != TextBoxChangeFn(0) {
//t.on_change(*t.text, window.state)
}
}
.left {
if t.sel(e.mods, e.key) {
return
}
if t.sel_end > 0 {
t.cursor_pos = t.sel_start + 1
}
t.sel_start = 0
t.sel_end = 0
t.ui.show_cursor = true // always show cursor when moving it (left, right, backspace etc)
t.cursor_pos--
if t.cursor_pos <= 0 {
t.cursor_pos = 0
}
}
.right {
if t.sel(e.mods, e.key) {
return
}
if t.sel_start > 0 {
t.cursor_pos = t.sel_end - 1
}
t.sel_end = 0
t.sel_start = 0
t.ui.show_cursor = true
t.cursor_pos++
if t.cursor_pos > text.len {
t.cursor_pos = text.len
}
}
.a {
if e.mods in [.super, .ctrl] {
t.sel_start = 0
t.sel_end = text.ustring().len - 1
}
}
.v {
if e.mods in [.super, .ctrl] {
t.insert(t.ui.clipboard.paste())
}
}
.tab {
t.ui.show_cursor = true
/*TODO if t.parent.just_tabbed {
t.parent.just_tabbed = false
return
} */
//println('TAB $t.id')
/* if e.mods == .shift {
t.parent.focus_previous()
}
else {
t.parent.focus_next()
} */
}
else {}
}
}
fn (mut t TextBox) set_sel(sel_start, sel_end int, key Key) {
if t.sel_direction == .right_to_left {
t.sel_start = sel_start
t.sel_end = sel_end
}
else {
t.sel_start = sel_end
t.sel_end = sel_start
}
}
fn (mut t TextBox) sel(mods KeyMod, key Key) bool {
mut sel_start := if t.sel_direction == .right_to_left { t.sel_start } else { t.sel_end }
mut sel_end := if t.sel_direction == .right_to_left { t.sel_end } else { t.sel_start }
text := *t.text
if mods == int(KeyMod.shift) + int(KeyMod.ctrl) {
mut i := t.cursor_pos
if sel_start > 0 {
i = if key == .left { sel_start - 1 } else { sel_start + 1 }
}
else if sel_start == 0 && sel_end > 0 {
i = 0
}
else {
t.sel_direction = if key == .left { SelectionDirection.right_to_left } else { SelectionDirection.left_to_right }
}
sel_end = t.cursor_pos
for {
if key == .left && i > 0 {
i--
}
else if key == .right && i < t.text.len {
i++
}
if i == 0 {
sel_start = 0
break
}
else if i == text.len {
sel_start = t.text.len
break
}
else if text[i].is_space() {
sel_start = if t.sel_direction == .right_to_left { i + 1 } else { i }
break
}
}
t.set_sel(sel_start, sel_end, key)
return true
}
if mods == .shift {
if (t.sel_direction == .right_to_left && sel_start == 0 && sel_end > 0) || (t.sel_direction == .left_to_right && sel_end == t.text.len) {
return true
}
if sel_start <= 0 {
sel_end = t.cursor_pos
sel_start = if key == .left { t.cursor_pos - 1 } else { t.cursor_pos + 1 }
t.sel_direction = if key == .left { SelectionDirection.right_to_left } else { SelectionDirection.left_to_right }
}
else {
sel_start = if key == .left { sel_start - 1 } else { sel_start + 1 }
}
t.set_sel(sel_start, sel_end, key)
return true
}
return false
}
fn (t &TextBox) point_inside(x, y f64) bool {
return x >= t.x && x <= t.x + t.width && y >= t.y && y <= t.y + t.height
}
fn tb_mouse_move(mut t TextBox, e &MouseEvent, zzz voidptr) {
if !t.point_inside(e.x, e.y) {
return
}
if t.dragging {
x := e.x - t.x - textbox_padding
reverse := x - t.last_x < 0
if t.sel_start <= 0 {
t.sel_start = t.cursor_pos
}
t.last_x = x
mut prev_width := 0
ustr := t.text.ustring()
for i in 1 .. ustr.len {
width := t.ui.gg.text_width(ustr.left(i))
if prev_width <= x && x <= width {
if i < t.sel_start && t.sel_end < t.sel_start {
t.sel_end = t.sel_start
t.sel_start = i
return
}
if reverse {
t.sel_start = i
}
else {
t.sel_end = i
}
return
}
prev_width = width
}
if reverse {
t.sel_start = 0
}
else {
t.sel_end = t.text.len
}
}
}
fn tb_click(mut t TextBox, e &MouseEvent, zzz voidptr) {
if !t.point_inside(e.x, e.y) {
t.dragging = false
return
}
if !t.dragging && e.action == 1 {
t.sel_start = 0
t.sel_end = 0
}
t.dragging = e.action == 1
t.ui.show_cursor = true
t.focus()
if *t.text == '' {
return
}
// Calculate cursor position from x
x := e.x - t.x - textbox_padding
if x <= 0 {
t.cursor_pos = 0
return
}
mut prev_width := 0
ustr := t.text.ustring()
for i in 1 .. ustr.len {
// width := t.ui.gg.text_width(t.text[..i])
width := t.ui.gg.text_width(ustr.left(i))
if prev_width <= x && x <= width {
t.cursor_pos = i
return
}
prev_width = width
}
t.cursor_pos = t.text.len
}
pub fn (mut t TextBox) focus() {
if t.is_focused {
return
}
parent := t.parent
parent.unfocus_all()
t.is_focused = true
}
fn (t &TextBox) is_focused() bool {
return t.is_focused
}
fn (mut t TextBox) unfocus() {
t.is_focused = false
t.sel_start = 0
t.sel_end = 0
}
fn (mut t TextBox) update() {
t.cursor_pos = t.text.ustring().len
}
pub fn (mut t TextBox) hide() {}
pub fn (mut t TextBox) set_text(s string) {
//t.text = s
//t.update()
}
//pub fn (mut t TextBox) on_change(func voidptr) {
//}
pub fn (mut t TextBox) on_return(func voidptr) {
}
pub fn (mut t TextBox) insert(s string) {
mut ustr := t.text.ustring()
old_len := ustr.len
// Remove the selection
if t.sel_start < t.sel_end {
unsafe {
*t.text = ustr.left(t.sel_start) + s + ustr.right(t.sel_end + 1)
}
}
else {
// Insert one character
// t.text = t.text[..t.cursor_pos] + s + t.text[t.cursor_pos..]
unsafe {
*t.text = ustr.left(t.cursor_pos) + s + ustr.right(t.cursor_pos)
}
ustr = t.text.ustring()
// The string is too long
if t.max_len > 0 && ustr.len >= t.max_len {
// t.text = t.text.limit(t.max_len)
unsafe {
*t.text = ustr.left(t.max_len)
}
ustr = t.text.ustring()
}
}
// t.cursor_pos += t.text.len - old_len
t.cursor_pos += ustr.len - old_len
t.sel_start = 0
t.sel_end = 0
}