Skip to content

Commit

Permalink
feat: user defined suspend hotkey
Browse files Browse the repository at this point in the history
For now, must be key combinations. And in the modifires, there must be <kbd>Ctrl</kbd>.
E.g., <kbd>Ctrl</kbd> + <kbd>space</kbd> or <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>S</kbd>

A special value is to use <kbd>Shift</kbd> alone.
  • Loading branch information
amorphobia committed Sep 7, 2024
1 parent 91203d8 commit 2106266
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 21 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ jobs:
run: |
WORK=`pwd`
SCHEMAS=${WORK}/schemas
SHARED=${WORK}/Data
rm -rf ${SHARED} && mkdir -p ${SHARED}
cp ${SCHEMAS}/prelude/*.yaml ${SHARED}/
cp ${SCHEMAS}/essay/essay.txt ${SHARED}/
cp ${SCHEMAS}/default.yaml ${SHARED}/
cp ${SCHEMAS}/pinyin-simp/*.yaml ${SHARED}/
cp -r ${SCHEMAS}/jiandao/* ${SHARED}/
cp ${WORK}/opencc/* ${SHARED}/opencc/
DATA=${WORK}/Data
rm -rf ${DATA} && mkdir -p ${DATA}
cp ${SCHEMAS}/prelude/*.yaml ${DATA}/
cp ${SCHEMAS}/essay/essay.txt ${DATA}/
cp ${SCHEMAS}/default.yaml ${DATA}/
cp ${SCHEMAS}/rabbit.yaml ${DATA}/
cp ${SCHEMAS}/pinyin-simp/*.yaml ${DATA}/
cp -r ${SCHEMAS}/jiandao/* ${DATA}/
cp ${WORK}/opencc/* ${DATA}/opencc/
- name: Upload Data
uses: actions/upload-artifact@v4
Expand Down
98 changes: 86 additions & 12 deletions Rabbit.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ RabbitMain(args) {

local m := (args.Length == 0) ? RABBIT_PARTIAL_MAINTENANCE : args[1]
if m != RABBIT_NO_MAINTENANCE {
if rime.start_maintenance(m == RABBIT_FULL_MAINTENANCE)
rime.join_maintenance_thread()
SetDefaultKeyboard(layout)
Deploy()
} else {
TrayTip()
TrayTip("维护完成", RABBIT_IME_NAME)
Expand Down Expand Up @@ -116,6 +116,7 @@ ExitRabbit(layout, reason, code) {
}

RegisterHotKeys() {
global rime
local shift := KeyDef.mask["Shift"]
local ctrl := KeyDef.mask["Ctrl"]
local alt := KeyDef.mask["Alt"]
Expand All @@ -137,10 +138,7 @@ RegisterHotKeys() {
for key, _ in key_map {
Hotkey("$" . key, ProcessKey.Bind(key, 0), "S0")
; need specify left/right to prevent fallback to modifier down/up hotkeys
if key = "Space"
Hotkey("$<^" . key, ProcessKey.Bind(key, ctrl), "S")
else
Hotkey("$<^" . key, ProcessKey.Bind(key, ctrl), "S0")
Hotkey("$<^" . key, ProcessKey.Bind(key, ctrl), "S0")
; do not register Alt + single key now
; if not key = "Tab" {
; Hotkey("$<!" . key, ProcessKey.Bind(key, alt), "S0")
Expand Down Expand Up @@ -176,15 +174,81 @@ RegisterHotKeys() {
; Hotkey("$+^!#" . key, ProcessKey.Bind(key, shift | ctrl | alt | win), "S0")
}
}

; 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
if suspend_hotkey_conf := rime.config_get_string(config, "suspend_hotkey") {
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
}
}

if 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 not suspend_hotkey_set {
Hotkey("$<^Space", , "S")
suspend_hotkey_mask := ctrl
suspend_hotkey := "Space"
suspend_hotkey_set := true
}
rime.config_close(config)
}
}

ProcessKey(key, mask, this_hotkey) {
if key = "Space" and mask == KeyDef.mask["Ctrl"] {
Suspend(-1)
ToolTip(A_IsSuspended ? "禁用" : "启用", , , STATUS_TOOLTIP)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -2000)
return
}
global suspend_hotkey_mask, suspend_hotkey
global last_is_hide
local code := 0
Loop 4 {
Expand Down Expand Up @@ -268,6 +332,16 @@ ProcessKey(key, mask, this_hotkey) {
} else
last_is_hide := false

if (key = suspend_hotkey or SubStr(key, 2) = suspend_hotkey) and (mask = suspend_hotkey_mask) {
ToolTip()
box.Show("Hide")
rime.clear_composition(session_id)
Suspend(-1)
ToolTip(A_IsSuspended ? "禁用" : "启用", , , STATUS_TOOLTIP)
SetTimer(() => ToolTip(, , , STATUS_TOOLTIP), -2000)
return
}

if context := rime.get_context(session_id) {
if context.composition.length > 0 {
if GetCaretPos(&caret_x, &caret_y, &caret_w, &caret_h) {
Expand Down
2 changes: 1 addition & 1 deletion RabbitDeployer.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Configurator extends Class {

{
rime.deploy()
; rime.deploy_config_file("rabbit.yaml", "config_version")
rime.deploy_config_file("rabbit.yaml", "config_version")
}

mutex.Close()
Expand Down
8 changes: 8 additions & 0 deletions schemas/rabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Rabbit settings
# encoding: utf-8

config_version: "0.1"

# Define the hotkey to suspend Rabbit
# Must use Control in modifiers
suspend_hotkey: Control+space

0 comments on commit 2106266

Please sign in to comment.