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.
This commit is contained in:
Francesco Abbate 2021-10-04 11:13:05 +02:00
parent 9fb166d3cc
commit f5cc91e400
5 changed files with 53 additions and 0 deletions

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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 }
};