|
| 1 | +-- 2024-10-09 Magician62011 |
| 2 | +-- 功能目标:实现主动切换界面横竖显示 |
| 3 | +-- 方法: |
| 4 | +-- 使用快捷键触发配置值'style/horizontal'更改 |
| 5 | +-- 使用lua_processor实现连续的状态判断 |
| 6 | +-- 使用switcher:process_key发送hotkey实现触发UI界面更新(若使用engine:process_key发送hotkey则会造成不再进入processor导致后续无法处理) |
| 7 | + |
| 8 | +-- 跨processor变量 |
| 9 | +local n = 0 |
| 10 | +local i = 0 |
| 11 | +local switcher_active = nil |
| 12 | +local Switcher_lasttime = nil |
| 13 | +local temp_input = nil |
| 14 | +local manual = false |
| 15 | + |
| 16 | +--控制日志输出打印 |
| 17 | +local function log_error(info) |
| 18 | + local log_enable = false --false --true |
| 19 | + if log_enable then |
| 20 | + log.error(info) |
| 21 | + end |
| 22 | +end |
| 23 | + |
| 24 | +local function horizontal_switch(key_event, env) |
| 25 | + --进入processor次数 |
| 26 | + n = n+1 |
| 27 | + log_error("n: "..n) |
| 28 | + |
| 29 | + --输出触发按键信息 |
| 30 | + local keyrepr = key_event:repr() |
| 31 | + log_error("keyrepr: "..tostring(keyrepr)) |
| 32 | + |
| 33 | + --获得engine和context |
| 34 | + local engine = env.engine |
| 35 | + log_error("engine: "..tostring(engine)) |
| 36 | + local context = engine.context |
| 37 | + log_error("context.input: "..tostring(context.input)) |
| 38 | + |
| 39 | + |
| 40 | + --再次进入processor时发送Escape 实现界面变化 |
| 41 | + if Switcher_lasttime ~= nil and switcher_active == true then |
| 42 | + log_error("Phase2**Switcher_lasttime.active-step2: "..tostring(Switcher_lasttime.active)) |
| 43 | + result_2 = Switcher_lasttime:process_key( KeyEvent("Escape") ) |
| 44 | + log_error("Phase2**send Escape : "..(result_2 and 'success' or 'fail')) |
| 45 | + --再检查active状态 |
| 46 | + log_error("Phase2**Switcher_lasttime.active-step3: "..tostring(Switcher_lasttime.active)) |
| 47 | + switcher_active = nil |
| 48 | + if i > 1 then |
| 49 | + temp_input = context.input |
| 50 | + i = 0 |
| 51 | + end |
| 52 | + context:clear() |
| 53 | + return 2 --关键处理,能使界面变化 |
| 54 | + end |
| 55 | + |
| 56 | + --第三次进入到processor时,处理context.input,在新界面恢复先前输入内容 |
| 57 | + if temp_input ~= nil and switcher_active == nil then |
| 58 | + context.input = temp_input |
| 59 | + log_error("Phase3***new context input replaced: "..tostring(context.input)) |
| 60 | + temp_input = nil |
| 61 | + return 2 |
| 62 | + end |
| 63 | + |
| 64 | + |
| 65 | + local schema = engine.schema |
| 66 | + local h_config_state = schema.config:get_bool('style/horizontal') --获得设置里横竖状态 布尔值 -- schema.yaml文件中要有style/horizontal的配置,否则返回nil,首次not h_config_state为true横向 |
| 67 | + log_error("h_config_state: "..tostring(h_config_state)) |
| 68 | + |
| 69 | + local function send_hotkey() |
| 70 | + --保存当前输入 |
| 71 | + temp_input = context.input |
| 72 | + log_error("Phase1*current context input: "..tostring(temp_input)) |
| 73 | + --修改配置值 |
| 74 | + schema.config:set_bool('style/horizontal', not h_config_state) |
| 75 | + log_error("Phase1*--change horizontal--") |
| 76 | + --检查配置值修改成功 |
| 77 | + local h_config_state_new = schema.config:get_bool('style/horizontal') |
| 78 | + log_error("Phase1*h_config_state_new: "..tostring(h_config_state_new )) |
| 79 | + |
| 80 | + -- 判断条件 发送hotkey(Control+grave) 触发开关界面 |
| 81 | + if h_config_state_new == not h_config_state then |
| 82 | + --创建开关对象 |
| 83 | + switcher = Switcher(env.engine) |
| 84 | + log_error("Phase1*switcher1: "..tostring(switcher)) |
| 85 | + --检查active状态 |
| 86 | + log_error("Phase1*switcher.active-original: "..tostring(switcher.active)) |
| 87 | + --发送hotkey |
| 88 | + local result_1 = switcher:process_key( KeyEvent("Control+grave") ) |
| 89 | + log_error("Phase1*send Control+grave : "..(result_1 and 'success' or 'fail')) |
| 90 | + --检查active状态 |
| 91 | + log_error("Phase1*switcher-if: "..tostring(switcher)) |
| 92 | + --保留active状态 |
| 93 | + switcher_active = switcher.active |
| 94 | + log_error("Phase1*switcher.active-step1: "..tostring(switcher_active)) |
| 95 | + --保留开关对象 |
| 96 | + Switcher_lasttime = switcher |
| 97 | + log_error("Phase1*Switcher_lasttime: "..tostring(Switcher_lasttime)) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + -- 不同条件触发配置值修改和发送hotkey: |
| 102 | + |
| 103 | + -- 1. 快捷键手动触发 功能完善 组合按键能确保进入processor三次以上 |
| 104 | + --if keyrepr == "Control+apostrophe" then |
| 105 | + local shortcut_key = schema.config:get_string("horizontal_switch/shortcut_key") or "Control+apostrophe" |
| 106 | + if keyrepr == shortcut_key then |
| 107 | + send_hotkey() |
| 108 | + manual = not manual --强制手动控制 防与has_tag冲突 |
| 109 | + end |
| 110 | + |
| 111 | + -- 2. has_tag自动触发 小瑕疵 每次切换时第一个字符会暂时不显示 |
| 112 | + |
| 113 | + --local tag = schema.config:get_string("mails/tag") --or 'mails' --需要自动切换的tag类型 |
| 114 | + local tags = schema.config:get_list("horizontal_switch/auto_switch_tags") --ConfigList |
| 115 | + log_error("tags: "..type(tags)) |
| 116 | + local tags_size = tags and tags.size or nil |
| 117 | + log_error("tags_size: "..tostring(tags_size)) |
| 118 | + |
| 119 | + if tags ~= nil and manual == false then |
| 120 | + local segment = context.composition:back() --当空输入时,segment会是nil |
| 121 | + local has_tag = false |
| 122 | + for i=0, tags.size-1 do |
| 123 | + tag = tags:get_value_at(i):get_string() |
| 124 | + has_tag = segment ~= nil and segment:has_tag(tag) or false --是否含有tag --此processor放在 - recognizer之后 |
| 125 | + log_error("--has tag--: "..tag.."--: "..tostring(has_tag)) |
| 126 | + if has_tag == true then break end |
| 127 | + end |
| 128 | + |
| 129 | + if has_tag == true then |
| 130 | + if h_config_state == true -- 当输入法是横向时,识别到tag,切换为竖向 |
| 131 | + then |
| 132 | + send_hotkey() |
| 133 | + end |
| 134 | + else --没有tag时恢复 |
| 135 | + if h_config_state == false -- 输入法是竖向时,切换回为横向 |
| 136 | + then |
| 137 | + i= i+1 -- 控制发送的变量 防止开关界面留置 |
| 138 | + if i > 1 then |
| 139 | + -- 跳过第1次 满足条件没有tag 第一次:旧按键Release,tag消失 |
| 140 | + -- 当第二次 满足条件,进入执行 第二次:新按键按下 进入 processor 但暂无法获得context.input,会在Phase2去处理(新按键Release进入processor时) |
| 141 | + -- 若取第三次满足条件,进入执行 第三次:新按键Release,进入此处逻辑,会因无后续按键进入proecessor,依然会造成界面留置。 |
| 142 | + -- 所以取一次新按键,两次进入processor完成界面刷新,是最好的方案,但缺点是第一个按键信息会暂时不显示 |
| 143 | + send_hotkey() |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + return 2 |
| 150 | +end |
| 151 | + |
| 152 | +return horizontal_switch |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +--[[ |
| 157 | +-- xxx.schema.yaml 对应位置添加配置: |
| 158 | +
|
| 159 | +engine: |
| 160 | + processors: |
| 161 | + - lua_processor@*horizontal_switch |
| 162 | +style: |
| 163 | + horizontal: true |
| 164 | +horizontal_switch: |
| 165 | + shortcut_key: "Control+apostrophe" |
| 166 | + auto_switch_tags: [mails] #[mails, radical_lookup] |
| 167 | +
|
| 168 | +-- 或使用patch: |
| 169 | +__patch: |
| 170 | + 'engine/processors/@before 3': lua_processor@*horizontal_switch |
| 171 | + style/horizontal: true # 横向展示 默认值 |
| 172 | + horizontal_switch/shortcut_key: "Control+apostrophe" # 手动切换快捷键 |
| 173 | + horizontal_switch/auto_switch_tags: [mails] # 自动切换的tags 非必选项 逗号分割 |
| 174 | +--]] |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
0 commit comments