fix: dismiss active launcher window on Escape
This commit is contained in:
@@ -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 })
|
||||
}
|
||||
Reference in New Issue
Block a user