From f5cc91e4003f40023bc7f25394d10543c868bee0 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Mon, 4 Oct 2021 11:13:05 +0200 Subject: [PATCH] WIP: testing IME text editing This commit is a test for IME text editing. For CJK text editing you may need to use a font that support CHK characters like: https://github.com/be5invis/Sarasa-Gothic Currently on windows we receive no text editing events. --- data/core/docview.lua | 23 +++++++++++++++++++++++ data/core/init.lua | 3 +++ data/core/rootview.lua | 5 +++++ data/core/view.lua | 3 +++ src/api/system.c | 19 +++++++++++++++++++ 5 files changed, 53 insertions(+) diff --git a/data/core/docview.lua b/data/core/docview.lua index 9a2972dc..a3e2cb88 100644 --- a/data/core/docview.lua +++ b/data/core/docview.lua @@ -309,6 +309,29 @@ function DocView:on_text_input(text) end +function DocView:draw_ime_text_editing(text, start, len) + local line, col = self.doc:get_selection() + local x, y = self:get_line_screen_position(line) + x = x + get_col_x_offset(line, col) + local default_font = self:get_font() + local subpixel_scale = default_font:subpixel_scale() + local tx, ty = subpixel_scale * x, y + self:get_line_text_y_offset() + renderer.draw_text_subpixel(default_font, text, tx, ty, style.text) +end + + +function DocView:on_ime_text_editing(text, start, len) + local line, col = self.doc:get_selection() + local x, y = self:get_line_screen_position(line) + x = x + get_col_x_offset(line, col) + local h = self:get_line_height() + local w = self:get_font():get_width(5) + system.set_ime_input_rect(x, y, w, h) + + core.root_view:defer_draw(draw_ime_text_editing, self, text, start, len) +end + + function DocView:update() -- scroll to make caret visible and reset blink timer if it moved local line, col = self.doc:get_selection() diff --git a/data/core/init.lua b/data/core/init.lua index 7ab4629d..65e3e6a7 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -954,6 +954,9 @@ function core.on_event(type, ...) end elseif type == "focuslost" then core.root_view:on_focus_lost(...) + elseif type == "textediting" then + core.root_view:on_ime_text_editing(...) + did_keymap = keymap.on_key_pressed(...) elseif type == "quit" then core.quit() end diff --git a/data/core/rootview.lua b/data/core/rootview.lua index 0d219474..df7c9dfa 100644 --- a/data/core/rootview.lua +++ b/data/core/rootview.lua @@ -1033,6 +1033,11 @@ function RootView:on_focus_lost(...) end +function RootView:on_ime_text_editing(...) + core.active_view:on_ime_text_editing(...) +end + + function RootView:interpolate_drag_overlay(overlay) self:move_towards(overlay, "x", overlay.to.x) self:move_towards(overlay, "y", overlay.to.y) diff --git a/data/core/view.lua b/data/core/view.lua index d1374ee4..631f06b0 100644 --- a/data/core/view.lua +++ b/data/core/view.lua @@ -102,6 +102,9 @@ function View:on_text_input(text) -- no-op end +-- no-op +View.on_ime_text_editing = View.on_text_input + function View:on_mouse_wheel(y) if self.scrollable then diff --git a/src/api/system.c b/src/api/system.c index d84f86dd..f9b5e752 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -193,6 +193,14 @@ top: lua_pushstring(L, get_key_name(&e, buf)); return 2; + case SDL_TEXTEDITING: + fprintf(stderr, "textediting: %s (%d, %d)\n", e.edit.text, e.edit.start, e.edit.length); fflush(stderr); + lua_pushstring(L, "textediting"); + lua_pushstring(L, e.edit.text); + lua_pushnumber(L, e.edit.start); + lua_pushnumber(L, e.edit.length); + return 4; + case SDL_TEXTINPUT: lua_pushstring(L, "textinput"); lua_pushstring(L, e.text.text); @@ -652,6 +660,16 @@ static int f_set_window_opacity(lua_State *L) { } +static int f_set_ime_input_rect(lua_State *L) { + SDL_Rect r; + r.x = luaL_checkinteger(L, 1); + r.y = luaL_checkinteger(L, 2); + r.w = luaL_checkinteger(L, 3); + r.h = luaL_checkinteger(L, 4); + SDL_SetTextInputRect(&r); + return 0; +} + static const luaL_Reg lib[] = { { "poll_event", f_poll_event }, { "wait_event", f_wait_event }, @@ -678,6 +696,7 @@ static const luaL_Reg lib[] = { { "exec", f_exec }, { "fuzzy_match", f_fuzzy_match }, { "set_window_opacity", f_set_window_opacity }, + { "set_ime_input_rect", f_set_ime_input_rect }, { NULL, NULL } };