Skip to content

Commit

Permalink
feat: click tray icon toggle ascii mode
Browse files Browse the repository at this point in the history
  • Loading branch information
amorphobia committed Dec 1, 2023
1 parent b961fdc commit c2ca88f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
8 changes: 6 additions & 2 deletions Lib/RabbitCommon.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#Include <librime-ahk\rime_api>
#Include <librime-ahk\rime_levers_api>

global AHK_NOTIFYICON := 0x404
global WM_LBUTTONDOWN := 0x201
global WM_LBUTTONUP := 0x202

global rime := RimeApi()
global RABBIT_IME_NAME := "玉兔毫"
global RABBIT_CODE_NAME := "Rabbit"
Expand All @@ -37,9 +41,9 @@ global FULL_SHAPE_TRUE_LABEL := "全角"
global FULL_SHAPE_FALSE_LABEL_ABBR := ""
global FULL_SHAPE_TRUE_LABEL_ABBR := ""
global ASCII_PUNCT_FALSE_LABEL := "。,"
global ASCII_PUNCT_TRUE_LABEL := ".,"
global ASCII_PUNCT_TRUE_LABEL := ". ,"
global ASCII_PUNCT_FALSE_LABEL_ABBR := ""
global ASCII_PUNCT_TRUE_LABEL_ABBR := ""
global ASCII_PUNCT_TRUE_LABEL_ABBR := "."

global ERROR_ALREADY_EXISTS := 183 ; https://learn.microsoft.com/windows/win32/debug/system-error-codes--0-499-

Expand Down
34 changes: 34 additions & 0 deletions Lib/RabbitTrayMenu.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
*/
TraySetIcon("Rabbit.ico")
A_IconTip := "玉兔毫"

global TRAY_SCHEMA_NAME := ""
global TRAY_ASCII_MODE := 0
global TRAY_FULL_SHAPE := 0
global TRAY_ASCII_PUNCT := 0

A_TrayMenu.Delete()
; A_TrayMenu.add("输入法设定")
; A_TrayMenu.add("用户词典管理")
Expand Down Expand Up @@ -49,3 +55,31 @@ if TRAY_MENU_GRAYOUT {
A_TrayMenu.Disable("重新部署")
A_TrayMenu.Disable("退出玉兔毫")
}

ClickHandler(wParam, lParam, msg, hWnd) {
if not rime
return
if (lParam == WM_LBUTTONUP) {
local old_ascii_mode := rime.get_option(session_id, "ascii_mode")
rime.set_option(session_id, "ascii_mode", !old_ascii_mode)
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)
}
}

UpdateTrayTip(schema_name := TRAY_SCHEMA_NAME, ascii_mode := TRAY_ASCII_MODE, full_shape := TRAY_FULL_SHAPE, ascii_punct := TRAY_ASCII_PUNCT) {
global TRAY_SCHEMA_NAME, TRAY_ASCII_MODE, TRAY_FULL_SHAPE, TRAY_ASCII_PUNCT
TRAY_SCHEMA_NAME := schema_name ? schema_name : TRAY_SCHEMA_NAME
TRAY_ASCII_MODE := !!ascii_mode
TRAY_FULL_SHAPE := !!full_shape
TRAY_ASCII_PUNCT := !!ascii_punct
A_IconTip := Format(
"玉兔毫 {}`n左键切换模式,右键打开菜单`n{} | {} | {}", TRAY_SCHEMA_NAME,
(TRAY_ASCII_MODE ? ASCII_MODE_TRUE_LABEL : ASCII_MODE_FALSE_LABEL),
(TRAY_FULL_SHAPE ? FULL_SHAPE_TRUE_LABEL : FULL_SHAPE_FALSE_LABEL),
(TRAY_ASCII_PUNCT ? ASCII_PUNCT_TRUE_LABEL : ASCII_PUNCT_FALSE_LABEL)
)
}
27 changes: 9 additions & 18 deletions Rabbit.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,15 @@ RabbitMain(args) {
RegisterHotKeys()
UpdateStateLabels()
if status := rime.get_status(session_id) {
local new_schema_name := status.schema_name
local new_ascii_mode := status.is_ascii_mode
local new_full_shape := status.is_full_shape
local new_ascii_punct := status.is_ascii_punct
local schema_name := status.schema_name
local ascii_mode := status.is_ascii_mode
local full_shape := status.is_full_shape
local ascii_punct := status.is_ascii_punct
rime.free_status(status)

A_IconTip := Format(
"玉兔毫 {}`n{} | {} | {}", new_schema_name,
(new_ascii_mode ? ASCII_MODE_TRUE_LABEL : ASCII_MODE_FALSE_LABEL),
(new_full_shape ? FULL_SHAPE_TRUE_LABEL : FULL_SHAPE_FALSE_LABEL),
(new_ascii_punct ? ASCII_PUNCT_TRUE_LABEL : ASCII_PUNCT_FALSE_LABEL)
)
UpdateTrayTip(schema_name, ascii_mode, full_shape, ascii_punct)
}
OnMessage(AHK_NOTIFYICON, ClickHandler.Bind())

OnExit(ExitRabbit.Bind(layout))
}
Expand Down Expand Up @@ -224,12 +220,7 @@ ProcessKey(key, mask, this_hotkey) {
UpdateStateLabels()
}

A_IconTip := Format(
"玉兔毫 {}`n{} | {} | {}", new_schema_name,
(new_ascii_mode ? ASCII_MODE_TRUE_LABEL : ASCII_MODE_FALSE_LABEL),
(new_full_shape ? FULL_SHAPE_TRUE_LABEL : FULL_SHAPE_FALSE_LABEL),
(new_ascii_punct ? ASCII_PUNCT_TRUE_LABEL : ASCII_PUNCT_FALSE_LABEL)
)
UpdateTrayTip(new_schema_name, new_ascii_mode, new_full_shape, new_ascii_punct)

local status_text := ""
local status_changed := false
Expand Down Expand Up @@ -327,9 +318,9 @@ UpdateStateLabels() {
str := rime.get_state_label(session_id, "ascii_punct", false)
ASCII_PUNCT_FALSE_LABEL := str ? str : "。,"
str := rime.get_state_label(session_id, "ascii_punct", true)
ASCII_PUNCT_TRUE_LABEL := str ? str : ".,"
ASCII_PUNCT_TRUE_LABEL := str ? str : ". ,"
slice := rime.get_state_label_abbreviated(session_id, "ascii_punct", false, true)
ASCII_PUNCT_FALSE_LABEL_ABBR := (slice and slice.slice !== "") ? slice.slice : ""
slice := rime.get_state_label_abbreviated(session_id, "ascii_punct", true, true)
ASCII_PUNCT_TRUE_LABEL_ABBR := (slice and slice.slice !== "") ? slice.slice : ""
ASCII_PUNCT_TRUE_LABEL_ABBR := (slice and slice.slice !== "") ? slice.slice : "."
}

0 comments on commit c2ca88f

Please sign in to comment.