From e5d78ce8fc1b866af3c3c68bc4bd2af78b343e58 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 29 Oct 2026 18:52:19 +0530 Subject: [PATCH] macOS: Commit in-progress IME text when the OS window loses focus Previously the pre-edit text was discarded on any focus loss, losing composed or dictated text when switching to another application. Now when the OS window loses focus the pre-edit text is committed, as native macOS applications do, preferring any final result the input method delivers when it is reset. Focus changes between kitty windows and closing windows still cancel the composition. Closes #10771 --- docs/changelog.rst | 3 ++ glfw/cocoa_window.m | 99 ++++++++++++++++++++++++++++++++++++++++++--- kitty/glfw.c | 20 ++++++--- 3 files changed, 111 insertions(+), 11 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 9951c3bc0..eb41f920a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -231,6 +231,9 @@ Detailed list of changes 0.50.0 [future] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- macOS: Commit in-progress IME or dictation text instead of discarding it when + the OS window loses focus, matching native macOS applications + - Fix wide characters being drawn on the wrong row after colliding with scaled text and emoji sequences being split into multiple cells when wrapped onto the next line (:pull:`10767`) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index b65ff9a40..887faae03 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -621,6 +621,8 @@ static const NSRange kEmptyRange = {NSNotFound, 0}; static void update_titlebar_button_visibility_after_fullscreen_transition(_GLFWwindow *, bool, bool); static void _glfwUpdateNotchCover(_GLFWwindow *); +static void flush_focus_loss_text(_GLFWwindow *window); + @implementation GLFWWindowDelegate - (instancetype)initWithGlfwWindow:(_GLFWwindow *)initWindow { @@ -739,6 +741,8 @@ static void _glfwUpdateNotchCover(_GLFWwindow *); - (void)windowDidBecomeKey:(NSNotification *)notification { (void)notification; + // Commit any pre-edit text still pending from the last focus loss before new input arrives + flush_focus_loss_text(window); if (_glfw.ns.disabledCursorWindow == window) _glfwCenterCursorInContentArea(window); _glfwInputWindowFocus(window, true); @@ -908,6 +912,9 @@ static void _glfwUpdateNotchCover(_GLFWwindow *); NSRange markedTextSelectedRange; NSRect markedRect; bool marked_text_cleared_by_insert; + bool committing_marked_text, marked_text_committed_by_ime; + NSString *focus_loss_text; + bool focus_loss_text_sent; int in_key_handler; NSString *input_source_at_last_key_event; GLFWDraggingSource *dragging_source; @@ -966,6 +973,7 @@ static void _glfwUpdateNotchCover(_GLFWwindow *); - (void)dealloc { [trackingArea release]; [markedText release]; + [focus_loss_text release]; [dragging_source release]; if (input_source_at_last_key_event) [input_source_at_last_key_event release]; [input_context release]; @@ -973,6 +981,7 @@ static void _glfwUpdateNotchCover(_GLFWwindow *); } - (void)removeGLFWWindow { + [self clearFocusLossText]; window = NULL; } @@ -2052,6 +2061,7 @@ glfwCocoaPreserveDroppedFilePromises(GLFWwindow *w) { } - (void)setMarkedText:(id)string selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange { + if (committing_marked_text) return; const char *s = polymorphic_string_as_utf8(string); debug_input( "\n\tsetMarkedText: %s selectedRange: (%lu, %lu) replacementRange: (%lu, %lu)\n", @@ -2101,19 +2111,79 @@ glfwCocoaPreserveDroppedFilePromises(GLFWwindow *w) { markedTextSelectedRange = kEmptyRange; } +static void +flush_focus_loss_text(_GLFWwindow *window) { + [(GLFWContentView *)window->ns.view flushFocusLossText]; +} + void _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { [w->ns.view updateIMEStateFor:ev->type focused:(bool)ev->focused]; } -- (void)updateIMEStateFor:(GLFWIMEUpdateType)which focused:(bool)focused { - if (which == GLFW_IME_UPDATE_FOCUS && !focused && [self hasMarkedText] && window) { - [input_context discardMarkedText]; - [self unmarkText]; - GLFWkeyevent glfw_keyevent = {.ime_state = GLFW_IME_PREEDIT_CHANGED}; +- (void)commitMarkedText { + NSString *text = [[[markedText string] copy] autorelease]; + [self unmarkText]; + // Reset the input method. Some input methods, such as dictation, respond + // by calling insertText with their final result, use it if they do. + committing_marked_text = true; + marked_text_committed_by_ime = false; + [self clearFocusLossText]; + [input_context discardMarkedText]; + committing_marked_text = false; + if (!window || marked_text_committed_by_ime) return; + // Other input methods may deliver their final result asynchronously, so + // wait briefly for it before committing the pre-edit text. + debug_input("Waiting for IME result before committing pre-edit text on focus loss: %s\n", [text UTF8String]); + focus_loss_text = [text retain]; + focus_loss_text_sent = false; + [self performSelector:@selector(sendFocusLossText) withObject:nil afterDelay:0.2]; +} + +- (void)sendFocusLossText { + [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sendFocusLossText) object:nil]; + if (!focus_loss_text || focus_loss_text_sent) return; + focus_loss_text_sent = true; + if (window) { + debug_input("Committing pre-edit text on focus loss: %s\n", [focus_loss_text UTF8String]); + GLFWkeyevent glfw_keyevent = {.text = [focus_loss_text UTF8String], .ime_state = GLFW_IME_COMMIT_TEXT}; _glfwInputKeyboard(window, &glfw_keyevent); _glfw.ns.text[0] = 0; } + // Remember the committed text for a while, to de-duplicate a result the + // input method delivers even later + [self performSelector:@selector(clearFocusLossText) withObject:nil afterDelay:2.0]; +} + +- (void)clearFocusLossText { + [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sendFocusLossText) object:nil]; + [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(clearFocusLossText) object:nil]; + [focus_loss_text release]; + focus_loss_text = nil; +} + +- (void)flushFocusLossText { + if (!focus_loss_text) return; + [self sendFocusLossText]; + [self clearFocusLossText]; +} + +- (void)updateIMEStateFor:(GLFWIMEUpdateType)which focused:(bool)focused { + if (which == GLFW_IME_UPDATE_FOCUS && !focused && [self hasMarkedText] && window) { + if (!_glfwPlatformWindowFocused(window)) { + // The OS window lost focus, commit the pre-edit text as native + // macOS applications do, so that it is not lost + [self commitMarkedText]; + } else { + // Focus moved to another kitty window or the window is being + // closed, cancel the composition + [input_context discardMarkedText]; + [self unmarkText]; + GLFWkeyevent glfw_keyevent = {.ime_state = GLFW_IME_PREEDIT_CHANGED}; + _glfwInputKeyboard(window, &glfw_keyevent); + _glfw.ns.text[0] = 0; + } + } if (which != GLFW_IME_UPDATE_CURSOR_POSITION) return; if (_glfwPlatformWindowFocused(window)) [[window->ns.view inputContext] invalidateCharacterCoordinates]; @@ -2179,6 +2249,22 @@ _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { } const char *utf8 = polymorphic_string_as_utf8(string); debug_input("\n\tinsertText: %s replacementRange: (%lu, %lu)\n", utf8, replacementRange.location, replacementRange.length); + if (focus_loss_text && !in_key_handler && !committing_marked_text) { + // The input method delivered its result after the OS window lost focus + NSString *text = [string isKindOfClass:[NSAttributedString class]] ? [(NSAttributedString *)string string] : (NSString *)string; + if (focus_loss_text_sent) { + if ([text isEqualToString:focus_loss_text]) { + debug_input("Ignoring IME result identical to pre-edit text already committed on focus loss\n"); + [self clearFocusLossText]; + return; + } + if ([text hasPrefix:focus_loss_text]) { + utf8 = [[text substringFromIndex:[focus_loss_text length]] UTF8String]; + debug_input("Sending only the part of the IME result not already committed on focus loss: %s\n", utf8); + } + } else debug_input("Using IME result instead of pre-edit text on focus loss\n"); + [self clearFocusLossText]; + } if ([self hasMarkedText] && !is_ascii_control_char(utf8[0])) { [self unmarkText]; marked_text_cleared_by_insert = true; @@ -2192,11 +2278,12 @@ _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { // Outside key handling there is no key event to accumulate text for. This // path already commits immediately, so do not constrain a full IME result // to the fixed-size buffer used by the key handlers. - if (!in_key_handler) { + if (!in_key_handler || committing_marked_text) { if (!is_ascii_control_char(utf8[0])) { debug_input("Sending text to kitty from insertText called from event loop: %s\n", utf8); GLFWkeyevent glfw_keyevent = {.text = utf8, .ime_state = GLFW_IME_COMMIT_TEXT}; _glfwInputKeyboard(window, &glfw_keyevent); + if (committing_marked_text) marked_text_committed_by_ime = true; } _glfw.ns.text[0] = 0; return; diff --git a/kitty/glfw.c b/kitty/glfw.c index 04ae94688..f1cb91f59 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -983,12 +983,22 @@ window_focus_callback(GLFWwindow *w, int focused) { osw->last_mouse_activity_at = now; osw->cursor_blink_zero_time = now; if (is_window_ready_for_callbacks()) { - WINDOW_CALLBACK(on_focus, "O", focused ? Py_True : Py_False); - if (!osw || osw->id != wid) osw = os_window_for_id(wid); - if (osw) { - GLFWIMEUpdateEvent ev = {.type = GLFW_IME_UPDATE_FOCUS, .focused = focused}; + GLFWIMEUpdateEvent ev = {.type = GLFW_IME_UPDATE_FOCUS, .focused = focused}; + // On focus loss update the IME before reporting the focus change, so + // that any pre-edit text the IME commits reaches the child before the + // focus out event. Updating the IME can dispatch key events which reset + // callback_os_window, so look it up again. + if (!focused) { glfwUpdateIMEState(osw->handle, &ev); - if (focused) { + osw = os_window_for_id(wid); + } + if (osw) { + WINDOW_CALLBACK(on_focus, "O", focused ? Py_True : Py_False); + if (!osw || osw->id != wid) osw = os_window_for_id(wid); + } + if (osw && focused) { + glfwUpdateIMEState(osw->handle, &ev); + if ((osw = os_window_for_id(wid))) { Tab *tab = osw->tabs + osw->active_tab; Window *window = tab->windows + tab->active_window; if (window->render_data.screen) update_ime_position(window, window->render_data.screen);