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: one width for the buttons and dropdowns in setting rows
v0lumehi60f11a836cb4
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swiftindex 7160e6da..b4beadf5 100644--- a/frontend/Sources/NSPortBuilder/ContentView.swift+++ b/frontend/Sources/NSPortBuilder/ContentView.swift@@ -3061,8 +3061,8 @@ struct BuilderView: View {}/// One line of a card: a status dot (its place kept when there is none, so every title starts on one line),- /// the title (with a disclosure chevron beside it when `open` is given), a secondary detail, and its controls,- /// whose right edges line up across the card.+ /// the title (with a disclosure chevron beside it when `open` is given), a secondary detail, and its controls.+ /// Its buttons and dropdowns are all LauncherControl.width wide, so they line up in one column across the cards.private func row<Trailing: View>(_ title: String, detail: String = "", dot: Color? = nil, help: String = "", open: Bool? = nil,@ViewBuilder trailing: () -> Trailing) -> some View {HStack(spacing: 12) {@@ -3088,7 +3088,7 @@ struct BuilderView: View {}}Spacer(minLength: 12)- trailing()+ trailing().launcherRowControls()}.padding(.horizontal, 18).padding(.vertical, 13).frame(minHeight: 52)@@ -3167,6 +3167,7 @@ struct BuilderView: View {Text(m.t(m.gameDir.isEmpty ? "data.choose" : "data.change")).font(.manrope(13, .semibold)).foregroundStyle(.black).padding(.horizontal, 16).padding(.vertical, 8)+ .launcherRowControlFrame().background(LauncherPalette.cream, in: Capsule())}.contentShape(Rectangle())@@ -3422,14 +3423,9 @@ struct BuilderView: View {// ---- Settings ----- /// Every button of the Settings cards is this wide, so they stand in one column.- static let settingsButtonWidth: CGFloat = 150- /// The language picker is wider: "Systemsprache" does not fit the button width.- static let settingsPickerWidth: CGFloat = 180-@ViewBuilder private var settingsPage: some View {pageHeading(m.t("settings.heading"), note: m.t("settings.note"))- settingsCards.buttonStyle(LauncherSecondaryButtonStyle(minWidth: Self.settingsButtonWidth))+ settingsCards}@ViewBuilder private var settingsCards: some View {@@ -3473,11 +3469,11 @@ struct BuilderView: View {row(m.t("settings.language"),detail: Lang.saved.map { m.t("settings.language.\($0.rawValue)") }?? m.t("settings.language.system"), dot: LauncherPalette.ready, help: "") {- // The launcher's own list, drawn like the buttons beside it and as wide as its label needs.+ // The launcher's own list, drawn like the buttons beside it and as wide as they are.// The language names are in their own language (see "settings.language.*"), only "system" follows the UI.LauncherDropdown(options: Self.languageCodes.map { (value: $0, label: m.t("settings.language." + ($0.isEmpty ? "system" : $0))) },selection: Lang.saved.map { $0.rawValue } ?? "", label: m.t("settings.language"),- style: .button, minWidth: Self.settingsPickerWidth) { v in+ style: .button) { v inLang.saved = v.isEmpty ? nil : Lang(rawValue: v)m.lang = Lang.initial()}@@ -3639,7 +3635,7 @@ struct BuilderView: View {}}divider- row(m.t("diag.title"), detail: m.t("diag.note")) { DiagnosticsButton(m: m, game: library.selected, width: Self.settingsButtonWidth) }+ row(m.t("diag.title"), detail: m.t("diag.note")) { DiagnosticsButton(m: m, game: library.selected) }}}diff --git a/frontend/Sources/NSPortBuilder/Diagnostics.swift b/frontend/Sources/NSPortBuilder/Diagnostics.swiftindex d4927395..7794fec9 100644--- a/frontend/Sources/NSPortBuilder/Diagnostics.swift+++ b/frontend/Sources/NSPortBuilder/Diagnostics.swift@@ -325,7 +325,6 @@ extension BuildModel {struct DiagnosticsButton: View {@ObservedObject var m: BuildModelvar game: LibraryGame? // the library's selected game: its log goes in the report- let width: CGFloatvar showCopied = false // the visual QA's confirmation@State private var busy = false@State private var copied = false@@ -334,7 +333,7 @@ struct DiagnosticsButton: View {Button(action: copy) {if copied || showCopied { Label(m.t("diag.copied"), systemImage: "checkmark") } else { Text(m.t("diag.copy")) }}- .buttonStyle(LauncherSecondaryButtonStyle(accent: copied || showCopied ? LauncherPalette.ready : nil, minWidth: width))+ .buttonStyle(LauncherSecondaryButtonStyle(accent: copied || showCopied ? LauncherPalette.ready : nil)).disabled(busy).padTarget("set.diagnostics", scope: "main") { if !busy { copy() } }}diff --git a/frontend/Sources/NSPortBuilder/LaunchOptionsView.swift b/frontend/Sources/NSPortBuilder/LaunchOptionsView.swiftindex 9346ee48..6d83bc4d 100644--- a/frontend/Sources/NSPortBuilder/LaunchOptionsView.swift+++ b/frontend/Sources/NSPortBuilder/LaunchOptionsView.swift@@ -177,7 +177,7 @@ struct LaunchOptionsView: View {}}Spacer(minLength: 12)- control(s, value, pixels: pixels).frame(width: LaunchControl.width, alignment: .trailing)+ control(s, value, pixels: pixels).launcherRowControls().frame(width: LaunchControl.width, alignment: .trailing)}.padding(.horizontal, 18).padding(.vertical, 13).frame(minHeight: 52)@@ -201,7 +201,8 @@ struct LaunchOptionsView: View {}}- /// The setting's control, filling the column every row shares (a toggle keeps its size at the column's right edge).+ /// The setting's control in the column every row shares: a segmented choice or a slider fills it, a dropdown is LauncherControl.width wide+ /// at its right edge, and a toggle keeps its size there.@ViewBuilder private func control(_ s: LaunchSetting, _ value: String, pixels: String?) -> some View {HStack(spacing: 10) {switch s.kind {@@ -210,7 +211,7 @@ struct LaunchOptionsView: View {if LaunchControl.isSegmented(env: s.env, labels: options.map(\.label)) {LauncherSegmented(options: options, selection: value) { set(s, $0) }} else {- LauncherDropdown(options: options, selection: value, label: s.label, fill: true) { set(s, $0) }+ LauncherDropdown(options: options, selection: value, label: s.label) { set(s, $0) }}case "range":let lo = s.min ?? 0, hi = s.max ?? 1diff --git a/frontend/Sources/NSPortBuilder/LauncherDropdown.swift b/frontend/Sources/NSPortBuilder/LauncherDropdown.swiftindex dd90c00c..a6e64a96 100644--- a/frontend/Sources/NSPortBuilder/LauncherDropdown.swift+++ b/frontend/Sources/NSPortBuilder/LauncherDropdown.swift@@ -215,7 +215,8 @@ struct LauncherDropdownList: View {/// The launcher's dropdown: a button with the current choice that opens a list drawn in the app's own style/// (its dark surface, Manrope, a checkmark on the chosen row). It never cuts the label short unless it is given-/// `fill` (the width of its container). With `minWidth` it is at least that wide and grows with its label.+/// `fill` (the width of its container). Inside a setting row it is exactly LauncherControl.width wide, its choice at+/// the left and the chevron at the right, the choice a little smaller when it does not fit./// `label` names the setting for VoiceOver, which then reads the choice as the value.struct LauncherDropdown: View {let options: [(value: String, label: String)]@@ -223,7 +224,6 @@ struct LauncherDropdown: View {var label = ""var style: LauncherDropdownStyle = .pillvar fill = false- var minWidth: CGFloat?let choose: (String) -> Void@Environment(\.launcherDropdownHost) private var host@State private var id = UUID()@@ -238,20 +238,21 @@ struct LauncherDropdown: View {fileprivate var current: String { options.first { $0.value == selection }?.label ?? selection }- fileprivate func face(open: Bool) -> some View {+ fileprivate func face(open: Bool, rowWidth: CGFloat?) -> some View {let rounded = style == .buttonlet shape = RoundedRectangle(cornerRadius: rounded ? 9 : 99)+ let width = fill ? nil : rowWidthreturn HStack(spacing: 8) {Text(current).font(.manrope(rounded ? 13 : 12.5, .semibold)).lineLimit(1)- .fixedSize(horizontal: !fill, vertical: false)- if fill || minWidth != nil { Spacer(minLength: 0) }+ .minimumScaleFactor(width == nil ? 1 : 0.85)+ .fixedSize(horizontal: !fill && width == nil, vertical: false)+ if fill || width != nil { Spacer(minLength: 0) }Image(systemName: "chevron.down").font(.system(size: 9, weight: .bold)).foregroundStyle(.secondary).rotationEffect(.degrees(open ? 180 : 0))}.padding(.horizontal, rounded ? 15 : 14).padding(.vertical, rounded ? 9 : 7)- .frame(minWidth: minWidth)+ .frame(width: width).frame(maxWidth: fill ? .infinity : nil)- .fixedSize(horizontal: !fill && minWidth != nil, vertical: false).background(Color.white.opacity(open ? 0.13 : (rounded ? 0.055 : 0.06)), in: shape).overlay(shape.strokeBorder(Color.white.opacity(open ? 0.18 : 0.10))).foregroundStyle(Color.white.opacity(0.92))@@ -266,6 +267,7 @@ struct LauncherDropdown: View {@ObservedObject var host: LauncherDropdownStatelet dropdown: LauncherDropdown@Environment(\.isEnabled) private var enabled+ @Environment(\.launcherControlWidth) private var rowWidthvar body: some View {let open = host.request?.id == dropdown.iddropdown.accessible(@@ -274,7 +276,7 @@ struct LauncherDropdown: View {host.request = open ? nil : LauncherDropdownRequest(id: dropdown.id, options: dropdown.options, selection: dropdown.selection,label: dropdown.label, choose: dropdown.choose)}- } label: { dropdown.face(open: open) }+ } label: { dropdown.face(open: open, rowWidth: rowWidth) }.buttonStyle(.plain).opacity(enabled ? 1 : 0.55).anchorPreference(key: LauncherDropdownAnchorKey.self, value: .bounds) { [dropdown.id: $0] }@@ -287,9 +289,10 @@ struct LauncherDropdown: View {let dropdown: LauncherDropdown@State private var open = false@Environment(\.isEnabled) private var enabled+ @Environment(\.launcherControlWidth) private var rowWidthvar body: some View {dropdown.accessible(- Button { open.toggle() } label: { dropdown.face(open: open) }+ Button { open.toggle() } label: { dropdown.face(open: open, rowWidth: rowWidth) }.buttonStyle(.plain).opacity(enabled ? 1 : 0.55).popover(isPresented: $open, arrowEdge: .bottom) {diff --git a/frontend/Sources/NSPortBuilder/LauncherStyles.swift b/frontend/Sources/NSPortBuilder/LauncherStyles.swiftindex 5d47c32e..dd456606 100644--- a/frontend/Sources/NSPortBuilder/LauncherStyles.swift+++ b/frontend/Sources/NSPortBuilder/LauncherStyles.swift@@ -62,18 +62,54 @@ struct TitlebarDoubleClick: NSViewRepresentable {func updateNSView(_ nsView: NSView, context: Context) {}}+/// The width of the button or dropdown at the trailing end of a setting row, the same on every page, so that these+/// controls stand in one column whatever their labels. A row hands it to its trailing controls (`launcherRowControls()`),+/// and LauncherSecondaryButtonStyle and LauncherDropdown take it from there. Outside a row they size to their label.+enum LauncherControl {+ /// Wide enough for the longest everyday label in every language on one line ("Sök efter uppdateringar", 148 pt in 13 pt+ /// Manrope semibold, plus the buttons' 15 pt of padding on each side). LauncherControlWidthTests measures them all.+ static let width: CGFloat = 184+ /// What the label of a button may take.+ static let buttonLabelWidth: CGFloat = width - 2 * 15+}++private struct LauncherControlWidthKey: EnvironmentKey { static let defaultValue: CGFloat? = nil }++extension EnvironmentValues {+ /// Set inside a setting row: every button and dropdown there is exactly this wide.+ var launcherControlWidth: CGFloat? {+ get { self[LauncherControlWidthKey.self] }+ set { self[LauncherControlWidthKey.self] = newValue }+ }+}++extension View {+ /// The trailing controls of a setting row: buttons and dropdowns take LauncherControl.width.+ func launcherRowControls() -> some View { environment(\.launcherControlWidth, LauncherControl.width) }++ /// This view as wide as a button of a setting row (for a control drawn by hand, like the cream capsule).+ func launcherRowControlFrame() -> some View { modifier(LauncherRowControlFrame()) }+}++private struct LauncherRowControlFrame: ViewModifier {+ @Environment(\.launcherControlWidth) private var width+ func body(content: Content) -> some View { content.frame(width: width) }+}+struct LauncherSecondaryButtonStyle: ButtonStyle {/// Text and border in this colour (the build bar's Stop); plain white otherwise.var accent: Color? = nil- /// At least this wide, the label centred: the buttons of a card line up in one column (Settings).- var minWidth: CGFloat? = nil+ /// Inside a setting row the button is exactly LauncherControl.width wide, its label centred and wrapped to a second+ /// line when it does not fit: the buttons of a page line up in one column.+ @Environment(\.launcherControlWidth) private var width@Environment(\.isEnabled) private var enabledfunc makeBody(configuration: Configuration) -> some View {configuration.label.font(.manrope(13, .semibold))- .lineLimit(1)+ .multilineTextAlignment(.center)+ .lineLimit(width == nil ? 1 : 2).padding(.horizontal, 15).padding(.vertical, 9)- .frame(minWidth: minWidth)+ .frame(width: width).background(Color.white.opacity(configuration.isPressed ? 0.13 : 0.055), in: RoundedRectangle(cornerRadius: 9)).overlay(RoundedRectangle(cornerRadius: 9).strokeBorder(accent?.opacity(0.75) ?? Color.white.opacity(0.10))).foregroundStyle(enabled ? (accent ?? Color.white.opacity(0.92)) : .gray)diff --git a/frontend/Sources/NSPortBuilder/Typography.swift b/frontend/Sources/NSPortBuilder/Typography.swiftindex 6ef0b7b8..cc478564 100644--- a/frontend/Sources/NSPortBuilder/Typography.swift+++ b/frontend/Sources/NSPortBuilder/Typography.swift@@ -24,12 +24,19 @@ enum Typography {}}- static func font(_ face: CTFontDescriptor?, size: CGFloat, weight: Font.Weight, fallback: Font) -> Font {- guard let face else { return fallback }+ private static func ctFont(_ face: CTFontDescriptor?, size: CGFloat, weight: Font.Weight) -> CTFont? {+ guard let face else { return nil }let variation = CTFontDescriptorCreateCopyWithAttributes(face, [kCTFontVariationAttribute: [0x7767_6874: axis(weight)]] as CFDictionary)- return Font(CTFontCreateWithFontDescriptor(variation, size, nil))+ return CTFontCreateWithFontDescriptor(variation, size, nil)}+ static func font(_ face: CTFontDescriptor?, size: CGFloat, weight: Font.Weight, fallback: Font) -> Font {+ ctFont(face, size: size, weight: weight).map { Font($0) } ?? fallback+ }++ /// Manrope as a CTFont, to measure how wide a label is set (nil when the face cannot be read).+ static func manropeFont(size: CGFloat, weight: Font.Weight) -> CTFont? { ctFont(manrope, size: size, weight: weight) }+static func text(_ size: CGFloat, _ weight: Font.Weight) -> Font {font(manrope, size: size, weight: weight, fallback: .system(size: size, weight: weight))}diff --git a/frontend/Tests/NSPortBuilderTests/LauncherControlWidthTests.swift b/frontend/Tests/NSPortBuilderTests/LauncherControlWidthTests.swiftnew file mode 100644index 00000000..7cd8fe62--- /dev/null+++ b/frontend/Tests/NSPortBuilderTests/LauncherControlWidthTests.swift@@ -0,0 +1,59 @@+import CoreText+import XCTest+@testable import NSPortBuilder++/// The buttons and dropdowns in the setting rows share one width (LauncherControl.width). These measure the labels the+/// launcher ships in every language against it, in the face and size the controls use.+final class LauncherControlWidthTests: XCTestCase {+ private let languages: [Lang] = [.zh, .en, .sv, .de]++ /// The labels of the buttons at the end of a setting row.+ private let buttonKeys = [+ "library.rescan", "library.chooseFolder", "controls.customize", "console.controllersOpen", "settings.setupBtn",+ "app.restartNow", "repo.check", "repo.pull", "repo.adopt", "repo.choose", "repo.download", "dep.recheck", "dep.install",+ "dep.openAppStore", "dep.xcode.fix", "build.setup", "data.choose", "data.change", "install.reveal", "build.showLibrary",+ "diag.copy", "diag.copied",+ ]++ /// Longer than one line in some language: they wrap to a second line, which the button has room for.+ private let wrapping: Set<String> = ["repo.adopt"]++ /// The names of the dropdown choices in the Settings cards.+ private let choiceKeys = [+ "settings.language.system", "settings.language.zh", "settings.language.en", "settings.language.sv", "settings.language.de",+ ]++ private func width(_ text: String) throws -> CGFloat {+ let font = try XCTUnwrap(Typography.manropeFont(size: 13, weight: .semibold))+ let line = CTLineCreateWithAttributedString(NSAttributedString(string: text, attributes: [.font: font]))+ return CTLineGetTypographicBounds(line, nil, nil, nil)+ }++ func testButtonLabelsFitOnOneLine() throws {+ for key in buttonKeys where !wrapping.contains(key) {+ for lang in languages {+ let text = L10n.string(key, lang)+ XCTAssertLessThanOrEqual(try width(text), LauncherControl.buttonLabelWidth, "\(lang) \(key): \(text)")+ }+ }+ }++ func testTheLongestLabelsFitOnTwoLines() throws {+ for key in buttonKeys {+ for lang in languages {+ let text = L10n.string(key, lang)+ XCTAssertLessThanOrEqual(try width(text), 2 * LauncherControl.buttonLabelWidth - 40, "\(lang) \(key): \(text)")+ }+ }+ }++ func testDropdownChoicesFitBesideTheChevron() throws {+ let chevron: CGFloat = 8 + 10+ for key in choiceKeys {+ for lang in languages {+ let text = L10n.string(key, lang)+ XCTAssertLessThanOrEqual(try width(text), LauncherControl.buttonLabelWidth - chevron, "\(lang) \(key): \(text)")+ }+ }+ }+}diff --git a/frontend/Tests/NSPortBuilderTests/LauncherDropdownTests.swift b/frontend/Tests/NSPortBuilderTests/LauncherDropdownTests.swiftindex 6c635ee9..7bba0ed8 100644--- a/frontend/Tests/NSPortBuilderTests/LauncherDropdownTests.swift+++ b/frontend/Tests/NSPortBuilderTests/LauncherDropdownTests.swift@@ -84,7 +84,8 @@ final class LauncherDropdownTests: XCTestCase {Text(L10n.string("settings.language.system", .de)).font(.manrope(12.5)).foregroundStyle(.secondary)}Spacer()- LauncherDropdown(options: options, selection: "", label: "Sprache", style: .button, minWidth: 150) { _ in }+ LauncherDropdown(options: options, selection: "", label: "Sprache", style: .button) { _ in }+ .launcherRowControls()}.padding(.horizontal, 18).padding(.vertical, 13).background(LauncherPalette.surface.opacity(0.75), in: RoundedRectangle(cornerRadius: 14))diff --git a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift b/frontend/Tests/NSPortBuilderTests/LibraryTests.swiftindex 0feca325..ade8f35f 100644--- a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift+++ b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift@@ -1127,9 +1127,9 @@ final class LibraryTests: XCTestCase {do {try capture("settings-diagnostics", height: 1500, page: .settings)let buttons = NSHostingView(rootView: HStack(spacing: 14) {- DiagnosticsButton(m: model, width: BuilderView.settingsButtonWidth)- DiagnosticsButton(m: model, width: BuilderView.settingsButtonWidth, showCopied: true)- }.padding(24).background(LauncherPalette.background).environmentObject(PadFocus()))+ DiagnosticsButton(m: model)+ DiagnosticsButton(m: model, showCopied: true)+ }.launcherRowControls().padding(24).background(LauncherPalette.background).environmentObject(PadFocus()))buttons.frame = NSRect(x: 0, y: 0, width: 420, height: 90)let window = NSWindow(contentRect: buttons.frame, styleMask: [.titled], backing: .buffered, defer: false)window.contentView = buttons