Skip to content

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

Keep the first tile's focus ring inside the console row after scrolling back

v0lumehi577ccadd4bf6
diff --git a/frontend/Sources/NSPortBuilder/ConsoleView.swift b/frontend/Sources/NSPortBuilder/ConsoleView.swift
index 8ace0d07..f5548596 100644
--- a/frontend/Sources/NSPortBuilder/ConsoleView.swift
+++ b/frontend/Sources/NSPortBuilder/ConsoleView.swift
@@ -188,7 +188,7 @@ enum ConsoleInput {
let width: CGFloat = compact ? size.width - inset * 2 - 16 : min(470, size.width * 0.32)
return ScrollViewReader { proxy in
ScrollView(.horizontal, showsIndicators: false) {
- HStack(alignment: .top, spacing: compact ? 22 : 28) {
+ HStack(alignment: .top, spacing: (compact ? 22 : 28) - 16) {
ForEach(state.games) { game in
let selected = state.focusedGame?.id == game.id
Button { driver.focus(game.id); driver.play(game.id) } label: {
@@ -205,9 +205,13 @@ enum ConsoleInput {
.accessibilityLabel(game.title + ", " + t(game.status.labelKey))
.accessibilityHint(t("console.playHint"))
.accessibilityAddTraits(selected ? .isSelected : [])
+ // every tile carries its own 8 pt of room for the ring and the focus scale, and the scroll target is this padded
+ // cell, so a scroll to the first or last tile never pushes that room out of the clip
+ .padding(.horizontal, 8)
+ .id(Self.rowCell(game.id))
}
}
- .padding(.horizontal, 8).padding(.vertical, 12) // room for the focused tile's small scale inside the clip
+ .padding(.vertical, 12)
}
.coordinateSpace(name: "console.row")
.onPreferenceChange(ConsoleTileFramesKey.self) { tileFrames = $0 }
@@ -216,16 +220,17 @@ enum ConsoleInput {
// The ring moves across the visible tiles. The row scrolls only when the focused tile would leave the visible area, by
// the least that brings it inside, and a new focus replaces the running scroll (held repeats do not queue).
guard let id else { return }
- let tile = "console.game." + id
- guard let frame = tileFrames[id] else { proxy.scrollTo(tile, anchor: .center); return }
+ guard let frame = tileFrames[id] else { proxy.scrollTo(Self.rowCell(id), anchor: .center); return }
let rowWidth = size.width - 2 * (inset - 8)
let anchor: UnitPoint? = frame.minX < 8 ? .leading : (frame.maxX > rowWidth - 8 ? .trailing : nil)
- if let anchor { withAnimation(.easeOut(duration: reduceMotion ? 0 : 0.2)) { proxy.scrollTo(tile, anchor: anchor) } }
+ if let anchor { withAnimation(.easeOut(duration: reduceMotion ? 0 : 0.2)) { proxy.scrollTo(Self.rowCell(id), anchor: anchor) } }
}
- .onAppear { if let id = state.focusedGame?.id { proxy.scrollTo("console.game." + id, anchor: .center) } }
+ .onAppear { if let id = state.focusedGame?.id { proxy.scrollTo(Self.rowCell(id), anchor: .center) } }
}
}
+ private static func rowCell(_ id: String) -> String { "console.row." + id }
+
private func hintBar(compact: Bool, width: CGFloat) -> some View {
let items = {
Group {
diff --git a/frontend/Tests/NSPortBuilderTests/ConsoleTests.swift b/frontend/Tests/NSPortBuilderTests/ConsoleTests.swift
index d0f9165d..e3100d66 100644
--- a/frontend/Tests/NSPortBuilderTests/ConsoleTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/ConsoleTests.swift
@@ -265,4 +265,53 @@ final class ConsoleTests: XCTestCase {
}
}
}
+
+ /// The focus ring of the first tile must survive a scroll to the far end and back. Scrolling to a tile's own edge used to push the
+ /// 8 pt of room for the ring out of the row's clip, so the ring was cut at the left. The ring is cream, so the test looks for a
+ /// cream column just left of the first tile's image. Set NX_CONSOLE_CAPTURE_DIR to keep the PNGs.
+ @MainActor func testFirstTileRingIsNotClippedAfterScrollingBack() throws {
+ NSApplication.shared.setActivationPolicy(.accessory)
+ let (width, height): (CGFloat, CGFloat) = (1280, 800)
+ let driver = MockConsoleDriver(scenario: .full, lang: .en, animated: false)
+ let host = NSHostingView(rootView: ConsoleView(driver: driver, lang: .en).transaction { $0.disablesAnimations = true }.frame(width: width, height: height))
+ let window = NSWindow(contentRect: NSRect(x: -10000, y: 0, width: width, height: height), styleMask: [.borderless], backing: .buffered, defer: false)
+ window.isReleasedWhenClosed = false
+ window.contentView = host
+ window.orderBack(nil)
+ defer { window.orderOut(nil); window.contentView = nil; window.close() }
+ func settle() { RunLoop.main.run(until: Date().addingTimeInterval(0.4)); host.layoutSubtreeIfNeeded() }
+ func snapshot(_ name: String) throws -> NSBitmapImageRep {
+ let rep = try XCTUnwrap(host.bitmapImageRepForCachingDisplay(in: host.bounds))
+ host.cacheDisplay(in: host.bounds, to: rep)
+ if let directory = ProcessInfo.processInfo.environment["NX_CONSOLE_CAPTURE_DIR"] {
+ let output = URL(fileURLWithPath: directory)
+ try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true)
+ try XCTUnwrap(rep.representation(using: .png, properties: [:])).write(to: output.appendingPathComponent(name + ".png"))
+ }
+ return rep
+ }
+ settle()
+ let first = try XCTUnwrap(driver.state.games.first).id, last = try XCTUnwrap(driver.state.games.last).id
+ let start = try snapshot("clip-1-start")
+ driver.focus(last); settle()
+ _ = try snapshot("clip-2-last")
+ driver.focus(first); settle()
+ let rep = try snapshot("clip-3-back")
+ // the ring is a vertical cream line along the image's left edge: the leftmost column with a long run of cream pixels
+ func ringColumn(_ rep: NSBitmapImageRep) -> Int? {
+ func isCream(_ x: Int, _ y: Int) -> Bool {
+ guard let c = rep.colorAt(x: x, y: y)?.usingColorSpace(.sRGB) else { return false }
+ return c.redComponent > 0.8 && c.greenComponent > 0.75 && c.blueComponent > 0.6 && c.alphaComponent > 0.9
+ }
+ let scale = CGFloat(rep.pixelsWide) / width
+ for x in 0..<Int(300 * scale) {
+ var run = 0
+ for y in 0..<rep.pixelsHigh { run = isCream(x, y) ? run + 1 : 0; if run >= Int(150 * scale) { return x } }
+ }
+ return nil
+ }
+ let before = try XCTUnwrap(ringColumn(start), "no focus ring found at the start")
+ let after = try XCTUnwrap(ringColumn(rep), "no focus ring found on the first tile after scrolling back")
+ XCTAssertEqual(after, before, "the first tile's ring is cut off after scrolling back (its left edge moved)")
+ }
}