Skip to content

Commit

Permalink
feat: keep ascii_mode for each process
Browse files Browse the repository at this point in the history
  • Loading branch information
amorphobia committed Sep 14, 2024
1 parent 85e63db commit f11f30f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Lib/RabbitCommon.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class RabbitConfig {
static suspend_hotkey := ""
static show_tips := true
static show_tips_time := 1200
static global_ascii := false
static preset_process_ascii := Map()
static process_ascii := Map()

static load() {
global rime
Expand All @@ -127,6 +130,20 @@ class RabbitConfig {
RabbitConfig.show_tips := false
}

if rime.config_test_get_bool(config, "global_ascii", &result)
RabbitConfig.global_ascii := !!result

if iter := rime.config_begin_map(config, "app_options") {
while rime.config_next(iter) {
proc_name := StrLower(iter.key)
if rime.config_test_get_bool(config, "app_options/" . proc_name . "/ascii_mode", &result) {
RabbitConfig.preset_process_ascii[proc_name] := !!result
RabbitConfig.process_ascii[proc_name] := !!result
}
}
rime.config_end(iter)
}

rime.config_close(config)
}
}
35 changes: 35 additions & 0 deletions Rabbit.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ RabbitMain(args) {
UpdateTrayTip(schema_name, ascii_mode, full_shape, ascii_punct)
}
OnMessage(AHK_NOTIFYICON, ClickHandler.Bind())
if !RabbitConfig.global_ascii
SetTimer(UpdateWinAscii)

OnExit(ExitRabbit.Bind(layout))
}
Expand Down Expand Up @@ -294,6 +296,7 @@ ProcessKey(key, mask, this_hotkey) {
local ascii_changed := false
if old_ascii_mode != new_ascii_mode {
ascii_changed := true
UpdateWinAscii(true)
status_text := new_ascii_mode ? ASCII_MODE_TRUE_LABEL_ABBR : ASCII_MODE_FALSE_LABEL_ABBR
} else if old_full_shape != new_full_shape {
status_changed := true
Expand Down Expand Up @@ -405,3 +408,35 @@ UpdateStateLabels() {
slice := rime.get_state_label_abbreviated(session_id, "ascii_punct", true, true)
ASCII_PUNCT_TRUE_LABEL_ABBR := (slice and slice.slice !== "") ? slice.slice : "."
}

UpdateWinAscii(set_to_current := false) {
if A_IsSuspended
return
global rime, session_id
act := WinExist("A")
if !act || !rime || !session_id {
return
}
proc_name := StrLower(WinGetProcessName())
current := rime.get_option(session_id, "ascii_mode")
target := !!current
if set_to_current {
; force to keep current mode
RabbitConfig.process_ascii[proc_name] := target
} else if RabbitConfig.process_ascii.Has(proc_name) {
; not first time to active window, restore the ascii_mode
target := RabbitConfig.process_ascii[proc_name]
rime.set_option(session_id, "ascii_mode", target)
} else if RabbitConfig.preset_process_ascii.Has(proc_name) {
; in preset, set ascii_mode as preset
target := RabbitConfig.preset_process_ascii[proc_name]
RabbitConfig.process_ascii[proc_name] := target
rime.set_option(session_id, "ascii_mode", target)
} else {
; not in preset, set ascii_mode to false
target := false
RabbitConfig.process_ascii[proc_name] := target
rime.set_option(session_id, "ascii_mode", target)
}
UpdateTrayTip(, target)
}
11 changes: 11 additions & 0 deletions schemas/rabbit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ suspend_hotkey: null
show_tips: true
# How long a period tips shows
show_tips_time: 1200

app_options:
cmd.exe:
ascii_mode: true
conhost.exe:
ascii_mode: true
windowsterminal.exe:
ascii_mode: true

# Use global ascii mode
global_ascii: false

0 comments on commit f11f30f

Please sign in to comment.