The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.
Easel model in CraCore (not used yet): items, tools, hit-testing, saving
Victorf7e3b8ff0043
diff --git a/Packages/CraCore/Sources/CraCore/Easel.swift b/Packages/CraCore/Sources/CraCore/Easel.swiftnew file mode 100644index 0000000..79bfe32--- /dev/null+++ b/Packages/CraCore/Sources/CraCore/Easel.swift@@ -0,0 +1,95 @@+import Foundation++/// Arc's Easel (File ▸ New Easel ⌃⇧E, Arc 1.172.1, docs/arc-parity.md "Easels, measured"): a tall+/// canvas the page area shows instead of a page, with a title box and things drawn with the toolbar's+/// tools. Saved on its own, one JSON file per easel; the sidebar row only points at it.+public struct Easel: Codable, Equatable, Sendable {+ /// Arc's canvas: the page's width, 2436 pt tall, scrolling.+ public static let canvasHeight: Double = 2436+ /// Arc's title box: 894 pt wide, centred, 52 pt below the top.+ public static let titleWidth: Double = 894, titleTop: Double = 52++ public var id: UUID+ public var title: String+ public var items: [Item]++ public init(id: UUID = UUID(), title: String = "Untitled Easel", items: [Item] = []) {+ (self.id, self.title, self.items) = (id, title, items)+ }++ /// The toolbar's tools, in Arc's order (EaselIconPointer … EaselIconFreehand).+ public enum Tool: String, CaseIterable, Sendable {+ case pointer, image, text, ellipse, rectangle, arrow, freehand+ }++ public struct Rect: Codable, Equatable, Sendable {+ public var x, y, width, height: Double+ public init(x: Double, y: Double, width: Double, height: Double) { (self.x, self.y, self.width, self.height) = (x, y, width, height) }+ /// The box two corners span, whichever way the drag went.+ public init(from a: Point, to b: Point) {+ self.init(x: min(a.x, b.x), y: min(a.y, b.y), width: abs(a.x - b.x), height: abs(a.y - b.y))+ }+ public func contains(_ p: Point) -> Bool { p.x >= x && p.x <= x + width && p.y >= y && p.y <= y + height }+ public func offset(by d: Point) -> Rect { Rect(x: x + d.x, y: y + d.y, width: width, height: height) }+ }++ public struct Point: Codable, Equatable, Sendable {+ public var x, y: Double+ public init(_ x: Double, _ y: Double) { (self.x, self.y) = (x, y) }+ }++ public struct Item: Codable, Equatable, Identifiable, Sendable {+ public enum Kind: Codable, Equatable, Sendable {+ case text(String, size: Double)+ case ellipse+ case rectangle+ case arrow(from: Point, to: Point)+ case freehand([Point])+ /// A picture kept next to the easel's file, by name.+ case image(file: String)+ }+ public var id: UUID+ public var kind: Kind+ public var frame: Rect+ /// #RRGGBB; nil draws in the palette's foreground.+ public var color: String?++ public init(id: UUID = UUID(), kind: Kind, frame: Rect, color: String? = nil) {+ (self.id, self.kind, self.frame, self.color) = (id, kind, frame, color)+ }++ /// The box a drag with `tool` from `a` to `b` makes; nil for tools that don't draw by dragging.+ public static func drawn(_ tool: Tool, from a: Point, to b: Point) -> Item? {+ let box = Rect(from: a, to: b)+ switch tool {+ case .ellipse: return Item(kind: .ellipse, frame: box)+ case .rectangle: return Item(kind: .rectangle, frame: box)+ case .arrow: return Item(kind: .arrow(from: a, to: b), frame: box)+ case .pointer, .image, .text, .freehand: return nil+ }+ }++ /// The same item moved by `d`.+ public func moved(by d: Point) -> Item {+ var out = self+ out.frame = frame.offset(by: d)+ switch kind {+ case .arrow(let a, let b): out.kind = .arrow(from: Point(a.x + d.x, a.y + d.y), to: Point(b.x + d.x, b.y + d.y))+ case .freehand(let pts): out.kind = .freehand(pts.map { Point($0.x + d.x, $0.y + d.y) })+ default: break+ }+ return out+ }+ }++ /// The topmost item under a point (the last drawn wins), for the pointer tool.+ public func item(at p: Point) -> Item? { items.last { $0.frame.contains(p) } }++ /// A freehand stroke's box, from its points.+ public static func freehand(_ points: [Point]) -> Item? {+ guard let first = points.first else { return nil }+ let xs = points.map(\.x), ys = points.map(\.y)+ return Item(kind: .freehand(points), frame: Rect(x: xs.min() ?? first.x, y: ys.min() ?? first.y,+ width: (xs.max() ?? 0) - (xs.min() ?? 0), height: (ys.max() ?? 0) - (ys.min() ?? 0)))+ }+}diff --git a/Packages/CraCore/Tests/CraCoreTests/EaselTests.swift b/Packages/CraCore/Tests/CraCoreTests/EaselTests.swiftnew file mode 100644index 0000000..25b2840--- /dev/null+++ b/Packages/CraCore/Tests/CraCoreTests/EaselTests.swift@@ -0,0 +1,44 @@+import CraCore+import Foundation+import Testing++/// Arc's Easel model (Arc 1.172.1): what the toolbar's tools make, hit-testing and the saved file.+struct EaselTests {+ @Test func aNewEaselIsArcs() {+ let e = Easel()+ #expect(e.title == "Untitled Easel" && e.items.isEmpty)+ #expect(Easel.Tool.allCases.map(\.rawValue) == ["pointer", "image", "text", "ellipse", "rectangle", "arrow", "freehand"])+ }++ @Test func dragsDrawShapesWhicheverWayTheyGo() {+ let r = Easel.Item.drawn(.rectangle, from: .init(200, 150), to: .init(100, 50))+ #expect(r?.kind == .rectangle && r?.frame == .init(x: 100, y: 50, width: 100, height: 100))+ let a = Easel.Item.drawn(.arrow, from: .init(10, 10), to: .init(60, 30))+ #expect(a?.kind == .arrow(from: .init(10, 10), to: .init(60, 30)))+ #expect(Easel.Item.drawn(.pointer, from: .init(0, 0), to: .init(5, 5)) == nil)+ }++ @Test func thePointerPicksTheTopmostItem() {+ var e = Easel()+ let low = Easel.Item(kind: .rectangle, frame: .init(x: 0, y: 0, width: 100, height: 100))+ let high = Easel.Item(kind: .ellipse, frame: .init(x: 50, y: 50, width: 100, height: 100))+ e.items = [low, high]+ #expect(e.item(at: .init(75, 75))?.id == high.id)+ #expect(e.item(at: .init(10, 10))?.id == low.id)+ #expect(e.item(at: .init(400, 400)) == nil)+ }++ @Test func movingCarriesArrowsAndStrokes() {+ let stroke = Easel.freehand([.init(0, 0), .init(10, 20), .init(30, 5)])!+ #expect(stroke.frame == .init(x: 0, y: 0, width: 30, height: 20))+ let moved = stroke.moved(by: .init(5, 5))+ #expect(moved.kind == .freehand([.init(5, 5), .init(15, 25), .init(35, 10)]) && moved.frame.x == 5)+ }++ @Test func survivesASave() throws {+ var e = Easel(title: "Moodboard")+ e.items = [Easel.Item(kind: .text("Hello", size: 22), frame: .init(x: 1, y: 2, width: 3, height: 4), color: "#FF0000"),+ Easel.Item(kind: .image(file: "a.png"), frame: .init(x: 0, y: 0, width: 10, height: 10))]+ #expect(try JSONDecoder().decode(Easel.self, from: JSONEncoder().encode(e)) == e)+ }+}