Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import AppKit
@MainActorfinal class UpdateMenuCard: NSView { private let label = NSTextField(wrappingLabelWithString: "") private let detail = NSTextField( wrappingLabelWithString: MenuText.text("The update is set to install when Parcel quits.")) private let installButton = NSButton(title: MenuText.text("Install and Relaunch"), target: nil, action: nil) private let laterButton = NSButton(title: MenuText.text("Later"), target: nil, action: nil) private var install: @MainActor () -> Void private var later: @MainActor () -> Void
init(version: String?, install: @escaping @MainActor () -> Void, later: @escaping @MainActor () -> Void) { self.install = install self.later = later super.init(frame: CGRect(x: 0, y: 0, width: 310, height: 66)) label.font = .menuFont(ofSize: 0) detail.font = .menuFont(ofSize: 0) detail.textColor = .secondaryLabelColor installButton.target = self installButton.action = #selector(installUpdate) laterButton.target = self laterButton.action = #selector(deferUpdate) addSubview(label) addSubview(detail) addSubview(installButton) addSubview(laterButton) update(version: version, install: install, later: later) }
required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") }
static func title(version: String?) -> String { version.map { String(format: MenuText.text("Update %@ ready"), locale: .current, $0) } ?? MenuText.text("Update ready — version unavailable") }
func update(version: String?, install: @escaping @MainActor () -> Void, later: @escaping @MainActor () -> Void) { label.stringValue = Self.title(version: version) self.install = install self.later = later resizeToFit() }
func update(from other: UpdateMenuCard) { label.stringValue = other.label.stringValue install = other.install later = other.later resizeToFit() }
private func resizeToFit() { let buttonsWidth = installButton.fittingSize.width + laterButton.fittingSize.width + 8 let width = max(310, buttonsWidth + 28, min(500, label.attributedStringValue.size().width + 28)) label.preferredMaxLayoutWidth = width - 28 detail.preferredMaxLayoutWidth = width - 28 frame.size = CGSize(width: width, height: label.fittingSize.height + detail.fittingSize.height + 47) needsLayout = true }
override func layout() { super.layout() let width = bounds.width - 28 let labelHeight = label.fittingSize.height let detailHeight = detail.fittingSize.height label.frame = CGRect(x: 14, y: bounds.height - labelHeight - 6, width: width, height: labelHeight) detail.frame = CGRect(x: 14, y: 37, width: width, height: detailHeight) let installWidth = installButton.fittingSize.width installButton.frame = CGRect(x: 14, y: 7, width: installWidth, height: 24) laterButton.frame = CGRect(x: 22 + installWidth, y: 7, width: laterButton.fittingSize.width, height: 24) }
@objc private func installUpdate() { install() } @objc private func deferUpdate() { later() }}