Share popover: leave Esc to an IME composition; drop the monitor on deinit (#178, finding 4)
Esc while an input method is composing in the email field (ja, zh-Hans, yue) belongs to the composition, which it cancels. The Esc key monitor and popoverShouldClose closed the panel instead; both now let Esc through while the field editor has marked text. The monitor is also removed in an isolated deinit, so a popover controller released while showing leaves none behind. Test: SharePopoverTests.testEscDuringAnIMECompositionLeavesThePanelOpen (marked text, Esc: the panel stays; unmarked, Esc: it closes), which fails with the composition check removed. The deinit removal has no test: the controller lives as long as its window's workspace.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+42
−1
native/apple/Sources/DocsKit/DocsSharePopover.swift+15 −1native/apple/Tests/DocsWindowTests/SharePopoverTests.swift+27 −0
diff --git a/native/apple/Sources/DocsKit/DocsSharePopover.swift b/native/apple/Sources/DocsKit/DocsSharePopover.swift| index 9ce421f..aa3b2aa 100644 |
| --- a/native/apple/Sources/DocsKit/DocsSharePopover.swift |
| +++ b/native/apple/Sources/DocsKit/DocsSharePopover.swift |
| @@ -41,7 +41,8 @@ import SwiftUI |
| popover.contentViewController?.view.window?.makeKey() |
| escMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in |
| guard event.keyCode == 53, event.modifierFlags.intersection(.deviceIndependentFlagsMask).isEmpty, |
| - let self, let panelWindow = self.popover?.contentViewController?.view.window, event.window === panelWindow else { return event } |
| + let self, let panelWindow = self.popover?.contentViewController?.view.window, event.window === panelWindow, |
| + !self.composing else { return event } |
| self.dismiss() |
| return nil |
| } |
| @@ -58,10 +59,23 @@ import SwiftUI |
| finish() |
| } |
| + /// The email field holds marked text: an input method is composing. |
| + private var composing: Bool { |
| + (popover?.contentViewController?.view.window?.firstResponder as? NSTextView)?.hasMarkedText() == true |
| + } |
| + |
| + /// A popover released while it shows must not leave its monitor behind. |
| + isolated deinit { |
| + if let escMonitor { NSEvent.removeMonitor(escMonitor) } |
| + } |
| + |
| func popoverShouldClose(_ popover: NSPopover) -> Bool { |
| // Esc, which a transient popover handles itself before the panel's |
| // Close shortcut sees it: Close's rules, always, URL or not. |
| if let event = NSApp.currentEvent, event.type == .keyDown, event.keyCode == 53 { |
| + // Esc during an IME composition (ja, zh-Hans, yue) is the input |
| + // method's: it cancels the composition, not the panel. |
| + if composing { return false } |
| returnFocus = true |
| return true |
| } |
diff --git a/native/apple/Tests/DocsWindowTests/SharePopoverTests.swift b/native/apple/Tests/DocsWindowTests/SharePopoverTests.swift| index 21ac4e8..43b2d22 100644 |
| --- a/native/apple/Tests/DocsWindowTests/SharePopoverTests.swift |
| +++ b/native/apple/Tests/DocsWindowTests/SharePopoverTests.swift |
| @@ -181,6 +181,33 @@ import XCTest |
| XCTAssertNotNil(share.panel, "and its panel is still there") |
| } |
| + /// Fable on #178, finding 4: Esc while an input method is composing in |
| + /// the email field (ja, zh-Hans, yue) belongs to the composition; the |
| + /// panel stays. Without marked text, Esc closes it. |
| + func testEscDuringAnIMECompositionLeavesThePanelOpen() async throws { |
| + let hosted = try await host() |
| + defer { Task { await close(hosted) } } |
| + let share = try await openShare(hosted) |
| + let panelWindow = try XCTUnwrap(share.popover?.contentViewController?.view.window) |
| + try await until("the email field has the keyboard") { panelWindow.firstResponder is NSTextView } |
| + let editor = try XCTUnwrap(panelWindow.firstResponder as? NSTextView) |
| + editor.setMarkedText("か", selectedRange: NSRange(location: 1, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) |
| + XCTAssertTrue(editor.hasMarkedText()) |
| + NSApp.postEvent(esc(panelWindow), atStart: false) |
| + await pump(0.4) |
| + XCTAssertNotNil(share.popover, "Esc went to the composition, not the panel") |
| + editor.unmarkText() |
| + XCTAssertFalse(editor.hasMarkedText()) |
| + NSApp.postEvent(esc(panelWindow), atStart: false) |
| + try await until("Esc closed it once nothing was composing") { share.popover == nil } |
| + } |
| + |
| + private func esc(_ window: NSWindow) -> NSEvent { |
| + NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, |
| + windowNumber: window.windowNumber, context: nil, characters: "\u{1b}", charactersIgnoringModifiers: "\u{1b}", |
| + isARepeat: false, keyCode: 53)! |
| + } |
| + |
| func testEscClosesItAndReturnsTheKeyboardToShare() async throws { |
| let hosted = try await host() |
| defer { Task { await close(hosted) } } |