Skip to content

Commit 88ba821

Browse files
authored
Export Rime::Switcher (#92)
* Add SwitcherReg * Include ticket.h * Remove const for engine * Export SwitcherReg * Add a sample script for Switcher
1 parent 0fdf0ad commit 88ba821

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

sample/lua/switch.lua

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
--[[
2+
switch_processor: 通过选择自定义的候选项来切换开关(以简繁切换和下一方案为例)
3+
4+
`Switcher` 适用于:
5+
1. 不方便或不倾向用 key_binder 处理的情况
6+
2. 自定义开关的读取(本例未体现)
7+
8+
须将 lua_processor@switch_processor 放在 engine/processors 里,并位于默认 selector 之前
9+
10+
为更好的使用本例,可以添加置顶的自定义词组,如
11+
〔简〕 simp
12+
〔繁〕 simp
13+
〔下一方案〕 next
14+
--]]
15+
16+
-- 帮助函数,返回被选中的候选的索引
17+
local function select_index(key, env)
18+
local ch = key.keycode
19+
local index = -1
20+
local select_keys = env.engine.schema.select_keys
21+
if select_keys ~= nil and select_keys ~= "" and not key.ctrl() and ch >= 0x20 and ch < 0x7f then
22+
local pos = string.find(select_keys, string.char(ch))
23+
if pos ~= nil then index = pos end
24+
elseif ch >= 0x30 and ch <= 0x39 then
25+
index = (ch - 0x30 + 9) % 10
26+
elseif ch >= 0xffb0 and ch < 0xffb9 then
27+
index = (ch - 0xffb0 + 9) % 10
28+
elseif ch == 0x20 then
29+
index = 0
30+
end
31+
return index
32+
end
33+
34+
-- 切换开关函数
35+
local function apply_switch(env, keyword, target_state)
36+
local ctx = env.engine.context
37+
local swt = env.switcher
38+
local conf = swt.user_config
39+
ctx:set_option(keyword, target_state)
40+
-- 如果设置了自动保存,则需相应的配置
41+
if swt:is_auto_save(keyword) and conf ~= nil then
42+
conf:set_bool("var/option/" .. keyword, target_state)
43+
end
44+
end
45+
46+
local kRejected = 0
47+
local kAccepted = 1
48+
local kNoop = 2
49+
50+
local function selector(key, env)
51+
if env.switcher == nil then return kNoop end
52+
if key:release() or key:alt() then return kNoop end
53+
local idx = select_index(key,env)
54+
if idx < 0 then return kNoop end
55+
local ctx = env.engine.context
56+
if ctx.input == "simp" then -- 当输入为 "simp" 时响应选择
57+
local state = nil
58+
if idx == 0 then
59+
state = true
60+
elseif idx == 1 then
61+
state = false
62+
end
63+
if state ~= nil then
64+
apply_switch(env, "simplification", state)
65+
ctx:clear() -- 切换完成后清空,避免上屏
66+
return kAccepted
67+
end
68+
elseif ctx.input == "next" and idx == 0 then
69+
env.switcher:select_next_schema()
70+
ctx:clear()
71+
return kAccepted
72+
end
73+
74+
return kNoop
75+
end
76+
77+
-- 初始化 switcher
78+
local function init(env)
79+
-- 若当前 librime-lua 版本未集成 Switcher 则无事发生
80+
if Switcher == nil then return end
81+
env.switcher = Switcher(env.engine)
82+
end
83+
84+
return { init = init, func = selector }

sample/rime.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,9 @@ single_char_filter = require("single_char")
6767
-- reverse_lookup_filter: 依地球拼音为候选项加上带调拼音的注释
6868
-- 详见 `lua/reverse.lua`
6969
reverse_lookup_filter = require("reverse")
70+
71+
-- III. processors:
72+
73+
-- switch_processor: 通过选择自定义的候选项来切换开关(以简繁切换和下一方案为例)
74+
-- 详见 `lua/switch.lua`
75+
switch_processor = require("switch")

src/types.cc

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <rime/gear/translator_commons.h>
1010
#include <rime/dict/reverse_lookup_dictionary.h>
1111
#include <rime/key_event.h>
12+
#include <rime/switcher.h>
1213
#include "lua_gears.h"
1314
#include "lib/lua_templates.h"
1415

@@ -816,7 +817,7 @@ namespace LogReg {
816817
}
817818
}
818819

819-
namespace RimeApiReg{
820+
namespace RimeApiReg {
820821
string get_rime_version() {
821822
RimeApi* rime = rime_get_api();
822823
return string(rime->get_version());
@@ -852,6 +853,38 @@ namespace RimeApiReg{
852853
}
853854
}
854855

856+
namespace SwitcherReg {
857+
typedef Switcher T;
858+
859+
an<T> make(Engine *engine) {
860+
return New<T>(engine);
861+
}
862+
863+
static const luaL_Reg funcs[] = {
864+
{ "Switcher", WRAP(make) },
865+
{ NULL, NULL },
866+
};
867+
868+
static const luaL_Reg methods[] = {
869+
{ "select_next_schema", WRAPMEM(T::SelectNextSchema) },
870+
{ "is_auto_save", WRAPMEM(T::IsAutoSave) },
871+
{ "refresh_menu", WRAPMEM(T::RefreshMenu) },
872+
{ "activate", WRAPMEM(T::Activate) },
873+
{ "deactivate", WRAPMEM(T::Deactivate) },
874+
{ NULL, NULL },
875+
};
876+
877+
static const luaL_Reg vars_get[] = {
878+
{ "attached_engine", WRAPMEM(T::attached_engine) },
879+
{ "user_config", WRAPMEM(T::user_config) },
880+
{ "active", WRAPMEM(T::active) },
881+
{ NULL, NULL },
882+
};
883+
884+
static const luaL_Reg vars_set[] = {
885+
{ NULL, NULL },
886+
};
887+
}
855888

856889
//--- Lua
857890
#define EXPORT(ns, L) \
@@ -898,6 +931,7 @@ void types_init(lua_State *L) {
898931
EXPORT(PropertyUpdateNotifierReg, L);
899932
EXPORT(KeyEventNotifierReg, L);
900933
EXPORT(ConnectionReg, L);
934+
EXPORT(SwitcherReg, L);
901935
LogReg::init(L);
902936
RimeApiReg::init(L);
903937
}

0 commit comments

Comments
 (0)