Keep the IME anchor following the cursor when child output moves it

Key presses update the IME position, but cursor motion caused by child
output, such as the echo of committed text, did not. The next composition
then started with a stale anchor and its candidate window briefly appeared
at the old position before jumping to the cursor.

Once an IME composition has been seen, push the cursor position to the IME
whenever it changes outside a composition in the active window of the
focused OS window. Nothing extra happens until an IME is used, and there is
no timer, so no feedback loop with GNOME's done events.

Closes #10644
This commit is contained in:
Kovid Goyal 2026-10-11 09:40:28 +05:30
parent 0a9c1f0112
commit 2414d64e67
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
7 changed files with 49 additions and 5 deletions

View file

@ -297,6 +297,10 @@ Detailed list of changes
its end, so that it no longer jumps horizontally while composing text
(:pull:`10642`)
- IME: Keep the candidate window anchored at the cursor when program output
moves it, so it no longer briefly appears at the previous position at the
start of the next composition (:pull:`10644`)
- Fix closing an unfocused window that comes before the focused window in the
layout sometimes moving focus to a different window

View file

@ -868,6 +868,29 @@ change_menubar_title(PyObject *title UNUSED) {
#endif
}
static void
sync_ime_position_with_cursor(OSWindow *os_window, Window *w) {
// Key presses update the IME position but cursor motion caused by child
// output, such as the echo of text committed by the IME, does not. Without
// this the next composition starts with a stale anchor and its candidate
// window flashes at the old position before jumping to the cursor.
Screen *screen = w->render_data.screen;
OverlayLine *ol = &screen->overlay_line;
if (screen_is_overlay_active(screen)) {
// During a composition the IME is anchored to the pre-edit text, see
// render_overlay_line(), so resync with the cursor once it ends.
ol->last_ime_cursor.valid = false;
return;
}
const index_type x = screen->cursor->x, y = screen->cursor->y;
if (ol->last_ime_cursor.valid && ol->last_ime_cursor.x == x && ol->last_ime_cursor.y == y) return;
ol->last_ime_cursor.x = x;
ol->last_ime_cursor.y = y;
ol->last_ime_cursor.valid = true;
// Redundant updates are dropped by the platform backends
update_ime_state_in_os_window(os_window, w, 0);
}
static bool
prepare_to_render_os_window(
OSWindow *os_window,
@ -934,6 +957,7 @@ prepare_to_render_os_window(
*active_window_id = w->id;
if (collect_cursor_info(&WD.screen->cursor_render_info, w, now, os_window)) needs_render = true;
WD.screen->cursor_render_info.is_focused = os_window->is_focused;
if (global_state.ime_used && os_window->is_focused) sync_ime_position_with_cursor(os_window, w);
set_os_window_title_from_window(w, os_window);
*active_window_bg = window_bg;
if (OPT(cursor_trail)) {

View file

@ -297,6 +297,8 @@ on_key_input(const GLFWkeyevent *ev) {
debug("handled wayland IME done event\n");
return;
case GLFW_IME_PREEDIT_CHANGED:
// Empty pre-edit events are also sent without an IME, for example on focus loss
if (*text) global_state.ime_used = true;
screen_update_overlay_text(screen, text);
update_ime_position(w, screen);
debug("updated pre-edit text: '%s'\n", text);

View file

@ -97,6 +97,7 @@ init_overlay_line(Screen *self, index_type columns, bool keep_active) {
self->overlay_line.cursor_x = 0;
self->overlay_line.last_ime_pos.x = 0;
self->overlay_line.last_ime_pos.y = 0;
self->overlay_line.last_ime_cursor.valid = false;
return true;
}

View file

@ -101,6 +101,11 @@ typedef struct {
struct {
index_type x, y;
} last_ime_pos;
// The cursor position last sent as the IME anchor outside of a composition, see sync_ime_position_with_cursor()
struct {
index_type x, y;
bool valid;
} last_ime_cursor;
} OverlayLine;
typedef struct ExtraCursor {

View file

@ -902,6 +902,15 @@ dispatch_pending_clicks(id_type timer_id UNUSED, void *data UNUSED) {
} while (dispatched);
}
void
update_ime_state_in_os_window(OSWindow *osw, Window *window, int update_focus) {
OSWindow *orig = global_state.callback_os_window;
global_state.callback_os_window = osw;
if (update_focus) update_ime_focus(osw, update_focus > 0);
if (update_focus >= 0 && window->render_data.screen) { update_ime_position(window, window->render_data.screen); }
global_state.callback_os_window = orig;
}
bool
update_ime_position_for_window(id_type window_id, bool force, int update_focus) {
for (size_t o = 0; o < global_state.num_os_windows; o++) {
@ -913,11 +922,7 @@ update_ime_position_for_window(id_type window_id, bool force, int update_focus)
if (window->id == window_id) {
// The screen may not be ready after the new window is created and focused, and still needs to enable IME.
if ((window->render_data.screen && (force || osw->is_focused)) || update_focus > 0) {
OSWindow *orig = global_state.callback_os_window;
global_state.callback_os_window = osw;
if (update_focus) update_ime_focus(osw, update_focus > 0);
if (update_focus >= 0 && window->render_data.screen) { update_ime_position(window, window->render_data.screen); }
global_state.callback_os_window = orig;
update_ime_state_in_os_window(osw, window, update_focus);
return true;
}
return false;

View file

@ -646,6 +646,8 @@ typedef struct GlobalState {
int mods_at_last_key_or_button_event;
CloseRequest quit_request;
bool redirect_mouse_handling;
// Set once an IME composition has been seen, see sync_ime_position_with_cursor()
bool ime_used;
WindowLogoTable *all_window_logos;
int gl_version;
bool supports_framebuffer_srgb;
@ -794,6 +796,7 @@ bool is_os_window_fullscreen(OSWindow *);
void update_ime_focus(OSWindow *osw, bool focused);
void update_ime_position(Window *w, Screen *screen);
bool update_ime_position_for_window(id_type window_id, bool force, int update_focus);
void update_ime_state_in_os_window(OSWindow *osw, Window *window, int update_focus);
void set_ignore_os_keyboard_processing(bool enabled);
void push_modifier_remap_to_glfw(void);
void update_menu_bar_title(PyObject *title UNUSED);