Open source CapCut alternative.
TypeScript
fix(app): keep the update menu open with an in-place spinner on Check for Updates [rebuild]
David Le9b2c69871fd1
diff --git a/apps/Donkey/Sources/Donkey/DonkeyStatusItemController.swift b/apps/Donkey/Sources/Donkey/DonkeyStatusItemController.swiftindex 19a77e84..b10d46f3 100644--- a/apps/Donkey/Sources/Donkey/DonkeyStatusItemController.swift+++ b/apps/Donkey/Sources/Donkey/DonkeyStatusItemController.swift@@ -15,8 +15,16 @@ final class DonkeyStatusItemController: NSObject, NSMenuDelegate {private let checkForUpdates: () -> Voidprivate let installUpdate: () -> Void- /// Latest update-checker state; drives the update section of the menu.- var updateState: UserQueryUpdateState?+ /// Latest update-checker state; drives the update section of the menu. Setting it while the menu+ /// is open refreshes the update row in place, so a user-triggered check spins and resolves without+ /// the menu having to be reopened.+ var updateState: UserQueryUpdateState? {+ didSet { refreshLiveUpdateItem() }+ }++ /// The update row of the currently-open menu, held so state changes can refresh it in place. Weak:+ /// the menu owns the view, and it drops away when the menu is next rebuilt.+ private weak var liveUpdateView: UpdateMenuItemView?init(checkForUpdates: @escaping () -> Void,@@ -63,32 +71,64 @@ final class DonkeyStatusItemController: NSObject, NSMenuDelegate {}/// The update section's single item, following the checker state: "Check for Updates" at rest,- /// a disabled progress line while checking or installing, and "Update App" once a check has an+ /// a spinning progress line while checking or installing, and "Update App" once a check has an/// update waiting — choosing it downloads, installs, quits, and relaunches automatically.+ ///+ /// The row is a custom view so a click keeps the menu open and spins in place instead of+ /// dismissing it; `liveUpdateView` holds it so later state changes refresh it live.private func updateItem() -> NSMenuItem {+ let item = NSMenuItem()+ let view = UpdateMenuItemView { [weak self] in self?.handleUpdateItemClick() }+ applyUpdatePresentation(to: view)+ item.view = view+ liveUpdateView = view+ return item+ }++ /// The title / clickability / busy triple the update row shows for the current checker state.+ private func updatePresentation() -> (title: String, isClickable: Bool, isBusy: Bool) {switch updateState?.status {case .available:let title = updateState?.latestVersion.map { "Update App (\($0))" } ?? "Update App"- return makeItem(title: title, action: #selector(installUpdateAction))+ return (title, true, false)case .checking:- return NSMenuItem(title: "Checking for Updates…", action: nil, keyEquivalent: "")+ return ("Checking for Updates…", false, true)case .installing:- return NSMenuItem(title: "Updating…", action: nil, keyEquivalent: "")+ return ("Updating…", false, true)default:- return makeItem(title: "Check for Updates", action: #selector(checkForUpdatesAction))+ return ("Check for Updates", true, false)}}- @objc private func goToAppAction(_ sender: Any?) {- NSWorkspace.shared.open(Self.appURL)+ private func applyUpdatePresentation(to view: UpdateMenuItemView) {+ let presentation = updatePresentation()+ view.configure(+ title: presentation.title,+ isClickable: presentation.isClickable,+ isBusy: presentation.isBusy+ )}- @objc private func checkForUpdatesAction(_ sender: Any?) {- checkForUpdates()+ private func refreshLiveUpdateItem() {+ guard let liveUpdateView else { return }+ applyUpdatePresentation(to: liveUpdateView)}- @objc private func installUpdateAction(_ sender: Any?) {- installUpdate()+ /// Route an update-row tap by the current state: install a waiting update, or start a check.+ /// While a check or install is running the row is non-clickable, so no tap arrives here.+ private func handleUpdateItemClick() {+ switch updateState?.status {+ case .available:+ installUpdate()+ case .checking, .installing:+ break+ default:+ checkForUpdates()+ }+ }++ @objc private func goToAppAction(_ sender: Any?) {+ NSWorkspace.shared.open(Self.appURL)}@objc private func quitAction(_ sender: Any?) {diff --git a/apps/Donkey/Sources/Donkey/UpdateMenuItemView.swift b/apps/Donkey/Sources/Donkey/UpdateMenuItemView.swiftnew file mode 100644index 00000000..e0465503--- /dev/null+++ b/apps/Donkey/Sources/Donkey/UpdateMenuItemView.swift@@ -0,0 +1,129 @@+import AppKit++/// The body of the update menu row. Unlike a standard menu item, a click here does not dismiss the+/// menu: the tap fires the handler and the row swaps to a spinner in place, so "Check for Updates"+/// can show progress while the menu stays open. The menu closes only when the user clicks elsewhere.+///+/// It draws its own native-looking highlight and title because custom-view items opt out of the+/// system's menu styling. Hover state comes from a tracking area; the trailing spinner runs for+/// in-progress states.+@MainActor+final class UpdateMenuItemView: NSView {+ private enum Metrics {+ static let height: CGFloat = 22+ static let textLeading: CGFloat = 14+ static let highlightInset: CGFloat = 5+ static let cornerRadius: CGFloat = 4+ static let spinnerSize: CGFloat = 14+ static let spinnerTrailing: CGFloat = 10+ }++ private let label = NSTextField(labelWithString: "")+ private let spinner = NSProgressIndicator()+ private let onClick: () -> Void++ private var isClickable = true+ private var isHovered = false++ init(onClick: @escaping () -> Void) {+ self.onClick = onClick+ super.init(frame: NSRect(x: 0, y: 0, width: 220, height: Metrics.height))+ // Let the enclosing menu stretch the row to its content width so the highlight spans it.+ autoresizingMask = .width++ label.font = NSFont.menuFont(ofSize: 0)+ label.lineBreakMode = .byTruncatingTail+ label.translatesAutoresizingMaskIntoConstraints = false+ addSubview(label)++ spinner.style = .spinning+ spinner.controlSize = .small+ spinner.isDisplayedWhenStopped = false+ // Animate off the main thread so the spinner keeps turning during menu event tracking.+ spinner.usesThreadedAnimation = true+ spinner.translatesAutoresizingMaskIntoConstraints = false+ addSubview(spinner)++ NSLayoutConstraint.activate([+ heightAnchor.constraint(equalToConstant: Metrics.height),+ label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: Metrics.textLeading),+ label.centerYAnchor.constraint(equalTo: centerYAnchor),+ spinner.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -Metrics.spinnerTrailing),+ spinner.centerYAnchor.constraint(equalTo: centerYAnchor),+ spinner.widthAnchor.constraint(equalToConstant: Metrics.spinnerSize),+ spinner.heightAnchor.constraint(equalToConstant: Metrics.spinnerSize),+ ])+ }++ @available(*, unavailable)+ required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }++ /// Point the row at the current update state: its title, whether a tap does anything, and whether+ /// the spinner runs. Safe to call while the menu is open — the row refreshes in place.+ func configure(title: String, isClickable: Bool, isBusy: Bool) {+ label.stringValue = title+ self.isClickable = isClickable+ if isBusy {+ spinner.startAnimation(nil)+ } else {+ spinner.stopAnimation(nil)+ }+ refreshAppearance()+ }++ private func refreshAppearance() {+ let highlighted = isHovered && isClickable+ if highlighted {+ label.textColor = .selectedMenuItemTextColor+ } else if isClickable {+ label.textColor = .labelColor+ } else {+ label.textColor = .disabledControlTextColor+ }+ needsDisplay = true+ }++ override func draw(_ dirtyRect: NSRect) {+ guard isHovered, isClickable else { return }+ let rect = bounds.insetBy(dx: Metrics.highlightInset, dy: 0)+ let path = NSBezierPath(+ roundedRect: rect,+ xRadius: Metrics.cornerRadius,+ yRadius: Metrics.cornerRadius+ )+ NSColor.selectedContentBackgroundColor.setFill()+ path.fill()+ }++ // MARK: - Hover and click++ override func updateTrackingAreas() {+ super.updateTrackingAreas()+ trackingAreas.forEach(removeTrackingArea)+ addTrackingArea(NSTrackingArea(+ rect: bounds,+ options: [.mouseEnteredAndExited, .activeAlways, .inVisibleRect],+ owner: self+ ))+ }++ override func mouseEntered(with event: NSEvent) {+ setHovered(true)+ }++ override func mouseExited(with event: NSEvent) {+ setHovered(false)+ }++ private func setHovered(_ hovered: Bool) {+ guard hovered != isHovered else { return }+ isHovered = hovered+ refreshAppearance()+ }++ override func mouseUp(with event: NSEvent) {+ let point = convert(event.locationInWindow, from: nil)+ guard isClickable, bounds.contains(point) else { return }+ onClick()+ }+}