Skip to content

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

Console mode: a proper connect dialog, controller chips in the top bar row

v0lumehid6981a5858b3
diff --git a/frontend/Sources/NSPortBuilder/ConsoleControllersView.swift b/frontend/Sources/NSPortBuilder/ConsoleControllersView.swift
index 20b6df0d..3f0d3bb1 100644
--- a/frontend/Sources/NSPortBuilder/ConsoleControllersView.swift
+++ b/frontend/Sources/NSPortBuilder/ConsoleControllersView.swift
@@ -113,7 +113,7 @@ struct ConsoleControllerChips: View {
chip(id: controller.id, label: controller.name + ", " + t(controller.kind.labelKey) + ", " + ((merged ? 1 : controller.player).map { String(format: t("console.player"), $0) } ?? t("console.unassigned")) + (controller.batteryPercent.map { ", " + String(format: t(controller.charging ? "console.batteryCharging" : "console.batteryLevel"), $0) } ?? "")) {
HStack(spacing: compact ? 5 : 8) {
Image(systemName: controller.kind.symbol).accessibilityHidden(true)
- Text((merged ? 1 : controller.player).map { String(format: t("console.playerShort"), $0) } ?? t("console.unassigned"))
+ Text((merged ? 1 : controller.player).map { String(format: t("console.playerShort"), $0) } ?? "\u{2014}") // the full "Unassigned" is in the Controllers list
ConsoleBattery(controller: controller, lang: lang, compact: compact)
}
}
@@ -142,6 +142,57 @@ struct ConsoleControllerChips: View {
}
}
+/// The controller strip of the desktop top bar: one row, never clipped. When the bar is short of room it drops the battery and
+/// shows the first controllers followed by a "+N" chip. Everything opens the Controllers sheet.
+struct DesktopControllerChips: View {
+ let controllers: [ConsoleController]
+ let merged: Bool
+ let lang: Lang
+ let barWidth: CGFloat
+ let compactBar: Bool
+ let openControllers: () -> Void
+ @EnvironmentObject private var pad: PadFocus
+
+ /// How many chips fit in a top bar of this width, and whether each can show its battery. The rest of the bar (logo, tabs, entry
+ /// button, search, build button) takes about 900 points with labelled tabs and 640 with icons.
+ static func layout(count: Int, barWidth: CGFloat, compactBar: Bool) -> (visible: Int, showsBattery: Bool) {
+ let available = barWidth - (compactBar ? 640 : 900)
+ let perChip: CGFloat = compactBar ? 62 : 122
+ let fit = max(1, Int(available / perChip))
+ guard count > fit else { return (count, !compactBar) }
+ return (max(1, fit - 1), false)
+ }
+
+ private func t(_ key: String) -> String { L10n.string(key, lang) }
+ var body: some View {
+ let layout = Self.layout(count: controllers.count, barWidth: barWidth, compactBar: compactBar)
+ HStack(spacing: 6) {
+ ForEach(controllers.prefix(layout.visible)) { controller in
+ let player = merged ? 1 : controller.player
+ chip(id: controller.id, label: controller.name + ", " + t(controller.kind.labelKey) + ", " + (player.map { String(format: t("console.player"), $0) } ?? t("console.unassigned"))) {
+ HStack(spacing: 5) {
+ Image(systemName: controller.kind.symbol).accessibilityHidden(true)
+ Text(player.map { String(format: t("console.playerShort"), $0) } ?? "\u{2014}")
+ if layout.showsBattery { ConsoleBattery(controller: controller, lang: lang, compact: true) }
+ }
+ }
+ }
+ if controllers.count > layout.visible {
+ chip(id: "more", label: t("console.controllers")) { Text("+\(controllers.count - layout.visible)") }
+ }
+ }
+ .fixedSize()
+ }
+ private func chip<C: View>(id: String, label: String, @ViewBuilder content: () -> C) -> some View {
+ Button(action: openControllers) {
+ content().font(.manrope(12, .semibold)).padding(.horizontal, 10).frame(height: 30)
+ .background(.white.opacity(0.075), in: Capsule()).contentShape(Capsule())
+ }
+ .buttonStyle(.plain).accessibilityLabel(label).accessibilityHint(t("console.controllers"))
+ .padTarget("main.chip." + id, scope: "main", radius: 24, activate: openControllers)
+ }
+}
+
struct ConsoleToastView: View {
let toast: ConsoleToast
var body: some View {
diff --git a/frontend/Sources/NSPortBuilder/ConsoleEntryView.swift b/frontend/Sources/NSPortBuilder/ConsoleEntryView.swift
index c8956733..0e4571a2 100644
--- a/frontend/Sources/NSPortBuilder/ConsoleEntryView.swift
+++ b/frontend/Sources/NSPortBuilder/ConsoleEntryView.swift
@@ -19,31 +19,52 @@ struct ConsoleEntryButton: View {
}
}
+/// The cream button of a dialog: the launcher's secondary button's size and radius, filled.
+struct LauncherPrimaryButtonStyle: ButtonStyle {
+ func makeBody(configuration: Configuration) -> some View {
+ configuration.label
+ .font(.manrope(13, .bold)).lineLimit(1)
+ .padding(.horizontal, 15).padding(.vertical, 9)
+ .background(LauncherPalette.cream.opacity(configuration.isPressed ? 0.8 : 1), in: RoundedRectangle(cornerRadius: 9))
+ .foregroundStyle(LauncherPalette.background)
+ .contentShape(RoundedRectangle(cornerRadius: 9))
+ }
+}
+
struct ConsoleConnectionPrompt: View {
let lang: Lang
let enterConsole: () -> Void
let dismissPrompt: () -> Void
@EnvironmentObject private var pad: PadFocus
+ @AppStorage(ConsolePrompt.defaultsKey) private var promptOn = true // the same switch as Settings
private func t(_ key: String) -> String { L10n.string(key, lang) }
+ private var glyphs: ConsoleGlyphs { .current() }
var body: some View {
ZStack {
Color.black.opacity(0.65)
- VStack(alignment: .leading, spacing: 20) {
- Label(t("console.connectionTitle"), systemImage: "gamecontroller").font(.manrope(26, .bold))
- Text(t("console.connectionNote")).font(.manrope(18)).foregroundStyle(.secondary)
- VStack(alignment: .leading, spacing: 16) {
- Button(t("console.later"), action: dismissPrompt).buttonStyle(LauncherSecondaryButtonStyle())
- .keyboardShortcut(.cancelAction)
- .padTarget("console.entry.later", scope: "console.entry", activate: dismissPrompt)
+ VStack(alignment: .leading, spacing: 14) {
+ Label(t("console.connectionTitle"), systemImage: "gamecontroller").font(.manrope(22, .bold))
+ Text(t("console.connectionNote")).font(.manrope(14)).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
+ // one row, macOS order: the way out on the left of the main choice, both of the launcher's standard size
+ HStack(spacing: 10) {
+ Toggle(t("console.promptNever"), isOn: Binding(get: { !promptOn }, set: { promptOn = !$0 }))
+ .toggleStyle(.checkbox).font(.manrope(12)).foregroundStyle(.secondary)
+ .padTarget("console.entry.never", scope: "console.entry", radius: 6, activate: { promptOn.toggle() })
+ Spacer(minLength: 12)
+ Button(action: dismissPrompt) {
+ HStack(spacing: 7) { ConsoleGlyphView(glyph: glyphs.glyph(for: .back), size: 15); Text(t("console.later")) }
+ }
+ .buttonStyle(LauncherSecondaryButtonStyle()).keyboardShortcut(.cancelAction)
+ .padTarget("console.entry.later", scope: "console.entry", activate: dismissPrompt)
Button(action: enterConsole) {
- Text(t("console.entry")).font(.manrope(17, .bold)).foregroundStyle(LauncherPalette.background)
- .padding(.horizontal, 24).padding(.vertical, 14).background(LauncherPalette.cream, in: Capsule())
+ HStack(spacing: 7) { ConsoleGlyphView(glyph: glyphs.glyph(for: .activate), size: 15).foregroundStyle(LauncherPalette.background); Text(t("console.entry")) }
}
- .buttonStyle(.plain).keyboardShortcut(.defaultAction)
- .padTarget("console.entry.enter", scope: "console.entry", radius: 30, activate: enterConsole)
+ .buttonStyle(LauncherPrimaryButtonStyle()).keyboardShortcut(.defaultAction)
+ .padTarget("console.entry.enter", scope: "console.entry", activate: enterConsole)
}
+ .padding(.top, 6)
}
- .padding(36).frame(maxWidth: 530).background(LauncherPalette.surface, in: RoundedRectangle(cornerRadius: 22))
+ .padding(26).frame(maxWidth: 560).background(LauncherPalette.surface, in: RoundedRectangle(cornerRadius: 22))
.overlay(RoundedRectangle(cornerRadius: 22).strokeBorder(.white.opacity(0.10)))
.padding(24)
}
diff --git a/frontend/Sources/NSPortBuilder/LauncherView.swift b/frontend/Sources/NSPortBuilder/LauncherView.swift
index 031e3724..310bdacd 100644
--- a/frontend/Sources/NSPortBuilder/LauncherView.swift
+++ b/frontend/Sources/NSPortBuilder/LauncherView.swift
@@ -275,6 +275,10 @@ struct ContentView: View {
.padTarget("tab." + destination.rawValue, scope: "main") { page = destination }
}
Spacer(minLength: 8)
+ if showsControllers {
+ DesktopControllerChips(controllers: consoleDriver.state.controllers, merged: consoleDriver.state.padMerge, lang: m.lang,
+ barWidth: geometry.size.width, compactBar: compact) { controllerSheet = true }
+ }
if let enterConsole { ConsoleEntryButton(lang: m.lang, compact: compact, enterConsole: enterConsole) }
searchField
Button { newBuild() } label: {
@@ -294,16 +298,9 @@ struct ContentView: View {
.padding(.leading, 90).padding(.trailing, 24)
.frame(height: 64)
.background(TitlebarDoubleClick()) // an empty part of the bar acts as the title bar does
- if showsControllers { HStack {
- Spacer(minLength: 0)
- ConsoleControllerChips(controllers: consoleDriver.state.controllers, merged: consoleDriver.state.padMerge, lang: m.lang,
- compact: true, scope: "main") { controllerSheet = true }
- .frame(width: min(geometry.size.width - 114, max(120, CGFloat(consoleDriver.state.controllers.count) * 116)))
- }
- .padding(.leading, 90).padding(.trailing, 24) }
}
}
- .frame(height: showsControllers ? 104 : 64)
+ .frame(height: 64)
}
diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swift
index 28b8800e..f9e5df64 100644
--- a/frontend/Sources/NSPortBuilder/Localization.swift
+++ b/frontend/Sources/NSPortBuilder/Localization.swift
@@ -153,6 +153,7 @@ enum L10n {
"console.promptSetting": ("\u{63d2}\u{5165}\u{624b}\u{67c4}\u{65f6}\u{63d0}\u{793a}\u{8fdb}\u{5165}\u{4e3b}\u{673a}\u{6a21}\u{5f0f}", "Offer console mode when a controller connects"),
"console.promptSettingNote": ("\u{6bcf}\u{6b21}\u{542f}\u{52a8}\u{6700}\u{591a}\u{63d0}\u{793a}\u{4e00}\u{6b21}\u{3002}", "At most once each time the launcher starts."),
"console.menuLeave": ("\u{9000}\u{51fa}\u{4e3b}\u{673a}\u{6a21}\u{5f0f}", "Leave Console Mode"),
+ "console.promptNever": ("\u{4e0d}\u{518d}\u{63d0}\u{793a}", "Don\u{2019}t ask again"),
"console.mergeEach": ("\u{6bcf}\u{4e2a}\u{624b}\u{67c4}\u{5bf9}\u{5e94}\u{4e00}\u{4f4d}\u{73a9}\u{5bb6}", "Each controller is its own player"),
"console.mockPress": ("\u{6a21}\u{62df}\u{6309}\u{4e0b}\u{6309}\u{94ae}", "Simulate a button press"),
"console.connectedUnassigned": ("%@ \u{5df2}\u{8fde}\u{63a5}", "%@ connected"),
diff --git a/frontend/Tests/NSPortBuilderTests/ConsolePadTests.swift b/frontend/Tests/NSPortBuilderTests/ConsolePadTests.swift
index 9dd552e1..23c11ca8 100644
--- a/frontend/Tests/NSPortBuilderTests/ConsolePadTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/ConsolePadTests.swift
@@ -195,13 +195,31 @@ final class ConsolePadTests: XCTestCase {
try XCTUnwrap(rep.representation(using: .png, properties: [:])).write(to: output.appendingPathComponent(name + ".png"))
window.orderOut(nil); window.contentView = nil; window.close()
}
- let driver = MockConsoleDriver(scenario: .controllersMixed, lang: .en, animated: false)
- driver.back()
+ ConsolePrompt.didShow() // the desktop shots must not show the prompt a connected controller would raise
let library = GameLibrary(file: output.appendingPathComponent("unused-library.json"))
- for width: CGFloat in [1280, 900] {
- try shot(AnyView(ContentView(model: BuildModel(), library: library, performStartupChecks: false, enterConsole: {}, controllerDriver: AnyConsoleDriver(driver))),
- "desktop-entry-\(Int(width))", width, 560)
+ for (name, scenario) in [("1", MockConsoleDriver.Scenario.full), ("3", .controllersMixed)] {
+ let driver = MockConsoleDriver(scenario: scenario, lang: .en, animated: false)
+ driver.back()
+ for width: CGFloat in [1280, 900] {
+ try shot(AnyView(ContentView(model: BuildModel(), library: library, performStartupChecks: false, enterConsole: {}, controllerDriver: AnyConsoleDriver(driver))),
+ "desktop-\(name)c-\(Int(width))", width, 720)
+ }
+ }
+ for lang in [Lang.en, .zh] {
+ try shot(AnyView(ConsoleConnectionPrompt(lang: lang, enterConsole: {}, dismissPrompt: {}).environmentObject(PadFocus()).preferredColorScheme(.dark)), "prompt-\(lang.rawValue)", 900, 500)
}
- try shot(AnyView(ConsoleConnectionPrompt(lang: .en, enterConsole: {}, dismissPrompt: {}).environmentObject(PadFocus()).preferredColorScheme(.dark)), "prompt-1280", 1280, 720)
+ }
+
+ func testChipStripFitsTheBar() {
+ // plenty of room: every chip with its battery
+ XCTAssertEqual(DesktopControllerChips.layout(count: 3, barWidth: 1400, compactBar: false).visible, 3)
+ XCTAssertTrue(DesktopControllerChips.layout(count: 3, barWidth: 1400, compactBar: false).showsBattery)
+ // a narrow bar: no battery, and the rest folds into one "+N" chip, at least one controller stays
+ let narrow = DesktopControllerChips.layout(count: 4, barWidth: 800, compactBar: true)
+ XCTAssertFalse(narrow.showsBattery)
+ XCTAssertGreaterThanOrEqual(narrow.visible, 1)
+ XCTAssertLessThan(narrow.visible, 4)
+ XCTAssertEqual(DesktopControllerChips.layout(count: 1, barWidth: 700, compactBar: true).visible, 1)
+ XCTAssertEqual(DesktopControllerChips.layout(count: 0, barWidth: 700, compactBar: true).visible, 0)
}
}