forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_layout_config.go
128 lines (112 loc) · 2.09 KB
/
app_layout_config.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
124
125
126
127
128
package inputdevices
import (
"encoding/json"
"sync"
)
type appLayoutConfig struct {
mu sync.Mutex
Layouts []string
Map map[string]int
}
func (kbd *Keyboard) loadAppLayoutConfig() error {
mapStr := kbd.setting.GetString(kbdKeyAppLayoutMap)
if mapStr == "" {
return nil
}
err := json.Unmarshal([]byte(mapStr), &kbd.appLayoutCfg)
if err != nil {
return err
}
return nil
}
func (c *appLayoutConfig) set(app, layout string) (changed bool) {
c.mu.Lock()
changed = c.setNoLock(app, layout)
c.mu.Unlock()
return
}
func (c *appLayoutConfig) setNoLock(app, layout string) (changed bool) {
idx := c.getLayoutIndex(layout)
if idx == -1 {
c.Layouts = append(c.Layouts, layout)
idx = len(c.Layouts) - 1
changed = true
}
if !changed {
oldIdx, ok := c.Map[app]
if ok {
if oldIdx != idx {
changed = true
}
} else {
changed = true
}
}
c.Map[app] = idx
return
}
func (c *appLayoutConfig) toJson() string {
c.mu.Lock()
defer c.mu.Unlock()
return toJSON(c)
}
func (c *appLayoutConfig) getLayoutIndex(layout string) int {
idx := -1
for i, l := range c.Layouts {
if l == layout {
idx = i
break
}
}
return idx
}
func (c *appLayoutConfig) deleteLayout(layout string) (changed bool) {
c.mu.Lock()
defer c.mu.Unlock()
idx := c.getLayoutIndex(layout)
if idx == -1 {
return false
}
for app, i := range c.Map {
if i == idx {
delete(c.Map, app)
}
}
m := c.toMap()
c.fromMap(m)
return true
}
func (c *appLayoutConfig) toMap() map[string]string {
result := make(map[string]string)
for app, idx := range c.Map {
if 0 <= idx && idx < len(c.Layouts) {
result[app] = c.Layouts[idx]
}
}
return result
}
func (c *appLayoutConfig) fromMap(m map[string]string) {
c.Layouts = nil
for key := range c.Map {
delete(c.Map, key)
}
for key, value := range m {
c.setNoLock(key, value)
}
}
func (c *appLayoutConfig) get(app string) (layout string, ok bool) {
c.mu.Lock()
defer c.mu.Unlock()
var idx int
idx, ok = c.Map[app]
if !ok {
return
}
if 0 <= idx && idx < len(c.Layouts) {
ok = true
layout = c.Layouts[idx]
} else {
return
}
return
}