Skip to content

Commit dd2d2b0

Browse files
committed
feat: tray menu deploy and sync
1 parent afced4d commit dd2d2b0

File tree

6 files changed

+129
-42
lines changed

6 files changed

+129
-42
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
cp Lib/librime-ahk/rime_api.ahk Lib/librime-ahk/LICENSE temp/Lib/librime-ahk/
3737
cp Lib/*.ahk temp/Lib/
3838
cp -r SharedSupport temp/
39-
cp dist/lib/rime.dll Rabbit.ahk Rabbit.exe Rabbit.ico LICENSE temp/
39+
cp dist/lib/rime.dll Rabbit.ahk Rabbit.exe Rabbit.ico RabbitDeployer.ahk LICENSE temp/
4040
cd temp && zip -r -q ../rabbit.zip *
4141
4242
- name: Upload Artifacts

Lib/RabbitCommon.ahk

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ global rime := RimeApi()
2222
global RABBIT_IME_NAME := "玉兔毫"
2323
global RABBIT_CODE_NAME := "Rabbit"
2424
global RABBIT_VERSION := "0.1.0"
25+
global RABBIT_NO_MAINTENANCE := "0"
26+
global RABBIT_PARTIAL_MAINTENANCE := "1"
27+
global RABBIT_FULL_MAINTENANCE := "2"
28+
29+
global ERROR_ALREADY_EXISTS := 183
30+
31+
class RabbitMutex {
32+
handle := 0
33+
errmsg := ""
34+
Create() {
35+
this.errmsg := ""
36+
this.handle := DllCall("CreateMutex", "Ptr", 0, "Int", true, "Str", "RabbitDeployerMutex")
37+
if DllCall("GetLastError") == ERROR_ALREADY_EXISTS {
38+
this.Close()
39+
this.errmsg := "mutex already exists"
40+
}
41+
return this.handle
42+
}
43+
Close() {
44+
if this.handle {
45+
DllCall("CloseHandle", "Ptr", this.handle)
46+
this.handle := 0
47+
}
48+
}
49+
}
2550

2651
CreateTraits() {
2752
traits := RimeTraits()

Lib/RabbitTrayMenu.ahk

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,38 @@
1616
*
1717
*/
1818

19+
global TRAY_MENU_GRAYOUT
20+
1921
TraySetIcon("Rabbit.ico") ; https://www.freepik.com/icon/rabbit_4905239
2022
A_TrayMenu.Delete()
2123
; A_TrayMenu.add("输入法设定")
2224
; A_TrayMenu.add("用户词典管理")
23-
A_TrayMenu.add("用户资料同步", (*) => false)
25+
A_TrayMenu.add("用户资料同步", (*) => Sync())
2426
A_TrayMenu.add()
2527
A_TrayMenu.add("用户文件夹", (*) => Run(A_ScriptDir . "\Rime"))
2628
A_TrayMenu.add("脚本文件夹", (*) => Run(A_ScriptDir))
2729
A_TrayMenu.add()
2830
A_TrayMenu.add("仓库主页", (*) => Run("https://github.com/amorphobia/rabbit"))
2931
A_TrayMenu.add()
30-
A_TrayMenu.add("重新部署", (*) => Reload())
32+
A_TrayMenu.add("重新部署", (*) => Deploy())
3133
A_TrayMenu.add("退出玉兔毫", (*) => ExitApp())
3234

33-
; A_TrayMenu.Disable("输入法设定")
34-
; A_TrayMenu.Disable("用户词典管理")
35-
A_TrayMenu.Disable("用户资料同步")
35+
Sync() {
36+
Run(A_AhkPath . " " . A_ScriptDir . "\RabbitDeployer.ahk sync 1")
37+
ExitApp()
38+
}
39+
Deploy() {
40+
Run(A_AhkPath . " " . A_ScriptDir . "\RabbitDeployer.ahk deploy 1")
41+
ExitApp()
42+
}
43+
44+
if TRAY_MENU_GRAYOUT {
45+
; A_TrayMenu.Disable("输入法设定")
46+
; A_TrayMenu.Disable("用户词典管理")
47+
A_TrayMenu.Disable("用户资料同步")
48+
A_TrayMenu.Disable("用户文件夹")
49+
A_TrayMenu.Disable("脚本文件夹")
50+
A_TrayMenu.Disable("仓库主页")
51+
A_TrayMenu.Disable("重新部署")
52+
A_TrayMenu.Disable("退出玉兔毫")
53+
}

