Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import AppKit
@MainActorfinal class MenuArrangementView: NSView { private var snapshot: LayoutSnapshot private var icons: [UInt64: NSImage] = [:]
init(snapshot: LayoutSnapshot) { self.snapshot = snapshot super.init(frame: .zero) setAccessibilityElement(true) setAccessibilityRole(.image) update(snapshot: snapshot) }
required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") }
override var isFlipped: Bool { true }
func update(snapshot: LayoutSnapshot) { self.snapshot = snapshot icons = Dictionary( uniqueKeysWithValues: snapshot.tiles.compactMap { tile in tile.application?.icon.map { (tile.id, $0) } }) let names = snapshot.tiles.compactMap { $0.application?.name } setAccessibilityLabel(([snapshot.accessibilityDescription] + names).joined(separator: ". ")) needsDisplay = true }
override func draw(_ dirtyRect: NSRect) { let canvas = bounds.insetBy(dx: 1, dy: 1) NSColor.controlBackgroundColor.setFill() let outline = NSBezierPath(roundedRect: canvas, xRadius: 8, yRadius: 8) outline.fill() NSColor.separatorColor.setStroke() outline.stroke() guard snapshot.bounds.width > 0, snapshot.bounds.height > 0 else { return } let scale = min((canvas.width - 12) / snapshot.bounds.width, (canvas.height - 12) / snapshot.bounds.height) let origin = CGPoint( x: canvas.midX - snapshot.bounds.width * scale / 2, y: canvas.midY - snapshot.bounds.height * scale / 2) for tile in snapshot.tiles { let clipped = tile.frame.intersection(snapshot.bounds) guard !clipped.isNull, clipped.width > 0, clipped.height > 0 else { continue } let rect = CGRect( x: origin.x + (clipped.minX - snapshot.bounds.minX) * scale, y: origin.y + (clipped.minY - snapshot.bounds.minY) * scale, width: clipped.width * scale, height: clipped.height * scale ).insetBy(dx: 2, dy: 2) guard rect.width > 0, rect.height > 0 else { continue } let path = NSBezierPath(roundedRect: rect, xRadius: 4, yRadius: 4) (tile.isFocused ? NSColor.controlAccentColor : NSColor.quaternaryLabelColor).setFill() path.fill() NSColor.labelColor.setStroke() path.lineWidth = tile.id == snapshot.primaryWindow ? 2 : (tile.isFocused ? 1.5 : 0.5) path.stroke() drawApplication(tile, in: rect) } }
private func drawApplication(_ tile: LayoutTile, in rect: CGRect) { guard let application = tile.application else { return } let content = rect.insetBy(dx: 4, dy: 4) guard content.width >= 16, content.height >= 26 else { return } let font = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize, weight: .medium) let labelHeight = ceil(font.ascender - font.descender + font.leading) let icon = icons[tile.id] let iconSize = icon == nil ? 0 : min(24, content.width, max(0, content.height - labelHeight - 4)) let spacing: CGFloat = icon == nil ? 0 : 4 let startY = content.midY - (iconSize + spacing + labelHeight) / 2 if let icon { icon.draw( in: CGRect(x: content.midX - iconSize / 2, y: startY, width: iconSize, height: iconSize), from: .zero, operation: .sourceOver, fraction: 1, respectFlipped: true, hints: nil) } let paragraph = NSMutableParagraphStyle() paragraph.alignment = .center paragraph.lineBreakMode = .byTruncatingTail let attributes: [NSAttributedString.Key: Any] = [ .font: font, .foregroundColor: tile.isFocused ? NSColor.selectedControlTextColor : NSColor.labelColor, .paragraphStyle: paragraph, ] (application.name as NSString).draw( in: CGRect( x: content.minX, y: startY + iconSize + spacing, width: content.width, height: labelHeight), withAttributes: attributes) }}