Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Launcher: the Dock mirrors a running build, and destructive actions ask for the name

LNPANDY0432138000dd
diff --git a/frontend-qt/nspb_qt/localization.py b/frontend-qt/nspb_qt/localization.py
index 4595a6ff..8e622f05 100644
--- a/frontend-qt/nspb_qt/localization.py
+++ b/frontend-qt/nspb_qt/localization.py
@@ -254,6 +254,8 @@ TABLE: dict[str, tuple[str, str, str, str]] = {
'saves.askToMac': ('用 %@ 的存档替换这台 Mac 上的存档?', 'Replace the save on this Mac with the one from %@?', 'Ersätt sparfilen på den här Mac-datorn med den från %@?', 'Spielstand auf diesem Mac durch den von %@ ersetzen?'),
'saves.askNote': ('请先在 %1$@ 上关闭游戏:正在运行的游戏之后会用自己的存档覆盖复制过去的存档。被替换的存档会先保存到 %2$@。', 'Close the game on %1$@ first: a running game writes its own save over the copy later. The save being replaced is kept in %2$@ first.', 'Stäng först spelet på %1$@: ett spel som körs skriver senare över kopian med sin egen sparfil. Sparfilen som ersätts sparas först i %2$@.', 'Beende das Spiel zuerst auf %1$@. Ein laufendes Spiel schreibt später seinen eigenen Spielstand über die Kopie. Der zu ersetzende Spielstand wird vorher in %2$@ gesichert.'),
'saves.cancel': ('取消', 'Cancel', 'Avbryt', 'Abbrechen'),
+ 'confirm.typePrompt': ('输入「%@」以确认', 'Type “%@” to confirm', 'Type “%@” to confirm', 'Type “%@” to confirm'),
+ 'confirm.cancel': ('取消', 'Cancel', 'Cancel', 'Cancel'),
'saves.replace': ('替换', 'Replace', 'Ersätt', 'Ersetzen'),
'saves.copying': ('正在复制存档…', 'Copying the save…', 'Kopierar sparfilen …', 'Spielstand wird kopiert …'),
'saves.copied': ('已复制。', 'Copied.', 'Kopierad.', 'Kopiert.'),
diff --git a/frontend/Sources/NSPortBuilder/FreeSpace.swift b/frontend/Sources/NSPortBuilder/FreeSpace.swift
index 41bb6dcd..d2cccbc3 100644
--- a/frontend/Sources/NSPortBuilder/FreeSpace.swift
+++ b/frontend/Sources/NSPortBuilder/FreeSpace.swift
@@ -303,10 +303,12 @@ struct FreeSpaceView: View {
.preferredColorScheme(.dark)
.tint(LauncherPalette.cream)
.onAppear { if survey == nil { measure() } }
- .confirmationDialog(String(format: m.t("free.confirm"), Self.bytes(chosenBytes)), isPresented: $confirming, titleVisibility: .visible) {
- Button(m.t("free.confirmButton"), role: .destructive) { removeChosen() }
- } message: {
- Text(([m.t("free.confirmNote")] + BuildFolderPart.allCases.filter { chosen.contains($0) && !($0 == .oldShaders && chosen.contains(.shaders)) }.map { m.t("free.cost." + $0.rawValue) }).joined(separator: "\n"))
+ .typeToConfirm(
+ isPresented: $confirming, phrase: game.name,
+ title: String(format: m.t("free.confirm"), Self.bytes(chosenBytes)),
+ message: ([m.t("free.confirmNote")] + BuildFolderPart.allCases.filter { chosen.contains($0) && !($0 == .oldShaders && chosen.contains(.shaders)) }.map { m.t("free.cost." + $0.rawValue) }).joined(separator: "\n"),
+ buttonTitle: m.t("free.confirmButton")) {
+ removeChosen()
}
}
diff --git a/frontend/Sources/NSPortBuilder/LauncherView.swift b/frontend/Sources/NSPortBuilder/LauncherView.swift
index 1f6c8c17..2e2b7b1e 100644
--- a/frontend/Sources/NSPortBuilder/LauncherView.swift
+++ b/frontend/Sources/NSPortBuilder/LauncherView.swift
@@ -186,7 +186,9 @@ struct ContentView: View {
// A clone, pull or update check has finished (the automatic pull at launch among them): the checkout may have moved
.onReceive(m.$repoBusy.removeDuplicates().dropFirst()) { busy in if !busy && performStartupChecks { checkTools() } }
.onChange(of: library.selectedID) { _ in resignNote = nil } // the last re-sign's note belongs to its game
+ .onChange(of: buildProgress) { progress in if m.running { DockProgress.update(progress) } }
.onReceive(m.$running.removeDuplicates()) { running in
+ if !running { DockProgress.update(nil) }
if running && activeBuildID == nil { activeBuildID = library.rememberInputs(m) }
if running && activeBuildID != nil { // the tools this build runs: a pull waits for the build (pullRepo, autoPull)
buildCommit = ""
@@ -215,14 +217,14 @@ struct ContentView: View {
)) {
Button(m.t("library.ok")) { launchError = nil; library.error = nil }
} message: { Text(launchError ?? library.error ?? "") }
- .confirmationDialog(m.t("library.removeQuestion"), isPresented: Binding(
- get: { removing != nil }, set: { if !$0 { removing = nil } }
- ), titleVisibility: .visible) {
- Button(m.t("library.remove"), role: .destructive) {
- if let game = removing { library.remove(game.id) }
- removing = nil
- }
- } message: { Text(m.t("library.removeNote")) }
+ .typeToConfirm(
+ isPresented: Binding(get: { removing != nil }, set: { if !$0 { removing = nil } }),
+ phrase: removing?.name ?? "",
+ title: m.t("library.removeQuestion"), message: m.t("library.removeNote"),
+ buttonTitle: m.t("library.remove")) {
+ if let game = removing { library.remove(game.id) }
+ removing = nil
+ }
.sheet(isPresented: Binding(get: { optionsFor != nil }, set: { if !$0 { optionsFor = nil } })) {
if let id = optionsFor { LaunchOptionsView(m: m, library: library, gameID: id).environmentObject(padFocus) }
}
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index 67df58c3..5a0e4e8e 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -297,6 +297,8 @@ enum L10n {
"saves.askToMac": ("用 %@ 的存档替换这台 Mac 上的存档?", "Replace the save on this Mac with the one from %@?"),
"saves.askNote": ("请先在 %1$@ 上关闭游戏:正在运行的游戏之后会用自己的存档覆盖复制过去的存档。被替换的存档会先保存到 %2$@。", "Close the game on %1$@ first: a running game writes its own save over the copy later. The save being replaced is kept in %2$@ first."),
"saves.cancel": ("取消", "Cancel"),
+ "confirm.typePrompt": ("输入「%@」以确认", "Type \u{201c}%@\u{201d} to confirm"),
+ "confirm.cancel": ("取消", "Cancel"),
"saves.replace": ("替换", "Replace"),
"saves.copying": ("正在复制存档…", "Copying the save…"),
"saves.copied": ("已复制。", "Copied."),
@@ -1225,6 +1227,8 @@ enum L10n {
"saves.askToMac": "Ersätt sparfilen på den här Mac-datorn med den från %@?",
"saves.askNote": "Stäng först spelet på %1$@: ett spel som körs skriver senare över kopian med sin egen sparfil. Sparfilen som ersätts sparas först i %2$@.",
"saves.cancel": "Avbryt",
+ "confirm.typePrompt": "Skriv \u{201c}%@\u{201d} för att bekräfta",
+ "confirm.cancel": "Avbryt",
"saves.replace": "Ersätt",
"saves.copying": "Kopierar sparfilen …",
"saves.copied": "Kopierad.",
@@ -2029,6 +2033,8 @@ enum L10n {
"saves.askToMac": "Spielstand auf diesem Mac durch den von %@ ersetzen?",
"saves.askNote": "Beende das Spiel zuerst auf %1$@. Ein laufendes Spiel schreibt später seinen eigenen Spielstand über die Kopie. Der zu ersetzende Spielstand wird vorher in %2$@ gesichert.",
"saves.cancel": "Abbrechen",
+ "confirm.typePrompt": "Gib \u{201e}%@\u{201c} ein, um zu bestätigen",
+ "confirm.cancel": "Abbrechen",
"saves.replace": "Ersetzen",
"saves.copying": "Spielstand wird kopiert …",
"saves.copied": "Kopiert.",
diff --git a/frontend/Sources/NSPortBuilder/TypeToConfirm.swift b/frontend/Sources/NSPortBuilder/TypeToConfirm.swift
new file mode 100644
index 00000000..d9e45be7
--- /dev/null
+++ b/frontend/Sources/NSPortBuilder/TypeToConfirm.swift
@@ -0,0 +1,74 @@
+import SwiftUI
+
+/// A destructive action that asks for a phrase to be typed before it runs (NX Play's Remove Game and
+/// Free Up Space): a click-through confirmation dialog is not enough for "deletes builds and data for good".
+///
+/// The phrase is the game's name — something in front of the player, not a magic word to look up.
+struct TypeToConfirm: ViewModifier {
+ @Binding var isPresented: Bool
+ let phrase: String
+ let title: String
+ let message: String
+ let buttonTitle: String
+ let action: () -> Void
+ @State private var typed = ""
+
+ func body(content: Content) -> some View {
+ content.alert(title, isPresented: $isPresented) {
+ TextField(String(format: L10n.string("confirm.typePrompt", .initial()), phrase), text: $typed)
+ Button(buttonTitle, role: .destructive) {
+ defer { typed = "" }
+ guard typed.trimmingCharacters(in: .whitespaces) == phrase else { return }
+ action()
+ }
+ Button(L10n.string("confirm.cancel", .initial()), role: .cancel) { typed = "" }
+ } message: {
+ Text(message + "\n" + String(format: L10n.string("confirm.typePrompt", .initial()), phrase))
+ }
+ }
+}
+
+extension View {
+ func typeToConfirm(isPresented: Binding<Bool>, phrase: String, title: String, message: String,
+ buttonTitle: String, action: @escaping () -> Void) -> some View {
+ modifier(TypeToConfirm(isPresented: isPresented, phrase: phrase, title: title, message: message,
+ buttonTitle: buttonTitle, action: action))
+ }
+}
+
+/// 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.
+enum DockProgress {
+ /// The text the tile shows for a progress fraction; 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()))%"
+ }
+
+ /// 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 {
+ dock.contentView = nil
+ dock.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.display()
+ }
+}
diff --git a/frontend/Tests/NSPortBuilderTests/DockProgressTests.swift b/frontend/Tests/NSPortBuilderTests/DockProgressTests.swift
new file mode 100644
index 00000000..77d3ca71
--- /dev/null
+++ b/frontend/Tests/NSPortBuilderTests/DockProgressTests.swift
@@ -0,0 +1,19 @@
+import XCTest
+@testable import NSPortBuilder
+
+/// The Dock tile's text for a running build (DockProgress in TypeToConfirm.swift): the visible part is
+/// pure, so it is tested; the AppKit half (drawing the tile) is not.
+final class DockProgressTests: XCTestCase {
+ func testThePercentTextForAProgressFraction() {
+ XCTAssertEqual(DockProgress.percentText(0), "0%")
+ XCTAssertEqual(DockProgress.percentText(0.404), "40%", "rounded, never 40.4%")
+ XCTAssertEqual(DockProgress.percentText(1), "100%")
+ }
+
+ func testNilAndOddValuesClearTheTile() {
+ XCTAssertNil(DockProgress.percentText(nil), "build not running: the plain icon comes back")
+ XCTAssertNil(DockProgress.percentText(-0.2))
+ XCTAssertNil(DockProgress.percentText(1.5))
+ XCTAssertNil(DockProgress.percentText(.nan))
+ }
+}