-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcuts.go
123 lines (98 loc) · 2.84 KB
/
shortcuts.go
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
package shortcuts
import (
"slices"
"sort"
)
// Retrieve shortcut in the list with given appID
func GetShortcut(shortcuts []*Shortcut, appID uint) *Shortcut {
for _, item := range shortcuts {
if item.AppID == appID {
return item
}
}
return &Shortcut{}
}
// Add shortcut to the list
func AddShortcut(shortcuts []*Shortcut, shortcut *Shortcut) ([]*Shortcut, error) {
// Check if already exist an app with the same reference
found := false
for index, item := range shortcuts {
if item.AppID != shortcut.AppID {
continue
}
// Keep current value for some keys
shortcut.IsHidden = item.IsHidden
shortcut.AllowDesktopConfig = item.AllowDesktopConfig
shortcut.AllowOverlay = item.AllowOverlay
shortcut.OpenVR = item.OpenVR
shortcut.Devkit = item.Devkit
shortcut.DevkitGameID = item.DevkitGameID
shortcut.DevkitOverrideAppID = item.DevkitOverrideAppID
shortcut.FlatpakAppID = item.FlatpakAppID
shortcut.LastPlayTime = item.LastPlayTime
// Merge tags to not lose current ones
for _, tag := range item.Tags {
if !slices.Contains(shortcut.Tags, tag) {
shortcut.Tags = append(shortcut.Tags, tag)
}
}
// Replace with new object data
shortcuts[index] = shortcut
found = true
break
}
// Append to the list if not exist
if !found {
shortcuts = append(shortcuts, shortcut)
}
return shortcuts, nil
}
// Remove shortcut from the list
func RemoveShortcut(shortcuts []*Shortcut, shortcut *Shortcut) ([]*Shortcut, error) {
updated := make([]*Shortcut, 0)
found := false
// Instead of appending one by one
// We detect the one to remove and add others in batch
for index, item := range shortcuts {
if item.AppID == shortcut.AppID {
updated = append(updated, shortcuts[:index]...)
updated = append(updated, shortcuts[index+1:]...)
found = true
break
}
}
if found {
return updated, nil
}
// If not found, then return the same list of shortcuts
return shortcuts, nil
}
// Sort shortcuts in alphabetical order
func SortShortcuts(shortcuts []*Shortcut) ([]*Shortcut, error) {
sort.Slice(shortcuts, func(i int, j int) bool {
return shortcuts[i].AppName < shortcuts[j].AppName
})
return shortcuts, nil
}
// Merge callback with rules to apply between target and source
type MergeCallback func(target *Shortcut, source *Shortcut)
// Merge shortcuts lists into one
func MergeShortcuts(main []*Shortcut, extra []*Shortcut, mergeCallback MergeCallback, appendWhenNotFound bool) []*Shortcut {
// When match is detected, call callback to merge data
// When has not match, it can append the item to the list if option is enabled
for _, item := range extra {
found := false
for _, existing := range main {
if existing.AppID != item.AppID {
continue
}
mergeCallback(existing, item)
found = true
break
}
if !found && appendWhenNotFound {
main = append(main, item)
}
}
return main
}