Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import AppKitimport ParcelCore
@MainActorfinal class StackGripView: OverlayChromeView { private(set) var grip: StackGrip private(set) var buttons: [StackGripButton] = [] private let countLabel = NSTextField(labelWithString: "") private let showControls: @MainActor () -> Void override var isFlipped: Bool { true }
init( grip: StackGrip, select: @escaping @MainActor (WindowID) -> Void, showControls: @escaping @MainActor () -> Void ) { self.grip = grip self.showControls = showControls super.init(frame: CGRect(origin: .zero, size: grip.frame.size)) setAccessibilityElement(true) setAccessibilityRole(.radioGroup) for chip in grip.chips { let button = StackGripButton(chip: chip, select: select) buttons.append(button) addSubview(button) } countLabel.font = .monospacedDigitSystemFont(ofSize: NSFont.smallSystemFontSize, weight: .regular) countLabel.alignment = .center countLabel.setAccessibilityElement(false) addSubview(countLabel) setAccessibilityChildren(buttons) setAccessibilityCustomActions([ NSAccessibilityCustomAction( name: String(localized: "Show window controls", table: "Stack", bundle: .module) ) { [weak self] in guard let self else { return false } self.showControls() return true } ]) update(grip) }
func update(_ grip: StackGrip) { precondition(self.grip.windows == grip.windows) self.grip = grip frame.size = grip.frame.size withoutImplicitAnimations { layer?.cornerRadius = min(8, grip.frame.height / 2) } setAccessibilityLabel(grip.accessibilityLabel) for (button, chip) in zip(buttons, grip.chips) { button.update(chip) } for (index, button) in buttons.enumerated() { button.nextKeyView = buttons[(index + 1) % buttons.count] } countLabel.stringValue = grip.windows.count.formatted() layoutCount() refreshPalette() }
override func viewDidMoveToWindow() { super.viewDidMoveToWindow() layoutCount() }
private func layoutCount() { countLabel.sizeToFit() let measured = countLabel.frame.size let scale = window?.backingScaleFactor ?? 1 countLabel.frame = Self.countFrame(in: grip.countFrame, measured: measured, scale: scale) }
static func countFrame(in bounds: CGRect, measured: CGSize, scale: CGFloat) -> CGRect { CGRect( x: ((bounds.midX - measured.width / 2) * scale).rounded() / scale, y: ((bounds.midY - measured.height / 2) * scale).rounded() / scale, width: measured.width, height: measured.height) }
override func applyPalette(_ palette: OverlayPalette) { super.applyPalette(palette) for button in buttons { button.applyPalette(palette) } }
override func cancelOperation(_ sender: Any?) { window?.orderOut(nil) } required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") }}