Skip to content

Commit

Permalink
feat: options to set tips show and time
Browse files Browse the repository at this point in the history
  • Loading branch information
amorphobia committed Sep 10, 2024
1 parent 07302f5 commit 67f231c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 70 deletions.
23 changes: 23 additions & 0 deletions Lib/RabbitCommon.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,26 @@ OnRimeMessage(context_object, session_id, message_type, message_value) {
; TrayTip(msg_type . ": " . msg_value . " (" . session_id . ")", RABBIT_IME_NAME)
}
}

class RabbitConfig {
static suspend_hotkey := ""
static show_tips := true
static show_tips_time := 1200

static load() {
global rime
if !rime || !config := rime.config_open("rabbit")
return

RabbitConfig.suspend_hotkey := rime.config_get_string(config, "suspend_hotkey")
if rime.config_test_get_bool(config, "show_tips", &result)
RabbitConfig.show_tips := !!result
if rime.config_test_get_int(config, "show_tips_time", &result) {
RabbitConfig.show_tips_time := Abs(result)
if result == 0
RabbitConfig.show_tips := false
}

rime.config_close(config)
}
}
12 changes: 8 additions & 4 deletions Lib/RabbitTrayMenu.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ ToggleSuspend() {
Suspend(-1)
TraySetIcon(A_IsSuspended ? "Lib\rabbit-alt.ico" : "Lib\rabbit.ico", , true)
UpdateTrayTip()
ToolTip(A_IsSuspended ? "禁用" : "启用", , , STATUS_TOOLTIP)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -2000)
if RabbitConfig.show_tips {
ToolTip(A_IsSuspended ? "禁用" : "启用", , , STATUS_TOOLTIP)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -RabbitConfig.show_tips_time)
}
}

if TRAY_MENU_GRAYOUT {
Expand All @@ -80,8 +82,10 @@ ClickHandler(wParam, lParam, msg, hWnd) {
local new_ascii_mode := rime.get_option(session_id, "ascii_mode")
UpdateTrayTip(, new_ascii_mode)
status_text := new_ascii_mode ? ASCII_MODE_TRUE_LABEL_ABBR : ASCII_MODE_FALSE_LABEL_ABBR
ToolTip(status_text, , , STATUS_TOOLTIP)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -2000)
if RabbitConfig.show_tips {
ToolTip(status_text, , , STATUS_TOOLTIP)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -RabbitConfig.show_tips_time)
}
}
}

Expand Down
116 changes: 50 additions & 66 deletions Rabbit.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ RabbitMain(args) {
throw Error("未能成功创建 RIME 会话。")
}

RabbitConfig.load()
RegisterHotKeys()
UpdateStateLabels()
if status := rime.get_status(session_id) {
Expand Down Expand Up @@ -175,73 +176,56 @@ RegisterHotKeys() {
}

; Read the hotkey to suspend / resume Rabbit
if config := rime.config_open("rabbit") {
global suspend_hotkey_mask := 0
global suspend_hotkey := ""
local suspend_hotkey_set := false
local suspend_hotkey_conf := rime.config_get_string(config, "suspend_hotkey")
rime.config_close(config)
if suspend_hotkey_conf {
local keys := StrSplit(suspend_hotkey_conf, "+", " ", 4)
local mask := 0
local target_key := ""
local num_modifiers := 0
for k in keys {
if k = "Control" {
num_modifiers += !(mask & ctrl)
mask |= ctrl
} else if k = "Alt" {
num_modifiers += !(mask & alt)
mask |= alt
} else if k = "Shift" {
num_modifiers += !(mask & shift)
mask |= shift
} else if not target_key {
target_key := k
}
}
global suspend_hotkey_mask := 0
global suspend_hotkey := ""
if !RabbitConfig.suspend_hotkey
return
local keys := StrSplit(RabbitConfig.suspend_hotkey, "+", " ", 4)
local mask := 0
local target_key := ""
local num_modifiers := 0
for k in keys {
if k = "Control" {
num_modifiers += !(mask & ctrl)
mask |= ctrl
} else if k = "Alt" {
num_modifiers += !(mask & alt)
mask |= alt
} else if k = "Shift" {
num_modifiers += !(mask & shift)
mask |= shift
} else if not target_key {
target_key := k
}
}

if target_key {
if KeyDef.rime_to_ahk.Has(target_key)
target_key := KeyDef.rime_to_ahk[target_key]
if num_modifiers = 1 {
if mask & ctrl {
Hotkey("$<^" . target_key, , "S")
Hotkey("$>^" . target_key, , "S")
suspend_hotkey_mask := mask
suspend_hotkey := target_key
suspend_hotkey_set := true
} else if mask & alt {
; do not support
} else if mask & shift {
; do not support
}
} else if num_modifiers > 1 {
local m := "$" . (mask & shift ? "+" : "") .
(mask & ctrl ? "^" : "") .
(mask & alt ? "!" : "")
Hotkey(m . target_key, , "S")
suspend_hotkey_mask := mask
suspend_hotkey := target_key
suspend_hotkey_set := true
}
} else if keys.Length == 1 {
if keys[1] = "Shift" {
; do not support now
Hotkey("$LShift", , "S")
Hotkey("$RShift", , "S")
Hotkey("$LShift Up", , "S")
Hotkey("$RShift Up", , "S")
suspend_hotkey_mask := mask | up
suspend_hotkey := "Shift"
suspend_hotkey_set := true
} else {
; do not support
}
if target_key {
if KeyDef.rime_to_ahk.Has(target_key)
target_key := KeyDef.rime_to_ahk[target_key]
if num_modifiers = 1 {
if mask & ctrl {
Hotkey("$<^" . target_key, , "S")
Hotkey("$>^" . target_key, , "S")
suspend_hotkey_mask := mask
suspend_hotkey := target_key
}
} else if num_modifiers > 1 {
local m := "$" . (mask & shift ? "+" : "") .
(mask & ctrl ? "^" : "") .
(mask & alt ? "!" : "")
Hotkey(m . target_key, , "S")
suspend_hotkey_mask := mask
suspend_hotkey := target_key
}
if not suspend_hotkey_set {
; do not enable suspend hotkey
} else if keys.Length == 1 {
if keys[1] = "Shift" {
; do not support now
Hotkey("$LShift", , "S")
Hotkey("$RShift", , "S")
Hotkey("$LShift Up", , "S")
Hotkey("$RShift Up", , "S")
suspend_hotkey_mask := mask | up
suspend_hotkey := "Shift"
}
}
}
Expand Down Expand Up @@ -319,9 +303,9 @@ ProcessKey(key, mask, this_hotkey) {
status_text := new_ascii_punct ? ASCII_PUNCT_TRUE_LABEL_ABBR : ASCII_PUNCT_FALSE_LABEL_ABBR
}

if status_changed || ascii_changed {
if RabbitConfig.show_tips && (status_changed || ascii_changed) {
ToolTip(status_text, , , STATUS_TOOLTIP)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -2000)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -RabbitConfig.show_tips_time)
}

if commit := rime.get_commit(session_id) {
Expand Down
4 changes: 4 additions & 0 deletions schemas/rabbit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ config_version: "0.1"

# Define the hotkey to suspend Rabbit
suspend_hotkey: null

show_tips: true
# How long a period tips shows
show_tips_time: 1200

0 comments on commit 67f231c

Please sign in to comment.