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
Console mode hides the pointer when idle and returns to full screen after a game
v0lumehi713d488c18a1
diff --git a/frontend/Sources/NSPortBuilder/ConsoleCoordinator.swift b/frontend/Sources/NSPortBuilder/ConsoleCoordinator.swiftindex 31f582ad..42f98a0a 100644--- a/frontend/Sources/NSPortBuilder/ConsoleCoordinator.swift+++ b/frontend/Sources/NSPortBuilder/ConsoleCoordinator.swift@@ -6,9 +6,9 @@ import AppKit/// are sequenced so they do not fight: on entering, the content switches after the window has finished going full screen, and on/// leaving, the console fades out first and the window leaves full screen after. A game is started from a window that has left full/// screen: a game opened from an app in its own full screen Space can come up as a window, so the window goes back to full screen-/// once the game has quit.+/// once the game has quit. In the console the pointer hides after a few idle seconds.@MainActor final class ConsoleCoordinator: ObservableObject {- @Published private(set) var active = false+ @Published private(set) var active = false { didSet { if active != oldValue { active ? pointer.start() : pointer.stop() } } }/// The launcher is showing (not the guided setup), so the mode can be entered.@Published var available = falseprivate var enteredFullScreen = false@@ -20,6 +20,8 @@ import AppKit/// Changes the window's full screen state and calls `done` when that animation has finished.private let setFullScreen: @MainActor (NSWindow, Bool, @escaping () -> Void) -> Voidprivate let fadeSeconds: TimeInterval+ /// The pointer hides while the console is idle (ConsolePointer.swift).+ private lazy var pointer = ConsolePointer(window: window)nonisolated static let transitionSeconds: TimeInterval = 0.4@@ -91,8 +93,11 @@ import AppKit}}- private func returnToFullScreen() {- if let w = window(), !w.styleMask.contains(.fullScreen) { setFullScreen(w, true) {} }+ /// The launcher can be asked while the game's Space is still going away, and the system drops that request. A window that is+ /// still not full screen when the animation is over is asked again, up to three times.+ private func returnToFullScreen(attempts: Int = 3) {+ guard attempts > 0, active, let w = window(), !w.styleMask.contains(.fullScreen) else { return }+ setFullScreen(w, true) { [weak self] in self?.returnToFullScreen(attempts: attempts - 1) }}}diff --git a/frontend/Sources/NSPortBuilder/ConsolePointer.swift b/frontend/Sources/NSPortBuilder/ConsolePointer.swiftnew file mode 100644index 00000000..a89505d3--- /dev/null+++ b/frontend/Sources/NSPortBuilder/ConsolePointer.swift@@ -0,0 +1,82 @@+import AppKit++/// When the pointer hides in console mode: after `idleSeconds` without movement, a click or a scroll while it is over the launcher+/// window, and it comes back with the next one (the game's pointer follows the same rule, patch 0032). `hidden` is the one+/// NSCursor.hide the launcher has outstanding, so hide and unhide stay balanced.+struct PointerIdle: Equatable {+ enum Step { case none, hide, show }+ var idleSeconds: TimeInterval = 3+ private(set) var hidden = false+ private var lastLocation: CGPoint?+ private var lastActivity: TimeInterval = 0++ /// One look at the pointer: its position, whether a button is down, and whether it is over the active launcher window.+ mutating func tick(now: TimeInterval, location: CGPoint, buttonDown: Bool, over: Bool) -> Step {+ if location != lastLocation || buttonDown { lastActivity = now }+ lastLocation = location+ if hidden { return lastActivity == now || !over ? show() : .none }+ if over, now - lastActivity >= idleSeconds { hidden = true; return .hide }+ return .none+ }++ /// A click or a scroll that did not move the pointer.+ mutating func activity(now: TimeInterval) -> Step {+ lastActivity = now+ return hidden ? show() : .none+ }++ /// Console mode ended: the pointer is shown if it was hidden.+ mutating func reset() -> Step { hidden ? show() : .none }++ private mutating func show() -> Step { hidden = false; return .show }+}++/// Hides the pointer over the console after a few idle seconds. It watches the pointer position on a short timer, so it needs+/// no tracking area or mouse-moved events from the window.+@MainActor final class ConsolePointer {+ private var idle = PointerIdle()+ private var timer: Timer?+ private var monitor: Any?+ private let window: () -> NSWindow?+ private let hide: () -> Void+ private let show: () -> Void++ init(window: @escaping () -> NSWindow?, hide: @escaping () -> Void = { NSCursor.hide() }, show: @escaping () -> Void = { NSCursor.unhide() }) {+ self.window = window+ self.hide = hide+ self.show = show+ }++ func start() {+ guard timer == nil else { return }+ timer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] _ in+ MainActor.assumeIsolated { self?.look() }+ }+ monitor = NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown, .scrollWheel]) { [weak self] event in+ MainActor.assumeIsolated { self?.apply(self?.idle.activity(now: ProcessInfo.processInfo.systemUptime) ?? .none) }+ return event+ }+ }++ func stop() {+ timer?.invalidate()+ timer = nil+ if let monitor { NSEvent.removeMonitor(monitor) }+ monitor = nil+ apply(idle.reset())+ }++ private func look() {+ let location = NSEvent.mouseLocation+ let over = (NSApp?.isActive ?? false) && (window()?.frame.contains(location) ?? false)+ apply(idle.tick(now: ProcessInfo.processInfo.systemUptime, location: location, buttonDown: NSEvent.pressedMouseButtons != 0, over: over))+ }++ private func apply(_ step: PointerIdle.Step) {+ switch step {+ case .hide: hide()+ case .show: show()+ case .none: break+ }+ }+}diff --git a/frontend/Tests/NSPortBuilderTests/ConsoleLiveTests.swift b/frontend/Tests/NSPortBuilderTests/ConsoleLiveTests.swiftindex 25144709..1e66eb4c 100644--- a/frontend/Tests/NSPortBuilderTests/ConsoleLiveTests.swift+++ b/frontend/Tests/NSPortBuilderTests/ConsoleLiveTests.swift@@ -212,6 +212,30 @@ import SwiftUIXCTAssertEqual(calls, [false, true, false])}+ func testFullScreenIsAskedAgainWhenTheSystemDroppedTheFirstRequest() throws {+ final class FakeWindow: NSWindow {+ var isFull = true+ override var styleMask: NSWindow.StyleMask {+ get { isFull ? super.styleMask.union(.fullScreen) : super.styleMask }+ set { super.styleMask = newValue.subtracting(.fullScreen) }+ }+ }+ var calls: [Bool] = []+ var dropped = 2+ let window = FakeWindow(contentRect: .zero, styleMask: [.titled], backing: .buffered, defer: true)+ let coordinator = ConsoleCoordinator(window: { window }, setFullScreen: { _, on, done in+ calls.append(on)+ if on, dropped > 0 { dropped -= 1 } else { window.isFull = on }+ done()+ }, appIsActive: { true })+ coordinator.available = true+ coordinator.enter()+ coordinator.leaveFullScreenForGame {}+ coordinator.resumeFullScreenAfterGame()+ XCTAssertEqual(calls, [false, true, true, true], "two requests were dropped, the third one worked")+ XCTAssertTrue(window.isFull)+ }+func testPlayWaitsForTheLaunchHookAndReportsTheEnd() async throws {var launches: [Launch] = []let (driver, _, _) = try makeDriver { launches.append($0) }diff --git a/frontend/Tests/NSPortBuilderTests/ConsolePointerTests.swift b/frontend/Tests/NSPortBuilderTests/ConsolePointerTests.swiftnew file mode 100644index 00000000..117b90dd--- /dev/null+++ b/frontend/Tests/NSPortBuilderTests/ConsolePointerTests.swift@@ -0,0 +1,54 @@+import XCTest+@testable import NSPortBuilder++/// When the pointer hides in console mode. Nothing here touches NSCursor.+final class ConsolePointerTests: XCTestCase {+ private let here = CGPoint(x: 10, y: 10)++ func testHidesAfterThreeIdleSecondsAndShowsOnMovement() {+ var idle = PointerIdle()+ XCTAssertEqual(idle.tick(now: 0, location: here, buttonDown: false, over: true), .none)+ XCTAssertEqual(idle.tick(now: 2.9, location: here, buttonDown: false, over: true), .none)+ XCTAssertEqual(idle.tick(now: 3, location: here, buttonDown: false, over: true), .hide)+ XCTAssertEqual(idle.tick(now: 4, location: here, buttonDown: false, over: true), .none, "hidden once")+ XCTAssertEqual(idle.tick(now: 4.25, location: CGPoint(x: 11, y: 10), buttonDown: false, over: true), .show)+ XCTAssertEqual(idle.tick(now: 5, location: CGPoint(x: 11, y: 10), buttonDown: false, over: true), .none, "the idle time starts again")+ XCTAssertEqual(idle.tick(now: 7.25, location: CGPoint(x: 11, y: 10), buttonDown: false, over: true), .hide)+ }++ func testAClickOrAScrollShowsItAndRestartsTheWait() {+ var idle = PointerIdle()+ _ = idle.tick(now: 0, location: here, buttonDown: false, over: true)+ XCTAssertEqual(idle.tick(now: 3, location: here, buttonDown: false, over: true), .hide)+ XCTAssertEqual(idle.activity(now: 4), .show)+ XCTAssertEqual(idle.activity(now: 4.1), .none, "nothing to show")+ XCTAssertEqual(idle.tick(now: 6, location: here, buttonDown: false, over: true), .none)+ XCTAssertEqual(idle.tick(now: 7.1, location: here, buttonDown: false, over: true), .hide)+ XCTAssertEqual(idle.tick(now: 7.25, location: here, buttonDown: true, over: true), .show, "a held button is activity")+ }++ func testStaysVisibleOutsideTheWindowAndComesBackWhenItLeaves() {+ var idle = PointerIdle()+ _ = idle.tick(now: 0, location: here, buttonDown: false, over: false)+ XCTAssertEqual(idle.tick(now: 10, location: here, buttonDown: false, over: false), .none, "not over the launcher: left alone")+ XCTAssertEqual(idle.tick(now: 11, location: here, buttonDown: false, over: true), .hide)+ XCTAssertEqual(idle.tick(now: 12, location: here, buttonDown: false, over: false), .show, "the launcher lost the pointer or the focus")+ }++ func testEndingTheConsoleShowsAHiddenPointerOnce() {+ var idle = PointerIdle()+ XCTAssertEqual(idle.reset(), .none)+ _ = idle.tick(now: 0, location: here, buttonDown: false, over: true)+ _ = idle.tick(now: 3, location: here, buttonDown: false, over: true)+ XCTAssertEqual(idle.reset(), .show)+ XCTAssertEqual(idle.reset(), .none)+ }++ @MainActor func testStoppingUnhidesWhatWasHidden() {+ var balance = 0+ let pointer = ConsolePointer(window: { nil }, hide: { balance += 1 }, show: { balance -= 1 })+ pointer.start()+ pointer.stop()+ XCTAssertEqual(balance, 0)+ }+}