Skip to content

Commit c93f487

Browse files
committed
feat: support to change user dir through registry
chore: change log dir to %TEMP%\rime.rabbit fix: first run does not load rabbit config fix: too frequently setting ascii mode
1 parent d5b3bf8 commit c93f487

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

Lib/RabbitCommon.ahk

+27-2
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,37 @@ CreateTraits() {
8989
traits.distribution_code_name := RABBIT_CODE_NAME
9090
traits.distribution_version := RABBIT_VERSION
9191
traits.app_name := "rime.rabbit"
92-
traits.shared_data_dir := "Data"
93-
traits.user_data_dir := "Rime"
92+
traits.shared_data_dir := RabbitSharedDataPath()
93+
traits.user_data_dir := RabbitUserDataPath()
94+
traits.log_dir := RabbitLogPath()
9495

9596
return traits
9697
}
9798

99+
RabbitUserDataPath() {
100+
try {
101+
local dir := RegRead("HKEY_CURRENT_USER\Software\Rime\Rabbit", "RimeUserDir")
102+
}
103+
if dir && Type(dir) = "String" {
104+
size := DllCall("ExpandEnvironmentStrings", "Str", dir, "Ptr", 0, "UInt", 0)
105+
path := Buffer(size * 2, 0)
106+
DllCall("ExpandEnvironmentStrings", "Str", dir, "Ptr", path, "UInt", path.Size)
107+
return StrGet(path)
108+
}
109+
return A_ScriptDir . "\Rime"
110+
}
111+
112+
RabbitSharedDataPath() {
113+
return A_ScriptDir . "\Data"
114+
}
115+
116+
RabbitLogPath() {
117+
path := A_Temp . "\rime.rabbit"
118+
if !DirExist(path)
119+
DirCreate(path)
120+
return path
121+
}
122+
98123
OnRimeMessage(context_object, session_id, message_type, message_value) {
99124
msg_type := StrGet(message_type, "UTF-8")
100125
msg_value := StrGet(message_value, "UTF-8")

Lib/RabbitTrayMenu.ahk

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ global TRAY_ASCII_MODE := 0
2525
global TRAY_FULL_SHAPE := 0
2626
global TRAY_ASCII_PUNCT := 0
2727

28-
SetupTrayMenu()
2928
UpdateTrayIcon()
3029

3130
SetupTrayMenu() {
@@ -38,7 +37,7 @@ SetupTrayMenu() {
3837

3938
A_TrayMenu.Add()
4039

41-
A_TrayMenu.Add("用户文件夹", (*) => Run(A_ScriptDir . "\Rime"))
40+
A_TrayMenu.Add("用户文件夹", (*) => Run(rime.get_user_data_dir_s()))
4241
A_TrayMenu.Add("脚本文件夹", (*) => Run(A_ScriptDir))
4342

4443
A_TrayMenu.Add()

Rabbit.ahk

+21-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ RabbitMain(args) {
5252
}
5353
}
5454

55+
; TODO: better handling of first run
56+
local first_run := !FileExist(RabbitUserDataPath() . "\default.custom.yaml")
57+
|| !FileExist(RabbitUserDataPath() . "\rabbit.custom.yaml")
58+
|| !FileExist(RabbitUserDataPath() . "\user.yaml")
59+
5560
rabbit_traits := CreateTraits()
5661
global rime
5762
rime.setup(rabbit_traits)
@@ -60,7 +65,10 @@ RabbitMain(args) {
6065

6166
local m := (args.Length == 0) ? RABBIT_PARTIAL_MAINTENANCE : args[1]
6267
if m != RABBIT_NO_MAINTENANCE {
63-
if rime.start_maintenance(m == RABBIT_FULL_MAINTENANCE)
68+
if first_run {
69+
SetDefaultKeyboard(layout)
70+
Deploy()
71+
} else if rime.start_maintenance(m == RABBIT_FULL_MAINTENANCE)
6472
rime.join_maintenance_thread()
6573
} else {
6674
TrayTip()
@@ -87,6 +95,7 @@ RabbitMain(args) {
8795

8896
UpdateTrayTip(schema_name, ascii_mode, full_shape, ascii_punct)
8997
}
98+
SetupTrayMenu()
9099
OnMessage(AHK_NOTIFYICON, ClickHandler.Bind())
91100
if !RabbitConfig.global_ascii
92101
SetTimer(UpdateWinAscii)
@@ -427,23 +436,28 @@ UpdateWinAscii(target := false, use_target := false, proc_name := "", by_tray_ic
427436
return
428437
}
429438
RabbitGlobals.active_win := proc_name
439+
; TODO: current state might not be accurate due to non-atomic
440+
current := !!rime.get_option(session_id, "ascii_mode")
430441
if use_target {
431442
; force to use passed target
432-
RabbitGlobals.process_ascii[proc_name] := target
443+
RabbitGlobals.process_ascii[proc_name] := !!target
433444
} else if RabbitGlobals.process_ascii.Has(proc_name) {
434445
; not first time to active window, restore the ascii_mode
435446
target := RabbitGlobals.process_ascii[proc_name]
436-
rime.set_option(session_id, "ascii_mode", target)
447+
if current !== target
448+
rime.set_option(session_id, "ascii_mode", target)
437449
} else if RabbitConfig.preset_process_ascii.Has(proc_name) {
438450
; in preset, set ascii_mode as preset
439451
target := RabbitConfig.preset_process_ascii[proc_name]
440-
RabbitGlobals.process_ascii[proc_name] := target
441-
rime.set_option(session_id, "ascii_mode", target)
452+
RabbitGlobals.process_ascii[proc_name] := !!target
453+
if current !== target
454+
rime.set_option(session_id, "ascii_mode", target)
442455
} else {
443456
; not in preset, set ascii_mode to false
444457
target := false
445-
RabbitGlobals.process_ascii[proc_name] := target
446-
rime.set_option(session_id, "ascii_mode", target)
458+
RabbitGlobals.process_ascii[proc_name] := !!target
459+
if current !== target
460+
rime.set_option(session_id, "ascii_mode", target)
447461
}
448462
UpdateTrayTip(, target)
449463
UpdateTrayIcon()

RabbitDeployer.ahk

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ ExitRabbitDeployer(reason, code) {
7070
}
7171

7272
CreateFileIfNotExist(filename) {
73-
user_data_dir := A_ScriptDir . "\Rime\"
73+
user_data_dir := RabbitUserDataPath() . "\"
7474
if not InStr(DirExist(user_data_dir), "D")
7575
DirCreate(user_data_dir)
7676
filepath := user_data_dir . filename

0 commit comments

Comments
 (0)