-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathinterface_shortcut.v
More file actions
100 lines (89 loc) · 2.23 KB
/
interface_shortcut.v
File metadata and controls
100 lines (89 loc) · 2.23 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
module ui
// Adding shortcuts field for a Widget or Component (having id field) makes it react as user-dedined shortcuts
// see tool_key for parsing shortcut as string
pub interface Shortcutable {
id string
mut:
shortcuts Shortcuts
}
// TODO: documentation
pub fn (mut s Shortcutable) add_shortcut(shortcut string, key_fn ShortcutFn) {
mods, code, key := parse_shortcut(shortcut)
if code == 0 {
s.shortcuts.chars[key] = Shortcut{
mods: mods
key_fn: key_fn
}
} else {
s.shortcuts.keys[code] = Shortcut{
mods: mods
key_fn: key_fn
}
}
}
// TODO: documentation
pub fn (mut s Shortcutable) add_shortcut_context(shortcut string, context voidptr) {
_, code, key := parse_shortcut(shortcut)
if code == 0 {
unsafe {
s.shortcuts.chars[key].context = context
}
} else {
unsafe {
s.shortcuts.keys[code].context = context
}
}
}
// TODO: documentation
pub fn (mut s Shortcutable) add_shortcut_with_context(shortcut string, key_fn ShortcutFn, context voidptr) {
s.add_shortcut(shortcut, key_fn)
s.add_shortcut_context(shortcut, context)
}
// This provides user defined shortcut actions (see grid and grid_data as a use case)
pub type ShortcutFn = fn (context voidptr)
pub type KeyShortcuts = map[int]Shortcut
pub type CharShortcuts = map[string]Shortcut
pub struct Shortcuts {
pub mut:
keys KeyShortcuts
chars CharShortcuts
}
pub struct Shortcut {
pub mut:
mods KeyMod
key_fn ShortcutFn = unsafe { nil }
context voidptr
}
// TODO: documentation
pub fn char_shortcut(e KeyEvent, shortcuts Shortcuts, context voidptr) {
// weirdly when .ctrl modifier the codepoint is differently interpreted
mut s := utf32_to_str(e.codepoint)
$if macos {
if e.mods == .ctrl {
s = rune(96 + e.codepoint).str()
}
}
if sc := shortcuts.chars[s] {
if has_key_mods(e.mods, sc.mods) {
if sc.context != unsafe { nil } {
sc.key_fn(sc.context)
} else {
sc.key_fn(context)
}
}
}
}
// TODO: documentation
pub fn key_shortcut(e KeyEvent, shortcuts Shortcuts, context voidptr) {
// println("key_shortcut ${int(e.key)}")
ikey := int(e.key)
if sc := shortcuts.keys[ikey] {
if has_key_mods(e.mods, sc.mods) {
if sc.context != unsafe { nil } {
sc.key_fn(sc.context)
} else {
sc.key_fn(context)
}
}
}
}