-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.v
1541 lines (1419 loc) · 36.1 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-2022 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
import os.font
fn C.sapp_mouse_locked() bool
// 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()
const (
default_window_color = gx.rgb(236, 236, 236)
default_font_size = 13
)
pub type WindowFn = fn (window &Window)
pub type ResizeFn = fn (w int, h int, window &Window)
pub type KeyFn = fn (e KeyEvent, window &Window)
pub type ClickFn = fn (e MouseEvent, window &Window)
pub type MouseMoveFn = fn (e MouseMoveEvent, window &Window)
pub type ScrollFn = fn (e ScrollEvent, window &Window)
[heap]
pub struct Window {
id string = '_window_'
pub mut:
// pub:
ui &UI = voidptr(0)
children []Widget
child_window &Window = voidptr(0)
parent_window &Window = voidptr(0)
has_textbox bool // for initial focus
just_tabbed bool
state voidptr
title string
width int
height int
click_fn ClickFn
mouse_down_fn ClickFn
mouse_up_fn ClickFn
files_droped_fn ClickFn
swipe_fn ClickFn
mouse_move_fn MouseMoveFn
scroll_fn ScrollFn
key_down_fn KeyFn
char_fn KeyFn
resize_fn ResizeFn
iconified_fn WindowFn
restored_fn WindowFn
quit_requested_fn WindowFn
suspended_fn WindowFn
resumed_fn WindowFn
on_init WindowFn
on_draw WindowFn
eventbus &eventbus.EventBus = eventbus.new()
resizable bool // resizable has limitation https://github.com/vlang/ui/issues/231
mode WindowSizeType
root_layout Layout = empty_stack
dpi_scale f32
// saved origin sizes
orig_width int
orig_height int
touch TouchInfo
bg_color gx.Color
// Text Config
text_cfg gx.TextCfg
// themes
color_themes map[string]ColorTheme
// widgets register
widgets map[string]Widget
widgets_counts map[string]int
// drag
dragger Dragger = Dragger{}
// tooltip
tooltip Tooltip = Tooltip{}
widgets_tooltip []Widget
tooltips []TooltipMessage
// with message
native_message bool
// focus stuff
do_focus bool
locked_focus string
// event manager
evt_mngr EventMngr
// Top subwindows
subwindows []&SubWindow
// ui mode on gg
immediate bool
children_immediate []Widget
needs_refresh bool = true
// ui settings
settings SettingsUI
}
[params]
pub struct WindowParams {
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_click ClickFn
on_mouse_down ClickFn
on_mouse_up ClickFn
on_files_droped ClickFn
on_swipe ClickFn
on_key_down KeyFn
on_char KeyFn
on_scroll ScrollFn
on_resize ResizeFn
on_iconify WindowFn
on_restore WindowFn
on_quit_request WindowFn
on_suspend WindowFn
on_resume WindowFn
on_mouse_move MouseMoveFn
on_init WindowFn
on_draw WindowFn
children []Widget
custom_bold_font_path string
native_rendering bool
resizable bool
mode WindowSizeType
immediate bool
// Text Config
lines int = 10
// message
native_message bool = true
// drag & drop
enable_dragndrop bool = true
max_dropped_files int = 5
max_dropped_file_path_length int = 2048
}
pub fn window(cfg WindowParams) &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
// vertical_align: gx.VerticalAlign.middle
// size: int(m / cfg.lines)
}
// C.printf(c'window() state =%p \n', cfg.state)
mut window := &Window{
state: cfg.state
title: cfg.title
bg_color: cfg.bg_color
width: width
height: height
// orig_width: width // 800
// orig_height: height // 600
children: cfg.children
on_init: cfg.on_init
on_draw: cfg.on_draw
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
files_droped_fn: cfg.on_files_droped
swipe_fn: cfg.on_swipe
resizable: resizable
mode: cfg.mode
resize_fn: cfg.on_resize
text_cfg: text_cfg
native_message: cfg.native_message
immediate: cfg.immediate
iconified_fn: cfg.on_iconify
restored_fn: cfg.on_restore
quit_requested_fn: cfg.on_quit_request
suspended_fn: cfg.on_suspend
resumed_fn: cfg.on_resume
}
// register default color themes
window.register_default_color_themes()
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.immediate {
frame_immediate
} else if cfg.native_rendering {
native_frame
} else {
frame
}
// native_frame_fn: native_frame
event_fn: on_event
user_data: window
font_path: if cfg.font_path == '' { font.default() } else { cfg.font_path }
custom_bold_font_path: cfg.custom_bold_font_path
init_fn: gg_init
cleanup_fn: gg_cleanup
// 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: !cfg.immediate
// drag & drop
enable_dragndrop: cfg.enable_dragndrop
max_dropped_files: cfg.max_dropped_files
max_dropped_file_path_length: cfg.max_dropped_file_path_length
)
mut ui_ctx := &UI{
gg: gcontext
clipboard: clipboard.new()
}
ui_ctx.load_icos()
window.ui = ui_ctx
// q := int(window)
// println('created window $q.hex()')
return window
}
pub fn (mut parent_window Window) child_window(cfg WindowParams) &Window {
// q := int(parent_window)
// println('child_window() q=$q.hex() parent={parent_window:p} ${cfg.on_draw:p}')
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
on_draw: cfg.on_draw
title: cfg.title
bg_color: cfg.bg_color
width: cfg.width
height: cfg.height
children: cfg.children
click_fn: cfg.on_click
}
window.widgets = &parent_window.widgets
window.widgets_counts = &parent_window.widgets_counts
parent_window.child_window = window
window.evt_mngr = &parent_window.evt_mngr
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
parent_window.register_child(*child)
child.init(parent_window)
}
// window.set_cursor()
return window
}
//----
fn gg_init(mut window Window) {
window.load_settings()
window.init_styles()
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')
// This add experimental ui message system
if !window.native_message {
window.add_message_dialog()
}
for mut child in window.children {
// println('init $child.id')
window.register_child(*child)
child.init(window)
}
// then subwindows
for mut sw in window.subwindows {
// println('init $child.id')
window.register_child(*sw)
sw.init(window)
}
// refresh the layout
window.update_layout()
if window.on_init != voidptr(0) {
window.on_init(window)
}
}
[manualfree]
fn gg_cleanup(mut window Window) {
// All the ckeanup goes here
for mut child in window.children {
// println('cleanup ${child.id()}')
child.cleanup()
}
unsafe { window.free() }
}
fn frame(mut w Window) {
w.ui.gg.begin()
mut children := if w.child_window == 0 { w.children } else { w.child_window.children }
for mut child in children {
child.draw()
}
for mut sw in w.subwindows {
sw.draw()
}
draw_tooltip(w)
if w.on_draw != voidptr(0) {
w.on_draw(w)
}
/*
if w.child_window.on_draw != voidptr(0) {
println('have child on_draw()')
w.child_window.on_draw(w.child_window)
}
*/
w.ui.gg.end()
}
fn frame_immediate(mut w Window) {
w.ui.gg.begin()
for mut child in w.children_immediate {
child.draw()
}
if !w.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 }
for mut child in children {
child.draw()
}
draw_tooltip(w)
if w.on_draw != voidptr(0) {
w.on_draw(w)
}
w.needs_refresh = false
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 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 = Touch{
pos: 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 = Touch{
pos: 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)
}
.files_droped {
window_files_droped(e, mut 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 {
window_resize(e, window.ui)
}
.iconified {
if window.iconified_fn != voidptr(0) {
window.iconified_fn(window)
}
}
.restored {
if window.restored_fn != voidptr(0) {
window.restored_fn(window)
}
}
.quit_requested {
if window.quit_requested_fn != voidptr(0) {
window.quit_requested_fn(window)
}
}
.suspended {
if window.suspended_fn != voidptr(0) {
window.suspended_fn(window)
}
}
.resumed {
if window.resumed_fn != voidptr(0) {
window.resumed_fn(window)
}
}
.touches_began {
if e.num_touches > 0 {
t := e.touches[0]
window.touch.start = Touch{
pos: 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 = Touch{
pos: 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 = Touch{
pos: Pos{
x: int(t.pos_x / window.ui.gg.scale)
y: int(t.pos_y / window.ui.gg.scale)
}
time: time.now()
}
if e.num_touches > 1 {
window_touch_scroll(e, window.ui)
} else {
// 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 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_key_down(event gg.Event, ui &UI) {
// println('keydown char=$event.char_code')
mut window := ui.window
// C.printf(c'g child=%p\n', child)
// println('window_keydown $event')
e := KeyEvent{
key: Key(event.key_code)
mods: KeyMod(event.modifiers)
codepoint: event.char_code
code: int(event.key_code)
// action: action
// mods: mod
}
// TODO: [Ctl]+[Tab] and [Ctl]+[Shift]+[Tab] not captured by sokol
if e.key == .tab {
if shift_key(e.mods) {
window.focus_prev()
} else {
window.focus_next()
}
} else if e.key == .escape {
println('escape')
}
if e.key == .escape && window.child_window != 0 {
// Close the child window on Escape
for mut child in window.child_window.children {
// println('cleanup ${child.id()}')
child.cleanup()
}
window.child_window = &Window(0)
}
if window.key_down_fn != KeyFn(0) {
window.key_down_fn(e, window)
}
// 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(event gg.Event, ui &UI) {
// println('keychar char=$event.char_code')
// println("window_char: $event")
window := ui.window
e := KeyEvent{
codepoint: event.char_code
mods: KeyMod(event.modifiers)
}
if window.char_fn != KeyFn(0) {
window.char_fn(e, window)
}
window.eventbus.publish(events.on_char, window, e)
}
fn window_mouse_down(event gg.Event, mut ui UI) {
mut 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)
}
}
*/
window.evt_mngr.point_inside_receivers_mouse_event(e, events.on_mouse_down)
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_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.dragger.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.update_tooltip(e)
window.eventbus.publish(events.on_mouse_move, 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.dragger.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_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_scroll(event gg.Event, ui &UI) {
mut window := ui.window
// println('title =$window.title')
e := ScrollEvent{
mouse_x: event.mouse_x / ui.gg.scale
mouse_y: event.mouse_y / ui.gg.scale
x: event.scroll_x / ui.gg.scale
y: event.scroll_y / ui.gg.scale
}
if window.scroll_fn != voidptr(0) {
window.scroll_fn(e, window)
}
window.evt_mngr.point_inside_receivers_scroll(e)
window.eventbus.publish(events.on_scroll, window, e)
}
fn window_touch_down(event gg.Event, ui &UI) {
mut window := ui.window
e := MouseEvent{
action: .down
x: window.touch.start.pos.x
y: window.touch.start.pos.y
}
window.evt_mngr.point_inside_receivers_mouse_event(e, events.on_mouse_down)
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_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_touch_up(event gg.Event, ui &UI) {
window := ui.window
e := MouseEvent{
action: .up
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_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_scroll(event gg.Event, ui &UI) {
mut window := ui.window
// println('title =$window.title')
s, m := window.touch.start, window.touch.move
adx, ady := m.pos.x - s.pos.x, m.pos.y - s.pos.y
e := ScrollEvent{
mouse_x: f64(m.pos.x)
mouse_y: f64(m.pos.y)
x: f64(adx) / 30.0
y: f64(ady) / 30.0
}
window.touch.start = window.touch.move
if window.scroll_fn != voidptr(0) {
window.scroll_fn(e, window)
}
window.eventbus.publish(events.on_scroll, window, e)
}
fn window_touch_swipe(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.swipe_fn != voidptr(0) && window.child_window == 0 { // && action == voidptr(0) {
window.swipe_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_swipe, window.child_window, e)
} else {
window.eventbus.publish(events.on_swipe, 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_files_droped(event gg.Event, mut ui UI) {
mut 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 window.files_droped_fn != voidptr(0) { // && action == voidptr(0) {
window.files_droped_fn(e, window)
}
// window.evt_mngr.point_inside_receivers_mouse_event(e, events.on_files_droped)
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_files_droped, window.child_window, e)
} else {
window.eventbus.publish(events.on_files_droped, window, e)
}
}
//----
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)
}
pub fn (mut w Window) refresh() {
w.ui.gg.refresh_ui()
$if macos {
C.darwin_window_refresh()
}
}
pub fn (w &Window) mouse_inside(x int, y int, width int, height int) bool {
return false
}
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
}