-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevents.go
More file actions
781 lines (706 loc) · 18.1 KB
/
Copy pathevents.go
File metadata and controls
781 lines (706 loc) · 18.1 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
// Copyright 2026 The gogpu Authors
// SPDX-License-Identifier: MIT
package gpucontext
import "sync"
// EventSource provides input events from the host application to UI frameworks.
//
// This interface enables UI frameworks (like gogpu/ui) to receive user input
// events from the host window system. The host application (e.g., gogpu.App)
// implements EventSource and passes it to the UI layer.
//
// Event callbacks are invoked on the main thread during the event loop.
// Callback functions should be fast and non-blocking.
//
// Example usage in a UI framework:
//
// func (ui *UI) AttachEvents(source gpucontext.EventSource) {
// source.OnMousePress(func(button MouseButton, x, y float64) {
// widget := ui.hitTest(x, y)
// if widget != nil {
// widget.HandleMouseDown(button, x, y)
// }
// })
//
// source.OnKeyPress(func(key Key, mods Modifiers) {
// ui.focused.HandleKeyDown(key, mods)
// })
// }
//
// Note: This interface is designed for gogpu ↔ ui integration.
// The rendering library (gg) does NOT use this interface.
type EventSource interface {
// Keyboard events
// OnKeyPress registers a callback for key press events.
OnKeyPress(func(key Key, mods Modifiers))
// OnKeyRelease registers a callback for key release events.
OnKeyRelease(func(key Key, mods Modifiers))
// OnTextInput registers a callback for text input events.
// Text input is the result of key presses after applying keyboard layouts
// and input methods. This is the preferred way to handle text entry.
OnTextInput(func(text string))
// Mouse events
//
// All mouse/pointer coordinates (x, y) are in logical DIP (device-independent
// pixels), consistent with WindowProvider.Size(). Do NOT divide by ScaleFactor —
// the framework applies DPI scaling internally on all platforms.
// OnMouseMove registers a callback for mouse movement.
// x, y are in logical DIP relative to the window content area.
OnMouseMove(func(x, y float64))
// OnMousePress registers a callback for mouse button press.
// x, y are in logical DIP relative to the window content area.
OnMousePress(func(button MouseButton, x, y float64))
// OnMouseRelease registers a callback for mouse button release.
// x, y are in logical DIP relative to the window content area.
OnMouseRelease(func(button MouseButton, x, y float64))
// OnScroll registers a callback for scroll wheel events.
// dx and dy are the scroll deltas (positive = right/down).
OnScroll(func(dx, dy float64))
// Window events
// OnResize registers a callback for window resize.
OnResize(func(width, height int))
// OnFocus registers a callback for focus change.
OnFocus(func(focused bool))
// IME events for international text input
// OnIMECompositionStart registers a callback for when IME composition begins.
// This is called when the user starts typing in an IME (e.g., for CJK input).
OnIMECompositionStart(fn func())
// OnIMECompositionUpdate registers a callback for IME composition updates.
// Called during composition with the current state (preview text, cursor).
OnIMECompositionUpdate(fn func(state IMEState))
// OnIMECompositionEnd registers a callback for when IME composition ends.
// The committed parameter contains the final text that should be inserted.
OnIMECompositionEnd(fn func(committed string))
}
// IMEState represents the current state of the Input Method Editor.
// This is used for CJK (Chinese, Japanese, Korean) and other complex text input.
//
// During IME composition, the user types phonetic characters that are converted
// to ideographic characters. The IMEState contains the current preview text
// and cursor information for rendering the composition inline.
type IMEState struct {
// Composing indicates whether IME is currently in composition mode.
Composing bool
// CompositionText is the text currently being composed (e.g., pinyin for Chinese).
// This should be displayed inline at the cursor position with special styling.
CompositionText string
// CursorPos is the cursor position within the composition text.
CursorPos int
// SelectionStart is the start of the selection within the composition text.
// This is used for candidate selection in some IME systems.
SelectionStart int
// SelectionEnd is the end of the selection within the composition text.
SelectionEnd int
}
// IMEController allows widgets to control IME behavior.
// This interface is typically implemented by the host window system.
type IMEController interface {
// SetIMEPosition tells the platform where to show the IME candidate window.
// The coordinates are in screen pixels relative to the window.
SetIMEPosition(x, y int)
// SetIMEEnabled enables or disables IME for the current input context.
// When disabled, key presses are delivered directly without IME processing.
// This is useful for password fields or non-text inputs.
SetIMEEnabled(enabled bool)
}
// Key represents a keyboard key.
//
// Values use grouped ranges with explicit base offsets (the net/http pattern).
// Each group reserves room for future expansion without shifting existing values.
// This is a platform-independent virtual key code scheme; platform code maps
// native scan codes / virtual keys to these values.
//
// Groups and ranges:
//
// KeyUnknown = 0
// Letters [1..31] — A-Z (26 used, 5 reserved)
// Digits [33..47] — 0-9 (10 used, 5 reserved)
// Function [49..80] — F1-F24 (24 used, 8 reserved)
// Navigation [81..112] — arrows, home, end, etc. (15 used, 17 reserved)
// Modifiers [113..128] — shift, ctrl, alt, super (8 used, 8 reserved)
// Punctuation [129..160] — brackets, operators, intl (13 used, 19 reserved)
// Numpad [161..192] — numpad digits + operators (18 used, 14 reserved)
// Locks [193..208] — caps, scroll, num lock (5 used, 11 reserved)
// Media [209..240] — playback controls (5 used, 27 reserved)
// Volume [241..248] — volume up/down/mute (3 used, 5 reserved)
// Browser [249..264] — navigation keys (5 used, 11 reserved)
// System [265..280] — context menu, cancel, launch (4 used, 12 reserved)
type Key uint16
// KeyUnknown represents an unrecognized or unmapped key.
const KeyUnknown Key = 0
// Letters [1..31] — 26 keys (A-Z), 5 reserved for future use.
const (
KeyA Key = iota + 1
KeyB
KeyC
KeyD
KeyE
KeyF
KeyG
KeyH
KeyI
KeyJ
KeyK
KeyL
KeyM
KeyN
KeyO
KeyP
KeyQ
KeyR
KeyS
KeyT
KeyU
KeyV
KeyW
KeyX
KeyY
KeyZ
)
// Digits [33..47] — 10 keys (0-9), 5 reserved for future use.
const (
Key0 Key = iota + 33
Key1
Key2
Key3
Key4
Key5
Key6
Key7
Key8
Key9
)
// Function keys [49..80] — 24 keys (F1-F24), 8 reserved for future use.
const (
KeyF1 Key = iota + 49
KeyF2
KeyF3
KeyF4
KeyF5
KeyF6
KeyF7
KeyF8
KeyF9
KeyF10
KeyF11
KeyF12
KeyF13
KeyF14
KeyF15
KeyF16
KeyF17
KeyF18
KeyF19
KeyF20
KeyF21
KeyF22
KeyF23
KeyF24
)
// Navigation [81..112] — 15 keys, 17 reserved for future use.
const (
KeyEscape Key = iota + 81
KeyTab
KeyBackspace
KeyEnter
KeySpace
KeyInsert
KeyDelete
KeyHome
KeyEnd
KeyPageUp
KeyPageDown
KeyLeft
KeyRight
KeyUp
KeyDown
)
// Modifiers as keys [113..128] — 8 keys, 8 reserved for future use.
// These represent physical modifier keys. For modifier state in event
// callbacks, use [Modifiers] flags instead.
const (
KeyLeftShift Key = iota + 113
KeyRightShift
KeyLeftControl
KeyRightControl
KeyLeftAlt
KeyRightAlt
KeyLeftSuper
KeyRightSuper
)
// Punctuation and symbols [129..160] — 13 keys, 19 reserved for future use.
const (
KeyMinus Key = iota + 129
KeyEqual
KeyLeftBracket
KeyRightBracket
KeyBackslash
KeySemicolon
KeyApostrophe
KeyGrave
KeyComma
KeyPeriod
KeySlash
KeyIntlBackslash // ISO 102nd key (between left Shift and Z on ISO layouts).
KeyIntlYen // JIS Yen key.
)
// Numpad [161..192] — 18 keys, 14 reserved for future use.
const (
KeyNumpad0 Key = iota + 161
KeyNumpad1
KeyNumpad2
KeyNumpad3
KeyNumpad4
KeyNumpad5
KeyNumpad6
KeyNumpad7
KeyNumpad8
KeyNumpad9
KeyNumpadDecimal
KeyNumpadDivide
KeyNumpadMultiply
KeyNumpadSubtract
KeyNumpadAdd
KeyNumpadEnter
KeyNumpadEqual // Numpad = (Mac keyboards, some international layouts).
KeyNumpadComma // Numpad , (Brazilian ABNT2 layout).
)
// Lock keys [193..208] — 5 keys, 11 reserved for future use.
const (
KeyCapsLock Key = iota + 193
KeyScrollLock
KeyNumLock
KeyPrintScreen
KeyPause
)
// Media keys [209..240] — 5 keys, 27 reserved for future use.
// Names follow the W3C UIEvents KeyboardEvent.code convention.
const (
KeyMediaPlayPause Key = iota + 209
KeyMediaStop
KeyMediaTrackNext
KeyMediaTrackPrevious
KeyMediaRecord
)
// Volume keys [241..248] — 3 keys, 5 reserved for future use.
// Names follow the W3C UIEvents KeyboardEvent.code convention.
const (
KeyAudioVolumeUp Key = iota + 241
KeyAudioVolumeDown
KeyAudioVolumeMute
)
// Browser keys [249..264] — 5 keys, 11 reserved for future use.
const (
KeyBrowserBack Key = iota + 249
KeyBrowserForward
KeyBrowserRefresh
KeyBrowserHome
KeyBrowserSearch
)
// System keys [265..280] — 4 keys, 12 reserved for future use.
const (
KeyContextMenu Key = iota + 265 // Application/context menu key (not VK_MENU/Alt).
KeyCancel // Cancel key (Ctrl+Break on Windows).
KeyLaunchApp1 // Launch application 1 (typically My Computer).
KeyLaunchApp2 // Launch application 2 (typically Calculator).
)
// String returns a human-readable name for the key.
func (k Key) String() string {
switch k {
case KeyUnknown:
return "Unknown"
case KeyA:
return "A"
case KeyB:
return "B"
case KeyC:
return "C"
case KeyD:
return "D"
case KeyE:
return "E"
case KeyF:
return "F"
case KeyG:
return "G"
case KeyH:
return "H"
case KeyI:
return "I"
case KeyJ:
return "J"
case KeyK:
return "K"
case KeyL:
return "L"
case KeyM:
return "M"
case KeyN:
return "N"
case KeyO:
return "O"
case KeyP:
return "P"
case KeyQ:
return "Q"
case KeyR:
return "R"
case KeyS:
return "S"
case KeyT:
return "T"
case KeyU:
return "U"
case KeyV:
return "V"
case KeyW:
return "W"
case KeyX:
return "X"
case KeyY:
return "Y"
case KeyZ:
return "Z"
case Key0:
return "0"
case Key1:
return "1"
case Key2:
return "2"
case Key3:
return "3"
case Key4:
return "4"
case Key5:
return "5"
case Key6:
return "6"
case Key7:
return "7"
case Key8:
return "8"
case Key9:
return "9"
case KeyF1:
return "F1"
case KeyF2:
return "F2"
case KeyF3:
return "F3"
case KeyF4:
return "F4"
case KeyF5:
return "F5"
case KeyF6:
return "F6"
case KeyF7:
return "F7"
case KeyF8:
return "F8"
case KeyF9:
return "F9"
case KeyF10:
return "F10"
case KeyF11:
return "F11"
case KeyF12:
return "F12"
case KeyF13:
return "F13"
case KeyF14:
return "F14"
case KeyF15:
return "F15"
case KeyF16:
return "F16"
case KeyF17:
return "F17"
case KeyF18:
return "F18"
case KeyF19:
return "F19"
case KeyF20:
return "F20"
case KeyF21:
return "F21"
case KeyF22:
return "F22"
case KeyF23:
return "F23"
case KeyF24:
return "F24"
case KeyEscape:
return "Escape"
case KeyTab:
return "Tab"
case KeyBackspace:
return "Backspace"
case KeyEnter:
return "Enter"
case KeySpace:
return "Space"
case KeyInsert:
return "Insert"
case KeyDelete:
return "Delete"
case KeyHome:
return "Home"
case KeyEnd:
return "End"
case KeyPageUp:
return "PageUp"
case KeyPageDown:
return "PageDown"
case KeyLeft:
return stringLeft
case KeyRight:
return stringRight
case KeyUp:
return "Up"
case KeyDown:
return "Down"
case KeyLeftShift:
return "LeftShift"
case KeyRightShift:
return "RightShift"
case KeyLeftControl:
return "LeftControl"
case KeyRightControl:
return "RightControl"
case KeyLeftAlt:
return "LeftAlt"
case KeyRightAlt:
return "RightAlt"
case KeyLeftSuper:
return "LeftSuper"
case KeyRightSuper:
return "RightSuper"
case KeyMinus:
return "Minus"
case KeyEqual:
return "Equal"
case KeyLeftBracket:
return "LeftBracket"
case KeyRightBracket:
return "RightBracket"
case KeyBackslash:
return "Backslash"
case KeySemicolon:
return "Semicolon"
case KeyApostrophe:
return "Apostrophe"
case KeyGrave:
return "Grave"
case KeyComma:
return "Comma"
case KeyPeriod:
return "Period"
case KeySlash:
return "Slash"
case KeyIntlBackslash:
return "IntlBackslash"
case KeyIntlYen:
return "IntlYen"
case KeyNumpad0:
return "Numpad0"
case KeyNumpad1:
return "Numpad1"
case KeyNumpad2:
return "Numpad2"
case KeyNumpad3:
return "Numpad3"
case KeyNumpad4:
return "Numpad4"
case KeyNumpad5:
return "Numpad5"
case KeyNumpad6:
return "Numpad6"
case KeyNumpad7:
return "Numpad7"
case KeyNumpad8:
return "Numpad8"
case KeyNumpad9:
return "Numpad9"
case KeyNumpadDecimal:
return "NumpadDecimal"
case KeyNumpadDivide:
return "NumpadDivide"
case KeyNumpadMultiply:
return "NumpadMultiply"
case KeyNumpadSubtract:
return "NumpadSubtract"
case KeyNumpadAdd:
return "NumpadAdd"
case KeyNumpadEnter:
return "NumpadEnter"
case KeyNumpadEqual:
return "NumpadEqual"
case KeyNumpadComma:
return "NumpadComma"
case KeyCapsLock:
return "CapsLock"
case KeyScrollLock:
return "ScrollLock"
case KeyNumLock:
return "NumLock"
case KeyPrintScreen:
return "PrintScreen"
case KeyPause:
return "Pause"
case KeyMediaPlayPause:
return "MediaPlayPause"
case KeyMediaStop:
return "MediaStop"
case KeyMediaTrackNext:
return "MediaTrackNext"
case KeyMediaTrackPrevious:
return "MediaTrackPrevious"
case KeyMediaRecord:
return "MediaRecord"
case KeyAudioVolumeUp:
return "AudioVolumeUp"
case KeyAudioVolumeDown:
return "AudioVolumeDown"
case KeyAudioVolumeMute:
return "AudioVolumeMute"
case KeyBrowserBack:
return "BrowserBack"
case KeyBrowserForward:
return "BrowserForward"
case KeyBrowserRefresh:
return "BrowserRefresh"
case KeyBrowserHome:
return "BrowserHome"
case KeyBrowserSearch:
return "BrowserSearch"
case KeyContextMenu:
return "ContextMenu"
case KeyCancel:
return "Cancel"
case KeyLaunchApp1:
return "LaunchApp1"
case KeyLaunchApp2:
return "LaunchApp2"
default:
return "Key(" + uitoa(uint(k)) + ")"
}
}
// uitoa converts a uint to its string representation without importing strconv.
func uitoa(val uint) string {
if val == 0 {
return "0"
}
var buf [20]byte // big enough for 64-bit uint
i := len(buf) - 1
for val > 0 {
buf[i] = byte('0' + val%10)
val /= 10
i--
}
return string(buf[i+1:])
}
var (
keyStringOnce sync.Once
keyStringMap map[string]Key
)
func buildKeyStringMap() {
m := make(map[string]Key, 140)
for k := Key(0); k <= KeyLaunchApp2; k++ {
s := k.String()
if s != "" && s[0] != 'K' {
m[s] = k
}
}
keyStringMap = m
}
// KeyFromString returns the Key for a given string name.
// The name must match the value returned by [Key.String] (e.g., "A", "F13",
// "MediaPlayPause", "ContextMenu"). Returns (KeyUnknown, false) if the name
// is not recognized.
//
// This function is safe for concurrent use and enables W3C
// KeyboardEvent.code compatibility: browser platforms can convert
// JavaScript event.code strings directly to Key values.
//
// key, ok := gpucontext.KeyFromString("A") // → KeyA, true
// key, ok := gpucontext.KeyFromString("MediaPlayPause") // → KeyMediaPlayPause, true
// key, ok := gpucontext.KeyFromString("nonexistent") // → KeyUnknown, false
func KeyFromString(name string) (Key, bool) {
keyStringOnce.Do(buildKeyStringMap)
k, ok := keyStringMap[name]
return k, ok
}
// Modifiers represents keyboard modifier keys.
type Modifiers uint8
const (
// ModShift indicates the Shift key is pressed.
ModShift Modifiers = 1 << iota
// ModControl indicates the Control key is pressed.
ModControl
// ModAlt indicates the Alt key is pressed (Option on macOS).
ModAlt
// ModSuper indicates the Super key is pressed (Windows/Command).
ModSuper
// ModCapsLock indicates Caps Lock is active.
ModCapsLock
// ModNumLock indicates Num Lock is active.
ModNumLock
)
// HasShift returns true if the Shift modifier is set.
func (m Modifiers) HasShift() bool {
return m&ModShift != 0
}
// HasControl returns true if the Control modifier is set.
func (m Modifiers) HasControl() bool {
return m&ModControl != 0
}
// HasAlt returns true if the Alt modifier is set.
func (m Modifiers) HasAlt() bool {
return m&ModAlt != 0
}
// HasSuper returns true if the Super modifier is set.
func (m Modifiers) HasSuper() bool {
return m&ModSuper != 0
}
// MouseButton represents a mouse button.
type MouseButton uint8
const (
// MouseButtonLeft is the primary mouse button.
MouseButtonLeft MouseButton = iota
// MouseButtonRight is the secondary mouse button.
MouseButtonRight
// MouseButtonMiddle is the middle mouse button (scroll wheel click).
MouseButtonMiddle
// MouseButton4 is an extra mouse button.
MouseButton4
// MouseButton5 is an extra mouse button.
MouseButton5
)
// NullEventSource is an EventSource that ignores all event registrations.
// Used when events are not needed.
type NullEventSource struct{}
// OnKeyPress does nothing.
func (NullEventSource) OnKeyPress(func(Key, Modifiers)) {}
// OnKeyRelease does nothing.
func (NullEventSource) OnKeyRelease(func(Key, Modifiers)) {}
// OnTextInput does nothing.
func (NullEventSource) OnTextInput(func(string)) {}
// OnMouseMove does nothing.
func (NullEventSource) OnMouseMove(func(float64, float64)) {}
// OnMousePress does nothing.
func (NullEventSource) OnMousePress(func(MouseButton, float64, float64)) {}
// OnMouseRelease does nothing.
func (NullEventSource) OnMouseRelease(func(MouseButton, float64, float64)) {}
// OnScroll does nothing.
func (NullEventSource) OnScroll(func(float64, float64)) {}
// OnResize does nothing.
func (NullEventSource) OnResize(func(int, int)) {}
// OnFocus does nothing.
func (NullEventSource) OnFocus(func(bool)) {}
// OnIMECompositionStart does nothing.
func (NullEventSource) OnIMECompositionStart(func()) {}
// OnIMECompositionUpdate does nothing.
func (NullEventSource) OnIMECompositionUpdate(func(IMEState)) {}
// OnIMECompositionEnd does nothing.
func (NullEventSource) OnIMECompositionEnd(func(string)) {}
// Ensure NullEventSource implements EventSource.
var _ EventSource = NullEventSource{}