forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KBTraits.swift
258 lines (220 loc) · 7.23 KB
/
KBTraits.swift
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
////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2019 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blink is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////
import Foundation
struct KBTraits: OptionSet, Hashable, Codable {
let rawValue: OptionBits
// device orientation
static let portrait = Self(rawValue: 1 << 0)
static let landscape = Self(rawValue: 1 << 1)
// keyboard type
static let hkb = Self(rawValue: 1 << 2)
static let skb = Self(rawValue: 1 << 3)
// key modifiers
static let escOn = Self(rawValue: 1 << 4)
static let escOff = Self(rawValue: 1 << 5)
static let altOn = Self(rawValue: 1 << 6)
static let altOff = Self(rawValue: 1 << 7)
static let ctrlOn = Self(rawValue: 1 << 8)
static let ctrlOff = Self(rawValue: 1 << 9)
static let cmdOn = Self(rawValue: 1 << 10)
static let cmdOff = Self(rawValue: 1 << 11)
// appearence
static let dark = Self(rawValue: 1 << 12)
static let light = Self(rawValue: 1 << 13)
static let floatingKBOn = Self(rawValue: 1 << 14)
static let floatingKBOff = Self(rawValue: 1 << 15)
// floating cursor
static let floatingCursorOn = Self(rawValue: 1 << 16)
static let floatingCursorOff = Self(rawValue: 1 << 17)
// selection
static let selectionOn = Self(rawValue: 1 << 18)
static let selectionOff = Self(rawValue: 1 << 19)
// clipboard
static let clipboardOn = Self(rawValue: 1 << 20)
static let clipboardOff = Self(rawValue: 1 << 21)
// ime mode
static let imeOff = Self(rawValue: 1 << 22)
static let imeOn = Self(rawValue: 1 << 23)
static let suggestionsOff = Self(rawValue: 1 << 24)
static let suggestionsOn = Self(rawValue: 1 << 25)
// shortcuts
static let orientations:Self = [.portrait, .landscape]
static let kbs: Self = [.hkb, .skb]
static let esc: Self = [.escOn, .escOff]
static let alt: Self = [.altOn, .altOff]
static let ctrl: Self = [.ctrlOn, .ctrlOff]
static let cmd: Self = [.cmdOn, .cmdOff]
static let modifiers:Self = [.esc, .alt, .ctrl, .cmd]
static let styles: Self = [.light, .dark]
static let floatingKB: Self = [.floatingKBOn, .floatingKBOff]
static let floatingCursor: Self = [.floatingCursorOn, .floatingCursorOff]
static let selection: Self = [.selectionOn, .selectionOff]
static let clipboard: Self = [.clipboardOn, .clipboardOff]
static let ime: Self = [.imeOn, .imeOff]
static let suggestions: Self = [.suggestionsOn, .suggestionsOff]
static let all:Self = [
.orientations,
.kbs,
.modifiers,
.styles,
.floatingKB,
.floatingCursor,
.selection,
.clipboard,
.ime,
.suggestions
]
static let initial = Self.all - [
.escOn, .cmdOn, .ctrlOn, .altOn,
.floatingKBOn, .floatingCursorOn,
.portrait, .hkb, .selectionOn, .clipboardOn,
.imeOn, .suggestionsOn
]
static let `default` = Self.all - [
.suggestionsOn, .hkb, .floatingCursorOn
]
static var defaultSuggestionsOnly: Self {
.default + .suggestionsOn - .suggestionsOff
}
}
extension KBTraits {
mutating func toggle(_ value: Bool, on: Self, off: Self) {
if value {
insert(on)
remove(off)
} else {
insert(off)
remove(on)
}
}
var isHKBAttached: Bool {
get { contains(.hkb) }
set { toggle(newValue, on: .hkb, off: .skb) }
}
var isFloatingKB: Bool {
get { contains(.floatingKBOn) }
set { toggle(newValue, on: .floatingKBOn, off: .floatingKBOff)}
}
var isPortrait: Bool {
get { contains(.portrait) }
set { toggle(newValue, on: .portrait, off: .landscape) }
}
var isLandscape: Bool {
get { contains(.landscape) }
set { toggle(newValue, on: .landscape, off: .portrait) }
}
var isFloatingCursor: Bool {
get { contains(.floatingCursorOn) }
set { toggle(newValue, on: .floatingCursorOn, off: .floatingCursorOff) }
}
var hasSelection: Bool {
get { contains(.selectionOn) }
set { toggle(newValue, on: .selectionOn, off: .selectionOff) }
}
var hasClipboard: Bool {
get { contains(.clipboardOn) }
set { toggle(newValue, on: .clipboardOn, off: .clipboardOff) }
}
var hasSuggestions: Bool {
get { contains(.suggestionsOn) }
set { toggle(newValue, on: .suggestionsOn, off: .suggestionsOff) }
}
var isIME: Bool {
get { contains(.imeOn) }
set { toggle(newValue, on: .imeOn, off: .imeOff) }
}
}
extension KBTraits {
init?(cssSelector: String) {
let cssMap:[String: Self] = [
"all": .all,
"orientations": .orientations,
"kbs": .kbs,
"esc": .esc,
"alt": .alt,
"ctrl": .ctrl,
"cmd": .cmd,
"modifiers": .modifiers,
"styles": .styles,
"floatingKB": .floatingKB,
"floatingKBOn": .floatingKBOn,
"floatingKBOff": .floatingKBOff,
// TODO: fill the rest
]
// aliases
// cssMap["all"] = .always
var result = Self(rawValue: 0)
let parts = cssSelector.split(separator: ":")
for var part in parts {
var subtruct = false
if part.hasPrefix("not(") && part.hasSuffix(")") {
part = part["not(".endIndex..<part.endIndex]
subtruct = true
}
let traits = part.split(separator: ".")
for t in traits {
guard let value = cssMap[String(t)] else {
debugPrint("Unknown css class", t)
return nil
}
if subtruct {
result.subtract(value)
} else {
result.insert(value)
}
}
}
self = result
}
}
extension KBTraits {
var modifierFlags: UIKeyModifierFlags {
var flags = UIKeyModifierFlags()
if contains(.cmdOn) {
flags.insert(.command)
}
if contains(.altOn) {
flags.insert(.alternate)
}
if contains(.ctrlOn) {
flags.insert(.control)
}
return flags
}
}
extension KBTraits {
static func - (lhs: KBTraits, rhs: KBTraits) -> KBTraits {
lhs.subtracting(rhs)
}
static func + (lhs: KBTraits, rhs: KBTraits) -> KBTraits {
lhs.union(rhs)
}
}