-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathactions.py
More file actions
285 lines (228 loc) · 7.92 KB
/
actions.py
File metadata and controls
285 lines (228 loc) · 7.92 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from __future__ import annotations
from enum import Enum, unique
from typing import Literal
class ActionDefaultArguments:
scan_targets = ["、", "。", "!", "?", ".", ",", ".", ",", "\n"]
@unique
class ScanDirection(str, Enum):
forward = "forward"
backward = "backward"
class Action:
"""
全てのActionの継承元となるクラス。
metaclassによって、自動的に割り当てられる。
"""
pass
class ActionMeta(type):
"""
全てのActionのメタクラス。
メンバー`type`を持つかの検証と、継承元の割り当てを行う。
Actionを作る際は、このクラスをmetaclassとして指定すればよい。
"""
def __new__(meta, name, bases, attributes):
bases = (Action,)
if not "type" in attributes:
raise TypeError("Action has no type!")
return type.__new__(meta, name, bases, attributes)
class InputAction(metaclass=ActionMeta):
type = "input"
def __init__(self, text: str):
"""
文字を入力するアクション
Parameters
----------
text: str
入力する文字
"""
self.text = text
class ReplaceLastCharactersAction(metaclass=ActionMeta):
type = "replace_last_characters"
def __init__(self, table: dict[str, str]):
"""
最後の文字を置換するアクション
Parameters
----------
table: dict[str, str]
置換に用いるテーブル
"""
self.table = table
@unique
class ReplaceType(str, Enum):
default = "default"
dakuten = "dakuten"
handakuten = "handakuten"
kogaki = "kogaki"
class ReplaceDefaultAction(metaclass=ActionMeta):
"""
azooKeyデフォルトの置換アクション
"""
type = "replace_default"
def __init__(self, replace_type: ReplaceType = ReplaceType.default, fallbacks: list[ReplaceType] = []):
"""
最後の文字を置換するアクション
Parameters
----------
replace_type: ReplaceType
置換の種類。
fallbacks: list[ReplaceType]
最初に設定した置換が成功しなかった場合のフォールバック
"""
self.replace_type = replace_type
self.fallbacks = fallbacks
@unique
class TabType(str, Enum):
system = "system"
custom = "custom"
class MoveTabAction(metaclass=ActionMeta):
type = "move_tab"
def __init__(self, tab_type: TabType, text: str):
"""
タブを移動するアクション
Parameters
----------
tab_type: TabType
タブのタイプ。"custom"または"system"を指定。
text: str
タブの識別子
"""
self.tab_type = tab_type
self.identifier = text
class MoveCursorAction(metaclass=ActionMeta):
type = "move_cursor"
def __init__(self, count: int):
"""
カーソルを移動するアクション
Parameters
----------
count: int
移動する文字数。負の値を指定した場合文頭方向に移動。
"""
self.count = count
class SmartMoveCursorAction(metaclass=ActionMeta):
type = "smart_move_cursor"
def __init__(self, direction: ScanDirection, targets: list[str]):
"""
指定した文字の隣までカーソルを移動するアクション
Parameters
----------
direction: ScanDirection
移動の向きを"forward"または"backward"で指定。
targets: list[str]
停止条件となる文字のリスト。
"""
self.direction = direction
self.targets = targets
class LaunchApplicationAction(metaclass=ActionMeta):
type = "launch_application"
def __init__(self, scheme_type: Literal['azooKey', 'shortcuts'], target: str):
"""
アプリケーションを起動するアクション。
schemeとしてazooKeyを選んだ場合、'azooKey://' + targetを開く。
schemeとしてshortcutsを選んだ場合、'shortcuts://' + targetを開く。
target = run-shortcut?name=<ショートカット名>とすることで、任意のショートカットを開くことができる。
Parameters
----------
scheme_type: Literal['azooKey', 'shortcuts']
target: str
"""
self.scheme_type = scheme_type
self.target = target
class SelectCandidateAction(metaclass=ActionMeta):
type = "select_candidate"
def __init__(self, selection_type: Literal['first', 'last', 'exact', 'offset'], value: int = None):
"""
候補を選択するアクション。
Parameters
----------
selection_type: Literal['first', 'last', 'exact', 'offset']
value: int, selection_typeとして'exact'と'offset'を選んだ場合、valueの引数をあわせて指定する
"""
if value is not None:
self.selection = { "type": selection_type, "value": value }
else:
self.selection = { "type": selection_type }
class DeleteAction(metaclass=ActionMeta):
type = "delete"
def __init__(self, count: int):
"""
文字を削除するアクション
Parameters
----------
count: int
削除する文字数。負の値を指定した場合は文末方向の文字を削除。
"""
self.count = count
class SmartDeleteAction(metaclass=ActionMeta):
type = "smart_delete"
def __init__(self, direction: ScanDirection, targets: list[str]):
"""
指定した文字の隣まで文字を削除するアクション
Parameters
----------
direction: ScanDirection
削除する向きを"forward"または"backward"で指定。
targets: list[str]
停止条件となる文字のリスト。
"""
self.direction = direction
self.targets = targets
class SmartDeleteDefaultAction(metaclass=ActionMeta):
"""
azooKeyデフォルトの文頭まで削除アクション
"""
type = "smart_delete_default"
class EnableResizingModeAction(metaclass=ActionMeta):
"""
片手モードの調整を始めるアクション
"""
type = "enable_resizing_mode"
class ToggleCursorBarAction(metaclass=ActionMeta):
"""
カーソルバーの表示状態をtoggleするアクション
"""
type = "toggle_cursor_bar"
class ToggleTabBarAction(metaclass=ActionMeta):
"""
タブバーの表示状態をtoggleするアクション
"""
type = "toggle_tab_bar"
class ToggleCapsLockStateAction(metaclass=ActionMeta):
"""
Caps lockの状態をtoggleするアクション
"""
type = "toggle_caps_lock_state"
class DismissKeyboardAction(metaclass=ActionMeta):
"""
キーボードを閉じるアクション
"""
type = "dismiss_keyboard"
class CompleteAction(metaclass=ActionMeta):
"""
確定するアクション
"""
type = "complete"
class PasteAction(metaclass=ActionMeta):
"""
ペーストを実行するアクション
"""
type = "paste"
@unique
class LongpressDuration(str, Enum):
normal = "normal"
light = "light"
class LongpressAction(object):
def __init__(self, duration: LongpressDuration = LongpressDuration.normal, start: list[Action] = [], repeat: list[Action] = []):
"""
イニシャライザ
Parameters
----------
duration: LongpressDuration = .normal
長押しが成立する判定までにかかる時間の目安
start: list[dict] = []
長押しが成立した段階で実行されるアクションのリスト
repeat: list[dict] = []
長押しが成立している間繰り返し実行されるアクションのリスト
"""
self.duration = duration
self.start = start
self.repeat = repeat