Lib/librime-ahk

Rabbit.ahk

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#Requires AutoHotkey v2.0 32-bit
1919
#SingleInstance Ignore
2020

21+
global TRAY_MENU_GRAYOUT := false
22+
2123
#Include <RabbitCommon>
2224
#Include <RabbitKeyTable>
2325
#Include <RabbitCandidateBox>
@@ -27,21 +29,43 @@
2729

2830
global session_id := 0
2931
global box := Gui()
32+
global mutex := RabbitMutex()
33+
34+
global STATUS_TOOLTIP := 2
3035

3136
RegisterHotKeys()
32-
RabbitMain()
37+
RabbitMain(A_Args)
3338

34-
RabbitMain() {
39+
RabbitMain(args) {
3540
local layout := DllCall("GetKeyboardLayout", "UInt", 0)
3641
SetDefaultKeyboard()
3742

43+
fail_count := 0
44+
while not mutex.Create() {
45+
fail_count++
46+
if fail_count > 500 {
47+
TrayTip()
48+
TrayTip("有其他进程正在使用 RIME,启动失败")
49+
Sleep(2000)
50+
ExitApp()
51+
}
52+
}
53+
3854
rabbit_traits := CreateTraits()
3955
global rime
4056
rime.setup(rabbit_traits)
4157
rime.set_notification_handler(OnMessage, 0)
4258
rime.initialize(rabbit_traits)
43-
if rime.start_maintenace(true)
44-
rime.join_maintenance_thread()
59+
60+
local m := (args.Length == 0) ? RABBIT_PARTIAL_MAINTENANCE : args[1]
61+
if m != RABBIT_NO_MAINTENANCE {
62+
if rime.start_maintenance(m == RABBIT_FULL_MAINTENANCE)
63+
rime.join_maintenance_thread()
64+
} else {
65+
TrayTip()
66+
TrayTip("维护完成", RABBIT_IME_NAME)
67+
SetTimer(TrayTip, -2000)
68+
}
4569

4670
global session_id := rime.create_session()
4771
if not session_id {
@@ -75,10 +99,15 @@ SetDefaultKeyboard(locale_id := 0x0409) {
7599

76100
ExitRabbit(layout, reason, code) {
77101
SetDefaultKeyboard(layout)
102+
TrayTip()
103+
ToolTip()
104+
ToolTip(, , , STATUS_TOOLTIP)
78105
if session_id {
79106
rime.destroy_session(session_id)
80107
rime.finalize()
81108
}
109+
if mutex
110+
mutex.Close()
82111
}
83112

84113
RegisterHotKeys() {
@@ -169,7 +198,6 @@ ProcessKey(key, mask, this_hotkey) {
169198
if not code
170199
return
171200

172-
static STATUS_TOOLTIP := 2
173201
if status := rime.get_status(session_id) {
174202
local old_ascii_mode := status.is_ascii_mode
175203
local old_full_shape := status.is_full_shape

RabbitDeployer.ahk

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,55 @@
1616
*
1717
*/
1818
#Requires AutoHotkey v2.0 32-bit
19-
; #NoTrayIcon
19+
20+
global TRAY_MENU_GRAYOUT := true
2021

2122
#Include <RabbitCommon>
23+
#Include <RabbitTrayMenu>
2224
#Include <librime-ahk\rime_levers_api>
2325

2426
global rime
2527
global ERROR_ALREADY_EXISTS := 183
2628
global INVALID_FILE_ATTRIBUTES := -1
2729
global FILE_ATTRIBUTE_DIRECTORY := 0x00000010
2830

29-
arg := A_Args.Length > 0 ? A_Args[1] : ""
30-
RunDeployer(arg)
31+
OnExit(ExitRabbitDeployer)
32+
33+
RunDeployer(A_Args)
34+
35+
RunDeployer(args) {
36+
TrayTip()
37+
TrayTip("维护中", RABBIT_IME_NAME)
3138

32-
RunDeployer(command) {
39+
command := args.Length > 0 ? args[1] : ""
3340
conf := Configurator()
3441
conf.Initialize()
35-
deployment_scheduled := command == "deploy"
36-
if deployment_scheduled
37-
return conf.UpdateWorkspace()
38-
dict_management := command == "dict"
39-
if dict_management
40-
return 0 ; return conf.DictManagement()
41-
sync_user_dict := command == "sync"
42-
if sync_user_dict
43-
return conf.SyncUserData()
44-
installing := command == "install"
45-
return conf.Run(installing)
42+
switch command {
43+
case "deploy":
44+
res := conf.UpdateWorkspace()
45+
opt := RABBIT_NO_MAINTENANCE
46+
case "dict":
47+
res := 0 ; conf.DictManagement()
48+
opt := RABBIT_PARTIAL_MAINTENANCE
49+
case "sync":
50+
res := conf.SyncUserData()
51+
opt := RABBIT_PARTIAL_MAINTENANCE
52+
default:
53+
res := conf.Run(command = "install")
54+
opt := RABBIT_PARTIAL_MAINTENANCE ; TODO: check if need maintenance
55+
}
56+
57+
if args.Length > 1 {
58+
sp := " "
59+
target := A_AhkPath . sp . A_ScriptDir . "\Rabbit.ahk"
60+
Run(target . sp . opt . sp . String(res))
61+
ExitApp()
62+
}
63+
return res
64+
}
65+
66+
ExitRabbitDeployer(reason, code) {
67+
TrayTip()
4668
}
4769

4870
CreateFileIfNotExist(filename) {
@@ -87,12 +109,9 @@ class Configurator extends Class {
87109
}
88110

89111
UpdateWorkspace(report_errors := false) {
90-
hMutex := DllCall("CreateMutex", "Ptr", 0, "Int", true, "Str", "RabbitDeployerMutex")
91-
if not hMutex {
92-
return 1
93-
}
94-
if DllCall("GetLastError") == ERROR_ALREADY_EXISTS {
95-
DllCall("CloseHandle", "Ptr", hMutex)
112+
mutex := RabbitMutex()
113+
if not mutex.Create() {
114+
; TODO: log error
96115
return 1
97116
}
98117

@@ -101,32 +120,29 @@ class Configurator extends Class {
101120
; rime.deploy_config_file("rabbit.yaml", "config_version")
102121
}
103122

104-
DllCall("CloseHandle", "Ptr", hMutex)
123+
mutex.Close()
105124

106125
return 0
107126
}
108127

109128
; DictManagement()
110129

111130
SyncUserData() {
112-
hMutex := DllCall("CreateMutex", "Ptr", 0, "Int", true, "Str", "RabbitDeployerMutex")
113-
if not hMutex {
114-
return 1
115-
}
116-
if DllCall("GetLastError") == ERROR_ALREADY_EXISTS {
117-
DllCall("CloseHandle", "Ptr", hMutex)
131+
mutex := RabbitMutex()
132+
if not mutex.Create() {
133+
; TODO: log error
118134
return 1
119135
}
120136

121137
{
122138
if not rime.sync_user_data() {
123-
DllCall("CloseHandle", "Ptr", hMutex)
139+
mutex.Close()
124140
return 1
125141
}
126142
rime.join_maintenance_thread()
127143
}
128144

129-
DllCall("CloseHandle", "Ptr", hMutex)
145+
mutex.Close()
130146

131147
return 0
132148
}

0 commit comments

Comments
 (0)