fix: dismiss active launcher window on Escape

This commit is contained in:
2026-10-17 00:05:20 +02:00
parent bc7e4fd013
commit a891d49bb1
10 changed files with 98 additions and 50 deletions
+6 -3
View File
@@ -202,7 +202,7 @@ import Testing
}
}
@Test @MainActor func emojiCommandRestoresLauncherSearchOnEscape() throws {
@Test @MainActor func emojiCommandBackNavigationRestoresLauncherSearch() throws {
_ = NSApplication.shared
let suite = "ConjureEmojiCommand.\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suite)!
@@ -215,6 +215,9 @@ import Testing
search.stringValue = "emoji"
launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: search))
_ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.insertNewline(_:)))
let back = try #require(descendants(content).compactMap { $0 as? NSButton }.first {
!$0.isHidden && $0.accessibilityLabel() == "Back to applications"
})
let grid = try #require(descendants(content).compactMap { $0 as? NSCollectionView }.first)
search.stringValue = ":woman_technologist:"
launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: search))
@@ -224,12 +227,12 @@ import Testing
$0.title == "Actions" && $0.target is EmojiPickerController
})
(actionsButton.target as? EmojiPickerController)?.showActions()
_ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.cancelOperation(_:)))
back.performClick(nil)
#expect(descendants(content).contains { $0 === grid })
#expect(search.stringValue == ":woman_technologist:")
let copyButton = try #require(descendants(content).compactMap { $0 as? NSButton }.first { $0.title == "Copy and Paste" })
#expect(copyButton.keyEquivalent == "\r")
_ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.cancelOperation(_:)))
back.performClick(nil)
#expect(!descendants(content).contains { $0 === grid })
#expect(search.stringValue == "emoji")
let table = try #require(descendants(content).compactMap { $0 as? NSTableView }.first)
@@ -0,0 +1,60 @@
import AppKit
import ConjureKit
import Testing
@testable import Conjure
@Test(arguments: ["launcher", "launcher-actions", "emoji", "emoji-actions", "translate-input", "translate-output", "translate-key"])
@MainActor func escapeDismissesEntireLauncher(_ destination: String) throws {
_ = NSApplication.shared
let suite = "ConjureEscape.\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let launcher = LauncherWindowController(
history: LaunchHistory(defaults: defaults, now: { Date(timeIntervalSinceReferenceDate: 12_000) }),
defaults: defaults)
let window = try #require(launcher.window)
defer { window.orderOut(nil) }
let content = try #require(window.contentView)
func descendants(_ view: NSView) -> [NSView] { [view] + view.subviews.flatMap(descendants) }
let search = try #require(descendants(content).compactMap { $0 as? NSSearchField }.first)
if !destination.hasPrefix("launcher") {
search.stringValue = destination.hasPrefix("emoji") ? "emoji" : "translate"
launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: search))
_ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.insertNewline(_:)))
}
window.makeKeyAndOrderFront(nil)
window.makeFirstResponder(search)
if destination == "launcher-actions" {
search.stringValue = "emoji"
launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: search))
let button = try #require(descendants(content).compactMap { $0 as? NSButton }.first {
$0.title == "Actions" && $0.target === launcher
})
button.performClick(nil)
}
if destination == "emoji-actions" {
let button = try #require(descendants(content).compactMap { $0 as? NSButton }.first {
$0.title == "Actions" && $0.target is EmojiPickerController
})
(button.target as? EmojiPickerController)?.showActions()
} else if destination == "translate-output" {
let output = try #require(descendants(content).compactMap { $0 as? NSTextView }.first {
$0.accessibilityLabel() == "Japanese translation"
})
window.makeFirstResponder(output)
} else if destination == "translate-key" {
let provider = try #require(descendants(content).compactMap { $0 as? NSPopUpButton }.first)
provider.selectItem(at: 1)
_ = provider.target?.perform(provider.action, with: provider)
let key = try #require(descendants(content).compactMap { $0 as? NSSecureTextField }.first)
window.makeFirstResponder(key)
}
#expect(window.isVisible)
let escape = try #require(NSEvent.keyEvent(
with: .keyDown, location: .zero, modifierFlags: [], timestamp: 1,
windowNumber: window.windowNumber, context: nil, characters: "\u{1b}",
charactersIgnoringModifiers: "\u{1b}", isARepeat: false, keyCode: 53))
window.sendEvent(escape)
#expect(!window.isVisible)
#expect(!descendants(content).contains { $0 is NSCollectionView })
}
+1 -7
View File
@@ -26,7 +26,7 @@ import Testing
}
}
@Test @MainActor func translationCommandOpensToolAndEscapeRestoresSearch() throws {
@Test @MainActor func translationCommandBackNavigationRestoresSearch() throws {
_ = NSApplication.shared
let launcher = LauncherWindowController()
let content = try #require(launcher.window?.contentView)
@@ -42,12 +42,6 @@ import Testing
_ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.insertNewline(_:)))
#expect(search.stringValue.isEmpty)
#expect(search.accessibilityLabel() == "English text to translate into Japanese")
search.stringValue = "Good morning"
launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: search))
_ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.cancelOperation(_:)))
#expect(search.stringValue == "translate")
#expect(table.numberOfRows == 1)
_ = launcher.control(search, textView: NSTextView(), doCommandBy: #selector(NSResponder.insertNewline(_:)))
let back = try #require(descendants(content).compactMap { $0 as? NSButton }.first {
!$0.isHidden && $0.accessibilityLabel() == "Back to applications"
})