-
Notifications
You must be signed in to change notification settings - Fork 3
/
RabbitDeployer.ahk
453 lines (394 loc) · 15 KB
/
RabbitDeployer.ahk
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright (c) 2023, 2024 Xuesong Peng <[email protected]>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#Requires AutoHotkey v2.0
#SingleInstance Ignore
;@Ahk2Exe-SetInternalName rabbit-deployer
;@Ahk2Exe-SetProductName 玉兔毫部署应用
;@Ahk2Exe-SetOrigFilename RabbitDeployer.ahk
#Include <RabbitCommon>
#Include <RabbitTrayMenu>
global IN_MAINTENANCE := true
global rime
global INVALID_FILE_ATTRIBUTES := -1
global FILE_ATTRIBUTE_DIRECTORY := 0x00000010
OnExit(ExitRabbitDeployer)
RunDeployer(A_Args)
RunDeployer(args) {
IN_MAINTENANCE := true
UpdateTrayIcon()
TrayTip()
TrayTip("维护中", RABBIT_IME_NAME)
SetupTrayMenu()
command := args.Length > 0 ? args[1] : ""
conf := Configurator()
conf.Initialize()
switch command {
case "deploy":
res := conf.UpdateWorkspace()
opt := RABBIT_NO_MAINTENANCE
case "dict":
res := conf.DictManagement()
opt := RABBIT_PARTIAL_MAINTENANCE
case "sync":
res := conf.SyncUserData()
opt := RABBIT_PARTIAL_MAINTENANCE
default:
res := conf.Run(command = "install")
opt := RABBIT_NO_MAINTENANCE
}
if args.Length > 1 {
Run(Format("{} `"{}\Rabbit.ahk`" {} {}", A_AhkPath, A_ScriptDir, opt, res))
ExitApp()
}
return res
}
ExitRabbitDeployer(reason, code) {
TrayTip()
}
CreateFileIfNotExist(filename) {
user_data_dir := RabbitUserDataPath() . "\"
if not InStr(DirExist(user_data_dir), "D")
DirCreate(user_data_dir)
filepath := user_data_dir . filename
if not InStr(FileExist(filepath), "N")
FileAppend("", filepath)
}
ConfigureSwitcher(levers, switcher_settings, &reconfigured) {
if !IsSet(reconfigured)
reconfigured := false
if not levers.load_settings(switcher_settings)
return false
; To mimic a dialog
result := {
yes : false
}
dialog := SwitcherSettingsDialog(switcher_settings, result)
dialog.Show()
WinWaitClose(dialog)
if result.yes {
if levers.save_settings(switcher_settings)
reconfigured := true
return true
}
return false
}
class Configurator extends Class {
__New() {
CreateFileIfNotExist("default.custom.yaml")
CreateFileIfNotExist("rabbit.custom.yaml")
}
Initialize() {
rabbit_traits := CreateTraits()
rime.setup(rabbit_traits)
rime.deployer_initialize(0)
}
Run(installing) {
levers := RimeLeversApi()
if not levers
return 1
switcher_settings := levers.switcher_settings_init()
skip_switcher_settings := installing && !levers.is_first_run(switcher_settings)
if !skip_switcher_settings {
ConfigureSwitcher(levers, switcher_settings, &reconfigured)
}
levers.custom_settings_destroy(switcher_settings)
if installing || reconfigured
return this.UpdateWorkspace()
return 0
}
UpdateWorkspace(report_errors := false) {
mutex := RabbitMutex()
if not mutex.Create() {
; TODO: log error
return 1
}
if mutex.lasterr == ERROR_ALREADY_EXISTS {
; TODO: log error
mutex.Close()
if report_errors {
MsgBox("正在执行另一项部署任务,方才所做的修改将在输入法再次启动后生效。", "【玉兔毫】", "Ok Iconi")
}
return 1
}
{
rime.deploy()
rime.deploy_config_file("rabbit.yaml", "config_version")
}
mutex.Close()
return 0
}
DictManagement() {
mutex := RabbitMutex()
if not mutex.Create() {
; TODO: log error
return 1
}
if mutex.lasterr == ERROR_ALREADY_EXISTS {
; TODO: log error
mutex.Close()
MsgBox("正在执行另一项部署任务,请稍后再试。", "【玉兔毫】", "Ok Iconi")
return 1
}
{
if rime.api_available("run_task") {
rime.run_task("installation_update")
}
dialog := DictManagementDialog()
dialog.Show()
WinWaitClose(dialog)
}
mutex.Close()
return 0
}
SyncUserData() {
mutex := RabbitMutex()
if not mutex.Create() {
; TODO: log error
return 1
}
if mutex.lasterr == ERROR_ALREADY_EXISTS {
; TODO: log error
mutex.Close()
MsgBox("正在执行另一项部署任务,请稍后再试。", "【玉兔毫】", "Ok Iconi")
return 1
}
{
if not rime.sync_user_data() {
mutex.Close()
return 1
}
rime.join_maintenance_thread()
}
mutex.Close()
return 0
}
}
class DictManagementDialog extends Gui {
__New() {
super.__New("-MaximizeBox -MinimizeBox", "【玉兔毫】用户词典管理", this)
this.api := RimeLeversApi()
; Layout
this.MarginX := 15
this.MarginY := 15
this.AddText(, "用户词典列表:")
this.dict_list := this.AddListBox("w190 h270", [])
this.dict_list.OnEvent("Change", (*) => this.OnUserDictListSelChange())
this.AddText("Section X+25 YP w315", " 当你需要将包含输入习惯的用户词典迁移到另一份配备了 Rime 输入法的系统,请在左列选中词典名称,「输出词典快照」将快照文件传到另一系统上,「合入词典快照」快照文件中的词条将合并到其所属的词典中。")
this.backup := this.AddButton("Disabled Y+30 w150", "输出词典快照")
this.backup.OnEvent("Click", (*) => this.OnBackup())
this.AddButton("X+20 YP w150", "合入词典快照").OnEvent("Click", (*) => this.OnRestore())
this.AddText("XS w315", "「导出文本码表」是为输入方案制作者设计的功能,将使用期间新造的词组以 Rime 词典中的码表格式导出,以便查看、编辑。「导入文本码表」可用于将其他来源的词库整理成 TSV 格式后导入到 Rime。在 Rime 输入法之间转移数据,请使用词典快照。")
this.export := this.AddButton("Disabled Y+30 w150", "导出文本码表")
this.export.OnEvent("Click", (*) => this.OnExport())
this.import := this.AddButton("Disabled X+20 YP w150", "导入文本码表")
this.import.OnEvent("Click", (*) => this.OnImport())
this.Populate()
}
Populate() {
if !iter := this.api.user_dict_iterator_init() {
return
}
while dict := this.api.next_user_dict(iter) {
this.dict_list.Add([dict])
}
this.api.user_dict_iterator_destroy(iter)
this.dict_list.Choose(0)
}
OnBackup() {
local sel := this.dict_list.Value
if sel <= 0 || sel > ControlGetItems(this.dict_list).Length {
MsgBox("请在左列选择要导出的词典名称。", ":-(", "Ok Iconi")
return
}
local path := rime.get_user_data_sync_dir()
if !DirExist(path) {
try {
DirCreate(path)
} catch {
MsgBox("未能完成导出操作。会不会是同步文件夹无法访问?", ":-(", "Ok Iconx")
return
}
}
local dict_name := this.dict_list.Text
file := path . "\" . dict_name . ".userdb.txt"
if !this.api.backup_user_dict(dict_name) {
MsgBox("不知哪里出错了,未能完成导出操作。", ":-(", "Ok Iconx")
return
} else if !FileExist(file) {
MsgBox("咦,输出的快照文件找不着了。", ":-(", "Ok Iconx")
return
}
Run(A_ComSpec . " /c explorer.exe /select,`"" . file . "`"", , "Hide")
}
OnRestore() {
local filter := "词典快照 (*.userdb.txt; *.userdb.kct.snapshot)"
if selected_path := FileSelect("1", , "打开", filter) { ; file must exist
if !this.api.restore_user_dict(selected_path)
MsgBox("不知哪里出错了,未能完成操作。", ":-(", "Ok Iconx")
else
MsgBox("完成了。", ":-)", "Ok Iconi")
}
}
OnExport() {
local sel := this.dict_list.Value
if sel <= 0 || sel > ControlGetItems(this.dict_list).Length {
MsgBox("请在左列选择要导出的词典名称。", ":-(", "Ok Iconi")
return
}
local dict_name := this.dict_list.Text
local file_name := dict_name . "_export.txt"
local filter := "文本文档 (*.txt)"
if selected_path := FileSelect("S18", file_name, "另存为", filter) { ; path must exist + warning on overwriting
if SubStr(selected_path, -4) != ".txt"
selected_path .= ".txt"
local result := this.api.export_user_dict(dict_name, selected_path)
if result < 0
MsgBox("不知哪里出错了,未能完成操作。", ":-(", "Ok Iconx")
else if !FileExist(selected_path)
MsgBox("咦,导出的文件找不着了。", ":-(", "Ok Iconx")
else {
MsgBox("导出了 " . result . " 条记录。", ":-)", "Ok Iconi")
Run(A_ComSpec . " /c explorer.exe /select,`"" . selected_path . "`"", , "Hide")
}
}
}
OnImport() {
local dict_name := this.dict_list.Text
local file_name := dict_name . "_export.txt"
local filter := "文本文档 (*.txt)"
if selected_path := FileSelect("1", file_name, "打开", filter) { ; file must exist
local result := this.api.import_user_dict(dict_name, selected_path)
if result < 0
MsgBox("不知哪里出错了,未能完成操作。", ":-(", "Ok Iconx")
else
MsgBox("导入了 " . result . " 条记录。", ":-)", "Ok Iconi")
}
}
OnUserDictListSelChange() {
local index := this.dict_list.Value
local enabled := index <= 0 ? false : true
this.backup.Enabled := enabled
this.export.Enabled := enabled
this.import.Enabled := enabled
}
}
class SwitcherSettingsDialog extends Gui {
__New(settings, result) {
super.__New("-MaximizeBox -MinimizeBox", "【玉兔毫】方案选单设定", this)
this.settings := settings
this.loaded := false
this.modified := false
this.api := RimeLeversApi()
this.item_data := Map()
this.result := result
; Layout
this.MarginX := 15
this.MarginY := 15
this.AddText(, "请勾选所需的输入方案:")
this.schema_list := this.AddListView("Section Checked NoSort w220 h175", ["方案名称"])
this.schema_list.OnEvent("Click", (ctrl, lvid) => this.OnSchemaListClick(lvid))
this.schema_list.OnEvent("ItemCheck", (ctrl, lvid, checked) => this.OnSchemaListItemCheck(lvid, checked))
this.description := this.AddText("YP w285 h175", "选中列表中的输入方案以查看简介")
this.AddText("XS", "在玉兔毫里,以下快捷键可唤出方案选单,以切换模式或选用其他输入方案。")
this.hotkeys := this.AddEdit("-Multi ReadOnly r1 w505")
this.more_schemas := this.AddButton("Disabled w155", "获取更多输入方案…")
this.ok := this.AddButton("X+60 YP w90", "中")
this.ok.OnEvent("Click", (*) => this.OnOK())
this.Populate()
}
Populate() {
if !this.settings
return
local available := this.api.get_available_schema_list(this.settings)
local selected := this.api.get_selected_schema_list(this.settings)
this.schema_list.Delete()
local recruited := Map()
local selected_list := selected.list
local available_list := available.list
Loop selected.size {
local schema_id := selected_list[A_Index].schema_id
Loop available.size {
item := available_list[A_Index]
info := RimeSchemaInfo(item)
if item.schema_id == schema_id && (!recruited.Has(info.Ptr) || recruited[info.Ptr] == false) {
recruited[info.Ptr] := true
row := this.schema_list.Add("Check", item.name)
this.item_data[row] := info
break
}
}
}
Loop available.size {
item := available_list[A_Index]
info := RimeSchemaInfo(item)
if !recruited.Has(info.Ptr) || recruited[info.Ptr] == false {
recruited[info.Ptr] := true
row := this.schema_list.Add(, item.name)
this.item_data[row] := info
}
}
txt := this.api.get_hotkeys(this.settings)
this.hotkeys.Value := txt
this.loaded := true
this.modified := false
}
OnSchemaListClick(lvid) {
if !this.loaded || !this.schema_list || lvid <= 0 || lvid > this.schema_list.GetCount() {
return
}
this.ShowDetails(this.item_data[lvid])
}
OnSchemaListItemCheck(lvid, checked) {
if !this.loaded || !this.schema_list || lvid <= 0 || lvid > this.schema_list.GetCount() {
return
}
this.modified := true
}
ShowDetails(info) {
if !info
return
details := ""
if name := this.api.get_schema_name(info)
details .= name
if author := this.api.get_schema_author(info)
details .= "`r`n`r`n" . author
if description := this.api.get_schema_description(info)
details .= "`r`n`r`n" . description
this.description.Value := details
}
OnOK() {
if this.modified && !!this.settings && this.schema_list.GetCount() != 0 {
selection := []
row := 0
while row := this.schema_list.GetNext(row, "Checked") {
if info := this.item_data[row]
selection.Push(this.api.get_schema_id(info))
}
if selection.Length == 0 {
MsgBox("至少要选用一项吧。", "玉兔毫不是这般用法", "Icon!")
return
}
this.api.select_schemas(this.settings, selection)
}
this.Exit(true)
}
Exit(yes) {
this.result.yes := yes
this.Destroy()
}
}