From 4f44945c0743d5e1d6432eacf591c69ed02af545 Mon Sep 17 00:00:00 2001 From: pagedown Date: Fri, 24 Feb 2023 19:57:09 +0800 Subject: [PATCH] macOS: Restore pre-edit text after inserting text from the service Add comments to explain how to get the methods to be called. --- glfw/cocoa_window.m | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index 7a9f542f8..9a8ed8751 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -1593,6 +1593,7 @@ void _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { } // Selected text as input to be sent to Services +// For example, after selecting an absolute path, open the global menu bar kitty->Services and click `Show in Finder`. - (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types { if (!_glfw.callbacks.get_current_selection) return NO; @@ -1613,6 +1614,7 @@ void _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { } // Service output to be handled +// For example, open System Settings->Keyboard->Keyboard Shortcuts->Services->Text, enable `Convert Text to Full Width`, select some text and execute the service. - (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard { NSString* text = nil; @@ -1625,17 +1627,17 @@ void _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { return NO; } if (text && [text length] > 0) { - // Terminal.app inserts the output, do the same + // The service wants us to replace the selection, but we can't replace anything but insert text. const char *utf8 = polymorphic_string_as_utf8(text); - if ([self hasMarkedText]) { - [self unmarkText]; - debug_key("Clearing pre-edit because insertText called from readSelectionFromPasteboard\n"); - GLFWkeyevent glfw_keyevent = {.ime_state = GLFW_IME_PREEDIT_CHANGED}; - _glfwInputKeyboard(window, &glfw_keyevent); - } debug_key("Sending text received in readSelectionFromPasteboard as key event\n"); GLFWkeyevent glfw_keyevent = {.text=utf8, .ime_state=GLFW_IME_COMMIT_TEXT}; _glfwInputKeyboard(window, &glfw_keyevent); + // Restore pre-edit text after inserting the received text + if ([self hasMarkedText]) { + glfw_keyevent.text = [[markedText string] UTF8String]; + glfw_keyevent.ime_state = GLFW_IME_PREEDIT_CHANGED; + _glfwInputKeyboard(window, &glfw_keyevent); + } return YES; } return NO;