55 lines
3.1 KiB
Swift
55 lines
3.1 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
@main
|
|
struct SpreadsheetLayoutTests {
|
|
@MainActor static func main() throws {
|
|
let source = URL(fileURLWithPath: CommandLine.arguments[1])
|
|
let layout = try JSONDecoder().decode(SpreadsheetLayout.self, from: Data(contentsOf: source)).validated()
|
|
let sheet = layout.sheets[0]
|
|
let narrow = SpreadsheetZoom.width.scale(sheet: sheet, viewport: CGSize(width: 800, height: 500))
|
|
let wide = SpreadsheetZoom.width.scale(sheet: sheet, viewport: CGSize(width: 1800, height: 900))
|
|
precondition(narrow * sheet.width <= 752)
|
|
precondition(wide == 1)
|
|
let page = SpreadsheetZoom.page.scale(sheet: sheet, viewport: CGSize(width: 800, height: 500))
|
|
precondition(page * sheet.height <= 452)
|
|
precondition(SpreadsheetZoom.actual.scale(sheet: sheet, viewport: .zero) == 1)
|
|
precondition(sheet.cells.first { $0.reference == "H30" }?.style.rotation == 255)
|
|
precondition(sheet.cells.first { $0.reference == "C25" }?.text == "001234567890")
|
|
precondition(sheet.cells.first { $0.reference == "F27" }?.text.trimmingCharacters(in: .whitespaces) == "544.30")
|
|
let signature = sheet.cells.first { $0.reference == "A30" }!
|
|
precondition(signature.textClip.width > signature.width)
|
|
print("Workbook layout, zoom bounds, vertical text and exact values passed")
|
|
if CommandLine.arguments.count > 2 {
|
|
let folder = URL(fileURLWithPath: CommandLine.arguments[2], isDirectory: true)
|
|
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
|
|
_ = NSApplication.shared
|
|
NSApplication.shared.appearance = NSAppearance(named: .aqua)
|
|
let canvas = SpreadsheetCanvasView(sheet: sheet, scale: 1)
|
|
try capture(canvas, to: folder.appendingPathComponent("sheet.png"))
|
|
for size in [CGSize(width: 1080, height: 620), CGSize(width: 820, height: 480)] {
|
|
let host = NSHostingView(rootView: ExpenseSpreadsheetPreview(layout: layout))
|
|
host.frame = NSRect(origin: .zero, size: size)
|
|
let window = NSWindow(contentRect: host.frame, styleMask: [.borderless], backing: .buffered, defer: false)
|
|
window.contentView = host
|
|
host.layoutSubtreeIfNeeded()
|
|
RunLoop.current.run(until: Date().addingTimeInterval(0.5))
|
|
try capture(host, to: folder.appendingPathComponent("preview-\(Int(size.width)).png"))
|
|
window.orderOut(nil)
|
|
}
|
|
print("Native preview snapshots written to \(folder.path)")
|
|
}
|
|
}
|
|
|
|
@MainActor static func capture(_ view: NSView, to file: URL) throws {
|
|
guard let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) else {
|
|
throw ExpenseTemplateFailure.message("Cannot create preview snapshot")
|
|
}
|
|
view.cacheDisplay(in: view.bounds, to: bitmap)
|
|
guard let png = bitmap.representation(using: .png, properties: [:]) else {
|
|
throw ExpenseTemplateFailure.message("Cannot encode preview snapshot")
|
|
}
|
|
try png.write(to: file)
|
|
}
|
|
}
|