forked from vlang/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.v
1184 lines (1094 loc) · 26.9 KB
/
window.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
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
// Copyright (c) 2020-2021 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 clipboard
import eventbus
import time
import math
const (
default_window_color = gx.rgb(236, 236, 236)
default_font_size = 13
)
pub type ClickFn = fn (e MouseEvent, window &Window)
pub type KeyFn = fn (e KeyEvent, func voidptr)
pub type ScrollFn = fn (e ScrollEvent, window &Window)
pub type MouseMoveFn = fn (e MouseMoveEvent, window &Window)
pub type ResizeFn = fn (w int, h int, window &Window)
pub type InitFn = fn (window &Window)
[heap]
pub struct Window {
pub mut:
// pub:
ui &UI = voidptr(0)
// glfw_obj &glfw.Window = voidptr(0)
children []Widget
child_window &Window = voidptr(0)
parent_window &Window = voidptr(0)
has_textbox bool // for initial focus
tab_index int
just_tabbed bool
state voidptr
draw_fn DrawFn
title string
mx f64
my f64
width int
height int
bg_color gx.Color
init_fn InitFn
click_fn ClickFn
mouse_down_fn ClickFn
mouse_up_fn ClickFn
scroll_fn ScrollFn
resize_fn ResizeFn
key_down_fn KeyFn
char_fn KeyFn
mouse_move_fn MouseMoveFn
eventbus &eventbus.EventBus = eventbus.new()
// resizable has limitation https://github.com/vlang/ui/issues/231
resizable bool // currently only for events.on_resized not modify children
mode WindowSizeType
root_layout Layout
dpi_scale f32
// saved origin sizes
orig_width int
orig_height int
touch TouchInfo
// Text Config
text_cfg gx.TextCfg
// drag
drag_activated bool
drag_widget Widget
drag_start_x f64
drag_start_y f64
drag_pos_x f64
drag_pos_y f64
drag_time time.Time
// themes
color_themes ColorThemes
// widgets register
widgets map[string]Widget
widgets_counts map[string]int
}
pub struct WindowConfig {
pub:
width int
height int
font_path string
title string
always_on_top bool
state voidptr
draw_fn DrawFn
bg_color gx.Color = ui.default_window_color
on_init InitFn
on_click ClickFn
on_mouse_down ClickFn
on_mouse_up ClickFn
on_key_down KeyFn
on_char KeyFn
on_scroll ScrollFn
on_resize ResizeFn
on_mouse_move MouseMoveFn
children []Widget
custom_bold_font_path string
native_rendering bool
resizable bool
mode WindowSizeType
// Text Config
lines int = 10
}
/*
pub fn window2(cfg WindowConfig) &Window {
return window(cfg, cfg.children)
}
*/
fn C.sapp_mouse_locked() bool
fn on_event(e &gg.Event, mut window Window) {
/*
if false && e.typ != .mouse_move {
print('window.on_event() $e.typ ') // code=$e.char_code')
if C.sapp_mouse_locked() {
println('locked')
} else {
println('unlocked')
}
}
*/
// window.ui.needs_refresh = true
// window.refresh()
$if macos {
if window.ui.gg.native_rendering {
if e.typ in [.key_down, .mouse_scroll, .mouse_up] {
C.darwin_window_refresh()
} else {
C.darwin_window_refresh()
}
}
}
window.ui.ticks = 0
// window.ui.ticks_since_refresh = 0
// println("on_event: $e.typ")
match e.typ {
.mouse_down {
// println("mouse down")
window_mouse_down(e, mut window.ui)
// IMPORTANT: No more need since inside window_handle_tap:
// window_click(e, window.ui)
// touch like
window.touch.start = {
pos: {
x: int(e.mouse_x / window.ui.gg.scale)
y: int(e.mouse_y / window.ui.gg.scale)
}
time: time.now()
}
}
.mouse_up {
// println('mouseup')
window_mouse_up(e, mut window.ui)
// NOT THERE since already done
// touch-like
window.touch.end = {
pos: {
x: int(e.mouse_x / window.ui.gg.scale)
y: int(e.mouse_y / window.ui.gg.scale)
}
time: time.now()
}
window_touch_tap_and_swipe(e, window.ui)
}
.key_down {
// println('key down')
window_key_down(e, window.ui)
}
.char {
// println('char')
window_char(e, window.ui)
}
.mouse_scroll {
window_scroll(e, window.ui)
}
.mouse_move {
// println('mod=$e.modifiers $e.num_touches $e.key_repeat $e.mouse_button')
window_mouse_move(e, window.ui)
}
.resized, .restored, .resumed {
window_resize(e, window.ui)
}
.touches_began {
if e.num_touches > 0 {
t := e.touches[0]
window.touch.start = {
pos: {
x: int(t.pos_x / window.ui.gg.scale)
y: int(t.pos_y / window.ui.gg.scale)
}
time: time.now()
}
window.touch.button = 0
window_touch_down(e, window.ui)
// println("touch BEGIN: ${window.touch.start} $e")
}
}
.touches_ended {
if e.num_touches > 0 {
t := e.touches[0]
window.touch.end = {
pos: {
x: int(t.pos_x / window.ui.gg.scale)
y: int(t.pos_y / window.ui.gg.scale)
}
time: time.now()
}
window.touch.button = -1
// println("touch END: ${window.touch.end} $window.touch.button")
window_touch_up(e, window.ui)
window_touch_tap_and_swipe(e, window.ui)
}
}
.touches_moved {
if e.num_touches > 0 {
t := e.touches[0]
window.touch.move = {
pos: {
x: int(t.pos_x / window.ui.gg.scale)
y: int(t.pos_y / window.ui.gg.scale)
}
time: time.now()
}
// println("touch move: ${window.touch.move} $window.touch.button")
window_touch_move(e, window.ui)
}
}
else {}
}
/*
if e.typ == .key_down {
game.key_down(e.key_code)
}
*/
}
fn gg_init(mut window Window) {
window.dpi_scale = gg.dpi_scale()
window_size := gg.window_size_real_pixels()
w := int(f32(window_size.width) / window.dpi_scale)
h := int(f32(window_size.height) / window.dpi_scale)
window.width, window.height = w, h
window.orig_width, window.orig_height = w, h
// println('gg_init: $w, $h')
for _, mut child in window.children {
// println('init $child.type_name()')
child.init(window)
window.register_child(*child)
}
// refresh the layout
window.update_layout()
if window.init_fn != voidptr(0) {
window.init_fn(window)
}
}
pub fn window(cfg WindowConfig, children []Widget) &Window {
/*
println('window()')
defer {
println('end of window()')
}
*/
mut width, mut height := cfg.width, cfg.height
mut resizable := cfg.resizable
mut fullscreen := false
mut sc_size := gg.Size{width, height}
// before fixing gg_screen_size() for other OS: Linux, Windows
$if macos {
sc_size = gg.screen_size()
}
match cfg.mode {
.max_size {
if sc_size.width > 0 {
width, height = sc_size.width, sc_size.height
resizable = true
}
}
.fullscreen {
if sc_size.width > 10 {
width, height = sc_size.width, sc_size.height
}
fullscreen = true
}
.resizable {
resizable = true
}
else {}
}
// default text_cfg
// m := f32(math.min(width, height))
mut text_cfg := gx.TextCfg{
color: gx.rgb(38, 38, 38)
align: gx.align_left
// size: int(m / cfg.lines)
}
// C.printf('window() state =%p \n', cfg.state)
mut window := &Window{
state: cfg.state
draw_fn: cfg.draw_fn
title: cfg.title
bg_color: cfg.bg_color
width: width
height: height
// orig_width: width // 800
// orig_height: height // 600
children: children
init_fn: cfg.on_init
click_fn: cfg.on_click
key_down_fn: cfg.on_key_down
char_fn: cfg.on_char
scroll_fn: cfg.on_scroll
mouse_move_fn: cfg.on_mouse_move
mouse_down_fn: cfg.on_mouse_down
mouse_up_fn: cfg.on_mouse_up
resizable: resizable
mode: cfg.mode
resize_fn: cfg.on_resize
text_cfg: text_cfg
}
// register default color themes
window.register_default_color_themes()
mut font_path := ''
$if android {
font_path = 'fonts/RobotoMono-Regular.ttf'
} $else {
font_path = if cfg.font_path == '' { gg.system_font_path() } else { cfg.font_path }
}
gcontext := gg.new_context(
width: width
height: height
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: cfg.title
resizable: resizable
fullscreen: fullscreen
frame_fn: if cfg.native_rendering { native_frame } else { frame }
// native_frame_fn: native_frame
event_fn: on_event
user_data: window
font_path: font_path
custom_bold_font_path: cfg.custom_bold_font_path
init_fn: gg_init
// keydown_fn: window_key_down
// char_fn: window_char
bg_color: cfg.bg_color // gx.rgb(230,230,230)
// window_state: ui
native_rendering: cfg.native_rendering
ui_mode: true
)
// wsize := gcontext.window.get_window_size()
// fsize := gcontext.window.get_framebuffer_size()
// scale := 2 //if wsize.width == fsize.width { 1 } else { 2 } // detect high dpi displays
mut ui_ctx := &UI{
gg: gcontext
clipboard: clipboard.new()
}
ui_ctx.load_icos()
/*
ui_ctx.gg.window.set_user_ptr(ui_ctx)
ui_ctx.gg.window.onkeydown(window_key_down)
ui_ctx.gg.window.onchar(window_char)
ui_ctx.gg.window.onmousemove(window_mouse_move)
ui_ctx.gg.window.on_click(window_click)
ui_ctx.gg.window.on_resize(window_resize)
ui_ctx.gg.window.on_scroll(window_scroll)
*/
window.ui = ui_ctx
/*
mut window := &Window{
state: cfg.state
ui: ui_ctx
//glfw_obj: ui_ctx.gg.window
draw_fn: cfg.draw_fn
title: cfg.title
bg_color: cfg.bg_color
width: cfg.width
height: cfg.height
children: children
click_fn: cfg.on_click
key_down_fn: cfg.on_key_down
scroll_fn: cfg.on_scroll
}
*/
// q := int(window)
// println('created window $q.hex()')
return window
}
pub fn child_window(cfg WindowConfig, mut parent_window Window, children []Widget) &Window {
// q := int(parent_window)
// println('child_window() parent=$q.hex()')
mut window := &Window{
parent_window: parent_window
// state: parent_window.state
state: cfg.state
ui: parent_window.ui
// glfw_obj: parent_window.ui.gg.window
draw_fn: cfg.draw_fn
title: cfg.title
bg_color: cfg.bg_color
width: cfg.width
height: cfg.height
children: children
click_fn: cfg.on_click
}
parent_window.child_window = window
for _, mut child in window.children {
// using `parent_window` here so that all events handled by the main window are redirected
// to parent_window.child_window.child
child.init(parent_window)
}
// window.set_cursor()
return window
}
/*
fn window_mouse_move(glfw_wnd voidptr, x, y f64) {
ui := &UI(glfw.get_window_user_pointer(glfw_wnd))
mut window := ui.window
x0,y0 := glfw.get_cursor_pos(glfw_wnd)
window.mx = int(x0)
window.my = int(y0)
e := MouseEvent{
x: int(x0)
y: int(y0)
}
/* if window.mouse_move_fn != 0 {
window.mouse_move_fn(e, &ui.window)
}
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.mouse_move(e)
}
} */
window.eventbus.publish(events.on_mouse_move, &window, e)
}
*/
// fn window_resize(glfw_wnd voidptr, width int, height int) {
fn window_resize(event gg.Event, ui &UI) {
mut window := ui.window
$if resize ? {
println('window resize ($event.window_width ,$event.window_height)')
}
if !window.resizable {
return
}
window.resize(event.window_width, event.window_height)
window.eventbus.publish(events.on_resize, window, voidptr(0))
if window.resize_fn != voidptr(0) {
window.resize_fn(event.window_width, event.window_height, window)
}
}
fn window_mouse_move(event gg.Event, ui &UI) {
mut window := ui.window
e := MouseMoveEvent{
x: event.mouse_x / ui.gg.scale
y: event.mouse_y / ui.gg.scale
mouse_button: int(event.mouse_button)
}
if window.drag_activated {
$if drag ? {
println('drag child ($e.x, $e.y)')
}
drag_child(mut window, e.x, e.y)
}
if window.mouse_move_fn != voidptr(0) {
window.mouse_move_fn(e, window)
}
window.eventbus.publish(events.on_mouse_move, window, e)
}
fn window_scroll(event gg.Event, ui &UI) {
window := ui.window
// println('title =$window.title')
e := ScrollEvent{
x: event.scroll_x
y: event.scroll_y
}
if window.scroll_fn != voidptr(0) {
window.scroll_fn(e, window)
}
window.eventbus.publish(events.on_scroll, window, e)
}
fn window_mouse_down(event gg.Event, mut ui UI) {
window := ui.window
e := MouseEvent{
action: .down
x: int(event.mouse_x / ui.gg.scale)
y: int(event.mouse_y / ui.gg.scale)
button: MouseButton(event.mouse_button)
mods: KeyMod(event.modifiers)
}
if int(event.mouse_button) < 3 {
ui.btn_down[int(event.mouse_button)] = true
}
if window.mouse_down_fn != voidptr(0) { // && action == voidptr(0) {
// window.mouse_down_fn(e, window)
}
/*
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.click(e)
}
}
*/
if window.child_window != 0 {
// If there's a child window, use it, so that the widget receives correct user pointer
window.eventbus.publish(events.on_mouse_down, window.child_window, e)
} else {
window.eventbus.publish(events.on_mouse_down, window, e)
}
}
fn window_mouse_up(event gg.Event, mut ui UI) {
mut window := ui.window
e := MouseEvent{
action: .up
x: int(event.mouse_x / ui.gg.scale)
y: int(event.mouse_y / ui.gg.scale)
button: MouseButton(event.mouse_button)
mods: KeyMod(event.modifiers)
}
if int(event.mouse_button) < 3 {
ui.btn_down[int(event.mouse_button)] = false
}
if window.drag_activated {
$if drag ? {
println('drag child ($e.x, $e.y)')
}
drop_child(mut window)
}
if window.child_window == 0 && window.mouse_up_fn != voidptr(0) { // && action == voidptr(0) {
window.mouse_up_fn(e, window)
}
/*
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.click(e)
}
}
*/
if window.child_window != 0 {
// If there's a child window, use it, so that the widget receives correct user pointer
window.eventbus.publish(events.on_mouse_up, window.child_window, e)
// window.eventbus.unsubscribe()
} else {
window.eventbus.publish(events.on_mouse_up, window, e)
}
}
fn window_touch_tap_and_swipe(event gg.Event, ui &UI) {
window := ui.window
s, e := window.touch.start, window.touch.end
adx, ady := math.abs(e.pos.x - s.pos.x), math.abs(e.pos.y - s.pos.y)
if math.max(adx, ady) < 10 {
window_touch_tap(event, ui)
} else {
window_touch_swipe(event, ui)
}
}
fn window_touch_tap(event gg.Event, ui &UI) {
window := ui.window
e := MouseEvent{
action: MouseAction.up // if event.typ == .mouse_up { MouseAction.up } else { MouseAction.down }
x: window.touch.end.pos.x
y: window.touch.end.pos.y
// button: MouseButton(event.mouse_button)
// mods: KeyMod(event.modifiers)
}
if window.click_fn != voidptr(0) && window.child_window == 0 { // && action == voidptr(0) {
window.click_fn(e, window)
}
if window.child_window != 0 {
// If there's a child window, use it, so that the widget receives correct user pointer
window.eventbus.publish(events.on_click, window.child_window, e)
} else {
window.eventbus.publish(events.on_click, window, e)
}
}
fn window_touch_swipe(event gg.Event, ui &UI) {
// window := ui.window
}
fn window_touch_down(event gg.Event, ui &UI) {
window := ui.window
e := MouseEvent{
x: window.touch.start.pos.x
y: window.touch.start.pos.y
}
if window.mouse_down_fn != voidptr(0) {
window.mouse_down_fn(e, window)
}
window.eventbus.publish(events.on_touch_down, window, e)
}
fn window_touch_up(event gg.Event, ui &UI) {
window := ui.window
e := MouseEvent{
x: window.touch.end.pos.x
y: window.touch.end.pos.y
}
if window.mouse_up_fn != voidptr(0) {
window.mouse_up_fn(e, window)
}
window.eventbus.publish(events.on_touch_up, window, e)
}
fn window_touch_move(event gg.Event, ui &UI) {
window := ui.window
e := MouseMoveEvent{
x: f64(window.touch.move.pos.x)
y: f64(window.touch.move.pos.y)
mouse_button: window.touch.button
}
if window.mouse_move_fn != voidptr(0) {
window.mouse_move_fn(e, window)
}
window.eventbus.publish(events.on_touch_move, window, e)
}
fn window_click(event gg.Event, ui &UI) {
window := ui.window
// println("typ $event.typ")
e := MouseEvent{
action: if event.typ == .mouse_up { MouseAction.up } else { MouseAction.down }
x: int(event.mouse_x / ui.gg.scale)
y: int(event.mouse_y / ui.gg.scale)
button: MouseButton(event.mouse_button)
mods: KeyMod(event.modifiers)
}
if window.click_fn != voidptr(0) { // && action == voidptr(0) {
window.click_fn(e, window)
}
/*
for child in window.children {
inside := child.point_inside(x, y) // TODO if ... doesn't work with interface calls
if inside {
child.click(e)
}
}
*/
if window.child_window != 0 {
// If there's a child window, use it, so that the widget receives correct user pointer
window.eventbus.publish(events.on_click, window.child_window, e)
} else {
window.eventbus.publish(events.on_click, window, e)
}
}
fn window_key_down(event gg.Event, ui &UI) {
// println('keydown char=$event.char_code')
mut window := ui.window
// C.printf('g child=%p\n', child)
e := KeyEvent{
key: Key(event.key_code)
mods: KeyMod(event.modifiers)
codepoint: 0 // event.char_code
// code: code
// action: action
// mods: mod
}
if e.key == .escape {
println('escape')
}
if e.key == .escape && window.child_window != 0 {
// Close the child window on Escape
window.child_window = &Window(0)
}
if window.key_down_fn != voidptr(0) {
window.key_down_fn(e, window.state)
}
// TODO
if true { // action == 2 || action == 1 {
window.eventbus.publish(events.on_key_down, window, e)
} else {
window.eventbus.publish(events.on_key_up, window, e)
}
/*
for child in window.children {
is_focused := child.is_focused()
if !is_focused {
continue
}
child.key_down()
}
*/
}
// fn window_char(glfw_wnd voidptr, codepoint u32) {
fn window_char(event gg.Event, ui &UI) {
// println('keychar char=$event.char_code')
window := ui.window
e := KeyEvent{
codepoint: event.char_code
mods: KeyMod(event.modifiers)
}
if window.key_down_fn != voidptr(0) {
window.key_down_fn(e, window.state)
}
window.eventbus.publish(events.on_key_down, window, e)
if window.char_fn != voidptr(0) {
window.char_fn(e, window.state)
}
// window.eventbus.publish(events.on_char, window, e)
window.eventbus.publish(events.on_char, window, e)
/*
for child in window.children {
is_focused := child.is_focused()
if !is_focused {
continue
}
child.key_down()
}
*/
}
fn (mut w Window) focus_next() {
mut doit := false
for mut child in w.children {
// Focus on the next widget
if doit {
child.focus()
break
}
is_focused := child.is_focused()
if is_focused {
doit = true
}
}
w.just_tabbed = true
}
fn (w &Window) focus_previous() {
for i, mut child in w.children {
is_focused := child.is_focused()
if is_focused && i > 0 {
mut prev := w.children[i - 1]
prev.focus()
// w.children[i - 1].focus()
}
}
}
pub fn (w &Window) set_cursor(cursor Cursor) {
// glfw.set_cursor(.ibeam)
// w.glfw_obj.set_cursor(.ibeam)
}
pub fn (w &Window) close() {
}
pub fn (mut w Window) refresh() {
// println('ui: window.refres()')
// w.ui.needs_refresh = true
w.ui.gg.refresh_ui()
$if macos {
C.darwin_window_refresh()
}
}
pub fn (w &Window) onmousedown(cb voidptr) {
}
pub fn (w &Window) onkeydown(cb voidptr) {
}
pub fn (mut w Window) on_click(func ClickFn) {
w.click_fn = func
}
pub fn (mut w Window) on_mousemove(func MouseMoveFn) {
w.mouse_move_fn = func
}
pub fn (mut w Window) on_scroll(func ScrollFn) {
w.scroll_fn = func
}
pub fn (w &Window) mouse_inside(x int, y int, width int, height int) bool {
return false
}
pub fn (w &Window) focus() {
}
pub fn (w &Window) always_on_top(val bool) {
}
// TODO remove this once interfaces are smarter
fn foo(w Widget) {
}
fn foo2(l Layout) {
}
fn bar() {
// foo(&TextBox{
// ui: 0
// })
// foo(&Button{
// ui: 0
// })
// foo(&ProgressBar{
// ui: 0
// })
// foo(&Slider{
// ui: 0
// })
// foo(&CheckBox{
// ui: 0
// })
// foo(&Label{
// ui: 0
// })
// foo(&Radio{
// ui: 0
// })
// foo(&Picture{
// ui: 0
// })
// foo(&Canvas{})
// foo(&Menu{
// ui: 0
// })
// foo(&Dropdown{
// ui: 0
// })
foo(&Transition{
ui: 0
animated_value: 0
})
// foo(&Stack{
// ui: 0
// })
// foo(&Switch{
// ui: 0
// })
// foo(&Rectangle{
// ui: 0
// })
// foo(&Group{
// ui: 0
// })
// foo(&Grid{
// ui: 0
// })
}
fn (w &Window) draw() {
}
fn frame(mut w Window) {
// Commented to make timer.v fluid and working on android at the same time
// if !w.ui.needs_refresh {
// // Draw 3 more frames after the "stop refresh" command
// w.ui.ticks++
// if w.ui.ticks > 3 {
// return
// }
// }
// println('frame() needs_refresh=$w.ui.needs_refresh $w.ui.ticks nr children=$w.children.len')
// game.frame_sw.restart()
// game.ft.flush()
w.ui.gg.begin()
// draw_scene()
mut children := if w.child_window == 0 { w.children } else { w.child_window.children }
for mut child in children {
child.draw()
}
w.ui.gg.end()
}
fn native_frame(mut w Window) {
// println('ui.native_frame()')
/*
if !w.ui.needs_refresh {
// Draw 3 more frames after the "stop refresh" command
w.ui.ticks++
if w.ui.ticks > 3 {
return
}
}
*/
mut children := if w.child_window == 0 { w.children } else { w.child_window.children }
// if w.child_window == 0 {
// Render all widgets, including Canvas
for mut child in children {
child.draw()
}
//}
// w.ui.needs_refresh = false
}
// fn C.sapp_macos_get_window() voidptr
fn C.sapp_set_window_title(&char)
// #define cls objc_getClass
// #define sel sel_getUid
#define objc_msg ((id (*)(id, SEL, ...))objc_msgSend)
#define objc_cls_msg ((id (*)(Class, SEL, ...))objc_msgSend)
fn C.objc_msg()
fn C.objc_cls_msg()
fn C.sel_getUid()
fn C.objc_getClass()
pub fn (mut w Window) set_title(title string) {
w.title = title
/*
$if macos {
x := C.sapp_macos_get_window()
C.objc_msg(x, C.sel_getUid("setTitle:"), C.objc_cls_msg(C.objc_getClass("NSString"),
C.sel_getUid("stringWithUTF8String:"),"Pure C App"))
println('SETTING')
#[nsw setTitlee:"test string"];
}
*/
C.sapp_set_window_title(title.str)
}
// Layout Interface Methods
pub fn (w &Window) get_ui() &UI {
return w.ui
}
pub fn (w &Window) get_state() voidptr {
return w.state
}
pub fn (w &Window) get_subscriber() &eventbus.Subscriber {
return w.eventbus.subscriber
}
pub fn (w &Window) size() (int, int) {
return w.width, w.height
}
fn (mut window Window) resize(w int, h int) {
window.width, window.height = w, h
window.ui.gg.resize(w, h)
for mut child in window.children {
if mut child is Stack {
child.resize(w, h)
}
}
}
pub fn (window &Window) unfocus_all() {
// println('window.unfocus_all()')
for mut child in window.children {
child.unfocus()
}
}
pub fn (w &Window) get_children() []Widget {
return w.children
}
// Experimental: attempt to register child to get it by id from window
fn (mut w Window) register_child(child Widget) {