Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
Launcher: the Dock tile shows the app icon with a gray progress bar
v0lumehi820add50e18e
diff --git a/frontend/Sources/NSPortBuilder/DockProgress.swift b/frontend/Sources/NSPortBuilder/DockProgress.swiftindex 50e632f8..66489e92 100644--- a/frontend/Sources/NSPortBuilder/DockProgress.swift+++ b/frontend/Sources/NSPortBuilder/DockProgress.swift@@ -1,38 +1,68 @@-import SwiftUI+import AppKit-/// The Dock icon mirrors a running build: a dark tile with the share of the work done, so a build's-/// progress is visible from wherever the launcher was left.+/// The Dock icon mirrors a running build like the App Store mirrors a download: the app icon with a+/// native gray progress bar along its bottom, so a build's progress is visible from wherever the+/// launcher was left. macOS has no public API for the App Store bar, so this is a plain+/// NSProgressIndicator on the Dock tile.enum DockProgress {- /// The text the tile shows for a progress fraction; nil clears the tile. Pure, so tests run it.+ /// The text for a progress fraction (accessibility label); nil clears the tile. Pure, so tests run it.static func percentText(_ fraction: Double?) -> String? {guard let fraction, fraction.isFinite, fraction >= 0, fraction <= 1 else { return nil }return "\(Int((fraction * 100).rounded()))%"}+ /// The tile's view: the app icon with a bar style progress indicator near its bottom edge.+ static func makeTileView(fraction: Double, icon: NSImage, size: CGFloat = 128) -> NSView {+ let tile = NSView(frame: NSRect(x: 0, y: 0, width: size, height: size))+ let iconView = NSImageView(frame: tile.bounds)+ iconView.image = icon+ iconView.imageScaling = .scaleProportionallyUpOrDown+ tile.addSubview(iconView)++ let inset = size * 0.1+ let bar = NSProgressIndicator(frame: NSRect(x: inset, y: size * 0.08, width: size - 2 * inset, height: 14))+ bar.style = .bar+ bar.isIndeterminate = false+ bar.minValue = 0+ bar.maxValue = 1+ bar.doubleValue = min(max(fraction, 0), 1)+ bar.controlSize = .regular+ bar.appearance = NSAppearance(named: .aqua)+ bar.setAccessibilityLabel(percentText(fraction))+ // The App Store bar is gray, not the accent color, and NSProgressIndicator has no tint on+ // current macOS. So the native bar is drawn to a bitmap, desaturated, and shown as an image.+ let barImage = grayImage(of: bar)+ let barView = NSImageView(frame: bar.frame)+ barView.image = barImage+ barView.imageScaling = .scaleAxesIndependently+ tile.addSubview(barView)+ return tile+ }++ /// The view drawn to an image with its colors turned to gray.+ static func grayImage(of view: NSView) -> NSImage {+ guard let rep = view.bitmapImageRepForCachingDisplay(in: view.bounds) else { return NSImage() }+ view.cacheDisplay(in: view.bounds, to: rep)+ let color = NSImage(size: view.bounds.size)+ color.addRepresentation(rep)+ guard let cg = rep.cgImage,+ let filter = CIFilter(name: "CIColorControls", parameters: [+ kCIInputImageKey: CIImage(cgImage: cg), kCIInputSaturationKey: 0.0]),+ let out = filter.outputImage,+ let gray = CIContext().createCGImage(out, from: out.extent) else { return color }+ return NSImage(cgImage: gray, size: view.bounds.size)+ }+/// Reflects the running build on the Dock tile; nil restores the plain icon.static func update(_ fraction: Double?) {let dock = NSApplication.shared.dockTile- guard let text = percentText(fraction) else {+ guard let fraction, percentText(fraction) != nil else {dock.contentView = nildock.display()return}- let done = min(max(fraction ?? 0, 0), 1)- let view = NSHostingView(rootView:- ZStack {- RoundedRectangle(cornerRadius: 18).fill(Color.black)- VStack(spacing: 6) {- Circle().trim(from: 0, to: done).stroke(Color(red: 1.0, green: 0.85, blue: 0.62),- style: StrokeStyle(lineWidth: 7, lineCap: .round))- .rotationEffect(.degrees(-90))- .frame(width: 52, height: 52)- Text(text).font(.system(size: 15, weight: .bold, design: .rounded)).monospacedDigit()- .foregroundStyle(.white)- }- }- .preferredColorScheme(.dark)- )- dock.contentView = view+ dock.contentView = makeTileView(fraction: fraction, icon: NSApplication.shared.applicationIconImage,+ size: dock.size.width)dock.display()}}diff --git a/frontend/Tests/NSPortBuilderTests/DockProgressTests.swift b/frontend/Tests/NSPortBuilderTests/DockProgressTests.swiftindex 251b0746..3fd7a018 100644--- a/frontend/Tests/NSPortBuilderTests/DockProgressTests.swift+++ b/frontend/Tests/NSPortBuilderTests/DockProgressTests.swift@@ -1,8 +1,7 @@import XCTest@testable import NSPortBuilder-/// The Dock tile's text for a running build (DockProgress): the visible part is-/// pure, so it is tested; the AppKit half (drawing the tile) is not.+/// The Dock tile's text for a running build (DockProgress): the text is pure, the tile view is built without a Dock.final class DockProgressTests: XCTestCase {func testThePercentTextForAProgressFraction() {XCTAssertEqual(DockProgress.percentText(0), "0%")@@ -16,4 +15,13 @@ final class DockProgressTests: XCTestCase {XCTAssertNil(DockProgress.percentText(1.5))XCTAssertNil(DockProgress.percentText(.nan))}++ @MainActor+ func testTheTileHoldsTheIconAndAGrayBar() {+ let icon = NSImage(size: NSSize(width: 64, height: 64))+ let tile = DockProgress.makeTileView(fraction: 0.5, icon: icon, size: 128)+ XCTAssertEqual(tile.frame.size, NSSize(width: 128, height: 128))+ XCTAssertEqual(tile.subviews.count, 2, "the icon and the bar")+ XCTAssertNotNil((tile.subviews.last as? NSImageView)?.image)+ }}