Skip to content

Commit 40848a5

Browse files
committed
Add KeyEventResult enum and support passthrough (filter) mode
1 parent a6f0054 commit 40848a5

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/addonloader/base.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,23 @@ local oldsetCurrentInputMethod=fcitx.setCurrentInputMethod
120120
local function setCurrentInputMethod(name,local_im)
121121
if(local_im == nil) then
122122
local_im = false
123-
end
123+
end
124124
oldsetCurrentInputMethod(name,local_im)
125125
end
126126

127127
fcitx.setCurrentInputMethod = setCurrentInputMethod
128128

129+
--- Result of key event handler. It represents the action to be taken
130+
-- after a fcitx.EventType.KeyEvent handler is executed.
131+
-- @table KeyEventResult
132+
-- @field NotHandled Let Fcitx5 continue its normal processing. Equivalent to returning `false`.
133+
-- @field Handled Fcitx5 handles the event and consumes it. Equivalent to returning `true`.
134+
-- @field Passthrough Fcitx5 will not process this event and will let it pass through to the application.
135+
local KeyEventResult = {
136+
NotHandled = 0,
137+
Handled = 1,
138+
Passthrough = 2,
139+
}
140+
fcitx.KeyEventResult = KeyEventResult
141+
129142
return fcitx

src/addonloader/luaaddonstate.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,25 @@ std::tuple<int> LuaAddonState::watchEventImpl(int eventType,
262262
return 3;
263263
},
264264
[](std::unique_ptr<LuaState> &state, KeyEvent &event) {
265-
auto b = lua_toboolean(state, -1);
266-
if (b) {
267-
event.filterAndAccept();
265+
if (lua_isinteger(state, -1)) {
266+
auto result = lua_tointeger(state, -1);
267+
switch (static_cast<KeyEventResult>(result)) {
268+
case KeyEventResult::NotHandled:
269+
break;
270+
case KeyEventResult::Passthrough:
271+
event.filter();
272+
break;
273+
case KeyEventResult::Handled:
274+
event.filterAndAccept();
275+
break;
276+
default:
277+
FCITX_LUA_ERROR() << "invalid KeyEventResult: " << result;
278+
}
279+
} else {
280+
auto b = lua_toboolean(state, -1);
281+
if (b) {
282+
event.filterAndAccept();
283+
}
268284
}
269285
});
270286
break;

src/addonloader/luaaddonstate.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ class Converter {
9595
ScopedConnection connection_;
9696
};
9797

98+
enum class KeyEventResult {
99+
NotHandled,
100+
Handled,
101+
Passthrough,
102+
};
103+
98104
class LuaAddonState {
99105
public:
100106
LuaAddonState(Library *luaLibrary, const std::string &name,

src/addonloader/luafunc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ FOREACH_LUA_FUNCTION(lua_pushstring)
2323
FOREACH_LUA_FUNCTION(lua_pushlstring)
2424
FOREACH_LUA_FUNCTION(lua_type)
2525
FOREACH_LUA_FUNCTION(lua_gettable)
26+
FOREACH_LUA_FUNCTION(lua_isinteger)
2627
FOREACH_LUA_FUNCTION(lua_tointegerx)
2728
FOREACH_LUA_FUNCTION(lua_pushnil)
2829
FOREACH_LUA_FUNCTION(lua_next)

0 commit comments

Comments
 (0)