-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelerator.go
169 lines (144 loc) · 4.07 KB
/
accelerator.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
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
// Copyright (c) 2023 The Go-Curses Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ctk
import (
"github.com/go-curses/cdk"
)
const TypeAccelerator cdk.CTypeTag = "ctk-accelerator"
func init() {
_ = cdk.TypesManager.AddType(TypeAccelerator, nil)
}
type Accelerator interface {
Object
Init() (already bool)
LockAccel()
UnlockAccel()
IsLocked() (locked bool)
Path() (path string)
Key() (key cdk.Key)
Mods() (mods cdk.ModMask)
Match(key cdk.Key, mods cdk.ModMask) (match bool)
Settings() (path string, key cdk.Key, mods cdk.ModMask)
Configure(key cdk.Key, mods cdk.ModMask)
UnsetKeyMods()
}
type CAccelerator struct {
CObject
accelLocking int
}
func NewDefaultAccelerator(path string) Accelerator {
return NewAccelerator(path, cdk.KeyNUL, cdk.ModNone)
}
func NewAccelerator(path string, key cdk.Key, mods cdk.ModMask) Accelerator {
a := &CAccelerator{}
a.Init()
a.Configure(key, mods)
return a
}
func (a *CAccelerator) Init() (already bool) {
if a.InitTypeItem(TypeAccelMap, a) {
return true
}
a.CObject.Init()
a.accelLocking = 0
_ = a.InstallProperty(PropertyAccelPath, cdk.StringProperty, true, "")
_ = a.InstallProperty(PropertyAccelKey, cdk.StructProperty, true, cdk.KeyNUL)
_ = a.InstallProperty(PropertyAccelMods, cdk.StructProperty, true, cdk.ModNone)
_ = a.InstallProperty(PropertyAccelLocked, cdk.BoolProperty, true, false)
return false
}
func (a *CAccelerator) LockAccel() {
a.Lock()
a.accelLocking += 1
a.Unlock()
if a.accelLocking == 1 {
if err := a.SetBoolProperty(PropertyAccelLocked, true); err != nil {
a.LogErr(err)
}
}
}
func (a *CAccelerator) UnlockAccel() {
a.Lock()
a.accelLocking -= 1
a.Unlock()
if a.accelLocking == 0 {
if err := a.SetBoolProperty(PropertyAccelLocked, false); err != nil {
a.LogErr(err)
}
}
}
func (a *CAccelerator) IsLocked() (locked bool) {
a.RLock()
locked = a.accelLocking > 0
a.RUnlock()
return
}
func (a *CAccelerator) Path() (path string) {
var err error
if path, err = a.GetStringProperty(PropertyAccelPath); err != nil {
a.LogErr(err)
}
return
}
func (a *CAccelerator) Key() (key cdk.Key) {
var ok bool
if v, err := a.GetStructProperty(PropertyAccelKey); err != nil {
a.LogErr(err)
} else if key, ok = v.(cdk.Key); !ok {
key = cdk.KeyNUL
a.LogError("value stored in %v is not of cdk.Key type: %v (%T)", PropertyAccelKey, v, v)
}
return
}
func (a *CAccelerator) Mods() (mods cdk.ModMask) {
var ok bool
if v, err := a.GetStructProperty(PropertyAccelMods); err != nil {
a.LogErr(err)
} else if mods, ok = v.(cdk.ModMask); !ok {
mods = cdk.ModNone
a.LogError("value stored in %v is not of cdk.ModMask type: %v (%T)", PropertyAccelMods, v, v)
}
return
}
func (a *CAccelerator) Match(key cdk.Key, mods cdk.ModMask) (match bool) {
k, m := a.Key(), a.Mods()
return key == k && mods == m
}
func (a *CAccelerator) Settings() (path string, key cdk.Key, mods cdk.ModMask) {
path = a.Path()
key = a.Key()
mods = a.Mods()
return
}
func (a *CAccelerator) Configure(key cdk.Key, mods cdk.ModMask) {
if a.IsLocked() {
a.LogError("accelerator is locked, cannot Configure: %v", a.Path())
return
}
a.Freeze()
if err := a.SetStructProperty(PropertyAccelKey, key); err != nil {
a.LogErr(err)
}
if err := a.SetStructProperty(PropertyAccelMods, mods); err != nil {
a.LogErr(err)
}
a.Thaw()
}
func (a *CAccelerator) UnsetKeyMods() {
a.Configure(cdk.KeyNUL, cdk.ModNone)
}
const PropertyAccelPath cdk.Property = "accel-path"
const PropertyAccelKey cdk.Property = "accel-key"
const PropertyAccelMods cdk.Property = "accel-mods"
const PropertyAccelLocked cdk.Property = "accel-locked"