style: use shared notes browsing and actions panels
This commit is contained in:
@@ -3,7 +3,7 @@ import ConjureKit
|
||||
import Testing
|
||||
@testable import Conjure
|
||||
|
||||
@Test(arguments: ["launcher", "launcher-actions", "emoji", "emoji-actions", "translate-input", "translate-output", "translate-actions", "translate-providers", "notes-editor"])
|
||||
@Test(arguments: ["launcher", "launcher-actions", "emoji", "emoji-actions", "translate-input", "translate-output", "translate-actions", "translate-providers", "notes-editor", "notes-browse", "notes-actions"])
|
||||
@MainActor func escapeClosesPickerBeforeDismissingLauncher(_ destination: String) throws {
|
||||
_ = NSApplication.shared
|
||||
let suite = "ConjureEscape.\(UUID().uuidString)"
|
||||
@@ -58,6 +58,14 @@ import Testing
|
||||
$0.accessibilityLabel() == "Japanese translation"
|
||||
})
|
||||
window.makeFirstResponder(output)
|
||||
} else if destination == "notes-browse" || destination == "notes-actions" {
|
||||
let shortcut = try #require(NSEvent.keyEvent(
|
||||
with: .keyDown, location: .zero, modifierFlags: .command, timestamp: 1,
|
||||
windowNumber: window.windowNumber, context: nil,
|
||||
characters: destination == "notes-browse" ? "p" : "k",
|
||||
charactersIgnoringModifiers: destination == "notes-browse" ? "p" : "k",
|
||||
isARepeat: false, keyCode: destination == "notes-browse" ? 35 : 40))
|
||||
#expect(window.performKeyEquivalent(with: shortcut))
|
||||
} else if destination == "notes-editor" {
|
||||
let editor = try #require(descendants(content).compactMap { $0 as? NSTextView }.first {
|
||||
$0.accessibilityLabel() == "Note editor"
|
||||
@@ -70,11 +78,21 @@ import Testing
|
||||
windowNumber: window.windowNumber, context: nil, characters: "\u{1b}",
|
||||
charactersIgnoringModifiers: "\u{1b}", isARepeat: false, keyCode: 53))
|
||||
window.sendEvent(escape)
|
||||
if destination.hasSuffix("-actions") || destination == "translate-providers" {
|
||||
if destination.hasSuffix("-actions") || destination == "translate-providers" || destination == "notes-browse" {
|
||||
#expect(window.isVisible)
|
||||
#expect(window.firstResponder === search.currentEditor())
|
||||
if destination.hasPrefix("notes") {
|
||||
let editor = try #require(descendants(content).compactMap { $0 as? NSTextView }.first {
|
||||
$0.accessibilityLabel() == "Note editor"
|
||||
})
|
||||
#expect(window.firstResponder === editor)
|
||||
} else {
|
||||
#expect(window.firstResponder === search.currentEditor())
|
||||
}
|
||||
window.sendEvent(escape)
|
||||
}
|
||||
#expect(!window.isVisible)
|
||||
#expect(!descendants(content).contains { $0 is NSCollectionView })
|
||||
#expect(!descendants(content).compactMap { $0 as? NSSearchField }.contains {
|
||||
$0.accessibilityLabel() == "Search notes" || $0.accessibilityLabel() == "Search notes actions"
|
||||
})
|
||||
}
|
||||
|
||||
@@ -118,4 +118,91 @@ struct LanguagePickerTests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func notesShareScrollableFuzzySelectionAndKeepDuplicateTitlesDistinct() throws {
|
||||
_ = NSApplication.shared
|
||||
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 750, height: 474),
|
||||
styleMask: .borderless, backing: .buffered, defer: false)
|
||||
window.isReleasedWhenClosed = false
|
||||
let host = try #require(window.contentView)
|
||||
let pill = NSView(frame: NSRect(x: 500, y: 8, width: 230, height: 36))
|
||||
let anchor = NSButton(frame: pill.bounds)
|
||||
pill.addSubview(anchor)
|
||||
host.addSubview(pill)
|
||||
let popup = ApplicationActionsController()
|
||||
defer { popup.close(animated: false); window.orderOut(nil) }
|
||||
var notes = (0..<20).map { (id: UUID(), title: String(format: "Note %02d", $0)) }
|
||||
notes[18].title = "Shared Plan"
|
||||
notes[19].title = "Shared Plan"
|
||||
var selected: UUID?
|
||||
popup.selectNote = { selected = $0 }
|
||||
popup.showNotes(notes, selected: notes[19].id, relativeTo: anchor)
|
||||
host.layoutSubtreeIfNeeded()
|
||||
let table = try #require(descendants(popup.view).compactMap { $0 as? NSTableView }.first)
|
||||
#expect(table.accessibilityLabel() == "Notes")
|
||||
#expect(table.numberOfRows == 20)
|
||||
#expect(table.selectedRow == 19)
|
||||
#expect(NSLocationInRange(table.selectedRow, table.rows(in: table.visibleRect)))
|
||||
#expect(table.visibleRect.height < table.bounds.height)
|
||||
let currentCell = try #require(table.view(atColumn: 0, row: 19, makeIfNecessary: true))
|
||||
let current = try #require(descendants(currentCell).compactMap { $0 as? NSButton }.first)
|
||||
#expect(current.accessibilityValue() as? String == "Current note")
|
||||
popup.moveSelection(by: -1)
|
||||
#expect(popup.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(selected == notes[18].id)
|
||||
popup.close(animated: false)
|
||||
|
||||
popup.showNotes(notes, selected: notes[19].id, relativeTo: anchor)
|
||||
let search = try #require(descendants(popup.view).compactMap { $0 as? NSSearchField }.first)
|
||||
#expect(search.accessibilityLabel() == "Search notes")
|
||||
search.stringValue = "shpl"
|
||||
search.delegate?.controlTextDidChange?(Notification(name: NSControl.textDidChangeNotification, object: search))
|
||||
let filteredTable = try #require(descendants(popup.view).compactMap { $0 as? NSTableView }.first)
|
||||
#expect(filteredTable.numberOfRows == 2)
|
||||
popup.moveSelection(by: 1)
|
||||
#expect(popup.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(selected == notes[19].id)
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func notesActionsSearchAndCommandShortcutsUseTheSharedPopup() throws {
|
||||
_ = NSApplication.shared
|
||||
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 750, height: 474),
|
||||
styleMask: .borderless, backing: .buffered, defer: false)
|
||||
window.isReleasedWhenClosed = false
|
||||
let host = try #require(window.contentView)
|
||||
let pill = NSView(frame: NSRect(x: 500, y: 8, width: 230, height: 36))
|
||||
let anchor = NSButton(frame: pill.bounds)
|
||||
pill.addSubview(anchor)
|
||||
host.addSubview(pill)
|
||||
let popup = ApplicationActionsController()
|
||||
defer { popup.close(animated: false); window.orderOut(nil) }
|
||||
var browsed = 0
|
||||
var created = 0
|
||||
popup.browseNotes = { browsed += 1 }
|
||||
popup.newNote = { created += 1 }
|
||||
|
||||
popup.showNotesActions(relativeTo: anchor)
|
||||
let search = try #require(descendants(popup.view).compactMap { $0 as? NSSearchField }.first)
|
||||
#expect(search.accessibilityLabel() == "Search notes actions")
|
||||
search.stringValue = "new"
|
||||
search.delegate?.controlTextDidChange?(Notification(name: NSControl.textDidChangeNotification, object: search))
|
||||
#expect(popup.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(created == 1)
|
||||
popup.close(animated: false)
|
||||
|
||||
for key in ["p", "n"] {
|
||||
popup.showNotesActions(relativeTo: anchor)
|
||||
let field = try #require(descendants(popup.view).compactMap { $0 as? NSSearchField }.first)
|
||||
let event = try #require(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: .command,
|
||||
timestamp: 0, windowNumber: window.windowNumber, context: nil, characters: key,
|
||||
charactersIgnoringModifiers: key, isARepeat: false, keyCode: key == "p" ? 35 : 45))
|
||||
#expect(field.performKeyEquivalent(with: event))
|
||||
#expect(!popup.isShown)
|
||||
popup.close(animated: false)
|
||||
}
|
||||
#expect(browsed == 1)
|
||||
#expect(created == 2)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,27 +16,52 @@ func notesSwitchWithoutLeakingEditsUndoOrAttachmentsAndRestoreSelection() throws
|
||||
let controller = NotesController(defaults: defaults, directory: directory)
|
||||
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 750, height: 390), styleMask: [.borderless], backing: .buffered, defer: false)
|
||||
window.contentViewController = controller
|
||||
defer { window.orderOut(nil) }
|
||||
defer { _ = controller.close(); window.orderOut(nil) }
|
||||
func descendants(_ view: NSView) -> [NSView] { [view] + view.subviews.flatMap(descendants) }
|
||||
let views = descendants(controller.view)
|
||||
let editor = try #require(views.compactMap { $0 as? NSTextView }.first)
|
||||
let actions = try #require(views.compactMap { $0 as? NSButton }.first { $0.accessibilityLabel() == "Choose a note" })
|
||||
let menu = try #require(actions.menu)
|
||||
let firstID = try #require(menu.items.first { $0.state == .on }?.representedObject as? UUID)
|
||||
// Empty notes have duplicate titles; both must remain individually selectable.
|
||||
let browse = try #require(views.compactMap { $0 as? NSButton }.first { $0.title == "Browse Notes" })
|
||||
#expect(browse.keyEquivalent == "p")
|
||||
#expect(!views.compactMap { $0 as? NSButton }.contains { $0.title == "New Note" })
|
||||
func selectedID() throws -> UUID { try #require(defaults.string(forKey: "selectedNote").flatMap(UUID.init(uuidString:))) }
|
||||
func picker(_ tool: NotesController) throws -> NSSearchField {
|
||||
try #require(descendants(tool.view).compactMap { $0 as? NSSearchField }.first { $0.accessibilityLabel() == "Search notes" })
|
||||
}
|
||||
func filter(_ field: NSSearchField, _ query: String) {
|
||||
field.stringValue = query
|
||||
field.delegate?.controlTextDidChange?(Notification(name: NSControl.textDidChangeNotification, object: field))
|
||||
field.window?.contentView?.layoutSubtreeIfNeeded()
|
||||
}
|
||||
let firstID = try selectedID()
|
||||
// Empty notes have duplicate titles; UUID identity must survive the shared picker.
|
||||
window.makeFirstResponder(editor)
|
||||
let newNoteKey = try #require(NSEvent.keyEvent(with: .keyDown, location: .zero,
|
||||
modifierFlags: .command, timestamp: 1, windowNumber: window.windowNumber, context: nil,
|
||||
characters: "n", charactersIgnoringModifiers: "n", isARepeat: false, keyCode: 45))
|
||||
#expect(window.performKeyEquivalent(with: newNoteKey))
|
||||
let secondID = try #require(menu.items.first { $0.state == .on }?.representedObject as? UUID)
|
||||
let secondID = try selectedID()
|
||||
#expect(secondID != firstID)
|
||||
for expectedID in [firstID, secondID] {
|
||||
browse.performClick(nil)
|
||||
_ = try picker(controller)
|
||||
let table = try #require(descendants(controller.view).compactMap { $0 as? NSTableView }.first)
|
||||
#expect(table.numberOfRows == 2)
|
||||
#expect(controller.handleControlKey(table.selectedRow == 0 ? "j" : "k"))
|
||||
#expect(controller.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(try selectedID() == expectedID)
|
||||
#expect(window.firstResponder === editor)
|
||||
}
|
||||
#expect(!controller.handleControlKey("j"))
|
||||
#expect(!controller.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
editor.insertText("# Second 🦊\n**bold** and `code`", replacementRange: editor.selectedRange())
|
||||
#expect((editor.textStorage?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont)?.pointSize == 17)
|
||||
let codeRange = (editor.string as NSString).range(of: "`code`")
|
||||
let codeFont = try #require(editor.textStorage?.attribute(.font, at: codeRange.location, effectiveRange: nil) as? NSFont)
|
||||
#expect(codeFont.isFixedPitch)
|
||||
menu.performActionForItem(at: try #require(menu.items.firstIndex { $0.representedObject as? UUID == firstID }))
|
||||
browse.performClick(nil)
|
||||
filter(try picker(controller), "nttld")
|
||||
#expect(controller.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(try selectedID() == firstID)
|
||||
#expect(editor.string.isEmpty)
|
||||
#expect(editor.undoManager?.canUndo != true)
|
||||
editor.insertText("First note", replacementRange: editor.selectedRange())
|
||||
@@ -58,7 +83,7 @@ func notesSwitchWithoutLeakingEditsUndoOrAttachmentsAndRestoreSelection() throws
|
||||
characters: "\r", charactersIgnoringModifiers: "\r", isARepeat: false, keyCode: 36))
|
||||
editor.keyDown(with: returnKey)
|
||||
#expect(editor.string == "First note\u{fffc}\u{fffc}\n")
|
||||
#expect(menu.items.first { $0.state == .on }?.representedObject as? UUID == firstID)
|
||||
#expect(try selectedID() == firstID)
|
||||
let restored = try NotesStore(directory: directory)
|
||||
let first = try #require(restored.notes.first { $0.id == firstID })
|
||||
#expect(first.contents.string == "First note\u{fffc}\u{fffc}\n")
|
||||
@@ -66,6 +91,9 @@ func notesSwitchWithoutLeakingEditsUndoOrAttachmentsAndRestoreSelection() throws
|
||||
#expect(first.contents.attribute(.attachment, at: 11, effectiveRange: nil) is NSTextAttachment)
|
||||
#expect(restored.notes.first { $0.id == secondID }?.contents.string == "# Second 🦊\n**bold** and `code`")
|
||||
let reopened = NotesController(defaults: defaults, directory: directory)
|
||||
let reopenedWindow = NSWindow(contentRect: window.frame, styleMask: [.borderless], backing: .buffered, defer: false)
|
||||
reopenedWindow.contentViewController = reopened
|
||||
defer { _ = reopened.close(); reopenedWindow.orderOut(nil) }
|
||||
let reopenedViews = descendants(reopened.view)
|
||||
let reopenedEditor = try #require(reopenedViews.compactMap { $0 as? NSTextView }.first)
|
||||
#expect(reopenedEditor.string == first.contents.string)
|
||||
@@ -83,14 +111,122 @@ func notesSwitchWithoutLeakingEditsUndoOrAttachmentsAndRestoreSelection() throws
|
||||
reopened.goBack()
|
||||
#expect(!closed)
|
||||
#expect(!reopened.save())
|
||||
let reopenedActions = try #require(reopenedViews.compactMap { $0 as? NSButton }.first { $0.accessibilityLabel() == "Choose a note" })
|
||||
let reopenedMenu = try #require(reopenedActions.menu)
|
||||
reopenedMenu.performActionForItem(at: try #require(reopenedMenu.items.firstIndex { $0.representedObject as? UUID == secondID }))
|
||||
reopened.browseNotes()
|
||||
filter(try picker(reopened), "scnd")
|
||||
#expect(reopened.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(reopenedEditor.string == "Unsaved")
|
||||
#expect(reopenedMenu.items.first { $0.state == .on }?.representedObject as? UUID == firstID)
|
||||
#expect(try selectedID() == firstID)
|
||||
reopened.browseNotes()
|
||||
#expect(!reopened.close())
|
||||
#expect(!closed)
|
||||
#expect(try picker(reopened).window === reopenedWindow)
|
||||
try FileManager.default.removeItem(at: directory)
|
||||
try FileManager.default.moveItem(at: backup, to: directory)
|
||||
#expect(reopened.save())
|
||||
reopened.goBack()
|
||||
#expect(reopened.close())
|
||||
#expect(closed)
|
||||
#expect(!descendants(reopened.view).contains { ($0 as? NSSearchField)?.accessibilityLabel() == "Search notes" })
|
||||
}
|
||||
|
||||
@Test @MainActor
|
||||
func notesLauncherRoutesShortcutsAndTearsDownPickersWithoutTakingEditorNavigation() throws {
|
||||
_ = NSApplication.shared
|
||||
let suite = "NotesShortcuts.\(UUID().uuidString)"
|
||||
let defaults = try #require(UserDefaults(suiteName: suite))
|
||||
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(suite)
|
||||
defer {
|
||||
defaults.removePersistentDomain(forName: suite)
|
||||
try? FileManager.default.removeItem(at: directory)
|
||||
}
|
||||
let store = try NotesStore(directory: directory)
|
||||
let atlas = try store.create()
|
||||
let journal = try store.create()
|
||||
try store.update(id: atlas, contents: NSAttributedString(string: "Project Atlas"))
|
||||
try store.update(id: journal, contents: NSAttributedString(string: "Weekend journal"))
|
||||
defaults.set(atlas.uuidString, forKey: "selectedNote")
|
||||
let launcher = LauncherWindowController(
|
||||
history: LaunchHistory(defaults: defaults, now: { Date(timeIntervalSinceReferenceDate: 12_000) }),
|
||||
defaults: defaults, notesDirectory: directory)
|
||||
let window = try #require(launcher.window)
|
||||
defer { launcher.dismiss(restoringFocus: false) }
|
||||
let content = try #require(window.contentView)
|
||||
func descendants(_ view: NSView) -> [NSView] { [view] + view.subviews.flatMap(descendants) }
|
||||
func search(_ label: String) throws -> NSSearchField {
|
||||
try #require(descendants(content).compactMap { $0 as? NSSearchField }.first { $0.accessibilityLabel() == label })
|
||||
}
|
||||
func key(_ text: String, code: UInt16, flags: NSEvent.ModifierFlags = []) throws -> NSEvent {
|
||||
let characters = flags == .control && text == "k" ? "\u{b}" : text
|
||||
return try #require(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags,
|
||||
timestamp: 1, windowNumber: window.windowNumber, context: nil, characters: characters,
|
||||
charactersIgnoringModifiers: text, isARepeat: false, keyCode: code))
|
||||
}
|
||||
func command(_ text: String, code: UInt16) throws {
|
||||
#expect(window.performKeyEquivalent(with: try key(text, code: code, flags: .command)))
|
||||
}
|
||||
func filter(_ field: NSSearchField, _ text: String) {
|
||||
field.stringValue = text
|
||||
field.delegate?.controlTextDidChange?(Notification(name: NSControl.textDidChangeNotification, object: field))
|
||||
content.layoutSubtreeIfNeeded()
|
||||
}
|
||||
let launcherSearch = try #require(descendants(content).compactMap { $0 as? NSSearchField }.first)
|
||||
launcherSearch.stringValue = "notes"
|
||||
launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: launcherSearch))
|
||||
#expect(launcher.control(launcherSearch, textView: NSTextView(), doCommandBy: #selector(NSResponder.insertNewline(_:))))
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
let browse = try #require(descendants(content).compactMap { $0 as? NSButton }.first { $0.title == "Browse Notes" })
|
||||
let tool = try #require(browse.target as? NotesController)
|
||||
let editor = try #require(descendants(content).compactMap { $0 as? NSTextView }.first { $0.accessibilityLabel() == "Note editor" })
|
||||
window.makeFirstResponder(editor)
|
||||
try command("p", code: 35)
|
||||
let picker = try search("Search notes")
|
||||
let table = try #require(descendants(content).compactMap { $0 as? NSTableView }.first { $0.accessibilityLabel() == "Notes" })
|
||||
#expect(window.firstResponder === picker.currentEditor())
|
||||
#expect(browse.superview?.isAccessibilityHidden() == true)
|
||||
#expect(tool.handle(#selector(NSResponder.moveUp(_:))))
|
||||
window.sendEvent(try key("j", code: 38, flags: .control))
|
||||
#expect(table.selectedRow == 1)
|
||||
window.sendEvent(try key("k", code: 40, flags: .control))
|
||||
#expect(table.selectedRow == 0)
|
||||
filter(picker, "wkndjrnl")
|
||||
#expect(table.numberOfRows == 1)
|
||||
#expect(tool.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(defaults.string(forKey: "selectedNote") == journal.uuidString)
|
||||
#expect(editor.string == "Weekend journal")
|
||||
#expect(window.firstResponder === editor)
|
||||
try command("p", code: 35)
|
||||
_ = try search("Search notes")
|
||||
try command("p", code: 35)
|
||||
#expect(window.firstResponder === editor)
|
||||
try command("k", code: 40)
|
||||
filter(try search("Search notes actions"), "Browse Notes")
|
||||
#expect(tool.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
_ = try search("Search notes")
|
||||
window.sendEvent(try key("\u{1b}", code: 53))
|
||||
#expect(window.isVisible && window.firstResponder === editor)
|
||||
try command("k", code: 40)
|
||||
filter(try search("Search notes actions"), "New Note")
|
||||
#expect(tool.handle(#selector(NSResponder.insertNewline(_:))))
|
||||
#expect(editor.string.isEmpty)
|
||||
let actionCreatedID = defaults.string(forKey: "selectedNote")
|
||||
try command("n", code: 45)
|
||||
#expect(defaults.string(forKey: "selectedNote") != actionCreatedID)
|
||||
let editorCreatedID = defaults.string(forKey: "selectedNote")
|
||||
try command("p", code: 35)
|
||||
_ = try search("Search notes")
|
||||
try command("n", code: 45)
|
||||
#expect(defaults.string(forKey: "selectedNote") != editorCreatedID)
|
||||
#expect(window.firstResponder === editor)
|
||||
#expect(!tool.handle(#selector(NSResponder.moveDown(_:))))
|
||||
#expect(!tool.handleControlKey("k"))
|
||||
editor.insertText("Navigation", replacementRange: editor.selectedRange())
|
||||
editor.setSelectedRange(NSRange(location: 0, length: 0))
|
||||
window.sendEvent(try key("k", code: 40, flags: .control))
|
||||
#expect(editor.string.isEmpty)
|
||||
editor.insertText("Editable", replacementRange: editor.selectedRange())
|
||||
editor.keyDown(with: try key("\r", code: 36))
|
||||
#expect(editor.string == "Editable\n")
|
||||
try command("p", code: 35)
|
||||
#expect(tool.close())
|
||||
#expect(!descendants(content).compactMap { $0 as? NSSearchField }.contains { $0.accessibilityLabel() == "Search notes" })
|
||||
#expect(launcherSearch.isHidden == false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user