style: match Raycast notes window

This commit is contained in:
Valdemar Lorenzen
2026-10-19 15:26:42 +02:00
parent 1c3ecb232b
commit 999d44226d
7 changed files with 332 additions and 73 deletions
+71 -5
View File
@@ -21,8 +21,6 @@ func notesSwitchWithoutLeakingEditsUndoOrAttachmentsAndRestoreSelection() throws
let views = descendants(controller.view)
let editor = try #require(views.compactMap { $0 as? NSTextView }.first)
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" })
@@ -54,7 +52,6 @@ func notesSwitchWithoutLeakingEditsUndoOrAttachmentsAndRestoreSelection() throws
#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)
@@ -99,7 +96,6 @@ func notesSwitchWithoutLeakingEditsUndoOrAttachmentsAndRestoreSelection() throws
#expect(reopenedEditor.string == first.contents.string)
// Removing Markdown must also remove its previous visual style without changing the source.
reopenedEditor.insertText("Plain text", replacementRange: NSRange(location: 0, length: reopenedEditor.string.utf16.count))
#expect((reopenedEditor.textStorage?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont)?.pointSize == 14)
#expect(reopenedEditor.string == "Plain text")
let backup = directory.appendingPathExtension("backup")
defer { try? FileManager.default.removeItem(at: backup) }
@@ -183,7 +179,6 @@ func notesLauncherRoutesShortcutsAndTearsDownPickersWithoutTakingEditorNavigatio
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)
@@ -1282,3 +1277,74 @@ func typingMarkersBeforeExistingWhitespaceWaitsForAnExplicitSeparator() throws {
}
}
}
@Test @MainActor
func notesFormattingPreservesAttachmentsMarkdownSelectionAndUndo() throws {
_ = NSApplication.shared
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
let suite = "NotesFormatting.\(UUID().uuidString)"
let defaults = try #require(UserDefaults(suiteName: suite))
defer {
try? FileManager.default.removeItem(at: directory)
defaults.removePersistentDomain(forName: suite)
}
let controller = NotesController(defaults: defaults, directory: directory)
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 570, height: 404),
styleMask: [.borderless], backing: .buffered, defer: false)
window.contentViewController = controller
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? NotesTextView }.first)
func click(_ label: String) throws {
let button = try #require(views.compactMap { $0 as? NSButton }.first { $0.accessibilityLabel() == label })
button.performClick(nil)
}
window.makeFirstResponder(editor)
editor.insertText("🦊 first\nsecond\nuntouched", replacementRange: editor.selectedRange())
let selection = NSRange(location: 0, length: "🦊 first\nsecond\n".utf16.count)
editor.setSelectedRange(selection)
try click("Bullet list")
#expect(editor.markdownString == "- 🦊 first\n- second\nuntouched")
try click("Ordered list")
#expect(editor.markdownString == "1. 🦊 first\n2. second\nuntouched")
try click("Checklist")
#expect(editor.markdownString == "- [ ] 🦊 first\n- [ ] second\nuntouched")
try click("Checklist")
#expect(editor.markdownString == "🦊 first\nsecond\nuntouched")
let originalSelection = editor.selectedRange()
try click("Bold")
#expect(editor.markdownString == "**🦊 first\nsecond\n**untouched")
try click("Bold")
#expect(editor.markdownString == "🦊 first\nsecond\nuntouched")
#expect(editor.selectedRange() == originalSelection)
editor.setSelectedRange(NSRange(location: 3, length: 0))
try click("Code block")
#expect(editor.markdownString == "```\n🦊 first\n```\nsecond\nuntouched")
try click("Code block")
#expect(editor.markdownString == "🦊 first\nsecond\nuntouched")
editor.setSelectedRange(NSRange(location: editor.string.utf16.count, length: 0))
let bitmap = try #require(NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 1, pixelsHigh: 1,
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0))
bitmap.setColor(.red, atX: 0, y: 0)
let file = FileWrapper(regularFileWithContents: try #require(bitmap.representation(using: .png, properties: [:])))
file.preferredFilename = "pixel.png"
let attachment = NSTextAttachment(fileWrapper: file)
editor.insertText(NSAttributedString(attachment: attachment), replacementRange: editor.selectedRange())
editor.setSelectedRange(NSRange(location: 0, length: editor.string.utf16.count))
let before = editor.markdownString
editor.breakUndoCoalescing()
editor.undoManager?.removeAllActions()
try click("Quote")
try #require(editor.markdownString == "> 🦊 first\n> second\n> untouched\u{fffc}")
#expect(editor.markdownContents.attribute(.attachment, at: editor.markdownContents.length - 1, effectiveRange: nil) is NSTextAttachment)
editor.undoManager?.endUndoGrouping()
editor.undoManager?.undo()
try #require(editor.markdownString == before)
#expect(editor.markdownContents.attribute(.attachment, at: editor.markdownContents.length - 1, effectiveRange: nil) is NSTextAttachment)
#expect(controller.save())
let saved = try #require(NotesStore(directory: directory).notes.first)
#expect(saved.contents.string == before)
#expect(saved.contents.attribute(.attachment, at: saved.contents.length - 1, effectiveRange: nil) is NSTextAttachment)
}