From 33088d59db742cf802977a9d8ec8b4ea3ca79ea0 Mon Sep 17 00:00:00 2001 From: Alynx Zhou Date: Mon, 23 Oct 2023 14:32:21 +0800 Subject: [PATCH] wayland/text-input: Pass char based offset to ClutterInputFocus Wayland's text-input-v3 uses byte based offset for cursor and anchor of surrounding text, but Clutter needs char based offset here. This commit converts byte based offset to char based offset before passing them to ClutterInputFocus. Fixes . Part-of: --- src/wayland/meta-wayland-text-input.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/wayland/meta-wayland-text-input.c b/src/wayland/meta-wayland-text-input.c index 2e694dc1e..c8f50847a 100644 --- a/src/wayland/meta-wayland-text-input.c +++ b/src/wayland/meta-wayland-text-input.c @@ -122,12 +122,18 @@ static void meta_wayland_text_input_focus_request_surrounding (ClutterInputFocus *focus) { MetaWaylandTextInput *text_input; + long cursor, anchor; + /* Clutter uses char offsets but text-input-v3 uses byte offsets. */ text_input = META_WAYLAND_TEXT_INPUT_FOCUS (focus)->text_input; + cursor = g_utf8_strlen (text_input->surrounding.text, + text_input->surrounding.cursor); + anchor = g_utf8_strlen (text_input->surrounding.text, + text_input->surrounding.anchor); clutter_input_focus_set_surrounding (focus, - text_input->surrounding.text, - text_input->surrounding.cursor, - text_input->surrounding.anchor); + text_input->surrounding.text, + cursor, + anchor); } static uint32_t @@ -683,16 +689,24 @@ text_input_commit_state (struct wl_client *client, if (text_input->pending_state & META_WAYLAND_PENDING_STATE_SURROUNDING_TEXT) { + long cursor, anchor; + /* Save the surrounding text for `delete_surrounding_text`. */ g_free (text_input->surrounding.text); text_input->surrounding.text = g_steal_pointer (&text_input->pending_surrounding.text); text_input->surrounding.cursor = text_input->pending_surrounding.cursor; text_input->surrounding.anchor = text_input->pending_surrounding.anchor; + /* Pass the surrounding text to Clutter to handle it with input method. */ + /* Clutter uses char offsets but text-input-v3 uses byte offsets. */ + cursor = g_utf8_strlen (text_input->surrounding.text, + text_input->surrounding.cursor); + anchor = g_utf8_strlen (text_input->surrounding.text, + text_input->surrounding.anchor); clutter_input_focus_set_surrounding (text_input->input_focus, text_input->surrounding.text, - text_input->surrounding.cursor, - text_input->surrounding.anchor); + cursor, + anchor); } if (text_input->pending_state & META_WAYLAND_PENDING_STATE_INPUT_RECT)