diff --git a/docs/changelog.rst b/docs/changelog.rst index e323bedb1..fc2f71667 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 67f384b96..171b8a490 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -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)) { diff --git a/kitty/keys.c b/kitty/keys.c index 34217f607..6a1846be4 100644 --- a/kitty/keys.c +++ b/kitty/keys.c @@ -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); diff --git a/kitty/screen.c b/kitty/screen.c index 073a0f835..605b4a659 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -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; } diff --git a/kitty/screen.h b/kitty/screen.h index 901417d4f..b9a9399d7 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -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 { diff --git a/kitty/state.c b/kitty/state.c index fd4f28a84..98e17b910 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -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; diff --git a/kitty/state.h b/kitty/state.h index 1b27724e8..42f6690e1 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -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);