90 lines · 3.7 KB · SwiftRaw
import AppKit/// The menu-bar item's image: the constellation-C, drawn as a template image/// so macOS inks it. Copied from CottonHost's `ConstellationMarkImage` and/// CottonPaper's generated `CottonMark` geometry (the app icon's own numbers).enum MenuBarMark { static let side: CGFloat = 18 private struct Point { let x: CGFloat let y: CGFloat let isKnot: Bool } private static let points: [Point] = [ Point(x: 234.7, y: 15.9, isKnot: true), Point(x: 407.7, y: 93.0, isKnot: true), Point(x: 66.6, y: 93.0, isKnot: true), Point(x: 93.1, y: 259.1, isKnot: true), Point(x: 92.9, y: 385.1, isKnot: true), Point(x: 422.7, y: 442.1, isKnot: true), Point(x: 64.3, y: 527.9, isKnot: true), Point(x: 388.2, y: 529.9, isKnot: true), Point(x: 248.2, y: 611.9, isKnot: true), Point(x: 0.0, y: 318.3, isKnot: false), Point(x: 232.7, y: 163.5, isKnot: false), Point(x: 294.1, y: 497.0, isKnot: false), ] private static let threads: [(Int, Int)] = [ (0, 1), (0, 2), (0, 3), (2, 3), (2, 9), (3, 4), (3, 9), (3, 10), (1, 10), (4, 6), (4, 8), (4, 9), (4, 11), (5, 11), (5, 7), (6, 8), (6, 9), (7, 8), ] private static let knotRadius: CGFloat = 15.5 private static let threadWidth: CGFloat = 13.5 private static let contentBounds = CGRect(x: -6.75, y: 0.4, width: 444.95, height: 627.0) /// The mark; with `dot`, a dot at its top right: the person must act. static func image(dot: Bool = false) -> NSImage { let size = NSSize(width: side, height: side) let image = NSImage(size: size, flipped: false) { _ in draw(in: size) if dot { let radius = size.width * 0.17 let ring = NSBezierPath(ovalIn: NSRect(x: size.width - radius * 2 - 0.5, y: size.height - radius * 2 - 0.5, width: radius * 2 + 1, height: radius * 2 + 1)) NSGraphicsContext.current?.compositingOperation = .clear ring.fill() NSGraphicsContext.current?.compositingOperation = .sourceOver NSColor.black.setFill() NSBezierPath(ovalIn: NSRect(x: size.width - radius * 2, y: size.height - radius * 2, width: radius * 2, height: radius * 2)).fill() } return true } image.isTemplate = true image.accessibilityDescription = "Cotton" return image } private static func draw(in size: NSSize) { let box = contentBounds let scale = min(size.width / box.width, size.height / box.height) let offsetX = (size.width - box.width * scale) / 2 - box.minX * scale let offsetY = (size.height - box.height * scale) / 2 - box.minY * scale // Floors keep the thread and knots legible at menu-bar size. let knot = max(knotRadius * scale, 0.85) let thread = max(threadWidth * scale, 0.7) // The logo is written with y running down; AppKit's runs up. func at(_ point: Point) -> CGPoint { CGPoint(x: point.x * scale + offsetX, y: size.height - (point.y * scale + offsetY)) } NSColor.black.setStroke() let lines = NSBezierPath() lines.lineWidth = thread lines.lineCapStyle = .round lines.lineJoinStyle = .round for (from, to) in threads { lines.move(to: at(points[from])) lines.line(to: at(points[to])) } lines.stroke() NSColor.black.setFill() for point in points where point.isKnot { let centre = at(point) NSBezierPath(ovalIn: NSRect(x: centre.x - knot, y: centre.y - knot, width: knot * 2, height: knot * 2)).fill() } }}