Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250import AppKitimport ParcelConfigimport ParcelCoreimport QuartzCore
@MainActorfinal class HoverTabButton: NSButton { enum Style { case named, grip } private(set) var tab: HoverWindowTab let style: Style var select: ((WindowID) -> Void)? var dragBegan: ((WindowID, CGPoint) -> Void)? var dragChanged: ((CGPoint) -> Void)? var dragEnded: ((CGPoint) -> Void)? var dragCancelled: (() -> Void)? var dismiss: (() -> Void)? var navigate: ((Bool) -> Void)? var isPointerTracking: Bool { downPoint != nil && !cancelled } private var downPoint: CGPoint? private var dragging = false private var cancelled = false private let line = CALayer() private var hovered = false private var stacked = false private var configuration = MotionConfiguration() private let paletteSource: OverlayPaletteSource private var paletteObservation: UUID?
init(tab: HoverWindowTab, style: Style, paletteSource: OverlayPaletteSource = .shared) { self.tab = tab self.style = style self.paletteSource = paletteSource super.init(frame: .zero) if style == .named { cell = CenteredHoverTabCell(textCell: "") } wantsLayer = true layer?.addSublayer(line) bezelStyle = .inline isBordered = false font = .systemFont(ofSize: 11, weight: .medium) imagePosition = .imageLeading alignment = .natural imageScaling = .scaleProportionallyDown lineBreakMode = .byTruncatingTail target = self action = #selector(pressed) setAccessibilityRole(.radioButton) update(tab: tab) if style == .grip { paletteObservation = paletteSource.observe { [weak self] in self?.renderGrip() } } }
required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } override var needsPanelToBecomeKey: Bool { false } override var acceptsFirstResponder: Bool { isEnabled && !isHidden } override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
func update(tab: HoverWindowTab) { self.tab = tab state = tab.selected ? .on : .off title = style == .named ? tab.name : "" if style == .named { let paragraph = NSMutableParagraphStyle() paragraph.lineBreakMode = .byTruncatingTail attributedTitle = NSAttributedString( string: tab.name, attributes: [ .font: NSFont.systemFont(ofSize: 11, weight: .medium), .foregroundColor: NSColor.labelColor, .paragraphStyle: paragraph, ]) let icon = tab.application.icon ?? NSImage(systemSymbolName: "macwindow", accessibilityDescription: nil) image = icon?.copy() as? NSImage image?.size = NSSize(width: 16, height: 16) isBordered = tab.selected } toolTip = tab.accessibilityLabel setAccessibilityLabel(tab.accessibilityLabel) setAccessibilityValue(tab.selected ? 1 : 0) setAccessibilityIdentifier("hover.tab.\(tab.window.rawValue)") needsDisplay = true needsLayout = true }
func updateGrip(hovered: Bool, configuration: MotionConfiguration, stacked: Bool = false) { self.hovered = hovered self.configuration = configuration self.stacked = stacked renderGrip() }
override func layout() { super.layout() renderGrip() }
override func viewDidChangeEffectiveAppearance() { super.viewDidChangeEffectiveAppearance() renderGrip() }
private func renderGrip() { guard style == .grip else { line.isHidden = true; return } let accessibility = OverlayAccessibility.system let motion = OverlayMotion(configuration: configuration, accessibility: accessibility) let targetSize = SelectionGripMetrics.size( selected: tab.selected, hovered: hovered, increaseContrast: accessibility.increaseContrast, available: bounds.size) let height = targetSize.height let previous = line.presentation()?.bounds.size ?? line.bounds.size let oldOpacity = line.presentation()?.opacity ?? line.opacity let opacity: Float = stacked ? (tab.selected ? 1 : accessibility.increaseContrast ? 0.85 : 0.65) : hovered ? 1 : accessibility.increaseContrast ? 0.65 : 0.28 withoutImplicitAnimations { line.isHidden = false line.bounds = CGRect(origin: .zero, size: targetSize) line.position = CGPoint(x: bounds.midX, y: bounds.midY) line.cornerRadius = height / 2 if stacked { line.backgroundColor = paletteSource.resolve( for: effectiveAppearance, accessibility: accessibility ).stackAccent } else { effectiveAppearance.performAsCurrentDrawingAppearance { line.backgroundColor = NSColor.labelColor.cgColor } } line.opacity = opacity line.contentsScale = window?.backingScaleFactor ?? 2 } guard previous != targetSize || oldOpacity != opacity else { return } line.removeAllAnimations() guard motion.behavior != .instant else { return } if motion.behavior == .animate, previous.width > 0, previous != targetSize { let spec = motion.spec(.pop) let spring = CASpringAnimation(keyPath: "bounds.size") spring.fromValue = NSValue(size: previous) spring.toValue = NSValue(size: targetSize) spring.mass = spec.spring.mass spring.stiffness = spec.spring.stiffness spring.damping = spec.spring.damping spring.duration = spec.duration line.add(spring, forKey: "grip-size") } let fade = CABasicAnimation(keyPath: "opacity") fade.fromValue = oldOpacity fade.toValue = opacity fade.duration = motion.spec(.fade).duration line.add(fade, forKey: "grip-opacity") }
@objc private func pressed() { select?(tab.window) }
override func mouseDown(with event: NSEvent) { guard let point = screenPoint(event) else { return } downPoint = point dragging = false cancelled = false highlight(true) }
override func mouseDragged(with event: NSEvent) { guard !cancelled, let downPoint, let point = screenPoint(event) else { return } if !dragging, hypot(point.x - downPoint.x, point.y - downPoint.y) >= 4 { dragging = true highlight(false) dragBegan?(tab.window, downPoint) } if dragging { dragChanged?(point) } }
override func mouseUp(with event: NSEvent) { guard downPoint != nil else { return } defer { downPoint = nil; dragging = false; highlight(false) } guard !cancelled else { return } if dragging { if let point = screenPoint(event) { dragEnded?(point) } else { dragCancelled?() } } else if bounds.contains(convert(event.locationInWindow, from: nil)) { pressed() } }
func cancelPointerDrag() { guard downPoint != nil, !cancelled else { return } cancelled = true highlight(false) if dragging { dragCancelled?() } }
override func cancelOperation(_ sender: Any?) { if isPointerTracking { cancelPointerDrag() } else { dismiss?() } }
override func keyDown(with event: NSEvent) { switch event.charactersIgnoringModifiers { case "\u{1B}": cancelOperation(nil) case "\u{F702}": navigate?(false) case "\u{F703}": navigate?(true) case "\r", " ": pressed() default: super.keyDown(with: event) } }
private func screenPoint(_ event: NSEvent) -> CGPoint? { guard let window else { return nil } let point = window.convertPoint(toScreen: event.locationInWindow) return CGPoint(x: point.x, y: (NSScreen.screens.first?.frame.maxY ?? 0) - point.y) }
isolated deinit { if let paletteObservation { paletteSource.removeObserver(paletteObservation) } }}
private final class CenteredHoverTabCell: NSButtonCell { private func contentFrames(in bounds: CGRect) -> (image: CGRect, title: CGRect) { let available = max(0, bounds.width - 12) let iconWidth = image == nil ? 0 : min(16, available) let text = attributedTitle.size() let spacing: CGFloat = iconWidth > 0 && text.width > 0 ? 5 : 0 let textWidth = min(ceil(text.width), max(0, available - iconWidth - spacing)) let gap = textWidth > 0 ? spacing : 0 let left = bounds.midX - (iconWidth + gap + textWidth) / 2 let rtl = controlView?.userInterfaceLayoutDirection == .rightToLeft let iconHeight = min(16, bounds.height) let textHeight = min(ceil(text.height), bounds.height) return ( CGRect( x: rtl ? left + textWidth + gap : left, y: bounds.midY - iconHeight / 2, width: iconWidth, height: iconHeight), CGRect( x: rtl ? left : left + iconWidth + gap, y: bounds.midY - textHeight / 2, width: textWidth, height: textHeight) ) }
override func imageRect(forBounds bounds: CGRect) -> CGRect { contentFrames(in: bounds).image } override func titleRect(forBounds bounds: CGRect) -> CGRect { contentFrames(in: bounds).title }
override func drawInterior(withFrame cellFrame: CGRect, in controlView: NSView) { let frames = contentFrames(in: controlView.bounds) image?.draw( in: frames.image, from: .zero, operation: .sourceOver, fraction: isEnabled ? 1 : 0.5, respectFlipped: true, hints: nil) attributedTitle.draw(in: frames.title) }}