feat: round embedded note images

This commit is contained in:
Valdemar Lorenzen
2026-10-20 12:36:35 +02:00
parent 794d927df1
commit f6d9d04b74
4 changed files with 78 additions and 1 deletions
@@ -41,6 +41,68 @@ func noteFormattingHidesSyntaxButPreservesMarkdownAndLiteralCode() throws {
#expect(reloaded.contents.string == source)
}
@Test @MainActor
func noteImagesRenderRoundedWithoutChangingSavedImageData() throws {
_ = NSApplication.shared
let image = NSImage(size: NSSize(width: 80, height: 48), flipped: false) { rect in
NSColor.red.setFill()
rect.fill()
return true
}
let tiff = try #require(image.tiffRepresentation)
let bitmap = try #require(NSBitmapImageRep(data: tiff))
let png = try #require(bitmap.representation(using: .png, properties: [:]))
let pasteboard = NSPasteboard.withUniqueName()
defer { pasteboard.releaseGlobally() }
pasteboard.setData(png, forType: .png)
let editor = NotesTextView(frame: NSRect(x: 0, y: 0, width: 200, height: 120))
editor.isRichText = true
editor.importsGraphics = true
#expect(editor.readSelection(from: pasteboard))
func checkRoundedDrawing() throws {
MarkdownHighlighting.apply(to: editor)
let layout = try #require(editor.layoutManager)
layout.ensureLayout(for: try #require(editor.textContainer))
let output = try #require(NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 200, pixelsHigh: 120,
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0))
let context = try #require(NSGraphicsContext(bitmapImageRep: output))
NSGraphicsContext.saveGraphicsState()
defer { NSGraphicsContext.restoreGraphicsState() }
NSGraphicsContext.current = context
context.cgContext.clear(CGRect(x: 0, y: 0, width: 200, height: 120))
layout.drawGlyphs(forGlyphRange: layout.glyphRange(forCharacterRange: NSRange(location: 0, length: 1),
actualCharacterRange: nil), at: .zero)
var left = 200, right = -1, top = 120, bottom = -1
for y in 0..<120 {
for x in 0..<200 where (output.colorAt(x: x, y: y)?.alphaComponent ?? 0) > 0.5 {
left = min(left, x); right = max(right, x)
top = min(top, y); bottom = max(bottom, y)
}
}
try #require(right > left && bottom > top)
for x in [left, right] {
for y in [top, bottom] {
#expect(try #require(output.colorAt(x: x, y: y)).alphaComponent < 0.1)
}
}
let center = try #require(output.colorAt(x: (left + right) / 2, y: (top + bottom) / 2)?.usingColorSpace(.deviceRGB))
#expect(center.redComponent > 0.9 && center.greenComponent < 0.1 && center.alphaComponent > 0.9)
}
try checkRoundedDrawing()
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
defer { try? FileManager.default.removeItem(at: directory) }
let store = try NotesStore(directory: directory)
let id = try store.create()
try store.update(id: id, contents: editor.markdownContents)
let reloaded = try #require(NotesStore(directory: directory).notes.first)
let attachment = try #require(reloaded.contents.attribute(.attachment, at: 0, effectiveRange: nil) as? NSTextAttachment)
#expect(attachment.fileWrapper?.regularFileContents == png)
editor.setMarkdownContents(reloaded.contents)
try checkRoundedDrawing()
}
@Test @MainActor
func hiddenNoteMarkupKeepsTypingDeletionFormattingAndUndoAtVisibleBoundaries() throws {
_ = NSApplication.shared