-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.v
More file actions
383 lines (353 loc) · 11 KB
/
Copy pathevents.v
File metadata and controls
383 lines (353 loc) · 11 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
module simplegui
// ControlValidator represents a validation function signature for control string values.
pub type ControlValidator = fn (value string) string
// set_value updates the value of a control by name and dispatches a change event.
pub fn (win &SimpleWindow) set_value(name string, value string) &SimpleWindow {
win.set_control_value(name, value)
unsafe {
mut w := &SimpleWindow(win)
w.dispatch_event(name, 'change', value)
}
return win
}
// on_key registers a keyboard shortcut event handler for the window.
pub fn (win &SimpleWindow) on_key(key string, callback StringEventCallback) &SimpleWindow {
norm_key := normalize_key_shortcut(key)
unsafe {
mut w := &SimpleWindow(win)
w.handlers << ControlEventHandler{
control_name: 'window'
event_name: 'key'
filter_value: norm_key
string_cb: callback
}
}
return win
}
// run_after executes a void callback after a specified duration in milliseconds.
pub fn (win &SimpleWindow) run_after(ms int, callback VoidEventCallback) &SimpleWindow {
unsafe {
mut w := &SimpleWindow(win)
w.handlers << ControlEventHandler{
control_name: 'window'
event_name: 'run_after'
void_cb: callback
}
}
if win.window_info != unsafe { nil } {
C.window_run_after(win.window_info, ms, c'window')
}
return win
}
// on_click registers an event handler for click events on a control.
pub fn (win &SimpleWindow) on_click(name string, callback VoidEventCallback) &SimpleWindow {
idx := win.find_handler(name, 'click')
mut handler := ControlEventHandler{
control_name: name
event_name: 'click'
void_cb: callback
}
unsafe {
mut w := &SimpleWindow(win)
if idx >= 0 {
w.handlers[idx] = handler
} else {
w.handlers << handler
}
}
return win
}
// on_change registers an event handler for value change events on a control.
pub fn (win &SimpleWindow) on_change(name string, callback StringEventCallback) &SimpleWindow {
idx := win.find_handler(name, 'change')
mut handler := ControlEventHandler{
control_name: name
event_name: 'change'
string_cb: callback
}
unsafe {
mut w := &SimpleWindow(win)
if idx >= 0 {
w.handlers[idx] = handler
} else {
w.handlers << handler
}
}
return win
}
// on_cell_button_click registers an event handler for table grid cell action buttons.
pub fn (win &SimpleWindow) on_cell_button_click(name string, callback StringEventCallback) &SimpleWindow {
idx := win.find_handler(name, 'click_cell_button')
mut handler := ControlEventHandler{
control_name: name
event_name: 'click_cell_button'
string_cb: callback
}
unsafe {
mut w := &SimpleWindow(win)
if idx >= 0 {
w.handlers[idx] = handler
} else {
w.handlers << handler
}
}
return win
}
// on_change_step registers a callback for wizard stepper step changes.
pub fn (win &SimpleWindow) on_change_step(name string, callback StringEventCallback) &SimpleWindow {
idx := win.find_handler(name, 'change_step')
mut handler := ControlEventHandler{
control_name: name
event_name: 'change_step'
string_cb: callback
}
unsafe {
mut w := &SimpleWindow(win)
if idx >= 0 {
w.handlers[idx] = handler
} else {
w.handlers << handler
}
}
return win
}
// on_click_tag registers a callback for tag cloud chip clicks.
pub fn (win &SimpleWindow) on_click_tag(name string, callback StringEventCallback) &SimpleWindow {
idx := win.find_handler(name, 'click_tag')
mut handler := ControlEventHandler{
control_name: name
event_name: 'click_tag'
string_cb: callback
}
unsafe {
mut w := &SimpleWindow(win)
if idx >= 0 {
w.handlers[idx] = handler
} else {
w.handlers << handler
}
}
return win
}
// on_any_event registers an observer callback that receives all dispatched events on the window.
pub fn (win &SimpleWindow) on_any_event(callback AnyEventCallback) &SimpleWindow {
unsafe {
mut w := &SimpleWindow(win)
w.any_event_handlers << callback
}
return win
}
// dispatch_event dispatches an event by name to registered handlers and observers.
pub fn (win &SimpleWindow) dispatch_event(name string, event_name string, value string) bool {
if win.debug_mode {
println('[simplegui DEBUG] Dispatching Event: "${event_name}" on Control: "${name}" (Value: "${value}")')
win.set_status('[DEBUG] ${event_name} on "${name}" -> "${value}"')
}
// Broadcast live event to window observers & system subscribers
for cb in win.any_event_handlers {
unsafe {
mut w := &SimpleWindow(win)
cb(mut w, name, event_name, value)
}
}
sys_broadcast_event(win.title, name, event_name, value)
mut handler_idx := -1
if event_name == 'file_drop' {
handler_idx = win.find_handler_by_filter(name, event_name, value)
if handler_idx < 0 {
handler_idx = win.find_handler_by_filter('window', event_name, value)
}
} else {
handler_idx = win.find_handler_by_filter(name, event_name, value)
}
if handler_idx < 0 {
return false
}
handler := win.handlers[handler_idx]
if event_name == 'run_after' {
unsafe {
mut w := &SimpleWindow(win)
w.handlers.delete(handler_idx)
}
}
if event_name == 'file_drop' {
files := value.split('|')
unsafe {
mut w := &SimpleWindow(win)
handler.file_drop_cb(mut w, files)
}
return true
} else if handler.void_cb != unsafe { nil } {
unsafe {
mut w := &SimpleWindow(win)
handler.void_cb(mut w)
}
return true
} else if handler.string_cb != unsafe { nil } {
unsafe {
mut w := &SimpleWindow(win)
handler.string_cb(mut w, value)
}
return true
}
return false
}
// click programmatically triggers a click event on a named control.
pub fn (win &SimpleWindow) click(name string) bool {
return win.dispatch_event(name, 'click', '')
}
// Cocoa event dispatcher to V
@[export: 'vlang_dispatch_event']
fn vlang_dispatch_event(win_ptr voidptr, name_str &u8, event_str &u8, value_str &u8) {
mut win := unsafe { &SimpleWindow(win_ptr) }
name := unsafe { name_str.vstring() }
event := unsafe { event_str.vstring() }
value := unsafe { value_str.vstring() }
// Update V struct state with the new value from Cocoa
if event == 'change' {
idx := win.find_control(name)
if idx >= 0 {
kind := win.controls[idx].kind
if kind == 'checkbox' || kind == 'toggle' {
win.controls[idx].checked = (value == 'true')
} else if kind in ['number', 'slider', 'vertical_slider', 'progress', 'stepper', 'knob',
'levelindicator'] {
win.controls[idx].number = value.int()
}
win.controls[idx].value = value
}
} else if event == 'column_change' {
win.table_selected_columns[name] = value.int()
}
win.dispatch_event(name, event, value)
}
@[export: 'vlang_dispatch_close_requested']
fn vlang_dispatch_close_requested(win_ptr voidptr) bool {
if win_ptr == unsafe { nil } {
return true
}
mut win := unsafe { &SimpleWindow(win_ptr) }
return win.can_close()
}
// set_interval starts a recurring timer that triggers a callback every ms milliseconds.
pub fn (win &SimpleWindow) set_interval(timer_name string, ms int, callback VoidEventCallback) &SimpleWindow {
unsafe {
mut w := &SimpleWindow(win)
w.handlers << ControlEventHandler{
control_name: timer_name
event_name: 'timer'
void_cb: callback
}
}
if win.window_info != unsafe { nil } {
C.window_set_interval(win.window_info, ms, timer_name.str)
}
return win
}
// stop_interval stops a recurring timer by name.
pub fn (win &SimpleWindow) stop_interval(timer_name string) &SimpleWindow {
if win.window_info != unsafe { nil } {
C.window_stop_interval(win.window_info, timer_name.str)
}
return win
}
// on_hover registers an event handler for mouse hover enter events on a control.
pub fn (win &SimpleWindow) on_hover(name string, callback VoidEventCallback) &SimpleWindow {
unsafe {
mut w := &SimpleWindow(win)
w.handlers << ControlEventHandler{
control_name: name
event_name: 'hover_enter'
void_cb: callback
}
}
if win.window_info != unsafe { nil } {
C.window_enable_hover_events(win.window_info, name.str)
}
return win
}
// on_hover_exit registers an event handler for mouse hover exit events on a control.
pub fn (win &SimpleWindow) on_hover_exit(name string, callback VoidEventCallback) &SimpleWindow {
unsafe {
mut w := &SimpleWindow(win)
w.handlers << ControlEventHandler{
control_name: name
event_name: 'hover_exit'
void_cb: callback
}
}
if win.window_info != unsafe { nil } {
C.window_enable_hover_events(win.window_info, name.str)
}
return win
}
// add_menu_item adds a menu item to an application menu and registers its click callback.
pub fn (win &SimpleWindow) add_menu_item(menu_name string, item_title string, shortcut string, callback VoidEventCallback) &SimpleWindow {
handler_name := 'menu_${menu_name}_${item_title}'
win.on_click(handler_name, callback)
if win.window_info != unsafe { nil } {
C.window_add_menu_item(win.window_info, menu_name.str, item_title.str, shortcut.str,
handler_name.str)
}
return win
}
// add_context_menu_item adds a context menu item to a control and registers its click callback.
pub fn (win &SimpleWindow) add_context_menu_item(control_name string, item_title string, callback VoidEventCallback) &SimpleWindow {
handler_name := 'context_${control_name}_${item_title}'
win.on_click(handler_name, callback)
if win.window_info != unsafe { nil } {
C.window_add_context_menu_item(win.window_info, control_name.str, item_title.str,
handler_name.str)
}
return win
}
// on_file_drop registers a callback invoked when files are drag-and-dropped onto the window.
pub fn (win &SimpleWindow) on_file_drop(callback FileDropCallback) &SimpleWindow {
unsafe {
mut w := &SimpleWindow(win)
w.handlers << ControlEventHandler{
control_name: 'window'
event_name: 'file_drop'
file_drop_cb: callback
}
}
return win
}
// add_dock_menu_item adds a menu item to the macOS application dock menu.
pub fn (win &SimpleWindow) add_dock_menu_item(title string, callback VoidEventCallback) &SimpleWindow {
handler_name := 'dock_menu_${title.replace(' ', '_').to_lower()}'
win.on_click(handler_name, callback)
if win.window_info != unsafe { nil } {
C.window_add_dock_menu_item(win.window_info, title.str, handler_name.str)
}
return win
}
// onclick registers a click callback on the last created control.
pub fn (win &SimpleWindow) onclick(callback VoidEventCallback) &SimpleWindow {
if win.last_control != '' {
win.on_click(win.last_control, callback)
}
return win
}
// onchange registers a change callback on the last created control.
pub fn (win &SimpleWindow) onchange(callback StringEventCallback) &SimpleWindow {
if win.last_control != '' {
win.on_change(win.last_control, callback)
}
return win
}
// onhover registers a hover enter callback on the last created control.
pub fn (win &SimpleWindow) onhover(callback VoidEventCallback) &SimpleWindow {
if win.last_control != '' {
win.on_hover(win.last_control, callback)
}
return win
}
// onhover_exit registers a hover exit callback on the last created control.
pub fn (win &SimpleWindow) onhover_exit(callback VoidEventCallback) &SimpleWindow {
if win.last_control != '' {
win.on_hover_exit(win.last_control, callback)
}
return win
}
// Shorthand aliases for value access