From 077a5865e8fdd38f6bddc5555c304257e28ca50b Mon Sep 17 00:00:00 2001 From: Anderson Shindy Oki Date: Sun, 11 Oct 2026 23:25:38 +0900 Subject: [PATCH 01/22] feat: enable controllers per stream in the controller panel --- Model/Stream/ControllerSlotAssignments.swift | 8 +- Model/Stream/ControllerStreamSelection.swift | 155 ++++++++++++++++ OPN/Stream/ControllerMappingDevices.swift | 20 +- OPN/Stream/NativeGamepadMonitor+Mapping.swift | 1 - .../NativeGamepadMonitor+Ordering.swift | 29 ++- ...NativeGamepadMonitor+StreamSelection.swift | 40 ++++ OPN/Stream/NativeGamepadMonitor.swift | 59 +++--- OPN/Stream/NativeStreamView.swift | 6 + .../ControllerOrderNavigationTests.swift | 19 ++ Tests/Settings/ControllerOrderRowsTests.swift | 64 +++++++ .../Stream/ControllerStreamRoutingTests.swift | 126 +++++++++++++ .../ControllerStreamSelectionTests.swift | 175 ++++++++++++++++++ View/Settings/ControllerOrderView.swift | 116 +++++++++--- .../NativeNVSTStreamHUD+ActionTiles.swift | 4 +- View/Stream/StreamHost.swift | 5 +- .../ControllerOrderNavigationModel.swift | 8 +- ViewModel/Settings/ControllerOrderRows.swift | 66 +++++++ ViewModel/Stream/NativeNVSTHostInput.swift | 10 + .../StreamControllerSelectionContext.swift | 27 +++ 19 files changed, 885 insertions(+), 53 deletions(-) create mode 100644 Model/Stream/ControllerStreamSelection.swift create mode 100644 OPN/Stream/NativeGamepadMonitor+StreamSelection.swift create mode 100644 Tests/Settings/ControllerOrderRowsTests.swift create mode 100644 Tests/Stream/ControllerStreamRoutingTests.swift create mode 100644 Tests/Stream/ControllerStreamSelectionTests.swift create mode 100644 ViewModel/Settings/ControllerOrderRows.swift create mode 100644 ViewModel/Stream/StreamControllerSelectionContext.swift diff --git a/Model/Stream/ControllerSlotAssignments.swift b/Model/Stream/ControllerSlotAssignments.swift index d711144d7..100475d02 100644 --- a/Model/Stream/ControllerSlotAssignments.swift +++ b/Model/Stream/ControllerSlotAssignments.swift @@ -5,10 +5,16 @@ struct ControllerSlotAssignments: Equatable, Sendable { let native: [ObjectIdentifier: Int] init(order: ControllerPlayerOrder, steamIDs: Set, nativeIDs: [InputDeviceID: ObjectIdentifier]) { + self.init(deviceIDs: order.deviceIDs, steamIDs: steamIDs, nativeIDs: nativeIDs) + } + + /// The filtered form: a stream hands in only the devices it admits, in shared order, so the + /// first four of *that* list take Player 1–4 and a disabled device leaves no gap behind it. + init(deviceIDs: [InputDeviceID], steamIDs: Set, nativeIDs: [InputDeviceID: ObjectIdentifier]) { var steam: [InputDeviceID: Int] = [:] var native: [ObjectIdentifier: Int] = [:] var slot = 0 - for id in order.deviceIDs where slot < ControllerPlayerOrder.maximumPlayers { + for id in deviceIDs where slot < ControllerPlayerOrder.maximumPlayers { if steamIDs.contains(id) { steam[id] = slot } else if let identifier = nativeIDs[id] { diff --git a/Model/Stream/ControllerStreamSelection.swift b/Model/Stream/ControllerStreamSelection.swift new file mode 100644 index 000000000..47010ff21 --- /dev/null +++ b/Model/Stream/ControllerStreamSelection.swift @@ -0,0 +1,155 @@ +import Foundation + +/// One live stream's controller admission: which connected devices this stream lets play, which of +/// them hold a Player 1–4 slot, and the session history the HUD renders beside them. +/// +/// The app-wide `ControllerMappingDevices` registry owns the *order* every stream shares; this type +/// owns only the per-stream decision layered on top of it. Two concurrent streams therefore enable +/// different subsets of one order without either one mutating the registry or an app-wide default, +/// and neither one can reorder or disconnect the other's hardware. +/// +/// Connection, enablement and effective slot are three separate facts here. A device is `known` +/// once this session has seen it, `enabled` only after an explicit admission decision, and holds a +/// slot only while it is both enabled and connected inside the first four of the shared order. +struct ControllerStreamSelection: Equatable, Sendable { + /// A device as the shared registry currently reports it. `name` is remembered for the session so + /// a row can still be labelled after the hardware is gone. + struct Device: Equatable, Sendable { + let id: InputDeviceID + let name: String + } + + /// What the HUD shows for one row. The player index is the *effective* slot, never the position + /// in the global order — the two differ as soon as anything before it is disabled. + enum RowState: Equatable, Sendable { + case active(playerIndex: Int) + case waiting + case disabled + case newlyIdentified + case disconnected + } + + struct Row: Equatable, Identifiable, Sendable { + let id: InputDeviceID + let name: String + /// The remembered choice, including for a disconnected row, where it is read-only. + let isEnabled: Bool + let state: RowState + + var playerIndex: Int? { + if case .active(let playerIndex) = state { return playerIndex } + return nil + } + } + + static let maximumPlayers = ControllerPlayerOrder.maximumPlayers + + private(set) var hasBegunSession = false + private var names: [InputDeviceID: String] = [:] + private var knownIDs: Set = [] + private var enabledIDs: Set = [] + /// Connected devices first seen after the session began, still awaiting an explicit decision. + private var newlyIdentifiedIDs: Set = [] + /// The shared order as of the last synchronization, so the rows and slots read one snapshot. + private var connectedIDs: [InputDeviceID] = [] + /// This session's history: known devices that are no longer connected, oldest name first. + private var disconnectedIDs: [InputDeviceID] = [] + + var isEmpty: Bool { knownIDs.isEmpty } + + /// Opens the session. Everything connected when the stream starts is admitted enabled and + /// remembered, which is what makes a later reconnect able to restore this stream's choice. + mutating func beginSession(devices: [Device]) { + hasBegunSession = true + names = [:] + knownIDs = [] + enabledIDs = [] + newlyIdentifiedIDs = [] + for device in devices { + names[device.id] = device.name + knownIDs.insert(device.id) + enabledIDs.insert(device.id) + } + connectedIDs = devices.map(\.id) + disconnectedIDs = [] + } + + /// Folds in the current discovery set, in shared order. + /// + /// A device this session already knows keeps its choice, whether it is connected or was + /// disconnected earlier. A device seen for the first time mid-session is admitted disabled and + /// requires an explicit enable; it is never matched to an earlier device by name or model, so a + /// changed identity becomes a new row rather than inheriting a selection. Returns the arrivals + /// so the caller can announce them. + @discardableResult + mutating func synchronize(devices: [Device]) -> [Device] { + guard hasBegunSession else { + beginSession(devices: devices) + return [] + } + var arrivals: [Device] = [] + for device in devices { + names[device.id] = device.name + guard !knownIDs.contains(device.id) else { continue } + knownIDs.insert(device.id) + enabledIDs.remove(device.id) + newlyIdentifiedIDs.insert(device.id) + arrivals.append(device) + } + connectedIDs = devices.map(\.id) + let connected = Set(connectedIDs) + disconnectedIDs = knownIDs.subtracting(connected).sorted { $0.rawValue < $1.rawValue } + return arrivals + } + + /// Enabling clears the new-device prompt; disabling keeps the row and the remembered choice, so + /// the same device can be re-enabled without a reconnect. Returns whether anything changed. + @discardableResult + mutating func setEnabled(_ enabled: Bool, for id: InputDeviceID) -> Bool { + guard knownIDs.contains(id) else { return false } + if enabled { + newlyIdentifiedIDs.remove(id) + return enabledIDs.insert(id).inserted + } + return enabledIDs.remove(id) != nil + } + + func isEnabled(_ id: InputDeviceID) -> Bool { enabledIDs.contains(id) } + func isNewlyIdentified(_ id: InputDeviceID) -> Bool { newlyIdentifiedIDs.contains(id) } + func name(for id: InputDeviceID) -> String? { names[id] } + func isConnected(_ id: InputDeviceID) -> Bool { connectedIDs.contains(id) } + + /// Enabled and connected, in the shared global order. This is the whole admitted set, not just + /// the four that fit: everything past the fourth is the HUD's Waiting rows. + var activeDeviceIDs: [InputDeviceID] { + connectedIDs.filter { enabledIDs.contains($0) } + } + + /// The effective Player 1–4 map: the first four admitted devices, in shared order, compacted so + /// disabling one promotes the next waiting device rather than leaving a gap. + var playerIndices: [InputDeviceID: Int] { + Dictionary(uniqueKeysWithValues: activeDeviceIDs.prefix(Self.maximumPlayers).enumerated().map { ($0.element, $0.offset) }) + } + + func playerIndex(for id: InputDeviceID) -> Int? { playerIndices[id] } + + /// Connected rows in shared order, then this session's disconnected history. + var rows: [Row] { + let slots = playerIndices + var rows = connectedIDs.map { id in + Row(id: id, name: names[id] ?? "Controller", isEnabled: enabledIDs.contains(id), + state: rowState(for: id, slots: slots)) + } + rows += disconnectedIDs.map { id in + Row(id: id, name: names[id] ?? "Controller", isEnabled: enabledIDs.contains(id), state: .disconnected) + } + return rows + } + + private func rowState(for id: InputDeviceID, slots: [InputDeviceID: Int]) -> RowState { + guard enabledIDs.contains(id) else { + return newlyIdentifiedIDs.contains(id) ? .newlyIdentified : .disabled + } + return slots[id].map(RowState.active(playerIndex:)) ?? .waiting + } +} diff --git a/OPN/Stream/ControllerMappingDevices.swift b/OPN/Stream/ControllerMappingDevices.swift index 653faa753..8cbeac856 100644 --- a/OPN/Stream/ControllerMappingDevices.swift +++ b/OPN/Stream/ControllerMappingDevices.swift @@ -17,6 +17,22 @@ struct ControllerMappingDevice: Equatable, Identifiable, Sendable { @MainActor final class ControllerMappingDevices: ObservableObject { static let shared = ControllerMappingDevices() + + /// Where this registry looks for hardware. The live defaults are the real GameController and + /// Steam HID monitors; a test substitutes a scripted list so selection and routing can be + /// exercised end to end without hardware attached. + @MainActor + struct Discovery { + var nativeControllers: () -> [GCController] + var steamDeviceIDs: () -> [InputDeviceID] + + static let live = Discovery( + nativeControllers: { NativeGamepadMonitor.availableNativeControllers() }, + steamDeviceIDs: { SteamControllerHIDMonitor.shared.activeDeviceIDs } + ) + } + + var discovery = Discovery.live @Published private(set) var devices: [ControllerMappingDevice] = [] @Published private(set) var playerOrder = ControllerPlayerOrder() private let orderChanges = PassthroughSubject() @@ -63,10 +79,10 @@ final class ControllerMappingDevices: ObservableObject { deinit { observers.forEach(NotificationCenter.default.removeObserver) } func refresh() { - let native = NativeGamepadMonitor.availableNativeControllers() + let native = discovery.nativeControllers() nativeIDs = identities.update(native.map(ObjectIdentifier.init)) controllers.removeAll() - var next = SteamControllerHIDMonitor.shared.activeDeviceIDs.enumerated().map { index, id in + var next = discovery.steamDeviceIDs().enumerated().map { index, id in ControllerMappingDevice(id: id, name: "Steam Controller \(index + 1)", family: .steam, hasTouchpad: false) } for (index, controller) in native.enumerated() { diff --git a/OPN/Stream/NativeGamepadMonitor+Mapping.swift b/OPN/Stream/NativeGamepadMonitor+Mapping.swift index 71f864e49..49d26217a 100644 --- a/OPN/Stream/NativeGamepadMonitor+Mapping.swift +++ b/OPN/Stream/NativeGamepadMonitor+Mapping.swift @@ -68,7 +68,6 @@ extension NativeGamepadMonitor { func refreshMappingConfiguration(replaySteam: Bool = true) { SteamControllerHIDMonitor.shared.refreshCaptureConfiguration() - let registry = ControllerMappingDevices.shared var configuration: [ObjectIdentifier: NativeControllerMappingConfiguration] = [:] for controller in pollState.cachedControllers { guard let id = registry.id(for: controller), diff --git a/OPN/Stream/NativeGamepadMonitor+Ordering.swift b/OPN/Stream/NativeGamepadMonitor+Ordering.swift index f46d88847..f1c8c6926 100644 --- a/OPN/Stream/NativeGamepadMonitor+Ordering.swift +++ b/OPN/Stream/NativeGamepadMonitor+Ordering.swift @@ -1,8 +1,33 @@ import Foundation extension NativeGamepadMonitor { - func prepareForControllerSlotChange() { - stopHaptics() + /// Every haptic this monitor owns, released together. Teardown and a wholesale slot reset want + /// this; a per-stream disable wants the narrowed form below so it cannot take another stream's + /// contribution with it. + public func stopHaptics() { + hapticStates.values.forEach { $0.stop() } + hapticStates.removeAll() + for (deviceID, task) in steamRumbleStopTasks { + task.cancel() + SteamControllerHIDMonitor.shared.sendRumble(deviceID: deviceID, leftAmplitude: 0, rightAmplitude: 0) + } + steamRumbleStopTasks.removeAll() + } + + /// Neutralizes the slots that are going away, then releases their owned mapped outputs, before + /// the replacement slot map is published. + /// + /// `keepingSteamSlots`/`keepingControllerSlots` are the *new* maps: when the caller has them, the + /// haptic release is narrowed to the devices that actually lost their slot, so disabling one + /// controller in this stream cannot stop an unrelated pad's rumble. Passing neither keeps the + /// teardown behaviour of stopping every haptic this monitor owns. + func prepareForControllerSlotChange(keepingSteamSlots: [InputDeviceID: Int]? = nil, + keepingControllerSlots: [ObjectIdentifier: Int]? = nil) { + if let keepingSteamSlots, let keepingControllerSlots { + stopHaptics(keepingSteamSlots: keepingSteamSlots, keepingControllerSlots: keepingControllerSlots) + } else { + stopHaptics() + } reapplyTasks.values.forEach { $0.cancel() } reapplyTasks.removeAll() let releases = pollingQueue.sync { pollState.prepareForSlotChange() } diff --git a/OPN/Stream/NativeGamepadMonitor+StreamSelection.swift b/OPN/Stream/NativeGamepadMonitor+StreamSelection.swift new file mode 100644 index 000000000..b8d35341d --- /dev/null +++ b/OPN/Stream/NativeGamepadMonitor+StreamSelection.swift @@ -0,0 +1,40 @@ +import Foundation + +/// One stream's controller admission: the HUD's rows, the toggle that edits them, and the haptic +/// release that follows a device losing its slot. +/// +/// Kept in its own extension so the per-stream feature does not grow `NativeGamepadMonitor`'s own +/// body past the repository's size budget; the storage still lives with the monitor that owns it. +extension NativeGamepadMonitor { + /// The HUD's rows for this stream: effective player per enabled device, plus the remembered + /// rows for whatever is disabled, waiting or gone. + var streamControllerRows: [ControllerStreamSelection.Row] { streamSelection.rows } + + /// Enables or disables one device for this stream only. The slot map, topology, mapped outputs + /// and rumble routing are all recomputed by the refresh, which neutralizes the old slot before + /// it publishes the replacement. + @discardableResult + func setStreamEnabled(_ enabled: Bool, for deviceID: InputDeviceID) -> Bool { + guard streamSelection.setEnabled(enabled, for: deviceID) else { return false } + OPNStreamTelemetry.capture("webrtc.input.gamepad.stream_selection", level: .info, + message: "Controller enablement changed for this stream.", + attributes: ["device": deviceID.rawValue, "enabled": String(enabled)]) + refreshControllerSlots() + return true + } + + /// Releases haptics only for the devices this stream no longer routes. A device disabled here + /// must stop vibrating for this stream without stopping an unrelated device's rumble, and + /// without reaching into another stream's monitor — which owns its own haptic state and its own + /// pending stops for the same physical hardware. + func stopHaptics(keepingSteamSlots: [InputDeviceID: Int], keepingControllerSlots: [ObjectIdentifier: Int]) { + for key in Array(hapticStates.keys) where keepingControllerSlots[key] == nil { + hapticStates[key]?.stop() + hapticStates.removeValue(forKey: key) + } + for deviceID in Array(steamRumbleStopTasks.keys) where keepingSteamSlots[deviceID] == nil { + steamRumbleStopTasks.removeValue(forKey: deviceID)?.cancel() + SteamControllerHIDMonitor.shared.sendRumble(deviceID: deviceID, leftAmplitude: 0, rightAmplitude: 0) + } + } +} diff --git a/OPN/Stream/NativeGamepadMonitor.swift b/OPN/Stream/NativeGamepadMonitor.swift index fd680cab5..b0eb34fac 100644 --- a/OPN/Stream/NativeGamepadMonitor.swift +++ b/OPN/Stream/NativeGamepadMonitor.swift @@ -96,9 +96,11 @@ public final class NativeGamepadMonitor { private var chordTracker = StreamOSKChordTracker() private var guideTapTracker = SteamGuideTapTracker() private var onScreenKeyboardCapturedDevices: Set = [] - private var hapticStates: [ObjectIdentifier: ControllerHapticState] = [:] + /// Internal, not private: the stream-selection extension narrows a haptic release to the + /// devices that actually lost their slot. + var hapticStates: [ObjectIdentifier: ControllerHapticState] = [:] /// Pending "motors off" for each Steam Controller currently rumbling. - private var steamRumbleStopTasks: [InputDeviceID: Task] = [:] + var steamRumbleStopTasks: [InputDeviceID: Task] = [:] private var steamRumbleCommandsSent = 0 private var accessibilityPromptShown = false @@ -112,11 +114,25 @@ public final class NativeGamepadMonitor { /// Returning true hands the raw snapshot to the on-screen keyboard instead of /// the binding engine. The keyboard also owns button navigation while active. public var onScreenKeyboardCapture: ((InputDeviceID, ControllerInputSnapshot) -> Bool)? + /// A controller that arrived mid-stream and is therefore admitted disabled in this stream. The + /// host turns this into the brief arrival notice; admission itself still needs the HUD toggle. + public var onStreamControllerArrival: ((String) -> Void)? + + /// This stream's admission decision, layered over the shared order. Owned here rather than in + /// the registry so a second stream's selection cannot be read or written by this one, and + /// released with the monitor when the stream ends. Written only through `setStreamEnabled` in + /// the stream-selection extension. + var streamSelection = ControllerStreamSelection() let mappingProvider: any ControllerMappingProviding + /// The shared order and device list this monitor reads. Injected so a test can script discovery; + /// production always passes the app-wide registry. + let registry: ControllerMappingDevices - init(mappingProvider: any ControllerMappingProviding = ControllerMappingStore.shared) { + init(mappingProvider: any ControllerMappingProviding = ControllerMappingStore.shared, + registry: ControllerMappingDevices = .shared) { self.mappingProvider = mappingProvider + self.registry = registry observerTokens = [ NotificationCenter.default.addObserver(forName: .GCControllerDidConnect, object: nil, queue: .main) { [weak self] _ in MainActor.assumeIsolated { self?.refreshControllerSlots() } @@ -129,7 +145,7 @@ public final class NativeGamepadMonitor { mappingSubscription = mappingProvider.revisionPublisher.dropFirst().sink { [weak self] _ in self?.refreshMappingConfiguration() } - orderSubscription = ControllerMappingDevices.shared.orderChangesPublisher.sink { [weak self] in + orderSubscription = registry.orderChangesPublisher.sink { [weak self] in self?.refreshControllerSlots() } } @@ -291,16 +307,6 @@ public final class NativeGamepadMonitor { /// one in every three hundred rather than flooding a log during a rumble-heavy fight. private var hapticCommandsSeen = 0 - public func stopHaptics() { - hapticStates.values.forEach { $0.stop() } - hapticStates.removeAll() - for (deviceID, task) in steamRumbleStopTasks { - task.cancel() - SteamControllerHIDMonitor.shared.sendRumble(deviceID: deviceID, leftAmplitude: 0, rightAmplitude: 0) - } - steamRumbleStopTasks.removeAll() - } - /// A Steam Controller's rumble is a *state* set by feature report, not a timed pattern, so a /// command with a duration is a set now and a clear later. A newer command for the same pad /// replaces the pending clear: games refresh the state every frame while vibrating and the @@ -343,21 +349,32 @@ public final class NativeGamepadMonitor { return (nativePaths + steamPaths).sorted { $0.playerIndex < $1.playerIndex } } - private func refreshControllerSlots() { + /// Internal, not private: `setStreamEnabled` in the stream-selection extension re-runs it after + /// changing this stream's admission. + func refreshControllerSlots() { let previousSteamSlots = pollState.steamControllerSlots - let registry = ControllerMappingDevices.shared + let previousControllerSlots = pollState.controllerSlots registry.refresh() + let arrivals = streamSelection.synchronize(devices: registry.orderedDevices.map { + ControllerStreamSelection.Device(id: $0.id, name: $0.name) + }) + for arrival in arrivals { onStreamControllerArrival?(arrival.name) } let nativeIDs = registry.devices.reduce(into: [InputDeviceID: ObjectIdentifier]()) { result, device in if let controller = registry.controller(for: device.id) { result[device.id] = ObjectIdentifier(controller) } } - let assignments = ControllerSlotAssignments(order: registry.playerOrder, - steamIDs: Set(SteamControllerHIDMonitor.shared.activeDeviceIDs), nativeIDs: nativeIDs) + // The shared order is filtered to this stream's admitted devices *before* the four-slot + // limit, so disabling a device compacts the remaining players instead of leaving a hole. + // Read the Steam membership off the registry rather than the HID monitor so the filtered + // assignment is built from one discovery snapshot — and so a scripted registry is honored. + let steamIDs = Set(registry.devices.filter { $0.family == .steam }.map(\.id)) + let assignments = ControllerSlotAssignments(deviceIDs: streamSelection.activeDeviceIDs, + steamIDs: steamIDs, nativeIDs: nativeIDs) let newSteamSlots = assignments.steam let newControllerSlots = assignments.native let cachedControllers = registry.orderedDevices.compactMap { registry.controller(for: $0.id) } .filter { newControllerSlots[ObjectIdentifier($0)] != nil } - if newSteamSlots != previousSteamSlots || newControllerSlots != pollState.controllerSlots { - prepareForControllerSlotChange() + if newSteamSlots != previousSteamSlots || newControllerSlots != previousControllerSlots { + prepareForControllerSlotChange(keepingSteamSlots: newSteamSlots, keepingControllerSlots: newControllerSlots) } pollingQueue.sync { pollState.controllerSlots = newControllerSlots @@ -547,7 +564,7 @@ public final class NativeGamepadMonitor { } @MainActor -private final class ControllerHapticState { +final class ControllerHapticState { private let controller: GCController private var engines: [GCHapticsLocality: CHHapticEngine] = [:] private var players: [GCHapticsLocality: any CHHapticPatternPlayer] = [:] diff --git a/OPN/Stream/NativeStreamView.swift b/OPN/Stream/NativeStreamView.swift index 68915f24a..85ab0c31e 100644 --- a/OPN/Stream/NativeStreamView.swift +++ b/OPN/Stream/NativeStreamView.swift @@ -349,6 +349,12 @@ public final class NativeStreamView: NSView { get { gamepadMonitor.onScreenKeyboardCapture } set { gamepadMonitor.onScreenKeyboardCapture = newValue } } + /// A controller that arrived mid-stream, and is therefore admitted disabled in this stream. + /// Set by the active stream host so the arrival can be announced without admitting the device. + public var onStreamControllerArrival: ((String) -> Void)? { + get { gamepadMonitor.onStreamControllerArrival } + set { gamepadMonitor.onStreamControllerArrival = newValue } + } public var hidesCursorWhilePointerLocked = true { didSet { guard isPointerLocked else { return } diff --git a/Tests/Settings/ControllerOrderNavigationTests.swift b/Tests/Settings/ControllerOrderNavigationTests.swift index 7f3c32a3c..d27677006 100644 --- a/Tests/Settings/ControllerOrderNavigationTests.swift +++ b/Tests/Settings/ControllerOrderNavigationTests.swift @@ -38,6 +38,25 @@ import Testing #expect(navigation.process(pressed, deviceIDs: ids) == .move("first", .later)) } + @Test func streamPanelConfirmTogglesEnablementInsteadOfCommittingAMove() { + let navigation = ControllerOrderNavigationModel() + navigation.activateToggles = true + let ids: [InputDeviceID] = ["first", "second"] + navigation.reconcile(ids) + #expect(navigation.process(.activate, deviceIDs: ids) == .toggle("first")) + // Left/right stays the reorder axis, because ordering is global in every panel. + #expect(navigation.process(.move(.right), deviceIDs: ids) == nil) + #expect(navigation.focus?.direction == .later) + #expect(navigation.process(.back, deviceIDs: ids) == .close) + } + + @Test func settingsSurfaceKeepsConfirmAsTheReorderAction() { + let navigation = ControllerOrderNavigationModel() + let ids: [InputDeviceID] = ["first", "second"] + navigation.reconcile(ids) + #expect(navigation.process(.activate, deviceIDs: ids) == .move("first", .later)) + } + @Test func controllerOrderSearchFindsSharedTools() throws { let result = try #require(SettingsSearchIndex.results(for: "reorder").first) #expect(result.title == "Controller Order") diff --git a/Tests/Settings/ControllerOrderRowsTests.swift b/Tests/Settings/ControllerOrderRowsTests.swift new file mode 100644 index 000000000..70e3116b9 --- /dev/null +++ b/Tests/Settings/ControllerOrderRowsTests.swift @@ -0,0 +1,64 @@ +import Foundation +import Testing +@testable import OpenNOW + +/// What the controller panel actually shows. These are the strings and states a player reads off the +/// HUD, so they are asserted here rather than left to a rendered snapshot. +@Suite struct ControllerOrderRowsTests { + private func streamRow(_ id: InputDeviceID, _ name: String = "Pad", enabled: Bool, + state: ControllerStreamSelection.RowState) -> ControllerStreamSelection.Row { + ControllerStreamSelection.Row(id: id, name: name, isEnabled: enabled, state: state) + } + + @Test func activeEnabledDeviceShowsOnWithItsEffectivePlayerNumber() { + let rows = ControllerOrderRows.stream([streamRow("a", "DualSense", enabled: true, state: .active(playerIndex: 1))], + families: ["a": .dualShock4]) + #expect(rows.first?.slotLabel == "PLAYER 2") + #expect(rows.first?.detail == ControllerFamily.dualShock4.label) + #expect(rows.first?.isEnabled == true) + #expect(rows.first?.showsEnableToggle == true) + #expect(rows.first?.isDimmed == false) + } + + @Test func enabledOverflowDeviceShowsWaiting() { + let rows = ControllerOrderRows.stream([streamRow("e", enabled: true, state: .waiting)], families: [:]) + #expect(rows.first?.slotLabel == "WAITING") + #expect(rows.first?.detail == "Waiting — four players already active") + #expect(rows.first?.isEnabled == true) + } + + @Test func connectedDisabledDeviceShowsOffAndNoPlayerNumber() { + let rows = ControllerOrderRows.stream([streamRow("b", enabled: false, state: .disabled)], families: [:]) + #expect(rows.first?.slotLabel == "OFF") + #expect(rows.first?.detail == "Disabled in this stream") + #expect(rows.first?.isEnabled == false) + #expect(rows.first?.showsEnableToggle == true) + } + + @Test func unknownMidStreamArrivalShowsTheNewDevicePrompt() { + let rows = ControllerOrderRows.stream([streamRow("n", enabled: false, state: .newlyIdentified)], families: [:]) + #expect(rows.first?.slotLabel == "NEW") + #expect(rows.first?.detail == "New controller — enable in this stream") + #expect(rows.first?.isEnabled == false) + } + + @Test func disconnectedDeviceStaysDimmedAndReadOnlyWithTheRememberedChoice() { + let rows = ControllerOrderRows.stream([streamRow("old", "Xbox Controller", enabled: true, state: .disconnected)], families: [:]) + #expect(rows.first?.slotLabel == "OFFLINE") + #expect(rows.first?.detail == "Disconnected — remembered for this session") + #expect(rows.first?.name == "Xbox Controller") + #expect(rows.first?.isEnabled == true) + #expect(rows.first?.showsEnableToggle == false) + #expect(rows.first?.isDimmed == true) + } + + @Test func settingsRowsKeepTheGlobalOrderAndOfferNoStreamToggle() { + let devices = [ + ControllerMappingDevice(id: "a", name: "Steam Controller 1", family: .steam, hasTouchpad: false), + ControllerMappingDevice(id: "b", name: "DualSense", family: .dualShock4, hasTouchpad: true), + ] + let rows = ControllerOrderRows.order(devices) + #expect(rows.map(\.slotLabel) == ["PLAYER 1", "PLAYER 2"]) + #expect(rows.allSatisfy { $0.showsEnableToggle == false && $0.isEnabled }) + } +} diff --git a/Tests/Stream/ControllerStreamRoutingTests.swift b/Tests/Stream/ControllerStreamRoutingTests.swift new file mode 100644 index 000000000..0a9eee752 --- /dev/null +++ b/Tests/Stream/ControllerStreamRoutingTests.swift @@ -0,0 +1,126 @@ +import Foundation +import GameController +import os +import Testing +@testable import OpenNOW + +/// Stream selection seen from the transport boundary: what the seat is told, which slots exist, and +/// what happens to held input when a device loses its slot to a disable or a disconnect. +/// +/// Discovery is scripted through `ControllerMappingDevices.Discovery`, so these run without hardware +/// attached. They prove the *model and routing* contract; they say nothing about how reliably real +/// hardware is identified across a reconnect. +@MainActor +@Suite struct ControllerStreamRoutingTests { + private func makeStore() throws -> (store: ControllerMappingStore, defaults: UserDefaults, suite: String) { + let suite = "ControllerStreamRoutingTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + return (ControllerMappingStore(defaults: defaults), defaults, suite) + } + + @Test func disablingADeviceCompactsTheSlotMapWithoutTouchingTheSharedOrder() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + let second = GCController.withExtendedGamepad() + let registry = ControllerMappingDevices() + registry.discovery = .init(nativeControllers: { [first, second] }, steamDeviceIDs: { [] }) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + let secondID = try #require(registry.id(for: second)) + #expect(monitor.inputPaths().map(\.playerIndex) == [0, 1]) + #expect(monitor.topology.playerIndices == [0, 1]) + + monitor.setStreamEnabled(false, for: secondID) + #expect(monitor.topology.playerIndices == [0]) + #expect(monitor.inputPaths().map(\.playerIndex) == [0]) + #expect(monitor.streamControllerRows.first { $0.id == secondID }?.state == .disabled) + #expect(monitor.streamControllerRows.first { $0.id == secondID }?.playerIndex == nil) + // Disabling is stream-local: the shared order keeps both devices where they were. + #expect(registry.playerOrder.deviceIDs == [firstID, secondID]) + + monitor.setStreamEnabled(true, for: secondID) + #expect(monitor.topology.playerIndices == [0, 1]) + #expect(monitor.inputPaths().map(\.playerIndex) == [0, 1]) + monitor.stop() + } + + @Test func everyDeviceDisabledProducesAnEmptyLocalSlotMapAndTopology() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + let registry = ControllerMappingDevices() + registry.discovery = .init(nativeControllers: { [first] }, steamDeviceIDs: { [] }) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + #expect(monitor.topology.playerIndices == [0]) + + monitor.setStreamEnabled(false, for: firstID) + #expect(monitor.topology.playerIndices.isEmpty) + #expect(monitor.inputPaths().isEmpty) + monitor.stop() + } + + @Test func midStreamArrivalIsAdmittedDisabledUntilThisStreamEnablesIt() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + var natives: [GCController] = [first] + let registry = ControllerMappingDevices() + registry.discovery = .init(nativeControllers: { natives }, steamDeviceIDs: { [] }) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + #expect(monitor.inputPaths().map(\.playerIndex) == [0]) + + let second = GCController.withExtendedGamepad() + natives = [first, second] + var arrivals: [String] = [] + monitor.onStreamControllerArrival = { arrivals.append($0) } + monitor.refreshInputState() + let secondID = try #require(registry.id(for: second)) + #expect(arrivals.count == 1) + #expect(monitor.streamControllerRows.first { $0.id == secondID }?.state == .newlyIdentified) + #expect(monitor.inputPaths().map(\.playerIndex) == [0]) + + monitor.setStreamEnabled(true, for: secondID) + #expect(monitor.topology.playerIndices == [0, 1]) + #expect(registry.playerOrder.deviceIDs == [firstID, secondID]) + monitor.stop() + } + + @Test func disableNeutralizesTheOldSlotBeforeTheCompactedMapIsConfigured() throws { + let controller = GCController.withExtendedGamepad() + let gamepad = try #require(controller.extendedGamepad) + let id = ObjectIdentifier(controller) + let state = NativeGamepadPollState() + state.cachedControllers = [controller] + state.controllerSlots = [id: 1] + let profile = ControllerMappingProfile(name: "Keyboard", family: .generic, + bindings: [.faceA: .keyboardKey(keyCode: 49, modifiers: [])]) + _ = state.configureMappings([id: NativeControllerMappingConfiguration(deviceID: "pad", playerIndex: 1, profile: profile)]) + gamepad.buttonA.setValue(1) + let captured = OSAllocatedUnfairLock(initialState: [UserInputEvent]()) + state.pollAndEmit(onEvents: { events in captured.withLock { $0 += events } }, onBatteryChange: { _ in }) + state.pendingEvents = captured.withLock { $0 } + + // The stream disables the device: the queued press is delivered, then the old slot is + // neutralized and its owned key released — before any replacement map exists. + let releases = state.prepareForSlotChange() + #expect(keyStates(releases) == [true, false]) + #expect(gamepads(releases).allSatisfy { $0.playerIndex == 1 && $0.buttons.isEmpty }) + + state.controllerSlots = [:] + state.cachedControllers = [] + captured.withLock { $0.removeAll() } + state.pollAndEmit(onEvents: { events in captured.withLock { $0 += events } }, onBatteryChange: { _ in }) + #expect(captured.withLock { $0 }.isEmpty) + } + + private func keyStates(_ events: [UserInputEvent]) -> [Bool] { + events.compactMap { if case .keyboard(let key) = $0 { key.isPressed } else { nil } } + } + + private func gamepads(_ events: [UserInputEvent]) -> [GamepadState] { + events.compactMap { if case .gamepad(let state) = $0 { state } else { nil } } + } +} diff --git a/Tests/Stream/ControllerStreamSelectionTests.swift b/Tests/Stream/ControllerStreamSelectionTests.swift new file mode 100644 index 000000000..f5cf9802e --- /dev/null +++ b/Tests/Stream/ControllerStreamSelectionTests.swift @@ -0,0 +1,175 @@ +import Foundation +import Testing +@testable import OpenNOW + +@Suite struct ControllerStreamSelectionTests { + private func device(_ id: InputDeviceID, _ name: String = "Pad") -> ControllerStreamSelection.Device { + ControllerStreamSelection.Device(id: id, name: name) + } + + // MARK: Assignment (global order A–E) + + @Test func enabledSubsetTakesCompactedSlotsInSharedOrder() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a"), device("b"), device("c"), device("d"), device("e")]) + for id: InputDeviceID in ["b", "d"] { selection.setEnabled(false, for: id) } + #expect(selection.activeDeviceIDs == ["a", "c", "e"]) + #expect(selection.playerIndices == ["a": 0, "c": 1, "e": 2]) + } + + @Test func disablingAMiddleDevicePromotesTheWaitingDevice() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a"), device("b"), device("c"), device("d"), device("e")]) + #expect(selection.playerIndices == ["a": 0, "b": 1, "c": 2, "d": 3]) + #expect(selection.rows.first { $0.id == "e" }?.state == .waiting) + selection.setEnabled(false, for: "b") + #expect(selection.activeDeviceIDs == ["a", "c", "d", "e"]) + #expect(selection.playerIndices == ["a": 0, "c": 1, "d": 2, "e": 3]) + #expect(selection.rows.first { $0.id == "e" }?.state == .active(playerIndex: 3)) + #expect(selection.rows.first { $0.id == "b" }?.state == .disabled) + #expect(selection.rows.first { $0.id == "b" }?.playerIndex == nil) + } + + @Test func reEnablingRestoresTheSharedOrderAndSendsTheLastDeviceBackToWaiting() { + var selection = ControllerStreamSelection() + let order = [device("a"), device("b"), device("c"), device("d"), device("e")] + selection.beginSession(devices: order) + selection.setEnabled(false, for: "b") + selection.setEnabled(true, for: "b") + #expect(selection.playerIndices == ["a": 0, "b": 1, "c": 2, "d": 3]) + #expect(selection.rows.first { $0.id == "e" }?.state == .waiting) + #expect(selection.rows.first { $0.id == "e" }?.isEnabled == true) + } + + @Test func reorderRecomputesSlotsWithoutChangingSelections() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a"), device("b"), device("c")]) + selection.setEnabled(false, for: "b") + #expect(selection.playerIndices == ["a": 0, "c": 1]) + selection.synchronize(devices: [device("c"), device("a"), device("b")]) + #expect(selection.playerIndices == ["c": 0, "a": 1]) + #expect(!selection.isEnabled("b")) + #expect(selection.isEnabled("c")) + #expect(selection.rows.map(\.id) == ["c", "a", "b"]) + } + + // MARK: Admission and lifecycle + + @Test func midStreamArrivalIsAdmittedDisabledUntilEnabled() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a")]) + let arrivals = selection.synchronize(devices: [device("a"), device("b")]) + #expect(arrivals.map(\.id) == ["b"]) + #expect(selection.rows.first { $0.id == "b" }?.state == .newlyIdentified) + #expect(selection.rows.first { $0.id == "b" }?.isEnabled == false) + #expect(selection.playerIndex(for: "b") == nil) + selection.setEnabled(true, for: "b") + #expect(selection.rows.first { $0.id == "b" }?.state == .active(playerIndex: 1)) + #expect(!selection.isNewlyIdentified("b")) + } + + @Test func disconnectCompactsTheRemainingEnabledPlayersAndRemembersTheChoice() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a"), device("b"), device("c")]) + _ = selection.synchronize(devices: [device("a"), device("c")]) + #expect(selection.activeDeviceIDs == ["a", "c"]) + #expect(selection.playerIndices == ["a": 0, "c": 1]) + let history = selection.rows.first { $0.id == "b" } + #expect(history?.state == .disconnected) + #expect(history?.isEnabled == true) + #expect(history?.playerIndex == nil) + #expect(selection.rows.map(\.id) == ["a", "c", "b"]) + } + + @Test func reliableReconnectRestoresEachStreamsOwnChoiceWithoutADuplicateRow() { + var first = ControllerStreamSelection() + var second = ControllerStreamSelection() + first.beginSession(devices: [device("a")]) + second.beginSession(devices: [device("a")]) + second.setEnabled(false, for: "a") + #expect(first.playerIndex(for: "a") == 0) + #expect(second.playerIndex(for: "a") == nil) + #expect(first.synchronize(devices: []).isEmpty) + #expect(second.synchronize(devices: []).isEmpty) + #expect(first.rows.map(\.state) == [.disconnected]) + // A trustworthy identity returns: the remembered choice is restored per stream, and the row + // is the live one rather than a live row beside a stale history row. + #expect(first.synchronize(devices: [device("a")]).isEmpty) + #expect(second.synchronize(devices: [device("a")]).isEmpty) + #expect(first.playerIndex(for: "a") == 0) + #expect(second.rows.first { $0.id == "a" }?.state == .disabled) + #expect(first.rows.count == 1) + #expect(second.rows.count == 1) + } + + @Test func reliableReconnectRecalculatesActiveOrWaitingFromTheCurrentOrder() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a"), device("b"), device("c"), device("d")]) + _ = selection.synchronize(devices: [device("b"), device("c"), device("d")]) + _ = selection.synchronize(devices: [device("b"), device("c"), device("d"), device("e")]) + selection.setEnabled(true, for: "e") + #expect(selection.playerIndices == ["b": 0, "c": 1, "d": 2, "e": 3]) + // The remembered device returns behind four enabled devices, so the current order makes it + // Waiting rather than Active. + _ = selection.synchronize(devices: [device("b"), device("c"), device("d"), device("e"), device("a")]) + #expect(selection.rows.first { $0.id == "a" }?.state == .waiting) + #expect(selection.rows.first { $0.id == "a" }?.isEnabled == true) + #expect(selection.rows.first { $0.id == "a" }?.playerIndex == nil) + } + + @Test func changedIdentityStartsDisabledAndKeepsTheOldHistory() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("n1", "DualSense")]) + let arrivals = selection.synchronize(devices: [device("n2", "DualSense")]) + #expect(arrivals.map(\.id) == ["n2"]) + #expect(selection.rows.first { $0.id == "n2" }?.state == .newlyIdentified) + #expect(selection.playerIndex(for: "n2") == nil) + #expect(selection.rows.first { $0.id == "n1" }?.state == .disconnected) + #expect(selection.rows.first { $0.id == "n1" }?.isEnabled == true) + #expect(selection.rows.count == 2) + } + + @Test func identicalLabelsNeverTransferSelectionOrMergeHistory() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("n1", "Xbox Wireless Controller")]) + selection.setEnabled(false, for: "n1") + _ = selection.synchronize(devices: [device("n1", "Xbox Wireless Controller"), device("n2", "Xbox Wireless Controller")]) + #expect(!selection.isEnabled("n1")) + #expect(!selection.isEnabled("n2")) + #expect(selection.rows.first { $0.id == "n2" }?.state == .newlyIdentified) + #expect(selection.rows.count == 2) + } + + @Test func newSessionDoesNotInheritSelectionOrHistory() { + var previous = ControllerStreamSelection() + previous.beginSession(devices: [device("a"), device("b")]) + previous.setEnabled(false, for: "a") + _ = previous.synchronize(devices: [device("b")]) + var restarted = ControllerStreamSelection() + restarted.beginSession(devices: [device("a")]) + #expect(restarted.isEnabled("a")) + #expect(restarted.playerIndex(for: "a") == 0) + #expect(restarted.rows.count == 1) + #expect(restarted.rows.first?.state == .active(playerIndex: 0)) + } + + @Test func repeatDiscoveryChangesNothing() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a"), device("b")]) + selection.setEnabled(false, for: "b") + let before = selection + #expect(selection.synchronize(devices: [device("a"), device("b")]).isEmpty) + #expect(selection == before) + #expect(selection.rows.count == 2) + } + + @Test func everyDeviceDisabledLeavesNoPlayerAndNoActiveDevice() { + var selection = ControllerStreamSelection() + selection.beginSession(devices: [device("a"), device("b")]) + selection.setEnabled(false, for: "a") + selection.setEnabled(false, for: "b") + #expect(selection.activeDeviceIDs.isEmpty) + #expect(selection.playerIndices.isEmpty) + #expect(selection.rows.allSatisfy { $0.state == .disabled && !$0.isEnabled }) + } +} diff --git a/View/Settings/ControllerOrderView.swift b/View/Settings/ControllerOrderView.swift index 0eba44fee..a62824726 100644 --- a/View/Settings/ControllerOrderView.swift +++ b/View/Settings/ControllerOrderView.swift @@ -2,24 +2,34 @@ import GameController import SwiftUI struct ControllerOrderView: View { + /// The stream whose selection this panel edits, or nil on the Settings surface, where the panel + /// only reorders the app-wide controller order. + var stream: StreamControllerSelectionContext? = nil @Environment(\.dismiss) private var dismiss @Environment(\.opnUIScale) private var uiScale @ObservedObject private var devices = ControllerMappingDevices.shared @StateObject private var navigation = ControllerOrderNavigationModel() @State private var inputWindow = ControllerOrderWindowReference() + @State private var streamRows: [ControllerStreamSelection.Row] = [] var body: some View { - let size = ControllerOrderPanel.sheetSize(deviceCount: devices.devices.count, uiScale: uiScale) - ControllerOrderPanel(devices: devices.orderedDevices, isCustom: devices.playerOrder.isCustom, focusedControl: navigation.focus, + let rows = self.rows + let size = ControllerOrderPanel.sheetSize(deviceCount: rows.count, uiScale: uiScale) + ControllerOrderPanel(rows: rows, isCustom: devices.playerOrder.isCustom, streamScoped: stream != nil, + focusedControl: navigation.focus, + onSetEnabled: { stream?.setEnabled($0, for: $1) }, onMove: devices.move, onReset: devices.resetOrder, onClose: { dismiss() }) .frame(minWidth: size.width, idealWidth: size.width, minHeight: size.height, idealHeight: size.height) .background(ControllerOrderWindowReader(reference: inputWindow)) .onExitCommand { dismiss() } .task { devices.refresh() + navigation.activateToggles = stream != nil + streamRows = stream?.rows ?? [] SteamControllerHIDMonitor.shared.beginInputCapture(inputWindow) defer { SteamControllerHIDMonitor.shared.endInputCapture(inputWindow) } while !Task.isCancelled { + refreshStreamRows() navigation.reconcile(devices.playerOrder.deviceIDs) if inputWindow.acceptsInput { if pollNavigation() { break } @@ -31,6 +41,21 @@ struct ControllerOrderView: View { } } + /// Rows come from the stream's own selection when there is one, so the panel shows the effective + /// player each enabled controller actually occupies rather than its place in the global order. + private var rows: [ControllerOrderRow] { + guard stream != nil else { return ControllerOrderRows.order(devices.orderedDevices) } + let families = Dictionary(uniqueKeysWithValues: devices.devices.map { ($0.id, $0.family) }) + return ControllerOrderRows.stream(streamRows, families: families) + } + + private func refreshStreamRows() { + guard let stream else { return } + let next = stream.rows + guard next != streamRows else { return } + streamRows = next + } + private func pollNavigation() -> Bool { for device in devices.devices { let snapshot: ControllerInputSnapshot? @@ -43,12 +68,19 @@ struct ControllerOrderView: View { let state = snapshot.gamepadState(deviceID: device.id, playerIndex: 0, timestamp: MediaTimestamp(nanoseconds: 0)) switch navigation.process(state, deviceIDs: devices.playerOrder.deviceIDs) { case .move(let id, let direction): devices.move(id, direction: direction) + case .toggle(let id): toggleStreamDevice(id) case .close: dismiss(); return true case nil: break } } return false } + + private func toggleStreamDevice(_ id: InputDeviceID) { + guard let stream, let row = streamRows.first(where: { $0.id == id }), row.state != .disconnected else { return } + stream.setEnabled(!row.isEnabled, for: id) + refreshStreamRows() + } } struct ControllerOrderPanel: View { @@ -57,9 +89,12 @@ struct ControllerOrderPanel: View { return SteamControllerSheetMetrics.size(width: 680, height: height, uiScale: uiScale) } - let devices: [ControllerMappingDevice] + let rows: [ControllerOrderRow] let isCustom: Bool + /// Whether the panel is editing one stream's selection rather than the app-wide order alone. + let streamScoped: Bool let focusedControl: ControllerOrderNavigationModel.Focus? + let onSetEnabled: (Bool, InputDeviceID) -> Void let onMove: (InputDeviceID, ControllerPlayerOrder.Direction) -> Void let onReset: () -> Void let onClose: () -> Void @@ -70,13 +105,13 @@ struct ControllerOrderPanel: View { SteamControllerModalTopBar() SteamControllerModalHeader(eyebrow: "CONTROLLERS", title: "Controller Order", uiScale: uiScale, onClose: onClose) SteamControllerModalRule() - if devices.isEmpty { + if rows.isEmpty { Text("No controllers connected") .font(.settingsFont(size: 16 * uiScale, weight: .medium)) .foregroundStyle(OPNDesign.Text.secondary) .frame(maxWidth: .infinity, maxHeight: .infinity) } else { - Text("Move controllers to choose Player 1–4. Changes apply immediately; mapping profiles stay with each controller. Remote Co-Op guests are not reordered.") + Text(explanation) .font(.settingsFont(size: 12 * uiScale)) .foregroundStyle(OPNDesign.Text.secondary) .fixedSize(horizontal: false, vertical: true) @@ -84,8 +119,8 @@ struct ControllerOrderPanel: View { ScrollViewReader { proxy in ScrollView { VStack(spacing: OPNDesign.Spacing.xSmall(scale: uiScale)) { - ForEach(Array(devices.enumerated()), id: \.element.id) { index, device in - row(device: device, index: index).id(device.id) + ForEach(Array(rows.enumerated()), id: \.element.id) { index, row in + self.row(row, index: index).id(row.id) } } .padding(.horizontal, OPNDesign.Spacing.card(scale: uiScale)) @@ -96,10 +131,10 @@ struct ControllerOrderPanel: View { if !Task.isCancelled, let id = scrollTarget.deviceID { proxy.scrollTo(id, anchor: .center) } } } - Text("D-pad: navigate · Confirm: move · Back: close") + Text(footerHint) .font(.settingsFont(size: 11 * uiScale)) .foregroundStyle(OPNDesign.Text.secondary) - Text("Order lasts for this connection session. New controllers join the end of a custom order. Some games may require reconnecting to recognize new player slots.") + Text(footerNote) .font(.settingsFont(size: 11 * uiScale)) .foregroundStyle(OPNDesign.Text.tertiary) .fixedSize(horizontal: false, vertical: true) @@ -123,49 +158,86 @@ struct ControllerOrderPanel: View { .background(OPNDesign.Surface.deep) } + private var explanation: String { + guard streamScoped else { + return "Move controllers to choose Player 1–4. Changes apply immediately; mapping profiles stay with each controller. Remote Co-Op guests are not reordered." + } + return "Enable controllers in this stream and move them in the shared order. Ordering is global and every stream uses it; the enable toggles affect this stream only. Mapping profiles stay with each controller, and Remote Co-Op guests are not reordered." + } + + private var footerHint: String { + streamScoped + ? "D-pad: navigate · Left/Right: reorder · Confirm: enable or disable · Back: close" + : "D-pad: navigate · Confirm: move · Back: close" + } + + private var footerNote: String { + streamScoped + ? "Enablement and remembered rows last for this stream session. Controllers connected after the stream starts stay disabled until you enable them. Some games may require reconnecting to recognize new player slots." + : "Order lasts for this connection session. New controllers join the end of a custom order. Some games may require reconnecting to recognize new player slots." + } + private struct ScrollTarget: Equatable { let deviceID: InputDeviceID? let index: Int? } private var scrollTarget: ScrollTarget { - ScrollTarget(deviceID: focusedControl?.deviceID, index: devices.firstIndex { $0.id == focusedControl?.deviceID }) + ScrollTarget(deviceID: focusedControl?.deviceID, index: rows.firstIndex { $0.id == focusedControl?.deviceID }) } - private func row(device: ControllerMappingDevice, index: Int) -> some View { + private func row(_ row: ControllerOrderRow, index: Int) -> some View { HStack(spacing: OPNDesign.Spacing.small(scale: uiScale)) { - Text(index < ControllerPlayerOrder.maximumPlayers ? "PLAYER \(index + 1)" : "WAITING") + Text(row.slotLabel) .font(.settingsFont(size: 11 * uiScale, weight: .bold)) - .foregroundStyle(index < ControllerPlayerOrder.maximumPlayers ? OPNDesign.accentInk : OPNDesign.Text.muted) + .foregroundStyle(slotColor(for: row)) .frame(width: 76 * uiScale, alignment: .leading) VStack(alignment: .leading, spacing: OPNDesign.Spacing.xxSmall(scale: uiScale)) { - Text(device.name) + Text(row.name) .font(.settingsFont(size: 14 * uiScale, weight: .bold)) .lineLimit(1) - .help(device.name) - Text(index < ControllerPlayerOrder.maximumPlayers ? device.family.label : "Move into the first four to use in the stream") + .help(row.name) + Text(row.detail) .font(.settingsFont(size: 11 * uiScale)) .foregroundStyle(OPNDesign.Text.tertiary) .fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) - moveButton(device: device, direction: .earlier, disabled: index == 0) - moveButton(device: device, direction: .later, disabled: index == devices.count - 1) + if row.showsEnableToggle { + enableToggle(for: row) + } + moveButton(row: row, direction: .earlier, disabled: index == 0) + moveButton(row: row, direction: .later, disabled: index == rows.count - 1) } .padding(OPNDesign.Spacing.small(scale: uiScale)) .background(OPNCornerShape(role: .card, scale: uiScale).fill(OPNDesign.Surface.panel)) .overlay { OPNCornerShape(role: .card, scale: uiScale).strokeBorder(OPNDesign.Stroke.subtle, lineWidth: 1) } + .opacity(row.isDimmed ? 0.55 : 1) + } + + /// The stream-scoped control. A disconnected row keeps showing the remembered choice but takes + /// no action, so history cannot be edited after the hardware is gone. + private func enableToggle(for row: ControllerOrderRow) -> some View { + Toggle(isOn: Binding(get: { row.isEnabled }, set: { onSetEnabled($0, row.id) }), uiScale: uiScale) + .accessibilityLabel("Enabled in this stream") + .accessibilityValue(row.isEnabled ? "On" : "Off") + .help(row.isEnabled ? "Enabled in this stream — disable it here" : "Disabled in this stream — enable it here") + } + + private func slotColor(for row: ControllerOrderRow) -> Color { + guard !row.isDimmed else { return OPNDesign.Text.muted } + return row.isEnabled ? OPNDesign.accentInk : OPNDesign.Text.muted } - private func moveButton(device: ControllerMappingDevice, direction: ControllerPlayerOrder.Direction, disabled: Bool) -> some View { - let label = direction == .earlier ? "Move \(device.name) earlier" : "Move \(device.name) later" - return Button { onMove(device.id, direction) } label: { + private func moveButton(row: ControllerOrderRow, direction: ControllerPlayerOrder.Direction, disabled: Bool) -> some View { + let label = direction == .earlier ? "Move \(row.name) earlier" : "Move \(row.name) later" + return Button { onMove(row.id, direction) } label: { Image(systemName: direction == .earlier ? "arrow.up" : "arrow.down") } .buttonStyle(OPNModalSecondaryButtonStyle(uiScale: uiScale)) .disabled(disabled) .overlay { - if focusedControl?.deviceID == device.id, focusedControl?.direction == direction { + if focusedControl?.deviceID == row.id, focusedControl?.direction == direction { OPNCornerShape(role: .control, scale: uiScale).strokeBorder(OPNDesign.accent, lineWidth: 2) } } diff --git a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift index b30a3eaae..23f5062ec 100644 --- a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift +++ b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift @@ -80,8 +80,8 @@ extension NativeNVSTMediaStreamSurface { isDisabled: false, action: { model.showingControllerMapping = true }), NativeHUDTile(id: "controller-order", - title: "Reorder Controllers", - subtitle: "Choose Player 1–4", + title: "Controllers", + subtitle: "Enable in this stream · Player 1–4", systemName: "arrow.up.arrow.down", isActive: false, isDisabled: false, diff --git a/View/Stream/StreamHost.swift b/View/Stream/StreamHost.swift index 6c263e225..19c3e351b 100644 --- a/View/Stream/StreamHost.swift +++ b/View/Stream/StreamHost.swift @@ -118,7 +118,9 @@ struct NativeNVSTMediaStreamSurface: View { ) } .sheet(isPresented: $model.showingControllerOrder) { - ControllerOrderView() + // The stream's own selection travels into the panel, so its toggles edit this session + // only and its rows show the effective player each controller occupies here. + ControllerOrderView(stream: model.streamControllerSelection) } } @@ -189,6 +191,7 @@ private struct NativeNVSTStreamHostView: NSViewRepresentable { nsView.streamView.onPointerLockChanged = nil nsView.streamView.onCommand = nil nsView.streamView.shouldHandleCommand = nil + nsView.streamView.onStreamControllerArrival = nil nsView.onResolve = nil nsView.onWindowChanged = nil } diff --git a/ViewModel/Settings/ControllerOrderNavigationModel.swift b/ViewModel/Settings/ControllerOrderNavigationModel.swift index 41a5d830d..ab694640c 100644 --- a/ViewModel/Settings/ControllerOrderNavigationModel.swift +++ b/ViewModel/Settings/ControllerOrderNavigationModel.swift @@ -9,9 +9,15 @@ final class ControllerOrderNavigationModel: ObservableObject { } enum Action: Equatable { case move(InputDeviceID, ControllerPlayerOrder.Direction) + case toggle(InputDeviceID) case close } + /// In a stream panel, confirm switches this stream's enablement for the focused controller; on + /// the Settings surface it keeps its original meaning of committing the focused reorder arrow. + /// Left/right stays the reorder axis in both, because ordering is global either way. + var activateToggles = false + @Published private(set) var focus: Focus? private let tracker = StreamHUDGamepadTracker() @@ -43,7 +49,7 @@ final class ControllerOrderNavigationModel: ObservableObject { guard let focus, let index = deviceIDs.firstIndex(of: focus.deviceID) else { return nil } switch step { case .activate: - return .move(focus.deviceID, focus.direction) + return activateToggles ? .toggle(focus.deviceID) : .move(focus.deviceID, focus.direction) case .move(let direction): switch direction { case .up, .down: diff --git a/ViewModel/Settings/ControllerOrderRows.swift b/ViewModel/Settings/ControllerOrderRows.swift new file mode 100644 index 000000000..1e81bb550 --- /dev/null +++ b/ViewModel/Settings/ControllerOrderRows.swift @@ -0,0 +1,66 @@ +import Foundation + +/// One rendered row of the controller panel, built from either the app-wide order (Settings) or a +/// stream's selection (the HUD), so the panel itself stays a renderer with no knowledge of which one +/// it is showing. +struct ControllerOrderRow: Identifiable, Equatable { + let id: InputDeviceID + let name: String + /// The effective slot, never the position in the shared order. + let slotLabel: String + let detail: String + /// The remembered choice, including for a disconnected row, where the toggle is read-only. + let isEnabled: Bool + let showsEnableToggle: Bool + let isDimmed: Bool +} + +/// The two ways the controller panel's rows are built. Kept out of the view so the strings a player +/// actually reads — and the state each one is derived from — are covered without rendering. +enum ControllerOrderRows { + /// The app-wide order on its own, as the Settings surface shows it: nothing is per-stream here, + /// so every connected device is enabled and its slot is its position in the order. + static func order(_ devices: [ControllerMappingDevice]) -> [ControllerOrderRow] { + devices.enumerated().map { index, device in + let isActive = index < ControllerPlayerOrder.maximumPlayers + return ControllerOrderRow(id: device.id, name: device.name, + slotLabel: isActive ? "PLAYER \(index + 1)" : "WAITING", + detail: isActive ? device.family.label : "Move into the first four to use in the stream", + isEnabled: true, showsEnableToggle: false, isDimmed: false) + } + } + + /// A stream's selection: the effective player each enabled device occupies, then the remembered + /// rows for whatever is disabled, waiting or gone. `families` supplies the type label for the + /// devices the registry still knows about; a disconnected row keeps only its remembered name. + static func stream(_ rows: [ControllerStreamSelection.Row], families: [InputDeviceID: ControllerFamily]) -> [ControllerOrderRow] { + rows.map { row in + ControllerOrderRow(id: row.id, name: row.name, + slotLabel: slotLabel(for: row.state), + detail: detail(for: row.state, family: families[row.id]), + isEnabled: row.isEnabled, + showsEnableToggle: row.state != .disconnected, + isDimmed: row.state == .disconnected) + } + } + + static func slotLabel(for state: ControllerStreamSelection.RowState) -> String { + switch state { + case .active(let playerIndex): "PLAYER \(playerIndex + 1)" + case .waiting: "WAITING" + case .disabled: "OFF" + case .newlyIdentified: "NEW" + case .disconnected: "OFFLINE" + } + } + + static func detail(for state: ControllerStreamSelection.RowState, family: ControllerFamily?) -> String { + switch state { + case .active: family?.label ?? "Controller" + case .waiting: "Waiting — four players already active" + case .disabled: "Disabled in this stream" + case .newlyIdentified: "New controller — enable in this stream" + case .disconnected: "Disconnected — remembered for this session" + } + } +} diff --git a/ViewModel/Stream/NativeNVSTHostInput.swift b/ViewModel/Stream/NativeNVSTHostInput.swift index 8477f4700..7fc1d2bb5 100644 --- a/ViewModel/Stream/NativeNVSTHostInput.swift +++ b/ViewModel/Stream/NativeNVSTHostInput.swift @@ -259,6 +259,9 @@ extension NativeNVSTHostViewModel { self.onScreenKeyboard.handleSteamSnapshot(deviceID: deviceID, snapshot: snapshot) return true } + view.onStreamControllerArrival = { [weak self] name in + self?.announceStreamControllerArrival(name) + } onScreenKeyboard.onOutput = { [weak self] output in self?.sendOnScreenKeyboardOutput(output) } @@ -285,6 +288,13 @@ extension NativeNVSTHostViewModel { } } + /// A controller that joined mid-stream is admitted disabled, so the notice says what to do + /// rather than implying it is already playing. + private func announceStreamControllerArrival(_ name: String) { + guard isConnected, !isEnding, !didEnd else { return } + showNativeTransientStreamMessage("\(name) connected — enable it in this stream") + } + /// The sheet consumes the settings-focus command vocabulary, not the HUD's step type. private static func mappingSheetCommand(for step: StreamHUDGamepadTracker.NavigationStep) -> ControllerInputCommand { switch step { diff --git a/ViewModel/Stream/StreamControllerSelectionContext.swift b/ViewModel/Stream/StreamControllerSelectionContext.swift new file mode 100644 index 000000000..1e6f61223 --- /dev/null +++ b/ViewModel/Stream/StreamControllerSelectionContext.swift @@ -0,0 +1,27 @@ +import Foundation + +/// The stream-local half of the controller panel: the monitor that owns this session's admission +/// decision, handed to the HUD's controller sheet so its toggles edit *this* stream only. +/// +/// Deliberately a thin handle rather than a second copy of the state. The monitor is the owner, the +/// panel only reads its rows and asks for a change; a stream that has not resolved its surface yet +/// has no context and the panel falls back to the app-wide order-only presentation. +@MainActor +struct StreamControllerSelectionContext { + let monitor: NativeGamepadMonitor + + var rows: [ControllerStreamSelection.Row] { monitor.streamControllerRows } + + func setEnabled(_ enabled: Bool, for deviceID: InputDeviceID) { + monitor.setStreamEnabled(enabled, for: deviceID) + } +} + +@MainActor +extension NativeNVSTHostViewModel { + /// Nil until the surface resolves — the panel then behaves exactly as it does in Settings. + var streamControllerSelection: StreamControllerSelectionContext? { + guard let nativeView else { return nil } + return StreamControllerSelectionContext(monitor: nativeView.gamepadMonitor) + } +} From 270160060ac138328548ef189a9a3a76240a1fb6 Mon Sep 17 00:00:00 2001 From: Anderson Shindy Oki Date: Sun, 11 Oct 2026 23:36:11 +0900 Subject: [PATCH 02/22] refactor: tighten the per-stream controller model and monitor seams --- Model/Stream/ControllerStreamSelection.swift | 101 ++++---- OPN/Stream/ControllerMappingDevices.swift | 10 +- .../NativeGamepadMonitor+Ordering.swift | 41 ---- ...NativeGamepadMonitor+StreamSelection.swift | 40 ---- OPN/Stream/NativeGamepadMonitor.swift | 217 ++++++++++++------ .../ControllerOrderNavigationTests.swift | 2 +- Tests/Settings/ControllerOrderRowsTests.swift | 38 +-- .../Stream/ControllerStreamRoutingTests.swift | 30 +-- .../ControllerStreamSelectionTests.swift | 14 +- View/Settings/ControllerOrderView.swift | 25 +- .../ControllerOrderNavigationModel.swift | 9 +- ViewModel/Settings/ControllerOrderRows.swift | 28 ++- .../StreamControllerSelectionContext.swift | 13 +- 13 files changed, 273 insertions(+), 295 deletions(-) delete mode 100644 OPN/Stream/NativeGamepadMonitor+StreamSelection.swift diff --git a/Model/Stream/ControllerStreamSelection.swift b/Model/Stream/ControllerStreamSelection.swift index 47010ff21..589fb09a2 100644 --- a/Model/Stream/ControllerStreamSelection.swift +++ b/Model/Stream/ControllerStreamSelection.swift @@ -1,26 +1,18 @@ import Foundation -/// One live stream's controller admission: which connected devices this stream lets play, which of -/// them hold a Player 1–4 slot, and the session history the HUD renders beside them. -/// -/// The app-wide `ControllerMappingDevices` registry owns the *order* every stream shares; this type -/// owns only the per-stream decision layered on top of it. Two concurrent streams therefore enable -/// different subsets of one order without either one mutating the registry or an app-wide default, -/// and neither one can reorder or disconnect the other's hardware. -/// -/// Connection, enablement and effective slot are three separate facts here. A device is `known` -/// once this session has seen it, `enabled` only after an explicit admission decision, and holds a -/// slot only while it is both enabled and connected inside the first four of the shared order. +/// One live stream's controller admission: the devices it lets play, the Player 1–4 slots they hold, +/// and the session history the HUD renders. The shared registry still owns the order every stream +/// uses; this type owns only the per-stream decision on top of it. struct ControllerStreamSelection: Equatable, Sendable { - /// A device as the shared registry currently reports it. `name` is remembered for the session so - /// a row can still be labelled after the hardware is gone. + /// A device as the shared registry reports it. The name is remembered for the session, so a row + /// can still be labelled after the hardware is gone. struct Device: Equatable, Sendable { let id: InputDeviceID let name: String } - /// What the HUD shows for one row. The player index is the *effective* slot, never the position - /// in the global order — the two differ as soon as anything before it is disabled. + /// What the HUD shows for one row. The player index is the effective slot, never the position in + /// the shared order — the two differ as soon as anything ahead of it is disabled. enum RowState: Equatable, Sendable { case active(playerIndex: Int) case waiting @@ -32,7 +24,7 @@ struct ControllerStreamSelection: Equatable, Sendable { struct Row: Equatable, Identifiable, Sendable { let id: InputDeviceID let name: String - /// The remembered choice, including for a disconnected row, where it is read-only. + /// The remembered choice, read-only once the device is disconnected. let isEnabled: Bool let state: RowState @@ -42,25 +34,24 @@ struct ControllerStreamSelection: Equatable, Sendable { } } - static let maximumPlayers = ControllerPlayerOrder.maximumPlayers - - private(set) var hasBegunSession = false + /// A device is known once this session has seen it, enabled only after an explicit decision, and + /// holds a slot only while it is both enabled and connected inside the first four of the order. + private(set) var isSessionStarted = false private var names: [InputDeviceID: String] = [:] private var knownIDs: Set = [] private var enabledIDs: Set = [] /// Connected devices first seen after the session began, still awaiting an explicit decision. private var newlyIdentifiedIDs: Set = [] - /// The shared order as of the last synchronization, so the rows and slots read one snapshot. + /// The shared order as of the last synchronization, so rows and slots read one snapshot. private var connectedIDs: [InputDeviceID] = [] - /// This session's history: known devices that are no longer connected, oldest name first. + /// This session's history: known devices that are no longer connected. private var disconnectedIDs: [InputDeviceID] = [] var isEmpty: Bool { knownIDs.isEmpty } - /// Opens the session. Everything connected when the stream starts is admitted enabled and - /// remembered, which is what makes a later reconnect able to restore this stream's choice. + /// Opens the session with everything connected at stream launch admitted enabled and remembered. mutating func beginSession(devices: [Device]) { - hasBegunSession = true + isSessionStarted = true names = [:] knownIDs = [] enabledIDs = [] @@ -74,50 +65,40 @@ struct ControllerStreamSelection: Equatable, Sendable { disconnectedIDs = [] } - /// Folds in the current discovery set, in shared order. - /// - /// A device this session already knows keeps its choice, whether it is connected or was - /// disconnected earlier. A device seen for the first time mid-session is admitted disabled and - /// requires an explicit enable; it is never matched to an earlier device by name or model, so a - /// changed identity becomes a new row rather than inheriting a selection. Returns the arrivals - /// so the caller can announce them. + /// Folds the current discovery set, in shared order, into this session: a known device keeps its + /// choice, a first-seen one is admitted disabled, and the arrivals come back to be announced. @discardableResult mutating func synchronize(devices: [Device]) -> [Device] { - guard hasBegunSession else { + guard isSessionStarted else { beginSession(devices: devices) return [] } - var arrivals: [Device] = [] - for device in devices { - names[device.id] = device.name - guard !knownIDs.contains(device.id) else { continue } - knownIDs.insert(device.id) - enabledIDs.remove(device.id) - newlyIdentifiedIDs.insert(device.id) - arrivals.append(device) + let arrivals = devices.filter { !knownIDs.contains($0.id) } + for device in devices { names[device.id] = device.name } + for arrival in arrivals { + knownIDs.insert(arrival.id) + enabledIDs.remove(arrival.id) + newlyIdentifiedIDs.insert(arrival.id) } connectedIDs = devices.map(\.id) - let connected = Set(connectedIDs) - disconnectedIDs = knownIDs.subtracting(connected).sorted { $0.rawValue < $1.rawValue } + disconnectedIDs = knownIDs.subtracting(connectedIDs).sorted { $0.rawValue < $1.rawValue } return arrivals } /// Enabling clears the new-device prompt; disabling keeps the row and the remembered choice, so /// the same device can be re-enabled without a reconnect. Returns whether anything changed. @discardableResult - mutating func setEnabled(_ enabled: Bool, for id: InputDeviceID) -> Bool { + mutating func setEnabled(_ isEnabled: Bool, for id: InputDeviceID) -> Bool { guard knownIDs.contains(id) else { return false } - if enabled { - newlyIdentifiedIDs.remove(id) - return enabledIDs.insert(id).inserted - } - return enabledIDs.remove(id) != nil + if !isEnabled { return enabledIDs.remove(id) != nil } + newlyIdentifiedIDs.remove(id) + return enabledIDs.insert(id).inserted } func isEnabled(_ id: InputDeviceID) -> Bool { enabledIDs.contains(id) } func isNewlyIdentified(_ id: InputDeviceID) -> Bool { newlyIdentifiedIDs.contains(id) } - func name(for id: InputDeviceID) -> String? { names[id] } func isConnected(_ id: InputDeviceID) -> Bool { connectedIDs.contains(id) } + func name(for id: InputDeviceID) -> String? { names[id] } /// Enabled and connected, in the shared global order. This is the whole admitted set, not just /// the four that fit: everything past the fourth is the HUD's Waiting rows. @@ -125,10 +106,11 @@ struct ControllerStreamSelection: Equatable, Sendable { connectedIDs.filter { enabledIDs.contains($0) } } - /// The effective Player 1–4 map: the first four admitted devices, in shared order, compacted so - /// disabling one promotes the next waiting device rather than leaving a gap. + /// The effective Player 1–4 map: the first four admitted devices, compacted so disabling one + /// promotes the next waiting device rather than leaving a gap. var playerIndices: [InputDeviceID: Int] { - Dictionary(uniqueKeysWithValues: activeDeviceIDs.prefix(Self.maximumPlayers).enumerated().map { ($0.element, $0.offset) }) + let admitted = activeDeviceIDs.prefix(ControllerPlayerOrder.maximumPlayers) + return Dictionary(uniqueKeysWithValues: admitted.enumerated().map { ($0.element, $0.offset) }) } func playerIndex(for id: InputDeviceID) -> Int? { playerIndices[id] } @@ -136,14 +118,17 @@ struct ControllerStreamSelection: Equatable, Sendable { /// Connected rows in shared order, then this session's disconnected history. var rows: [Row] { let slots = playerIndices - var rows = connectedIDs.map { id in - Row(id: id, name: names[id] ?? "Controller", isEnabled: enabledIDs.contains(id), - state: rowState(for: id, slots: slots)) + let connected = connectedIDs.map { id in + makeRow(id, isEnabled: enabledIDs.contains(id), state: rowState(for: id, slots: slots)) } - rows += disconnectedIDs.map { id in - Row(id: id, name: names[id] ?? "Controller", isEnabled: enabledIDs.contains(id), state: .disconnected) + let history = disconnectedIDs.map { id in + makeRow(id, isEnabled: enabledIDs.contains(id), state: .disconnected) } - return rows + return connected + history + } + + private func makeRow(_ id: InputDeviceID, isEnabled: Bool, state: RowState) -> Row { + Row(id: id, name: names[id] ?? "Unknown controller", isEnabled: isEnabled, state: state) } private func rowState(for id: InputDeviceID, slots: [InputDeviceID: Int]) -> RowState { diff --git a/OPN/Stream/ControllerMappingDevices.swift b/OPN/Stream/ControllerMappingDevices.swift index 8cbeac856..6b9d7a50e 100644 --- a/OPN/Stream/ControllerMappingDevices.swift +++ b/OPN/Stream/ControllerMappingDevices.swift @@ -18,9 +18,8 @@ struct ControllerMappingDevice: Equatable, Identifiable, Sendable { final class ControllerMappingDevices: ObservableObject { static let shared = ControllerMappingDevices() - /// Where this registry looks for hardware. The live defaults are the real GameController and - /// Steam HID monitors; a test substitutes a scripted list so selection and routing can be - /// exercised end to end without hardware attached. + /// Where this registry looks for hardware: the real monitors live, a scripted list in a test, so + /// selection and routing can be exercised end to end without hardware attached. @MainActor struct Discovery { var nativeControllers: () -> [GCController] @@ -32,7 +31,7 @@ final class ControllerMappingDevices: ObservableObject { ) } - var discovery = Discovery.live + let discovery: Discovery @Published private(set) var devices: [ControllerMappingDevice] = [] @Published private(set) var playerOrder = ControllerPlayerOrder() private let orderChanges = PassthroughSubject() @@ -64,7 +63,8 @@ final class ControllerMappingDevices: ObservableObject { private var controllers: [InputDeviceID: GCController] = [:] nonisolated(unsafe) private var observers: [NSObjectProtocol] = [] - init() { + init(discovery: Discovery = .live) { + self.discovery = discovery for name in [Notification.Name.GCControllerDidConnect, .GCControllerDidDisconnect] { observers.append(NotificationCenter.default.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in MainActor.assumeIsolated { self?.refresh() } diff --git a/OPN/Stream/NativeGamepadMonitor+Ordering.swift b/OPN/Stream/NativeGamepadMonitor+Ordering.swift index f1c8c6926..6579a87da 100644 --- a/OPN/Stream/NativeGamepadMonitor+Ordering.swift +++ b/OPN/Stream/NativeGamepadMonitor+Ordering.swift @@ -1,47 +1,6 @@ import Foundation extension NativeGamepadMonitor { - /// Every haptic this monitor owns, released together. Teardown and a wholesale slot reset want - /// this; a per-stream disable wants the narrowed form below so it cannot take another stream's - /// contribution with it. - public func stopHaptics() { - hapticStates.values.forEach { $0.stop() } - hapticStates.removeAll() - for (deviceID, task) in steamRumbleStopTasks { - task.cancel() - SteamControllerHIDMonitor.shared.sendRumble(deviceID: deviceID, leftAmplitude: 0, rightAmplitude: 0) - } - steamRumbleStopTasks.removeAll() - } - - /// Neutralizes the slots that are going away, then releases their owned mapped outputs, before - /// the replacement slot map is published. - /// - /// `keepingSteamSlots`/`keepingControllerSlots` are the *new* maps: when the caller has them, the - /// haptic release is narrowed to the devices that actually lost their slot, so disabling one - /// controller in this stream cannot stop an unrelated pad's rumble. Passing neither keeps the - /// teardown behaviour of stopping every haptic this monitor owns. - func prepareForControllerSlotChange(keepingSteamSlots: [InputDeviceID: Int]? = nil, - keepingControllerSlots: [ObjectIdentifier: Int]? = nil) { - if let keepingSteamSlots, let keepingControllerSlots { - stopHaptics(keepingSteamSlots: keepingSteamSlots, keepingControllerSlots: keepingControllerSlots) - } else { - stopHaptics() - } - reapplyTasks.values.forEach { $0.cancel() } - reapplyTasks.removeAll() - let releases = pollingQueue.sync { pollState.prepareForSlotChange() } - for event in releases { emitInputEvent(event) } - for (id, slot) in pollState.steamControllerSlots.sorted(by: { $0.value < $1.value }) { - if bindingEngines[id] != nil { - releaseSteamBinding(deviceID: id, playerIndex: slot) - } else { - emitInputEvent(.gamepad(GamepadState(deviceID: id, playerIndex: slot, - timestamp: MediaTimestamp(nanoseconds: DispatchTime.now().uptimeNanoseconds)))) - } - } - } - func emitCurrentSteamStates() { for (id, slot) in pollState.steamControllerSlots.sorted(by: { $0.value < $1.value }) { guard let snapshot = SteamControllerHIDMonitor.shared.snapshot(for: id) else { continue } diff --git a/OPN/Stream/NativeGamepadMonitor+StreamSelection.swift b/OPN/Stream/NativeGamepadMonitor+StreamSelection.swift deleted file mode 100644 index b8d35341d..000000000 --- a/OPN/Stream/NativeGamepadMonitor+StreamSelection.swift +++ /dev/null @@ -1,40 +0,0 @@ -import Foundation - -/// One stream's controller admission: the HUD's rows, the toggle that edits them, and the haptic -/// release that follows a device losing its slot. -/// -/// Kept in its own extension so the per-stream feature does not grow `NativeGamepadMonitor`'s own -/// body past the repository's size budget; the storage still lives with the monitor that owns it. -extension NativeGamepadMonitor { - /// The HUD's rows for this stream: effective player per enabled device, plus the remembered - /// rows for whatever is disabled, waiting or gone. - var streamControllerRows: [ControllerStreamSelection.Row] { streamSelection.rows } - - /// Enables or disables one device for this stream only. The slot map, topology, mapped outputs - /// and rumble routing are all recomputed by the refresh, which neutralizes the old slot before - /// it publishes the replacement. - @discardableResult - func setStreamEnabled(_ enabled: Bool, for deviceID: InputDeviceID) -> Bool { - guard streamSelection.setEnabled(enabled, for: deviceID) else { return false } - OPNStreamTelemetry.capture("webrtc.input.gamepad.stream_selection", level: .info, - message: "Controller enablement changed for this stream.", - attributes: ["device": deviceID.rawValue, "enabled": String(enabled)]) - refreshControllerSlots() - return true - } - - /// Releases haptics only for the devices this stream no longer routes. A device disabled here - /// must stop vibrating for this stream without stopping an unrelated device's rumble, and - /// without reaching into another stream's monitor — which owns its own haptic state and its own - /// pending stops for the same physical hardware. - func stopHaptics(keepingSteamSlots: [InputDeviceID: Int], keepingControllerSlots: [ObjectIdentifier: Int]) { - for key in Array(hapticStates.keys) where keepingControllerSlots[key] == nil { - hapticStates[key]?.stop() - hapticStates.removeValue(forKey: key) - } - for deviceID in Array(steamRumbleStopTasks.keys) where keepingSteamSlots[deviceID] == nil { - steamRumbleStopTasks.removeValue(forKey: deviceID)?.cancel() - SteamControllerHIDMonitor.shared.sendRumble(deviceID: deviceID, leftAmplitude: 0, rightAmplitude: 0) - } - } -} diff --git a/OPN/Stream/NativeGamepadMonitor.swift b/OPN/Stream/NativeGamepadMonitor.swift index b0eb34fac..4f53e2fd6 100644 --- a/OPN/Stream/NativeGamepadMonitor.swift +++ b/OPN/Stream/NativeGamepadMonitor.swift @@ -96,11 +96,9 @@ public final class NativeGamepadMonitor { private var chordTracker = StreamOSKChordTracker() private var guideTapTracker = SteamGuideTapTracker() private var onScreenKeyboardCapturedDevices: Set = [] - /// Internal, not private: the stream-selection extension narrows a haptic release to the - /// devices that actually lost their slot. - var hapticStates: [ObjectIdentifier: ControllerHapticState] = [:] + private var hapticStates: [ObjectIdentifier: ControllerHapticState] = [:] /// Pending "motors off" for each Steam Controller currently rumbling. - var steamRumbleStopTasks: [InputDeviceID: Task] = [:] + private var steamRumbleStopTasks: [InputDeviceID: Task] = [:] private var steamRumbleCommandsSent = 0 private var accessibilityPromptShown = false @@ -118,11 +116,9 @@ public final class NativeGamepadMonitor { /// host turns this into the brief arrival notice; admission itself still needs the HUD toggle. public var onStreamControllerArrival: ((String) -> Void)? - /// This stream's admission decision, layered over the shared order. Owned here rather than in - /// the registry so a second stream's selection cannot be read or written by this one, and - /// released with the monitor when the stream ends. Written only through `setStreamEnabled` in - /// the stream-selection extension. - var streamSelection = ControllerStreamSelection() + /// This stream's admission decision, layered over the shared order; written only through + /// `setStreamEnabled`, so another stream's selection cannot reach it. + private(set) var streamSelection = ControllerStreamSelection() let mappingProvider: any ControllerMappingProviding /// The shared order and device list this monitor reads. Injected so a test can script discovery; @@ -349,67 +345,6 @@ public final class NativeGamepadMonitor { return (nativePaths + steamPaths).sorted { $0.playerIndex < $1.playerIndex } } - /// Internal, not private: `setStreamEnabled` in the stream-selection extension re-runs it after - /// changing this stream's admission. - func refreshControllerSlots() { - let previousSteamSlots = pollState.steamControllerSlots - let previousControllerSlots = pollState.controllerSlots - registry.refresh() - let arrivals = streamSelection.synchronize(devices: registry.orderedDevices.map { - ControllerStreamSelection.Device(id: $0.id, name: $0.name) - }) - for arrival in arrivals { onStreamControllerArrival?(arrival.name) } - let nativeIDs = registry.devices.reduce(into: [InputDeviceID: ObjectIdentifier]()) { result, device in - if let controller = registry.controller(for: device.id) { result[device.id] = ObjectIdentifier(controller) } - } - // The shared order is filtered to this stream's admitted devices *before* the four-slot - // limit, so disabling a device compacts the remaining players instead of leaving a hole. - // Read the Steam membership off the registry rather than the HID monitor so the filtered - // assignment is built from one discovery snapshot — and so a scripted registry is honored. - let steamIDs = Set(registry.devices.filter { $0.family == .steam }.map(\.id)) - let assignments = ControllerSlotAssignments(deviceIDs: streamSelection.activeDeviceIDs, - steamIDs: steamIDs, nativeIDs: nativeIDs) - let newSteamSlots = assignments.steam - let newControllerSlots = assignments.native - let cachedControllers = registry.orderedDevices.compactMap { registry.controller(for: $0.id) } - .filter { newControllerSlots[ObjectIdentifier($0)] != nil } - if newSteamSlots != previousSteamSlots || newControllerSlots != previousControllerSlots { - prepareForControllerSlotChange(keepingSteamSlots: newSteamSlots, keepingControllerSlots: newControllerSlots) - } - pollingQueue.sync { - pollState.controllerSlots = newControllerSlots - pollState.steamControllerSlots = newSteamSlots - pollState.cachedControllers = cachedControllers - } - for deviceID in previousSteamSlots.keys where newSteamSlots[deviceID] == nil { - chordTracker.removeDevice(deviceID) - guideTapTracker.removeDevice(deviceID) - onScreenKeyboardCapturedDevices.remove(deviceID) - } - let staleCursorDeviceIDs = localCursorModeHeld.filter { newSteamSlots[$0] == nil } - if !staleCursorDeviceIDs.isEmpty { - localCursorModeHeld.subtract(staleCursorDeviceIDs) - SteamControllerLocalCursorInjector.shared.reset() - } - refreshBatteryLabels() - let hapticPlayerIndices = pollState.cachedControllers.compactMap { controller in - controller.haptics == nil ? nil : newControllerSlots[ObjectIdentifier(controller)] - } - let allPlayerIndices = Array(newControllerSlots.values) + Array(newSteamSlots.values) - let newTopology = StreamGamepadTopology(playerIndices: allPlayerIndices, hapticPlayerIndices: hapticPlayerIndices) - if topology != newTopology { - topology = newTopology - onTopologyChanged?(newTopology) - } - refreshMappingConfiguration(replaySteam: false) - if pollingAllowed { - emitCurrentSteamStates() - newControllerSlots.isEmpty ? stopPollingTimer() : startPollingTimer() - } - let totalSlots = newControllerSlots.count + newSteamSlots.count - OPNStreamTelemetry.capture("webrtc.input.gamepad.controllers", level: .info, message: "Detected \(totalSlots) controller(s).", attributes: ["connected": String(totalSlots), "steam": String(newSteamSlots.count)]) - } - private func refreshBatteryLabels() { var slots = Dictionary(uniqueKeysWithValues: pollState.steamControllerSlots.map { ($0.key.rawValue, $0.value) }) for (id, slot) in pollState.controllerSlots { slots["native-\(id.hashValue)"] = slot } @@ -563,8 +498,148 @@ public final class NativeGamepadMonitor { } } +// MARK: - Slot publication and per-stream admission + +/// The slot map, its transport-visible consequences, and the stream-local enablement that filters +/// it. An extension in this file, so it still reaches the private slot, haptic and cursor state. +extension NativeGamepadMonitor { + /// Enables or disables one device for this stream only. + @discardableResult + func setStreamEnabled(_ isEnabled: Bool, for deviceID: InputDeviceID) -> Bool { + var next = streamSelection + guard next.setEnabled(isEnabled, for: deviceID) else { return false } + streamSelection = next + OPNStreamTelemetry.capture("webrtc.input.gamepad.stream_selection", level: .info, + message: "Controller enablement changed for this stream.", + attributes: ["device": deviceID.rawValue, "enabled": String(isEnabled)]) + refreshControllerSlots() + return true + } + + /// The HUD's rows for this stream: effective player per enabled device, plus the remembered rows + /// for whatever is disabled, waiting or gone. + var streamControllerRows: [ControllerStreamSelection.Row] { streamSelection.rows } + + /// Neutralizes the slots that are going away, then releases their owned mapped outputs, before + /// the replacement slot map is published. + func prepareForControllerSlotChange() { + stopHaptics() + releaseControllerSlots() + } + + /// The same transition with the replacement maps in hand, so a device that keeps its slot keeps + /// its haptics and only the devices that lost one are released. + func prepareForControllerSlotChange(keepingSteamSlots: [InputDeviceID: Int], + keepingControllerSlots: [ObjectIdentifier: Int]) { + stopHaptics(keepingSteamSlots: keepingSteamSlots, keepingControllerSlots: keepingControllerSlots) + releaseControllerSlots() + } + + /// Every haptic this monitor owns, released together. + public func stopHaptics() { + hapticStates.values.forEach { $0.stop() } + hapticStates.removeAll() + for (deviceID, task) in steamRumbleStopTasks { + task.cancel() + SteamControllerHIDMonitor.shared.sendRumble(deviceID: deviceID, leftAmplitude: 0, rightAmplitude: 0) + } + steamRumbleStopTasks.removeAll() + } + + private func releaseControllerSlots() { + reapplyTasks.values.forEach { $0.cancel() } + reapplyTasks.removeAll() + let releases = pollingQueue.sync { pollState.prepareForSlotChange() } + for event in releases { emitInputEvent(event) } + for (id, slot) in pollState.steamControllerSlots.sorted(by: { $0.value < $1.value }) { + if bindingEngines[id] != nil { + releaseSteamBinding(deviceID: id, playerIndex: slot) + continue + } + emitInputEvent(.gamepad(GamepadState(deviceID: id, playerIndex: slot, + timestamp: MediaTimestamp(nanoseconds: DispatchTime.now().uptimeNanoseconds)))) + } + } + + /// Releases haptics only for the devices this stream no longer routes, so disabling one pad here + /// cannot stop an unrelated pad's rumble or another stream's contribution. + private func stopHaptics(keepingSteamSlots: [InputDeviceID: Int], keepingControllerSlots: [ObjectIdentifier: Int]) { + for key in Array(hapticStates.keys) where keepingControllerSlots[key] == nil { + hapticStates[key]?.stop() + hapticStates.removeValue(forKey: key) + } + for deviceID in Array(steamRumbleStopTasks.keys) where keepingSteamSlots[deviceID] == nil { + steamRumbleStopTasks.removeValue(forKey: deviceID)?.cancel() + SteamControllerHIDMonitor.shared.sendRumble(deviceID: deviceID, leftAmplitude: 0, rightAmplitude: 0) + } + } + + private func refreshControllerSlots() { + let previousSteamSlots = pollState.steamControllerSlots + let previousControllerSlots = pollState.controllerSlots + registry.refresh() + for arrival in synchronizeStreamSelection() { onStreamControllerArrival?(arrival.name) } + let nativeIDs = registry.devices.reduce(into: [InputDeviceID: ObjectIdentifier]()) { result, device in + if let controller = registry.controller(for: device.id) { result[device.id] = ObjectIdentifier(controller) } + } + // The shared order is filtered to this stream's admitted devices *before* the four-slot limit, + // so a disabled device compacts the remaining players instead of leaving a hole behind it. + let steamIDs = Set(registry.devices.filter { $0.family == .steam }.map(\.id)) + let assignments = ControllerSlotAssignments(deviceIDs: streamSelection.activeDeviceIDs, + steamIDs: steamIDs, nativeIDs: nativeIDs) + let newSteamSlots = assignments.steam + let newControllerSlots = assignments.native + let cachedControllers = registry.orderedDevices.compactMap { registry.controller(for: $0.id) } + .filter { newControllerSlots[ObjectIdentifier($0)] != nil } + if newSteamSlots != previousSteamSlots || newControllerSlots != previousControllerSlots { + prepareForControllerSlotChange(keepingSteamSlots: newSteamSlots, keepingControllerSlots: newControllerSlots) + } + pollingQueue.sync { + pollState.controllerSlots = newControllerSlots + pollState.steamControllerSlots = newSteamSlots + pollState.cachedControllers = cachedControllers + } + for deviceID in previousSteamSlots.keys where newSteamSlots[deviceID] == nil { + chordTracker.removeDevice(deviceID) + guideTapTracker.removeDevice(deviceID) + onScreenKeyboardCapturedDevices.remove(deviceID) + } + let staleCursorDeviceIDs = localCursorModeHeld.filter { newSteamSlots[$0] == nil } + if !staleCursorDeviceIDs.isEmpty { + localCursorModeHeld.subtract(staleCursorDeviceIDs) + SteamControllerLocalCursorInjector.shared.reset() + } + refreshBatteryLabels() + let hapticPlayerIndices = pollState.cachedControllers.compactMap { controller in + controller.haptics == nil ? nil : newControllerSlots[ObjectIdentifier(controller)] + } + let allPlayerIndices = Array(newControllerSlots.values) + Array(newSteamSlots.values) + let newTopology = StreamGamepadTopology(playerIndices: allPlayerIndices, hapticPlayerIndices: hapticPlayerIndices) + if topology != newTopology { + topology = newTopology + onTopologyChanged?(newTopology) + } + refreshMappingConfiguration(replaySteam: false) + if pollingAllowed { + emitCurrentSteamStates() + newControllerSlots.isEmpty ? stopPollingTimer() : startPollingTimer() + } + let totalSlots = newControllerSlots.count + newSteamSlots.count + OPNStreamTelemetry.capture("webrtc.input.gamepad.controllers", level: .info, message: "Detected \(totalSlots) controller(s).", attributes: ["connected": String(totalSlots), "steam": String(newSteamSlots.count)]) + } + + /// Folds current discovery into this stream's selection and returns whatever arrived new. + private func synchronizeStreamSelection() -> [ControllerStreamSelection.Device] { + let devices = registry.orderedDevices.map { ControllerStreamSelection.Device(id: $0.id, name: $0.name) } + var next = streamSelection + let arrivals = next.synchronize(devices: devices) + streamSelection = next + return arrivals + } +} + @MainActor -final class ControllerHapticState { +private final class ControllerHapticState { private let controller: GCController private var engines: [GCHapticsLocality: CHHapticEngine] = [:] private var players: [GCHapticsLocality: any CHHapticPatternPlayer] = [:] diff --git a/Tests/Settings/ControllerOrderNavigationTests.swift b/Tests/Settings/ControllerOrderNavigationTests.swift index d27677006..c369c4a74 100644 --- a/Tests/Settings/ControllerOrderNavigationTests.swift +++ b/Tests/Settings/ControllerOrderNavigationTests.swift @@ -40,7 +40,7 @@ import Testing @Test func streamPanelConfirmTogglesEnablementInsteadOfCommittingAMove() { let navigation = ControllerOrderNavigationModel() - navigation.activateToggles = true + navigation.isConfirmTogglingEnablement = true let ids: [InputDeviceID] = ["first", "second"] navigation.reconcile(ids) #expect(navigation.process(.activate, deviceIDs: ids) == .toggle("first")) diff --git a/Tests/Settings/ControllerOrderRowsTests.swift b/Tests/Settings/ControllerOrderRowsTests.swift index 70e3116b9..fbc5cac15 100644 --- a/Tests/Settings/ControllerOrderRowsTests.swift +++ b/Tests/Settings/ControllerOrderRowsTests.swift @@ -5,60 +5,60 @@ import Testing /// What the controller panel actually shows. These are the strings and states a player reads off the /// HUD, so they are asserted here rather than left to a rendered snapshot. @Suite struct ControllerOrderRowsTests { - private func streamRow(_ id: InputDeviceID, _ name: String = "Pad", enabled: Bool, + private func streamRow(_ id: InputDeviceID, _ name: String = "Pad", isEnabled: Bool, state: ControllerStreamSelection.RowState) -> ControllerStreamSelection.Row { - ControllerStreamSelection.Row(id: id, name: name, isEnabled: enabled, state: state) + ControllerStreamSelection.Row(id: id, name: name, isEnabled: isEnabled, state: state) } @Test func activeEnabledDeviceShowsOnWithItsEffectivePlayerNumber() { - let rows = ControllerOrderRows.stream([streamRow("a", "DualSense", enabled: true, state: .active(playerIndex: 1))], + let rows = ControllerOrderRows.stream([streamRow("a", "DualSense", isEnabled: true, state: .active(playerIndex: 1))], families: ["a": .dualShock4]) #expect(rows.first?.slotLabel == "PLAYER 2") - #expect(rows.first?.detail == ControllerFamily.dualShock4.label) + #expect(rows.first?.statusDetail == ControllerFamily.dualShock4.label) #expect(rows.first?.isEnabled == true) - #expect(rows.first?.showsEnableToggle == true) + #expect(rows.first?.isEnableToggleShown == true) #expect(rows.first?.isDimmed == false) } @Test func enabledOverflowDeviceShowsWaiting() { - let rows = ControllerOrderRows.stream([streamRow("e", enabled: true, state: .waiting)], families: [:]) + let rows = ControllerOrderRows.stream([streamRow("e", isEnabled: true, state: .waiting)], families: [:]) #expect(rows.first?.slotLabel == "WAITING") - #expect(rows.first?.detail == "Waiting — four players already active") + #expect(rows.first?.statusDetail == "Waiting — four players already active") #expect(rows.first?.isEnabled == true) } - @Test func connectedDisabledDeviceShowsOffAndNoPlayerNumber() { - let rows = ControllerOrderRows.stream([streamRow("b", enabled: false, state: .disabled)], families: [:]) + @Test func connectedDisabledDeviceShowsOffWithoutAPlayerNumber() { + let rows = ControllerOrderRows.stream([streamRow("b", isEnabled: false, state: .disabled)], families: [:]) #expect(rows.first?.slotLabel == "OFF") - #expect(rows.first?.detail == "Disabled in this stream") + #expect(rows.first?.statusDetail == "Disabled in this stream") #expect(rows.first?.isEnabled == false) - #expect(rows.first?.showsEnableToggle == true) + #expect(rows.first?.isEnableToggleShown == true) } @Test func unknownMidStreamArrivalShowsTheNewDevicePrompt() { - let rows = ControllerOrderRows.stream([streamRow("n", enabled: false, state: .newlyIdentified)], families: [:]) + let rows = ControllerOrderRows.stream([streamRow("n", isEnabled: false, state: .newlyIdentified)], families: [:]) #expect(rows.first?.slotLabel == "NEW") - #expect(rows.first?.detail == "New controller — enable in this stream") + #expect(rows.first?.statusDetail == "New controller — enable in this stream") #expect(rows.first?.isEnabled == false) } - @Test func disconnectedDeviceStaysDimmedAndReadOnlyWithTheRememberedChoice() { - let rows = ControllerOrderRows.stream([streamRow("old", "Xbox Controller", enabled: true, state: .disconnected)], families: [:]) + @Test func disconnectedDeviceStaysDimmedWithTheRememberedChoice() { + let rows = ControllerOrderRows.stream([streamRow("old", "Xbox Controller", isEnabled: true, state: .disconnected)], families: [:]) #expect(rows.first?.slotLabel == "OFFLINE") - #expect(rows.first?.detail == "Disconnected — remembered for this session") + #expect(rows.first?.statusDetail == "Disconnected — remembered for this session") #expect(rows.first?.name == "Xbox Controller") #expect(rows.first?.isEnabled == true) - #expect(rows.first?.showsEnableToggle == false) + #expect(rows.first?.isEnableToggleShown == false) #expect(rows.first?.isDimmed == true) } - @Test func settingsRowsKeepTheGlobalOrderAndOfferNoStreamToggle() { + @Test func settingsRowsKeepTheGlobalOrderWithoutAStreamToggle() { let devices = [ ControllerMappingDevice(id: "a", name: "Steam Controller 1", family: .steam, hasTouchpad: false), ControllerMappingDevice(id: "b", name: "DualSense", family: .dualShock4, hasTouchpad: true), ] let rows = ControllerOrderRows.order(devices) #expect(rows.map(\.slotLabel) == ["PLAYER 1", "PLAYER 2"]) - #expect(rows.allSatisfy { $0.showsEnableToggle == false && $0.isEnabled }) + #expect(rows.allSatisfy { $0.isEnableToggleShown == false && $0.isEnabled }) } } diff --git a/Tests/Stream/ControllerStreamRoutingTests.swift b/Tests/Stream/ControllerStreamRoutingTests.swift index 0a9eee752..2461ea6ea 100644 --- a/Tests/Stream/ControllerStreamRoutingTests.swift +++ b/Tests/Stream/ControllerStreamRoutingTests.swift @@ -6,10 +6,7 @@ import Testing /// Stream selection seen from the transport boundary: what the seat is told, which slots exist, and /// what happens to held input when a device loses its slot to a disable or a disconnect. -/// -/// Discovery is scripted through `ControllerMappingDevices.Discovery`, so these run without hardware -/// attached. They prove the *model and routing* contract; they say nothing about how reliably real -/// hardware is identified across a reconnect. +/// Discovery is scripted, so these prove the model and routing contract without hardware attached. @MainActor @Suite struct ControllerStreamRoutingTests { private func makeStore() throws -> (store: ControllerMappingStore, defaults: UserDefaults, suite: String) { @@ -23,8 +20,7 @@ import Testing defer { defaults.removePersistentDomain(forName: suite) } let first = GCController.withExtendedGamepad() let second = GCController.withExtendedGamepad() - let registry = ControllerMappingDevices() - registry.discovery = .init(nativeControllers: { [first, second] }, steamDeviceIDs: { [] }) + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [first, second] }, steamDeviceIDs: { [] })) let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) let firstID = try #require(registry.id(for: first)) let secondID = try #require(registry.id(for: second)) @@ -45,12 +41,11 @@ import Testing monitor.stop() } - @Test func everyDeviceDisabledProducesAnEmptyLocalSlotMapAndTopology() throws { + @Test func everyDeviceDisabledProducesAnEmptyLocalSlotMap() throws { let (store, defaults, suite) = try makeStore() defer { defaults.removePersistentDomain(forName: suite) } let first = GCController.withExtendedGamepad() - let registry = ControllerMappingDevices() - registry.discovery = .init(nativeControllers: { [first] }, steamDeviceIDs: { [] }) + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [first] }, steamDeviceIDs: { [] })) let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) let firstID = try #require(registry.id(for: first)) #expect(monitor.topology.playerIndices == [0]) @@ -66,8 +61,7 @@ import Testing defer { defaults.removePersistentDomain(forName: suite) } let first = GCController.withExtendedGamepad() var natives: [GCController] = [first] - let registry = ControllerMappingDevices() - registry.discovery = .init(nativeControllers: { natives }, steamDeviceIDs: { [] }) + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { natives }, steamDeviceIDs: { [] })) let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) let firstID = try #require(registry.id(for: first)) #expect(monitor.inputPaths().map(\.playerIndex) == [0]) @@ -117,10 +111,20 @@ import Testing } private func keyStates(_ events: [UserInputEvent]) -> [Bool] { - events.compactMap { if case .keyboard(let key) = $0 { key.isPressed } else { nil } } + events.compactMap { + switch $0 { + case .keyboard(let key): key.isPressed + default: nil + } + } } private func gamepads(_ events: [UserInputEvent]) -> [GamepadState] { - events.compactMap { if case .gamepad(let state) = $0 { state } else { nil } } + events.compactMap { + switch $0 { + case .gamepad(let state): state + default: nil + } + } } } diff --git a/Tests/Stream/ControllerStreamSelectionTests.swift b/Tests/Stream/ControllerStreamSelectionTests.swift index f5cf9802e..e03f8fbcd 100644 --- a/Tests/Stream/ControllerStreamSelectionTests.swift +++ b/Tests/Stream/ControllerStreamSelectionTests.swift @@ -30,7 +30,7 @@ import Testing #expect(selection.rows.first { $0.id == "b" }?.playerIndex == nil) } - @Test func reEnablingRestoresTheSharedOrderAndSendsTheLastDeviceBackToWaiting() { + @Test func reEnablingSendsTheLastDeviceBackToWaiting() { var selection = ControllerStreamSelection() let order = [device("a"), device("b"), device("c"), device("d"), device("e")] selection.beginSession(devices: order) @@ -68,7 +68,7 @@ import Testing #expect(!selection.isNewlyIdentified("b")) } - @Test func disconnectCompactsTheRemainingEnabledPlayersAndRemembersTheChoice() { + @Test func disconnectCompactsTheRemainingEnabledPlayers() { var selection = ControllerStreamSelection() selection.beginSession(devices: [device("a"), device("b"), device("c")]) _ = selection.synchronize(devices: [device("a"), device("c")]) @@ -102,7 +102,7 @@ import Testing #expect(second.rows.count == 1) } - @Test func reliableReconnectRecalculatesActiveOrWaitingFromTheCurrentOrder() { + @Test func reliableReconnectRecalculatesTheSlotFromTheCurrentOrder() { var selection = ControllerStreamSelection() selection.beginSession(devices: [device("a"), device("b"), device("c"), device("d")]) _ = selection.synchronize(devices: [device("b"), device("c"), device("d")]) @@ -117,7 +117,7 @@ import Testing #expect(selection.rows.first { $0.id == "a" }?.playerIndex == nil) } - @Test func changedIdentityStartsDisabledAndKeepsTheOldHistory() { + @Test func changedIdentityStartsDisabledWithTheOldHistoryRetained() { var selection = ControllerStreamSelection() selection.beginSession(devices: [device("n1", "DualSense")]) let arrivals = selection.synchronize(devices: [device("n2", "DualSense")]) @@ -129,7 +129,7 @@ import Testing #expect(selection.rows.count == 2) } - @Test func identicalLabelsNeverTransferSelectionOrMergeHistory() { + @Test func identicalLabelsNeverTransferSelection() { var selection = ControllerStreamSelection() selection.beginSession(devices: [device("n1", "Xbox Wireless Controller")]) selection.setEnabled(false, for: "n1") @@ -140,7 +140,7 @@ import Testing #expect(selection.rows.count == 2) } - @Test func newSessionDoesNotInheritSelectionOrHistory() { + @Test func newSessionDoesNotInheritSelection() { var previous = ControllerStreamSelection() previous.beginSession(devices: [device("a"), device("b")]) previous.setEnabled(false, for: "a") @@ -163,7 +163,7 @@ import Testing #expect(selection.rows.count == 2) } - @Test func everyDeviceDisabledLeavesNoPlayerAndNoActiveDevice() { + @Test func everyDeviceDisabledLeavesNoActiveDevice() { var selection = ControllerStreamSelection() selection.beginSession(devices: [device("a"), device("b")]) selection.setEnabled(false, for: "a") diff --git a/View/Settings/ControllerOrderView.swift b/View/Settings/ControllerOrderView.swift index a62824726..e4deff35c 100644 --- a/View/Settings/ControllerOrderView.swift +++ b/View/Settings/ControllerOrderView.swift @@ -15,16 +15,16 @@ struct ControllerOrderView: View { var body: some View { let rows = self.rows let size = ControllerOrderPanel.sheetSize(deviceCount: rows.count, uiScale: uiScale) - ControllerOrderPanel(rows: rows, isCustom: devices.playerOrder.isCustom, streamScoped: stream != nil, + ControllerOrderPanel(rows: rows, isCustom: devices.playerOrder.isCustom, isStreamScoped: stream != nil, focusedControl: navigation.focus, - onSetEnabled: { stream?.setEnabled($0, for: $1) }, + onSetStreamEnabled: { stream?.setEnabled($0, for: $1) }, onMove: devices.move, onReset: devices.resetOrder, onClose: { dismiss() }) .frame(minWidth: size.width, idealWidth: size.width, minHeight: size.height, idealHeight: size.height) .background(ControllerOrderWindowReader(reference: inputWindow)) .onExitCommand { dismiss() } .task { devices.refresh() - navigation.activateToggles = stream != nil + navigation.isConfirmTogglingEnablement = stream != nil streamRows = stream?.rows ?? [] SteamControllerHIDMonitor.shared.beginInputCapture(inputWindow) defer { SteamControllerHIDMonitor.shared.endInputCapture(inputWindow) } @@ -42,7 +42,7 @@ struct ControllerOrderView: View { } /// Rows come from the stream's own selection when there is one, so the panel shows the effective - /// player each enabled controller actually occupies rather than its place in the global order. + /// player each enabled controller occupies rather than its place in the global order. private var rows: [ControllerOrderRow] { guard stream != nil else { return ControllerOrderRows.order(devices.orderedDevices) } let families = Dictionary(uniqueKeysWithValues: devices.devices.map { ($0.id, $0.family) }) @@ -81,6 +81,7 @@ struct ControllerOrderView: View { stream.setEnabled(!row.isEnabled, for: id) refreshStreamRows() } + } struct ControllerOrderPanel: View { @@ -92,9 +93,9 @@ struct ControllerOrderPanel: View { let rows: [ControllerOrderRow] let isCustom: Bool /// Whether the panel is editing one stream's selection rather than the app-wide order alone. - let streamScoped: Bool + let isStreamScoped: Bool let focusedControl: ControllerOrderNavigationModel.Focus? - let onSetEnabled: (Bool, InputDeviceID) -> Void + let onSetStreamEnabled: (Bool, InputDeviceID) -> Void let onMove: (InputDeviceID, ControllerPlayerOrder.Direction) -> Void let onReset: () -> Void let onClose: () -> Void @@ -159,20 +160,20 @@ struct ControllerOrderPanel: View { } private var explanation: String { - guard streamScoped else { + guard isStreamScoped else { return "Move controllers to choose Player 1–4. Changes apply immediately; mapping profiles stay with each controller. Remote Co-Op guests are not reordered." } return "Enable controllers in this stream and move them in the shared order. Ordering is global and every stream uses it; the enable toggles affect this stream only. Mapping profiles stay with each controller, and Remote Co-Op guests are not reordered." } private var footerHint: String { - streamScoped + isStreamScoped ? "D-pad: navigate · Left/Right: reorder · Confirm: enable or disable · Back: close" : "D-pad: navigate · Confirm: move · Back: close" } private var footerNote: String { - streamScoped + isStreamScoped ? "Enablement and remembered rows last for this stream session. Controllers connected after the stream starts stay disabled until you enable them. Some games may require reconnecting to recognize new player slots." : "Order lasts for this connection session. New controllers join the end of a custom order. Some games may require reconnecting to recognize new player slots." } @@ -197,13 +198,13 @@ struct ControllerOrderPanel: View { .font(.settingsFont(size: 14 * uiScale, weight: .bold)) .lineLimit(1) .help(row.name) - Text(row.detail) + Text(row.statusDetail) .font(.settingsFont(size: 11 * uiScale)) .foregroundStyle(OPNDesign.Text.tertiary) .fixedSize(horizontal: false, vertical: true) } .frame(maxWidth: .infinity, alignment: .leading) - if row.showsEnableToggle { + if row.isEnableToggleShown { enableToggle(for: row) } moveButton(row: row, direction: .earlier, disabled: index == 0) @@ -218,7 +219,7 @@ struct ControllerOrderPanel: View { /// The stream-scoped control. A disconnected row keeps showing the remembered choice but takes /// no action, so history cannot be edited after the hardware is gone. private func enableToggle(for row: ControllerOrderRow) -> some View { - Toggle(isOn: Binding(get: { row.isEnabled }, set: { onSetEnabled($0, row.id) }), uiScale: uiScale) + Toggle(isOn: Binding(get: { row.isEnabled }, set: { onSetStreamEnabled($0, row.id) }), uiScale: uiScale) .accessibilityLabel("Enabled in this stream") .accessibilityValue(row.isEnabled ? "On" : "Off") .help(row.isEnabled ? "Enabled in this stream — disable it here" : "Disabled in this stream — enable it here") diff --git a/ViewModel/Settings/ControllerOrderNavigationModel.swift b/ViewModel/Settings/ControllerOrderNavigationModel.swift index ab694640c..782b1fc70 100644 --- a/ViewModel/Settings/ControllerOrderNavigationModel.swift +++ b/ViewModel/Settings/ControllerOrderNavigationModel.swift @@ -13,10 +13,9 @@ final class ControllerOrderNavigationModel: ObservableObject { case close } - /// In a stream panel, confirm switches this stream's enablement for the focused controller; on - /// the Settings surface it keeps its original meaning of committing the focused reorder arrow. - /// Left/right stays the reorder axis in both, because ordering is global either way. - var activateToggles = false + /// In a stream panel, confirm toggles this stream's enablement for the focused controller; on + /// the Settings surface it keeps its meaning of committing the focused reorder arrow. + var isConfirmTogglingEnablement = false @Published private(set) var focus: Focus? private let tracker = StreamHUDGamepadTracker() @@ -49,7 +48,7 @@ final class ControllerOrderNavigationModel: ObservableObject { guard let focus, let index = deviceIDs.firstIndex(of: focus.deviceID) else { return nil } switch step { case .activate: - return activateToggles ? .toggle(focus.deviceID) : .move(focus.deviceID, focus.direction) + return isConfirmTogglingEnablement ? .toggle(focus.deviceID) : .move(focus.deviceID, focus.direction) case .move(let direction): switch direction { case .up, .down: diff --git a/ViewModel/Settings/ControllerOrderRows.swift b/ViewModel/Settings/ControllerOrderRows.swift index 1e81bb550..63defaacb 100644 --- a/ViewModel/Settings/ControllerOrderRows.swift +++ b/ViewModel/Settings/ControllerOrderRows.swift @@ -1,45 +1,43 @@ import Foundation /// One rendered row of the controller panel, built from either the app-wide order (Settings) or a -/// stream's selection (the HUD), so the panel itself stays a renderer with no knowledge of which one -/// it is showing. +/// stream's selection (the HUD), so the panel itself stays a renderer. struct ControllerOrderRow: Identifiable, Equatable { let id: InputDeviceID let name: String /// The effective slot, never the position in the shared order. let slotLabel: String - let detail: String - /// The remembered choice, including for a disconnected row, where the toggle is read-only. + let statusDetail: String + /// The remembered choice, read-only once the device is disconnected. let isEnabled: Bool - let showsEnableToggle: Bool + let isEnableToggleShown: Bool let isDimmed: Bool } /// The two ways the controller panel's rows are built. Kept out of the view so the strings a player -/// actually reads — and the state each one is derived from — are covered without rendering. +/// reads, and the state each is derived from, are covered without rendering. enum ControllerOrderRows { - /// The app-wide order on its own, as the Settings surface shows it: nothing is per-stream here, - /// so every connected device is enabled and its slot is its position in the order. + /// The app-wide order alone: nothing here is per-stream, so every connected device is enabled and + /// its slot is simply its position. static func order(_ devices: [ControllerMappingDevice]) -> [ControllerOrderRow] { devices.enumerated().map { index, device in let isActive = index < ControllerPlayerOrder.maximumPlayers return ControllerOrderRow(id: device.id, name: device.name, slotLabel: isActive ? "PLAYER \(index + 1)" : "WAITING", - detail: isActive ? device.family.label : "Move into the first four to use in the stream", - isEnabled: true, showsEnableToggle: false, isDimmed: false) + statusDetail: isActive ? device.family.label : "Move into the first four to use in the stream", + isEnabled: true, isEnableToggleShown: false, isDimmed: false) } } /// A stream's selection: the effective player each enabled device occupies, then the remembered - /// rows for whatever is disabled, waiting or gone. `families` supplies the type label for the - /// devices the registry still knows about; a disconnected row keeps only its remembered name. + /// rows for whatever is disabled, waiting or gone. A disconnected row keeps its remembered name. static func stream(_ rows: [ControllerStreamSelection.Row], families: [InputDeviceID: ControllerFamily]) -> [ControllerOrderRow] { rows.map { row in ControllerOrderRow(id: row.id, name: row.name, slotLabel: slotLabel(for: row.state), - detail: detail(for: row.state, family: families[row.id]), + statusDetail: statusDetail(for: row.state, family: families[row.id]), isEnabled: row.isEnabled, - showsEnableToggle: row.state != .disconnected, + isEnableToggleShown: row.state != .disconnected, isDimmed: row.state == .disconnected) } } @@ -54,7 +52,7 @@ enum ControllerOrderRows { } } - static func detail(for state: ControllerStreamSelection.RowState, family: ControllerFamily?) -> String { + static func statusDetail(for state: ControllerStreamSelection.RowState, family: ControllerFamily?) -> String { switch state { case .active: family?.label ?? "Controller" case .waiting: "Waiting — four players already active" diff --git a/ViewModel/Stream/StreamControllerSelectionContext.swift b/ViewModel/Stream/StreamControllerSelectionContext.swift index 1e6f61223..ea5cc8490 100644 --- a/ViewModel/Stream/StreamControllerSelectionContext.swift +++ b/ViewModel/Stream/StreamControllerSelectionContext.swift @@ -1,19 +1,16 @@ import Foundation -/// The stream-local half of the controller panel: the monitor that owns this session's admission -/// decision, handed to the HUD's controller sheet so its toggles edit *this* stream only. -/// -/// Deliberately a thin handle rather than a second copy of the state. The monitor is the owner, the -/// panel only reads its rows and asks for a change; a stream that has not resolved its surface yet -/// has no context and the panel falls back to the app-wide order-only presentation. +/// The stream-local half of the controller panel: the monitor that owns this session's admission, +/// narrowed to the two operations the HUD's sheet needs so its toggles edit this stream only. +/// A stream whose surface has not resolved has no context, so the panel shows the app-wide order. @MainActor struct StreamControllerSelectionContext { let monitor: NativeGamepadMonitor var rows: [ControllerStreamSelection.Row] { monitor.streamControllerRows } - func setEnabled(_ enabled: Bool, for deviceID: InputDeviceID) { - monitor.setStreamEnabled(enabled, for: deviceID) + func setEnabled(_ isEnabled: Bool, for deviceID: InputDeviceID) { + monitor.setStreamEnabled(isEnabled, for: deviceID) } } From 84f1a406fbbdb66776bb74cd352464d8db81b108 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 11 Oct 2026 17:53:03 +0200 Subject: [PATCH 03/22] feat: tile stream windows and add borderless full screen behind the couch co-op flag Adds the couchCoop Labs flag. With it on, two or more stream windows are dealt over the screens (one per screen first, then split side by side or top and bottom) and put into a borderless mode that covers the menu bar. The same mode is a HUD tile for any single window. Tiled and borderless windows letterbox instead of holding the aspect lock, skip native full screen and do not save their frame. A window the reader moves leaves the layout. Co-Authored-By: Claude Sonnet 5.5 --- OPN/Services/OPNLabs.swift | 14 ++- OPN/Stream/OPNStreamBorderless.swift | 102 +++++++++++++++ OPN/Stream/OPNStreamPictureInPicture.swift | 10 ++ OPN/Stream/OPNStreamTileLayout.swift | 100 +++++++++++++++ OPN/Stream/OPNStreamWindow.swift | 47 ++++++- OPN/Stream/OPNStreamWindowLayoutState.swift | 29 +++++ Tests/Stream/CouchCoopBorderlessTests.swift | 98 +++++++++++++++ .../NativeNVSTHUDFocusEntryParityTests.swift | 20 +++ Tests/Stream/OPNStreamTileLayoutTests.swift | 118 ++++++++++++++++++ .../Stream/StreamWindowConcurrencyTests.swift | 46 +++++++ View/Settings/SettingsLabsViews.swift | 48 +++++++ .../NativeNVSTStreamHUD+ActionTiles.swift | 14 +++ View/Stream/OPNStreamWindowPresenter.swift | 93 +++++++++++++- View/Stream/OPNStreamWindowRootView.swift | 5 +- .../NativeNVSTHostViewModel+Controls.swift | 28 ++++- ...iveNVSTHostViewModel+FullScreenEntry.swift | 1 + .../NativeNVSTHostViewModel+HUDSections.swift | 8 ++ 17 files changed, 773 insertions(+), 8 deletions(-) create mode 100644 OPN/Stream/OPNStreamBorderless.swift create mode 100644 OPN/Stream/OPNStreamTileLayout.swift create mode 100644 OPN/Stream/OPNStreamWindowLayoutState.swift create mode 100644 Tests/Stream/CouchCoopBorderlessTests.swift create mode 100644 Tests/Stream/OPNStreamTileLayoutTests.swift diff --git a/OPN/Services/OPNLabs.swift b/OPN/Services/OPNLabs.swift index 88fab9037..05859fef4 100644 --- a/OPN/Services/OPNLabs.swift +++ b/OPN/Services/OPNLabs.swift @@ -30,14 +30,26 @@ enum OPNLabs { since: "0.15" ) + /// Couch co-op on one Mac: extra accounts stream in their own windows, each window gets its own + /// controllers, and the windows tile themselves. Off by default because it changes how the stream + /// windows are placed and lets pads keep playing while another window has the keyboard focus. + static let couchCoop = OPNLabsFlag( + id: "couchCoop", + title: "Couch Co-op", + summary: "Adds Stream > Detach New Session, which opens a window for a second or third account. Each window takes its own controllers, plays while another window is in front, and the stream windows tile across your screens.", + since: "0.15" + ) + /// Every trial in flight. Empty is the normal state, and the Settings rail drops the Labs /// destination while it is. - static let flags: [OPNLabsFlag] = [clipboardCapture] + static let flags: [OPNLabsFlag] = [clipboardCapture, couchCoop] /// Whether the in-stream clipboard history is switched on. Read at every point the feature is /// offered — the HUD panel, the Settings card, the binding and the capture path itself. static var isClipboardCaptureEnabled: Bool { isEnabled(clipboardCapture) } + static var isCouchCoopEnabled: Bool { isEnabled(couchCoop) } + static var hasFlags: Bool { !flags.isEmpty } static func isEnabled(_ flag: OPNLabsFlag) -> Bool { diff --git a/OPN/Stream/OPNStreamBorderless.swift b/OPN/Stream/OPNStreamBorderless.swift new file mode 100644 index 000000000..34723ec62 --- /dev/null +++ b/OPN/Stream/OPNStreamBorderless.swift @@ -0,0 +1,102 @@ +// Borderless windowed full screen, as a mode of the dedicated stream window. +// +// The window keeps its style mask. Changing `styleMask` on a window that is already on screen is +// the trap `OPNStreamPictureInPicture` documents, and `.titled` is what the Dock icon counts. The +// mode is the frame, the level and the standard buttons instead: the window is set to a screen's +// frame, raised above the menu bar and the Dock when it covers the whole screen, and its traffic +// lights are hidden. Leaving puts back what entering replaced. + +import AppKit + +@MainActor +enum OPNStreamBorderless { + /// Above the menu bar, so a window that covers a screen covers it too. + static let coveringLevel = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 1) + + /// Where a window the reader has to reach sits: above the borderless stream windows, which cover + /// the whole screen they are on. + static let aboveCoveringLevel = NSWindow.Level(rawValue: coveringLevel.rawValue + 1) + + /// Whether `frame` fills one of `screenFrames`. Only such a window is raised over the menu bar + /// and the Dock; a tile that shares a screen stays an ordinary window beside its neighbours. + static func coversWholeScreen(_ frame: NSRect, screenFrames: [NSRect]) -> Bool { + screenFrames.contains { screen in + abs(screen.minX - frame.minX) < 1 && abs(screen.minY - frame.minY) < 1 + && abs(screen.width - frame.width) < 1 && abs(screen.height - frame.height) < 1 + } + } + + static func level(for frame: NSRect, screenFrames: [NSRect]) -> NSWindow.Level { + coversWholeScreen(frame, screenFrames: screenFrames) ? coveringLevel : .normal + } + + /// Puts the window into borderless mode at `frame`, or moves an already borderless window to a + /// new one. What was there before the first entry is kept for the way out. + static func enter(_ window: OPNStreamWindow, frame: NSRect, screenFrames: [NSRect] = NSScreen.screens.map(\.frame)) { + if window.borderlessRestoreState == nil { + window.borderlessRestoreState = OPNStreamWindow.WindowedState( + level: window.level, + collectionBehavior: window.collectionBehavior, + frame: window.frame, + isMiniaturized: window.isMiniaturized + ) + OPNStreamWindowFrameStore.save(window.frame, isPictureInPicture: false, defaults: window.frameStoreDefaults) + } + if window.isMiniaturized { window.deminiaturize(nil) } + window.layoutState.isBorderless = true + StreamWindowGeometryGate.releaseAspectRatioLock(window) + OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: true) + let level = level(for: frame, screenFrames: screenFrames) + window.level = level + for child in window.childWindows ?? [] { child.level = level } + apply(frame: frame, to: window) + } + + static func exit(_ window: OPNStreamWindow) { + guard window.layoutState.isBorderless else { return } + window.layoutState.isBorderless = false + OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: false) + guard let state = window.borderlessRestoreState else { + window.level = .normal + for child in window.childWindows ?? [] { child.level = .normal } + return + } + window.borderlessRestoreState = nil + window.level = state.level + for child in window.childWindows ?? [] { child.level = state.level } + apply(frame: state.frame, to: window) + if state.isMiniaturized { window.miniaturize(nil) } + } + + /// The window's own toggle, from the HUD: the window is the reader's from here on, so the layout + /// stops placing it whichever way it goes. + static func toggleByReader(_ window: OPNStreamWindow) { + if window.layoutState.isBorderless { + releaseToReader(window) + return + } + window.layoutState.isManuallyPlaced = true + enter(window, frame: targetFrame(for: window)) + } + + /// Leaves borderless and the layout together, for a window that is about to become something + /// else (Picture in Picture) or that the reader no longer wants tiled. + static func releaseToReader(_ window: OPNStreamWindow) { + window.layoutState.isManuallyPlaced = true + window.layoutState.isAutoTiled = false + exit(window) + window.clearLayoutFrame() + } + + /// The screen a borderless toggle fills when nothing else decided: the one the window is on. + static func targetFrame(for window: NSWindow) -> NSRect { + (window.screen ?? NSScreen.main)?.frame ?? window.frame + } + + private static func apply(frame: NSRect, to window: OPNStreamWindow) { + StreamWindowGeometryGate.whenGeometryIsMutable { [weak window] in + guard let window else { return } + window.setFrameFromLayout(frame) + } + } +} diff --git a/OPN/Stream/OPNStreamPictureInPicture.swift b/OPN/Stream/OPNStreamPictureInPicture.swift index b3ed7d3cf..22f332f94 100644 --- a/OPN/Stream/OPNStreamPictureInPicture.swift +++ b/OPN/Stream/OPNStreamPictureInPicture.swift @@ -152,3 +152,13 @@ enum OPNStreamWindowChrome { window.isMovableByWindowBackground = isPictureInPicture } } + +extension OPNStreamWindowChrome { + /// Borderless drops the traffic lights and nothing else: the window stays where the layout put it + /// rather than becoming draggable, because a stray drag would take it out of the layout. + static func applyBorderless(to window: NSWindow, isBorderless: Bool) { + for button in [NSWindow.ButtonType.closeButton, .miniaturizeButton, .zoomButton] { + window.standardWindowButton(button)?.isHidden = isBorderless + } + } +} diff --git a/OPN/Stream/OPNStreamTileLayout.swift b/OPN/Stream/OPNStreamTileLayout.swift new file mode 100644 index 000000000..87e9d7fde --- /dev/null +++ b/OPN/Stream/OPNStreamTileLayout.swift @@ -0,0 +1,100 @@ +// Where each stream window goes when several are on screen, as pure geometry. +// +// The presenter owns the windows and the screens; this owns only the arithmetic, so the placement +// rules can be asserted without a window server. + +import CoreGraphics +import Foundation + +enum OPNStreamTileOrientation: String, CaseIterable, Identifiable, Sendable { + case auto + case sideBySide + case topAndBottom + + static let storageKey = "OpenNOW.CouchCoop.Tiling" + + var id: String { rawValue } + + var title: String { + switch self { + case .auto: "Automatic" + case .sideBySide: "Side by Side" + case .topAndBottom: "Top and Bottom" + } + } + + static var stored: OPNStreamTileOrientation { + OPNAppPreferenceStorage.standard.string(forKey: storageKey).flatMap(Self.init(rawValue:)) ?? .auto + } + + static func store(_ orientation: OPNStreamTileOrientation) { + OPNAppPreferenceStorage.standard.set(orientation.rawValue, forKey: storageKey) + } +} + +struct OPNStreamTile: Equatable, Sendable { + let frame: CGRect + let screenIndex: Int +} + +enum OPNStreamTileLayout { + /// One tile per window, in window order. A window gets a whole screen while there are screens to + /// spare; past that the windows are dealt over the screens in order and a shared screen is split. + static func tiles(screens: [CGRect], windowCount: Int, orientation: OPNStreamTileOrientation) -> [OPNStreamTile] { + guard windowCount > 0, !screens.isEmpty else { return [] } + let share = windowsPerScreen(screenCount: screens.count, windowCount: windowCount) + var tiles: [OPNStreamTile] = [] + for (screenIndex, count) in share.enumerated() where count > 0 { + let cells = split(screens[screenIndex], into: count, orientation: orientation) + tiles.append(contentsOf: cells.map { OPNStreamTile(frame: $0, screenIndex: screenIndex) }) + } + return tiles + } + + /// How many windows each screen holds. Earlier screens take the remainder, so the main screen is + /// never the one left with fewer. + static func windowsPerScreen(screenCount: Int, windowCount: Int) -> [Int] { + guard screenCount > 0, windowCount > 0 else { return [] } + let base = windowCount / screenCount + let remainder = windowCount % screenCount + return (0.. [CGRect] { + guard count > 0 else { return [] } + guard count > 1 else { return [area] } + let grid = gridShape(for: count, in: area, orientation: orientation) + var cells: [CGRect] = [] + for index in 0.. (columns: Int, rows: Int) { + let prefersColumns = switch orientation { + case .auto: area.width >= area.height + case .sideBySide: true + case .topAndBottom: false + } + if count <= 3 { + return prefersColumns ? (count, 1) : (1, count) + } + let long = Int(Double(count).squareRoot().rounded(.up)) + let short = Int((Double(count) / Double(long)).rounded(.up)) + return prefersColumns ? (long, short) : (short, long) + } + + private static func edge(of origin: CGFloat, length: CGFloat, step: Int, steps: Int) -> CGFloat { + origin + (length * CGFloat(step) / CGFloat(steps)).rounded() + } +} diff --git a/OPN/Stream/OPNStreamWindow.swift b/OPN/Stream/OPNStreamWindow.swift index 5d7f74df6..6e7bcd2bf 100644 --- a/OPN/Stream/OPNStreamWindow.swift +++ b/OPN/Stream/OPNStreamWindow.swift @@ -39,6 +39,39 @@ final class OPNStreamWindow: NSWindow { /// What PiP entry replaced, so leaving PiP puts the window back exactly where it was. var windowedState: WindowedState? + /// What borderless entry replaced, so leaving it puts the window back where the reader left it. + var borderlessRestoreState: WindowedState? + + /// Shared with the window's content, which letterboxes instead of locking its aspect while the + /// layout owns the frame. + let layoutState = OPNStreamWindowLayoutState() + + /// The last frame the layout gave this window. A move or resize that leaves it is the reader + /// taking the window back, which a frame change alone cannot tell from the layout's own. + private(set) var layoutFrame: NSRect? + + func setFrameFromLayout(_ frame: NSRect) { + layoutFrame = frame + setFrame(frame, display: true) + } + + func clearLayoutFrame() { + layoutFrame = nil + } + + /// Whether the window is where the layout left it. + var isAtLayoutFrame: Bool { + guard let layoutFrame else { return false } + return abs(frame.minX - layoutFrame.minX) < 1 && abs(frame.minY - layoutFrame.minY) < 1 + && abs(frame.width - layoutFrame.width) < 1 && abs(frame.height - layoutFrame.height) < 1 + } + + /// A titled window is pushed below the menu bar when it is placed. A borderless one is asked to + /// cover it, so the frame it was given is the frame it keeps. + override func constrainFrameRect(_ frameRect: NSRect, to screen: NSScreen?) -> NSRect { + layoutState.isBorderless ? frameRect : super.constrainFrameRect(frameRect, to: screen) + } + /// The stream surface's answer to the close button, installed once the surface is up. Returning /// `true` keeps the window open - see `OPNStreamWindowCloseGuard` for why the window itself /// never decides. @@ -49,6 +82,16 @@ final class OPNStreamWindow: NSWindow { /// all, and the controls panel it raises - and the surface is the only thing that has it. weak var sessionSurface: (any OPNStreamWindowSessionSurface)? + /// Couch co-op: the stream in this window keeps its pads while another stream window is in + /// front. Written by the presenter and handed to the stream surface, which may not have + /// resolved yet, so the surface also reads it when it attaches. + var allowsBackgroundGamepadInput = false { + didSet { + guard oldValue != allowsBackgroundGamepadInput else { return } + sessionSurface?.setBackgroundGamepadInput(allowsBackgroundGamepadInput) + } + } + /// The close guard, held **strongly and by the window**. /// /// `NSWindow.delegate` is a `weak` property (AppKit's `NSWindow.h`), so a guard that is only @@ -88,7 +131,7 @@ final class OPNStreamWindow: NSWindow { // A full-screen frame belongs to the Space, not to the user: restoring it would open the // next session full-bleed. The exit re-fires the move, so the windowed placement is saved // once the window is back on the desktop. - guard !styleMask.contains(.fullScreen) else { return } + guard !styleMask.contains(.fullScreen), layoutState.persistsFrame else { return } OPNStreamWindowFrameStore.save(frame, isPictureInPicture: isPictureInPicture, defaults: frameStoreDefaults) } @@ -107,6 +150,8 @@ final class OPNStreamWindow: NSWindow { protocol OPNStreamWindowSessionSurface: AnyObject { var isConnected: Bool { get } var isPictureInPicture: Bool { get } + func setBackgroundGamepadInput(_ isAllowed: Bool) + func disableControllersForStream(_ deviceIDs: Set) /// Raises the stream controls panel. Called with a `nil` completion, which is what keeps the /// third button reading "End Stream" rather than "Quit OpenNOW". Deliberately written out /// rather than leaning on `showStreamControls`'s default argument: a defaulted parameter does diff --git a/OPN/Stream/OPNStreamWindowLayoutState.swift b/OPN/Stream/OPNStreamWindowLayoutState.swift new file mode 100644 index 000000000..100480cbc --- /dev/null +++ b/OPN/Stream/OPNStreamWindowLayoutState.swift @@ -0,0 +1,29 @@ +// How one stream window is laid out on the desktop, as state the window and its content share. +// +// Borderless and tiled windows both give up the aspect lock and the titlebar strip: the picture +// letterboxes inside whatever frame the window was given instead of the frame following the +// picture. The window owns this, the stream's root view reads it, and the presenter writes it. + +import Foundation +import Observation + +@MainActor +@Observable +final class OPNStreamWindowLayoutState { + /// The window covers a frame edge to edge with its chrome hidden. + var isBorderless = false + /// The presenter owns this window's frame. Cleared the moment the reader moves or resizes it. + var isAutoTiled = false + + /// The reader placed this window themselves, so the layout leaves it where it is. + var isManuallyPlaced = false + + var releasesAspectLock: Bool { isBorderless || isAutoTiled } + + /// A window the layout owns never enters native full screen by itself, and the session-ready + /// full-screen request leaves it alone. + var suppressesNativeFullScreen: Bool { isBorderless || isAutoTiled } + + /// Placements are remembered only for a window the reader placed. + var persistsFrame: Bool { !isBorderless && !isAutoTiled } +} diff --git a/Tests/Stream/CouchCoopBorderlessTests.swift b/Tests/Stream/CouchCoopBorderlessTests.swift new file mode 100644 index 000000000..7ab114d6e --- /dev/null +++ b/Tests/Stream/CouchCoopBorderlessTests.swift @@ -0,0 +1,98 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized) struct CouchCoopBorderlessTests { + private let screens = [ + NSRect(x: 0, y: 0, width: 1920, height: 1080), + NSRect(x: 1920, y: 0, width: 2560, height: 1440), + ] + + @Test func aWindowThatFillsAScreenCoversTheMenuBar() { + #expect(OPNStreamBorderless.coversWholeScreen(screens[1], screenFrames: screens)) + #expect(OPNStreamBorderless.level(for: screens[0], screenFrames: screens) == OPNStreamBorderless.coveringLevel) + #expect(OPNStreamBorderless.coveringLevel.rawValue > NSWindow.Level.mainMenu.rawValue) + } + + @Test func aTileThatSharesAScreenStaysAnOrdinaryWindow() { + let half = NSRect(x: 0, y: 0, width: 960, height: 1080) + #expect(!OPNStreamBorderless.coversWholeScreen(half, screenFrames: screens)) + #expect(OPNStreamBorderless.level(for: half, screenFrames: screens) == .normal) + } + + @Test func aReachableWindowSitsAboveTheCoveringStreams() { + #expect(OPNStreamBorderless.aboveCoveringLevel.rawValue > OPNStreamBorderless.coveringLevel.rawValue) + } + + @Test func borderlessAndTiledWindowsLetterboxAndNeverEnterNativeFullScreenOrPersistAFrame() { + let state = OPNStreamWindowLayoutState() + #expect(!state.releasesAspectLock && !state.suppressesNativeFullScreen && state.persistsFrame) + + state.isBorderless = true + #expect(state.releasesAspectLock && state.suppressesNativeFullScreen && !state.persistsFrame) + + state.isBorderless = false + state.isAutoTiled = true + #expect(state.releasesAspectLock && state.suppressesNativeFullScreen && !state.persistsFrame) + + state.isAutoTiled = false + #expect(!state.releasesAspectLock && !state.suppressesNativeFullScreen && state.persistsFrame) + } + + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func enteringBorderlessHidesTheButtonsKeepsTheStyleMaskAndLeavingRestoresTheWindow() async throws { + let suite = "OpenNOWTests.Borderless.\(UUID().uuidString)" + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: UserDefaults(suiteName: suite) ?? .standard) + defer { UserDefaults(suiteName: suite)?.removePersistentDomain(forName: suite) } + let before = window.frame + let level = window.level + let styleMask = window.styleMask + let target = NSRect(x: 100, y: 100, width: 800, height: 450) + + OPNStreamBorderless.enter(window, frame: target, screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) + + #expect(window.layoutState.isBorderless) + #expect(window.styleMask == styleMask) + #expect(window.standardWindowButton(.closeButton)?.isHidden == true) + #expect(window.frame == target) + #expect(window.level == .normal) + + OPNStreamBorderless.exit(window) + try await Task.sleep(for: .milliseconds(200)) + + #expect(!window.layoutState.isBorderless) + #expect(window.standardWindowButton(.closeButton)?.isHidden == false) + #expect(window.level == level) + #expect(window.frame == before) + #expect(window.borderlessRestoreState == nil) + } + + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func aBorderlessWindowDoesNotWriteItsFrameToThePlacementStore() async throws { + let suite = "OpenNOWTests.BorderlessStore.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + defer { defaults.removePersistentDomain(forName: suite) } + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: defaults) + OPNStreamWindowFrameStore.forget(isPictureInPicture: false, defaults: defaults) + + window.layoutState.isAutoTiled = true + window.setFrame(NSRect(x: 10, y: 10, width: 700, height: 400), display: false) + try await Task.sleep(for: .milliseconds(200)) + + #expect(OPNStreamWindowFrameStore.rememberedFrame(isPictureInPicture: false, defaults: defaults) == nil) + window.stopPersistingFrame() + } + + @Test func aWindowMovedAwayFromItsLayoutFrameIsNotAtIt() { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + #expect(!window.isAtLayoutFrame) + window.setFrameFromLayout(NSRect(x: 50, y: 50, width: 640, height: 360)) + #expect(window.isAtLayoutFrame) + window.setFrame(NSRect(x: 80, y: 50, width: 640, height: 360), display: false) + #expect(!window.isAtLayoutFrame) + window.clearLayoutFrame() + #expect(!window.isAtLayoutFrame) + } +} diff --git a/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift b/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift index d0c60a13c..3cfe34efc 100644 --- a/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift +++ b/Tests/Stream/NativeNVSTHUDFocusEntryParityTests.swift @@ -28,6 +28,26 @@ struct NativeNVSTHUDFocusEntryParityTests { #expect(focusIDs(in: "input", on: model) == surface.nativeHUDInputTiles.map(\.id)) } + /// The borderless tile exists only behind the Couch Co-op flag, and when it does a pad reaches it + /// the same way the pointer does. + @Test func theBorderlessTileAndItsFocusEntryAppearTogetherBehindTheFlag() { + let previous = OPNLabs.isCouchCoopEnabled + defer { OPNLabs.setEnabled(OPNLabs.couchCoop, previous) } + + OPNLabs.setEnabled(OPNLabs.couchCoop, false) + let (offSurface, offModel) = makeHUDSurface() + #expect(!offSurface.nativeHUDDisplayTiles.map(\.id).contains("borderless-full-screen")) + #expect(!offModel.hudFocusEntries.map(\.id).contains("borderless-full-screen")) + + OPNLabs.setEnabled(OPNLabs.couchCoop, true) + let (onSurface, onModel) = makeHUDSurface() + #expect(onSurface.nativeHUDDisplayTiles.map(\.id).contains("borderless-full-screen")) + #expect(focusIDs(in: "display", on: onModel) == onSurface.nativeHUDDisplayTiles.map(\.id)) + let tile = onSurface.nativeHUDDisplayTiles.first { $0.id == "borderless-full-screen" } + let entry = onModel.hudFocusEntries.first { $0.id == "borderless-full-screen" } + #expect(tile?.isDisabled == entry?.isDisabled) + } + /// The AUDIO panel has no tiles any more: the mute tiles were replaced by the volume sliders' /// leading icons, so its rows are hand-listed here in draw order — output above microphone, each /// volume a two-entry row of its mute icon and its slider. diff --git a/Tests/Stream/OPNStreamTileLayoutTests.swift b/Tests/Stream/OPNStreamTileLayoutTests.swift new file mode 100644 index 000000000..4077cb569 --- /dev/null +++ b/Tests/Stream/OPNStreamTileLayoutTests.swift @@ -0,0 +1,118 @@ +import CoreGraphics +import Testing +@testable import OpenNOW + +@Suite struct OPNStreamTileLayoutTests { + private let landscape = CGRect(x: 0, y: 0, width: 1920, height: 1080) + private let secondScreen = CGRect(x: 1920, y: 0, width: 2560, height: 1440) + private let portrait = CGRect(x: 0, y: 0, width: 1080, height: 1920) + + @Test func twoWindowsOnTwoScreensFillOneScreenEach() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 2, orientation: .auto) + #expect(tiles == [ + OPNStreamTile(frame: landscape, screenIndex: 0), + OPNStreamTile(frame: secondScreen, screenIndex: 1), + ]) + } + + @Test func twoWindowsOnOneLandscapeScreenSplitSideBySideInAutomatic() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 2, orientation: .auto) + #expect(tiles.map(\.frame) == [ + CGRect(x: 0, y: 0, width: 960, height: 1080), + CGRect(x: 960, y: 0, width: 960, height: 1080), + ]) + #expect(tiles.allSatisfy { $0.screenIndex == 0 }) + } + + @Test func twoWindowsOnOnePortraitScreenSplitTopAndBottomInAutomatic() { + let tiles = OPNStreamTileLayout.tiles(screens: [portrait], windowCount: 2, orientation: .auto) + #expect(tiles.map(\.frame) == [ + CGRect(x: 0, y: 960, width: 1080, height: 960), + CGRect(x: 0, y: 0, width: 1080, height: 960), + ]) + } + + @Test func explicitOrientationOverridesTheScreenShape() { + let stacked = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 2, orientation: .topAndBottom) + #expect(stacked.map(\.frame) == [ + CGRect(x: 0, y: 540, width: 1920, height: 540), + CGRect(x: 0, y: 0, width: 1920, height: 540), + ]) + let columns = OPNStreamTileLayout.tiles(screens: [portrait], windowCount: 2, orientation: .sideBySide) + #expect(columns.map(\.frame) == [ + CGRect(x: 0, y: 0, width: 540, height: 1920), + CGRect(x: 540, y: 0, width: 540, height: 1920), + ]) + } + + @Test func threeWindowsOnOneScreenShareItInOneStrip() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 3, orientation: .sideBySide) + #expect(tiles.map(\.frame.width) == [640, 640, 640]) + #expect(tiles.map(\.frame.minX) == [0, 640, 1280]) + } + + @Test func fourWindowsOnOneScreenFormAGrid() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 4, orientation: .auto) + #expect(tiles.map(\.frame) == [ + CGRect(x: 0, y: 540, width: 960, height: 540), + CGRect(x: 960, y: 540, width: 960, height: 540), + CGRect(x: 0, y: 0, width: 960, height: 540), + CGRect(x: 960, y: 0, width: 960, height: 540), + ]) + } + + @Test func threeWindowsOnTwoScreensGiveTheFirstScreenTheExtraWindow() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 3, orientation: .auto) + #expect(tiles.map(\.screenIndex) == [0, 0, 1]) + #expect(tiles[2].frame == secondScreen) + #expect(tiles[0].frame.width + tiles[1].frame.width == landscape.width) + } + + @Test func oddScreenSizesLeaveNoSeamAndNoOverlap() { + let odd = CGRect(x: 0, y: 0, width: 1367, height: 769) + for count in 2...7 { + for orientation in OPNStreamTileOrientation.allCases { + let tiles = OPNStreamTileLayout.tiles(screens: [odd], windowCount: count, orientation: orientation) + #expect(tiles.count == count) + let covered = tiles.reduce(0) { $0 + $1.frame.width * $1.frame.height } + #expect(covered == odd.width * odd.height, "\(count) windows, \(orientation)") + for (index, tile) in tiles.enumerated() { + #expect(odd.contains(tile.frame)) + for other in tiles[(index + 1)...] { + #expect(tile.frame.intersection(other.frame).isEmpty || tile.frame.intersection(other.frame).width * tile.frame.intersection(other.frame).height == 0) + } + } + } + } + } + + @Test func screensOfDifferentSizesKeepTheirOwnTiles() { + let tiles = OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 4, orientation: .sideBySide) + let first = tiles.filter { $0.screenIndex == 0 } + let second = tiles.filter { $0.screenIndex == 1 } + #expect(first.count == 2 && second.count == 2) + #expect(first.allSatisfy { landscape.contains($0.frame) }) + #expect(second.allSatisfy { secondScreen.contains($0.frame) }) + #expect(second.map(\.frame.width) == [1280, 1280]) + } + + @Test func nothingToPlaceProducesNoTiles() { + #expect(OPNStreamTileLayout.tiles(screens: [], windowCount: 2, orientation: .auto).isEmpty) + #expect(OPNStreamTileLayout.tiles(screens: [landscape], windowCount: 0, orientation: .auto).isEmpty) + } + + @Test func oneWindowFillsItsScreen() { + #expect(OPNStreamTileLayout.tiles(screens: [landscape, secondScreen], windowCount: 1, orientation: .auto) == [ + OPNStreamTile(frame: landscape, screenIndex: 0), + ]) + } + + @Test func theOrientationPreferenceRoundTripsThroughStorage() { + let original = OPNStreamTileOrientation.stored + defer { OPNStreamTileOrientation.store(original) } + for orientation in OPNStreamTileOrientation.allCases { + OPNStreamTileOrientation.store(orientation) + #expect(OPNStreamTileOrientation.stored == orientation) + } + } +} diff --git a/Tests/Stream/StreamWindowConcurrencyTests.swift b/Tests/Stream/StreamWindowConcurrencyTests.swift index 35b6a25bc..eecdccf80 100644 --- a/Tests/Stream/StreamWindowConcurrencyTests.swift +++ b/Tests/Stream/StreamWindowConcurrencyTests.swift @@ -69,6 +69,52 @@ import Testing #expect(OPNStreamWindowPresenter.shared.windows[game.id] == nil) } + /// With couch co-op on, two streams are tiled borderless and share the screens; with the flag off + /// nothing is ever placed; ending one hands the survivor back its own placement. + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func couchCoopTilesTwoStreamsAndReleasesTheSurvivor() async throws { + let previousFlag = OPNLabs.isCouchCoopEnabled + defer { OPNLabs.setEnabled(OPNLabs.couchCoop, previousFlag) } + let registry = OPNGameSessionRegistry.shared + let accountA = makeLoginAccountForTesting(email: "a@example.com", userId: "user-a") + let accountB = makeLoginAccountForTesting(email: "b@example.com", userId: "user-b", isActive: false) + let sessionA = makeLoginSessionForTesting(accountEmail: accountA.email, id: "tile-a", userId: "user-a") + let sessionB = makeLoginSessionForTesting(accountEmail: accountB.email, id: "tile-b", userId: "user-b") + defer { + sessionA.purgeTokens() + sessionB.purgeTokens() + } + let gameA = try #require(begin(registry: registry, account: accountA, session: sessionA)) + let gameB = try #require(begin(registry: registry, account: accountB, session: sessionB)) + defer { + OPNStreamWindowPresenter.shared.dismiss(gameA.id) + OPNStreamWindowPresenter.shared.dismiss(gameB.id) + registry.end(gameA) + registry.end(gameB) + } + gameA.configuration = Self.configuration(title: "Cyberpunk 2077") + gameB.configuration = Self.configuration(title: "Manor Lords") + + OPNLabs.setEnabled(OPNLabs.couchCoop, false) + OPNStreamWindowPresenter.shared.syncWithOwnedSessions() + let windowA = try #require(OPNStreamWindowPresenter.shared.windows[gameA.id]) + let windowB = try #require(OPNStreamWindowPresenter.shared.windows[gameB.id]) + #expect(!windowA.layoutState.isAutoTiled && !windowB.layoutState.isAutoTiled) + #expect(!windowA.allowsBackgroundGamepadInput) + + OPNLabs.setEnabled(OPNLabs.couchCoop, true) + OPNStreamWindowPresenter.shared.retileWindows() + try await Task.sleep(for: .milliseconds(300)) + #expect(windowA.layoutState.isAutoTiled && windowB.layoutState.isAutoTiled) + #expect(windowA.layoutState.isBorderless && windowB.layoutState.isBorderless) + #expect(windowA.frame != windowB.frame) + + OPNStreamWindowPresenter.shared.dismiss(gameB.id) + try await Task.sleep(for: .milliseconds(300)) + #expect(!windowA.layoutState.isAutoTiled) + #expect(!windowA.layoutState.isBorderless) + } + private static func configuration(title: String) -> StreamLaunchConfiguration { StreamLaunchConfiguration( title: title, diff --git a/View/Settings/SettingsLabsViews.swift b/View/Settings/SettingsLabsViews.swift index afba7a3ed..f9d069242 100644 --- a/View/Settings/SettingsLabsViews.swift +++ b/View/Settings/SettingsLabsViews.swift @@ -28,7 +28,55 @@ struct LabsSettingsPage: View { } } .settingsSection("labs") + if isCouchCoopEnabled { + CouchCoopTilingCard(uiScale: uiScale) + } + } + } + + @AppStorage(OPNLabs.couchCoop.storageKey) private var isCouchCoopEnabled = false +} + +/// How two or more stream windows share a screen. Shown only while Couch Co-op is on, since nothing +/// else reads it. +struct CouchCoopTilingCard: View { + let uiScale: CGFloat + + @State private var orientation = OPNStreamTileOrientation.auto + + var body: some View { + SettingsCard(title: "Couch Co-op Layout", badge: .experimental, uiScale: uiScale) { + Text("With two accounts on two screens each stream fills its own screen. When streams share a screen they split it, and this decides how.") + .font(.settingsFont(size: 12 * uiScale, weight: .medium)) + .foregroundStyle(OPNDesign.Text.tertiary) + .fixedSize(horizontal: false, vertical: true) + SettingsDivider(uiScale: uiScale) + HStack { + Text("Split shared screens") + .font(.settingsFont(size: 13 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Spacer(minLength: 0) + OPNDropdownMenu(items: OPNStreamTileOrientation.allCases.map { option in + OPNDropdownItem(id: option.id, title: option.title, isSelected: option == orientation) { + orientation = option + OPNStreamTileOrientation.store(option) + } + }) { + HStack(spacing: OPNDesign.Spacing.xSmall(scale: uiScale)) { + Text(orientation.title) + .font(.settingsFont(size: 12 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Image(systemName: "chevron.down") + .font(.settingsFont(size: 9 * uiScale, weight: .bold)) + } + .padding(.horizontal, OPNDesign.Spacing.controlRow(scale: uiScale)) + .frame(height: 30 * uiScale) + .background(OPNCornerShape(role: .control, scale: uiScale).fill(OPNDesign.Fill.neutral(0.075))) + .overlay { OPNCornerShape(role: .control, scale: uiScale).strokeBorder(OPNDesign.Stroke.regular, lineWidth: 1) } + } + } } + .onAppear { orientation = OPNStreamTileOrientation.stored } } } diff --git a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift index 23f5062ec..c73f3e196 100644 --- a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift +++ b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift @@ -38,6 +38,20 @@ extension NativeNVSTMediaStreamSurface { } var nativeHUDDisplayTiles: [NativeHUDTile] { + let tiles = nativeHUDBaseDisplayTiles + guard model.isBorderlessTileAvailable else { return tiles } + return tiles + [ + NativeHUDTile(id: "borderless-full-screen", + title: model.isBorderlessWindow ? "Leave Borderless" : "Borderless Full Screen", + subtitle: model.isBorderlessWindow ? "Borderless" : "Fill the screen, keep the Spaces", + systemName: "rectangle.dashed", + isActive: model.isBorderlessWindow, + isDisabled: model.isBorderlessTileDisabled, + action: model.toggleBorderlessFullScreen), + ] + } + + private var nativeHUDBaseDisplayTiles: [NativeHUDTile] { [ NativeHUDTile(id: "floating-stats", title: model.nativeStatsVisible ? "Hide Floating Stats" : "Show Floating Stats", diff --git a/View/Stream/OPNStreamWindowPresenter.swift b/View/Stream/OPNStreamWindowPresenter.swift index a7261f840..d0ce559d0 100644 --- a/View/Stream/OPNStreamWindowPresenter.swift +++ b/View/Stream/OPNStreamWindowPresenter.swift @@ -10,6 +10,7 @@ // import AppKit +import GameController import SwiftUI @MainActor @@ -19,10 +20,13 @@ final class OPNStreamWindowPresenter { /// One window per session, keyed by the session's own id: two accounts can stream at once, and /// each window belongs to the session that opened it. private(set) var windows: [UUID: OPNStreamWindow] = [:] + /// Creation order, which is the order the couch co-op layout deals windows out in. + private var windowOrder: [UUID] = [] private var presentedConfigurationIDs: [UUID: UUID] = [:] private var hostingViews: [UUID: NSHostingView] = [:] /// Held so the observation is not dropped the moment it is installed. private var sessionChangeObserver: NSObjectProtocol? + private var screenChangeObserver: NSObjectProtocol? /// Per-window full-screen bookkeeping, tracked from `present` so a dismissal that lands /// mid-enter is recognised before AppKit sets `.fullScreen`. @@ -113,6 +117,14 @@ final class OPNStreamWindowPresenter { ) { [weak self] _ in MainActor.assumeIsolated { self?.syncWithOwnedSessions() } } + // swiftlint:disable:next view_owns_long_lived_effect -- application-lifetime AppKit presenter, installed once by the app delegate + screenChangeObserver = NotificationCenter.default.addObserver( + forName: NSApplication.didChangeScreenParametersNotification, + object: nil, + queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.retileWindows() } + } } /// Brings every owned session's window up to date: one window per session that has a stream, and @@ -139,7 +151,7 @@ final class OPNStreamWindowPresenter { // the first and read as a replacement. Cascaded clear, the way macOS does it. let cascaded = Self.cascadedFrame(for: window.frame, taken: windows.values.map(\.frame)) window.setFrame(OPNStreamWindowFrameStore.onScreen(cascaded), display: false) - let hostingView = NSHostingView(rootView: OPNStreamWindowRootView(configuration: configuration, session: session)) + let hostingView = NSHostingView(rootView: OPNStreamWindowRootView(configuration: configuration, session: session, layoutState: window.layoutState)) window.contentView = hostingView // The game's title belongs to the window the game is in; the catalog window keeps its own. window.title = Self.windowTitle(for: configuration) @@ -151,6 +163,7 @@ final class OPNStreamWindowPresenter { OPNStreamWindowCloseGuard.install(on: window) windows[session.id] = window + windowOrder.append(session.id) hostingViews[session.id] = hostingView presentedConfigurationIDs[session.id] = configuration.id // Tracked for the window's whole life so a mid-enter dismissal sees the transition. @@ -165,6 +178,8 @@ final class OPNStreamWindowPresenter { case .takeKey: window.makeKeyAndOrderFront(nil) case .orderFrontWithoutActivating: window.orderFront(nil) } + retileWindows() + updateBackgroundGamepadInput() OPNLog.info(.launch, "Stream window presented for \(configuration.applicationID) session=\(session.id)") } @@ -175,10 +190,13 @@ final class OPNStreamWindowPresenter { presentedConfigurationIDs[sessionID] = nil hostingViews[sessionID] = nil windows[sessionID] = nil + windowOrder.removeAll { $0 == sessionID } OPNStreamWindowCloseGuard.uninstall(from: window) window.closeRequestHandler = nil window.sessionSurface = nil dismissWindow(window) + retileWindows() + updateBackgroundGamepadInput() } /// Decides whether this window can go out now or has to wait for a full-screen transition. @@ -253,6 +271,63 @@ final class OPNStreamWindowPresenter { styleMask.contains(.fullScreen) } + /// Takes `deviceIDs` out of every stream that is already running, so the pads a new window plays + /// with stop answering in the others. Streams that have not resolved their surface yet have + /// nothing to take them from. + func takePads(_ deviceIDs: Set) { + guard !deviceIDs.isEmpty else { return } + for window in windows.values { window.sessionSurface?.disableControllersForStream(deviceIDs) } + } + + /// Lets pads keep playing in the background exactly while couch co-op has two or more streams up, + /// and tells GameController to deliver their values while the app is not frontmost. + private func updateBackgroundGamepadInput() { + let isAllowed = OPNCouchCoopInputPolicy.allowsBackgroundGamepads( + isFlagEnabled: OPNLabs.isCouchCoopEnabled, + liveStreamCount: windows.count + ) + GCController.shouldMonitorBackgroundEvents = isAllowed + for window in windows.values { window.allowsBackgroundGamepadInput = isAllowed } + } + + /// Which windows the couch co-op layout places, and where. A window the reader placed, a window + /// in Picture in Picture and a window in its own full-screen Space are not the layout's to move. + /// Fewer than two placeable windows is not a layout at all: tiled windows go back to where they + /// were, and with the flag off nothing is ever tiled. + func retileWindows() { + let ordered = windowOrder.compactMap { windows[$0] } + let placeable = OPNLabs.isCouchCoopEnabled + ? ordered.filter { Self.isPlaceableByLayout($0) } + : [] + guard placeable.count >= 2 else { + for window in ordered where window.layoutState.isAutoTiled { releaseTile(window) } + return + } + let tiles = OPNStreamTileLayout.tiles( + screens: NSScreen.screens.map(\.frame), + windowCount: placeable.count, + orientation: OPNStreamTileOrientation.stored + ) + guard tiles.count == placeable.count else { return } + for (window, tile) in zip(placeable, tiles) { + window.layoutState.isAutoTiled = true + OPNStreamBorderless.enter(window, frame: tile.frame) + } + for window in ordered where window.layoutState.isAutoTiled && !placeable.contains(where: { $0 === window }) { + releaseTile(window) + } + } + + private static func isPlaceableByLayout(_ window: OPNStreamWindow) -> Bool { + !window.layoutState.isManuallyPlaced && !window.isPictureInPicture && !window.styleMask.contains(.fullScreen) + } + + private func releaseTile(_ window: OPNStreamWindow) { + window.layoutState.isAutoTiled = false + OPNStreamBorderless.exit(window) + window.clearLayoutFrame() + } + /// Observers live on the `WindowPresentation`, never on the presenter, so two windows at once /// route to the right one. @discardableResult @@ -276,11 +351,25 @@ final class OPNStreamWindowPresenter { self?.endTransition(.exiting, for: id) } } - presentation.observerTokens = [willEnter, didEnter, willExit, didExit] + let layoutTokens = [NSWindow.didMoveNotification, NSWindow.didResizeNotification].map { name in + center.addObserver(forName: name, object: window, queue: .main) { [weak window] _ in + MainActor.assumeIsolated { window.map(OPNStreamWindowPresenter.noteFrameChange) } + } + } + presentation.observerTokens = [willEnter, didEnter, willExit, didExit] + layoutTokens presentations[id] = presentation return presentation } + /// A tiled window that is no longer where the layout put it was taken by the reader, and stays + /// theirs: the layout stops placing it. + private static func noteFrameChange(_ window: OPNStreamWindow) { + guard window.layoutState.isAutoTiled, !window.isAtLayoutFrame else { return } + window.layoutState.isAutoTiled = false + window.layoutState.isManuallyPlaced = true + window.clearLayoutFrame() + } + private func setTransition(_ transition: FullScreenTransition, for id: ObjectIdentifier) { presentations[id]?.transition = transition } diff --git a/View/Stream/OPNStreamWindowRootView.swift b/View/Stream/OPNStreamWindowRootView.swift index dae300705..5a0525255 100644 --- a/View/Stream/OPNStreamWindowRootView.swift +++ b/View/Stream/OPNStreamWindowRootView.swift @@ -17,6 +17,7 @@ import SwiftUI struct OPNStreamWindowRootView: View { let configuration: StreamLaunchConfiguration let session: OPNGameSession + let layoutState: OPNStreamWindowLayoutState @AppStorage(OPNInterfacePreferences.uiScaleKey) private var uiScale = OPNInterfacePreferences.defaultUIScale @AppStorage(OPNThemePreferences.accentColorKey) private var accentColorRawValue = OPNThemePreferences.AccentColor.cloudGreen.rawValue @AppStorage(OPNThemePreferences.appearanceKey) private var appearanceRawValue = OPNThemePreferences.Appearance.dark.rawValue @@ -45,7 +46,7 @@ struct OPNStreamWindowRootView: View { GeometryReader { proxy in StreamStageLayout( viewport: proxy.size, - topInset: windowTopInset, + topInset: layoutState.isBorderless ? 0 : windowTopInset, aspectRatio: CGFloat(session.streamProfile.aspectRatio) ) { _ in StreamHostView( @@ -74,7 +75,7 @@ struct OPNStreamWindowRootView: View { } .ignoresSafeArea(edges: .all) .background(Color.black) - .background(StreamWindowAspectConfigurator(aspectRatio: session.streamProfile.aspectRatio, isLocked: true)) + .background(StreamWindowAspectConfigurator(aspectRatio: session.streamProfile.aspectRatio, isLocked: !layoutState.releasesAspectLock)) .environment(\.opnUIScale, uiScale) .opnObservingCornerStyle() .opnObservingLiquidGlass() diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift index ff4fe6ee2..8c3085039 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift @@ -392,7 +392,30 @@ extension NativeNVSTHostViewModel { /// The full-screen tile and its focus entry share this rather than each writing the condition /// out: a PiP window is small and floating, and there is no full screen to enter from it. - var isFullScreenTileDisabled: Bool { nativeView?.window == nil || isPictureInPicture } + var isFullScreenTileDisabled: Bool { nativeView?.window == nil || isPictureInPicture || isBorderlessWindow } + + private var streamLayoutState: OPNStreamWindowLayoutState? { + (nativeView?.window as? OPNStreamWindow)?.layoutState + } + + var isBorderlessWindow: Bool { streamLayoutState?.isBorderless ?? false } + + /// Borderless is a couch co-op mode, so the tile exists only behind the flag, and like full + /// screen it has nothing to act on in a small floating window or in a native full-screen Space. + var isBorderlessTileAvailable: Bool { OPNLabs.isCouchCoopEnabled } + + var isBorderlessTileDisabled: Bool { + nativeView?.window == nil || isPictureInPicture || streamWindowIsFullScreen || isFullScreenTransitioning + } + + /// The reader's own choice, so the layout stops placing this window: it stays where the toggle + /// left it, borderless or not. + func toggleBorderlessFullScreen() { + guard isBorderlessTileAvailable, !isBorderlessTileDisabled, !isEnding, !didEnd, + let window = nativeView?.window as? OPNStreamWindow else { return } + OPNStreamBorderless.toggleByReader(window) + showNativeTransientStreamMessage(window.layoutState.isBorderless ? "Borderless full screen" : "Leaving borderless full screen") + } /// The window owns the transition: nothing here touches the style mask, collection behaviour, /// aspect ratio or frame - `WindowFitting` grants `.fullScreenPrimary` before the window @@ -401,7 +424,7 @@ extension NativeNVSTHostViewModel { func toggleNativeFullScreen() { // There is no full screen to enter from a small floating window; the tile is disabled in // PiP and this is the same answer for the menu bar and the shortcut. - guard let window = nativeView?.window, !isFullScreenTransitioning, !isPictureInPicture else { return } + guard let window = nativeView?.window, !isFullScreenTransitioning, !isPictureInPicture, !isBorderlessWindow else { return } guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } // Read before the toggle: `.fullScreen` is only inserted once the transition finishes. let isEnteringFullScreen = !window.styleMask.contains(.fullScreen) @@ -428,6 +451,7 @@ extension NativeNVSTHostViewModel { func togglePictureInPicture() { guard let window = nativeView?.window as? OPNStreamWindow, !isEnding, !didEnd else { return } guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } + if window.layoutState.isBorderless { OPNStreamBorderless.releaseToReader(window) } switch OPNStreamPictureInPicture.request( isPictureInPicture: isPictureInPicture, isFullScreen: window.styleMask.contains(.fullScreen), diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift b/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift index 29d19ee2c..c8fea83c5 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+FullScreenEntry.swift @@ -35,6 +35,7 @@ extension NativeNVSTHostViewModel { for attempt in 1...Self.sessionReadyFullScreenAttemptLimit { try? await Task.sleep(for: Self.sessionReadyFullScreenRetryDelay) guard !Task.isCancelled else { return } + if let window = nativeView?.window as? OPNStreamWindow, window.layoutState.suppressesNativeFullScreen { return } attemptCount = attempt switch nativeFullScreenEntryAttempt() { case .stop, .alreadyFullScreen: diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift b/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift index fab0c70b8..745d8f278 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+HUDSections.swift @@ -141,6 +141,14 @@ extension NativeNVSTHostViewModel { static let clipboardCaptureModeFocusID = "clipboard-capture-mode" private var displayFocusEntries: [StreamHUDFocusEntry] { + let entries = baseDisplayFocusEntries + guard isBorderlessTileAvailable else { return entries } + return entries + [ + StreamHUDFocusEntry(id: "borderless-full-screen", isDisabled: isBorderlessTileDisabled, group: "display", columns: 4, action: toggleBorderlessFullScreen), + ] + } + + private var baseDisplayFocusEntries: [StreamHUDFocusEntry] { [ StreamHUDFocusEntry(id: "floating-stats", isDisabled: !sidebarCapabilities.supports(.floatingStats), group: "display", columns: 4, action: toggleNativeStatsHUD), StreamHUDFocusEntry(id: "full-screen", isDisabled: isFullScreenTileDisabled, group: "display", columns: 4, action: toggleNativeFullScreen), From b48f6b798c559875e1e47793666178871441e2c7 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 11 Oct 2026 17:53:08 +0200 Subject: [PATCH 04/22] feat: give each stream window its own controllers and let pads play out of focus A stream can start with a preset list of disabled pads, keyed by the owning account, and can take its pads out of the streams already running. With two or more streams up under the couch co-op flag, a window's pads keep driving it while another window has the keyboard, and GameController is asked to deliver background events. Keyboard and mouse stay with the frontmost window. Co-Authored-By: Claude Sonnet 5.5 --- .../Stream/ControllerMappingFocusPolicy.swift | 7 +- Model/Stream/StreamingTypes.swift | 10 +- OPN/Stream/NativeGamepadMonitor.swift | 14 +++ OPN/Stream/NativeStreamView.swift | 13 ++- OPN/Stream/NativeStreamViewInput.swift | 17 ++- OPN/Stream/OPNCouchCoopInputPolicy.swift | 12 ++ OPN/Stream/OPNGameSession.swift | 1 + OPN/Stream/OPNStreamControllerPreset.swift | 47 ++++++++ Tests/Stream/CouchCoopInputRoutingTests.swift | 46 ++++++++ .../OPNStreamControllerPresetTests.swift | 104 ++++++++++++++++++ Tests/Stream/StreamSessionIdentityTests.swift | 2 + View/Stream/StreamHost.swift | 16 ++- ViewModel/Stream/NativeNVSTHostInput.swift | 11 +- .../StreamControllerSelectionContext.swift | 11 ++ 14 files changed, 294 insertions(+), 17 deletions(-) create mode 100644 OPN/Stream/OPNCouchCoopInputPolicy.swift create mode 100644 OPN/Stream/OPNStreamControllerPreset.swift create mode 100644 Tests/Stream/CouchCoopInputRoutingTests.swift create mode 100644 Tests/Stream/OPNStreamControllerPresetTests.swift diff --git a/Model/Stream/ControllerMappingFocusPolicy.swift b/Model/Stream/ControllerMappingFocusPolicy.swift index 5edbaea90..41a40425c 100644 --- a/Model/Stream/ControllerMappingFocusPolicy.swift +++ b/Model/Stream/ControllerMappingFocusPolicy.swift @@ -1,6 +1,9 @@ enum ControllerMappingFocusPolicy { + /// Mappings follow the frontmost stream, except that couch co-op lets a pad keep playing its own + /// stream while another window has the focus. The overlay and remote-input gates still apply. static func allowsMappings(appIsActive: Bool, windowIsKey: Bool, - remoteInputEnabled: Bool, overlayCapturesInput: Bool) -> Bool { - appIsActive && windowIsKey && remoteInputEnabled && !overlayCapturesInput + remoteInputEnabled: Bool, overlayCapturesInput: Bool, + allowsBackgroundPads: Bool = false) -> Bool { + ((appIsActive && windowIsKey) || allowsBackgroundPads) && remoteInputEnabled && !overlayCapturesInput } } diff --git a/Model/Stream/StreamingTypes.swift b/Model/Stream/StreamingTypes.swift index 69771ff9b..bc0ceaae2 100644 --- a/Model/Stream/StreamingTypes.swift +++ b/Model/Stream/StreamingTypes.swift @@ -46,11 +46,18 @@ extension StreamLaunchConfiguration { /// The catalog identity of the game this session runs, snapshotted at launch so its controller /// mappings follow its own game rather than whichever one started last. static let mappingGameIdentityKey = "mappingGameIdentity" + /// The stable id of the account that owns the session, so per-account choices made before the + /// stream existed (which controllers it plays with) find their way to it. + static let owningAccountIDKey = "owningAccountID" var owningAccountDisplayName: String { (metadata[Self.owningAccountDisplayNameKey] ?? "").trimmingCharacters(in: .whitespacesAndNewlines) } + var owningAccountID: String { + (metadata[Self.owningAccountIDKey] ?? "").trimmingCharacters(in: .whitespacesAndNewlines) + } + var mappingGameIdentity: String { (metadata[Self.mappingGameIdentityKey] ?? "").trimmingCharacters(in: .whitespacesAndNewlines) } @@ -59,9 +66,10 @@ extension StreamLaunchConfiguration { /// by, whose game it is, and which game's mappings it uses. The id has to come from the session /// that owns the launch - the stream surface registers its commands under the configuration's id, /// so a different one here would leave every targeted control reaching nothing. - func snapshottingSession(id: UUID, ownerDisplayName: String, mappingGameIdentity: String) -> StreamLaunchConfiguration { + func snapshottingSession(id: UUID, ownerDisplayName: String, ownerAccountID: String, mappingGameIdentity: String) -> StreamLaunchConfiguration { var metadata = metadata metadata[Self.owningAccountDisplayNameKey] = ownerDisplayName + metadata[Self.owningAccountIDKey] = ownerAccountID metadata[Self.mappingGameIdentityKey] = mappingGameIdentity return StreamLaunchConfiguration( id: id, diff --git a/OPN/Stream/NativeGamepadMonitor.swift b/OPN/Stream/NativeGamepadMonitor.swift index 4f53e2fd6..cf0b4657b 100644 --- a/OPN/Stream/NativeGamepadMonitor.swift +++ b/OPN/Stream/NativeGamepadMonitor.swift @@ -516,6 +516,20 @@ extension NativeGamepadMonitor { return true } + /// Disables every listed device that this stream knows, in one slot refresh. The couch co-op + /// preset and the hand-off between windows both land here, so the pads another window plays + /// with are out of this one before its first poll. + func disableForStream(_ deviceIDs: Set) { + var next = streamSelection + let changed = deviceIDs.filter { next.setEnabled(false, for: $0) } + guard !changed.isEmpty else { return } + streamSelection = next + OPNStreamTelemetry.capture("webrtc.input.gamepad.stream_selection", level: .info, + message: "Controllers left out of this stream.", + attributes: ["count": String(changed.count)]) + refreshControllerSlots() + } + /// The HUD's rows for this stream: effective player per enabled device, plus the remembered rows /// for whatever is disabled, waiting or gone. var streamControllerRows: [ControllerStreamSelection.Row] { streamSelection.rows } diff --git a/OPN/Stream/NativeStreamView.swift b/OPN/Stream/NativeStreamView.swift index 85ab0c31e..d6e233de4 100644 --- a/OPN/Stream/NativeStreamView.swift +++ b/OPN/Stream/NativeStreamView.swift @@ -197,6 +197,14 @@ public final class NativeStreamView: NSView { updateControllerMappingFocus() } } + /// Couch co-op: this stream's pads keep playing while another stream window has the focus. Keyboard + /// and mouse stay with the frontmost window either way. Set by the presenter as windows come and go. + public internal(set) var allowsBackgroundGamepadInput = false { + didSet { + guard oldValue != allowsBackgroundGamepadInput else { return } + updateControllerMappingFocus() + } + } public var locksPointerWhenRelativeModeSelected = false public var confinesCursorToWindowInAbsoluteMode = false { didSet { @@ -419,8 +427,11 @@ public final class NativeStreamView: NSView { /// The provider is fixed per surface: a stream's mappings come from the game it was launched for, /// which is what keeps two accounts' concurrent games from sharing one game's overrides. - init(frame frameRect: NSRect, mappingProvider: any ControllerMappingProviding = ControllerMappingStore.shared) { + init(frame frameRect: NSRect, + mappingProvider: any ControllerMappingProviding = ControllerMappingStore.shared, + initiallyDisabledControllers: Set = []) { gamepadMonitor = NativeGamepadMonitor(mappingProvider: mappingProvider) + gamepadMonitor.disableForStream(initiallyDisabledControllers) super.init(frame: frameRect) wantsLayer = true layer?.backgroundColor = NSColor.black.cgColor diff --git a/OPN/Stream/NativeStreamViewInput.swift b/OPN/Stream/NativeStreamViewInput.swift index c5de5f063..f71b814e1 100644 --- a/OPN/Stream/NativeStreamViewInput.swift +++ b/OPN/Stream/NativeStreamViewInput.swift @@ -245,17 +245,17 @@ extension NativeStreamView { onInputEvent?(.mouse(.button(deviceID: "mouse", button: button, isPressed: isPressed, timestamp: Self.timestamp()))) } - func releasePressedInputs() { + func releasePressedInputs(includingGamepads: Bool = true) { let timestamp = Self.timestamp() let keyboardEvents = pressedKeyboardEvents.values let mouseButtons = pressedMouseButtons - let gamepadStates = activeGamepadStates.values + let gamepadStates = includingGamepads ? Array(activeGamepadStates.values) : [] pressedKeyboardEvents.removeAll() textInputKeyCodes.removeAll() pushToTalkState?.release() textInputState.cancel() updateCompositionBar() - activeGamepadStates.removeAll() + if includingGamepads { activeGamepadStates.removeAll() } preciseScrollRemainder = 0 preciseHorizontalScrollRemainder = 0 lastEmittedAbsoluteMouseEvent = nil @@ -293,13 +293,18 @@ extension NativeStreamView { func updateControllerMappingFocus() { gamepadMonitor.setMappingsEnabled(ControllerMappingFocusPolicy.allowsMappings( appIsActive: NSApplication.shared.isActive, windowIsKey: window?.isKeyWindow == true, - remoteInputEnabled: remoteInputEnabled, overlayCapturesInput: localOverlayCapturesInput + remoteInputEnabled: remoteInputEnabled, overlayCapturesInput: localOverlayCapturesInput, + allowsBackgroundPads: allowsBackgroundGamepadInput )) } func handleFocusLoss() { - gamepadMonitor.setMappingsEnabled(false) - releasePressedInputs() + if allowsBackgroundGamepadInput { + updateControllerMappingFocus() + } else { + gamepadMonitor.setMappingsEnabled(false) + } + releasePressedInputs(includingGamepads: !allowsBackgroundGamepadInput) setPointerLocked(false) applyLocalCursorPolicy() } diff --git a/OPN/Stream/OPNCouchCoopInputPolicy.swift b/OPN/Stream/OPNCouchCoopInputPolicy.swift new file mode 100644 index 000000000..828236f97 --- /dev/null +++ b/OPN/Stream/OPNCouchCoopInputPolicy.swift @@ -0,0 +1,12 @@ +// When a stream's controllers keep playing while another stream window has the focus. + +import Foundation + +enum OPNCouchCoopInputPolicy { + /// Pads are a device the system delivers to the process rather than to a window, so two streams + /// can each play their own while one holds the keyboard. That is only wanted when couch co-op is + /// on and there is a second stream for it to matter to. + static func allowsBackgroundGamepads(isFlagEnabled: Bool, liveStreamCount: Int) -> Bool { + isFlagEnabled && liveStreamCount >= 2 + } +} diff --git a/OPN/Stream/OPNGameSession.swift b/OPN/Stream/OPNGameSession.swift index a919a09d9..71e85508f 100644 --- a/OPN/Stream/OPNGameSession.swift +++ b/OPN/Stream/OPNGameSession.swift @@ -300,6 +300,7 @@ final class OPNGameSession { let ownedConfiguration = configuration.snapshottingSession( id: id, ownerDisplayName: account.displayName, + ownerAccountID: accountID.rawValue, mappingGameIdentity: ControllerMappingStore.shared.currentGameIdentity ?? "" ) if activeDiscordPresence == nil { diff --git a/OPN/Stream/OPNStreamControllerPreset.swift b/OPN/Stream/OPNStreamControllerPreset.swift new file mode 100644 index 000000000..aff3bbb3e --- /dev/null +++ b/OPN/Stream/OPNStreamControllerPreset.swift @@ -0,0 +1,47 @@ +// Which controllers a stream should play with, decided before the stream exists. +// +// The Detach New Session window picks an account and the pads to leave out of its stream. The +// stream is created later, by the launch, so the choice waits here under the account's id and is +// handed over once, when that account's stream first sees its controllers. + +import Foundation + +struct OPNStreamControllerSelection: Equatable, Sendable { + /// Pads this stream leaves out. + var disabled: Set = [] + /// Whether the pads this stream plays with are taken out of every other live stream, so one + /// tap sets a pad per player instead of the same pads answering in every window. + var takesPadsFromOtherStreams = false + + /// The pads the new stream keeps, which are the ones the others give up. + func handedOff(connected: [InputDeviceID]) -> Set { + guard takesPadsFromOtherStreams else { return [] } + return Set(connected).subtracting(disabled) + } +} + +@MainActor +final class OPNStreamControllerPreset { + static let shared = OPNStreamControllerPreset() + + private var selections: [String: OPNStreamControllerSelection] = [:] + + init() {} + + func store(_ selection: OPNStreamControllerSelection, forAccount accountID: String) { + guard !accountID.isEmpty else { return } + guard selection != OPNStreamControllerSelection() else { + selections[accountID] = nil + return + } + selections[accountID] = selection + } + + func take(forAccount accountID: String) -> OPNStreamControllerSelection { + selections.removeValue(forKey: accountID) ?? OPNStreamControllerSelection() + } + + func peek(forAccount accountID: String) -> OPNStreamControllerSelection { + selections[accountID] ?? OPNStreamControllerSelection() + } +} diff --git a/Tests/Stream/CouchCoopInputRoutingTests.swift b/Tests/Stream/CouchCoopInputRoutingTests.swift new file mode 100644 index 000000000..3e5e3b06d --- /dev/null +++ b/Tests/Stream/CouchCoopInputRoutingTests.swift @@ -0,0 +1,46 @@ +import Testing +@testable import OpenNOW + +@MainActor @Suite struct CouchCoopInputRoutingTests { + private let gamepad = UserInputEvent.gamepad(GamepadState(deviceID: "pad", playerIndex: 0, timestamp: MediaTimestamp(nanoseconds: 0))) + private let keyboard = UserInputEvent.keyboard(KeyboardEvent(deviceID: "keyboard", keyCode: 0, scanCode: 0, modifiers: [], isPressed: true, timestamp: MediaTimestamp(nanoseconds: 0))) + private let mouse = UserInputEvent.mouse(.button(deviceID: "mouse", button: .left, isPressed: true, timestamp: MediaTimestamp(nanoseconds: 0))) + + @Test func padsPlayInTheBackgroundOnlyWithTheFlagAndASecondStream() { + #expect(!OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: false, liveStreamCount: 2)) + #expect(!OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 1)) + #expect(!OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 0)) + #expect(OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 2)) + #expect(OPNCouchCoopInputPolicy.allowsBackgroundGamepads(isFlagEnabled: true, liveStreamCount: 4)) + } + + @Test func aBackgroundWindowAcceptsItsPadsButNeverTheKeyboardOrMouse() { + #expect(NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: false, allowsBackgroundGamepad: true)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(keyboard, isPictureInPictureMode: false, allowsBackgroundGamepad: true)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(mouse, isPictureInPictureMode: false, allowsBackgroundGamepad: true)) + } + + @Test func withoutCouchCoopOrPictureInPictureNothingPassesWhileNotFrontmost() { + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: false, allowsBackgroundGamepad: false)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: false)) + #expect(NativeNVSTHostViewModel.acceptsWhileNotFrontmost(gamepad, isPictureInPictureMode: true)) + #expect(!NativeNVSTHostViewModel.acceptsWhileNotFrontmost(keyboard, isPictureInPictureMode: true, allowsBackgroundGamepad: true)) + } + + @Test func mappingsFollowABackgroundPadWhileTheOverlayAndRemoteGatesStillApply() { + for overlay in [false, true] { + for remote in [false, true] { + let allowed = ControllerMappingFocusPolicy.allowsMappings( + appIsActive: false, windowIsKey: false, remoteInputEnabled: remote, + overlayCapturesInput: overlay, allowsBackgroundPads: true + ) + #expect(allowed == (remote && !overlay)) + let withoutBackground = ControllerMappingFocusPolicy.allowsMappings( + appIsActive: false, windowIsKey: false, remoteInputEnabled: remote, + overlayCapturesInput: overlay, allowsBackgroundPads: false + ) + #expect(!withoutBackground) + } + } + } +} diff --git a/Tests/Stream/OPNStreamControllerPresetTests.swift b/Tests/Stream/OPNStreamControllerPresetTests.swift new file mode 100644 index 000000000..090ee11f9 --- /dev/null +++ b/Tests/Stream/OPNStreamControllerPresetTests.swift @@ -0,0 +1,104 @@ +import Foundation +import GameController +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct OPNStreamControllerPresetTests { + private func makeStore() throws -> (store: ControllerMappingStore, defaults: UserDefaults, suite: String) { + let suite = "OPNStreamControllerPresetTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + return (ControllerMappingStore(defaults: defaults), defaults, suite) + } + + @Test func aStoredSelectionIsHandedOverOnceUnderItsAccount() { + let preset = OPNStreamControllerPreset() + let selection = OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: true) + preset.store(selection, forAccount: "account-a") + #expect(preset.peek(forAccount: "account-a") == selection) + #expect(preset.peek(forAccount: "account-b") == OPNStreamControllerSelection()) + #expect(preset.take(forAccount: "account-a") == selection) + #expect(preset.take(forAccount: "account-a") == OPNStreamControllerSelection()) + } + + @Test func anEmptySelectionClearsWhatWasStored() { + let preset = OPNStreamControllerPreset() + preset.store(OPNStreamControllerSelection(disabled: ["pad-a"]), forAccount: "account-a") + preset.store(OPNStreamControllerSelection(), forAccount: "account-a") + #expect(preset.peek(forAccount: "account-a") == OPNStreamControllerSelection()) + } + + @Test func theHandOffIsTheConnectedPadsThisStreamKeepsAndOnlyWhenAsked() { + let connected: [InputDeviceID] = ["pad-a", "pad-b", "pad-c"] + let off = OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: false) + let on = OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: true) + #expect(off.handedOff(connected: connected).isEmpty) + #expect(on.handedOff(connected: connected) == ["pad-b", "pad-c"]) + } + + @Test func disabledPadsAreOutOfAStreamBeforeItsFirstSlotIsPublished() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + let second = GCController.withExtendedGamepad() + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [first, second] }, steamDeviceIDs: { [] })) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + let secondID = try #require(registry.id(for: second)) + + monitor.disableForStream([firstID]) + + #expect(monitor.topology.playerIndices == [0]) + #expect(monitor.streamControllerRows.first { $0.id == firstID }?.state == .disabled) + #expect(monitor.streamControllerRows.first { $0.id == secondID }?.playerIndex == 0) + monitor.stop() + } + + @Test func otherStreamsLoseThePadsTheNewStreamKeeps() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + let second = GCController.withExtendedGamepad() + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [first, second] }, steamDeviceIDs: { [] })) + let running = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + let secondID = try #require(registry.id(for: second)) + let selection = OPNStreamControllerSelection(disabled: [firstID], takesPadsFromOtherStreams: true) + + running.disableForStream(selection.handedOff(connected: registry.orderedDevices.map(\.id))) + + #expect(running.streamControllerRows.first { $0.id == firstID }?.isEnabled == true) + #expect(running.streamControllerRows.first { $0.id == secondID }?.isEnabled == false) + running.stop() + } + + @Test func aReconnectedPadKeepsItsStreamState() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let first = GCController.withExtendedGamepad() + var attached = [first] + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { attached }, steamDeviceIDs: { [] })) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + let firstID = try #require(registry.id(for: first)) + monitor.disableForStream([firstID]) + + attached = [] + registry.refresh() + attached = [first] + registry.refresh() + monitor.setStreamEnabled(false, for: firstID) + + #expect(monitor.streamControllerRows.first { $0.id == firstID }?.isEnabled == false) + monitor.stop() + } + + @Test func disablingAnUnknownPadChangesNothing() throws { + let (store, defaults, suite) = try makeStore() + defer { defaults.removePersistentDomain(forName: suite) } + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [] }, steamDeviceIDs: { [] })) + let monitor = NativeGamepadMonitor(mappingProvider: store, registry: registry) + monitor.disableForStream(["not-connected"]) + #expect(monitor.streamControllerRows.isEmpty) + monitor.stop() + } +} diff --git a/Tests/Stream/StreamSessionIdentityTests.swift b/Tests/Stream/StreamSessionIdentityTests.swift index a8bb4191d..85251e532 100644 --- a/Tests/Stream/StreamSessionIdentityTests.swift +++ b/Tests/Stream/StreamSessionIdentityTests.swift @@ -38,11 +38,13 @@ private final class RecordingDiscordPresence: DiscordPresenceServing { let snapshotted = configuration.snapshottingSession( id: sessionID, ownerDisplayName: "Account A", + ownerAccountID: "account-a", mappingGameIdentity: "game-1" ) #expect(snapshotted.id == sessionID) #expect(snapshotted.owningAccountDisplayName == "Account A") + #expect(snapshotted.owningAccountID == "account-a") #expect(snapshotted.mappingGameIdentity == "game-1") #expect(snapshotted.applicationID == configuration.applicationID) #expect(snapshotted.title == configuration.title) diff --git a/View/Stream/StreamHost.swift b/View/Stream/StreamHost.swift index 19c3e351b..69111464d 100644 --- a/View/Stream/StreamHost.swift +++ b/View/Stream/StreamHost.swift @@ -80,6 +80,7 @@ struct NativeNVSTMediaStreamSurface: View { // so arriving a turn later is safe. NativeNVSTStreamHostView( mappingGameIdentity: configuration.mappingGameIdentity, + owningAccountID: configuration.owningAccountID, onResolve: { view in Task { @MainActor in model.nativeView = view @@ -166,11 +167,12 @@ private struct NativeNVSTStreamHostView: NSViewRepresentable { /// The game this surface is streaming, so its pad mappings resolve from its own overrides rather /// than from whichever game's session started last. let mappingGameIdentity: String + let owningAccountID: String let onResolve: @MainActor (NativeStreamView) -> Void var onWindowChanged: @MainActor (NSWindow?) -> Void = { _ in } func makeNSView(context: Context) -> NativeNVSTSurfaceContainerView { - let view = NativeNVSTSurfaceContainerView(frame: .zero, mappingGameIdentity: mappingGameIdentity) + let view = NativeNVSTSurfaceContainerView(frame: .zero, mappingGameIdentity: mappingGameIdentity, owningAccountID: owningAccountID) view.onResolve = onResolve view.onWindowChanged = onWindowChanged return view @@ -202,8 +204,16 @@ private struct NativeNVSTStreamHostView: NSViewRepresentable { var onWindowChanged: (@MainActor (NSWindow?) -> Void)? private var didResolve = false - init(frame frameRect: NSRect, mappingGameIdentity: String) { - streamView = NativeStreamView(frame: .zero, mappingProvider: ControllerMappingGameProvider(gameIdentity: mappingGameIdentity)) + init(frame frameRect: NSRect, mappingGameIdentity: String, owningAccountID: String) { + let selection = OPNStreamControllerPreset.shared.take(forAccount: owningAccountID) + streamView = NativeStreamView( + frame: .zero, + mappingProvider: ControllerMappingGameProvider(gameIdentity: mappingGameIdentity), + initiallyDisabledControllers: selection.disabled + ) + OPNStreamWindowPresenter.shared.takePads( + selection.handedOff(connected: ControllerMappingDevices.shared.orderedDevices.map(\.id)) + ) super.init(frame: frameRect) wantsLayer = true layer?.backgroundColor = NSColor.black.cgColor diff --git a/ViewModel/Stream/NativeNVSTHostInput.swift b/ViewModel/Stream/NativeNVSTHostInput.swift index 7fc1d2bb5..8d7a2edb5 100644 --- a/ViewModel/Stream/NativeNVSTHostInput.swift +++ b/ViewModel/Stream/NativeNVSTHostInput.swift @@ -57,6 +57,7 @@ extension NativeNVSTHostViewModel { func attachStreamWindow(_ window: NSWindow?) { guard let window = window as? OPNStreamWindow, !didEnd else { return } window.sessionSurface = self + nativeView?.allowsBackgroundGamepadInput = window.allowsBackgroundGamepadInput } /// The HUD's full-screen tile reads `streamWindowIsFullScreen` rather than the style mask, so it @@ -147,7 +148,8 @@ extension NativeNVSTHostViewModel { } guard path != nil, isConnected, !unifiedHUDVisible, !streamControlsVisible, !isEnding, !didEnd else { return } if view.remoteInputEnabled, !NativeNVSTInputDispatcher.isNeutralizing(event), - !Self.acceptsWhileNotFrontmost(event, isPictureInPictureMode: view.isPictureInPictureMode) { + !Self.acceptsWhileNotFrontmost(event, isPictureInPictureMode: view.isPictureInPictureMode, + allowsBackgroundGamepad: view.allowsBackgroundGamepadInput) { guard NSApplication.shared.isActive, view.window?.isKeyWindow == true else { return } } lastAcceptedStreamInputAt = Date() @@ -179,13 +181,14 @@ extension NativeNVSTHostViewModel { /// Whether an event reaches the game without this window being frontmost. /// - /// Only the gamepad, and only in PiP. PiP is a small floating surface that never activates the + /// Only the gamepad, and only in PiP or while couch co-op has several streams running. PiP is a small floating surface that never activates the /// app, so a game pad - a device the system delivers to the process rather than to a window - /// has to keep working while the user is in another app; that is the mode's whole point. The /// keyboard and mouse stay gated: typing or clicking in the app the user moved to must not reach /// the game. - static func acceptsWhileNotFrontmost(_ event: UserInputEvent, isPictureInPictureMode: Bool) -> Bool { - guard isPictureInPictureMode else { return false } + static func acceptsWhileNotFrontmost(_ event: UserInputEvent, isPictureInPictureMode: Bool, + allowsBackgroundGamepad: Bool = false) -> Bool { + guard isPictureInPictureMode || allowsBackgroundGamepad else { return false } if case .gamepad = event { return true } return false } diff --git a/ViewModel/Stream/StreamControllerSelectionContext.swift b/ViewModel/Stream/StreamControllerSelectionContext.swift index ea5cc8490..906390497 100644 --- a/ViewModel/Stream/StreamControllerSelectionContext.swift +++ b/ViewModel/Stream/StreamControllerSelectionContext.swift @@ -22,3 +22,14 @@ extension NativeNVSTHostViewModel { return StreamControllerSelectionContext(monitor: nativeView.gamepadMonitor) } } + +@MainActor +extension NativeNVSTHostViewModel { + func setBackgroundGamepadInput(_ isAllowed: Bool) { + nativeView?.allowsBackgroundGamepadInput = isAllowed + } + + func disableControllersForStream(_ deviceIDs: Set) { + nativeView?.gamepadMonitor.disableForStream(deviceIDs) + } +} From 1e2513d5b82f9a24353e5cf07f4983c40fd408db Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 11 Oct 2026 17:53:08 +0200 Subject: [PATCH 05/22] feat: add Stream > Detach New Session to stream a second account in its own window The new window picks a saved account that is not already streaming, lets the reader switch off controllers for that stream, and hosts that account's catalog without touching the main window's active account. Tokens are refreshed through a service of its own so no other account is activated. Co-Authored-By: Claude Sonnet 5.5 --- OPN/Services/OPNMainWindowCloseGuard.swift | 8 + OPNApp.swift | 16 + .../Settings/DetachedSessionModelTests.swift | 92 +++++ View/Catalog/CatalogView.swift | 12 +- .../DetachedSession/DetachedSessionView.swift | 340 ++++++++++++++++++ .../DetachedSessionModel.swift | 71 ++++ .../DetachedSessionRefresher.swift | 75 ++++ .../DetachedSessionViewModel.swift | 61 ++++ 8 files changed, 672 insertions(+), 3 deletions(-) create mode 100644 Tests/Settings/DetachedSessionModelTests.swift create mode 100644 View/DetachedSession/DetachedSessionView.swift create mode 100644 ViewModel/DetachedSession/DetachedSessionModel.swift create mode 100644 ViewModel/DetachedSession/DetachedSessionRefresher.swift create mode 100644 ViewModel/DetachedSession/DetachedSessionViewModel.swift diff --git a/OPN/Services/OPNMainWindowCloseGuard.swift b/OPN/Services/OPNMainWindowCloseGuard.swift index cd9537ee2..2e6823c36 100644 --- a/OPN/Services/OPNMainWindowCloseGuard.swift +++ b/OPN/Services/OPNMainWindowCloseGuard.swift @@ -22,6 +22,14 @@ enum OPNMainWindow { } } + /// Puts the window in front of everything else, for a request that starts in another window and + /// ends in this one. + static func bringForward() { + guard let window = existing() else { return } + window.makeKeyAndOrderFront(nil) + NSApp.activate(ignoringOtherApps: true) + } + /// Whether a surface outside the window has to build one before handing it work. /// /// The close button hides the window rather than tearing the scene down, so a hidden or minimized diff --git a/OPNApp.swift b/OPNApp.swift index 52f205b10..808d8fb43 100644 --- a/OPNApp.swift +++ b/OPNApp.swift @@ -20,6 +20,10 @@ struct OPNApp: App { /// changes nothing, which is what breaks the loop. @State private var isMenuBarStatusItemInserted = OPNMenuBarSessionModel.shared.isStatusItemInserted + /// Mirrors the Couch Co-op Labs flag so the Stream menu follows it live: the flag is the only + /// thing that decides whether the Detach New Session command exists. + @AppStorage(OPNLabs.couchCoop.storageKey) private var isCouchCoopEnabled = false + let sharedModelContainer: ModelContainer init() { @@ -258,6 +262,11 @@ struct OPNApp: App { Button("Join Remote Co-Op as Guest…") { openWindow(id: "remote-coop-guest") } + if isCouchCoopEnabled { + Button("Detach New Session…") { + openWindow(id: OPNDetachedSessionScene.id, value: UUID()) + } + } Button("Toggle Microphone") { _ = StreamSessionLifecycle.sendCommand(.toggleMicrophone) } @@ -302,6 +311,13 @@ struct OPNApp: App { isMenuBarStatusItemInserted = isInserted } + WindowGroup("Detached Session", id: OPNDetachedSessionScene.id, for: UUID.self) { _ in + DetachedSessionView() + .environmentObject(systemAppearance) + } + .defaultSize(width: 1100, height: 680) + .modelContainer(sharedModelContainer) + Window("Join Remote Co-Op", id: "remote-coop-guest") { RemoteCoOpGuestView() } diff --git a/Tests/Settings/DetachedSessionModelTests.swift b/Tests/Settings/DetachedSessionModelTests.swift new file mode 100644 index 000000000..8eab1adaf --- /dev/null +++ b/Tests/Settings/DetachedSessionModelTests.swift @@ -0,0 +1,92 @@ +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct DetachedSessionModelTests { + private func candidate(_ email: String, id: String?, hasSession: Bool = true) -> DetachedAccountCandidate { + DetachedAccountCandidate( + email: email, + displayName: email, + membershipTier: "Free", + accountID: id.flatMap { OPNAccountID(rawValue: $0) }, + hasSavedSession: hasSession + ) + } + + private let accountA = OPNAccountID(providerIdpId: "idp", vendorSubject: "user-a", localFallbackSubject: "a@example.com") + private let accountB = OPNAccountID(providerIdpId: "idp", vendorSubject: "user-b", localFallbackSubject: "b@example.com") + + @Test func anAccountThatOwnsAGameIsBusyAndOneWithoutASessionNeedsSignIn() throws { + let idA = try #require(accountA) + let idB = try #require(accountB) + let rows = DetachedSessionModel.rows( + candidates: [ + candidate("a@example.com", id: idA.rawValue), + candidate("b@example.com", id: idB.rawValue), + candidate("c@example.com", id: idB.rawValue, hasSession: false), + candidate("d@example.com", id: nil), + ], + ownedAccountIDs: [idA] + ) + #expect(rows.map(\.availability) == [.runningGame, .available, .signInRequired, .signInRequired]) + #expect(rows.map(\.availability.isSelectable) == [false, true, false, false]) + } + + @Test func theControllerChoiceBecomesThePresetAndTheKeptPads() { + let selection = DetachedSessionModel.selection(disabled: ["pad-a"], takesPadsFromOtherStreams: true) + #expect(selection.disabled == ["pad-a"]) + #expect(selection.takesPadsFromOtherStreams) + #expect(DetachedSessionModel.keptControllers(connected: ["pad-a", "pad-b"], disabled: ["pad-a"]) == ["pad-b"]) + } + + @Test func confirmingStoresTheSelectionUnderTheAccountAndMovesToTheCatalog() throws { + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: true) + viewModel.setController("pad-a", enabled: false) + let row = DetachedAccountRow(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available) + + viewModel.confirm(row: row, accountID: idB) + + #expect(viewModel.phase == .catalog(email: "b@example.com")) + #expect(preset.peek(forAccount: idB.rawValue) == OPNStreamControllerSelection(disabled: ["pad-a"], takesPadsFromOtherStreams: true)) + } + + @Test func aBusyAccountCannotBeConfirmed() throws { + let idA = try #require(accountA) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false) + let row = DetachedAccountRow(email: "a@example.com", displayName: "A", membershipTier: "Free", availability: .runningGame) + + viewModel.confirm(row: row, accountID: idA) + + #expect(viewModel.phase == .setup) + #expect(preset.peek(forAccount: idA.rawValue) == OPNStreamControllerSelection()) + } + + @Test func goingBackDropsTheSelectionForAStreamThatNeverStarted() throws { + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false) + viewModel.setController("pad-a", enabled: false) + let row = DetachedAccountRow(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available) + viewModel.confirm(row: row, accountID: idB) + + viewModel.backToSetup(accountID: idB) + + #expect(viewModel.phase == .setup) + #expect(preset.peek(forAccount: idB.rawValue) == OPNStreamControllerSelection()) + } + + @Test func theHandOffToggleStartsOnOnlyWhenAnotherStreamIsRunning() { + #expect(DetachedSessionViewModel(preset: OPNStreamControllerPreset(), hasOtherStreams: true).takesPadsFromOtherStreams) + #expect(!DetachedSessionViewModel(preset: OPNStreamControllerPreset(), hasOtherStreams: false).takesPadsFromOtherStreams) + } + + @Test func theCouchCoopFlagIsRegisteredAndOffByDefault() { + #expect(OPNLabs.flags.contains(OPNLabs.couchCoop)) + #expect(OPNLabs.couchCoop.id == "couchCoop") + #expect(OPNLabs.couchCoop.storageKey == "OpenNOW.Labs.couchCoop") + #expect(!OPNLabs.isEnabled(id: "a-flag-that-was-removed")) + } +} diff --git a/View/Catalog/CatalogView.swift b/View/Catalog/CatalogView.swift index 575872ad4..907cb7db5 100644 --- a/View/Catalog/CatalogView.swift +++ b/View/Catalog/CatalogView.swift @@ -138,6 +138,9 @@ struct CatalogView: View { let onForget: (LoginAccount) -> Void let onRefreshAuth: () async -> Bool let onWindowTitleChange: (String?) -> Void + /// A catalog hosted in a Detach New Session window. It belongs to one account and one window, so + /// it never takes over the menu bar's session source or its account list from the main window. + private let isDetached: Bool @Binding private var pendingGameShortcut: GFNGameShortcut? @@ -269,8 +272,10 @@ struct CatalogView: View { onSignOut: @escaping (LoginAccount) -> Void, onForget: @escaping (LoginAccount) -> Void, onRefreshAuth: @escaping () async -> Bool, - onWindowTitleChange: @escaping (String?) -> Void + onWindowTitleChange: @escaping (String?) -> Void, + isDetached: Bool = false ) { + self.isDetached = isDetached self.accounts = accounts self.signedOutAccountEmails = signedOutAccountEmails self.onSwitch = onSwitch @@ -454,7 +459,7 @@ struct CatalogView: View { await viewModel.awaitAccountScopedState() // Bound for as long as this window is on screen: the menu bar surface follows the // launch flow through it, and can hand a launch back to it while it exists. - viewModel.attachMenuBarSurface() + if !isDetached { viewModel.attachMenuBarSurface() } } // Reported from the view rather than from the fetch callbacks: the point of the signal is // that a frame carrying real content has been built, not that bytes arrived. @@ -464,6 +469,7 @@ struct CatalogView: View { } .onChange(of: pendingGameShortcut) { @MainActor _, _ in consumePendingGameShortcut() } .onChange(of: menuBarAccountsSignature, initial: true) { @MainActor _, _ in + guard !isDetached else { return } viewModel.updateMenuBarAccounts(accounts, signedOutAccountEmails: signedOutAccountEmails) } .onChange(of: viewModel.activeStreamConfiguration) { @MainActor _, _ in @@ -484,7 +490,7 @@ struct CatalogView: View { .onChange(of: cloudSync.pendingConflicts) { @MainActor _, _ in refreshSyncConflictAlert() } .onAppear { @MainActor in refreshSyncConflictAlert() } .onDisappear { @MainActor in - viewModel.detachMenuBarSurface() + if !isDetached { viewModel.detachMenuBarSurface() } onWindowTitleChange(nil) } .opnConfirmation( diff --git a/View/DetachedSession/DetachedSessionView.swift b/View/DetachedSession/DetachedSessionView.swift new file mode 100644 index 000000000..c39198b17 --- /dev/null +++ b/View/DetachedSession/DetachedSessionView.swift @@ -0,0 +1,340 @@ +// The Detach New Session window: one more account streaming beside the ones already running. +// +// Pick an account, choose the controllers this stream leaves out, then browse and launch from the +// account's own catalog. It never touches the main window's active account: the catalog is built +// for the chosen account explicitly and launches through the shared game session registry. + +import AppKit +import SwiftData +import SwiftUI + +struct DetachedSessionView: View { + @Environment(\.modelContext) private var modelContext + @Query(sort: \LoginAccount.lastLoginAt, order: .reverse) private var accounts: [LoginAccount] + @Query(sort: \LoginSession.issuedAt, order: .reverse) private var sessions: [LoginSession] + @AppStorage(OPNInterfacePreferences.uiScaleKey) private var uiScale = OPNInterfacePreferences.defaultUIScale + @AppStorage(OPNThemePreferences.accentColorKey) private var accentColorRawValue = OPNThemePreferences.AccentColor.cloudGreen.rawValue + @AppStorage(OPNThemePreferences.appearanceKey) private var appearanceRawValue = OPNThemePreferences.Appearance.dark.rawValue + @EnvironmentObject private var systemAppearance: OPNSystemAppearance + @ObservedObject private var devices = ControllerMappingDevices.shared + @State private var viewModel = DetachedSessionViewModel() + @State private var windowTitle = "Detached Session" + + private let registry = OPNGameSessionRegistry.shared + + private var rows: [DetachedAccountRow] { + DetachedSessionModel.rows(candidates: candidates, ownedAccountIDs: Set(registry.sessions.map(\.accountID))) + } + + private var candidates: [DetachedAccountCandidate] { + accounts.map { account in + DetachedAccountCandidate( + email: account.email, + displayName: account.displayName, + membershipTier: account.membershipTier, + accountID: account.resolveStableAccountID(), + hasSavedSession: savedSession(for: account) != nil + ) + } + } + + private func savedSession(for account: LoginAccount) -> LoginSession? { + sessions.first { $0.accountEmail == account.email && !$0.accessToken.isEmpty } + } + + private var preferredColorScheme: ColorScheme? { + switch OPNThemePreferences.Appearance(rawValue: appearanceRawValue) ?? .dark { + case .system: nil + case .dark: .dark + case .light: .light + } + } + + var body: some View { + let _ = OPNDesign.applyTheme( + accent: OPNThemePreferences.AccentColor(rawValue: accentColorRawValue) ?? .cloudGreen, + appearance: OPNThemePreferences.Appearance(rawValue: appearanceRawValue) ?? .dark, + systemColorScheme: systemAppearance.colorScheme + ) + Group { + switch viewModel.phase { + case .setup: + setup + case .catalog(let email): + catalog(for: email) + } + } + .frame(minWidth: 640, minHeight: 480) + .ignoresSafeArea() + .background(OPNDesign.Surface.app) + .background(DetachedSessionWindowConfigurator(title: windowTitle)) + .environment(\.opnUIScale, uiScale) + .opnObservingCornerStyle() + .opnObservingLiquidGlass() + .preferredColorScheme(preferredColorScheme) + .onAppear { devices.refresh() } + .onDisappear { viewModel.discardPendingSelection(for: selectedAccount?.storedAccountID) } + } + + private var selectedAccount: LoginAccount? { + guard let email = viewModel.selectedEmail else { return nil } + return accounts.first { $0.email == email } + } + + // MARK: - Setup + + private var setup: some View { + ScrollView { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.large(scale: uiScale)) { + header + accountSection + controllerSection + footer + } + .padding(.horizontal, OPNDesign.Spacing.pageHorizontal(scale: uiScale)) + .padding(.top, OPNDesign.Spacing.xxLarge(scale: uiScale)) + .padding(.bottom, OPNDesign.Spacing.xLarge(scale: uiScale)) + .frame(maxWidth: 720 * uiScale) + .frame(maxWidth: .infinity) + } + } + + private var header: some View { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xSmall(scale: uiScale)) { + Text("DETACH NEW SESSION") + .font(.settingsFont(size: 11 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.accentInk) + Text("Stream with another account") + .font(.settingsFont(size: 24 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Text("This window streams for the account you pick, beside the games already running. Each account plays its own session.") + .font(.settingsFont(size: 13 * uiScale)) + .foregroundStyle(OPNDesign.Text.secondary) + .fixedSize(horizontal: false, vertical: true) + } + } + + private var accountSection: some View { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.small(scale: uiScale)) { + sectionTitle("1 Account") + if rows.isEmpty { + note("No saved accounts yet. Add one to start.") + } + ForEach(rows) { row in + accountRow(row) + } + Button { + OPNMainWindow.reveal() + OPNMenuBarSessionModel.shared.requestAddAccount() + OPNMainWindow.bringForward() + } label: { + Label("Add account…", systemImage: "person.badge.plus") + } + .buttonStyle(OPNModalSecondaryButtonStyle(uiScale: uiScale)) + .help("Signs in another account in the main window without signing out the others.") + } + } + + private func accountRow(_ row: DetachedAccountRow) -> some View { + let isSelected = viewModel.selectedEmail == row.email + return Button { + guard row.availability.isSelectable else { return } + viewModel.selectedEmail = row.email + } label: { + HStack(spacing: OPNDesign.Spacing.small(scale: uiScale)) { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xxSmall(scale: uiScale)) { + Text(row.displayName.isEmpty ? row.email : row.displayName) + .font(.settingsFont(size: 14 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + .lineLimit(1) + Text("\(row.email) · \(row.availability.detail)") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + .lineLimit(1) + } + Spacer(minLength: 0) + if isSelected { + Image(systemName: "checkmark.circle.fill") + .foregroundStyle(OPNDesign.accentInk) + } + } + .padding(OPNDesign.Spacing.small(scale: uiScale)) + .frame(maxWidth: .infinity, alignment: .leading) + .background(OPNCornerShape(role: .card, scale: uiScale).fill(OPNDesign.Surface.panel)) + .overlay { + OPNCornerShape(role: .card, scale: uiScale) + .strokeBorder(isSelected ? OPNDesign.accent : OPNDesign.Stroke.subtle, lineWidth: isSelected ? 2 : 1) + } + .opacity(row.availability.isSelectable ? 1 : 0.5) + } + .buttonStyle(.plain) + .disabled(!row.availability.isSelectable) + .accessibilityLabel(row.displayName) + .accessibilityValue(row.availability.detail) + } + + private var controllerSection: some View { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.small(scale: uiScale)) { + sectionTitle("2 Controllers") + if devices.orderedDevices.isEmpty { + note("No controllers connected. Connect one and it appears here.") + } + ForEach(devices.orderedDevices) { device in + controllerRow(device) + } + if !devices.orderedDevices.isEmpty { + handOffRow + } + } + } + + private func controllerRow(_ device: ControllerMappingDevice) -> some View { + HStack(spacing: OPNDesign.Spacing.small(scale: uiScale)) { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xxSmall(scale: uiScale)) { + Text(device.name) + .font(.settingsFont(size: 14 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + .lineLimit(1) + Text(viewModel.isControllerEnabled(device.id) ? "Plays in this stream" : "Disabled in this stream") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + } + Spacer(minLength: 0) + Text("Disable in this stream") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.secondary) + Toggle( + isOn: Binding( + get: { !viewModel.isControllerEnabled(device.id) }, + set: { viewModel.setController(device.id, enabled: !$0) } + ), + uiScale: uiScale + ) + .accessibilityLabel("Disable \(device.name) in this stream") + } + .padding(OPNDesign.Spacing.small(scale: uiScale)) + .background(OPNCornerShape(role: .card, scale: uiScale).fill(OPNDesign.Surface.panel)) + .overlay { OPNCornerShape(role: .card, scale: uiScale).strokeBorder(OPNDesign.Stroke.subtle, lineWidth: 1) } + } + + private var handOffRow: some View { + HStack(spacing: OPNDesign.Spacing.small(scale: uiScale)) { + VStack(alignment: .leading, spacing: OPNDesign.Spacing.xxSmall(scale: uiScale)) { + Text("Take these controllers from the other streams") + .font(.settingsFont(size: 13 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Text("One tap sets a controller per player. Turn it back on from each stream's Controllers panel.") + .font(.settingsFont(size: 11 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + .fixedSize(horizontal: false, vertical: true) + } + Spacer(minLength: 0) + Toggle(isOn: Binding(get: { viewModel.takesPadsFromOtherStreams }, set: { viewModel.takesPadsFromOtherStreams = $0 }), uiScale: uiScale) + .accessibilityLabel("Take these controllers from the other streams") + } + .padding(OPNDesign.Spacing.small(scale: uiScale)) + } + + private var footer: some View { + HStack { + Spacer() + Button("CHOOSE A GAME") { + guard let account = selectedAccount, let accountID = account.resolveStableAccountID(), + let row = rows.first(where: { $0.email == account.email }) else { return } + viewModel.confirm(row: row, accountID: accountID) + } + .buttonStyle(VendorGetInButtonStyle(uiScale: uiScale)) + .disabled(selectedAccount == nil) + .opacity(selectedAccount == nil ? 0.46 : 1) + } + } + + private func sectionTitle(_ title: String) -> some View { + Text(title.uppercased()) + .font(.settingsFont(size: 11 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.secondary) + } + + private func note(_ text: String) -> some View { + Text(text) + .font(.settingsFont(size: 12 * uiScale)) + .foregroundStyle(OPNDesign.Text.tertiary) + } + + // MARK: - Catalog + + @ViewBuilder + private func catalog(for email: String) -> some View { + if let account = accounts.first(where: { $0.email == email }), let session = savedSession(for: account) { + CatalogView( + account: account, + session: session, + accounts: [account], + signedOutAccountEmails: [], + pendingGameShortcut: .constant(nil), + onSwitch: { _ in }, + onAddAccount: { + OPNMainWindow.reveal() + OPNMenuBarSessionModel.shared.requestAddAccount() + OPNMainWindow.bringForward() + }, + onPresentAccountChooser: {}, + onSignOut: { _ in }, + onForget: { _ in }, + onRefreshAuth: { await DetachedSessionRefresher(modelContext: modelContext).refresh(account: account, session: session) }, + onWindowTitleChange: { windowTitle = $0 ?? account.displayName }, + isDetached: true + ) + .id(session.id) + .onAppear { windowTitle = account.displayName } + } else { + VStack(spacing: OPNDesign.Spacing.medium(scale: uiScale)) { + note("This account has no saved session. Sign in again from the main window.") + Button("BACK") { viewModel.backToSetup(accountID: selectedAccount?.storedAccountID) } + .buttonStyle(OPNModalSecondaryButtonStyle(uiScale: uiScale)) + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + } +} + +/// A detached window is an ordinary titled window with the catalog's transparent titlebar, raised +/// above the borderless stream windows that cover the screens it is opened from. +private struct DetachedSessionWindowConfigurator: NSViewRepresentable { + let title: String + + func makeNSView(context: Context) -> ConfigurationView { + ConfigurationView(frame: .zero) + } + + func updateNSView(_ view: ConfigurationView, context: Context) { + view.title = title + view.apply() + } + + final class ConfigurationView: NSView { + var title = "" + + override func viewDidMoveToWindow() { + super.viewDidMoveToWindow() + apply() + } + + func apply() { + guard let window else { return } + if !window.styleMask.contains(.fullSizeContentView) { + window.styleMask.insert(.fullSizeContentView) + } + window.titleVisibility = .hidden + window.titlebarAppearsTransparent = true + window.isOpaque = true + window.backgroundColor = .black + window.level = OPNStreamBorderless.aboveCoveringLevel + if window.title != title { window.title = title } + } + } +} + +enum OPNDetachedSessionScene { + static let id = "detached-session" +} diff --git a/ViewModel/DetachedSession/DetachedSessionModel.swift b/ViewModel/DetachedSession/DetachedSessionModel.swift new file mode 100644 index 000000000..3dcb507db --- /dev/null +++ b/ViewModel/DetachedSession/DetachedSessionModel.swift @@ -0,0 +1,71 @@ +// What the Detach New Session window decides, as plain values. +// +// The window picks an account for a new stream and the controllers that stream leaves out. Which +// accounts can be picked, and what the controller choice becomes, do not need a window or a store, +// so they live here where a test can reach them. + +import Foundation + +struct DetachedAccountCandidate: Equatable, Sendable { + let email: String + let displayName: String + let membershipTier: String + let accountID: OPNAccountID? + let hasSavedSession: Bool +} + +enum DetachedAccountAvailability: Equatable, Sendable { + case available + case runningGame + case signInRequired + + var isSelectable: Bool { self == .available } + + var detail: String { + switch self { + case .available: "Ready to stream" + case .runningGame: "Already streaming in another window" + case .signInRequired: "Sign in required" + } + } +} + +struct DetachedAccountRow: Identifiable, Equatable, Sendable { + let email: String + let displayName: String + let membershipTier: String + let availability: DetachedAccountAvailability + + var id: String { email } +} + +enum DetachedSessionModel { + /// An account that already owns a game cannot start a second one, and one with no saved session + /// has nothing to stream with, so neither is offered as a choice. + static func rows(candidates: [DetachedAccountCandidate], ownedAccountIDs: Set) -> [DetachedAccountRow] { + candidates.map { candidate in + DetachedAccountRow( + email: candidate.email, + displayName: candidate.displayName, + membershipTier: candidate.membershipTier, + availability: availability(of: candidate, ownedAccountIDs: ownedAccountIDs) + ) + } + } + + static func availability(of candidate: DetachedAccountCandidate, ownedAccountIDs: Set) -> DetachedAccountAvailability { + guard candidate.hasSavedSession, let accountID = candidate.accountID else { return .signInRequired } + return ownedAccountIDs.contains(accountID) ? .runningGame : .available + } + + /// The pads the reader switched off for the new stream become its preset, and the hand-off + /// toggle says whether the pads it keeps are taken out of the streams already running. + static func selection(disabled: Set, takesPadsFromOtherStreams: Bool) -> OPNStreamControllerSelection { + OPNStreamControllerSelection(disabled: disabled, takesPadsFromOtherStreams: takesPadsFromOtherStreams) + } + + /// The pads a stream would keep, in the order the reader sees them. + static func keptControllers(connected: [InputDeviceID], disabled: Set) -> [InputDeviceID] { + connected.filter { !disabled.contains($0) } + } +} diff --git a/ViewModel/DetachedSession/DetachedSessionRefresher.swift b/ViewModel/DetachedSession/DetachedSessionRefresher.swift new file mode 100644 index 000000000..4f7edc2ce --- /dev/null +++ b/ViewModel/DetachedSession/DetachedSessionRefresher.swift @@ -0,0 +1,75 @@ +// Refreshes a saved account's tokens without making it the active account. +// +// `LoginViewModel` refreshes through one auth service whose session is the active account's, and +// re-activates the account it restores. A detached window's account is not the active one, so it +// gets a service of its own and writes the refreshed tokens back to that account's stored session +// only, leaving every `isActive` flag where it was. + +import Foundation +import SwiftData + +@MainActor +struct DetachedSessionRefresher { + let modelContext: ModelContext + + func refresh(account: LoginAccount, session storedSession: LoginSession) async -> Bool { + guard !storedSession.accessToken.isEmpty else { return false } + var jarvisSession = JarvisSession( + accessToken: storedSession.accessToken, + idToken: storedSession.idToken, + refreshToken: storedSession.refreshToken, + userId: storedSession.userId, + displayName: account.displayName, + email: account.email, + membershipTier: account.membershipTier, + idpId: storedSession.idpId.isEmpty ? account.providerIdpId : storedSession.idpId, + expiresAt: Int64(storedSession.expiresAt.timeIntervalSince1970), + isAuthenticated: true, + clientToken: storedSession.clientToken, + clientTokenExpiry: Int64(storedSession.clientTokenExpiresAt.timeIntervalSince1970 * 1000.0), + clientTokenExpiryLength: 0, + accessTokenExpiry: Int64(storedSession.expiresAt.timeIntervalSince1970 * 1000.0) + ) + if jarvisSession.idTokenExpiry == 0 { + jarvisSession.idTokenExpiry = JarvisSessionParser.idTokenExpiry(storedSession.idToken) + } + let service = JarvisAuthService(transport: JarvisURLSessionTransport()) + do { + await service.setSession(jarvisSession) + let refreshed = try await service.refreshSession(force: !jarvisSession.isIdTokenValid) + store(refreshed, in: storedSession) + OPNLog.info(.auth, "Detached session refreshed account=\(account.email)") + return true + } catch { + OPNLog.warning(.auth, "Detached session refresh failed account=\(account.email) error=\(error.localizedDescription)") + return storedSession.canContinueOffline && !storedSession.isExpired + } + } + + private func store(_ refreshed: JarvisSession, in storedSession: LoginSession) { + let now = Date() + let expiry = Date(timeIntervalSince1970: TimeInterval(refreshed.expiresAt > 0 ? refreshed.expiresAt : Int64(now.addingTimeInterval(86_400).timeIntervalSince1970))) + let clientExpiry = refreshed.clientTokenExpiry > 0 ? Date(timeIntervalSince1970: TimeInterval(refreshed.clientTokenExpiry) / 1000.0) : expiry + storedSession.updateAuthentication( + accountEmail: storedSession.accountEmail, + authMethod: storedSession.authMethod, + accessToken: refreshed.accessToken, + clientToken: refreshed.clientToken, + idToken: refreshed.idToken, + refreshToken: refreshed.refreshToken, + userId: refreshed.userId.isEmpty ? storedSession.userId : refreshed.userId, + idpId: storedSession.idpId, + deviceId: storedSession.deviceId, + issuedAt: now, + expiresAt: expiry, + clientTokenExpiresAt: clientExpiry, + isActive: storedSession.isActive, + canContinueOffline: storedSession.canContinueOffline + ) + do { + try modelContext.save() + } catch { + OPNLog.warning(.auth, "Detached session refresh could not be saved: \(error.localizedDescription)") + } + } +} diff --git a/ViewModel/DetachedSession/DetachedSessionViewModel.swift b/ViewModel/DetachedSession/DetachedSessionViewModel.swift new file mode 100644 index 000000000..6157dc0f4 --- /dev/null +++ b/ViewModel/DetachedSession/DetachedSessionViewModel.swift @@ -0,0 +1,61 @@ +// State of one Detach New Session window: which account it streams for and which controllers it +// leaves out, until the reader moves on to the catalog. + +import Foundation +import Observation + +@MainActor +@Observable +final class DetachedSessionViewModel { + enum Phase: Equatable { + case setup + case catalog(email: String) + } + + private(set) var phase: Phase = .setup + var selectedEmail: String? + private(set) var disabledControllers: Set = [] + var takesPadsFromOtherStreams: Bool + + private let preset: OPNStreamControllerPreset + + init(preset: OPNStreamControllerPreset = .shared, hasOtherStreams: Bool = OPNGameSessionRegistry.shared.hasSessions) { + self.preset = preset + takesPadsFromOtherStreams = hasOtherStreams + } + + func setController(_ id: InputDeviceID, enabled: Bool) { + if enabled { + disabledControllers.remove(id) + } else { + disabledControllers.insert(id) + } + } + + func isControllerEnabled(_ id: InputDeviceID) -> Bool { + !disabledControllers.contains(id) + } + + /// Stores the controller choice under the account, where the stream finds it when it starts, and + /// moves on to the catalog the account will launch from. + func confirm(row: DetachedAccountRow, accountID: OPNAccountID) { + guard row.availability.isSelectable else { return } + preset.store( + DetachedSessionModel.selection(disabled: disabledControllers, takesPadsFromOtherStreams: takesPadsFromOtherStreams), + forAccount: accountID.rawValue + ) + phase = .catalog(email: row.email) + } + + func backToSetup(accountID: OPNAccountID?) { + discardPendingSelection(for: accountID) + phase = .setup + } + + /// A choice made for a stream that never started must not reach a later launch by the same + /// account. Once the account owns a game the stream has taken it, so there is nothing to drop. + func discardPendingSelection(for accountID: OPNAccountID?) { + guard let accountID, !OPNGameSessionRegistry.shared.isOwned(by: accountID) else { return } + preset.store(OPNStreamControllerSelection(), forAccount: accountID.rawValue) + } +} From a8a5bfaa6ac11c83b4292d3878e902e7609d8322 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 11 Oct 2026 19:04:19 +0200 Subject: [PATCH 06/22] fix: keep the Detach window from covering the main window during add account The Detach window sat above the main window's level, so the sign-in the button started in the main window was hidden behind it. The window now steps back to an ordinary level when it hands off, and is only raised while a borderless stream window is actually covering a screen. Co-Authored-By: Claude Sonnet 5.5 --- .../DetachedSession/DetachedSessionView.swift | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/View/DetachedSession/DetachedSessionView.swift b/View/DetachedSession/DetachedSessionView.swift index c39198b17..a888f9a97 100644 --- a/View/DetachedSession/DetachedSessionView.swift +++ b/View/DetachedSession/DetachedSessionView.swift @@ -76,6 +76,16 @@ struct DetachedSessionView: View { .onDisappear { viewModel.discardPendingSelection(for: selectedAccount?.storedAccountID) } } + /// The sign-in belongs to the main window, so this window steps back to an ordinary level first: + /// raised above the stream windows it would sit over the main window and hide the very flow it + /// just started. + private static func addAccountInMainWindow() { + NSApp.keyWindow?.level = .normal + OPNMainWindow.reveal() + OPNMenuBarSessionModel.shared.requestAddAccount() + OPNMainWindow.bringForward() + } + private var selectedAccount: LoginAccount? { guard let email = viewModel.selectedEmail else { return nil } return accounts.first { $0.email == email } @@ -124,9 +134,7 @@ struct DetachedSessionView: View { accountRow(row) } Button { - OPNMainWindow.reveal() - OPNMenuBarSessionModel.shared.requestAddAccount() - OPNMainWindow.bringForward() + Self.addAccountInMainWindow() } label: { Label("Add account…", systemImage: "person.badge.plus") } @@ -273,11 +281,7 @@ struct DetachedSessionView: View { signedOutAccountEmails: [], pendingGameShortcut: .constant(nil), onSwitch: { _ in }, - onAddAccount: { - OPNMainWindow.reveal() - OPNMenuBarSessionModel.shared.requestAddAccount() - OPNMainWindow.bringForward() - }, + onAddAccount: { Self.addAccountInMainWindow() }, onPresentAccountChooser: {}, onSignOut: { _ in }, onForget: { _ in }, @@ -329,7 +333,8 @@ private struct DetachedSessionWindowConfigurator: NSViewRepresentable { window.titlebarAppearsTransparent = true window.isOpaque = true window.backgroundColor = .black - window.level = OPNStreamBorderless.aboveCoveringLevel + let hasCoveringStream = OPNStreamWindowPresenter.shared.windows.values.contains { $0.layoutState.isBorderless } + window.level = hasCoveringStream ? OPNStreamBorderless.aboveCoveringLevel : .normal if window.title != title { window.title = title } } } From 413e92e719a92fa354ce96590950d6c7df6df51d Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 08:43:05 +0100 Subject: [PATCH 07/22] fix(deps): cap swift-collections below 1.7 so tests load on macOS 26 swift-collections 1.7.0 gates code on compiler(>=6.4) without an availability check. Built with Xcode 27 (Swift 6.4), the test bundle references _swift_initBorrow, which only the macOS 27 runtime exports, so dlopen of the bundle fails on macOS 26. Co-Authored-By: Claude Sonnet 5.5 --- Package.resolved | 6 +++--- Package.swift | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Package.resolved b/Package.resolved index 2d6ba862a..4db25ccff 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "01123e69d8484f54232a3318f5e52e33432de29594af053dffbffb78d91e122b", + "originHash" : "9672e9ed34fe1ec686d49e0e3d5dcd539eda6e3d5190cfa0d1d2e0bd7da0cffa", "pins" : [ { "identity" : "ably-cocoa", @@ -60,8 +60,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-collections.git", "state" : { - "revision" : "a66de878e87ef5a3d5d390e0f6d9002aa5541a43", - "version" : "1.7.0" + "revision" : "a0cb0954ecb21e4e31b0070e6ed5674e8556685a", + "version" : "1.6.0" } }, { diff --git a/Package.swift b/Package.swift index 7829b1b55..ca4217b7c 100644 --- a/Package.swift +++ b/Package.swift @@ -26,7 +26,12 @@ let package = Package( // Hosted signaling (see RemoteCoOp/hosted-signaling-plan.md). Pinned exactly: this carries // the signaling for a live session, and an unattended minor bump is not something to // discover mid-stream. - .package(url: "https://github.com/ably/ably-cocoa.git", exact: "1.4.0") + .package(url: "https://github.com/ably/ably-cocoa.git", exact: "1.4.0"), + // Transitive (swift-nio), capped below 1.7: 1.7.0 gates code on `compiler(>=6.4)` without an + // availability check, so on Xcode 27 the test bundle references `_swift_initBorrow` and fails + // to load on macOS 26 (the symbol only ships in the macOS 27 runtime). Lift the cap once + // 1.7.x is safe on macOS 26 for the toolchains we build with. + .package(url: "https://github.com/apple/swift-collections.git", "1.0.0"..<"1.7.0") ], targets: [ // The native NVST bundle's protocol libraries (milestone 4), built as *static* frameworks From 2206695c8a3702e4bf22f45407d41d76ad3e7d16 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 08:51:43 +0100 Subject: [PATCH 08/22] docs: move swift-collections cap rationale from Package.swift to README Co-Authored-By: Claude Sonnet 5.5 --- Package.swift | 4 ---- README.md | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index ca4217b7c..44e3069fc 100644 --- a/Package.swift +++ b/Package.swift @@ -27,10 +27,6 @@ let package = Package( // the signaling for a live session, and an unattended minor bump is not something to // discover mid-stream. .package(url: "https://github.com/ably/ably-cocoa.git", exact: "1.4.0"), - // Transitive (swift-nio), capped below 1.7: 1.7.0 gates code on `compiler(>=6.4)` without an - // availability check, so on Xcode 27 the test bundle references `_swift_initBorrow` and fails - // to load on macOS 26 (the symbol only ships in the macOS 27 runtime). Lift the cap once - // 1.7.x is safe on macOS 26 for the toolchains we build with. .package(url: "https://github.com/apple/swift-collections.git", "1.0.0"..<"1.7.0") ], targets: [ diff --git a/README.md b/README.md index 2f3509b21..53beb4e5c 100644 --- a/README.md +++ b/README.md @@ -350,6 +350,8 @@ Run the package tests from the repository root so SwiftPM uses one shared `.buil swift test --scratch-path .build/shared ``` +`swift-collections` (a transitive dependency of swift-nio) is capped below 1.7 in `Package.swift`. Version 1.7.0 gates code on `compiler(>=6.4)` without an availability check, so on Xcode 27 the test bundle references `_swift_initBorrow` and fails to load on macOS 26, where that symbol is missing (it only ships in the macOS 27 runtime). Lift the cap once 1.7.x is safe on macOS 26 for the toolchains we build with. +
Project layout, packages, and tooling From b33133fd8ae509ea3fa34e35b8323de32518aae9 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 10:58:17 +0100 Subject: [PATCH 09/22] test: stop the close-guard test from ending the test host on macOS 26 Driving performClose(_:) on a stream window made the swiftpm-testing-helper process exit 0 mid-run, so the rest of the suite never ran and no summary was printed. Call the guard's windowShouldClose directly instead. Co-Authored-By: Claude Sonnet 5.5 --- Tests/Stream/OPNStreamPictureInPictureTests.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/Stream/OPNStreamPictureInPictureTests.swift b/Tests/Stream/OPNStreamPictureInPictureTests.swift index 54df79063..e758e408d 100644 --- a/Tests/Stream/OPNStreamPictureInPictureTests.swift +++ b/Tests/Stream/OPNStreamPictureInPictureTests.swift @@ -196,7 +196,9 @@ struct OPNStreamPictureInPictureTests { window.makeKeyAndOrderFront(nil) await settleRunLoop() - window.standardWindowButton(.closeButton)?.performClick(nil) + // What the close button sends. `performClose(_:)` itself is not driven here: on macOS 26 it + // ends the test host's process once the run loop turns, taking the rest of the run with it. + #expect(window.delegate?.windowShouldClose?(window) == false) await settleRunLoop() #expect(handlerCalls == 1, "the close button has to reach the guard's handler") From c2eb3b9a4cfaec64fbf0e7b0ee1237ba623996b7 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 10:58:17 +0100 Subject: [PATCH 10/22] test: stop the close-guard test from ending the test host on macOS 26 Driving performClose(_:) on a stream window made the swiftpm-testing-helper process exit 0 mid-run, so the rest of the suite never ran and no summary was printed. Call the guard's windowShouldClose directly instead. Co-Authored-By: Claude Sonnet 5.5 --- Tests/Stream/OPNStreamPictureInPictureTests.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/Stream/OPNStreamPictureInPictureTests.swift b/Tests/Stream/OPNStreamPictureInPictureTests.swift index 54df79063..e758e408d 100644 --- a/Tests/Stream/OPNStreamPictureInPictureTests.swift +++ b/Tests/Stream/OPNStreamPictureInPictureTests.swift @@ -196,7 +196,9 @@ struct OPNStreamPictureInPictureTests { window.makeKeyAndOrderFront(nil) await settleRunLoop() - window.standardWindowButton(.closeButton)?.performClick(nil) + // What the close button sends. `performClose(_:)` itself is not driven here: on macOS 26 it + // ends the test host's process once the run loop turns, taking the rest of the run with it. + #expect(window.delegate?.windowShouldClose?(window) == false) await settleRunLoop() #expect(handlerCalls == 1, "the close button has to reach the guard's handler") From 0cacb2754d4b8794fa321a8203375fb3a8d6d2a5 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 11:23:26 +0100 Subject: [PATCH 11/22] fix: don't treat a move before the first tile lands as the reader taking the window Co-Authored-By: Claude Sonnet 5.5 --- View/Stream/OPNStreamWindowPresenter.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/View/Stream/OPNStreamWindowPresenter.swift b/View/Stream/OPNStreamWindowPresenter.swift index d0ce559d0..363a37776 100644 --- a/View/Stream/OPNStreamWindowPresenter.swift +++ b/View/Stream/OPNStreamWindowPresenter.swift @@ -364,7 +364,9 @@ final class OPNStreamWindowPresenter { /// A tiled window that is no longer where the layout put it was taken by the reader, and stays /// theirs: the layout stops placing it. private static func noteFrameChange(_ window: OPNStreamWindow) { - guard window.layoutState.isAutoTiled, !window.isAtLayoutFrame else { return } + // No layout frame yet means the placement is still waiting on the geometry gate, so a move + // that lands before it is not the reader's. + guard window.layoutState.isAutoTiled, window.layoutFrame != nil, !window.isAtLayoutFrame else { return } window.layoutState.isAutoTiled = false window.layoutState.isManuallyPlaced = true window.clearLayoutFrame() From 7a36e918d96c4c25dceb54ce8921c1b5be7dee91 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 12:00:44 +0100 Subject: [PATCH 12/22] feat: let couch co-op tiles sit below the menu bar or fill the screens, and retile live Tiles now divide each screen's visible area by default. A Full Screen option raises every tile over the menu bar and Dock so all streams stay visible together, and the HUD tile flips it for tiled windows. Changing the layout settings retiles open windows at once. The Couch Co-op flag asks for a restart when switched. The Detach window re-reads its level when a stream window takes or leaves the covering level instead of only on a SwiftUI update. Co-Authored-By: Claude Sonnet 5.5 --- OPN/Services/OPNAppRelaunch.swift | 30 +++++++ OPN/Services/OPNLabs.swift | 6 +- OPN/Stream/NativeStreamView.swift | 5 +- OPN/Stream/OPNGameSession.swift | 3 +- OPN/Stream/OPNStreamBorderless.swift | 19 ++++- OPN/Stream/OPNStreamTileLayout.swift | 33 ++++++++ Tests/Stream/CouchCoopBorderlessTests.swift | 77 ++++++++++++++++++ .../DetachedSession/DetachedSessionView.swift | 26 +++++- View/Settings/SettingsLabsViews.swift | 81 +++++++++++++------ .../NativeNVSTStreamHUD+ActionTiles.swift | 6 +- View/Stream/OPNStreamWindowPresenter.swift | 17 +++- .../NativeNVSTHostViewModel+Borderless.swift | 59 ++++++++++++++ .../NativeNVSTHostViewModel+Controls.swift | 23 ------ 13 files changed, 319 insertions(+), 66 deletions(-) create mode 100644 OPN/Services/OPNAppRelaunch.swift create mode 100644 ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift diff --git a/OPN/Services/OPNAppRelaunch.swift b/OPN/Services/OPNAppRelaunch.swift new file mode 100644 index 000000000..337e77872 --- /dev/null +++ b/OPN/Services/OPNAppRelaunch.swift @@ -0,0 +1,30 @@ +// Restarts the app, for a setting that is only read once at launch. + +import AppKit + +@MainActor +enum OPNAppRelaunch { + /// How long the helper waits for this process to exit before giving up, so a quit the reader + /// cancels does not leave a relaunch pending forever. + static let waitSeconds = 60 + + /// The shell line that outlives this process: it waits for `pid` to go, then opens the bundle. + /// The bundle path travels as an argument rather than inside the script text. + static func helperArguments(pid: Int32) -> [String] { + let script = "i=0; while kill -0 \(pid) 2>/dev/null && [ $i -lt \(waitSeconds * 5) ]; do sleep 0.2; i=$((i+1)); done; /usr/bin/open \"$0\"" + return ["-c", script, Bundle.main.bundlePath] + } + + static func relaunch() { + let helper = Process() + helper.executableURL = URL(fileURLWithPath: "/bin/sh") + helper.arguments = helperArguments(pid: ProcessInfo.processInfo.processIdentifier) + do { + try helper.run() + } catch { + OPNLog.warning(.app, "Relaunch helper could not start: \(error.localizedDescription)") + return + } + NSApp.terminate(nil) + } +} diff --git a/OPN/Services/OPNLabs.swift b/OPN/Services/OPNLabs.swift index 05859fef4..7117c4ef0 100644 --- a/OPN/Services/OPNLabs.swift +++ b/OPN/Services/OPNLabs.swift @@ -15,6 +15,9 @@ struct OPNLabsFlag: Identifiable, Equatable, Sendable { let summary: String /// The release it went on trial in, so a flag nobody promoted is visible as stale. let since: String + /// Whether the flag is only read as the app starts, so switching it asks for a restart rather + /// than trying to change what is already running. + var requiresRestart = false var storageKey: String { "OpenNOW.Labs.\(id)" } } @@ -37,7 +40,8 @@ enum OPNLabs { id: "couchCoop", title: "Couch Co-op", summary: "Adds Stream > Detach New Session, which opens a window for a second or third account. Each window takes its own controllers, plays while another window is in front, and the stream windows tile across your screens.", - since: "0.15" + since: "0.15", + requiresRestart: true ) /// Every trial in flight. Empty is the normal state, and the Settings rail drops the Labs diff --git a/OPN/Stream/NativeStreamView.swift b/OPN/Stream/NativeStreamView.swift index 06324ea88..92d01e58f 100644 --- a/OPN/Stream/NativeStreamView.swift +++ b/OPN/Stream/NativeStreamView.swift @@ -200,10 +200,7 @@ public final class NativeStreamView: NSView { /// Couch co-op: this stream's pads keep playing while another stream window has the focus. Keyboard /// and mouse stay with the frontmost window either way. Set by the presenter as windows come and go. public internal(set) var allowsBackgroundGamepadInput = false { - didSet { - guard oldValue != allowsBackgroundGamepadInput else { return } - updateControllerMappingFocus() - } + didSet { if oldValue != allowsBackgroundGamepadInput { updateControllerMappingFocus() } } } public var locksPointerWhenRelativeModeSelected = false public var confinesCursorToWindowInAbsoluteMode = false { diff --git a/OPN/Stream/OPNGameSession.swift b/OPN/Stream/OPNGameSession.swift index f30a37c26..999d860b1 100644 --- a/OPN/Stream/OPNGameSession.swift +++ b/OPN/Stream/OPNGameSession.swift @@ -303,8 +303,7 @@ final class OPNGameSession { ) let ownedConfiguration = configuration.snapshottingSession( id: id, - ownerDisplayName: account.displayName, - ownerAccountID: accountID.rawValue, + ownerDisplayName: account.displayName, ownerAccountID: accountID.rawValue, mappingGameIdentity: ControllerMappingStore.shared.currentGameIdentity ?? "" ) if activeDiscordPresence == nil { diff --git a/OPN/Stream/OPNStreamBorderless.swift b/OPN/Stream/OPNStreamBorderless.swift index 34723ec62..4b53aefcd 100644 --- a/OPN/Stream/OPNStreamBorderless.swift +++ b/OPN/Stream/OPNStreamBorderless.swift @@ -31,8 +31,11 @@ enum OPNStreamBorderless { } /// Puts the window into borderless mode at `frame`, or moves an already borderless window to a - /// new one. What was there before the first entry is kept for the way out. - static func enter(_ window: OPNStreamWindow, frame: NSRect, screenFrames: [NSRect] = NSScreen.screens.map(\.frame)) { + /// new one. What was there before the first entry is kept for the way out. `coversMenuBar` raises + /// the window over the menu bar and the Dock even when it is only one tile of a screen, which is + /// how tiles fill a screen together; without it only a window that fills a screen is raised. + static func enter(_ window: OPNStreamWindow, frame: NSRect, coversMenuBar: Bool = false, + screenFrames: [NSRect] = NSScreen.screens.map(\.frame)) { if window.borderlessRestoreState == nil { window.borderlessRestoreState = OPNStreamWindow.WindowedState( level: window.level, @@ -46,15 +49,17 @@ enum OPNStreamBorderless { window.layoutState.isBorderless = true StreamWindowGeometryGate.releaseAspectRatioLock(window) OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: true) - let level = level(for: frame, screenFrames: screenFrames) + let level = coversMenuBar ? coveringLevel : level(for: frame, screenFrames: screenFrames) window.level = level for child in window.childWindows ?? [] { child.level = level } apply(frame: frame, to: window) + postCoveringStateChange() } static func exit(_ window: OPNStreamWindow) { guard window.layoutState.isBorderless else { return } window.layoutState.isBorderless = false + defer { postCoveringStateChange() } OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: false) guard let state = window.borderlessRestoreState else { window.level = .normal @@ -68,6 +73,14 @@ enum OPNStreamBorderless { if state.isMiniaturized { window.miniaturize(nil) } } + /// Posted whenever a stream window takes or gives up the covering level, so a window that has to + /// stay reachable above it re-reads its level then instead of on a timer. + static let coveringStateDidChange = Notification.Name("OpenNOW.StreamBorderless.coveringStateDidChange") + + static func postCoveringStateChange() { + NotificationCenter.default.post(name: coveringStateDidChange, object: nil) + } + /// The window's own toggle, from the HUD: the window is the reader's from here on, so the layout /// stops placing it whichever way it goes. static func toggleByReader(_ window: OPNStreamWindow) { diff --git a/OPN/Stream/OPNStreamTileLayout.swift b/OPN/Stream/OPNStreamTileLayout.swift index 87e9d7fde..6f054ef58 100644 --- a/OPN/Stream/OPNStreamTileLayout.swift +++ b/OPN/Stream/OPNStreamTileLayout.swift @@ -32,6 +32,39 @@ enum OPNStreamTileOrientation: String, CaseIterable, Identifiable, Sendable { } } +/// Whether tiled windows stay below the menu bar and the Dock, or fill each screen over them. +enum OPNStreamTileCoverage: String, CaseIterable, Identifiable, Sendable { + case visibleArea + case fullScreen + + static let storageKey = "OpenNOW.CouchCoop.Coverage" + + var id: String { rawValue } + + var title: String { + switch self { + case .visibleArea: "Below the Menu Bar" + case .fullScreen: "Full Screen" + } + } + + static var stored: OPNStreamTileCoverage { + OPNAppPreferenceStorage.standard.string(forKey: storageKey).flatMap(Self.init(rawValue:)) ?? .visibleArea + } + + static func store(_ coverage: OPNStreamTileCoverage) { + OPNAppPreferenceStorage.standard.set(coverage.rawValue, forKey: storageKey) + } + + /// The area the tiles of each screen divide: the whole frame when covering, the part the menu + /// bar and Dock leave free otherwise. Both lists are in screen order. + func areas(frames: [CGRect], visibleFrames: [CGRect]) -> [CGRect] { + self == .fullScreen ? frames : visibleFrames + } + + var flipped: OPNStreamTileCoverage { self == .fullScreen ? .visibleArea : .fullScreen } +} + struct OPNStreamTile: Equatable, Sendable { let frame: CGRect let screenIndex: Int diff --git a/Tests/Stream/CouchCoopBorderlessTests.swift b/Tests/Stream/CouchCoopBorderlessTests.swift index 7ab114d6e..9c20718f4 100644 --- a/Tests/Stream/CouchCoopBorderlessTests.swift +++ b/Tests/Stream/CouchCoopBorderlessTests.swift @@ -95,4 +95,81 @@ import Testing window.clearLayoutFrame() #expect(!window.isAtLayoutFrame) } + + @Test func tileCoverageDividesTheVisibleAreaOrTheWholeFrame() { + let frames = [NSRect(x: 0, y: 0, width: 1920, height: 1080)] + let visible = [NSRect(x: 0, y: 70, width: 1920, height: 985)] + #expect(OPNStreamTileCoverage.visibleArea.areas(frames: frames, visibleFrames: visible) == visible) + #expect(OPNStreamTileCoverage.fullScreen.areas(frames: frames, visibleFrames: visible) == frames) + #expect(OPNStreamTileCoverage.visibleArea.flipped == .fullScreen) + #expect(OPNStreamTileCoverage.fullScreen.flipped == .visibleArea) + } + + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func aTileThatCoversTheMenuBarIsRaisedEvenWhenItSharesAScreen() async throws { + let suite = "OpenNOWTests.BorderlessCover.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + defer { defaults.removePersistentDomain(forName: suite) } + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: defaults) + let half = NSRect(x: 0, y: 0, width: 960, height: 1080) + + OPNStreamBorderless.enter(window, frame: half, coversMenuBar: true, screenFrames: screens) + #expect(window.level == OPNStreamBorderless.coveringLevel) + + OPNStreamBorderless.enter(window, frame: half, coversMenuBar: false, screenFrames: screens) + #expect(window.level == .normal) + OPNStreamBorderless.exit(window) + } + + @Test func enteringAndLeavingBorderlessAnnouncesTheCoveringStateChange() async throws { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + var posts = 0 + let token = NotificationCenter.default.addObserver(forName: OPNStreamBorderless.coveringStateDidChange, object: nil, queue: nil) { _ in posts += 1 } + defer { NotificationCenter.default.removeObserver(token) } + + OPNStreamBorderless.enter(window, frame: NSRect(x: 0, y: 0, width: 640, height: 360), screenFrames: screens) + OPNStreamBorderless.exit(window) + + #expect(posts == 2) + } + + /// A tile whose placement is still waiting on the geometry gate has no layout frame yet. A move + /// that lands in that gap is not the reader taking the window. + @Test func aMoveBeforeTheFirstTileLandsKeepsTheWindowTiled() { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + window.layoutState.isAutoTiled = true + + OPNStreamWindowPresenter.noteFrameChange(window) + + #expect(window.layoutState.isAutoTiled) + #expect(!window.layoutState.isManuallyPlaced) + } + + @Test func aMoveAwayFromAPlacedTileHandsTheWindowToTheReader() { + let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) + window.layoutState.isAutoTiled = true + window.setFrameFromLayout(NSRect(x: 50, y: 50, width: 640, height: 360)) + + OPNStreamWindowPresenter.noteFrameChange(window) + #expect(window.layoutState.isAutoTiled) + + window.setFrame(NSRect(x: 90, y: 50, width: 640, height: 360), display: false) + OPNStreamWindowPresenter.noteFrameChange(window) + + #expect(!window.layoutState.isAutoTiled) + #expect(window.layoutState.isManuallyPlaced) + } + + @Test func theRelaunchHelperWaitsForThisProcessAndOpensTheBundleFromAnArgument() { + let arguments = OPNAppRelaunch.helperArguments(pid: 4242) + #expect(arguments.first == "-c") + #expect(arguments[1].contains("kill -0 4242")) + #expect(arguments[1].contains("open \"$0\"")) + #expect(arguments.last == Bundle.main.bundlePath) + } + + @Test func onlyTheCouchCoopFlagAsksForARestart() { + #expect(OPNLabs.couchCoop.requiresRestart) + #expect(!OPNLabs.clipboardCapture.requiresRestart) + } } diff --git a/View/DetachedSession/DetachedSessionView.swift b/View/DetachedSession/DetachedSessionView.swift index a888f9a97..98b124195 100644 --- a/View/DetachedSession/DetachedSessionView.swift +++ b/View/DetachedSession/DetachedSessionView.swift @@ -318,10 +318,26 @@ private struct DetachedSessionWindowConfigurator: NSViewRepresentable { final class ConfigurationView: NSView { var title = "" + private var levelObserver: NSObjectProtocol? override func viewDidMoveToWindow() { super.viewDidMoveToWindow() + guard window != nil else { + if let levelObserver { NotificationCenter.default.removeObserver(levelObserver) } + levelObserver = nil + return + } apply() + // The level only matters when a stream window takes or gives up the covering level, and + // the stream windows say so. Reading it then keeps this window reachable without a timer. + if levelObserver == nil { + // swiftlint:disable:next view_owns_long_lived_effect -- removed when the view leaves its window; an NSView has no SwiftUI lifetime to tie it to + levelObserver = NotificationCenter.default.addObserver( + forName: OPNStreamBorderless.coveringStateDidChange, object: nil, queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.applyLevel() } + } + } } func apply() { @@ -333,10 +349,16 @@ private struct DetachedSessionWindowConfigurator: NSViewRepresentable { window.titlebarAppearsTransparent = true window.isOpaque = true window.backgroundColor = .black - let hasCoveringStream = OPNStreamWindowPresenter.shared.windows.values.contains { $0.layoutState.isBorderless } - window.level = hasCoveringStream ? OPNStreamBorderless.aboveCoveringLevel : .normal + applyLevel() if window.title != title { window.title = title } } + + private func applyLevel() { + guard let window else { return } + let hasCoveringStream = OPNStreamWindowPresenter.shared.windows.values.contains { $0.layoutState.isBorderless } + let level = hasCoveringStream ? OPNStreamBorderless.aboveCoveringLevel : NSWindow.Level.normal + if window.level != level { window.level = level } + } } } diff --git a/View/Settings/SettingsLabsViews.swift b/View/Settings/SettingsLabsViews.swift index f9d069242..6eb6e2ad5 100644 --- a/View/Settings/SettingsLabsViews.swift +++ b/View/Settings/SettingsLabsViews.swift @@ -37,12 +37,13 @@ struct LabsSettingsPage: View { @AppStorage(OPNLabs.couchCoop.storageKey) private var isCouchCoopEnabled = false } -/// How two or more stream windows share a screen. Shown only while Couch Co-op is on, since nothing -/// else reads it. +/// How two or more stream windows share the screens. Shown only while Couch Co-op is on, since +/// nothing else reads it. A change places the windows that are already open at once. struct CouchCoopTilingCard: View { let uiScale: CGFloat @State private var orientation = OPNStreamTileOrientation.auto + @State private var coverage = OPNStreamTileCoverage.visibleArea var body: some View { SettingsCard(title: "Couch Co-op Layout", badge: .experimental, uiScale: uiScale) { @@ -51,32 +52,47 @@ struct CouchCoopTilingCard: View { .foregroundStyle(OPNDesign.Text.tertiary) .fixedSize(horizontal: false, vertical: true) SettingsDivider(uiScale: uiScale) - HStack { - Text("Split shared screens") - .font(.settingsFont(size: 13 * uiScale, weight: .bold)) - .foregroundStyle(OPNDesign.Text.primary) - Spacer(minLength: 0) - OPNDropdownMenu(items: OPNStreamTileOrientation.allCases.map { option in - OPNDropdownItem(id: option.id, title: option.title, isSelected: option == orientation) { - orientation = option - OPNStreamTileOrientation.store(option) - } - }) { - HStack(spacing: OPNDesign.Spacing.xSmall(scale: uiScale)) { - Text(orientation.title) - .font(.settingsFont(size: 12 * uiScale, weight: .bold)) - .foregroundStyle(OPNDesign.Text.primary) - Image(systemName: "chevron.down") - .font(.settingsFont(size: 9 * uiScale, weight: .bold)) - } - .padding(.horizontal, OPNDesign.Spacing.controlRow(scale: uiScale)) - .frame(height: 30 * uiScale) - .background(OPNCornerShape(role: .control, scale: uiScale).fill(OPNDesign.Fill.neutral(0.075))) - .overlay { OPNCornerShape(role: .control, scale: uiScale).strokeBorder(OPNDesign.Stroke.regular, lineWidth: 1) } + dropdownRow("Split shared screens", selection: orientation.title, items: OPNStreamTileOrientation.allCases.map { option in + OPNDropdownItem(id: option.id, title: option.title, isSelected: option == orientation) { + orientation = option + OPNStreamTileOrientation.store(option) + OPNStreamWindowPresenter.shared.retileWindows() + } + }) + SettingsDivider(uiScale: uiScale) + dropdownRow("Stream windows sit", selection: coverage.title, items: OPNStreamTileCoverage.allCases.map { option in + OPNDropdownItem(id: option.id, title: option.title, isSelected: option == coverage) { + coverage = option + OPNStreamWindowPresenter.shared.setTileCoverage(option) } + }) + } + .onAppear { + orientation = OPNStreamTileOrientation.stored + coverage = OPNStreamTileCoverage.stored + } + } + + private func dropdownRow(_ title: String, selection: String, items: [OPNDropdownItem]) -> some View { + HStack { + Text(title) + .font(.settingsFont(size: 13 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Spacer(minLength: 0) + OPNDropdownMenu(items: items) { + HStack(spacing: OPNDesign.Spacing.xSmall(scale: uiScale)) { + Text(selection) + .font(.settingsFont(size: 12 * uiScale, weight: .bold)) + .foregroundStyle(OPNDesign.Text.primary) + Image(systemName: "chevron.down") + .font(.settingsFont(size: 9 * uiScale, weight: .bold)) + } + .padding(.horizontal, OPNDesign.Spacing.controlRow(scale: uiScale)) + .frame(height: 30 * uiScale) + .background(OPNCornerShape(role: .control, scale: uiScale).fill(OPNDesign.Fill.neutral(0.075))) + .overlay { OPNCornerShape(role: .control, scale: uiScale).strokeBorder(OPNDesign.Stroke.regular, lineWidth: 1) } } } - .onAppear { orientation = OPNStreamTileOrientation.stored } } } @@ -85,6 +101,7 @@ struct LabsFlagRow: View { let uiScale: CGFloat @State private var isEnabled = false + @State private var isRestartPromptPresented = false var body: some View { SettingsToggleRow( @@ -95,8 +112,22 @@ struct LabsFlagRow: View { ) { enabled in isEnabled = enabled OPNLabs.setEnabled(flag, enabled) + if flag.requiresRestart { isRestartPromptPresented = true } } .onAppear { isEnabled = OPNLabs.isEnabled(flag) } + .opnConfirmation( + isPresented: $isRestartPromptPresented, + eyebrow: "LABS", + title: "Restart to apply \(flag.title)?", + message: "\(flag.title) takes full effect when OpenNOW starts. Restarting ends any stream that is running.", + actions: [ + OPNConfirmationAction("LATER", role: .cancel) { isRestartPromptPresented = false }, + OPNConfirmationAction("RESTART NOW") { + isRestartPromptPresented = false + OPNAppRelaunch.relaunch() + } + ] + ) } } diff --git a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift index acfe11c75..9cb86deaa 100644 --- a/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift +++ b/View/Stream/NativeNVSTStreamHUD+ActionTiles.swift @@ -42,10 +42,10 @@ extension NativeNVSTMediaStreamSurface { guard model.isBorderlessTileAvailable else { return tiles } return tiles + [ NativeHUDTile(id: "borderless-full-screen", - title: model.isBorderlessWindow ? "Leave Borderless" : "Borderless Full Screen", - subtitle: model.isBorderlessWindow ? "Borderless" : "Fill the screen, keep the Spaces", + title: model.borderlessTileTitle, + subtitle: model.borderlessTileSubtitle, systemName: "rectangle.dashed", - isActive: model.isBorderlessWindow, + isActive: model.isBorderlessTileActive, isDisabled: model.isBorderlessTileDisabled, action: model.toggleBorderlessFullScreen), ] diff --git a/View/Stream/OPNStreamWindowPresenter.swift b/View/Stream/OPNStreamWindowPresenter.swift index 363a37776..2bcdf9ce1 100644 --- a/View/Stream/OPNStreamWindowPresenter.swift +++ b/View/Stream/OPNStreamWindowPresenter.swift @@ -191,6 +191,7 @@ final class OPNStreamWindowPresenter { hostingViews[sessionID] = nil windows[sessionID] = nil windowOrder.removeAll { $0 == sessionID } + OPNStreamBorderless.postCoveringStateChange() OPNStreamWindowCloseGuard.uninstall(from: window) window.closeRequestHandler = nil window.sessionSurface = nil @@ -303,21 +304,31 @@ final class OPNStreamWindowPresenter { for window in ordered where window.layoutState.isAutoTiled { releaseTile(window) } return } + let coverage = OPNStreamTileCoverage.stored let tiles = OPNStreamTileLayout.tiles( - screens: NSScreen.screens.map(\.frame), + screens: coverage.areas(frames: NSScreen.screens.map(\.frame), visibleFrames: NSScreen.screens.map(\.visibleFrame)), windowCount: placeable.count, orientation: OPNStreamTileOrientation.stored ) guard tiles.count == placeable.count else { return } for (window, tile) in zip(placeable, tiles) { window.layoutState.isAutoTiled = true - OPNStreamBorderless.enter(window, frame: tile.frame) + OPNStreamBorderless.enter(window, frame: tile.frame, coversMenuBar: coverage == .fullScreen) } for window in ordered where window.layoutState.isAutoTiled && !placeable.contains(where: { $0 === window }) { releaseTile(window) } } + /// Switches tiled windows between sitting below the menu bar and filling their screens, and + /// places them again at once. + func setTileCoverage(_ coverage: OPNStreamTileCoverage) { + OPNStreamTileCoverage.store(coverage) + retileWindows() + } + + var hasTiledWindows: Bool { windows.values.contains { $0.layoutState.isAutoTiled } } + private static func isPlaceableByLayout(_ window: OPNStreamWindow) -> Bool { !window.layoutState.isManuallyPlaced && !window.isPictureInPicture && !window.styleMask.contains(.fullScreen) } @@ -363,7 +374,7 @@ final class OPNStreamWindowPresenter { /// A tiled window that is no longer where the layout put it was taken by the reader, and stays /// theirs: the layout stops placing it. - private static func noteFrameChange(_ window: OPNStreamWindow) { + static func noteFrameChange(_ window: OPNStreamWindow) { // No layout frame yet means the placement is still waiting on the geometry gate, so a move // that lands before it is not the reader's. guard window.layoutState.isAutoTiled, window.layoutFrame != nil, !window.isAtLayoutFrame else { return } diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift new file mode 100644 index 000000000..b7fbcbbbe --- /dev/null +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift @@ -0,0 +1,59 @@ +// The Borderless Full Screen tile and what it does to a stream window: a single window toggles its +// own borderless mode, and a window the couch co-op layout places flips every tile between sitting +// below the menu bar and filling the screens. +// +// AppKit is imported for the same reason as the main file: the window is what these controls act on. +// +// swiftlint:disable:next no_appkit_in_view_model +import AppKit +import Foundation + +@MainActor +extension NativeNVSTHostViewModel { + private var streamLayoutState: OPNStreamWindowLayoutState? { + (nativeView?.window as? OPNStreamWindow)?.layoutState + } + + var isBorderlessWindow: Bool { streamLayoutState?.isBorderless ?? false } + + /// A window the couch co-op layout places. Its tile switches every tiled window between sitting + /// below the menu bar and filling the screens, rather than taking this one window out. + private var isTiledWindow: Bool { streamLayoutState?.isAutoTiled ?? false } + + var isBorderlessTileActive: Bool { + isTiledWindow ? OPNStreamTileCoverage.stored == .fullScreen : isBorderlessWindow + } + + var borderlessTileTitle: String { + if isTiledWindow { return OPNStreamTileCoverage.stored == .fullScreen ? "Tiles Below Menu Bar" : "Tiles Fill Screen" } + return isBorderlessWindow ? "Leave Borderless" : "Borderless Full Screen" + } + + var borderlessTileSubtitle: String { + if isTiledWindow { return OPNStreamTileCoverage.stored == .fullScreen ? "Filling every screen" : "Below the menu bar" } + return isBorderlessWindow ? "Borderless" : "Fill the screen, keep the Spaces" + } + + /// Borderless is a couch co-op mode, so the tile exists only behind the flag, and like full + /// screen it has nothing to act on in a small floating window or in a native full-screen Space. + var isBorderlessTileAvailable: Bool { OPNLabs.isCouchCoopEnabled } + + var isBorderlessTileDisabled: Bool { + nativeView?.window == nil || isPictureInPicture || streamWindowIsFullScreen || isFullScreenTransitioning + } + + /// The reader's own choice, so the layout stops placing this window: it stays where the toggle + /// left it, borderless or not. + func toggleBorderlessFullScreen() { + guard isBorderlessTileAvailable, !isBorderlessTileDisabled, !isEnding, !didEnd, + let window = nativeView?.window as? OPNStreamWindow else { return } + if window.layoutState.isAutoTiled { + let coverage = OPNStreamTileCoverage.stored.flipped + OPNStreamWindowPresenter.shared.setTileCoverage(coverage) + showNativeTransientStreamMessage(coverage == .fullScreen ? "Tiles fill the screen" : "Tiles below the menu bar") + return + } + OPNStreamBorderless.toggleByReader(window) + showNativeTransientStreamMessage(window.layoutState.isBorderless ? "Borderless full screen" : "Leaving borderless full screen") + } +} diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift index ed013e8b5..0ec74066b 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift @@ -394,29 +394,6 @@ extension NativeNVSTHostViewModel { /// out: a PiP window is small and floating, and there is no full screen to enter from it. var isFullScreenTileDisabled: Bool { nativeView?.window == nil || isPictureInPicture || isBorderlessWindow } - private var streamLayoutState: OPNStreamWindowLayoutState? { - (nativeView?.window as? OPNStreamWindow)?.layoutState - } - - var isBorderlessWindow: Bool { streamLayoutState?.isBorderless ?? false } - - /// Borderless is a couch co-op mode, so the tile exists only behind the flag, and like full - /// screen it has nothing to act on in a small floating window or in a native full-screen Space. - var isBorderlessTileAvailable: Bool { OPNLabs.isCouchCoopEnabled } - - var isBorderlessTileDisabled: Bool { - nativeView?.window == nil || isPictureInPicture || streamWindowIsFullScreen || isFullScreenTransitioning - } - - /// The reader's own choice, so the layout stops placing this window: it stays where the toggle - /// left it, borderless or not. - func toggleBorderlessFullScreen() { - guard isBorderlessTileAvailable, !isBorderlessTileDisabled, !isEnding, !didEnd, - let window = nativeView?.window as? OPNStreamWindow else { return } - OPNStreamBorderless.toggleByReader(window) - showNativeTransientStreamMessage(window.layoutState.isBorderless ? "Borderless full screen" : "Leaving borderless full screen") - } - /// The window owns the transition: nothing here touches the style mask, collection behaviour, /// aspect ratio or frame - `WindowFitting` grants `.fullScreenPrimary` before the window /// is first painted, which is the only point the window server takes it reliably. From 580a24de3429e1c7b846842fe04fcc4ae3c6abbc Mon Sep 17 00:00:00 2001 From: Anderson Shindy Oki Date: Mon, 26 Oct 2026 00:30:53 +0900 Subject: [PATCH 13/22] fix: isolate couch co-op account services and detached actions --- App/OPNMainAccountActions.swift | 144 +++++++ App/OPNReportIssueCommands.swift | 15 + DESIGN.md | 12 +- OPN/Core/OPNDlssPreferences.swift | 17 +- OPN/Core/OPNLaunchEntitlements.swift | 80 ++++ OPN/Core/OPNStreamPreferences+Regions.swift | 8 +- OPN/Core/OPNStreamPreferences+Settings.swift | 2 +- OPN/GameServices/CatalogLaunchPrefetch.swift | 28 +- OPN/GameServices/GameLaunchBridging.swift | 1 + OPN/GameServices/OPNGameLaunchBridge.swift | 2 +- OPN/GameServices/OPNGameService+Panels.swift | 2 +- .../OPNGameService+Provider.swift | 43 ++- OPN/GameServices/OPNGameService.swift | 8 +- .../OPNSessionManager+SessionInfo.swift | 2 +- OPN/GameServices/OPNSessionManager.swift | 4 + OPN/GameServices/OPNSessionPayloads.swift | 10 +- ...NStreamSessionCoordinator+Allocation.swift | 12 +- .../OPNStreamSessionCoordinator.swift | 8 +- OPN/Services/OPNMainWindowCloseGuard.swift | 93 +++++ OPN/Stream/OPNGameSession.swift | 11 +- OPN/Stream/OPNGameSessionOwnership.swift | 4 + OPNApp.swift | 15 +- Tests/Auth/DetachedAccountActionTests.swift | 155 ++++++++ Tests/Auth/DetachedAccountTestSupport.swift | 121 ++++++ .../Auth/DetachedSessionRefresherTests.swift | 173 +++++++++ .../CatalogAccountMembershipTests.swift | 126 ++++++ Tests/Catalog/CatalogAccountServiceStub.swift | 128 +++++++ Tests/Catalog/CatalogOfflineSwitchTests.swift | 173 +++++++++ .../Games/CatalogServiceIsolationTests.swift | 204 ++++++++++ Tests/Games/DlssOverridesTests.swift | 5 +- Tests/Games/SessionManagerTests.swift | 1 + .../MainWindowAccountHandoffTests.swift | 90 +++++ .../Settings/DetachedSessionModelTests.swift | 33 +- .../DetachedSessionPresentationTests.swift | 360 ++++++++++++++++++ Tests/Stream/DlssStatsHUDRowTests.swift | 88 ++++- .../LaunchEntitlementIsolationTests.swift | 133 +++++++ .../StreamPreflightProviderRoutingTests.swift | 134 +++++++ View/Catalog/CatalogTopBarViews.swift | 3 +- View/Components/OPNConfirmationModal.swift | 28 +- View/Components/OPNReportIssueModal.swift | 10 +- View/ContentView.swift | 6 + .../DetachedSession/DetachedSessionView.swift | 137 +++++-- View/Settings/SettingsReportViews.swift | 3 +- View/Stream/NativeNVSTStreamStatsHUD.swift | 6 +- ViewModel/AppRootViewModel.swift | 2 + ViewModel/CatalogAccountRequestIdentity.swift | 49 +++ ViewModel/CatalogBrowsing.swift | 2 +- ViewModel/CatalogHomeSections.swift | 27 ++ ViewModel/CatalogLaunchFlow.swift | 12 +- ViewModel/CatalogMenuBarSurface.swift | 2 +- ViewModel/CatalogOwnership.swift | 19 +- ViewModel/CatalogPanelLoading.swift | 61 ++- ViewModel/CatalogPatchingAndState.swift | 5 +- ViewModel/CatalogViewModel+Settings.swift | 2 +- ViewModel/CatalogViewModel.swift | 51 +-- .../DetachedSessionRefresher.swift | 104 ++++- .../DetachedSessionViewModel.swift | 30 +- 57 files changed, 2797 insertions(+), 207 deletions(-) create mode 100644 App/OPNMainAccountActions.swift create mode 100644 App/OPNReportIssueCommands.swift create mode 100644 OPN/Core/OPNLaunchEntitlements.swift create mode 100644 Tests/Auth/DetachedAccountActionTests.swift create mode 100644 Tests/Auth/DetachedAccountTestSupport.swift create mode 100644 Tests/Auth/DetachedSessionRefresherTests.swift create mode 100644 Tests/Catalog/CatalogAccountMembershipTests.swift create mode 100644 Tests/Catalog/CatalogAccountServiceStub.swift create mode 100644 Tests/Catalog/CatalogOfflineSwitchTests.swift create mode 100644 Tests/Games/CatalogServiceIsolationTests.swift create mode 100644 Tests/Services/MainWindowAccountHandoffTests.swift create mode 100644 Tests/Settings/DetachedSessionPresentationTests.swift create mode 100644 Tests/Stream/LaunchEntitlementIsolationTests.swift create mode 100644 Tests/Stream/StreamPreflightProviderRoutingTests.swift create mode 100644 ViewModel/CatalogAccountRequestIdentity.swift diff --git a/App/OPNMainAccountActions.swift b/App/OPNMainAccountActions.swift new file mode 100644 index 000000000..71b91ab4e --- /dev/null +++ b/App/OPNMainAccountActions.swift @@ -0,0 +1,144 @@ +import Foundation +import SwiftData + +@MainActor +final class OPNMainAccountActions { + static let shared = OPNMainAccountActions() + + struct Target { + let identifier: PersistentIdentifier + let accountID: String + let email: String + let userID: String + let providerID: String + + init(account: LoginAccount) { + identifier = account.persistentModelID + accountID = account.stableAccountID + email = account.email + userID = account.userId + providerID = account.providerIdpId + } + } + + enum Request { + case addAccount + case reauthenticate(Target) + case signOut(Target) + case forget(Target) + } + + private weak var login: LoginViewModel? + private var pendingRequests: [Request] = [] + private let confirmation: OPNConfirmationPresentation + private let handoff: OPNMainWindowAccountHandoff + + private enum ActionError: Error { + case missingContext + } + + init(confirmation: OPNConfirmationPresentation = .shared, handoff: OPNMainWindowAccountHandoff = .shared) { + self.confirmation = confirmation + self.handoff = handoff + } + + @discardableResult + func bind(login: LoginViewModel) -> Task { + self.login = login + let requests = pendingRequests + pendingRequests.removeAll() + return Task { + for request in requests { + await perform(request, login: login) + } + } + } + + func unbind(login: LoginViewModel) { + guard self.login === login else { return } + self.login = nil + } + + @discardableResult + func request(_ request: Request) -> Task? { + guard let login else { + pendingRequests.append(request) + return nil + } + return Task { await perform(request, login: login) } + } + + private func perform(_ request: Request, login: LoginViewModel) async { + do { + try reload(login: login) + } catch { + showFailure("Saved accounts could not be loaded. Try again.") + return + } + switch request { + case .addAccount: + login.beginAddAccount() + case .reauthenticate(let target): + guard let account = resolve(target, login: login) else { return } + login.dismissAccountChooser() + login.beginReauthentication(for: account) + case .signOut(let target): + guard let account = resolve(target, login: login) else { return } + login.validationMessage = "" + await login.signOutAccount(account) + finishMutation(login: login) + case .forget(let target): + guard let account = resolve(target, login: login) else { return } + login.validationMessage = "" + login.forgetAccount(account) + finishMutation(login: login) + } + } + + private func reload(login: LoginViewModel) throws { + guard let context = login.modelContext else { throw ActionError.missingContext } + login.update( + modelContext: context, + accounts: try context.fetch(FetchDescriptor()), + sessions: try context.fetch(FetchDescriptor()), + devices: try context.fetch(FetchDescriptor()) + ) + } + + private func resolve(_ target: Target, login: LoginViewModel) -> LoginAccount? { + guard let account = login.accounts.first(where: { + $0.persistentModelID == target.identifier && $0.stableAccountID == target.accountID + && $0.email == target.email && $0.userId == target.userID && $0.providerIdpId == target.providerID + }) else { + showFailure("This saved account changed or was removed. Choose the account again before continuing.") + return nil + } + return account + } + + private func finishMutation(login: LoginViewModel) { + guard login.validationMessage.isEmpty else { + showFailure(login.validationMessage) + return + } + endHandoff() + } + + private func showFailure(_ message: String) { + confirmation.present( + .init( + eyebrow: "ACCOUNT", + title: "Account action unavailable", + message: message, + actions: [OPNConfirmationAction("CLOSE") { [weak confirmation] in confirmation?.dismiss() }] + ), + onDismiss: { [weak self] in self?.endHandoff() } + ) + handoff.begin(from: nil) + } + + private func endHandoff() { + guard login?.signInRequest == nil else { return } + handoff.end() + } +} diff --git a/App/OPNReportIssueCommands.swift b/App/OPNReportIssueCommands.swift new file mode 100644 index 000000000..84743d24f --- /dev/null +++ b/App/OPNReportIssueCommands.swift @@ -0,0 +1,15 @@ +import SwiftUI + +struct OPNReportIssueCommands: Commands { + @FocusedValue(\.opnReportIssuePresentation) private var presentation + + var body: some Commands { + CommandGroup(replacing: .help) { + Button { + (presentation ?? .shared).present(context: .appEnvironment()) + } label: { + Label("Report an Issue…", systemImage: "exclamationmark.bubble") + } + } + } +} diff --git a/DESIGN.md b/DESIGN.md index 1da99c71f..56f42e437 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -1321,7 +1321,7 @@ The screen renders at 100 % interface scale like every other transient splash. ### Confirmation Modal (`OPNConfirmationModal`) The app-shell replacement for the native `.confirmationDialog` and `.alert`, which render rounded -macOS system chrome. Raised through the `opnConfirmation` modifier and rendered once at the app root +macOS system chrome. Raised through the `opnConfirmation` modifier and rendered once at its window root by `OPNConfirmationOverlay` — above the update prompt, below the splash — so it covers the whole surface even when the page that raised it lives inside a scroll view. Full-cover Scrim behind a centered panel following the modal spec (Panel background, 1px Stroke Regular, 2px accent top bar, @@ -1336,10 +1336,16 @@ actions trailing. Actions are `OPNConfirmationAction`s in visual order: `.cancel centered panel springs up from 0.96 scale with a fade. Reduce Motion collapses both to a plain cross-fade. +Detached catalogs own a separate confirmation and report presenter through `DetachedSessionModalHost`. +The host injects these references into its descendants and the focused scene's Help command; it never +observes the main window's shared requests. Confirmation modifiers dismiss only their own request, so +an unrelated page disappearing cannot clear another page's dialog. Main-window update prompts keep +their existing host and ordering. + ### Report an Issue Modal (`OPNReportIssueModal`) -The two-channel support form, raised through `OPNReportIssuePresentation.shared.present(context:)` -and rendered once at the app root by `OPNReportIssueOverlay` — above the update prompt, below the +The two-channel support form, raised through the window's `OPNReportIssuePresentation.present(context:)` +(the shared presenter in the main window) and rendered once at that window root by `OPNReportIssueOverlay` — above the update prompt, below the confirmation modal — so it covers the whole surface even when the page that raised it lives inside a scroll view. It reuses the confirmation modal's shell exactly (Panel background, 1px Stroke Regular, 2px accent top bar, modal shadow #000000 @ 0.58 radius 28 y 20, App Bar header with a 10pt bold diff --git a/OPN/Core/OPNDlssPreferences.swift b/OPN/Core/OPNDlssPreferences.swift index b15666f74..631dea565 100644 --- a/OPN/Core/OPNDlssPreferences.swift +++ b/OPN/Core/OPNDlssPreferences.swift @@ -15,10 +15,12 @@ extension OPNStreamPreferences { storage.synchronize() } - /// The entitlements the launch path filters against, remembered so a session request does not - /// need a subscription fetch on its own path. + /// The active browsing account's presentation mirror. Launches carry their own snapshot. public static func loadDlssEntitlements() -> DlssEntitlements { - guard let dictionary = storage.object(forKey: k.dlssEntitlements) as? [String: Any] else { return .none } + dlssEntitlements(from: storage.object(forKey: k.dlssEntitlements) as? [String: Any] ?? [:]) + } + + static func dlssEntitlements(from dictionary: [String: Any]) -> DlssEntitlements { return DlssEntitlements( superResolution: capability(from: dictionary, key: "superResolution"), frameGeneration: capability(from: dictionary, key: "frameGeneration"), @@ -28,14 +30,17 @@ extension OPNStreamPreferences { } public static func saveDlssEntitlements(_ entitlements: DlssEntitlements) { - let dictionary: [String: Any] = [ + storage.set(dlssEntitlementsDictionary(entitlements), forKey: k.dlssEntitlements) + storage.synchronize() + } + + static func dlssEntitlementsDictionary(_ entitlements: DlssEntitlements) -> [String: Any] { + [ "superResolution": capabilityDictionary(entitlements.superResolution), "frameGeneration": capabilityDictionary(entitlements.frameGeneration), "rayReconstruction": capabilityDictionary(entitlements.rayReconstruction), "frameGenerationMultipliers": entitlements.frameGenerationMultipliers, ] - storage.set(dictionary, forKey: k.dlssEntitlements) - storage.synchronize() } /// Signing out drops the stored entitlements, so a later launch cannot ask for an override the diff --git a/OPN/Core/OPNLaunchEntitlements.swift b/OPN/Core/OPNLaunchEntitlements.swift new file mode 100644 index 000000000..d5209d2b0 --- /dev/null +++ b/OPN/Core/OPNLaunchEntitlements.swift @@ -0,0 +1,80 @@ +import Foundation + +struct OPNLaunchEntitlements: Equatable, Sendable { + static let none = OPNLaunchEntitlements() + static let settingsKey = "accountEntitlements" + static let metadataKey = "launchEntitlements" + static let providerMetadataKey = "providerStreamingBaseURL" + + let dlss: DlssEntitlements + let audioChannelCount: Int + let isInGameSettingsPersistenceEntitled: Bool + + init(dlss: DlssEntitlements = .none, audioChannelCount: Int = 2, isInGameSettingsPersistenceEntitled: Bool = false) { + self.dlss = dlss + self.audioChannelCount = min(max(audioChannelCount, 2), 8) + self.isInGameSettingsPersistenceEntitled = isInGameSettingsPersistenceEntitled + } + + init(subscription: OPNSubscriptionInfo) { + self.init( + dlss: subscription.dlssEntitlements, + audioChannelCount: subscription.entitledAudioChannelCount, + isInGameSettingsPersistenceEntitled: subscription.isInGameSettingsPersistenceEntitled + ) + } + + init(settings: [String: Any]) { + self.init(stored: settings[Self.settingsKey] as? [String: Any] ?? [:]) + } + + init(metadata: [String: String]) { + let stored = metadata[Self.metadataKey]?.data(using: .utf8).flatMap { + (try? JSONSerialization.jsonObject(with: $0)) as? [String: Any] + } + self.init(stored: stored ?? [:]) + } + + private init(stored: [String: Any]) { + self.init( + dlss: OPNStreamPreferences.dlssEntitlements(from: stored["dlss"] as? [String: Any] ?? [:]), + audioChannelCount: stored["audioChannelCount"] as? Int ?? 2, + isInGameSettingsPersistenceEntitled: stored["isInGameSettingsPersistenceEntitled"] as? Bool ?? false + ) + } + + var storedDictionary: [String: Any] { + [ + "dlss": OPNStreamPreferences.dlssEntitlementsDictionary(dlss), + "audioChannelCount": audioChannelCount, + "isInGameSettingsPersistenceEntitled": isInGameSettingsPersistenceEntitled, + ] + } + + var metadataValue: String { + guard let encoded = try? JSONSerialization.data(withJSONObject: storedDictionary, options: .sortedKeys), + let value = String(data: encoded, encoding: .utf8) else { return "{}" } + return value + } +} + +extension StreamLaunchConfiguration { + var launchEntitlements: OPNLaunchEntitlements { OPNLaunchEntitlements(metadata: metadata) } + + func snapshottingEntitlements(_ entitlements: OPNLaunchEntitlements, providerStreamingBaseURL: String) -> StreamLaunchConfiguration { + var metadata = metadata + metadata[OPNLaunchEntitlements.metadataKey] = entitlements.metadataValue + metadata[OPNLaunchEntitlements.providerMetadataKey] = providerStreamingBaseURL + return StreamLaunchConfiguration( + id: id, + title: title, + applicationID: applicationID, + accessToken: accessToken, + accountLinked: accountLinked, + selectedStore: selectedStore, + resumeSessionID: resumeSessionID, + resumeServer: resumeServer, + metadata: metadata + ) + } +} diff --git a/OPN/Core/OPNStreamPreferences+Regions.swift b/OPN/Core/OPNStreamPreferences+Regions.swift index f9f17c921..e888a907f 100644 --- a/OPN/Core/OPNStreamPreferences+Regions.swift +++ b/OPN/Core/OPNStreamPreferences+Regions.swift @@ -9,10 +9,10 @@ extension OPNStreamPreferences { normalizedHTTPSBaseUrlOrEmpty(storage.string(forKey: k.selectedRegionUrl) ?? "") } - public static func loadSelectedStreamingBaseUrl() -> String { + public static func loadSelectedStreamingBaseUrl(providerStreamingBaseURL: String? = nil) -> String { let selected = loadSelectedRegionUrl() if !selected.isEmpty { return normalizedBaseUrl(selected) } - return defaultStreamingBaseUrl + return normalizedBaseUrl(providerStreamingBaseURL ?? defaultStreamingBaseUrl) } public static func loadSelectedRegionUrl(forGame appId: String) -> String { @@ -20,12 +20,12 @@ extension OPNStreamPreferences { return normalizedHTTPSBaseUrlOrEmpty(string(dictionary[k.selectedRegionUrl], "")) } - public static func loadSelectedStreamingBaseUrl(forGame appId: String) -> String { + public static func loadSelectedStreamingBaseUrl(forGame appId: String, providerStreamingBaseURL: String? = nil) -> String { if let dictionary = gameProfileDictionary(for: appId), bool(dictionary[k.gameProfileEnabled], false) { let selected = string(dictionary[k.selectedRegionUrl], "") if !selected.isEmpty { return normalizedBaseUrl(selected) } } - return loadSelectedStreamingBaseUrl() + return loadSelectedStreamingBaseUrl(providerStreamingBaseURL: providerStreamingBaseURL) } public static func saveSelectedRegionUrl(_ url: String) { diff --git a/OPN/Core/OPNStreamPreferences+Settings.swift b/OPN/Core/OPNStreamPreferences+Settings.swift index 419413b19..126caaa40 100644 --- a/OPN/Core/OPNStreamPreferences+Settings.swift +++ b/OPN/Core/OPNStreamPreferences+Settings.swift @@ -51,7 +51,7 @@ extension OPNStreamPreferences { public static func runNetworkPreflight(token: String, providerStreamingBaseUrl: String, requestedMaxBitrateMbps: Int, completion: @escaping @MainActor @Sendable (OPNStreamNetworkPreflightResult) -> Void) { var initial = OPNStreamNetworkPreflightResult() - initial.streamingBaseUrl = loadSelectedStreamingBaseUrl() + initial.streamingBaseUrl = loadSelectedStreamingBaseUrl(providerStreamingBaseURL: providerStreamingBaseUrl) initial.networkType = currentNetworkType() initial.recommendedMaxBitrateMbps = max(1, requestedMaxBitrateMbps) let initialResult = initial diff --git a/OPN/GameServices/CatalogLaunchPrefetch.swift b/OPN/GameServices/CatalogLaunchPrefetch.swift index cda6eae4a..e183b7caa 100644 --- a/OPN/GameServices/CatalogLaunchPrefetch.swift +++ b/OPN/GameServices/CatalogLaunchPrefetch.swift @@ -89,7 +89,7 @@ final class CatalogLaunchPrefetch { case failed } - let gameService = OPNGameService.shared + let gameService = OPNGameService() let imageCache = CatalogImageCache.shared private(set) var accountIdentifier = "" @@ -103,13 +103,13 @@ final class CatalogLaunchPrefetch { private var didPrefetchHeroImages = false private var didPrefetchRailImages = false - private init() {} + init() {} var hasHomePanels: Bool { !(panels[.main] ?? []).isEmpty } var isFetching: Bool { isActiveState(panelStates[.marquee]) || isActiveState(panelStates[.main]) } - func start(accountIdentifier: String, accessToken: String, idToken: String) { + func start(accountIdentifier: String, accessToken: String, idToken: String, idpId: String = "") { guard !retention.isHandoverFinished else { return } guard (panelStates[.marquee] ?? .idle) == .idle, (panelStates[.main] ?? .idle) == .idle else { return } guard !accountIdentifier.isEmpty, !accessToken.isEmpty || !idToken.isEmpty else { return } @@ -120,6 +120,17 @@ final class CatalogLaunchPrefetch { retention.recordDeliveriesStarted(deliveryKeys) startedAt = ContinuousClock.now StartupReadiness.shared.noteProgress() + guard !idpId.isEmpty else { + fetchLaunchData(accountIdentifier: accountIdentifier, accessToken: accessToken, idToken: idToken) + return + } + gameService.fetchProviderInfo(idpId: idpId) { [weak self] _, _, _, _ in + self?.fetchLaunchData(accountIdentifier: accountIdentifier, accessToken: accessToken, idToken: idToken) + } + } + + private func fetchLaunchData(accountIdentifier: String, accessToken: String, idToken: String) { + guard accountIdentifier == self.accountIdentifier, !retention.isHandoverFinished else { return } // Also prewarms the vpcId lookup, which every catalog query waits on. gameService.configureCatalogSession(accessToken: accessToken, idToken: idToken, userId: accountIdentifier) OPNLog.info(.catalog, "Launch panel prefetch started") @@ -170,7 +181,8 @@ final class CatalogLaunchPrefetch { /// delivered snapshot to every later caller, so without this a launch-time favorites list was /// re-adopted by every reload for the rest of the session: a favorite removed in this session /// came straight back on the next refresh, which made the control look dead. - func invalidate(_ kind: GameListKind) { + func invalidate(_ kind: GameListKind, accountIdentifier: String? = nil) { + guard accountIdentifier == nil || accountIdentifier == self.accountIdentifier else { return } gameLists[kind] = nil gameListStates[kind] = .idle } @@ -211,16 +223,18 @@ final class CatalogLaunchPrefetch { /// Tells this prefetch the home catalog has run every launch attach point, so the graphs may go /// as soon as the last delivery lands. - func recordLaunchResultsAdopted() { + func recordLaunchResultsAdopted(accountIdentifier: String? = nil) { + guard accountIdentifier == nil || accountIdentifier == self.accountIdentifier else { return } retention.recordLaunchResultsAdopted() releaseRetainedGraphsWhenReady() } /// Drops everything so a later explicit refresh goes to the network instead /// of adopting launch-time results. - func invalidate() { + func invalidate(accountIdentifier: String? = nil) { + guard accountIdentifier == nil || accountIdentifier == self.accountIdentifier else { return } dropRetainedGraphs() - accountIdentifier = "" + self.accountIdentifier = "" panelStates = [:] gameListStates = [:] startedAt = nil diff --git a/OPN/GameServices/GameLaunchBridging.swift b/OPN/GameServices/GameLaunchBridging.swift index 15727b1c2..f30d88076 100644 --- a/OPN/GameServices/GameLaunchBridging.swift +++ b/OPN/GameServices/GameLaunchBridging.swift @@ -13,6 +13,7 @@ protocol GameLaunchServiceConfiguring { func setAccountLinkingToken(_ token: String) func setUserId(_ id: String) func setVpcId(_ id: String) + func providerStreamingBaseURL() -> String func resolveLaunchAppId(game: OPNGameInfo, variantIndex: Int, completion: @escaping OPNLaunchAppIdCallback) } diff --git a/OPN/GameServices/OPNGameLaunchBridge.swift b/OPN/GameServices/OPNGameLaunchBridge.swift index 51b97719d..a9036a02f 100644 --- a/OPN/GameServices/OPNGameLaunchBridge.swift +++ b/OPN/GameServices/OPNGameLaunchBridge.swift @@ -99,7 +99,7 @@ public final class OPNGameLaunchBridge { selectedStore: selectedStore, metadata: launchMetadata.merging(Self.hdrCatalogSupportMetadata(for: selectedVariant)) { current, _ in current } ) - let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: appId) + let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: appId, providerStreamingBaseURL: gameService.providerStreamingBaseURL()) OPNActiveSessionService.fetchActiveSessions(accessToken: token, streamingBaseUrl: streamingBaseUrl, deviceId: deviceId) { [weak self] ok, sessions, _ in guard let self else { return } if ok { diff --git a/OPN/GameServices/OPNGameService+Panels.swift b/OPN/GameServices/OPNGameService+Panels.swift index d0c5084a3..83374347c 100644 --- a/OPN/GameServices/OPNGameService+Panels.swift +++ b/OPN/GameServices/OPNGameService+Panels.swift @@ -28,7 +28,7 @@ extension OPNGameService { // once (and the vpcId correction below asks again), so identical requests join // one fetch instead of each paying for a 660KB response. A late joiner is // replayed whatever the fetch has delivered so far. - let groupKey = "\(cacheKind)|\(accountIdentifier)|\(locale)" + let groupKey = "\(cacheKind)|\(accountIdentifier)|\(providerBaseUrl)|\(accessToken.hashValue)|\(locale)" Self.panelFetchLock.lock() if let existing = Self.pendingPanelFetches[groupKey] { Self.panelFetchLock.unlock() diff --git a/OPN/GameServices/OPNGameService+Provider.swift b/OPN/GameServices/OPNGameService+Provider.swift index d5668de1e..c32fbd577 100644 --- a/OPN/GameServices/OPNGameService+Provider.swift +++ b/OPN/GameServices/OPNGameService+Provider.swift @@ -2,37 +2,49 @@ import AppKit import Foundation extension OPNGameService { - // The service-urls document is global reference data, but it is looked up by both - // the login provider picker and the catalog session setup, so a single launch - // requested it twice (measured 1280ms + 1028ms). Memoized per idpId with in-flight - // coalescing; the streaming base URL side effect is replayed on every hit. func fetchProviderInfo(idpId: String, completion: @escaping OPNProviderInfoCallback) { + let requestID = UUID() + let token = accessToken + let requestingUserID = userId + providerContextLock.withLock { providerRequestID = requestID } + let delivery: OPNProviderInfoCallback = { [weak self] success, info, endpoint, error in + guard let self else { return } + let isCurrent = self.providerContextLock.withLock { + guard self.providerRequestID == requestID, self.accessToken == token, + self.userId == requestingUserID else { return false } + self.storedProviderStreamingBaseURL = endpoint.streamingServiceUrl + return true + } + guard isCurrent else { + completion(false, info, endpoint, "Provider lookup superseded") + return + } + completion(success, info, endpoint, error) + } Self.referenceDataLock.lock() if let entry = Self.providerInfoCache[idpId], Date().timeIntervalSince(entry.timestamp) <= Self.referenceDataFreshSeconds { Self.referenceDataLock.unlock() let (info, endpoint) = entry.value - providerStreamingBaseUrl = endpoint.streamingServiceUrl - dispatchProviderInfo(completion, true, info, endpoint, "") + dispatchProviderInfo(delivery, true, info, endpoint, "") return } if Self.pendingProviderInfoCallbacks[idpId] != nil { - Self.pendingProviderInfoCallbacks[idpId]?.append(completion) + Self.pendingProviderInfoCallbacks[idpId]?.append(delivery) Self.referenceDataLock.unlock() return } - Self.pendingProviderInfoCallbacks[idpId] = [completion] + Self.pendingProviderInfoCallbacks[idpId] = [delivery] Self.referenceDataLock.unlock() - fetchProviderInfoUncached(idpId: idpId) { [weak self] success, info, endpoint, error in + fetchProviderInfoUncached(idpId: idpId) { success, info, endpoint, error in Self.referenceDataLock.lock() if success { Self.providerInfoCache[idpId] = ReferenceDataEntry(value: (info, endpoint), timestamp: Date()) } let callbacks = Self.pendingProviderInfoCallbacks.removeValue(forKey: idpId) ?? [] Self.referenceDataLock.unlock() - guard let self else { return } for callback in callbacks { - self.dispatchProviderInfo(callback, success, info, endpoint, error) + callback(success, info, endpoint, error) } } } @@ -47,14 +59,12 @@ extension OPNGameService { request.setValue("application/json", forHTTPHeaderField: "Accept") request.setValue(Self.gfnUserAgent, forHTTPHeaderField: "User-Agent") let networkStart = OPNNetworkLog.start(request, operation: "provider.serviceUrls") - OPNSessionProxySessionProvider.shared.controlPlaneURLSession().dataTask(with: request) { [weak self] data, response, error in + OPNSessionProxySessionProvider.shared.controlPlaneURLSession().dataTask(with: request) { [self] data, response, error in OPNNetworkLog.finish(operation: "provider.serviceUrls", startedAt: networkStart, data: data, response: response, error: error) - guard let self else { return } let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 0 guard error == nil, let data, statusCode == 200 else { var endpoint = OPNGameProviderEndpoint() endpoint.streamingServiceUrl = Self.defaultStreamingBaseUrl - self.providerStreamingBaseUrl = endpoint.streamingServiceUrl self.dispatchProviderInfo(completion, false, OPNGameProviderInfo(), endpoint, error?.localizedDescription ?? "Provider info request failed") return } @@ -63,9 +73,8 @@ extension OPNGameService { let info = self.parseGameProviderInfo(json) var endpoint = self.selectGameProviderEndpoint(info, idpId: idpId) if endpoint.streamingServiceUrl.isEmpty { endpoint.streamingServiceUrl = Self.defaultStreamingBaseUrl } - self.providerStreamingBaseUrl = self.normalizeStreamingBaseUrl(endpoint.streamingServiceUrl) - if self.providerStreamingBaseUrl.isEmpty { self.providerStreamingBaseUrl = Self.defaultStreamingBaseUrl } - endpoint.streamingServiceUrl = self.providerStreamingBaseUrl + let normalized = self.normalizeStreamingBaseUrl(endpoint.streamingServiceUrl) + endpoint.streamingServiceUrl = normalized.isEmpty ? Self.defaultStreamingBaseUrl : normalized self.dispatchProviderInfo(completion, true, info, endpoint, "") }.resume() } diff --git a/OPN/GameServices/OPNGameService.swift b/OPN/GameServices/OPNGameService.swift index 309a7439f..9dfc85b3e 100644 --- a/OPN/GameServices/OPNGameService.swift +++ b/OPN/GameServices/OPNGameService.swift @@ -101,7 +101,13 @@ final class OPNGameService: @unchecked Sendable { var graphqlURL = "https://games.geforce.com/graphql" var persistedGraphqlURL = LCARS.productionAppsGraphQLURLString var streamingBaseUrl = "" - var providerStreamingBaseUrl = OPNGameService.defaultStreamingBaseUrl + let providerContextLock = NSLock() + var providerRequestID = UUID() + var storedProviderStreamingBaseURL = OPNGameService.defaultStreamingBaseUrl + var providerStreamingBaseUrl: String { + get { providerContextLock.withLock { storedProviderStreamingBaseURL } } + set { providerContextLock.withLock { storedProviderStreamingBaseURL = newValue } } + } init(sessionManager: any StreamSessionManaging = OPNSessionManager.shared, dataCache: any CatalogDataCaching = OPNGameDataCache.shared) { self.sessionManager = sessionManager diff --git a/OPN/GameServices/OPNSessionManager+SessionInfo.swift b/OPN/GameServices/OPNSessionManager+SessionInfo.swift index 125abbd02..ec8f64ba7 100644 --- a/OPN/GameServices/OPNSessionManager+SessionInfo.swift +++ b/OPN/GameServices/OPNSessionManager+SessionInfo.swift @@ -56,7 +56,7 @@ extension OPNSessionManager { /// so a stream that looks like it forgot the setting can be told apart from an unsent one. func logInGameSettingsPersistenceRequest(_ settings: [String: Any]) { let toggle = OPNStreamPreferences.loadPersistInGameSettings() - let entitled = OPNStreamPreferences.loadEntitledInGameSettingsPersistence() + let entitled = OPNLaunchEntitlements(settings: settings).isInGameSettingsPersistenceEntitled let sent = bool(settings["enablePersistingInGameSettings"]) OPNDiagnostics.logInfoMessage(OPNDiagnostics.formattedLogMessage(level: "info", area: "SessionManager", message: "In-game settings persistence sent=\(sent ? "on" : "off") toggle=\(toggle ? "on" : "off") membership=\(entitled ? "entitled" : "not-entitled")")) } diff --git a/OPN/GameServices/OPNSessionManager.swift b/OPN/GameServices/OPNSessionManager.swift index 9c2580c14..ecabb3a43 100644 --- a/OPN/GameServices/OPNSessionManager.swift +++ b/OPN/GameServices/OPNSessionManager.swift @@ -104,6 +104,10 @@ final class OPNSessionManager: NSObject, @unchecked Sendable { hdrEnabled: Bool, deviceId: String, selectedStore: String) -> [String: Any] { + let entitlements = OPNLaunchEntitlements(settings: effectiveSettings) + var effectiveSettings = effectiveSettings + effectiveSettings["audioChannelCount"] = min(requestedAudioChannelCount(effectiveSettings), entitlements.audioChannelCount) + effectiveSettings["enablePersistingInGameSettings"] = bool(effectiveSettings["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled let timezoneOffset = -TimeZone.current.secondsFromGMT() * 1000 let metadata = [ ["key": "SubSessionId", "value": UUID().uuidString.lowercased()], diff --git a/OPN/GameServices/OPNSessionPayloads.swift b/OPN/GameServices/OPNSessionPayloads.swift index d179c2a33..28b2c6862 100644 --- a/OPN/GameServices/OPNSessionPayloads.swift +++ b/OPN/GameServices/OPNSessionPayloads.swift @@ -63,13 +63,17 @@ final class OPNPollClaimSessionContext: @unchecked Sendable { // exist elsewhere in the module. extension OPNSessionManager { func settingsByApplyingCloudVariables(_ settings: [String: Any], capabilities: OPNStreamDeviceCapabilities) -> [String: Any] { + let entitlements = OPNLaunchEntitlements(settings: settings) + var cloudVariables = OPNStreamPreferences.loadCachedCloudVariables() + cloudVariables.entitledAudioChannelCount = entitlements.audioChannelCount let resolved = StreamSettingsResolver.resolve( profile: streamProfile(from: settings), capabilities: streamDeviceCapabilities(from: capabilities), - cloudVariables: streamCloudVariables(from: OPNStreamPreferences.loadCachedCloudVariables()) + cloudVariables: streamCloudVariables(from: cloudVariables) ) var result = settings result.merge(resolved.dictionary(gameLanguage: string(settings["gameLanguage"]), accountLinked: bool(settings["accountLinked"], fallback: true), selectedStore: string(settings["selectedStore"]))) { _, new in new } + result["enablePersistingInGameSettings"] = bool(result["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled return result } @@ -150,11 +154,9 @@ extension OPNSessionManager { if let prefilterModel = chosenPrefilterModel(int(settings["prefilterModel"])) { features["prefilterModel"] = prefilterModel } - // Cloud DLSS overrides ride the same object, filtered against the stored entitlements at the - // single point where the payload is built: an unentitled override can never reach the wire. let dlssPreference = settings["dlssOverrides"] as? [String: Any] let dlssOverrides = (dlssPreference.map(DlssOverrides.init(stored:)) ?? OPNStreamPreferences.loadDlssOverrides()) - .applyingAccess(OPNStreamPreferences.loadDlssEntitlements()) + .applyingAccess(OPNLaunchEntitlements(settings: settings).dlss) features["dlssOverrides"] = dlssOverrides.sessionRequestDictionary return features } diff --git a/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift b/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift index b678c28e2..913989a48 100644 --- a/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift +++ b/OPN/GameServices/OPNStreamSessionCoordinator+Allocation.swift @@ -170,7 +170,7 @@ extension OPNStreamSessionCoordinator { let cloudVariables = await fetchCloudVariables(configuration: configuration) var settings = settingsByApplyingCloudVariables(baseSettings, variables: cloudVariables) let requestedMaxBitrateMbps = int(settings["maxBitrateMbps"]) - let preflight = await runNetworkPreflight(token: configuration.accessToken, requestedMaxBitrateMbps: requestedMaxBitrateMbps) + let preflight = await runNetworkPreflight(token: configuration.accessToken, requestedMaxBitrateMbps: requestedMaxBitrateMbps, providerStreamingBaseURL: configuration.metadata[OPNLaunchEntitlements.providerMetadataKey] ?? OPNGameService.defaultStreamingBaseUrl) if preflight.serverReportedWarning || !preflight.continueRecommended || !preflight.warningMessage.isEmpty { OPNTelemetryRecorder.record(OPNTelemetryEvent(name: .networkTest, parameters: [ "status": "warning", @@ -185,7 +185,7 @@ extension OPNStreamSessionCoordinator { if preflight.recommendedMaxBitrateMbps > 0 { settings["maxBitrateMbps"] = min(int(settings["maxBitrateMbps"]), preflight.recommendedMaxBitrateMbps) } - let streamingBaseUrl = preflight.streamingBaseUrl.isEmpty ? OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: configuration.applicationID) : preflight.streamingBaseUrl + let streamingBaseUrl = preflight.streamingBaseUrl.isEmpty ? OPNStreamPreferences.loadSelectedStreamingBaseUrl(forGame: configuration.applicationID, providerStreamingBaseURL: configuration.metadata[OPNLaunchEntitlements.providerMetadataKey]) : preflight.streamingBaseUrl return PreparedStreamLaunch(settings: settings, streamingBaseUrl: streamingBaseUrl) } @@ -197,11 +197,11 @@ extension OPNStreamSessionCoordinator { } } - func runNetworkPreflight(token: String, requestedMaxBitrateMbps: Int) async -> OPNStreamNetworkPreflightResult { + func runNetworkPreflight(token: String, requestedMaxBitrateMbps: Int, providerStreamingBaseURL: String = OPNGameService.defaultStreamingBaseUrl) async -> OPNStreamNetworkPreflightResult { await withCheckedContinuation { continuation in OPNStreamPreferences.runNetworkPreflight( token: token, - providerStreamingBaseUrl: OPNGameService.shared.providerStreamingBaseURL(), + providerStreamingBaseUrl: providerStreamingBaseURL, requestedMaxBitrateMbps: requestedMaxBitrateMbps, completion: { preflight in continuation.resume(returning: preflight) } ) @@ -210,6 +210,9 @@ extension OPNStreamSessionCoordinator { func settingsByApplyingCloudVariables(_ settings: [String: Any], variables: OPNStreamCloudVariables) -> [String: Any] { let capabilities = OPNStreamPreferences.loadDeviceCapabilities() + let entitlements = OPNLaunchEntitlements(settings: settings) + var variables = variables + variables.entitledAudioChannelCount = entitlements.audioChannelCount let resolved = StreamSettingsResolver.resolve( profile: streamProfile(from: settings), capabilities: streamDeviceCapabilities(from: capabilities), @@ -217,6 +220,7 @@ extension OPNStreamSessionCoordinator { ) var result = settings result.merge(resolved.dictionary(gameLanguage: string(settings["gameLanguage"], fallback: OPNLocale.currentGFNLocale()), accountLinked: bool(settings["accountLinked"], fallback: true), selectedStore: string(settings["selectedStore"]))) { _, new in new } + result["enablePersistingInGameSettings"] = bool(result["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled return result } } diff --git a/OPN/GameServices/OPNStreamSessionCoordinator.swift b/OPN/GameServices/OPNStreamSessionCoordinator.swift index 383d46d40..24e0306c8 100644 --- a/OPN/GameServices/OPNStreamSessionCoordinator.swift +++ b/OPN/GameServices/OPNStreamSessionCoordinator.swift @@ -311,13 +311,17 @@ public final class OPNStreamSessionCoordinator: NativeNVSTSessionProvider, Strea func makeSettings(configuration: StreamLaunchConfiguration) -> [String: Any] { let capabilities = OPNStreamPreferences.loadDeviceCapabilities() let profile = OPNStreamPreferences.launchProfile(forGame: configuration.applicationID, capabilities: capabilities) + let entitlements = configuration.launchEntitlements + var cloudVariables = OPNStreamPreferences.loadCachedCloudVariables() + cloudVariables.entitledAudioChannelCount = entitlements.audioChannelCount let resolved = StreamSettingsResolver.resolve( profile: streamProfile(from: profile, catalogHdrSupport: configuration.hdrCatalogSupport(isStoreLauncherSession: profile.steamBigPictureMode)), capabilities: streamDeviceCapabilities(from: capabilities), - cloudVariables: streamCloudVariables(from: OPNStreamPreferences.loadCachedCloudVariables()) + cloudVariables: streamCloudVariables(from: cloudVariables) ) var settings = resolved.dictionary(gameLanguage: OPNLocale.currentGFNLocale(), accountLinked: configuration.accountLinked, selectedStore: configuration.selectedStore) - settings["enablePersistingInGameSettings"] = bool(settings["enablePersistingInGameSettings"]) && OPNStreamPreferences.loadEntitledInGameSettingsPersistence() + settings[OPNLaunchEntitlements.settingsKey] = entitlements.storedDictionary + settings["enablePersistingInGameSettings"] = bool(settings["enablePersistingInGameSettings"]) && entitlements.isInGameSettingsPersistenceEntitled // The saved DLSS preference rides the launch settings flow unfiltered; the payload builder // is what applies membership access. settings["dlssOverrides"] = OPNStreamPreferences.loadDlssOverrides().storedDictionary diff --git a/OPN/Services/OPNMainWindowCloseGuard.swift b/OPN/Services/OPNMainWindowCloseGuard.swift index 2e6823c36..dde71f1a0 100644 --- a/OPN/Services/OPNMainWindowCloseGuard.swift +++ b/OPN/Services/OPNMainWindowCloseGuard.swift @@ -75,6 +75,98 @@ enum OPNMainWindow { } } +@MainActor +final class OPNMainWindowAccountHandoff { + static let shared = OPNMainWindowAccountHandoff() + + private weak var window: NSWindow? + private weak var sourceWindow: NSWindow? + private var originalLevel: NSWindow.Level? + private var isRequested = false + private var observerTokens: [NSObjectProtocol] = [] + private var closeObserver: NSObjectProtocol? + private let mainWindow: () -> NSWindow? + private let coveringLevels: () -> [NSWindow.Level] + + init( + mainWindow: @escaping () -> NSWindow? = { OPNMainWindow.existing() }, + coveringLevels: @escaping () -> [NSWindow.Level] = { + NSApp.windows.compactMap { window in + guard let stream = window as? OPNStreamWindow, + stream.layoutState.isBorderless, stream.isVisible else { return nil } + return stream.level + } + } + ) { + self.mainWindow = mainWindow + self.coveringLevels = coveringLevels + } + + func begin(from sourceWindow: NSWindow?) { + isRequested = true + if let sourceWindow { self.sourceWindow = sourceWindow } + installObservers() + guard let window = mainWindow() else { return } + attach(window) + if window.isMiniaturized { window.deminiaturize(nil) } + window.makeKeyAndOrderFront(nil) + NSApp.activate(ignoringOtherApps: true) + } + + func attach(_ window: NSWindow) { + guard isRequested else { return } + if self.window !== window { + restoreLevel() + if let closeObserver { NotificationCenter.default.removeObserver(closeObserver) } + self.window = window + originalLevel = window.level + closeObserver = NotificationCenter.default.addObserver( + forName: NSWindow.willCloseNotification, object: window, queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.end() } + } + } + applyLevel() + } + + func end() { + let returnWindow = sourceWindow + let isReturningFocus = window?.isKeyWindow == true && NSApp.isActive + isRequested = false + restoreLevel() + window = nil + sourceWindow = nil + for token in observerTokens { NotificationCenter.default.removeObserver(token) } + observerTokens.removeAll() + if let closeObserver { NotificationCenter.default.removeObserver(closeObserver) } + closeObserver = nil + if isReturningFocus, let returnWindow, returnWindow.isVisible { returnWindow.makeKeyAndOrderFront(nil) } + } + + private func applyLevel() { + guard let window, let originalLevel else { return } + let coveringLevel = coveringLevels().map(\.rawValue).max() ?? NSWindow.Level.normal.rawValue + let sourceLevel = sourceWindow?.level.rawValue ?? NSWindow.Level.normal.rawValue + let obstructionLevel = max(coveringLevel, sourceLevel) + let requiredLevel = obstructionLevel > NSWindow.Level.normal.rawValue ? obstructionLevel + 1 : originalLevel.rawValue + window.level = NSWindow.Level(rawValue: max(originalLevel.rawValue, requiredLevel)) + } + + private func restoreLevel() { + if let window, let originalLevel { window.level = originalLevel } + originalLevel = nil + } + + private func installObservers() { + guard observerTokens.isEmpty else { return } + observerTokens.append(NotificationCenter.default.addObserver( + forName: OPNStreamBorderless.coveringStateDidChange, object: nil, queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.applyLevel() } + }) + } +} + /// Enforces `OPNWindowClosePreferences.behavior` on the main window's close button. /// /// The quit choice is enforced here rather than left to AppKit's last-window hook: a status item @@ -112,6 +204,7 @@ enum OPNMainWindowCloseGuard { /// notification, and the Dock icon decision is `OPNDockIconController`'s: with nothing on screen /// and the menu-bar-only choice in force, the app leaves the Dock. static var hideMainWindow: @MainActor () -> Void = { + OPNMainWindowAccountHandoff.shared.end() OPNMainWindow.existing()?.orderOut(nil) OPNDockIconController.apply() } diff --git a/OPN/Stream/OPNGameSession.swift b/OPN/Stream/OPNGameSession.swift index 999d860b1..b0ba967a3 100644 --- a/OPN/Stream/OPNGameSession.swift +++ b/OPN/Stream/OPNGameSession.swift @@ -24,6 +24,8 @@ final class OPNGameSession { let account: LoginAccount let session: LoginSession let streamProfile: OPNStreamPreferenceProfile + let launchEntitlements: OPNLaunchEntitlements + let providerStreamingBaseURL: String private(set) var accountID: OPNAccountID var phase: OPNGameSessionPhase = .checkingSession @@ -70,6 +72,8 @@ final class OPNGameSession { launchBridge: any GameLaunchBridging, discordPresence: any DiscordPresenceServing, streamProfile: OPNStreamPreferenceProfile, + launchEntitlements: OPNLaunchEntitlements = .none, + providerStreamingBaseURL: String = OPNGameService.defaultStreamingBaseUrl, registry: OPNGameSessionRegistry, results: OPNGameSessionResultStore ) { @@ -81,6 +85,8 @@ final class OPNGameSession { self.launchBridge = launchBridge self.discordPresence = discordPresence self.streamProfile = streamProfile + self.launchEntitlements = launchEntitlements + self.providerStreamingBaseURL = providerStreamingBaseURL self.registry = registry self.results = results } @@ -305,7 +311,7 @@ final class OPNGameSession { id: id, ownerDisplayName: account.displayName, ownerAccountID: accountID.rawValue, mappingGameIdentity: ControllerMappingStore.shared.currentGameIdentity ?? "" - ) + ).snapshottingEntitlements(launchEntitlements, providerStreamingBaseURL: providerStreamingBaseURL) if activeDiscordPresence == nil { activeDiscordPresence = discordPresence(for: ownedConfiguration) } @@ -467,6 +473,9 @@ final class OPNGameSession { return discordPresence(for: game) } +} + +extension OPNGameSession { // MARK: - Shared launch helpers static func mediaConfiguration(from configuration: OPNStreamLaunchConfiguration, titleOverride: String = "", membershipTier: String = "") -> StreamLaunchConfiguration { diff --git a/OPN/Stream/OPNGameSessionOwnership.swift b/OPN/Stream/OPNGameSessionOwnership.swift index cdd38ac0f..22cf7be07 100644 --- a/OPN/Stream/OPNGameSessionOwnership.swift +++ b/OPN/Stream/OPNGameSessionOwnership.swift @@ -30,6 +30,8 @@ final class OPNGameSessionRegistry { launchBridge: any GameLaunchBridging, discordPresence: any DiscordPresenceServing, streamProfile: OPNStreamPreferenceProfile, + launchEntitlements: OPNLaunchEntitlements = .none, + providerStreamingBaseURL: String = OPNGameService.defaultStreamingBaseUrl, results: OPNGameSessionResultStore = .shared ) -> OPNGameSession? { guard let accountID = account.resolveStableAccountID() else { @@ -45,6 +47,8 @@ final class OPNGameSessionRegistry { launchBridge: launchBridge, discordPresence: discordPresence, streamProfile: streamProfile, + launchEntitlements: launchEntitlements, + providerStreamingBaseURL: providerStreamingBaseURL, registry: self, results: results ) diff --git a/OPNApp.swift b/OPNApp.swift index d83f5d8d8..8c1f641fb 100644 --- a/OPNApp.swift +++ b/OPNApp.swift @@ -180,7 +180,12 @@ struct OPNApp: App { CatalogLaunchPrefetch.shared.primeFromCache(accountIdentifier: userId) return } - CatalogLaunchPrefetch.shared.start(accountIdentifier: userId, accessToken: session.accessToken, idToken: session.idToken) + CatalogLaunchPrefetch.shared.start( + accountIdentifier: userId, + accessToken: session.accessToken, + idToken: session.idToken, + idpId: session.idpId.isEmpty ? (account?.providerIdpId ?? "") : session.idpId + ) } } @@ -252,13 +257,7 @@ struct OPNApp: App { } } OPNCatalogCommands() - CommandGroup(replacing: .help) { - Button { - OPNReportIssuePresentation.shared.present(context: .appEnvironment()) - } label: { - Label("Report an Issue…", systemImage: "exclamationmark.bubble") - } - } + OPNReportIssueCommands() CommandMenu("Stream") { Button("Join Remote Co-Op as Guest…") { openWindow(id: "remote-coop-guest") diff --git a/Tests/Auth/DetachedAccountActionTests.swift b/Tests/Auth/DetachedAccountActionTests.swift new file mode 100644 index 000000000..2d68258f4 --- /dev/null +++ b/Tests/Auth/DetachedAccountActionTests.swift @@ -0,0 +1,155 @@ +import Foundation +import SwiftData +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct DetachedAccountActionTests { + @Test func signOutTargetsTheDetachedAccountWithoutChangingTheMainAccount() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let confirmation = OPNConfirmationPresentation() + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: confirmation, handoff: handoff) + await actions.bind(login: fixture.login).value + + await actions.request(.signOut(.init(account: fixture.detachedAccount)))?.value + + #expect(fixture.auth.endedEmails == [fixture.detachedEmail]) + #expect(fixture.detachedSession.accessToken.isEmpty) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(!fixture.mainSession.accessToken.isEmpty) + #expect(confirmation.request == nil) + } + + @Test func forgettingAnAccountQueuedWithoutAMainSceneUsesTheCapturedIdentity() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + let request = actions.request(.forget(.init(account: fixture.detachedAccount))) + #expect(request == nil) + #expect(fixture.auth.endedEmails.isEmpty) + + await actions.bind(login: fixture.login).value + + #expect(fixture.auth.endedEmails == [fixture.detachedEmail]) + #expect(fixture.login.accounts.map(\.email) == [fixture.mainAccount.email]) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)") == nil) + } + + @Test func aReplacedAccountDoesNotReceiveAPreviouslyQueuedDestructiveAction() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let confirmation = OPNConfirmationPresentation() + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: confirmation, handoff: handoff) + actions.request(.forget(.init(account: fixture.detachedAccount))) + let userID = fixture.detachedAccount.userId + let context = fixture.container.mainContext + context.delete(fixture.detachedAccount) + try context.save() + let replacement = makeLoginAccountForTesting(email: fixture.detachedEmail, userId: userID, isActive: false) + context.insert(replacement) + try context.save() + + await actions.bind(login: fixture.login).value + + #expect(fixture.login.accounts.contains { $0 === replacement }) + #expect(fixture.auth.endedEmails.isEmpty) + #expect(confirmation.request?.title == "Account action unavailable") + #expect(fixture.login.activeAccount === fixture.mainAccount) + confirmation.dismiss() + } + + @Test func accountActionsRecheckOwnershipWhenTheRequestArrives() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let confirmation = OPNConfirmationPresentation() + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: confirmation, handoff: handoff) + let target = OPNMainAccountActions.Target(account: fixture.detachedAccount) + actions.request(.signOut(target)) + _ = try #require(fixture.registry.begin( + account: fixture.detachedAccount, session: fixture.detachedSession, + gameService: OPNGameService.shared, launchBridge: OPNGameLaunchBridge.shared, + discordPresence: DiscordRichPresence.shared, streamProfile: OPNStreamPreferenceProfile() + )) + + await actions.bind(login: fixture.login).value + #expect(confirmation.request?.message == OPNAccountMutationGuard.activeGameSessionMessage) + confirmation.dismiss() + await actions.request(.forget(target))?.value + + #expect(confirmation.request?.message == OPNAccountMutationGuard.activeGameSessionMessage) + #expect(fixture.auth.endedEmails.isEmpty) + #expect(!fixture.detachedSession.accessToken.isEmpty) + #expect(fixture.login.accounts.contains { $0.email == fixture.detachedEmail }) + confirmation.dismiss() + } + + @Test func addingAnAccountUsesTheMainLoginFlow() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + actions.request(.addAccount) + + await actions.bind(login: fixture.login).value + #expect(fixture.login.signInRequest == .addAccount) + } + + @Test(arguments: [false, true]) + func reauthenticationRevealsTheExactAccountWhileTheMainChooserIsOpen(isQueuedBeforeBind: Bool) async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [] }) + defer { handoff.end() } + let actions = OPNMainAccountActions(confirmation: OPNConfirmationPresentation(), handoff: handoff) + let mainAccessToken = fixture.mainSession.accessToken + let detachedAccessToken = fixture.detachedSession.accessToken + let gameSession = try #require(fixture.registry.begin( + account: fixture.detachedAccount, session: fixture.detachedSession, + gameService: OPNGameService.shared, launchBridge: OPNGameLaunchBridge.shared, + discordPresence: DiscordRichPresence.shared, streamProfile: OPNStreamPreferenceProfile() + )) + if !isQueuedBeforeBind { await actions.bind(login: fixture.login).value } + let chooserReason: AccountChooserReason = isQueuedBeforeBind ? .startup : .switchAccount + fixture.login.accountChooserReason = chooserReason + + let request = actions.request(.reauthenticate(.init(account: fixture.detachedAccount))) + if isQueuedBeforeBind { + #expect(request == nil) + #expect(fixture.login.signInRequest == nil) + #expect(fixture.login.accountChooserReason == chooserReason) + await actions.bind(login: fixture.login).value + } + await request?.value + + #expect(fixture.login.signInRequest == .reauthenticate(email: fixture.detachedEmail)) + #expect(fixture.login.accountChooserReason == nil) + #expect(fixture.login.reauthAccount === fixture.detachedAccount) + #expect(fixture.login.email == fixture.detachedEmail) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(fixture.mainSession.accessToken == mainAccessToken) + #expect(fixture.detachedSession.accessToken == detachedAccessToken) + #expect(fixture.registry.sessions.count == 1) + #expect(fixture.registry.sessions.first === gameSession) + #expect(fixture.auth.endedEmails.isEmpty) + + fixture.login.cancelReauthentication() + + #expect(fixture.login.signInRequest == nil) + #expect(fixture.login.accountChooserReason == nil) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(fixture.registry.sessions.first === gameSession) + } +} diff --git a/Tests/Auth/DetachedAccountTestSupport.swift b/Tests/Auth/DetachedAccountTestSupport.swift new file mode 100644 index 000000000..faf591a5d --- /dev/null +++ b/Tests/Auth/DetachedAccountTestSupport.swift @@ -0,0 +1,121 @@ +import Foundation +import SwiftData +@testable import OpenNOW + +final class DetachedRecordingAuthService: LoginAuthServing, @unchecked Sendable { + private(set) var endedEmails: [String] = [] + + func invalidatePendingAuthentication() {} + + func startOAuthLogin(providerIdpId: String, completion: @escaping OPNAuthCallback) { + Task { @MainActor in completion(false, OPNAuthSession(), "No network in detached-account tests") } + } + + func startStarfleetDeviceCodeLogin(providerIdpId: String, challengeHandler: @escaping OPNDeviceCodeChallengeCallback, completion: @escaping OPNAuthCallback) { + Task { @MainActor in completion(false, OPNAuthSession(), "No network in detached-account tests") } + } + + func endSavedSession(userId: String, email: String) { + endedEmails.append(email) + } +} + +private final class DetachedProviderInfoService: GameProviderInfoServing, @unchecked Sendable { + func fetchProviderInfo(idpId: String, completion: @escaping OPNProviderInfoCallback) { + Task { @MainActor in completion(false, OPNGameProviderInfo(), OPNGameProviderEndpoint(), "No network in detached-account tests") } + } +} + +@MainActor +final class DetachedAccountTestFixture { + let container: ModelContainer + let login: LoginViewModel + let registry = OPNGameSessionRegistry() + let auth = DetachedRecordingAuthService() + let mainAccount: LoginAccount + let detachedAccount: LoginAccount + let mainSession: LoginSession + let detachedSession: LoginSession + let detachedEmail: String + let detachedSessionID: String + private var sessionIDs: [String] = [] + + init() throws { + container = try ModelContainer( + for: LoginAccount.self, LoginSession.self, LoginDeviceRegistration.self, + configurations: ModelConfiguration(isStoredInMemoryOnly: true, cloudKitDatabase: .none) + ) + let identity = UUID().uuidString + mainAccount = makeLoginAccountForTesting(email: "main-\(identity)@example.invalid", userId: "main-\(identity)") + detachedEmail = "detached-\(identity)@example.invalid" + detachedAccount = makeLoginAccountForTesting(email: detachedEmail, userId: "detached-\(identity)", isActive: false) + mainSession = makeLoginSessionForTesting(accountEmail: mainAccount.email, userId: mainAccount.userId) + detachedSessionID = UUID().uuidString + detachedSession = makeLoginSessionForTesting(accountEmail: detachedEmail, id: detachedSessionID, userId: detachedAccount.userId) + detachedSession.isActive = false + login = LoginViewModel(authService: auth, providerInfoService: DetachedProviderInfoService(), sessionRegistry: registry) + let context = container.mainContext + context.insert(mainAccount) + context.insert(detachedAccount) + context.insert(mainSession) + context.insert(detachedSession) + try context.save() + login.update(modelContext: context, accounts: [mainAccount, detachedAccount], sessions: [mainSession, detachedSession], devices: []) + sessionIDs = [mainSession.id, detachedSessionID] + GFNTokenStore.save( + .init(accessToken: detachedSession.accessToken, idToken: detachedSession.idToken, + refreshToken: detachedSession.refreshToken, clientToken: detachedSession.clientToken), + forIdentity: "session.\(detachedSessionID)" + ) + } + + func insertReplacementSession(id: String = UUID().uuidString) throws -> LoginSession { + let replacement = makeLoginSessionForTesting(accountEmail: detachedEmail, id: id, userId: detachedAccount.userId) + replacement.isActive = false + sessionIDs.append(replacement.id) + container.mainContext.insert(replacement) + try container.mainContext.save() + return replacement + } + + func tearDown() { + for id in sessionIDs { GFNTokenStore.delete(forIdentity: "session.\(id)") } + } +} + +actor SuspendedDetachedRefresh { + enum Failure: Error { + case unavailable + } + + private var started: CheckedContinuation? + private var completion: CheckedContinuation? + private var input: JarvisSession? + + func refresh(_ session: JarvisSession) async throws -> JarvisSession { + input = session + started?.resume() + started = nil + return try await withCheckedThrowingContinuation { completion = $0 } + } + + func waitUntilStarted() async { + guard input == nil else { return } + await withCheckedContinuation { started = $0 } + } + + func complete(isSuccessful: Bool = true, replacementUserID: String? = nil) { + guard var session = input else { return } + defer { completion = nil } + guard isSuccessful else { + completion?.resume(throwing: Failure.unavailable) + return + } + session.accessToken = "refreshed-access" + session.idToken = "refreshed-id" + session.refreshToken = "refreshed-refresh" + session.clientToken = "refreshed-client" + if let replacementUserID { session.userId = replacementUserID } + completion?.resume(returning: session) + } +} diff --git a/Tests/Auth/DetachedSessionRefresherTests.swift b/Tests/Auth/DetachedSessionRefresherTests.swift new file mode 100644 index 000000000..6488fa91a --- /dev/null +++ b/Tests/Auth/DetachedSessionRefresherTests.swift @@ -0,0 +1,173 @@ +import Foundation +import SwiftData +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct DetachedSessionRefresherTests { + enum Invalidation: CaseIterable, Equatable, Sendable { + case purge + case signOut + case forget + case deleteSession + case replaceSession + case replaceSessionUsingOriginalID + case deleteAccount + case replaceAccount + case replaceCredentials + } + + @Test(.timeLimit(.minutes(1)), arguments: Invalidation.allCases) + func aSuspendedRefreshDiscardsInvalidatedCredentials(_ invalidation: Invalidation) async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalToken = fixture.detachedSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + + try await invalidate(invalidation, fixture: fixture) + await suspended.complete() + + #expect(await task.value == false) + #expect(fixture.mainAccount.isActive) + #expect(fixture.mainSession.isActive) + let persisted = GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)") + switch invalidation { + case .replaceCredentials: + #expect(persisted?.accessToken == "new-login-access") + #expect(persisted?.refreshToken == "new-login-refresh") + case .purge, .signOut, .forget: + #expect(persisted == nil) + default: + #expect(persisted?.accessToken == originalToken) + } + } + + @Test(.timeLimit(.minutes(1))) + func aFreshRefreshPersistsOnlyTheInactiveAccountsTokens() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalMainToken = fixture.mainSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + await suspended.complete() + + #expect(await task.value) + #expect(fixture.detachedSession.accessToken == "refreshed-access") + #expect(GFNTokenStore.load(forIdentity: "session.\(fixture.detachedSessionID)")?.refreshToken == "refreshed-refresh") + #expect(!fixture.detachedAccount.isActive) + #expect(!fixture.detachedSession.isActive) + #expect(fixture.login.activeAccount === fixture.mainAccount) + #expect(fixture.mainSession.accessToken == originalMainToken) + } + + @Test(.timeLimit(.minutes(1))) + func aFailedRefreshCannotOfferOfflineContinuationAfterAPurge() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + fixture.detachedSession.purgeTokens() + await suspended.complete(isSuccessful: false) + + #expect(await task.value == false) + #expect(fixture.detachedSession.accessToken.isEmpty) + } + + @Test(.timeLimit(.minutes(1))) + func aCancelledRefreshDiscardsItsLateResponse() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalToken = fixture.detachedSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + task.cancel() + await suspended.complete() + + #expect(await task.value == false) + #expect(fixture.detachedSession.accessToken == originalToken) + } + + @Test(.timeLimit(.minutes(1))) + func aResponseForAnotherSubjectNeverReplacesTheSelectedAccountsTokens() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let originalToken = fixture.detachedSession.accessToken + let suspended = SuspendedDetachedRefresh() + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext, refreshSession: suspended.refresh) + let task = Task { await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) } + await suspended.waitUntilStarted() + await suspended.complete(replacementUserID: fixture.mainAccount.userId) + + #expect(await task.value == false) + #expect(fixture.detachedSession.accessToken == originalToken) + } + + @Test func deletedModelsAreRejectedBeforeStartingARefresh() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let context = fixture.container.mainContext + context.delete(fixture.detachedSession) + context.delete(fixture.detachedAccount) + try context.save() + let refresher = DetachedSessionRefresher(modelContext: context) { session in + Issue.record("Deleted models reached the refresh transport") + return session + } + + #expect(await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession) == false) + } + + @Test func anUnchangedUnexpiredSessionCanContinueOfflineWhenRefreshFails() async throws { + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + let refresher = DetachedSessionRefresher(modelContext: fixture.container.mainContext) { _ in + throw SuspendedDetachedRefresh.Failure.unavailable + } + + #expect(await refresher.refresh(account: fixture.detachedAccount, session: fixture.detachedSession)) + #expect(!fixture.detachedAccount.isActive) + #expect(!fixture.detachedSession.isActive) + } + + private func invalidate(_ invalidation: Invalidation, fixture: DetachedAccountTestFixture) async throws { + let context = fixture.container.mainContext + switch invalidation { + case .purge: + fixture.detachedSession.purgeTokens() + case .signOut: + await fixture.login.signOutAccount(fixture.detachedAccount) + case .forget: + fixture.login.forgetAccount(fixture.detachedAccount) + case .deleteSession: + context.delete(fixture.detachedSession) + case .replaceSession: + context.delete(fixture.detachedSession) + try context.save() + _ = try fixture.insertReplacementSession() + case .replaceSessionUsingOriginalID: + context.delete(fixture.detachedSession) + try context.save() + _ = try fixture.insertReplacementSession(id: fixture.detachedSessionID) + case .deleteAccount: + context.delete(fixture.detachedAccount) + case .replaceAccount: + let userID = fixture.detachedAccount.userId + context.delete(fixture.detachedAccount) + try context.save() + context.insert(makeLoginAccountForTesting(email: fixture.detachedEmail, userId: userID, isActive: false)) + case .replaceCredentials: + fixture.detachedSession.accessToken = "new-login-access" + fixture.detachedSession.refreshToken = "new-login-refresh" + } + try context.save() + } +} diff --git a/Tests/Catalog/CatalogAccountMembershipTests.swift b/Tests/Catalog/CatalogAccountMembershipTests.swift new file mode 100644 index 000000000..07c396117 --- /dev/null +++ b/Tests/Catalog/CatalogAccountMembershipTests.swift @@ -0,0 +1,126 @@ +import Foundation +import SwiftData +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct CatalogAccountMembershipTests { + private let preferenceKeys = ["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.EntitledAudioChannelCount", "OpenNOW.Stream.EntitledInGameSettingsPersistence"] + + @Test func anInactiveAuthenticatedCatalogLoadsItsOwnMembershipWithoutChangingTheActiveAccount() { + withPreservedPreferences(preferenceKeys) { + let active = makeCatalog(email: "active@example.test", isActive: true) + let detached = makeCatalog(email: "detached@example.test", isActive: false) + let activeMembership = subscription(tier: "Ultimate", channels: 8, isEntitled: true) + OPNStreamPreferences.saveDlssEntitlements(activeMembership.dlssEntitlements) + defer { + active.model.session.purgeTokens() + detached.model.session.purgeTokens() + } + active.model.loadAccount() + detached.model.loadAccount() + #expect(active.model.dlssMembershipState == .loading) + #expect(detached.model.dlssMembershipState == .loading) + #expect(OPNStreamPreferences.loadDlssEntitlements() == activeMembership.dlssEntitlements) + + active.service.completeSubscription(at: 0, subscription: activeMembership) + detached.service.completeSubscription(at: 0, subscription: subscription(tier: "Free", channels: 2, isEntitled: false)) + + #expect(detached.model.account.membershipTier == "Free") + #expect(detached.model.dlssMembershipState == .loaded(.none)) + #expect(active.model.account.membershipTier == "Ultimate") + #expect(active.model.launchEntitlements.audioChannelCount == 8) + #expect(detached.model.launchEntitlements.audioChannelCount == 2) + #expect(OPNStreamPreferences.loadDlssEntitlements() == active.model.launchEntitlements.dlss) + #expect(OPNStreamPreferences.loadEntitledAudioChannelCount() == 8) + #expect(OPNStreamPreferences.loadEntitledInGameSettingsPersistence()) + } + } + + enum Invalidation: CaseIterable, Sendable { + case signOut, deleteAccount, deleteSession, replaceCredentials, replaceProvider + } + + @Test(arguments: Invalidation.allCases) + func anInvalidatedCatalogRejectsLateMembership(_ invalidation: Invalidation) throws { + let container = try ModelContainer(for: LoginAccount.self, LoginSession.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true, cloudKitDatabase: .none)) + let catalog = makeCatalog(email: "late-\(invalidation)@example.test", isActive: false) + container.mainContext.insert(catalog.model.account) + container.mainContext.insert(catalog.model.session) + defer { + if invalidation != .deleteSession { catalog.model.session.purgeTokens() } + } + catalog.model.loadAccount() + #expect(catalog.service.subscriptionRequestCount == 1) + switch invalidation { + case .signOut: + catalog.model.session.purgeTokens() + case .deleteAccount: + container.mainContext.delete(catalog.model.account) + try container.mainContext.save() + case .deleteSession: + catalog.model.session.purgeTokens() + container.mainContext.delete(catalog.model.session) + try container.mainContext.save() + case .replaceCredentials: + catalog.model.session.idToken = "replacement-token" + case .replaceProvider: + catalog.model.session.idpId = "replacement-provider" + } + catalog.service.completeSubscription(at: 0, subscription: subscription(tier: "Ultimate", channels: 8, isEntitled: true)) + #expect(catalog.model.launchEntitlements == .none) + #expect(catalog.model.dlssMembershipState == .loading) + } + + @Test func replacingCredentialsCannotReusePreviouslyLoadedLaunchEntitlements() { + let catalog = makeCatalog(email: "replaced@example.test", isActive: false) + defer { catalog.model.session.purgeTokens() } + catalog.model.loadAccount() + catalog.service.completeSubscription(at: 0, subscription: subscription(tier: "Ultimate", channels: 8, isEntitled: true)) + #expect(catalog.model.currentLaunchEntitlements.audioChannelCount == 8) + catalog.model.session.idToken = "replacement-token" + #expect(catalog.model.currentLaunchEntitlements == .none) + } + + @Test func aNewMembershipRequestCannotBeOverwrittenByAnOlderCompletion() { + let catalog = makeCatalog(email: "reloaded@example.test", isActive: false) + defer { catalog.model.session.purgeTokens() } + catalog.model.loadAccount() + catalog.model.loadAccount() + catalog.service.completeSubscription(at: 1, subscription: subscription(tier: "Performance", channels: 6, isEntitled: false)) + catalog.service.completeSubscription(at: 0, subscription: subscription(tier: "Ultimate", channels: 8, isEntitled: true)) + #expect(catalog.model.account.membershipTier == "Performance") + #expect(catalog.model.launchEntitlements.audioChannelCount == 6) + #expect(catalog.model.dlssMembershipState == .loaded(.none)) + } + + @Test func anInactiveMembershipFailureFinishesCheckingWithoutClearingAnotherAccount() { + withPreservedPreferences(preferenceKeys) { + OPNStreamPreferences.saveDlssEntitlements(subscription(tier: "Ultimate", channels: 8, isEntitled: true).dlssEntitlements) + let catalog = makeCatalog(email: "failed@example.test", isActive: false) + defer { catalog.model.session.purgeTokens() } + catalog.model.loadAccount() + catalog.service.completeSubscription(at: 0, subscription: OPNSubscriptionInfo(), isSuccessful: false) + #expect(catalog.model.dlssMembershipState == .unavailable) + #expect(catalog.model.launchEntitlements == .none) + #expect(OPNStreamPreferences.loadDlssEntitlements().superResolution.isEntitled) + } + } + + private func makeCatalog(email: String, isActive: Bool) -> (model: CatalogViewModel, service: CatalogAccountServiceStub) { + let account = makeLoginAccountForTesting(email: email, userId: email, isActive: isActive) + let session = makeLoginSessionForTesting(accountEmail: email, userId: email) + session.isActive = isActive + let service = CatalogAccountServiceStub() + return (CatalogViewModel(account: account, session: session, gameService: service, onRefreshAuth: { false }), service) + } + + private func subscription(tier: String, channels: Int, isEntitled: Bool) -> OPNSubscriptionInfo { + var subscription = OPNSubscriptionInfo() + subscription.membershipTier = tier + subscription.entitledAudioChannelCount = channels + subscription.isInGameSettingsPersistenceEntitled = isEntitled + if isEntitled { subscription.dlssEntitlements = DlssEntitlements(superResolution: DlssCapability(isEntitled: true)) } + return subscription + } +} diff --git a/Tests/Catalog/CatalogAccountServiceStub.swift b/Tests/Catalog/CatalogAccountServiceStub.swift new file mode 100644 index 000000000..549f261df --- /dev/null +++ b/Tests/Catalog/CatalogAccountServiceStub.swift @@ -0,0 +1,128 @@ +import Foundation +@testable import OpenNOW + +final class CatalogAccountServiceStub: CatalogGameServing, @unchecked Sendable { + struct SessionCredentials: Equatable, Sendable { + let accessToken: String + let idToken: String + let userID: String + } + + private let lock = NSLock() + private var subscriptions: [OPNSubscriptionCallback] = [] + private var sessionCredentials: SessionCredentials? + private var subscriptionUsers: [String] = [] + + var subscriptionRequestCount: Int { lock.withLock { subscriptions.count } } + var configuredCredentials: SessionCredentials? { lock.withLock { sessionCredentials } } + var subscriptionUserIDs: [String] { lock.withLock { subscriptionUsers } } + + @MainActor + func completeSubscription(at index: Int, subscription: OPNSubscriptionInfo, isSuccessful: Bool = true) { + let completion = lock.withLock { subscriptions[index] } + completion(isSuccessful, subscription, isSuccessful ? "" : "Membership unavailable") + } + + func configureCatalogSession(accessToken: String, idToken: String, userId: String) { + lock.withLock { + sessionCredentials = SessionCredentials(accessToken: accessToken, idToken: idToken, userID: userId) + } + } + + func fetchSubscriptionInfo(userId: String, completion: @escaping OPNSubscriptionCallback) { + lock.withLock { + subscriptions.append(completion) + subscriptionUsers.append(userId) + } + } + + func fetchUserAccount(completion: @escaping OPNUserAccountCallback) { + Task { @MainActor in completion(true, OPNUserAccountInfo(), "") } + } + + func browseCatalogObject(searchQuery: String, sortId: String, filterIds: [String], fetchCount: Int, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogBrowseResultObject, String) -> Void) { + Task { @MainActor in completion(false, OPNCatalogBrowseResultObject(), "Unused browse") } + } + + func browseCatalogObject(searchQuery: String, sortId: String, filterIds: [String], fetchCount: Int, forceRefresh: Bool, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogBrowseResultObject, String) -> Void) { + browseCatalogObject(searchQuery: searchQuery, sortId: sortId, filterIds: filterIds, fetchCount: fetchCount, completion: completion) + } + + func browseCatalogObject(searchQuery: String, sortId: String, filterIds: [String], fetchCount: Int, forceRefresh: Bool, cursor: String, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogBrowseResultObject, String) -> Void) { + browseCatalogObject(searchQuery: searchQuery, sortId: sortId, filterIds: filterIds, fetchCount: fetchCount, completion: completion) + } + + func fetchMarqueePanelObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogPanelObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused marquee") } + } + + func fetchMainPanelObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogPanelObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused panels") } + } + + func fetchLibraryGameObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogGameObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused library") } + } + + func fetchFavoriteGameObjects(completion: @escaping @MainActor @Sendable (Bool, [OPNCatalogGameObject], String) -> Void) { + Task { @MainActor in completion(false, [], "Unused favorites") } + } + + func fetchGameObjectByCMSId(_ cmsId: String, completion: @escaping @MainActor @Sendable (Bool, OPNCatalogGameObject?, String) -> Void) { + Task { @MainActor in completion(false, nil, "Unused game lookup") } + } + + func fetchStoreDefinitions(completion: @escaping OPNStoreDefinitionsCallback) { + Task { @MainActor in completion(false, [], "Unused stores") } + } + + func fetchSubscriptionDefinitions(completion: @escaping OPNSubscriptionDefinitionsCallback) { + Task { @MainActor in completion(false, [], "Unused subscriptions") } + } + + func fetchAppPatchStatuses(appIds: [String], completion: @escaping OPNAppPatchStatusesCallback) { + Task { @MainActor in completion(false, [:], "Unused patch statuses") } + } + + func fetchLibraryPatchStatuses(completion: @escaping OPNAppPatchStatusesCallback) { + Task { @MainActor in completion(false, [:], "Unused patch statuses") } + } + + func addOwnedVariant(_ variantId: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused ownership mutation") } + } + + func removeOwnedVariant(_ variantId: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused ownership mutation") } + } + + func selectOwnedVariant(_ variantId: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused ownership mutation") } + } + + func addFavoriteApp(_ appId: String, completion: @escaping OPNFavoriteActionCallback) { + Task { @MainActor in completion(false, "Unused favorite mutation") } + } + + func removeFavoriteApp(_ appId: String, completion: @escaping OPNFavoriteActionCallback) { + Task { @MainActor in completion(false, "Unused favorite mutation") } + } + + func syncAccountProvider(store: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused store sync") } + } + + func startAccountLinking(store: String, completion: @escaping OPNOwnershipActionCallback) { + Task { @MainActor in completion(false, "Unused store linking") } + } + + func resolveStoreURL(game: OPNGameInfo, variantIndex: Int, completion: @escaping OPNStoreURLCallback) { + Task { @MainActor in completion(false, "", "Unused store URL") } + } + + func fetchProviderInfo(idpId: String, completion: @escaping OPNProviderInfoCallback) { + Task { @MainActor in completion(false, OPNGameProviderInfo(), OPNGameProviderEndpoint(), "Unused provider") } + } + + func providerStreamingBaseURL() -> String { OPNGameService.defaultStreamingBaseUrl } +} diff --git a/Tests/Catalog/CatalogOfflineSwitchTests.swift b/Tests/Catalog/CatalogOfflineSwitchTests.swift new file mode 100644 index 000000000..73e3e9d8e --- /dev/null +++ b/Tests/Catalog/CatalogOfflineSwitchTests.swift @@ -0,0 +1,173 @@ +import Foundation +import Testing +@testable import OpenNOW + +@MainActor +private final class OfflineCatalogPresence: DiscordPresenceServing { + var isEnabled = false + private(set) var lastState = DiscordPresenceState.idle + + func update(_ state: DiscordPresenceState) { lastState = state } +} + +@MainActor +@Suite struct CatalogOfflineSwitchTests { + @Test(arguments: [false, true]) + func anOfflineSwitchKeepsDetachedMembershipOwnedByItsSavedSession(_ isRequestPendingDuringSwitch: Bool) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + installOfflineRefreshResponse() + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try expireIDTokens(in: fixture) + let service = CatalogAccountServiceStub() + let model = makeDetachedCatalog(fixture: fixture, service: service) + let ownerCredentials = credentials(for: fixture.detachedSession) + try await restoreOfflineSession(fixture.detachedAccount, fixture: fixture) + if isRequestPendingDuringSwitch { model.loadAccount() } + + try await restoreOfflineSession(fixture.mainAccount, fixture: fixture) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(!fixture.detachedAccount.isActive && !fixture.detachedSession.isActive) + #expect(fixture.detachedAccount.authStatus == JarvisAuthStatus.notLoggedIn.rawValue) + #expect(credentials(for: fixture.detachedSession) == ownerCredentials) + if !isRequestPendingDuringSwitch { model.loadAccount() } + + try #require(service.subscriptionRequestCount == 1) + #expect(model.dlssMembershipState == .loading) + #expect(service.subscriptionUserIDs == [fixture.detachedSession.userId]) + let mainService = CatalogAccountServiceStub() + let mainCatalog = CatalogViewModel(account: fixture.mainAccount, session: fixture.mainSession, gameService: mainService, onRefreshAuth: { false }) + #expect(mainCatalog.configureCatalogService()) + #expect(model.configureCatalogService()) + #expect(service.configuredCredentials == ownerCredentials) + #expect(mainService.configuredCredentials == credentials(for: fixture.mainSession)) + withPreservedPreferences(["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.EntitledAudioChannelCount", "OpenNOW.Stream.EntitledInGameSettingsPersistence"]) { + OPNStreamPreferences.clearDlssEntitlements() + OPNStreamPreferences.saveEntitledAudioChannelCount(2) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(false) + service.completeSubscription(at: 0, subscription: membership()) + #expect(model.dlssMembershipState == .loaded(membership().dlssEntitlements)) + #expect(model.currentLaunchEntitlements == OPNLaunchEntitlements(subscription: membership())) + #expect(OPNStreamPreferences.loadDlssEntitlements() == .none) + #expect(OPNStreamPreferences.loadEntitledAudioChannelCount() == 2) + #expect(!OPNStreamPreferences.loadEntitledInGameSettingsPersistence()) + } + try expectDetachedResumeIsAdmitted(model, fixture: fixture) + #expect(fixture.login.activeSession === fixture.mainSession) + #expect(SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count == 2) + } + } + + @Test(arguments: [false, true]) + func signingOutAfterAnOfflineSwitchRevokesDetachedCatalogAccess(_ isMembershipLoaded: Bool) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + installOfflineRefreshResponse() + defer { SessionManagerURLProtocol.uninstall(host: "login.nvidia.com") } + let fixture = try DetachedAccountTestFixture() + defer { fixture.tearDown() } + try expireIDTokens(in: fixture) + try await restoreOfflineSession(fixture.detachedAccount, fixture: fixture) + try await restoreOfflineSession(fixture.mainAccount, fixture: fixture) + let service = CatalogAccountServiceStub() + let model = makeDetachedCatalog(fixture: fixture, service: service) + model.loadAccount() + try #require(service.subscriptionRequestCount == 1) + if isMembershipLoaded { + service.completeSubscription(at: 0, subscription: membership()) + #expect(model.currentLaunchEntitlements.audioChannelCount == 8) + } + let membershipBeforeSignOut = model.account.membershipTier + + await fixture.login.signOutAccount(fixture.detachedAccount) + #expect(fixture.auth.endedEmails == [fixture.detachedEmail]) + #expect(fixture.detachedSession.accessToken.isEmpty && fixture.detachedSession.idToken.isEmpty) + #expect(fixture.detachedSession.refreshToken.isEmpty && fixture.detachedSession.clientToken.isEmpty) + #expect(!model.configureCatalogService()) + if !isMembershipLoaded { service.completeSubscription(at: 0, subscription: membership()) } + #expect(model.account.membershipTier == membershipBeforeSignOut) + #expect(model.currentLaunchEntitlements == .none) + model.activeHomeSession = resumableSession() + model.resumeActiveHomeSession() + #expect(!fixture.registry.hasSessions) + #expect(model.launchErrorMessage == "Sign in again before launching a game.") + model.loadAccount() + #expect(service.subscriptionRequestCount == 1) + #expect(model.dlssMembershipState == .signedOut) + #expect(fixture.login.activeSession === fixture.mainSession) + } + } + + private func installOfflineRefreshResponse() { + SessionManagerURLProtocol.install(host: "login.nvidia.com") { _ in + SessionManagerURLProtocol.response(json: ["error": "invalid_grant"], status: 401) + } + } + + private func expireIDTokens(in fixture: DetachedAccountTestFixture) throws { + for session in [fixture.mainSession, fixture.detachedSession] { + let claims: [String: Any] = ["sub": session.userId, "email": session.accountEmail, "exp": 1] + let payload = try JSONSerialization.data(withJSONObject: claims).base64EncodedString() + .replacingOccurrences(of: "+", with: "-") + .replacingOccurrences(of: "/", with: "_") + .replacingOccurrences(of: "=", with: "") + session.idToken = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.\(payload)." + try #require(JarvisSessionParser.idTokenExpiry(session.idToken) == 1_000) + try #require(session.canContinueOffline && !session.isExpired) + } + } + + private func restoreOfflineSession(_ account: LoginAccount, fixture: DetachedAccountTestFixture) async throws { + let previousRequestCount = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").count + try #require(await fixture.login.restoreAccountSession(account)) + try #require(fixture.login.successMessage.contains("offline session")) + let refreshRequests = SessionManagerURLProtocol.recordedRequests(host: "login.nvidia.com").dropFirst(previousRequestCount) + try #require(refreshRequests.count == 1) + let request = try #require(refreshRequests.first) + #expect(request.httpMethod == "POST") + #expect(request.url?.path == "/token") + #expect(fixture.login.activeAccount === account) + } + + private func makeDetachedCatalog(fixture: DetachedAccountTestFixture, service: CatalogAccountServiceStub) -> CatalogViewModel { + CatalogViewModel( + account: fixture.detachedAccount, session: fixture.detachedSession, gameService: service, + discordPresence: OfflineCatalogPresence(), sessionRegistry: fixture.registry, + sessionResultStore: OPNGameSessionResultStore(), onRefreshAuth: { false } + ) + } + + private func credentials(for session: LoginSession) -> CatalogAccountServiceStub.SessionCredentials { + .init(accessToken: session.accessToken, idToken: session.idToken, userID: session.userId) + } + + private func membership() -> OPNSubscriptionInfo { + var subscription = OPNSubscriptionInfo() + subscription.membershipTier = "Ultimate" + subscription.entitledAudioChannelCount = 8 + subscription.isInGameSettingsPersistenceEntitled = true + subscription.dlssEntitlements = DlssEntitlements(superResolution: DlssCapability(isEntitled: true)) + return subscription + } + + private func resumableSession() -> OPNActiveSessionObject { + OPNActiveSessionObject(sessionId: "offline-owner-seat", appId: 123, status: 2, serverIp: "owner-seat.nvidiagrid.net", streamingBaseUrl: OPNGameService.defaultStreamingBaseUrl, signalingUrl: "") + } + + private func expectDetachedResumeIsAdmitted(_ model: CatalogViewModel, fixture: DetachedAccountTestFixture) throws { + try withPreservedPreferences([OPNSessionReadyAction.modeKey]) { + OPNSessionReadyAction.mode = .off + model.activeHomeSession = resumableSession() + model.resumeActiveHomeSession() + let ownerID = try #require(fixture.detachedAccount.resolveStableAccountID()) + let owned = try #require(fixture.registry.session(ownedBy: ownerID)) + defer { owned.cancelStreamLaunch() } + let configuration = try #require(owned.configuration) + #expect(configuration.accessToken == fixture.detachedSession.idToken) + #expect(configuration.owningAccountID == ownerID.rawValue) + #expect(configuration.launchEntitlements == OPNLaunchEntitlements(subscription: membership())) + #expect(configuration.resumeSessionID == "offline-owner-seat") + #expect(model.launchErrorMessage.isEmpty) + } + } +} diff --git a/Tests/Games/CatalogServiceIsolationTests.swift b/Tests/Games/CatalogServiceIsolationTests.swift new file mode 100644 index 000000000..9f55ff582 --- /dev/null +++ b/Tests/Games/CatalogServiceIsolationTests.swift @@ -0,0 +1,204 @@ +import Foundation +import Testing +@testable import OpenNOW + +private final class CatalogRetryCounter: @unchecked Sendable { + private let lock = NSLock() + private var attempts = 0 + + var count: Int { lock.withLock { attempts } } + + func next() -> Int { + lock.withLock { + attempts += 1 + return attempts + } + } +} + +@MainActor +@Suite struct CatalogServiceIsolationTests { + @Test func loadingAnotherCatalogKeepsEveryRequestOwnedByTheInitiatingAccount() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let counter = CatalogRetryCounter() + SessionManagerURLProtocol.install(host: "*", paths: ["/graphql", "/v2/serverInfo"]) { request in + Self.catalogResponse(request, counter: counter) + } + defer { SessionManagerURLProtocol.uninstall(host: "*") } + let first = makeCatalog(email: "catalog-a@example.test", userID: "user-a-\(UUID().uuidString)") + let second = makeCatalog(email: "catalog-b@example.test", userID: "user-b-\(UUID().uuidString)") + defer { + first.session.purgeTokens() + second.session.purgeTokens() + } + let firstService = try #require(first.gameService as? OPNGameService) + let secondService = try #require(second.gameService as? OPNGameService) + let firstBridge = try #require(first.launchBridge as? OPNGameLaunchBridge) + #expect((firstBridge.gameService as? OPNGameService) === firstService) + #expect(firstService !== secondService) + #expect(firstService !== OPNGameService.shared) + firstService.graphqlURL = "https://catalog-a.example.test/graphql" + firstService.persistedGraphqlURL = firstService.graphqlURL + firstService.providerStreamingBaseUrl = "https://provider-a.nvidiagrid.net/" + secondService.providerStreamingBaseUrl = "https://provider-b.nvidiagrid.net/" + first.configureCatalogService() + let favoritesSucceeded = await withCheckedContinuation { continuation in + firstService.fetchFavoriteGameObjects { success, _, _ in continuation.resume(returning: success) } + second.configureCatalogService() + first.selectGame(Self.game()) + first.toggleFavoriteSelectedGame() + first.confirmSelectedVariantOwned() + } + #expect(favoritesSucceeded) + try await waitUntil { first.errorMessage == "Ownership rejected for isolation test" && !first.isLoadingFavorites && counter.count == 3 } + let requests = SessionManagerURLProtocol.recordedRequests(host: "*") + let graphql = requests.filter { $0.url?.path == "/graphql" } + #expect(graphql.count >= 4) + #expect(graphql.allSatisfy { $0.url?.host == "catalog-a.example.test" }) + #expect(graphql.allSatisfy { $0.value(forHTTPHeaderField: "Authorization") == "GFNJWT \(first.session.idToken)" }) + let queries = SessionManagerURLProtocol.recordedJSONBodies(host: "*").compactMap { $0["query"] as? String } + #expect(queries.contains { $0.contains("AddFavoriteApp") }) + #expect(queries.contains { $0.contains("AddOwnedVariant") }) + #expect(requests.filter { $0.url?.path == "/v2/serverInfo" }.contains { $0.url?.host == "provider-a.nvidiagrid.net" }) + #expect(requests.filter { $0.url?.path == "/v2/serverInfo" }.contains { $0.url?.host == "provider-b.nvidiagrid.net" }) + #expect(firstService.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + #expect(secondService.providerStreamingBaseURL() == "https://provider-b.nvidiagrid.net/") + } + } + + @Test func coalescedProviderCompletionsConfigureEveryRequestingService() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let providerID = "coalesced-\(UUID().uuidString)" + SessionManagerURLProtocol.install(host: "pcs.geforcenow.com") { _ in + Self.providerResponse(firstID: providerID, secondID: "unused") + } + defer { SessionManagerURLProtocol.uninstall(host: "pcs.geforcenow.com") } + let first = OPNGameService() + let second = OPNGameService() + var outcomes: [Bool] = [] + first.fetchProviderInfo(idpId: providerID) { success, _, _, _ in outcomes.append(success) } + second.fetchProviderInfo(idpId: providerID) { success, _, _, _ in outcomes.append(success) } + try await waitUntil { outcomes.count == 2 } + #expect(outcomes == [true, true]) + #expect(first.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + #expect(second.providerStreamingBaseURL() == first.providerStreamingBaseURL()) + #expect(SessionManagerURLProtocol.recordedRequests(host: "pcs.geforcenow.com").count == 1) + } + } + + @Test func aLateProviderCompletionCannotReplaceANewerProviderContext() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let firstID = "provider-a-\(UUID().uuidString)" + let secondID = "provider-b-\(UUID().uuidString)" + SessionManagerURLProtocol.install(host: "pcs.geforcenow.com") { _ in + Self.providerResponse(firstID: firstID, secondID: secondID) + } + defer { SessionManagerURLProtocol.uninstall(host: "pcs.geforcenow.com") } + let changed = OPNGameService() + let unchanged = OPNGameService() + var outcomes: [Bool] = [] + changed.fetchProviderInfo(idpId: firstID) { success, _, _, _ in outcomes.append(success) } + changed.fetchProviderInfo(idpId: secondID) { success, _, _, _ in outcomes.append(success) } + unchanged.fetchProviderInfo(idpId: firstID) { success, _, _, _ in outcomes.append(success) } + try await waitUntil { outcomes.count == 3 } + #expect(outcomes.filter { !$0 }.count == 1) + #expect(changed.providerStreamingBaseURL() == "https://provider-b.nvidiagrid.net/") + #expect(unchanged.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + let cached = OPNGameService() + cached.fetchProviderInfo(idpId: firstID) { success, _, _, _ in outcomes.append(success) } + try await waitUntil { outcomes.count == 4 } + #expect(outcomes.last == true) + #expect(cached.providerStreamingBaseURL() == "https://provider-a.nvidiagrid.net/") + #expect(SessionManagerURLProtocol.recordedRequests(host: "pcs.geforcenow.com").count == 2) + } + } + + @Test func launchPrefetchResolvesItsOwnProviderBeforeSendingAccountRequests() async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + let providerID = "prefetch-\(UUID().uuidString)" + SessionManagerURLProtocol.install(host: "*", paths: ["/v1/serviceUrls", "/v2/serverInfo", "/graphql"]) { request in + if request.url?.path == "/v1/serviceUrls" { + return Self.providerResponse(firstID: "unused", secondID: providerID) + } + if request.url?.path == "/v2/serverInfo" { + return SessionManagerURLProtocol.response(json: ["requestStatus": ["serverId": "GFN-PC"]]) + } + return SessionManagerURLProtocol.response(json: ["data": [ + "panels": [], + "apps": ["items": [], "pageInfo": ["hasNextPage": false]], + "filterGroupDefinitions": [], + "sortOrderDefinitions": [["id": "a_to_z", "orderBy": "sortName:ASC", "label": "A-Z"]], + ]]) + } + defer { SessionManagerURLProtocol.uninstall(host: "*") } + let prefetch = CatalogLaunchPrefetch() + prefetch.gameService.graphqlURL = "https://prefetch.example.test/graphql" + prefetch.gameService.persistedGraphqlURL = prefetch.gameService.graphqlURL + let userID = "prefetch-user-\(UUID().uuidString)" + var events: Set = [] + prefetch.start(accountIdentifier: userID, accessToken: "prefetch-access", idToken: "prefetch-id", idpId: providerID) + _ = prefetch.attach(accountIdentifier: userID) { event in + switch event { + case .panels(let kind, _), .panelsFailed(let kind, _): events.insert(kind.rawValue) + case .games(let kind, _), .gamesFailed(let kind, _): events.insert(kind.rawValue) + } + } + try await waitUntil { events.count == 4 } + prefetch.recordLaunchResultsAdopted(accountIdentifier: userID) + let requests = SessionManagerURLProtocol.recordedRequests(host: "*") + let streamingRequests = requests.filter { $0.url?.path == "/v2/serverInfo" } + #expect(!streamingRequests.isEmpty) + #expect(streamingRequests.allSatisfy { $0.url?.host == "provider-b.nvidiagrid.net" }) + #expect(streamingRequests.allSatisfy { $0.value(forHTTPHeaderField: "Authorization") == "GFNJWT prefetch-id" }) + #expect(prefetch.gameService !== OPNGameService.shared) + } + } + + private func makeCatalog(email: String, userID: String) -> CatalogViewModel { + let account = makeLoginAccountForTesting(email: email, userId: userID) + let session = makeLoginSessionForTesting(accountEmail: email, userId: userID) + return CatalogViewModel(account: account, session: session, onRefreshAuth: { false }) + } + + private func waitUntil(_ predicate: () -> Bool) async throws { + for _ in 0..<300 { + if predicate() { return } + try await Task.sleep(for: .milliseconds(10)) + } + try #require(predicate(), "The scripted catalog response did not arrive") + } + + nonisolated private static func catalogResponse(_ request: URLRequest, counter: CatalogRetryCounter) -> (Int, Data) { + if request.url?.path == "/v2/serverInfo" { + return SessionManagerURLProtocol.response(json: ["requestStatus": ["serverId": "GFN-PC"]]) + } + let query = SessionManagerURLProtocol.bodyData(from: request).flatMap { + (try? JSONSerialization.jsonObject(with: $0)) as? [String: Any] + }?["query"] as? String ?? "" + if query.contains("AddOwnedVariant") { + return SessionManagerURLProtocol.response(json: ["errors": [["message": "Ownership rejected for isolation test"]]]) + } + if query.contains("AddFavoriteApp") { + return SessionManagerURLProtocol.response(json: ["data": ["addFavoriteApp": ["app": ["id": "game-a"]]]]) + } + if counter.next() == 1 { + return SessionManagerURLProtocol.response(json: ["errors": [["message": "PersistedQueryNotFound", "extensions": ["code": "PERSISTED_QUERY_NOT_FOUND"]]]], status: 400) + } + return SessionManagerURLProtocol.response(json: ["data": ["panels": []]]) + } + + nonisolated private static func providerResponse(firstID: String, secondID: String) -> (Int, Data) { + SessionManagerURLProtocol.response(json: ["gfnServiceInfo": ["gfnServiceEndpoints": [ + ["idpId": firstID, "loginProvider": "Provider A", "streamingServiceUrl": "https://provider-a.nvidiagrid.net/"], + ["idpId": secondID, "loginProvider": "Provider B", "streamingServiceUrl": "https://provider-b.nvidiagrid.net/"], + ]]]) + } + + private static func game() -> OPNCatalogGameObject { + var game = OPNGameInfo() + game.id = "game-a" + game.title = "Account A Game" + game.variants = [OPNGameVariant(id: "123", appStore: "STEAM")] + return OPNCatalogGameObject(game: game) + } +} diff --git a/Tests/Games/DlssOverridesTests.swift b/Tests/Games/DlssOverridesTests.swift index d75910dcd..22386c2c4 100644 --- a/Tests/Games/DlssOverridesTests.swift +++ b/Tests/Games/DlssOverridesTests.swift @@ -342,7 +342,7 @@ import Testing frameGenerationMultipliers: [3, 4, 5, 6] ) ) - let settings: [String: Any] = ["dlssOverrides": [ + let settings: [String: Any] = [OPNLaunchEntitlements.settingsKey: OPNLaunchEntitlements(dlss: OPNStreamPreferences.loadDlssEntitlements()).storedDictionary, "dlssOverrides": [ "srMode": 6, "srModel": 2, "srScale": 80, @@ -382,7 +382,8 @@ import Testing rayReconstruction: DlssCapability(isEntitled: false, isUpsell: true) ) ) - let features = OPNSessionManager().requestedStreamingFeatures([:], hdrEnabled: false) + let settings: [String: Any] = [OPNLaunchEntitlements.settingsKey: OPNLaunchEntitlements(dlss: OPNStreamPreferences.loadDlssEntitlements()).storedDictionary] + let features = OPNSessionManager().requestedStreamingFeatures(settings, hdrEnabled: false) let overrides = try #require(features["dlssOverrides"] as? [String: Any]) #expect(overrides["srMode"] as? Int == DlssSuperResolutionMode.dlaa.rawValue) #expect(overrides["fgMode"] as? Int == 0) diff --git a/Tests/Games/SessionManagerTests.swift b/Tests/Games/SessionManagerTests.swift index f533625dd..16be0fb04 100644 --- a/Tests/Games/SessionManagerTests.swift +++ b/Tests/Games/SessionManagerTests.swift @@ -63,6 +63,7 @@ func expectReleaseCloudMatchRequestBody(_ requestData: [String: Any], metadata: var settings = minimalSettings() settings["networkTestSessionId"] = "stale-session-id" settings["enablePersistingInGameSettings"] = true + settings[OPNLaunchEntitlements.settingsKey] = OPNLaunchEntitlements(isInGameSettingsPersistenceEntitled: true).storedDictionary settings["partnerCustomData"] = "partner-data" settings["userAge"] = 21 settings["streamingQualityProfile"] = 4 diff --git a/Tests/Services/MainWindowAccountHandoffTests.swift b/Tests/Services/MainWindowAccountHandoffTests.swift new file mode 100644 index 000000000..ebf0f5c08 --- /dev/null +++ b/Tests/Services/MainWindowAccountHandoffTests.swift @@ -0,0 +1,90 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized) struct MainWindowAccountHandoffTests { + @Test func signInRisesAboveEveryObstructionUntilTheHandoffEnds() { + let main = makeWindow() + let detached = makeWindow() + let covering = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 20) + detached.level = NSWindow.Level(rawValue: covering.rawValue + 4) + let handoff = OPNMainWindowAccountHandoff(mainWindow: { main }, coveringLevels: { [covering] }) + defer { + handoff.end() + main.close() + detached.close() + } + + handoff.begin(from: detached) + + #expect(main.level.rawValue > covering.rawValue) + #expect(main.level.rawValue > detached.level.rawValue) + #expect(main.isVisible) + handoff.end() + #expect(main.level == .normal) + #expect(detached.level.rawValue == covering.rawValue + 4) + } + + @Test func anAbsentMainSceneReceivesThePendingElevationWhenItAttaches() { + let main = makeWindow() + let covering = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 8) + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { [covering] }) + defer { + handoff.end() + main.close() + } + + handoff.begin(from: nil) + #expect(main.level == .normal) + handoff.attach(main) + + #expect(main.level.rawValue == covering.rawValue + 1) + handoff.end() + #expect(main.level == .normal) + } + + @Test func coveringLevelChangesAreObservedOnlyForTheLifetimeOfTheHandoff() { + let main = makeWindow() + var covering = NSWindow.Level(rawValue: NSWindow.Level.mainMenu.rawValue + 3) + let handoff = OPNMainWindowAccountHandoff(mainWindow: { main }, coveringLevels: { [covering] }) + defer { + handoff.end() + main.close() + } + handoff.begin(from: nil) + covering = NSWindow.Level(rawValue: covering.rawValue + 10) + NotificationCenter.default.post(name: OPNStreamBorderless.coveringStateDidChange, object: nil) + #expect(main.level.rawValue == covering.rawValue + 1) + + handoff.end() + NotificationCenter.default.post(name: OPNStreamBorderless.coveringStateDidChange, object: nil) + + #expect(main.level == .normal) + } + + @Test func ordinaryPresentationPreservesTheOriginalLevelAcrossRepeatedRequests() { + let main = makeWindow() + main.level = .floating + let handoff = OPNMainWindowAccountHandoff(mainWindow: { main }, coveringLevels: { [] }) + defer { + handoff.end() + main.close() + } + + handoff.begin(from: nil) + handoff.begin(from: nil) + #expect(main.level == .floating) + handoff.end() + + #expect(main.level == .floating) + } + + private func makeWindow() -> NSWindow { + _ = NSApplication.shared + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 640, height: 480), + styleMask: [.titled, .closable], backing: .buffered, defer: false) + window.isReleasedWhenClosed = false + return window + } +} diff --git a/Tests/Settings/DetachedSessionModelTests.swift b/Tests/Settings/DetachedSessionModelTests.swift index 8eab1adaf..8a7018fff 100644 --- a/Tests/Settings/DetachedSessionModelTests.swift +++ b/Tests/Settings/DetachedSessionModelTests.swift @@ -72,7 +72,7 @@ import Testing let row = DetachedAccountRow(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available) viewModel.confirm(row: row, accountID: idB) - viewModel.backToSetup(accountID: idB) + viewModel.backToSetup() #expect(viewModel.phase == .setup) #expect(preset.peek(forAccount: idB.rawValue) == OPNStreamControllerSelection()) @@ -83,6 +83,37 @@ import Testing #expect(!DetachedSessionViewModel(preset: OPNStreamControllerPreset(), hasOtherStreams: false).takesPadsFromOtherStreams) } + @Test func changingAccountsDropsThePreviousAccountsUnconsumedControllerChoice() throws { + let idA = try #require(accountA) + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false, registry: OPNGameSessionRegistry()) + viewModel.setController("pad-a", enabled: false) + viewModel.confirm(row: .init(email: "a@example.com", displayName: "A", membershipTier: "Free", availability: .available), accountID: idA) + + viewModel.confirm(row: .init(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available), accountID: idB) + + #expect(preset.peek(forAccount: idA.rawValue) == OPNStreamControllerSelection()) + #expect(preset.peek(forAccount: idB.rawValue).disabled == ["pad-a"]) + #expect(viewModel.selectedEmail == "b@example.com") + viewModel.discardPendingSelection() + #expect(preset.peek(forAccount: idB.rawValue) == OPNStreamControllerSelection()) + } + + @Test func discardingAChoiceDoesNotEraseAReplacementMadeByAnotherWindow() throws { + let idB = try #require(accountB) + let preset = OPNStreamControllerPreset() + let viewModel = DetachedSessionViewModel(preset: preset, hasOtherStreams: false, registry: OPNGameSessionRegistry()) + viewModel.setController("pad-a", enabled: false) + viewModel.confirm(row: .init(email: "b@example.com", displayName: "B", membershipTier: "Free", availability: .available), accountID: idB) + let replacement = OPNStreamControllerSelection(disabled: ["pad-b"], takesPadsFromOtherStreams: true) + preset.store(replacement, forAccount: idB.rawValue) + + viewModel.discardPendingSelection() + + #expect(preset.peek(forAccount: idB.rawValue) == replacement) + } + @Test func theCouchCoopFlagIsRegisteredAndOffByDefault() { #expect(OPNLabs.flags.contains(OPNLabs.couchCoop)) #expect(OPNLabs.couchCoop.id == "couchCoop") diff --git a/Tests/Settings/DetachedSessionPresentationTests.swift b/Tests/Settings/DetachedSessionPresentationTests.swift new file mode 100644 index 000000000..951b68239 --- /dev/null +++ b/Tests/Settings/DetachedSessionPresentationTests.swift @@ -0,0 +1,360 @@ +import AppKit +import SwiftUI +import Testing +import Vision +@testable import OpenNOW + +@MainActor +@Suite(.serialized) struct DetachedSessionPresentationTests { + @Test func dismissingAnUnrelatedConfirmationDoesNotDismissTheCurrentOwner() { + let presentation = OPNConfirmationPresentation() + let owner = UUID() + var dismissals = 0 + presentation.present(request, ownerID: owner, onDismiss: { dismissals += 1 }) + + presentation.dismiss(ownerID: UUID()) + + #expect(presentation.request != nil) + #expect(dismissals == 0) + presentation.dismiss(ownerID: owner) + #expect(presentation.request == nil) + #expect(dismissals == 1) + } + + @Test func replacingAConfirmationRetiresOnlyThePreviousOwnersState() { + let presentation = OPNConfirmationPresentation() + let previousOwner = UUID() + let currentOwner = UUID() + var previousDismissals = 0 + var currentDismissals = 0 + presentation.present(request, ownerID: previousOwner, onDismiss: { previousDismissals += 1 }) + presentation.present(request, ownerID: currentOwner, onDismiss: { currentDismissals += 1 }) + + presentation.dismiss(ownerID: previousOwner) + + #expect(previousDismissals == 1) + #expect(currentDismissals == 0) + #expect(presentation.request != nil) + presentation.dismiss(ownerID: currentOwner) + #expect(currentDismissals == 1) + } + + @Test func aDescendantConfirmationUsesItsDetachedRootsPresenter() async throws { + _ = NSApplication.shared + let confirmation = OPNConfirmationPresentation() + let title = "Detached confirmation \(UUID().uuidString)" + let hosting = NSHostingView(rootView: DetachedConfirmationFixture(title: title) + .modifier(DetachedSessionModalHost(confirmation: confirmation, reportIssue: OPNReportIssuePresentation())) + .environment(\.opnUIScale, 1.25)) + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1100, height: 680), + styleMask: [.titled], backing: .buffered, defer: false) + window.isReleasedWhenClosed = false + window.contentView = hosting + window.orderFront(nil) + defer { + confirmation.dismiss() + if OPNConfirmationPresentation.shared.request?.title == title { OPNConfirmationPresentation.shared.dismiss() } + window.close() + } + for _ in 0..<100 { + if confirmation.request != nil { break } + try await Task.sleep(for: .milliseconds(10)) + } + + #expect(confirmation.request?.title == title) + #expect(OPNConfirmationPresentation.shared.request?.title != title) + } + + @Test(arguments: [CGFloat(1.25), CGFloat(1.5)]) + func aDetachedConfirmationDrawsOnlyInItsOwnRootAtLargerScales(_ scale: CGFloat) throws { + OPNDesign.applyTheme(accent: .cloudGreen, appearance: .dark, systemColorScheme: .dark) + let detached = OPNConfirmationPresentation() + detached.present(request, onDismiss: {}) + let other = OPNConfirmationPresentation() + let detachedCapture = try render( + confirmation: detached, report: OPNReportIssuePresentation(), scale: scale, + name: "detached-confirmation-\(scale).png" + ) + let otherCapture = try render( + confirmation: other, report: OPNReportIssuePresentation(), scale: scale, + name: "other-window-no-confirmation-\(scale).png" + ) + + #expect(detachedCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) > 500) + #expect(otherCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0, y: 0, width: 0.15, height: 1), threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0.85, y: 0, width: 0.15, height: 1), threshold: 40) == 0) + detached.dismiss() + } + + /// Offscreen evidence covers scoped modal chrome; the hosted test below verifies the form contents. + @Test(arguments: [CGFloat(1.25), CGFloat(1.5)]) + func aDetachedReportDrawsOnlyInItsOwnRootAtLargerScales(_ scale: CGFloat) throws { + OPNDesign.applyTheme(accent: .cloudGreen, appearance: .dark, systemColorScheme: .dark) + let detached = OPNReportIssuePresentation() + let other = OPNReportIssuePresentation() + detached.present(context: OPNIssueReportContext()) + let detachedCapture = try render( + confirmation: OPNConfirmationPresentation(), report: detached, scale: scale, + name: "detached-report-\(scale).png" + ) + let otherCapture = try render( + confirmation: OPNConfirmationPresentation(), report: other, scale: scale, + name: "other-window-no-report-\(scale).png" + ) + + #expect(detached.isPresented) + #expect(!other.isPresented) + #expect(detachedCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) > 500) + #expect(otherCapture.greenDominantPixelCount(in: GlassCapture.wholeImage, threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0, y: 0, width: 1, height: 0.01), threshold: 40) == 0) + #expect(detachedCapture.greenDominantPixelCount(in: CGRect(x: 0, y: 0.99, width: 1, height: 0.01), threshold: 40) == 0) + detached.dismiss() + } + + @Test( + .enabled(if: GlassEvidence.captureDirectory != nil), + .disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason)), + arguments: [CGFloat(1.25), CGFloat(1.5)] + ) + func aHostedDetachedReportExposesEditableFieldsAtLargerScales(_ scale: CGFloat) async throws { + _ = NSApplication.shared + OPNDesign.applyTheme(accent: .cloudGreen, appearance: .dark, systemColorScheme: .dark) + let report = OPNReportIssuePresentation() + report.present(context: OPNIssueReportContext()) + report.draft.category = .bug + report.draft.summary = HostedReportFixture.summary + report.draft.details = HostedReportFixture.details + report.draft.includesDiagnostics = false + defer { report.dismiss() } + + for viewport in HostedReportViewport.allCases { + let name = "detached-report-hosted-\(scale)-\(viewport.rawValue).png" + let capture = try await captureHostedReport(report, scale: scale, viewport: viewport, name: name) + try verifyCapturedReport(capture, viewport: viewport, name: name) + } + } + + private func captureHostedReport( + _ report: OPNReportIssuePresentation, scale: CGFloat, viewport: HostedReportViewport, name: String + ) async throws -> GlassCapture { + let suiteName = "DetachedSessionHostedReportTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suiteName)) + let probe = HostedReportWindowProbe() + defer { + probe.close() + defaults.removePersistentDomain(forName: suiteName) + } + let content = OPNDesign.Surface.app + .modifier(DetachedSessionModalHost(confirmation: OPNConfirmationPresentation(), reportIssue: report)) + .background(HostedReportWindowReader(probe: probe)) + .environment(\.opnUIScale, scale) + .environment(\.opnLiquidGlass, .off) + .environment(\.colorScheme, .dark) + .environment(\.locale, Locale(identifier: "en_US_POSIX")) + .transaction { $0.disablesAnimations = true } + .defaultAppStorage(defaults) + let captureTask = Task { + try await GlassEvidence.captureInWindow( + content, over: OPNDesign.Surface.app, + size: CGSize(width: 1100, height: 680), named: name + ) + } + do { + let form = try await probe.waitForNativeForm() + try form.reveal(viewport) + try form.verifyVisibleField(in: viewport) + let capture = try await captureTask.value + #expect(report.draft.summary == HostedReportFixture.summary) + #expect(report.draft.details == HostedReportFixture.details) + return capture + } catch { + captureTask.cancel() + _ = await captureTask.result + throw error + } + } + + private func verifyCapturedReport(_ capture: GlassCapture, viewport: HostedReportViewport, name: String) throws { + let request = VNRecognizeTextRequest() + request.recognitionLevel = .accurate + request.recognitionLanguages = ["en-US"] + request.usesLanguageCorrection = false + try VNImageRequestHandler(cgImage: capture.image, options: [:]).perform([request]) + let lines = (request.results ?? []).compactMap { $0.topCandidates(1).first?.string } + let recognized = lines.joined(separator: " ").lowercased().filter { $0.isLetter || $0.isNumber } + let expectedField = viewport == .top ? HostedReportFixture.summary : HostedReportFixture.details + let expected = expectedField.lowercased().filter { $0.isLetter || $0.isNumber } + #expect(recognized.contains(expected), "\(name) omitted its seeded field value. Recognized text: \(lines)") + #expect(recognized.contains("sendreport"), "\(name) lost its pinned footer. Recognized text: \(lines)") + if viewport == .bottom { + #expect(recognized.contains("includediagnostics"), "\(name) did not expose the bottom of the form. Recognized text: \(lines)") + } + } + + private var request: OPNConfirmationPresentation.Request { + .init(eyebrow: "DETACHED ACCOUNT", title: "Sign out of this account?", + message: "Only this saved account is signed out. The account in the main window stays connected.", + actions: [OPNConfirmationAction("CANCEL", role: .cancel) {}, OPNConfirmationAction("SIGN OUT", role: .destructive) {}]) + } + + private func render(confirmation: OPNConfirmationPresentation, report: OPNReportIssuePresentation, scale: CGFloat, name: String) throws -> GlassCapture { + let suiteName = "DetachedSessionPresentationTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suiteName)) + defer { defaults.removePersistentDomain(forName: suiteName) } + let renderer = ImageRenderer(content: OPNDesign.Surface.app + .modifier(DetachedSessionModalHost(confirmation: confirmation, reportIssue: report)) + .frame(width: 1100, height: 680) + .environment(\.opnUIScale, scale) + .environment(\.opnLiquidGlass, .off) + .transaction { $0.disablesAnimations = true } + .defaultAppStorage(defaults)) + renderer.scale = 2 + let image = try #require(renderer.cgImage) + if let directory = ProcessInfo.processInfo.environment["OPN_SNAPSHOT_DIR"] { + let bitmap = NSBitmapImageRep(cgImage: image) + let png = try #require(bitmap.representation(using: .png, properties: [:])) + try png.write(to: URL(fileURLWithPath: directory).appendingPathComponent(name)) + } + return GlassCapture(image: image) + } +} + +private struct DetachedConfirmationFixture: View { + let title: String + @State private var isPresented = true + + var body: some View { + OPNDesign.Surface.app + .opnConfirmation( + isPresented: $isPresented, eyebrow: "DETACHED", title: title, message: "Scoped to this window", + actions: [OPNConfirmationAction("CLOSE", role: .cancel) { isPresented = false }] + ) + } +} + +private enum HostedReportFixture { + static let summary = "Detached summary evidence" + static let details = "Detached description evidence" +} + +private enum HostedReportViewport: String, CaseIterable { + case top + case bottom +} + +@MainActor +private struct HostedReportNativeForm { + let summary: NSView + let details: NSView + let scrollView: NSScrollView + + func reveal(_ viewport: HostedReportViewport) throws { + let document = try #require(scrollView.documentView) + let clipView = scrollView.contentView + #expect(document.bounds.height > clipView.bounds.height, "The fixture must exercise vertical form scrolling") + let start = document.bounds.minY + let end = max(start, document.bounds.maxY - clipView.bounds.height) + let isShowingDocumentEnd = viewport == .bottom + let originY = isShowingDocumentEnd == document.isFlipped ? end : start + clipView.scroll(to: NSPoint(x: clipView.bounds.minX, y: originY)) + scrollView.reflectScrolledClipView(clipView) + scrollView.window?.contentView?.layoutSubtreeIfNeeded() + scrollView.window?.displayIfNeeded() + } + + func verifyVisibleField(in viewport: HostedReportViewport) throws { + #expect(HostedReportWindowProbe.editableValue(in: summary) == HostedReportFixture.summary) + #expect(HostedReportWindowProbe.editableValue(in: details) == HostedReportFixture.details) + let window = try #require(scrollView.window) + #expect(window.isVisible) + let field = viewport == .top ? summary : details + #expect(field.window === window) + #expect(!field.isHiddenOrHasHiddenAncestor) + #expect(!field.visibleRect.isEmpty, "The seeded field is outside the form viewport") + #expect(field.visibleRect.width >= field.bounds.width * 0.95, "The form clips the native field horizontally") + } +} + +@MainActor +private final class HostedReportWindowProbe { + private var window: NSWindow? + + func attach(_ window: NSWindow?) { + guard let window else { return } + window.isReleasedWhenClosed = false + self.window = window + } + + func close() { + guard let window else { return } + self.window = nil + window.orderOut(nil) + window.contentView = nil + window.close() + } + + func waitForNativeForm() async throws -> HostedReportNativeForm { + for _ in 0..<100 { + if let form = nativeForm() { return form } + try await Task.sleep(for: .milliseconds(10)) + } + return try #require(nativeForm(), "The hosted report did not install editable Summary/Description controls inside a scroll view") + } + + static func editableValue(in view: NSView) -> String? { + if let field = view as? NSTextField, field.isEditable { return field.stringValue } + if let editor = view as? NSTextView, editor.isEditable { return editor.string } + return nil + } + + private func nativeForm() -> HostedReportNativeForm? { + guard let content = window?.contentView else { return nil } + content.layoutSubtreeIfNeeded() + let inputs = editableInputs(in: content) + guard let summary = inputs.first(where: { Self.editableValue(in: $0) == HostedReportFixture.summary }), + let details = inputs.first(where: { Self.editableValue(in: $0) == HostedReportFixture.details }), + !summary.bounds.isEmpty, !details.bounds.isEmpty, + let scrollView = formScrollView(containing: summary, details: details) else { return nil } + return HostedReportNativeForm(summary: summary, details: details, scrollView: scrollView) + } + + private func editableInputs(in view: NSView) -> [NSView] { + let descendants = view.subviews.flatMap { editableInputs(in: $0) } + guard Self.editableValue(in: view) != nil else { return descendants } + return [view] + descendants + } + + private func formScrollView(containing summary: NSView, details: NSView) -> NSScrollView? { + var ancestor = summary.superview + while let view = ancestor { + if let scrollView = view as? NSScrollView, let document = scrollView.documentView, + details.isDescendant(of: document) { return scrollView } + ancestor = view.superview + } + return nil + } +} + +private struct HostedReportWindowReader: NSViewRepresentable { + let probe: HostedReportWindowProbe + + func makeNSView(context: Context) -> ProbeView { + let view = ProbeView(frame: .zero) + view.probe = probe + return view + } + + func updateNSView(_ nsView: ProbeView, context: Context) { + probe.attach(nsView.window) + } + + final class ProbeView: NSView { + weak var probe: HostedReportWindowProbe? + + override func viewDidMoveToWindow() { + super.viewDidMoveToWindow() + probe?.attach(window) + } + } +} diff --git a/Tests/Stream/DlssStatsHUDRowTests.swift b/Tests/Stream/DlssStatsHUDRowTests.swift index 1ffb9c2ec..6eb7ae9a7 100644 --- a/Tests/Stream/DlssStatsHUDRowTests.swift +++ b/Tests/Stream/DlssStatsHUDRowTests.swift @@ -2,7 +2,9 @@ // starts, and - once the seat's `DlssState` message arrives - what the running session actually // has. The request is never presented as an activation. +import AppKit import Foundation +import SwiftUI import Testing @testable import OpenNOW @@ -11,9 +13,8 @@ import Testing @Test func theStatsHUDReportsTheNextSessionRequest() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let rows = makeOverlay().nativeStatsDlssGroup.rows + let rows = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.rows #expect(rows.map(\.label) == ["Super Resolution", "Frame Generation", "Ray Reconstruction"]) #expect(rows.map(\.value) == ["DLAA 100%", "Dynamic up to 4×", "Preset D"]) } @@ -22,16 +23,14 @@ import Testing @Test func theDlssGroupSaysTheValuesAreForTheNextSession() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - #expect(makeOverlay().nativeStatsDlssGroup.label == "DLSS · NEXT SESSION") + #expect(makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.label == "DLSS · NEXT SESSION") } } @Test func theDlssGroupNeverClaimsAnOverrideIsActive() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let group = makeOverlay().nativeStatsDlssGroup + let group = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup let text = ([group.label] + group.rows.flatMap { [$0.label, $0.value, $0.detail ?? ""] }).joined(separator: " ").lowercased() let words = Set(text.split { !$0.isLetter }.map(String.init)) for claim in ["active", "enabled", "unsupported", "on"] { @@ -53,8 +52,7 @@ import Testing @Test func gameSettingsReadAsInGame() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(.useGameSettings) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let rows = makeOverlay().nativeStatsDlssGroup.rows + let rows = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.rows #expect(rows.map(\.value) == ["In-game", "In-game", "In-game"]) } } @@ -71,7 +69,6 @@ import Testing @Test func theSeatsReportReplacesTheRequestOnceItArrives() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) let group = makeOverlay(applied: appliedState).nativeStatsDlssGroup #expect(group.label == "DLSS · THIS SESSION") @@ -85,7 +82,6 @@ import Testing @Test func aSeatThatOverrodeNothingReadsAsInGame() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) let group = makeOverlay(applied: NvstDlssStateNotification( isSuperResolutionOverridden: false, @@ -104,7 +100,6 @@ import Testing @Test func theFrameGenerationRowPrefersTheMultiplierTheSeatIsRunning() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) let state = NvstDlssStateNotification( isSuperResolutionOverridden: appliedState.isSuperResolutionOverridden, @@ -140,13 +135,58 @@ import Testing @Test func theHudRowAndTheCardSummaryAgree() { withDlssPreferencesPreserved { OPNStreamPreferences.saveDlssOverrides(requestedOverrides) - OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) - let overrides = OPNStreamPreferences.effectiveDlssOverrides() - let rows = makeOverlay().nativeStatsDlssGroup.rows + let overrides = requestedOverrides.applyingAccess(grantedEntitlements) + let rows = makeOverlay(entitlements: grantedEntitlements).nativeStatsDlssGroup.rows #expect(overrides.nextSessionRequestSummary == "SR \(rows[0].value) · FG \(rows[1].value) · RR \(rows[2].value)") } } + @Test func concurrentLaunchSnapshotsIgnoreChangesToTheActiveAccountEntitlementMirror() { + withDlssPreferencesPreserved { + OPNStreamPreferences.saveDlssOverrides(requestedOverrides) + let entitled = makeOverlay(entitlements: grantedEntitlements) + let unentitled = makeOverlay() + + OPNStreamPreferences.saveDlssEntitlements(grantedEntitlements) + #expect(entitled.nativeStatsDlssGroup.rows.map(\.value) == ["DLAA 100%", "Dynamic up to 4×", "Preset D"]) + #expect(unentitled.nativeStatsDlssGroup.rows.map(\.value) == ["In-game", "In-game", "In-game"]) + + OPNStreamPreferences.clearDlssEntitlements() + #expect(entitled.nativeStatsDlssGroup.rows.map(\.value) == ["DLAA 100%", "Dynamic up to 4×", "Preset D"]) + #expect(unentitled.nativeStatsDlssGroup.rows.map(\.value) == ["In-game", "In-game", "In-game"]) + } + } + + @Test(arguments: [CGFloat(1.25), CGFloat(1.5)]) + func eachLaunchMembershipRendersAtNonDefaultInterfaceScales(_ scale: CGFloat) throws { + try withDlssPreferencesPreserved { + OPNStreamPreferences.saveDlssOverrides(requestedOverrides) + let overlays = [ + ("entitled", makeOverlay(entitlements: grantedEntitlements)), + ("unentitled", makeOverlay()), + ] + for (name, overlay) in overlays { + let width = (NativeNVSTStatsPanel.width + 24) * scale + let height = 220 * scale + let content = NativeNVSTStatsPanel(transport: "NATIVE NVST", heroes: [], groups: [overlay.nativeStatsDlssGroup]) + .padding(12) + .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) + .environment(\.opnUIScale, scale) + .opnInterfaceScale(scale) + .frame(width: width, height: height) + .background(Color.black) + .transaction { $0.disablesAnimations = true } + let renderer = ImageRenderer(content: content) + renderer.scale = 2 + let image = try #require(renderer.nsImage) + let pixelTolerance = 1 / renderer.scale + #expect(abs(image.size.width - width) < pixelTolerance) + #expect(abs(image.size.height - height) < pixelTolerance) + try writeSnapshot(image, name: "hud-dlss-\(name)-\(Int(scale * 100)).png") + } + } + } + private var requestedOverrides: DlssOverrides { DlssOverrides( superResolutionMode: DlssSuperResolutionMode.dlaa.rawValue, @@ -168,13 +208,25 @@ import Testing ) } - private func makeOverlay(applied: NvstDlssStateNotification? = nil) -> NativeNVSTStatsOverlay { - NativeNVSTStatsOverlay( + private func makeOverlay(entitlements: DlssEntitlements = .none, applied: NvstDlssStateNotification? = nil) -> NativeNVSTStatsOverlay { + let configuration = StreamLaunchConfiguration( + title: "Game", applicationID: "100", accessToken: "test-token", accountLinked: true, selectedStore: "steam" + ).snapshottingEntitlements(OPNLaunchEntitlements(dlss: entitlements), providerStreamingBaseURL: OPNGameService.defaultStreamingBaseUrl) + return NativeNVSTStatsOverlay( stats: NativeNVSTStreamStatsModel(), - applicationID: "100", + applicationID: configuration.applicationID, detailLevel: .advanced, hdrModeText: "", - appliedDlssState: applied + appliedDlssState: applied, + dlssEntitlements: configuration.launchEntitlements.dlss ) } + + private func writeSnapshot(_ image: NSImage, name: String) throws { + guard let directory = ProcessInfo.processInfo.environment["OPN_SNAPSHOT_DIR"] else { return } + let tiff = try #require(image.tiffRepresentation) + let bitmap = try #require(NSBitmapImageRep(data: tiff)) + let png = try #require(bitmap.representation(using: .png, properties: [:])) + try png.write(to: URL(fileURLWithPath: directory).appendingPathComponent(name)) + } } diff --git a/Tests/Stream/LaunchEntitlementIsolationTests.swift b/Tests/Stream/LaunchEntitlementIsolationTests.swift new file mode 100644 index 000000000..a51103804 --- /dev/null +++ b/Tests/Stream/LaunchEntitlementIsolationTests.swift @@ -0,0 +1,133 @@ +import Foundation +import Testing +@testable import OpenNOW + +@MainActor +private final class LaunchEntitlementPresenceStub: DiscordPresenceServing { + var isEnabled = false + private(set) var lastState = DiscordPresenceState.idle + + func update(_ state: DiscordPresenceState) { lastState = state } +} + +@MainActor +@Suite struct LaunchEntitlementIsolationTests { + @Test func eachAppOwnedLaunchKeepsItsMembershipThroughFinalPayloadResolution() throws { + try withPreservedPreferences(["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.DlssOverrides", "OpenNOW.Stream.EntitledAudioChannelCount", "OpenNOW.Stream.EntitledInGameSettingsPersistence", "OpenNOW.Stream.PersistInGameSettings", OPNSessionReadyAction.modeKey]) { + OPNSessionReadyAction.mode = .off + let granted = OPNLaunchEntitlements( + dlss: DlssEntitlements(superResolution: DlssCapability(isEntitled: true)), + audioChannelCount: 8, + isInGameSettingsPersistenceEntitled: true + ) + let firstService = OPNGameService() + let first = try makeOwnedSession(email: "launch-a@example.test", entitlements: granted, provider: "https://provider-a.nvidiagrid.net/", gameService: firstService) + let second = try makeOwnedSession(email: "launch-b@example.test", entitlements: .none, provider: "https://provider-b.nvidiagrid.net/") + defer { + first.cancelStreamLaunch() + second.cancelStreamLaunch() + first.session.purgeTokens() + second.session.purgeTokens() + } + let firstConfiguration = try #require(first.configuration) + let secondConfiguration = try #require(second.configuration) + #expect(firstConfiguration.launchEntitlements == granted) + #expect(secondConfiguration.launchEntitlements == .none) + #expect(firstConfiguration.metadata[OPNLaunchEntitlements.providerMetadataKey] == "https://provider-a.nvidiagrid.net/") + #expect(secondConfiguration.resumesExistingSession) + firstService.providerStreamingBaseUrl = "https://provider-changed.nvidiagrid.net/" + #expect(first.configuration?.metadata[OPNLaunchEntitlements.providerMetadataKey] == "https://provider-a.nvidiagrid.net/") + + OPNStreamPreferences.clearDlssEntitlements() + OPNStreamPreferences.saveEntitledAudioChannelCount(2) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(false) + OPNStreamPreferences.savePersistInGameSettings(true) + let coordinator = OPNStreamSessionCoordinator() + var firstSettings = coordinator.makeSettings(configuration: firstConfiguration) + #expect(OPNLaunchEntitlements(settings: firstSettings) == granted) + #expect(firstSettings["enablePersistingInGameSettings"] as? Bool == true) + OPNStreamPreferences.saveDlssEntitlements(granted.dlss) + OPNStreamPreferences.saveEntitledAudioChannelCount(8) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(true) + var secondSettings = coordinator.makeSettings(configuration: secondConfiguration) + #expect(secondSettings["enablePersistingInGameSettings"] as? Bool == false) + firstSettings["surroundMode"] = "7.1" + secondSettings["surroundMode"] = "7.1" + var overrides = DlssOverrides.useGameSettings + overrides.superResolutionModel = 1 + overrides.superResolutionMode = DlssSuperResolutionMode.quality.rawValue + firstSettings["dlssOverrides"] = overrides.storedDictionary + secondSettings["dlssOverrides"] = overrides.storedDictionary + try expectPayloadEntitlements(firstSettings: firstSettings, secondSettings: secondSettings) + } + } + + @Test func aLaunchWithoutAnAccountSnapshotCannotBorrowGlobalMembership() { + withPreservedPreferences(["OpenNOW.Stream.DlssEntitlements", "OpenNOW.Stream.DlssOverrides"]) { + OPNStreamPreferences.saveDlssEntitlements(DlssEntitlements(superResolution: DlssCapability(isEntitled: true))) + var overrides = DlssOverrides.useGameSettings + overrides.superResolutionModel = 1 + overrides.superResolutionMode = DlssSuperResolutionMode.quality.rawValue + let settings: [String: Any] = ["dlssOverrides": overrides.storedDictionary] + let features = OPNSessionManager().requestedStreamingFeatures(settings, hdrEnabled: false) + #expect((features["dlssOverrides"] as? [String: Any])?["srMode"] as? Int == 0) + #expect(OPNLaunchEntitlements(metadata: [OPNLaunchEntitlements.metadataKey: "not-json"]) == .none) + } + } + + @Test func automaticRegionSelectionUsesTheLaunchingProviderUnlessTheReaderChoseARegion() { + withPreservedPreferences(["OpenNOW.Stream.RegionUrl"]) { + OPNStreamPreferences.saveSelectedRegionUrl("") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-a.nvidiagrid.net/") == "https://provider-a.nvidiagrid.net/") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-b.nvidiagrid.net/") == "https://provider-b.nvidiagrid.net/") + OPNStreamPreferences.saveSelectedRegionUrl("https://chosen-region.nvidiagrid.net/") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-a.nvidiagrid.net/") == "https://chosen-region.nvidiagrid.net/") + #expect(OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: "https://provider-b.nvidiagrid.net/") == "https://chosen-region.nvidiagrid.net/") + } + } + + private func expectPayloadEntitlements(firstSettings: [String: Any], secondSettings: [String: Any]) throws { + let coordinator = OPNStreamSessionCoordinator() + var cloudVariables = OPNStreamCloudVariables() + cloudVariables.entitledAudioChannelCount = 2 + let firstCloudSettings = coordinator.settingsByApplyingCloudVariables(firstSettings, variables: cloudVariables) + cloudVariables.entitledAudioChannelCount = 8 + let secondCloudSettings = coordinator.settingsByApplyingCloudVariables(secondSettings, variables: cloudVariables) + let manager = OPNSessionManager() + var capabilities = OPNStreamDeviceCapabilities() + capabilities.audioOutputChannelCount = 8 + let firstPayloadSettings = manager.settingsByApplyingCloudVariables(firstCloudSettings, capabilities: capabilities) + let secondPayloadSettings = manager.settingsByApplyingCloudVariables(secondCloudSettings, capabilities: capabilities) + let appID = try #require(OPNLaunchAppId.resolve("123")) + let firstPayload = manager.sessionRequestData(launchAppId: appID, internalTitle: "Game A", settings: firstPayloadSettings, capabilities: capabilities, hdrEnabled: false, deviceId: "device-a", selectedStore: "STEAM") + let secondPayload = manager.sessionRequestData(launchAppId: appID, internalTitle: "Game B", settings: secondPayloadSettings, capabilities: capabilities, hdrEnabled: false, deviceId: "device-b", selectedStore: "STEAM") + #expect(firstPayload["requestedAudioFormat"] as? Int == 3) + #expect(secondPayload["requestedAudioFormat"] as? Int == 1) + #expect(firstPayload["enablePersistingInGameSettings"] as? Bool == true) + #expect(secondPayload["enablePersistingInGameSettings"] as? Bool == false) + #expect(firstPayload[OPNLaunchEntitlements.settingsKey] == nil) + let firstFeatures = try #require(firstPayload["requestedStreamingFeatures"] as? [String: Any]) + let secondFeatures = try #require(secondPayload["requestedStreamingFeatures"] as? [String: Any]) + #expect((firstFeatures["dlssOverrides"] as? [String: Any])?["srMode"] as? Int == DlssSuperResolutionMode.quality.rawValue) + #expect((secondFeatures["dlssOverrides"] as? [String: Any])?["srMode"] as? Int == 0) + } + + private func makeOwnedSession(email: String, entitlements: OPNLaunchEntitlements, provider: String, gameService: OPNGameService = OPNGameService()) throws -> OPNGameSession { + let account = makeLoginAccountForTesting(email: email, userId: email) + let session = makeLoginSessionForTesting(accountEmail: email, userId: email) + let registry = OPNGameSessionRegistry() + let owned = try #require(registry.begin( + account: account, + session: session, + gameService: gameService, + launchBridge: OPNGameLaunchBridge(gameService: gameService), + discordPresence: LaunchEntitlementPresenceStub(), + streamProfile: OPNStreamPreferenceProfile(), + launchEntitlements: entitlements, + providerStreamingBaseURL: provider, + results: OPNGameSessionResultStore() + )) + owned.beginResume(title: "Game", applicationID: "123", sessionID: "resume-\(email)", server: "seat.nvidiagrid.net", game: nil) + return owned + } +} diff --git a/Tests/Stream/StreamPreflightProviderRoutingTests.swift b/Tests/Stream/StreamPreflightProviderRoutingTests.swift new file mode 100644 index 000000000..bfcd6b912 --- /dev/null +++ b/Tests/Stream/StreamPreflightProviderRoutingTests.swift @@ -0,0 +1,134 @@ +import Foundation +import Testing +@testable import OpenNOW + +@MainActor +@Suite struct StreamPreflightProviderRoutingTests { + enum RegionSelection: CaseIterable, Sendable { + case automaticUncached, automaticCachedElsewhere, explicit + + var selectedURL: String { + switch self { + case .automaticUncached, .automaticCachedElsewhere: "" + case .explicit: "https://preflight-chosen.nvidiagrid.net/" + } + } + + var cachedRegions: [OPNStreamRegionOption] { + switch self { + case .automaticUncached: [] + case .automaticCachedElsewhere: + [OPNStreamRegionOption(name: "Another provider's cached region", url: "https://preflight-stale.nvidiagrid.net/", latencyMs: 10)] + case .explicit: + [OPNStreamRegionOption(name: "Chosen region", url: selectedURL, latencyMs: 10)] + } + } + } + + enum NetworkOutcome: CaseIterable, Sendable { + case completed, provisioned, unavailable, malformed + + var isSessionAvailable: Bool { self == .completed || self == .provisioned } + } + + @Test(arguments: RegionSelection.allCases, NetworkOutcome.allCases) + func preflightKeepsEachOwnersRouteThroughFinalAllocation(_ region: RegionSelection, _ outcome: NetworkOutcome) async throws { + try await networkTestIsolationLock.withLock { @MainActor @Sendable in + SessionManagerURLProtocol.install(host: "*", paths: ["/v2/serverInfo", "/v2/nettestsession", "/v2/session"]) { request in + Self.response(to: request, outcome: outcome) + } + defer { SessionManagerURLProtocol.uninstall(host: "*") } + let storage = OPNAppPreferenceStorage.standard + let activeSessionKey = OPNSessionManager.persistedActiveSessionIdKey + let savedActiveSessionID = withExclusivePreferenceDomain { storage.string(forKey: activeSessionKey) } + defer { + withExclusivePreferenceDomain { + if let savedActiveSessionID { storage.set(savedActiveSessionID, forKey: activeSessionKey) } + if savedActiveSessionID == nil { storage.removeObject(forKey: activeSessionKey) } + storage.synchronize() + } + } + + for owner in ["a", "b"] { + let providerURL = "https://preflight-provider-\(owner).nvidiagrid.net/" + let configuration = StreamLaunchConfiguration( + title: "Owner \(owner)", applicationID: "123", accessToken: "owner-\(owner)-token", + accountLinked: true, selectedStore: "Steam", + metadata: [OPNLaunchEntitlements.providerMetadataKey: providerURL] + ) + let providerSnapshot = try #require(configuration.metadata[OPNLaunchEntitlements.providerMetadataKey]) + let expectedURL = region.selectedURL.isEmpty ? providerURL : region.selectedURL + let expectedHost = try #require(URL(string: expectedURL)?.host) + let requestOffset = SessionManagerURLProtocol.recordedRequests(host: "*").count + let preflight = await runPreflight(configuration: configuration, providerURL: providerSnapshot, region: region) + #expect(preflight.streamingBaseUrl == expectedURL) + #expect(preflight.networkTestSessionId == (outcome.isSessionAvailable ? "nettest-\(expectedHost)" : "")) + #expect(preflight.serverReportedWarning == (outcome == .provisioned)) + if outcome == .completed { #expect(preflight.latencyMs == 12) } + + var settings = minimalSettings() + settings["networkTestSessionId"] = preflight.networkTestSessionId + let launch = PreparedStreamLaunch(settings: settings, streamingBaseUrl: preflight.streamingBaseUrl) + let coordinator = OPNStreamSessionCoordinator(sessionManager: OPNSessionManager(), deviceId: "owner-\(owner)-device") + let allocated = try await coordinator.allocateSession(configuration: configuration, launch: launch) + #expect(allocated.isReady) + try await waitForRegionRequest(after: requestOffset) + let requests = Array(SessionManagerURLProtocol.recordedRequests(host: "*").dropFirst(requestOffset)) + let networkRequests = requests.filter { $0.url?.path == "/v2/nettestsession" } + let allocationRequests = requests.filter { $0.url?.path == "/v2/session" } + #expect(networkRequests.count == 1) + #expect(allocationRequests.count == 1) + #expect((networkRequests + allocationRequests).allSatisfy { $0.url?.host == expectedHost && $0.httpMethod == "POST" }) + #expect(requests.allSatisfy { $0.value(forHTTPHeaderField: "Authorization") == "GFNJWT \(configuration.accessToken)" }) + #expect(requests.filter { $0.url?.path == "/v2/serverInfo" }.allSatisfy { $0.url?.absoluteString == providerURL + "v2/serverInfo" }) + let allocationPayloads = SessionManagerURLProtocol.recordedJSONBodies(host: "*").compactMap { $0["sessionRequestData"] as? [String: Any] } + let payload = try #require(allocationPayloads.last) + #expect(payload["deviceHashId"] as? String == "owner-\(owner)-device") + #expect((payload["networkTestSessionId"] as? String ?? "") == preflight.networkTestSessionId) + #expect(configuration.metadata[OPNLaunchEntitlements.providerMetadataKey] == providerSnapshot) + } + #expect(SessionManagerURLProtocol.strayRequestSummaries().isEmpty) + } + } + + private func runPreflight(configuration: StreamLaunchConfiguration, providerURL: String, region: RegionSelection) async -> OPNStreamNetworkPreflightResult { + await withCheckedContinuation { continuation in + withPreservedPreferences(["OpenNOW.Stream.RegionUrl", "OpenNOW.Stream.CachedRegions"]) { + OPNStreamPreferences.saveSelectedRegionUrl(region.selectedURL) + OPNStreamPreferences.saveCachedRegions(region.cachedRegions) + OPNStreamPreferences.runNetworkPreflight(token: configuration.accessToken, providerStreamingBaseUrl: providerURL, requestedMaxBitrateMbps: 50) { + continuation.resume(returning: $0) + } + } + } + } + + private func waitForRegionRequest(after offset: Int) async throws { + for _ in 0..<300 { + let requests = SessionManagerURLProtocol.recordedRequests(host: "*").dropFirst(offset) + if requests.contains(where: { $0.url?.path == "/v2/serverInfo" }) { return } + try await Task.sleep(for: .milliseconds(10)) + } + Issue.record("The provider region refresh did not reach the scripted transport") + } + + nonisolated private static func response(to request: URLRequest, outcome: NetworkOutcome) -> (Int, Data) { + guard request.url?.path != "/v2/serverInfo" else { + return SessionManagerURLProtocol.response(json: [:], status: 404) + } + guard request.url?.path != "/v2/session" else { + return SessionManagerURLProtocol.response(json: sessionResponse(statusCode: 1, sessionStatus: 2)) + } + guard outcome != .unavailable else { return SessionManagerURLProtocol.response(json: [:], status: 404) } + guard outcome != .malformed else { return SessionManagerURLProtocol.response(json: [:]) } + let host = request.url?.host ?? "missing-host" + var response: [String: Any] = ["netTestSession": [ + "sessionId": "nettest-\(host)", + "connectionInfo": [["ip": "measurement-endpoint.example.test", "port": 443, "appLevelProtocol": 5]], + ]] + if outcome == .completed { + response["testResult"] = ["status": "COMPLETED", "latencyMs": 12, "maxPacketSize": 1400] + } + return SessionManagerURLProtocol.response(json: response) + } +} diff --git a/View/Catalog/CatalogTopBarViews.swift b/View/Catalog/CatalogTopBarViews.swift index 67af37c03..6842636ca 100644 --- a/View/Catalog/CatalogTopBarViews.swift +++ b/View/Catalog/CatalogTopBarViews.swift @@ -14,6 +14,7 @@ struct CatalogTopBar: View { let onForget: (LoginAccount) -> Void @Environment(\.opnUIScale) private var uiScale @Environment(\.openWindow) private var openWindow + @Environment(\.opnReportIssuePresentation) private var reportIssuePresentation @AppStorage(OPNInterfacePreferences.controllerModeEnabledKey) private var controllerModeEnabled = false @ObservedObject private var keybindings = OPNKeybindingsObserver.shared @@ -98,7 +99,7 @@ struct CatalogTopBar: View { .opnTooltip("Search games (\(keybindings.combo(for: .openSearch).label))") .matchedGeometryEffect(id: Self.searchGeometryID, in: searchTransition) } - Button { OPNReportIssuePresentation.shared.present(context: .current(viewModel: viewModel)) } label: { + Button { (reportIssuePresentation ?? .shared).present(context: .current(viewModel: viewModel)) } label: { CatalogTopBarIconLabel(systemName: "exclamationmark.bubble") } .buttonStyle(.opnPressable(scale: 0.90)) diff --git a/View/Components/OPNConfirmationModal.swift b/View/Components/OPNConfirmationModal.swift index 7176d9941..d5ec4741d 100644 --- a/View/Components/OPNConfirmationModal.swift +++ b/View/Components/OPNConfirmationModal.swift @@ -43,17 +43,26 @@ final class OPNConfirmationPresentation: ObservableObject { @Published private(set) var request: Request? private var onDismiss: (() -> Void)? + private var ownerID: UUID? - func present(_ request: Request, onDismiss: @escaping () -> Void) { + func present(_ request: Request, ownerID: UUID = UUID(), onDismiss: @escaping () -> Void) { + if self.ownerID != ownerID { dismiss() } + self.ownerID = ownerID self.request = request self.onDismiss = onDismiss } + func dismiss(ownerID: UUID) { + guard self.ownerID == ownerID else { return } + dismiss() + } + /// Clears the modal and asks whoever presented it to clear its own state. Safe to call when /// nothing is presented. func dismiss() { guard request != nil else { return } request = nil + ownerID = nil let dismissHandler = onDismiss onDismiss = nil dismissHandler?() @@ -97,7 +106,7 @@ struct OPNConfirmationPanel: View { /// Hosts the shared confirmation modal at the app root. The full-cover scrim sits behind the /// centered panel; the scrim, close control, Escape, and `.cancel` all dismiss. struct OPNConfirmationOverlay: View { - @ObservedObject private var presentation = OPNConfirmationPresentation.shared + @ObservedObject var presentation: OPNConfirmationPresentation = .shared var body: some View { ZStack { @@ -115,6 +124,10 @@ struct OPNConfirmationOverlay: View { } } +extension EnvironmentValues { + @Entry var opnConfirmationPresentation: OPNConfirmationPresentation? +} + /// App-shell confirmation dialog following the modal spec: Panel background, 1px Stroke Regular, /// 2px accent top bar, modal shadow, App Bar header with eyebrow and 20pt bold title, and a footer /// of square buttons. Replaces the native `.confirmationDialog` / `.alert`, which render rounded @@ -252,6 +265,8 @@ extension View { } private struct OPNConfirmationPresenter: ViewModifier { + @Environment(\.opnConfirmationPresentation) private var scopedPresentation + @State private var ownerID = UUID() let isPresented: Binding let eyebrow: String let title: String @@ -262,21 +277,24 @@ private struct OPNConfirmationPresenter: ViewModifier { content .onAppear { syncPresentation(isPresented.wrappedValue) } .onChange(of: isPresented.wrappedValue) { _, presented in syncPresentation(presented) } - .onDisappear { OPNConfirmationPresentation.shared.dismiss() } + .onDisappear { presentation.dismiss(ownerID: ownerID) } } + private var presentation: OPNConfirmationPresentation { scopedPresentation ?? .shared } + private func syncPresentation(_ presented: Bool) { guard presented else { - OPNConfirmationPresentation.shared.dismiss() + presentation.dismiss(ownerID: ownerID) return } - OPNConfirmationPresentation.shared.present( + presentation.present( OPNConfirmationPresentation.Request( eyebrow: eyebrow, title: title, message: message, actions: actions ), + ownerID: ownerID, onDismiss: { isPresented.wrappedValue = false } ) } diff --git a/View/Components/OPNReportIssueModal.swift b/View/Components/OPNReportIssueModal.swift index 8c6fdadd1..4bbbb5583 100644 --- a/View/Components/OPNReportIssueModal.swift +++ b/View/Components/OPNReportIssueModal.swift @@ -3,7 +3,7 @@ import SwiftUI /// Window-level host for the Report an Issue modal. Mounted once at the app root so the form /// reaches the catalog, the login wall, and Settings through the same path. struct OPNReportIssueOverlay: View { - @ObservedObject private var presentation = OPNReportIssuePresentation.shared + @ObservedObject var presentation: OPNReportIssuePresentation = .shared @Environment(\.opnUIScale) private var uiScale var body: some View { @@ -31,6 +31,14 @@ struct OPNReportIssueOverlay: View { } } +extension EnvironmentValues { + @Entry var opnReportIssuePresentation: OPNReportIssuePresentation? +} + +extension FocusedValues { + @Entry var opnReportIssuePresentation: OPNReportIssuePresentation? +} + /// App-shell support form following the modal spec: Panel background, 1px Stroke Regular, 2px accent /// top bar, modal shadow, App Bar header, and a square-button footer. /// diff --git a/View/ContentView.swift b/View/ContentView.swift index 005421794..b5f292668 100644 --- a/View/ContentView.swift +++ b/View/ContentView.swift @@ -114,6 +114,7 @@ struct ContentView: View { .environment(\.opnTileDensity, tileDensity) .opnObservingCornerStyle() .opnObservingLiquidGlass() + .focusedSceneValue(\.opnReportIssuePresentation, OPNReportIssuePresentation.shared) .onDisappear { root.unbind() } // Binding happens inside the bootstrap, not in an `onAppear`: SwiftUI starts a `.task` // before it calls `onAppear`, so the two would race. @@ -123,6 +124,10 @@ struct ContentView: View { .onChange(of: accounts.count) { _, _ in syncViewModel() } .onChange(of: sessions.count) { _, _ in syncViewModel() } .onChange(of: devices.count) { _, _ in syncViewModel() } + .onChange(of: viewModel.signInRequest) { previous, current in + guard previous != nil, current == nil else { return } + OPNMainWindowAccountHandoff.shared.end() + } .onOpenURL { url in root.handleOpenURL(url) } } @@ -199,6 +204,7 @@ private struct WindowTitleConfigurator: NSViewRepresentable { if #available(macOS 11.0, *) { window.titlebarSeparatorStyle = .none } + OPNMainWindowAccountHandoff.shared.attach(window) } } diff --git a/View/DetachedSession/DetachedSessionView.swift b/View/DetachedSession/DetachedSessionView.swift index 98b124195..1d1aecddb 100644 --- a/View/DetachedSession/DetachedSessionView.swift +++ b/View/DetachedSession/DetachedSessionView.swift @@ -1,8 +1,7 @@ // The Detach New Session window: one more account streaming beside the ones already running. // -// Pick an account, choose the controllers this stream leaves out, then browse and launch from the -// account's own catalog. It never touches the main window's active account: the catalog is built -// for the chosen account explicitly and launches through the shared game session registry. +// Browse and launch from an account-owned catalog without activating it in the main window. +// Explicit account-management actions use the main window's login flow. import AppKit import SwiftData @@ -10,6 +9,7 @@ import SwiftUI struct DetachedSessionView: View { @Environment(\.modelContext) private var modelContext + @Environment(\.openWindow) private var openWindow @Query(sort: \LoginAccount.lastLoginAt, order: .reverse) private var accounts: [LoginAccount] @Query(sort: \LoginSession.issuedAt, order: .reverse) private var sessions: [LoginSession] @AppStorage(OPNInterfacePreferences.uiScaleKey) private var uiScale = OPNInterfacePreferences.defaultUIScale @@ -19,6 +19,11 @@ struct DetachedSessionView: View { @ObservedObject private var devices = ControllerMappingDevices.shared @State private var viewModel = DetachedSessionViewModel() @State private var windowTitle = "Detached Session" + // swiftlint:disable:next view_constructs_service -- window-owned UI state, not a service; DESIGN.md Confirmation Modal + @StateObject private var confirmation = OPNConfirmationPresentation() + // swiftlint:disable:next view_constructs_service -- window-owned UI state, not a service; DESIGN.md Report an Issue Modal + @StateObject private var reportIssue = OPNReportIssuePresentation() + @State private var windowReference = DetachedSessionWindowReference() private let registry = OPNGameSessionRegistry.shared @@ -64,26 +69,49 @@ struct DetachedSessionView: View { catalog(for: email) } } + .modifier(DetachedSessionModalHost(confirmation: confirmation, reportIssue: reportIssue)) .frame(minWidth: 640, minHeight: 480) .ignoresSafeArea() .background(OPNDesign.Surface.app) - .background(DetachedSessionWindowConfigurator(title: windowTitle)) + .background(DetachedSessionWindowConfigurator(title: windowTitle, reference: windowReference)) + .background(OPNInterfaceScaleDensityBooster(scale: uiScale)) .environment(\.opnUIScale, uiScale) .opnObservingCornerStyle() .opnObservingLiquidGlass() .preferredColorScheme(preferredColorScheme) .onAppear { devices.refresh() } - .onDisappear { viewModel.discardPendingSelection(for: selectedAccount?.storedAccountID) } + .onChange(of: viewModel.phase) { _, phase in + guard phase == .setup else { return } + windowTitle = "Detached Session" + } + .onDisappear { + viewModel.discardPendingSelection() + confirmation.dismiss() + } } - /// The sign-in belongs to the main window, so this window steps back to an ordinary level first: - /// raised above the stream windows it would sit over the main window and hide the very flow it - /// just started. - private static func addAccountInMainWindow() { - NSApp.keyWindow?.level = .normal - OPNMainWindow.reveal() - OPNMenuBarSessionModel.shared.requestAddAccount() - OPNMainWindow.bringForward() + private func requestMainAccountAction(_ request: OPNMainAccountActions.Request) { + OPNMainWindowAccountHandoff.shared.begin(from: windowReference.window) + if OPNMainWindow.needsPresentation { + OPNDockIconController.showDockIcon() + openWindow(id: OPNMainWindow.identifier) + } + OPNMainAccountActions.shared.request(request) + } + + private func requestAccountMutation(_ account: LoginAccount, request: (OPNMainAccountActions.Target) -> OPNMainAccountActions.Request) { + guard let storedAccounts = try? modelContext.fetch(FetchDescriptor()), + let currentAccount = storedAccounts.first(where: { $0 === account }), + !modelContext.deletedModelsArray.contains(where: { $0 === account }) else { + confirmation.present( + .init(eyebrow: "ACCOUNT", title: "Account action unavailable", + message: "This saved account changed or was removed. Choose the account again before continuing.", + actions: [OPNConfirmationAction("BACK") { [weak confirmation = confirmation] in confirmation?.dismiss() }]), + onDismiss: viewModel.backToSetup + ) + return + } + requestMainAccountAction(request(.init(account: currentAccount))) } private var selectedAccount: LoginAccount? { @@ -134,7 +162,7 @@ struct DetachedSessionView: View { accountRow(row) } Button { - Self.addAccountInMainWindow() + requestMainAccountAction(.addAccount) } label: { Label("Add account…", systemImage: "person.badge.plus") } @@ -277,14 +305,14 @@ struct DetachedSessionView: View { CatalogView( account: account, session: session, - accounts: [account], - signedOutAccountEmails: [], + accounts: accounts, + signedOutAccountEmails: Set(accounts.filter { savedSession(for: $0) == nil }.map(\.email)), pendingGameShortcut: .constant(nil), - onSwitch: { _ in }, - onAddAccount: { Self.addAccountInMainWindow() }, - onPresentAccountChooser: {}, - onSignOut: { _ in }, - onForget: { _ in }, + onSwitch: switchDetachedAccount, + onAddAccount: { requestMainAccountAction(.addAccount) }, + onPresentAccountChooser: viewModel.backToSetup, + onSignOut: { requestAccountMutation($0, request: OPNMainAccountActions.Request.signOut) }, + onForget: { requestAccountMutation($0, request: OPNMainAccountActions.Request.forget) }, onRefreshAuth: { await DetachedSessionRefresher(modelContext: modelContext).refresh(account: account, session: session) }, onWindowTitleChange: { windowTitle = $0 ?? account.displayName }, isDetached: true @@ -294,21 +322,69 @@ struct DetachedSessionView: View { } else { VStack(spacing: OPNDesign.Spacing.medium(scale: uiScale)) { note("This account has no saved session. Sign in again from the main window.") - Button("BACK") { viewModel.backToSetup(accountID: selectedAccount?.storedAccountID) } + Button("SIGN IN AGAIN") { + guard let account = accounts.first(where: { $0.email == email }) else { + viewModel.backToSetup() + return + } + requestMainAccountAction(.reauthenticate(.init(account: account))) + } + .buttonStyle(VendorGetInButtonStyle(uiScale: uiScale)) + Button("BACK", action: viewModel.backToSetup) .buttonStyle(OPNModalSecondaryButtonStyle(uiScale: uiScale)) } .frame(maxWidth: .infinity, maxHeight: .infinity) } } + + private func switchDetachedAccount(_ account: LoginAccount) { + viewModel.backToSetup() + windowTitle = "Detached Session" + guard let storedAccounts = try? modelContext.fetch(FetchDescriptor()), + let currentAccount = storedAccounts.first(where: { $0 === account }), + let row = rows.first(where: { $0.email == currentAccount.email }) else { return } + guard savedSession(for: currentAccount) != nil else { + requestMainAccountAction(.reauthenticate(.init(account: currentAccount))) + return + } + guard row.availability.isSelectable, let accountID = currentAccount.storedAccountID else { return } + viewModel.confirm(row: row, accountID: accountID) + } +} + +struct DetachedSessionModalHost: ViewModifier { + @ObservedObject var confirmation: OPNConfirmationPresentation + @ObservedObject var reportIssue: OPNReportIssuePresentation + + func body(content: Content) -> some View { + ZStack { + content + OPNReportIssueOverlay(presentation: reportIssue) + .zIndex(92) + OPNConfirmationOverlay(presentation: confirmation) + .zIndex(95) + } + .environment(\.opnConfirmationPresentation, confirmation) + .environment(\.opnReportIssuePresentation, reportIssue) + .focusedSceneValue(\.opnReportIssuePresentation, reportIssue) + } +} + +@MainActor +private final class DetachedSessionWindowReference { + weak var window: NSWindow? } /// A detached window is an ordinary titled window with the catalog's transparent titlebar, raised /// above the borderless stream windows that cover the screens it is opened from. private struct DetachedSessionWindowConfigurator: NSViewRepresentable { let title: String + let reference: DetachedSessionWindowReference func makeNSView(context: Context) -> ConfigurationView { - ConfigurationView(frame: .zero) + let view = ConfigurationView(frame: .zero) + view.reference = reference + return view } func updateNSView(_ view: ConfigurationView, context: Context) { @@ -316,15 +392,20 @@ private struct DetachedSessionWindowConfigurator: NSViewRepresentable { view.apply() } + static func dismantleNSView(_ view: ConfigurationView, coordinator: ()) { + view.unbind() + } + final class ConfigurationView: NSView { var title = "" + weak var reference: DetachedSessionWindowReference? private var levelObserver: NSObjectProtocol? override func viewDidMoveToWindow() { super.viewDidMoveToWindow() + reference?.window = window guard window != nil else { - if let levelObserver { NotificationCenter.default.removeObserver(levelObserver) } - levelObserver = nil + unbind() return } apply() @@ -340,6 +421,12 @@ private struct DetachedSessionWindowConfigurator: NSViewRepresentable { } } + func unbind() { + if let levelObserver { NotificationCenter.default.removeObserver(levelObserver) } + levelObserver = nil + reference?.window = nil + } + func apply() { guard let window else { return } if !window.styleMask.contains(.fullSizeContentView) { diff --git a/View/Settings/SettingsReportViews.swift b/View/Settings/SettingsReportViews.swift index 79eb99fc8..b1d2f3c34 100644 --- a/View/Settings/SettingsReportViews.swift +++ b/View/Settings/SettingsReportViews.swift @@ -2,6 +2,7 @@ import SwiftUI /// The support entry beside Support Diagnostics: one button into the two-channel report form. struct ReportIssueSettingsPage: View { + @Environment(\.opnReportIssuePresentation) private var reportIssuePresentation let viewModel: CatalogViewModel let uiScale: CGFloat @@ -13,7 +14,7 @@ struct ReportIssueSettingsPage: View { SettingsCard(title: "Report an Issue", uiScale: uiScale) { HStack(alignment: .top, spacing: 10 * uiScale) { SettingsActionButton(title: "REPORT AN ISSUE", uiScale: uiScale) { - OPNReportIssuePresentation.shared.present(context: .current(viewModel: viewModel)) + (reportIssuePresentation ?? .shared).present(context: .current(viewModel: viewModel)) } Text("Open a pre-filled OpenNOW client report on GitHub, or send a stream quality issue to NVIDIA with a copyable session summary.") .font(.settingsFont(size: 12 * uiScale, weight: .medium)) diff --git a/View/Stream/NativeNVSTStreamStatsHUD.swift b/View/Stream/NativeNVSTStreamStatsHUD.swift index d4d4e0748..b8064562a 100644 --- a/View/Stream/NativeNVSTStreamStatsHUD.swift +++ b/View/Stream/NativeNVSTStreamStatsHUD.swift @@ -227,6 +227,7 @@ struct NativeNVSTStatsOverlay: View { let hdrModeText: String /// The seat's applied DLSS override state, nil until it reports one. let appliedDlssState: NvstDlssStateNotification? + let dlssEntitlements: DlssEntitlements var body: some View { NativeNVSTStatsPanel( @@ -285,7 +286,7 @@ struct NativeNVSTStatsOverlay: View { /// The request the next session will carry, which is all there is to report until the seat /// sends its own state. It composes the same labels the settings card's summary does. private var nativeStatsNextSessionDlssGroup: NativeNVSTStatsPanel.Group { - let overrides = OPNStreamPreferences.effectiveDlssOverrides() + let overrides = OPNStreamPreferences.loadDlssOverrides().applyingAccess(dlssEntitlements) return NativeNVSTStatsPanel.Group(label: "DLSS · NEXT SESSION", rows: [ NativeNVSTStatsPanel.Row(label: "Super Resolution", value: overrides.superResolutionRequestLabel), NativeNVSTStatsPanel.Row(label: "Frame Generation", value: overrides.frameGenerationRequestLabel), @@ -608,7 +609,8 @@ extension NativeNVSTMediaStreamSurface { applicationID: configuration.applicationID, detailLevel: model.statsDetail, hdrModeText: model.nativeHdrModeText, - appliedDlssState: model.nativeDlssState + appliedDlssState: model.nativeDlssState, + dlssEntitlements: model.configuration.launchEntitlements.dlss ) .opnTransition(.scale(scale: 0.94, anchor: model.statsPosition.transitionAnchor).combined(with: .opacity)) .streamStatsHUDPosition(model.statsPosition, isSidebarVisible: model.unifiedHUDVisible) diff --git a/ViewModel/AppRootViewModel.swift b/ViewModel/AppRootViewModel.swift index 31bacfa03..eb52fb2c1 100644 --- a/ViewModel/AppRootViewModel.swift +++ b/ViewModel/AppRootViewModel.swift @@ -27,6 +27,7 @@ final class AppRootViewModel: ObservableObject { func bind(login: LoginViewModel) { self.login = login + OPNMainAccountActions.shared.bind(login: login) guard fileOpenObserver == nil else { return } fileOpenObserver = NotificationCenter.default .publisher(for: .opnDidOpenFile) @@ -38,6 +39,7 @@ final class AppRootViewModel: ObservableObject { func unbind() { fileOpenObserver = nil + if let login { OPNMainAccountActions.shared.unbind(login: login) } login = nil } diff --git a/ViewModel/CatalogAccountRequestIdentity.swift b/ViewModel/CatalogAccountRequestIdentity.swift new file mode 100644 index 000000000..dc1ded72d --- /dev/null +++ b/ViewModel/CatalogAccountRequestIdentity.swift @@ -0,0 +1,49 @@ +import Foundation +import SwiftData + +struct CatalogAccountRequestIdentity: Equatable, Sendable { + private let accountEmail: String + private let accountID: String + private let accountUserID: String + private let accountProviderID: String + private let lastLoginAt: Date + private let sessionID: String + private let sessionEmail: String + private let userID: String + private let providerID: String + private let accessToken: String + private let idToken: String + private let refreshToken: String + private let issuedAt: Date + private let isAccountStored: Bool + private let isSessionStored: Bool + + @MainActor + init(account: LoginAccount, session: LoginSession) { + accountEmail = account.email + accountID = account.stableAccountID + accountUserID = account.userId + accountProviderID = account.providerIdpId + lastLoginAt = account.lastLoginAt + sessionID = session.id + sessionEmail = session.accountEmail + userID = session.userId + providerID = session.idpId + accessToken = session.accessToken + idToken = session.idToken + refreshToken = session.refreshToken + issuedAt = session.issuedAt + isAccountStored = account.modelContext != nil + isSessionStored = session.modelContext != nil + } + + @MainActor + func isCurrent(account: LoginAccount, session: LoginSession) -> Bool { + guard !account.isDeleted, !session.isDeleted, + !isAccountStored || account.modelContext != nil, + !isSessionStored || session.modelContext != nil, + account.email == session.accountEmail, + !session.accessToken.isEmpty || !session.idToken.isEmpty else { return false } + return self == Self(account: account, session: session) + } +} diff --git a/ViewModel/CatalogBrowsing.swift b/ViewModel/CatalogBrowsing.swift index 700e132e3..785bc537d 100644 --- a/ViewModel/CatalogBrowsing.swift +++ b/ViewModel/CatalogBrowsing.swift @@ -152,6 +152,7 @@ extension CatalogViewModel { } func browseCatalog(forceRefresh: Bool) { + guard configureCatalogService() else { return } browseGeneration += 1 let generation = browseGeneration let browseStartTime = CFAbsoluteTimeGetCurrent() @@ -159,7 +160,6 @@ extension CatalogViewModel { isLoadingMoreCatalog = false catalogEndCursor = "" errorMessage = "" - configureCatalogService() let query = searchQuery.trimmed let resultCount = catalogGames.count gameService.browseCatalogObject( diff --git a/ViewModel/CatalogHomeSections.swift b/ViewModel/CatalogHomeSections.swift index 19b20845d..aea3830d2 100644 --- a/ViewModel/CatalogHomeSections.swift +++ b/ViewModel/CatalogHomeSections.swift @@ -268,6 +268,33 @@ extension CatalogViewModel { // rather than in the view model's class body: they are read from many bodies, and a cached getter // reads its inputs on the cached path so Observation still registers the dependency. extension CatalogViewModel { + var marqueeGames: [OPNCatalogGameObject] { + _ = marqueePanels + if let cachedMarqueeGames { return cachedMarqueeGames } + var games: [OPNCatalogGameObject] = [] + var seen = Set() + for panel in marqueePanels { + for section in panel.sections { + for game in section.games { + let key = Self.identity(for: game) + guard !key.isEmpty, !seen.contains(key) else { continue } + seen.insert(key) + games.append(game) + } + } + } + cachedMarqueeGames = games + return games + } + + var heroRotationGames: [OPNCatalogGameObject] { + _ = marqueePanels + if let cachedHeroRotationGames { return cachedHeroRotationGames } + let games = Self.dedupedByTitleGrouping(marqueeGames.filter(Self.hasMarqueeHeroArtwork)) + cachedHeroRotationGames = games + return games + } + var visibleFilterGroups: [OPNCatalogFilterGroupObject] { _ = filterGroups if let cachedVisibleFilterGroups { return cachedVisibleFilterGroups } diff --git a/ViewModel/CatalogLaunchFlow.swift b/ViewModel/CatalogLaunchFlow.swift index 6b4c2c0f3..0f1d11ca5 100644 --- a/ViewModel/CatalogLaunchFlow.swift +++ b/ViewModel/CatalogLaunchFlow.swift @@ -103,7 +103,7 @@ extension CatalogViewModel { } func openGameShortcut(_ shortcut: GFNGameShortcut) { - configureCatalogService() + guard configureCatalogService() else { return } let title = shortcut.lookupTitle.isEmpty ? shortcut.displayName : shortcut.lookupTitle OPNLog.info(.shortcut, "CatalogViewModel resolving shortcut cmsId=\(shortcut.cmsId) shortName=\(shortcut.shortName) parentGameId=\(shortcut.parentGameId) title=\(title)") setActionMessage("Opening \(title.isEmpty ? "GeForce NOW shortcut" : title)...") @@ -182,6 +182,10 @@ extension CatalogViewModel { /// and before any window is replaced, or reports why it could not. Another account's game is not /// in the way: the limit is one game per account. private func beginOwnedSession() -> OPNGameSession? { + guard CatalogAccountRequestIdentity(account: account, session: session).isCurrent(account: account, session: session) else { + reportLaunchFailure("Sign in again before launching a game.") + return nil + } guard let accountID = account.resolveStableAccountID() else { reportLaunchFailure("This account has no saved identity to start a game with. Sign in again.") return nil @@ -192,7 +196,9 @@ extension CatalogViewModel { gameService: gameService, launchBridge: launchBridge, discordPresence: discordPresence, - streamProfile: streamProfile + streamProfile: streamProfile, + launchEntitlements: currentLaunchEntitlements, + providerStreamingBaseURL: gameService.providerStreamingBaseURL() ) else { reportLaunchFailure(refusalMessage(for: accountID)) return nil @@ -264,7 +270,7 @@ extension CatalogViewModel { guard launchFlowState == .idle, activeStreamConfiguration == nil, !isCheckingHomeSession else { return } isCheckingHomeSession = true let token = launchToken - let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl() + let streamingBaseUrl = OPNStreamPreferences.loadSelectedStreamingBaseUrl(providerStreamingBaseURL: gameService.providerStreamingBaseURL()) OPNActiveSessionService.fetchActiveSessions(accessToken: token, streamingBaseUrl: streamingBaseUrl, deviceId: resolveCloudmatchDeviceId()) { [weak self] ok, sessions, _ in guard let self else { return } self.isCheckingHomeSession = false diff --git a/ViewModel/CatalogMenuBarSurface.swift b/ViewModel/CatalogMenuBarSurface.swift index e08eea795..0a2998fab 100644 --- a/ViewModel/CatalogMenuBarSurface.swift +++ b/ViewModel/CatalogMenuBarSurface.swift @@ -222,7 +222,7 @@ extension CatalogViewModel: OPNMenuBarSessionSource { /// reopened from the menu bar starts fetching as it appears — so an unknown game is resolved by /// browsing for its title, the same way an unresolved shortcut is. func launchGame(_ game: OPNMenuBarGame) { - configureCatalogService() + guard configureCatalogService() else { return } let title = game.title.trimmingCharacters(in: .whitespacesAndNewlines) OPNLog.info(.launch, "Menu bar launching game appId=\(game.appId) title=\(title)") if let match = knownGame(matching: game, title: title) { diff --git a/ViewModel/CatalogOwnership.swift b/ViewModel/CatalogOwnership.swift index f9967c85b..568b8d43f 100644 --- a/ViewModel/CatalogOwnership.swift +++ b/ViewModel/CatalogOwnership.swift @@ -55,7 +55,7 @@ extension CatalogViewModel { } func toggleFavoriteSelectedGame() { - guard let selectedGame else { return } + guard let selectedGame, configureCatalogService() else { return } let appId = Self.favoriteAppId(for: selectedGame) let identity = Self.identity(for: selectedGame) guard !appId.isEmpty, !identity.isEmpty else { return } @@ -223,7 +223,8 @@ extension CatalogViewModel { } func autoMarkFreeToPlaySelectedVariantThenLaunch() { - guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty else { return } + guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty, + configureCatalogService() else { return } let gameIdentity = Self.identity(for: selectedGame) let variantId = variant.id let variantIndex = selectedVariantIndex @@ -247,7 +248,7 @@ extension CatalogViewModel { } func beginMarkSelectedVariantOwnedFlow() { - guard let selectedGame, selectedVariant(in: selectedGame) != nil else { return } + guard let selectedGame, selectedVariant(in: selectedGame) != nil, configureCatalogService() else { return } presentedModal = .storePicker(stage: .resyncing) ownershipFlowMessage = syncingOwnershipMessage(for: selectedGame) let stores = CatalogAccountParsing.uniqueNonEmpty(selectedGame.variants.map(\.appStore)) @@ -274,7 +275,8 @@ extension CatalogViewModel { } func confirmSelectedVariantOwned() { - guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty else { return } + guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty, + configureCatalogService() else { return } let gameIdentity = Self.identity(for: selectedGame) let variantId = variant.id let title = selectedGame.title.isEmpty ? "game" : selectedGame.title @@ -298,7 +300,8 @@ extension CatalogViewModel { } func removeSelectedVariantOwned() { - guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty else { return } + guard let selectedGame, let variant = selectedVariant(in: selectedGame), !variant.id.isEmpty, + configureCatalogService() else { return } let gameIdentity = Self.identity(for: selectedGame) let variantId = variant.id let title = selectedGame.title.isEmpty ? "game" : selectedGame.title @@ -316,7 +319,7 @@ extension CatalogViewModel { } func selectOwnedVariant(_ variant: OPNCatalogGameVariantObject) { - guard !variant.id.isEmpty else { return } + guard !variant.id.isEmpty, configureCatalogService() else { return } let variantId = variant.id gameService.selectOwnedVariant(variantId) { [weak self] success, error in guard let self else { return } @@ -336,7 +339,7 @@ extension CatalogViewModel { } func syncStoreAccount(_ store: String) { - guard !store.isEmpty else { return } + guard !store.isEmpty, configureCatalogService() else { return } setActionMessage("Syncing \(displayName(forStore: store)) account...") gameService.syncAccountProvider(store: store) { [weak self] success, error in guard let self else { return } @@ -358,7 +361,7 @@ extension CatalogViewModel { } func linkStoreAccount(_ store: String) { - guard !store.isEmpty else { return } + guard !store.isEmpty, configureCatalogService() else { return } setActionMessage("Opening \(displayName(forStore: store)) account linking...") gameService.startAccountLinking(store: store) { [weak self] success, error in guard let self else { return } diff --git a/ViewModel/CatalogPanelLoading.swift b/ViewModel/CatalogPanelLoading.swift index bc8ece87e..485b789ed 100644 --- a/ViewModel/CatalogPanelLoading.swift +++ b/ViewModel/CatalogPanelLoading.swift @@ -57,10 +57,10 @@ extension CatalogViewModel { } func loadPanels() { + guard configureCatalogService() else { return } isLoadingPanels = true isLoadingMarquee = true errorMessage = "" - configureCatalogService() // The launch prefetch may already own (or have finished) these queries; // adopt whatever it has instead of asking for the same panels twice. @@ -190,7 +190,7 @@ extension CatalogViewModel { } func loadLibrary() { - configureCatalogService() + guard configureCatalogService() else { return } isLoadingLibrary = true // See `loadFavorites()`: the launch prefetch fires this alongside the home panels, under // the splash screen, so it has often already arrived by the time this gate lets it through. @@ -221,7 +221,7 @@ extension CatalogViewModel { } func loadFavorites() { - configureCatalogService() + guard configureCatalogService() else { return } isLoadingFavorites = true // The launch prefetch fires this alongside the home panels, under the splash screen, so // by the time the panels rail unblocks this call it has often already arrived (or arrives @@ -240,12 +240,12 @@ extension CatalogViewModel { /// Reloads a list the user has just changed. The launch snapshot is dropped first, so this /// goes to the network instead of adopting the state that existed before the change. func reloadFavoritesAfterChange() { - CatalogLaunchPrefetch.shared.invalidate(.favorites) + CatalogLaunchPrefetch.shared.invalidate(.favorites, accountIdentifier: catalogAccountIdentifier) loadFavorites() } func reloadLibraryAfterChange() { - CatalogLaunchPrefetch.shared.invalidate(.library) + CatalogLaunchPrefetch.shared.invalidate(.library, accountIdentifier: catalogAccountIdentifier) loadLibrary() } @@ -300,9 +300,20 @@ extension CatalogViewModel { } func loadAccount() { + let identity = CatalogAccountRequestIdentity(account: account, session: session) + entitlementIdentity = nil + let requestID = UUID() + subscriptionRequestID = requestID + guard identity.isCurrent(account: account, session: session) else { + subscriptionStatus = .unavailable + dlssMembershipState = .signedOut + launchEntitlements = .none + return + } configureCatalogService() gameService.fetchUserAccount { [weak self] success, account, error in - guard let self else { return } + guard let self, self.subscriptionRequestID == requestID, + identity.isCurrent(account: self.account, session: self.session) else { return } if success { self.accountStores = CatalogAccountParsing.parseStoreAccounts(account) self.accountSubscriptions = CatalogAccountParsing.parseAccountSubscriptions(account) @@ -315,28 +326,25 @@ extension CatalogViewModel { guard !userId.isEmpty else { subscriptionStatus = .unavailable dlssMembershipState = .signedOut - OPNStreamPreferences.clearDlssEntitlements() + launchEntitlements = .none return } dlssMembershipState = .loading - // The stored copy is what every session request is filtered against, so it is cleared before - // the fetch rather than left holding the previous account's membership. - OPNStreamPreferences.clearDlssEntitlements() + launchEntitlements = .none gameService.fetchSubscriptionInfo(userId: userId) { [weak self] success, subscription, error in - // A switch or sign-out can complete before this answers, and the stored entitlements are - // global: a stale success would filter the next account's requests with this one's. - guard let self, self.session.isActive else { return } + guard let self, self.subscriptionRequestID == requestID, + identity.isCurrent(account: self.account, session: self.session) else { return } guard success else { - if self.refreshAuthIfNeeded(error: error) { self.subscriptionStatus = .unavailable } + self.subscriptionStatus = .unavailable + _ = self.refreshAuthIfNeeded(error: error) self.dlssMembershipState = .unavailable - OPNStreamPreferences.clearDlssEntitlements() + self.launchEntitlements = .none return } self.subscriptionStatus = CatalogSubscriptionStatus(subscription: subscription) - OPNStreamPreferences.saveEntitledAudioChannelCount(subscription.entitledAudioChannelCount) - OPNStreamPreferences.saveEntitledInGameSettingsPersistence(subscription.isInGameSettingsPersistenceEntitled) - // Re-evaluated on every account change. The saved preference is kept whatever this says. - OPNStreamPreferences.saveDlssEntitlements(subscription.dlssEntitlements) + self.launchEntitlements = OPNLaunchEntitlements(subscription: subscription) + self.entitlementIdentity = identity + self.updateActiveAccountEntitlementMirror(subscription) self.dlssMembershipState = .loaded(subscription.dlssEntitlements) let membershipTier = subscription.membershipTier.trimmingCharacters(in: .whitespacesAndNewlines) guard !membershipTier.isEmpty else { return } @@ -344,8 +352,21 @@ extension CatalogViewModel { } } + var currentLaunchEntitlements: OPNLaunchEntitlements { + guard let entitlementIdentity, + entitlementIdentity.isCurrent(account: account, session: session) else { return .none } + return launchEntitlements + } + + private func updateActiveAccountEntitlementMirror(_ subscription: OPNSubscriptionInfo) { + guard account.isActive, session.isActive else { return } + OPNStreamPreferences.saveEntitledAudioChannelCount(subscription.entitledAudioChannelCount) + OPNStreamPreferences.saveEntitledInGameSettingsPersistence(subscription.isInGameSettingsPersistenceEntitled) + OPNStreamPreferences.saveDlssEntitlements(subscription.dlssEntitlements) + } + func loadStores() { - configureCatalogService() + guard configureCatalogService() else { return } gameService.fetchStoreDefinitions { [weak self] success, definitions, _ in guard let self else { return } if success { self.storeDefinitions = definitions.map(CatalogAccountParsing.parseStoreDefinition) } diff --git a/ViewModel/CatalogPatchingAndState.swift b/ViewModel/CatalogPatchingAndState.swift index 6047cfa0c..de4cdcceb 100644 --- a/ViewModel/CatalogPatchingAndState.swift +++ b/ViewModel/CatalogPatchingAndState.swift @@ -304,9 +304,12 @@ extension CatalogViewModel { } } - func configureCatalogService() { + @discardableResult + func configureCatalogService() -> Bool { + guard CatalogAccountRequestIdentity(account: account, session: session).isCurrent(account: account, session: session) else { return false } let userId = session.userId.isEmpty ? account.userId : session.userId gameService.configureCatalogSession(accessToken: session.accessToken, idToken: session.idToken, userId: userId) + return true } func refreshAuthIfNeeded(error: String) -> Bool { diff --git a/ViewModel/CatalogViewModel+Settings.swift b/ViewModel/CatalogViewModel+Settings.swift index 3f8cff677..44dbaf406 100644 --- a/ViewModel/CatalogViewModel+Settings.swift +++ b/ViewModel/CatalogViewModel+Settings.swift @@ -634,7 +634,7 @@ extension CatalogViewModel { /// default output device and the membership's entitlement. var surroundModeSubtitle: String { let deviceChannels = streamCapabilities.audioOutputChannelCount - let entitled = OPNStreamPreferences.loadEntitledAudioChannelCount() + let entitled = currentLaunchEntitlements.audioChannelCount let negotiated = StreamSettingsResolver.audioChannelCount(surroundMode: streamProfile.surroundMode.value, deviceOutputChannels: deviceChannels, entitledChannels: entitled) let layout = negotiated >= 8 ? "7.1" : (negotiated >= 6 ? "5.1" : "stereo") var text = "Output device has \(deviceChannels) channel\(deviceChannels == 1 ? "" : "s"); next session streams \(layout)." diff --git a/ViewModel/CatalogViewModel.swift b/ViewModel/CatalogViewModel.swift index 0a8d4e2c5..1c927e461 100644 --- a/ViewModel/CatalogViewModel.swift +++ b/ViewModel/CatalogViewModel.swift @@ -327,6 +327,9 @@ final class CatalogViewModel { } } var subscriptionStatus = CatalogSubscriptionStatus.unavailable + var launchEntitlements = OPNLaunchEntitlements.none + var entitlementIdentity: CatalogAccountRequestIdentity? + var subscriptionRequestID = UUID() /// The saved global cloud DLSS override, mirrored here so the card renders from state. var dlssOverrides = DlssOverrides.useGameSettings /// What the account's membership includes, kept apart from the entitlements so a membership that @@ -438,11 +441,11 @@ final class CatalogViewModel { /// Whether that work has landed; `startupContentGate` holds the splash until it has. var isAccountScopedStateLoaded = false - init(account: LoginAccount, session: LoginSession, gameService: any CatalogGameServing = OPNGameService.shared, launchBridge: any GameLaunchBridging = OPNGameLaunchBridge.shared, imageCache: any CatalogImageServing = CatalogImageCache.shared, discordPresence: any DiscordPresenceServing = DiscordRichPresence.shared, systemIntegration: any SystemIntegrationServing = AppKitSystemIntegration(), sessionRegistry: OPNGameSessionRegistry = .shared, sessionResultStore: OPNGameSessionResultStore = .shared, dlssCompatibilityService: OPNDlssCompatibilityService = .shared, dlssAnnouncer: OPNDlssCompatibilityAnnouncer = OPNDlssCompatibilityAnnouncer(), onSwitchAccount: @escaping (LoginAccount) -> Void = { _ in }, onAddAccount: @escaping () -> Void = {}, onPresentAccountChooser: @escaping () -> Void = {}, onRefreshAuth: @escaping () async -> Bool) { + init(account: LoginAccount, session: LoginSession, gameService: any CatalogGameServing = OPNGameService(), launchBridge: (any GameLaunchBridging)? = nil, imageCache: any CatalogImageServing = CatalogImageCache.shared, discordPresence: any DiscordPresenceServing = DiscordRichPresence.shared, systemIntegration: any SystemIntegrationServing = AppKitSystemIntegration(), sessionRegistry: OPNGameSessionRegistry = .shared, sessionResultStore: OPNGameSessionResultStore = .shared, dlssCompatibilityService: OPNDlssCompatibilityService = .shared, dlssAnnouncer: OPNDlssCompatibilityAnnouncer = OPNDlssCompatibilityAnnouncer(), onSwitchAccount: @escaping (LoginAccount) -> Void = { _ in }, onAddAccount: @escaping () -> Void = {}, onPresentAccountChooser: @escaping () -> Void = {}, onRefreshAuth: @escaping () async -> Bool) { self.account = account self.session = session self.gameService = gameService - self.launchBridge = launchBridge + self.launchBridge = launchBridge ?? OPNGameLaunchBridge(gameService: (gameService as? any GameLaunchServiceConfiguring) ?? OPNGameService()) self.imageCache = imageCache self.discordPresence = discordPresence self.systemIntegration = systemIntegration @@ -527,33 +530,6 @@ final class CatalogViewModel { cachedVisibleFilterGroups = nil } - var marqueeGames: [OPNCatalogGameObject] { - _ = marqueePanels - if let cachedMarqueeGames { return cachedMarqueeGames } - var games: [OPNCatalogGameObject] = [] - var seen = Set() - for panel in marqueePanels { - for section in panel.sections { - for game in section.games { - let key = Self.identity(for: game) - guard !key.isEmpty, !seen.contains(key) else { continue } - seen.insert(key) - games.append(game) - } - } - } - cachedMarqueeGames = games - return games - } - - var heroRotationGames: [OPNCatalogGameObject] { - _ = marqueePanels - if let cachedHeroRotationGames { return cachedHeroRotationGames } - let games = Self.dedupedByTitleGrouping(marqueeGames.filter(Self.hasMarqueeHeroArtwork)) - cachedHeroRotationGames = games - return games - } - var isBrowseMode: Bool { !selectedFilterIds.isEmpty || searchQuery.contains { !$0.isWhitespace } } @@ -593,13 +569,22 @@ final class CatalogViewModel { func refresh() { // An explicit refresh must hit the network, not re-adopt the panels the // launch prefetch already handed over. - CatalogLaunchPrefetch.shared.invalidate() + CatalogLaunchPrefetch.shared.invalidate(accountIdentifier: catalogAccountIdentifier) loadCatalogDataAfterProviderConfiguration(forceCatalogRefresh: true) } func loadCatalogDataAfterProviderConfiguration(forceCatalogRefresh: Bool = false) { - configureCatalogService() - Task { await configureCatalogProviderEndpoint() } + let identity = CatalogAccountRequestIdentity(account: account, session: session) + guard identity.isCurrent(account: account, session: session) else { return } + Task { [weak self] in + guard let self else { return } + await configureCatalogProviderEndpoint() + guard identity.isCurrent(account: account, session: session) else { return } + loadConfiguredCatalogData(forceCatalogRefresh: forceCatalogRefresh) + } + } + + private func loadConfiguredCatalogData(forceCatalogRefresh: Bool) { // Claim the library and favorites rail slots up front rather than when their (deferred) // fetches start. Setting them later meant the rails appeared mid-load and pushed // everything under them down the page. This has to run before `loadPanels()`, whose @@ -640,7 +625,7 @@ final class CatalogViewModel { self.loadFavorites() // Every launch attach point has now run, so the prefetch's copy of the panels and the // game lists is a duplicate from here on. - CatalogLaunchPrefetch.shared.recordLaunchResultsAdopted() + CatalogLaunchPrefetch.shared.recordLaunchResultsAdopted(accountIdentifier: self.catalogAccountIdentifier) self.loadAccount() self.loadStores() } diff --git a/ViewModel/DetachedSession/DetachedSessionRefresher.swift b/ViewModel/DetachedSession/DetachedSessionRefresher.swift index 4f7edc2ce..17775bf0e 100644 --- a/ViewModel/DetachedSession/DetachedSessionRefresher.swift +++ b/ViewModel/DetachedSession/DetachedSessionRefresher.swift @@ -11,14 +11,28 @@ import SwiftData @MainActor struct DetachedSessionRefresher { let modelContext: ModelContext + private let refreshSession: @Sendable (JarvisSession) async throws -> JarvisSession + + init( + modelContext: ModelContext, + refreshSession: @escaping @Sendable (JarvisSession) async throws -> JarvisSession = { session in + let service = JarvisAuthService(transport: JarvisURLSessionTransport(), session: session) + return try await service.refreshSession(force: !session.isIdTokenValid) + } + ) { + self.modelContext = modelContext + self.refreshSession = refreshSession + } func refresh(account: LoginAccount, session storedSession: LoginSession) async -> Bool { - guard !storedSession.accessToken.isEmpty else { return false } + guard contains(account: account, session: storedSession) else { return false } + let ownership = RefreshOwnership(account: account, session: storedSession) + guard isCurrent(ownership, account: account, session: storedSession) else { return false } var jarvisSession = JarvisSession( accessToken: storedSession.accessToken, idToken: storedSession.idToken, refreshToken: storedSession.refreshToken, - userId: storedSession.userId, + userId: storedSession.userId.isEmpty ? account.userId : storedSession.userId, displayName: account.displayName, email: account.email, membershipTier: account.membershipTier, @@ -33,20 +47,54 @@ struct DetachedSessionRefresher { if jarvisSession.idTokenExpiry == 0 { jarvisSession.idTokenExpiry = JarvisSessionParser.idTokenExpiry(storedSession.idToken) } - let service = JarvisAuthService(transport: JarvisURLSessionTransport()) do { - await service.setSession(jarvisSession) - let refreshed = try await service.refreshSession(force: !jarvisSession.isIdTokenValid) - store(refreshed, in: storedSession) - OPNLog.info(.auth, "Detached session refreshed account=\(account.email)") + let refreshed = try await refreshSession(jarvisSession) + guard !Task.isCancelled, isCurrent(ownership, account: account, session: storedSession) else { return false } + guard !refreshed.accessToken.isEmpty, + jarvisSession.userId.isEmpty || refreshed.userId.isEmpty || refreshed.userId == jarvisSession.userId else { return false } + try store(refreshed, in: storedSession) + OPNLog.info(.auth, "Detached session refreshed account=\(ownership.email)") return true } catch { - OPNLog.warning(.auth, "Detached session refresh failed account=\(account.email) error=\(error.localizedDescription)") + OPNLog.warning(.auth, "Detached session refresh failed account=\(ownership.email) error=\(error.localizedDescription)") + guard !Task.isCancelled, isCurrent(ownership, account: account, session: storedSession) else { return false } return storedSession.canContinueOffline && !storedSession.isExpired } } - private func store(_ refreshed: JarvisSession, in storedSession: LoginSession) { + private func contains(account: LoginAccount, session: LoginSession) -> Bool { + guard !modelContext.deletedModelsArray.contains(where: { $0 === account || $0 === session }), + let accounts = try? modelContext.fetch(FetchDescriptor()), + let sessions = try? modelContext.fetch(FetchDescriptor()) else { return false } + return accounts.contains { $0 === account } && sessions.contains { $0 === session } + } + + private func isCurrent(_ ownership: RefreshOwnership, account: LoginAccount, session: LoginSession) -> Bool { + guard !ownership.accessToken.isEmpty, + !modelContext.deletedModelsArray.contains(where: { + $0.persistentModelID == ownership.accountIdentifier || $0.persistentModelID == ownership.sessionIdentifier + }) else { return false } + let email = ownership.email + let sessionID = ownership.sessionID + guard let currentAccount = try? modelContext.fetch(FetchDescriptor(predicate: #Predicate { $0.email == email })).first, + let currentSession = try? modelContext.fetch(FetchDescriptor(predicate: #Predicate { $0.id == sessionID })).first, + currentAccount === account, currentSession === session else { return false } + return currentAccount.persistentModelID == ownership.accountIdentifier + && currentSession.persistentModelID == ownership.sessionIdentifier + && currentAccount.stableAccountID == ownership.accountID + && currentAccount.userId == ownership.accountUserID + && currentAccount.providerIdpId == ownership.providerID + && currentSession.accountEmail == email + && currentSession.userId == ownership.userID + && currentSession.idpId == ownership.sessionProviderID + && currentSession.issuedAt == ownership.issuedAt + && currentSession.accessToken == ownership.accessToken + && currentSession.idToken == ownership.idToken + && currentSession.refreshToken == ownership.refreshToken + && currentSession.clientToken == ownership.clientToken + } + + private func store(_ refreshed: JarvisSession, in storedSession: LoginSession) throws { let now = Date() let expiry = Date(timeIntervalSince1970: TimeInterval(refreshed.expiresAt > 0 ? refreshed.expiresAt : Int64(now.addingTimeInterval(86_400).timeIntervalSince1970))) let clientExpiry = refreshed.clientTokenExpiry > 0 ? Date(timeIntervalSince1970: TimeInterval(refreshed.clientTokenExpiry) / 1000.0) : expiry @@ -66,10 +114,40 @@ struct DetachedSessionRefresher { isActive: storedSession.isActive, canContinueOffline: storedSession.canContinueOffline ) - do { - try modelContext.save() - } catch { - OPNLog.warning(.auth, "Detached session refresh could not be saved: \(error.localizedDescription)") + try modelContext.save() + } + + private struct RefreshOwnership { + let accountIdentifier: PersistentIdentifier + let sessionIdentifier: PersistentIdentifier + let accountID: String + let accountUserID: String + let providerID: String + let email: String + let sessionID: String + let userID: String + let sessionProviderID: String + let issuedAt: Date + let accessToken: String + let idToken: String + let refreshToken: String + let clientToken: String + + init(account: LoginAccount, session: LoginSession) { + accountIdentifier = account.persistentModelID + sessionIdentifier = session.persistentModelID + accountID = account.stableAccountID + accountUserID = account.userId + providerID = account.providerIdpId + email = account.email + sessionID = session.id + userID = session.userId + sessionProviderID = session.idpId + issuedAt = session.issuedAt + accessToken = session.accessToken + idToken = session.idToken + refreshToken = session.refreshToken + clientToken = session.clientToken } } } diff --git a/ViewModel/DetachedSession/DetachedSessionViewModel.swift b/ViewModel/DetachedSession/DetachedSessionViewModel.swift index 6157dc0f4..6d6c74c79 100644 --- a/ViewModel/DetachedSession/DetachedSessionViewModel.swift +++ b/ViewModel/DetachedSession/DetachedSessionViewModel.swift @@ -18,9 +18,13 @@ final class DetachedSessionViewModel { var takesPadsFromOtherStreams: Bool private let preset: OPNStreamControllerPreset + private let registry: OPNGameSessionRegistry + private var pendingAccountID: OPNAccountID? + private var pendingSelection: OPNStreamControllerSelection? - init(preset: OPNStreamControllerPreset = .shared, hasOtherStreams: Bool = OPNGameSessionRegistry.shared.hasSessions) { + init(preset: OPNStreamControllerPreset = .shared, hasOtherStreams: Bool = OPNGameSessionRegistry.shared.hasSessions, registry: OPNGameSessionRegistry = .shared) { self.preset = preset + self.registry = registry takesPadsFromOtherStreams = hasOtherStreams } @@ -40,22 +44,30 @@ final class DetachedSessionViewModel { /// moves on to the catalog the account will launch from. func confirm(row: DetachedAccountRow, accountID: OPNAccountID) { guard row.availability.isSelectable else { return } - preset.store( - DetachedSessionModel.selection(disabled: disabledControllers, takesPadsFromOtherStreams: takesPadsFromOtherStreams), - forAccount: accountID.rawValue - ) + discardPendingSelection() + let selection = DetachedSessionModel.selection(disabled: disabledControllers, takesPadsFromOtherStreams: takesPadsFromOtherStreams) + preset.store(selection, forAccount: accountID.rawValue) + pendingAccountID = accountID + pendingSelection = selection + selectedEmail = row.email phase = .catalog(email: row.email) } - func backToSetup(accountID: OPNAccountID?) { - discardPendingSelection(for: accountID) + func backToSetup() { + discardPendingSelection() + selectedEmail = nil phase = .setup } /// A choice made for a stream that never started must not reach a later launch by the same /// account. Once the account owns a game the stream has taken it, so there is nothing to drop. - func discardPendingSelection(for accountID: OPNAccountID?) { - guard let accountID, !OPNGameSessionRegistry.shared.isOwned(by: accountID) else { return } + func discardPendingSelection() { + let accountID = pendingAccountID + let selection = pendingSelection + pendingAccountID = nil + pendingSelection = nil + guard let accountID, let selection, !registry.isOwned(by: accountID), + preset.peek(forAccount: accountID.rawValue) == selection else { return } preset.store(OPNStreamControllerSelection(), forAccount: accountID.rawValue) } } From 3e67b97bf0892f6dd5ec1092d9f489c72cc8cc0b Mon Sep 17 00:00:00 2001 From: Anderson Shindy Oki Date: Mon, 26 Oct 2026 01:40:23 +0900 Subject: [PATCH 14/22] fix: harden couch co-op input and window transitions --- .../ControllerBindingOutputLedger.swift | 16 + OPN/Stream/NativeGamepadMonitor+Mapping.swift | 18 +- OPN/Stream/NativeGamepadMonitor.swift | 7 +- OPN/Stream/NativeStreamView.swift | 12 +- OPN/Stream/NativeStreamViewInput.swift | 11 +- OPN/Stream/NativeStreamViewPointer.swift | 23 +- OPN/Stream/OPNStreamBorderless.swift | 76 ++-- OPN/Stream/OPNStreamPictureInPicture.swift | 24 +- OPN/Stream/OPNStreamWindow.swift | 55 +++ OPN/Stream/StreamWindowGeometryGate.swift | 14 + .../ControllerBindingOutputLedgerTests.swift | 26 ++ Tests/Stream/CouchCoopBorderlessTests.swift | 9 +- .../Stream/CouchCoopNativeInputFixture.swift | 225 ++++++++++ Tests/Stream/CouchCoopNativeInputTests.swift | 303 ++++++++++++++ Tests/Stream/NativeNVSTInputParityTests.swift | 9 + ...ativeStreamViewCaptureLifecycleTests.swift | 7 + .../NativeStreamViewCursorFixtures.swift | 2 + .../NativeStreamViewCursorPolicyTests.swift | 45 +- Tests/Stream/NativeStreamViewMouseTests.swift | 29 +- .../StreamWindowLayoutMembershipTests.swift | 201 +++++++++ .../StreamWindowModeTransitionTests.swift | 388 ++++++++++++++++++ .../StreamWindowTransitionFixtures.swift | 168 ++++++++ View/Stream/OPNStreamWindowPresenter.swift | 70 +++- ViewModel/Stream/NativeNVSTHostInput.swift | 15 +- .../NativeNVSTHostViewModel+Borderless.swift | 3 +- .../NativeNVSTHostViewModel+Controls.swift | 84 ---- ...veNVSTHostViewModel+PictureInPicture.swift | 96 +++++ 27 files changed, 1769 insertions(+), 167 deletions(-) create mode 100644 Tests/Stream/CouchCoopNativeInputFixture.swift create mode 100644 Tests/Stream/CouchCoopNativeInputTests.swift create mode 100644 Tests/Stream/StreamWindowLayoutMembershipTests.swift create mode 100644 Tests/Stream/StreamWindowModeTransitionTests.swift create mode 100644 Tests/Stream/StreamWindowTransitionFixtures.swift create mode 100644 ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift diff --git a/Model/Stream/ControllerBindingOutputLedger.swift b/Model/Stream/ControllerBindingOutputLedger.swift index 77cedaf3e..33f22ab17 100644 --- a/Model/Stream/ControllerBindingOutputLedger.swift +++ b/Model/Stream/ControllerBindingOutputLedger.swift @@ -24,6 +24,22 @@ struct ControllerBindingOutputLedger { } } + mutating func releaseHeldOutputs(timestamp: MediaTimestamp) -> [UserInputEvent] { + var events: [UserInputEvent] = [] + for code in keys.keys.sorted() { + guard let owner = keys[code]?.keys.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } + events.append(.keyboard(KeyboardEvent(deviceID: owner, keyCode: code, scanCode: code, + modifiers: [], isPressed: false, timestamp: timestamp))) + } + for button in mouseButtons.keys.sorted(by: { $0.rawValue < $1.rawValue }) { + guard let owner = mouseButtons[button]?.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } + events.append(.mouse(.button(deviceID: owner, button: button, isPressed: false, timestamp: timestamp))) + } + keys.removeAll() + mouseButtons.removeAll() + return events + } + private mutating func keyboardEvents(for event: KeyboardEvent) -> [UserInputEvent] { let previous = keys let previousModifiers = modifiers diff --git a/OPN/Stream/NativeGamepadMonitor+Mapping.swift b/OPN/Stream/NativeGamepadMonitor+Mapping.swift index 49d26217a..6175620cb 100644 --- a/OPN/Stream/NativeGamepadMonitor+Mapping.swift +++ b/OPN/Stream/NativeGamepadMonitor+Mapping.swift @@ -1,6 +1,14 @@ import Foundation extension NativeGamepadMonitor { + func dispatchPendingInput() { + let (commands, events) = pollingQueue.sync { + (pollState.takePendingCommands(), pollState.takePendingEvents()) + } + for command in commands { onStreamCommand?(command) } + for event in events { emitInputEvent(event) } + } + /// While the Guide/Steam button is held, `includePointerMotion` is false — trackpad /// and stick pointer motion is suppressed here so `SteamControllerLocalCursorInjector` /// can drive the real macOS cursor from the right pad instead. Buttons/triggers/sticks @@ -34,7 +42,7 @@ extension NativeGamepadMonitor { } func emitInputEvent(_ event: UserInputEvent) { - if !mappingsEnabled || !pollingAllowed { + if !mappingsEnabled || !pollingAllowed || !isKeyboardMouseInputEnabled { switch event { case .keyboard(let key) where key.isPressed: return case .mouse(.button(_, _, true, _)), .mouse(.moved), .mouse(.wheel), .mouse(.horizontalWheel): return @@ -44,6 +52,14 @@ extension NativeGamepadMonitor { for output in bindingOutputLedger.events(for: event) { onInputEvent?(output) } } + func setKeyboardMouseInputEnabled(_ isEnabled: Bool) { + guard isKeyboardMouseInputEnabled != isEnabled else { return } + isKeyboardMouseInputEnabled = isEnabled + guard !isEnabled else { return } + let timestamp = MediaTimestamp(nanoseconds: DispatchTime.now().uptimeNanoseconds) + for event in bindingOutputLedger.releaseHeldOutputs(timestamp: timestamp) { onInputEvent?(event) } + } + func setMappingsEnabled(_ enabled: Bool) { guard mappingsEnabled != enabled else { return } mappingsEnabled = enabled diff --git a/OPN/Stream/NativeGamepadMonitor.swift b/OPN/Stream/NativeGamepadMonitor.swift index d7d201045..4d077b34b 100644 --- a/OPN/Stream/NativeGamepadMonitor.swift +++ b/OPN/Stream/NativeGamepadMonitor.swift @@ -78,6 +78,7 @@ public final class NativeGamepadMonitor { let pollingQueue = DispatchQueue(label: "com.opennow.gamepad-poll", qos: .userInteractive) var pollingAllowed = false var mappingsEnabled = false + var isKeyboardMouseInputEnabled = true private var mappingSubscription: AnyCancellable? private var orderSubscription: AnyCancellable? var bindingOutputLedger = ControllerBindingOutputLedger() @@ -365,11 +366,7 @@ public final class NativeGamepadMonitor { guard let self else { return } self.pollState.pendingEvents.append(contentsOf: events) Task { @MainActor [weak self] in - guard let self else { return } - let commands = self.pollingQueue.sync { self.pollState.takePendingCommands() } - let pending = self.pollingQueue.sync { self.pollState.takePendingEvents() } - for command in commands { self.onStreamCommand?(command) } - for event in pending { self.emitInputEvent(event) } + self?.dispatchPendingInput() } }, onBatteryChange: { [weak self] changes in guard let self else { return } diff --git a/OPN/Stream/NativeStreamView.swift b/OPN/Stream/NativeStreamView.swift index 92d01e58f..af9f9acf2 100644 --- a/OPN/Stream/NativeStreamView.swift +++ b/OPN/Stream/NativeStreamView.swift @@ -173,17 +173,12 @@ public final class NativeStreamView: NSView { } var cursorLocationProvider: () -> CGPoint = { NSEvent.mouseLocation } var cursorWarpHandler: ((CGPoint) -> Void)? + var inputFocusProvider: (() -> (isApplicationActive: Bool, isWindowKey: Bool))? // `internal(set)` rather than `private(set)`: the type's own extensions in the neighbouring // files write these, and the public contract is unchanged — nothing outside the module can. public internal(set) var isPointerLocked = false public internal(set) var isAbsoluteCursorConfined = false public internal(set) var isEmittingNeutralizingAbsolutePosition = false - public var isCursorCaptured: Bool { isPointerLocked || isAbsoluteCursorConfined } - /// A cursor rect and a pointer capture only mean anything for the key window of the active app. - /// Taking either while another app is frontmost steals a pointer this window cannot even draw in. - public var isFrontmostInputTarget: Bool { - NSApplication.shared.isActive && window?.isKeyWindow == true - } /// PiP is a small, floating viewing surface. While it is on screen the stream must not take the /// pointer - the cursor has to stay usable for the strip and for whatever else the user moved /// to - and the controller has to keep working even though the window is deliberately never @@ -431,8 +426,9 @@ public final class NativeStreamView: NSView { /// which is what keeps two accounts' concurrent games from sharing one game's overrides. init(frame frameRect: NSRect, mappingProvider: any ControllerMappingProviding = ControllerMappingStore.shared, - initiallyDisabledControllers: Set = []) { - gamepadMonitor = NativeGamepadMonitor(mappingProvider: mappingProvider) + initiallyDisabledControllers: Set = [], + controllerRegistry: ControllerMappingDevices = .shared) { + gamepadMonitor = NativeGamepadMonitor(mappingProvider: mappingProvider, registry: controllerRegistry) gamepadMonitor.disableForStream(initiallyDisabledControllers) super.init(frame: frameRect) wantsLayer = true diff --git a/OPN/Stream/NativeStreamViewInput.swift b/OPN/Stream/NativeStreamViewInput.swift index f71b814e1..900efb092 100644 --- a/OPN/Stream/NativeStreamViewInput.swift +++ b/OPN/Stream/NativeStreamViewInput.swift @@ -291,19 +291,18 @@ extension NativeStreamView { } func updateControllerMappingFocus() { + let focus = inputFocus + gamepadMonitor.setKeyboardMouseInputEnabled(isFrontmostInputTarget && remoteInputEnabled && !localOverlayCapturesInput) gamepadMonitor.setMappingsEnabled(ControllerMappingFocusPolicy.allowsMappings( - appIsActive: NSApplication.shared.isActive, windowIsKey: window?.isKeyWindow == true, + appIsActive: focus.isApplicationActive, windowIsKey: focus.isWindowKey, remoteInputEnabled: remoteInputEnabled, overlayCapturesInput: localOverlayCapturesInput, allowsBackgroundPads: allowsBackgroundGamepadInput )) } func handleFocusLoss() { - if allowsBackgroundGamepadInput { - updateControllerMappingFocus() - } else { - gamepadMonitor.setMappingsEnabled(false) - } + gamepadMonitor.setKeyboardMouseInputEnabled(false) + gamepadMonitor.setMappingsEnabled(allowsBackgroundGamepadInput && remoteInputEnabled && !localOverlayCapturesInput) releasePressedInputs(includingGamepads: !allowsBackgroundGamepadInput) setPointerLocked(false) applyLocalCursorPolicy() diff --git a/OPN/Stream/NativeStreamViewPointer.swift b/OPN/Stream/NativeStreamViewPointer.swift index ed04b8d82..e01e0ccbf 100644 --- a/OPN/Stream/NativeStreamViewPointer.swift +++ b/OPN/Stream/NativeStreamViewPointer.swift @@ -5,8 +5,25 @@ import AppKit import QuartzCore extension NativeStreamView { + public var isCursorCaptured: Bool { isPointerLocked || isAbsoluteCursorConfined } + + /// A cursor rect and a pointer capture only mean anything for the key window of the active app. + /// Taking either while another app is frontmost steals a pointer this window cannot even draw in. + public var isFrontmostInputTarget: Bool { + let focus = inputFocus + return focus.isApplicationActive && focus.isWindowKey + } + + var inputFocus: (isApplicationActive: Bool, isWindowKey: Bool) { + inputFocusProvider?() ?? (NSApplication.shared.isActive, window?.isKeyWindow == true) + } + + var canAcquirePointerCapture: Bool { + remoteInputEnabled && !localOverlayCapturesInput && isFrontmostInputTarget && !isPictureInPictureMode + } + func enablePointerLock() { - guard window != nil else { return } + guard canAcquirePointerCapture, window != nil else { return } disableAbsoluteCursorConfinement() guard !isAbsoluteCursorConfined else { return } guard cursorAssociationHandler(false) == .success else { @@ -89,9 +106,9 @@ extension NativeStreamView { } func captureAbsoluteCursorIfNeeded() { - guard remoteInputEnabled, directMouseInputEnabled, confinesCursorToWindowInAbsoluteMode, + guard canAcquirePointerCapture, directMouseInputEnabled, confinesCursorToWindowInAbsoluteMode, mouseInputMode == .absolute, !isPointerLocked, !isAbsoluteCursorConfined, - !isPictureInPictureMode, window != nil else { return } + window != nil else { return } guard Self.confinedCursorPoint(cursorLocationProvider(), to: window?.frame ?? .zero) != nil else { return } isAbsoluteCursorConfined = true window?.acceptsMouseMovedEvents = true diff --git a/OPN/Stream/OPNStreamBorderless.swift b/OPN/Stream/OPNStreamBorderless.swift index 4b53aefcd..c9a996bb1 100644 --- a/OPN/Stream/OPNStreamBorderless.swift +++ b/OPN/Stream/OPNStreamBorderless.swift @@ -36,6 +36,7 @@ enum OPNStreamBorderless { /// how tiles fill a screen together; without it only a window that fills a screen is raised. static func enter(_ window: OPNStreamWindow, frame: NSRect, coversMenuBar: Bool = false, screenFrames: [NSRect] = NSScreen.screens.map(\.frame)) { + guard !window.isPresentationEnding, !window.isPictureInPicture else { return } if window.borderlessRestoreState == nil { window.borderlessRestoreState = OPNStreamWindow.WindowedState( level: window.level, @@ -45,21 +46,46 @@ enum OPNStreamBorderless { ) OPNStreamWindowFrameStore.save(window.frame, isPictureInPicture: false, defaults: window.frameStoreDefaults) } - if window.isMiniaturized { window.deminiaturize(nil) } window.layoutState.isBorderless = true - StreamWindowGeometryGate.releaseAspectRatioLock(window) - OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: true) let level = coversMenuBar ? coveringLevel : level(for: frame, screenFrames: screenFrames) - window.level = level - for child in window.childWindows ?? [] { child.level = level } - apply(frame: frame, to: window) - postCoveringStateChange() + window.scheduleLayoutChange { window in + if window.isMiniaturized { window.deminiaturize(nil) } + StreamWindowGeometryGate.releaseAspectRatioLock(window) + OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: true) + window.level = level + for child in window.childWindows ?? [] { child.level = level } + window.setFrameFromLayout(frame) + postCoveringStateChange(for: window) + } } - static func exit(_ window: OPNStreamWindow) { - guard window.layoutState.isBorderless else { return } + static func exit(_ window: OPNStreamWindow, completion: (@MainActor () -> Void)? = nil) { + guard !window.isPresentationEnding else { return } + guard window.layoutState.isBorderless || window.borderlessRestoreState != nil || + StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { + window.cancelPendingLayout() + completion?() + return + } + window.scheduleLayoutChange { window in + restoreWindowedState(window, isFrameRestored: true) + guard !Task.isCancelled, !window.isPresentationEnding else { return } + completion?() + } + window.isBorderlessExitPending = window.layoutState.isBorderless + } + + private static func restoreWindowedState(_ window: OPNStreamWindow, isFrameRestored: Bool) { + guard window.layoutState.isBorderless || window.borderlessRestoreState != nil else { + window.clearLayoutFrame() + return + } + let wasBorderless = window.layoutState.isBorderless window.layoutState.isBorderless = false - defer { postCoveringStateChange() } + defer { + window.clearLayoutFrame() + if wasBorderless { postCoveringStateChange(for: window) } + } OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: false) guard let state = window.borderlessRestoreState else { window.level = .normal @@ -69,7 +95,8 @@ enum OPNStreamBorderless { window.borderlessRestoreState = nil window.level = state.level for child in window.childWindows ?? [] { child.level = state.level } - apply(frame: state.frame, to: window) + guard isFrameRestored else { return } + window.setFrameFromLayout(state.frame) if state.isMiniaturized { window.miniaturize(nil) } } @@ -77,39 +104,42 @@ enum OPNStreamBorderless { /// stay reachable above it re-reads its level then instead of on a timer. static let coveringStateDidChange = Notification.Name("OpenNOW.StreamBorderless.coveringStateDidChange") - static func postCoveringStateChange() { - NotificationCenter.default.post(name: coveringStateDidChange, object: nil) + static func postCoveringStateChange(for window: OPNStreamWindow? = nil) { + NotificationCenter.default.post(name: coveringStateDidChange, object: window) } /// The window's own toggle, from the HUD: the window is the reader's from here on, so the layout /// stops placing it whichever way it goes. static func toggleByReader(_ window: OPNStreamWindow) { - if window.layoutState.isBorderless { + if window.layoutState.isBorderless, !window.isBorderlessExitPending { releaseToReader(window) return } window.layoutState.isManuallyPlaced = true enter(window, frame: targetFrame(for: window)) + window.layoutMembershipDidChange?() } /// Leaves borderless and the layout together, for a window that is about to become something /// else (Picture in Picture) or that the reader no longer wants tiled. - static func releaseToReader(_ window: OPNStreamWindow) { + static func releaseToReader(_ window: OPNStreamWindow, completion: (@MainActor () -> Void)? = nil) { window.layoutState.isManuallyPlaced = true window.layoutState.isAutoTiled = false - exit(window) window.clearLayoutFrame() + exit(window, completion: completion) + window.layoutMembershipDidChange?() + } + + static func takePlacement(_ window: OPNStreamWindow) { + window.cancelPendingLayout() + window.layoutState.isManuallyPlaced = true + window.layoutState.isAutoTiled = false + restoreWindowedState(window, isFrameRestored: false) + window.layoutMembershipDidChange?() } /// The screen a borderless toggle fills when nothing else decided: the one the window is on. static func targetFrame(for window: NSWindow) -> NSRect { (window.screen ?? NSScreen.main)?.frame ?? window.frame } - - private static func apply(frame: NSRect, to window: OPNStreamWindow) { - StreamWindowGeometryGate.whenGeometryIsMutable { [weak window] in - guard let window else { return } - window.setFrameFromLayout(frame) - } - } } diff --git a/OPN/Stream/OPNStreamPictureInPicture.swift b/OPN/Stream/OPNStreamPictureInPicture.swift index 22f332f94..3942e3161 100644 --- a/OPN/Stream/OPNStreamPictureInPicture.swift +++ b/OPN/Stream/OPNStreamPictureInPicture.swift @@ -66,8 +66,21 @@ enum OPNStreamPictureInPicture { OPNStreamStageGeometry.centered(size: contentSize, within: visibleFrame) } - static func enter(_ window: OPNStreamWindow, aspectRatio: CGFloat) { - guard !window.isPictureInPicture else { return } + static func enter(_ window: OPNStreamWindow, aspectRatio: CGFloat, + prepareForEntry: (@MainActor () -> Bool)? = nil, + completion: (@MainActor () -> Void)? = nil) { + guard !window.isPictureInPicture, !window.isPresentationEnding, !window.styleMask.contains(.fullScreen) else { return } + OPNStreamBorderless.releaseToReader(window) { [weak window] in + guard let window, !window.isPresentationEnding, prepareForEntry?() != false else { return } + enterRestoredWindow(window, aspectRatio: aspectRatio) + completion?() + } + if !window.isPictureInPicture, window.hasPendingLayoutChange { + window.pictureInPictureRequestID = UUID() + } + } + + private static func enterRestoredWindow(_ window: OPNStreamWindow, aspectRatio: CGFloat) { window.windowedState = OPNStreamWindow.WindowedState( level: window.level, collectionBehavior: window.collectionBehavior, @@ -102,7 +115,12 @@ enum OPNStreamPictureInPicture { } static func exit(_ window: OPNStreamWindow) { + if window.isPictureInPicturePending { + OPNStreamBorderless.exit(window) + window.layoutMembershipDidChange?() + } guard window.isPictureInPicture else { return } + window.cancelPendingLayout() OPNStreamWindowFrameStore.save(window.frame, isPictureInPicture: true, defaults: window.frameStoreDefaults) window.isPictureInPicture = false OPNStreamWindowChrome.apply(to: window, isPictureInPicture: false) @@ -110,6 +128,7 @@ enum OPNStreamPictureInPicture { window.level = .normal window.collectionBehavior = [.fullScreenPrimary] setAllSpacesMembership(onChildWindowsOf: window, isMember: false) + window.layoutMembershipDidChange?() return } window.windowedState = nil @@ -118,6 +137,7 @@ enum OPNStreamPictureInPicture { setAllSpacesMembership(onChildWindowsOf: window, isMember: false) window.setFrame(state.frame, display: true) if state.isMiniaturized { window.miniaturize(nil) } + window.layoutMembershipDidChange?() } /// The carrier child windows are not children in the view hierarchy, so nothing else re-derives diff --git a/OPN/Stream/OPNStreamWindow.swift b/OPN/Stream/OPNStreamWindow.swift index 6e7bcd2bf..518938fb5 100644 --- a/OPN/Stream/OPNStreamWindow.swift +++ b/OPN/Stream/OPNStreamWindow.swift @@ -50,7 +50,62 @@ final class OPNStreamWindow: NSWindow { /// taking the window back, which a frame change alone cannot tell from the layout's own. private(set) var layoutFrame: NSRect? + private(set) var isApplyingLayoutChange = false + private(set) var isPresentationEnding = false + private var layoutTask: Task? + private var layoutRequestID = UUID() + var isBorderlessExitPending = false + var pictureInPictureRequestID: UUID? + var isPictureInPicturePending: Bool { pictureInPictureRequestID != nil } + var hasPendingLayoutChange: Bool { layoutTask != nil } + var layoutMembershipDidChange: (@MainActor () -> Void)? + + func cancelPendingLayout() { + layoutRequestID = UUID() + layoutTask?.cancel() + layoutTask = nil + pictureInPictureRequestID = nil + isBorderlessExitPending = false + } + + func scheduleLayoutChange(_ body: @escaping @MainActor (OPNStreamWindow) -> Void) { + cancelPendingLayout() + guard !isPresentationEnding else { return } + let requestID = layoutRequestID + layoutTask = Task { @MainActor [weak self] in + guard let self else { return } + defer { + if self.layoutRequestID == requestID { + self.layoutTask = nil + self.pictureInPictureRequestID = nil + self.isBorderlessExitPending = false + } + } + guard await StreamWindowGeometryGate.waitUntilGeometryIsMutable(for: self), + self.layoutRequestID == requestID, !self.isPresentationEnding, + !self.styleMask.contains(.fullScreen) else { return } + self.isApplyingLayoutChange = true + defer { self.isApplyingLayoutChange = false } + body(self) + } + } + + func endPresentation() { + isPresentationEnding = true + cancelPendingLayout() + layoutMembershipDidChange = nil + } + + override func close() { + endPresentation() + super.close() + OPNStreamBorderless.postCoveringStateChange(for: self) + } + func setFrameFromLayout(_ frame: NSRect) { + let wasApplyingLayoutChange = isApplyingLayoutChange + isApplyingLayoutChange = true + defer { isApplyingLayoutChange = wasApplyingLayoutChange } layoutFrame = frame setFrame(frame, display: true) } diff --git a/OPN/Stream/StreamWindowGeometryGate.swift b/OPN/Stream/StreamWindowGeometryGate.swift index d9afc942d..4114c622d 100644 --- a/OPN/Stream/StreamWindowGeometryGate.swift +++ b/OPN/Stream/StreamWindowGeometryGate.swift @@ -66,6 +66,20 @@ enum StreamWindowGeometryGate { window.resizeIncrements = onePointIncrements } + @MainActor + static func waitUntilGeometryIsMutable(for window: NSWindow) async -> Bool { + for _ in 0..<200 { + guard !Task.isCancelled else { return false } + guard shouldDeferGeometryMutation(for: window) else { return true } + do { + try await Task.sleep(for: .milliseconds(50)) + } catch { + return false + } + } + return !Task.isCancelled && !shouldDeferGeometryMutation(for: window) + } + /// Runs `body` once the nested loop ends, and drops it if that never happens. /// /// A zoom animation has no "did end" notification to observe, so this polls at roughly its own diff --git a/Tests/Stream/ControllerBindingOutputLedgerTests.swift b/Tests/Stream/ControllerBindingOutputLedgerTests.swift index 211695446..b831ee711 100644 --- a/Tests/Stream/ControllerBindingOutputLedgerTests.swift +++ b/Tests/Stream/ControllerBindingOutputLedgerTests.swift @@ -69,4 +69,30 @@ import Testing let release = keyboard(ledger.events(for: key("plain", code: 0, down: false))) #expect(release.map(\.modifiers) == [.shift]) } + + @Test func focusReleaseClearsSharedDesktopOwnersWithoutTouchingGamepads() { + var ledger = ControllerBindingOutputLedger() + _ = ledger.events(for: key("second", code: 49, down: true, modifiers: .shift)) + _ = ledger.events(for: key("first", code: 49, down: true, modifiers: .control)) + _ = ledger.events(for: key("second", code: 0, down: true)) + let mousePress = UserInputEvent.mouse(.button(deviceID: "first", button: .left, isPressed: true, + timestamp: MediaTimestamp(nanoseconds: 1))) + _ = ledger.events(for: mousePress) + _ = ledger.events(for: .mouse(.button(deviceID: "second", button: .left, isPressed: true, + timestamp: MediaTimestamp(nanoseconds: 1)))) + let stamp = MediaTimestamp(nanoseconds: 2) + + let releases = ledger.releaseHeldOutputs(timestamp: stamp) + + #expect(keyboard(releases).map(\.keyCode) == [0, 49]) + #expect(keyboard(releases).allSatisfy { !$0.isPressed && $0.modifiers.isEmpty }) + #expect(releases.last == .mouse(.button(deviceID: "first", button: .left, isPressed: false, timestamp: stamp))) + #expect(releases.count == 3) + #expect(ledger.releaseHeldOutputs(timestamp: stamp).isEmpty) + #expect(ledger.events(for: key("first", down: false)).isEmpty) + #expect(ledger.events(for: key("second", down: false)).isEmpty) + let pad = UserInputEvent.gamepad(GamepadState(deviceID: "first", playerIndex: 0, buttons: .south, timestamp: stamp)) + #expect(ledger.events(for: pad) == [pad]) + #expect(ledger.events(for: mousePress) == [mousePress]) + } } diff --git a/Tests/Stream/CouchCoopBorderlessTests.swift b/Tests/Stream/CouchCoopBorderlessTests.swift index 9c20718f4..0484da93b 100644 --- a/Tests/Stream/CouchCoopBorderlessTests.swift +++ b/Tests/Stream/CouchCoopBorderlessTests.swift @@ -114,21 +114,28 @@ import Testing let half = NSRect(x: 0, y: 0, width: 960, height: 1080) OPNStreamBorderless.enter(window, frame: half, coversMenuBar: true, screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) #expect(window.level == OPNStreamBorderless.coveringLevel) OPNStreamBorderless.enter(window, frame: half, coversMenuBar: false, screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) #expect(window.level == .normal) OPNStreamBorderless.exit(window) + try await Task.sleep(for: .milliseconds(200)) } @Test func enteringAndLeavingBorderlessAnnouncesTheCoveringStateChange() async throws { let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) var posts = 0 - let token = NotificationCenter.default.addObserver(forName: OPNStreamBorderless.coveringStateDidChange, object: nil, queue: nil) { _ in posts += 1 } + let token = NotificationCenter.default.addObserver(forName: OPNStreamBorderless.coveringStateDidChange, object: window, queue: .main) { _ in + MainActor.assumeIsolated { posts += 1 } + } defer { NotificationCenter.default.removeObserver(token) } OPNStreamBorderless.enter(window, frame: NSRect(x: 0, y: 0, width: 640, height: 360), screenFrames: screens) + try await Task.sleep(for: .milliseconds(200)) OPNStreamBorderless.exit(window) + try await Task.sleep(for: .milliseconds(200)) #expect(posts == 2) } diff --git a/Tests/Stream/CouchCoopNativeInputFixture.swift b/Tests/Stream/CouchCoopNativeInputFixture.swift new file mode 100644 index 000000000..5a676672c --- /dev/null +++ b/Tests/Stream/CouchCoopNativeInputFixture.swift @@ -0,0 +1,225 @@ +import AppKit +import GameController +import os +import Testing +@testable import OpenNOW + +@MainActor +final class CouchCoopInputFocus { + var isApplicationActive = true + var isWindowKey = true +} + +@MainActor +final class CouchCoopFocusReleaseRecorder { + private(set) var sequence: [String] = [] + private(set) var releaseCaptureStates: [Bool] = [] + + init(view: NativeStreamView) { + let inputHandler = view.onInputEvent + view.onInputEvent = { [weak self, weak view] event in + switch event { + case .keyboard(let key) where !key.isPressed: self?.sequence.append("key-release") + case .mouse(.button(_, _, false, _)): self?.sequence.append("mouse-release") + default: break + } + if NativeNVSTInputDispatcher.isNeutralizing(event) { self?.releaseCaptureStates.append(view?.isPointerLocked == true) } + inputHandler?(event) + } + let pointerHandler = view.onPointerLockChanged + view.onPointerLockChanged = { [weak self] isLocked in + if !isLocked { self?.sequence.append("unlock") } + pointerHandler?(isLocked) + } + } +} + +actor CouchCoopNativeInputRecorder { + private var inputs: [UserInputEvent] = [] + private var barrierSnapshots: [String: [UserInputEvent]] = [:] + private var barrierWaiters: [String: CheckedContinuation<[UserInputEvent], Error>] = [:] + + func append(_ input: NativeNVSTInput) { + guard case .event(let event) = input else { return } + if case .text("input-test-barrier", let marker, _) = event { + guard let waiter = barrierWaiters.removeValue(forKey: marker) else { + barrierSnapshots[marker] = inputs + return + } + waiter.resume(returning: inputs) + return + } + inputs.append(event) + } + + func snapshot(at marker: String) async throws -> [UserInputEvent] { + try Task.checkCancellation() + if let snapshot = barrierSnapshots.removeValue(forKey: marker) { return snapshot } + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { continuation in + guard !Task.isCancelled else { + continuation.resume(throwing: CancellationError()) + return + } + barrierWaiters[marker] = continuation + } + } onCancel: { + Task { await self.cancelBarrier(marker) } + } + } + + private func cancelBarrier(_ marker: String) { + barrierSnapshots.removeValue(forKey: marker) + barrierWaiters.removeValue(forKey: marker)?.resume(throwing: CancellationError()) + } +} + +@MainActor +final class CouchCoopNativeInputFixture { + let focus: CouchCoopInputFocus + let window: NSWindow + let view: NativeStreamView + let model: NativeNVSTHostViewModel + let store: ControllerMappingStore + let dispatcher: NativeNVSTInputDispatcher + private let recorder: CouchCoopNativeInputRecorder + private let nativePollState = NativeGamepadPollState() + private let defaults: UserDefaults + private let defaultsDomain: String + private(set) var cursorAssociations: [Bool] = [] + + init() throws { + let defaultsDomain = "CouchCoopNativeInputTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsDomain)) + let store = ControllerMappingStore(defaults: defaults) + for family in [ControllerFamily.steam, .generic] { + var profile = store.createProfile(named: "Input ownership", family: family) + profile.bindings[.faceA] = .mouseButton(.left) + profile.bindings[.faceB] = .keyboardKey(keyCode: 49, modifiers: .shift) + profile.bindings[.faceX] = .gamepadChord(ControllerButtonChord(buttons: .north)) + profile.bindings[.faceY] = .streamCommand(.togglePointerCapture) + store.updateProfile(profile) + } + let window = NSWindow(contentRect: NSRect(x: 100, y: 100, width: 1280, height: 720), + styleMask: .borderless, backing: .buffered, defer: false) + let registry = ControllerMappingDevices(discovery: .init(nativeControllers: { [] }, steamDeviceIDs: { [] })) + let view = NativeStreamView(frame: NSRect(x: 0, y: 0, width: 1280, height: 720), + mappingProvider: store, controllerRegistry: registry) + let model = NativeNVSTHostViewModel( + configuration: StreamLaunchConfiguration(title: "Input ownership", applicationID: "input-test", + accessToken: "unused", accountLinked: true, selectedStore: "steam"), + sessionProvider: StubNativeNVSTSessionProvider(), preventDisplaySleep: false, onProgress: nil, + onEnd: { _, _, _ in } + ) + let recorder = CouchCoopNativeInputRecorder() + self.focus = CouchCoopInputFocus() + self.window = window + self.view = view + self.model = model + self.store = store + self.recorder = recorder + self.defaults = defaults + self.defaultsDomain = defaultsDomain + self.dispatcher = NativeNVSTInputDispatcher { input in await recorder.append(input) } + configureInputOwnership() + } + + private func configureInputOwnership() { + window.isReleasedWhenClosed = false + view.inputFocusProvider = { [focus] in (focus.isApplicationActive, focus.isWindowKey) } + view.hidesCursorWhilePointerLocked = false + view.cursorLocationProvider = { CGPoint(x: 740, y: 460) } + view.cursorWarpHandler = { _ in } + window.contentView = view + view.gamepadMonitor.stop() + view.gamepadMonitor.pollingAllowed = true + view.allowsBackgroundGamepadInput = true + view.updateControllerMappingFocus() + model.nativeView = view + model.path = NativeNVSTStreamingPath(sessionProvider: StubNativeNVSTSessionProvider(), transport: RecordingNativeTransport()) + model.isConnected = true + model.inputDispatcher = dispatcher + model.configureInput(for: view) + view.onPointerLockChanged = { [weak model] in model?.pointerLocked = $0 } + view.cursorAssociationHandler = { [weak self] isAssociated in + self?.cursorAssociations.append(isAssociated) + return .success + } + } + + func configureNativeControllers(_ controllers: [(deviceID: InputDeviceID, controller: GCController, profile: ControllerMappingProfile?)]) { + let monitor = view.gamepadMonitor + let state = nativePollState + monitor.pollingQueue.sync { + state.cachedControllers = controllers.map { $0.controller } + state.controllerSlots = Dictionary(uniqueKeysWithValues: controllers.enumerated().map { + (ObjectIdentifier($0.element.controller), $0.offset) + }) + let configurations = Dictionary(uniqueKeysWithValues: controllers.enumerated().map { + (ObjectIdentifier($0.element.controller), NativeControllerMappingConfiguration( + deviceID: $0.element.deviceID, playerIndex: $0.offset, profile: $0.element.profile + )) + }) + _ = state.configureMappings(configurations) + state.hidSnapshots = { [:] } + } + } + + func pollNativeControllers() { + let monitor = view.gamepadMonitor + let state = nativePollState + let captured = OSAllocatedUnfairLock(initialState: [UserInputEvent]()) + let commands = monitor.pollingQueue.sync { + state.pollAndEmit(onEvents: { events in captured.withLock { $0 += events } }, onBatteryChange: { _ in }) + monitor.pollState.pendingEvents.append(contentsOf: captured.withLock { $0 }) + return state.takePendingCommands() + } + for command in commands { monitor.onStreamCommand?(command) } + monitor.dispatchPendingInput() + } + + var routingDiagnostics: String { + let monitor = view.gamepadMonitor + let state = nativePollState + let slots = monitor.pollingQueue.sync { + "scriptedSlots=\(state.controllerSlots.values.sorted()) liveSlots=\(monitor.pollState.controllerSlots.values.sorted())" + } + let active = view.activeGamepadStates.keys.sorted().compactMap { playerIndex -> String? in + guard let pad = view.activeGamepadStates[playerIndex] else { return nil } + return "\(playerIndex):\(pad.deviceID.rawValue):buttons=\(pad.buttons.rawValue):leftX=\(pad.leftStickX)" + } + return "\(slots) active=\(active) mappings=\(monitor.mappingsEnabled) polling=\(monitor.pollingAllowed)" + } + + func sendSteam(_ snapshot: ControllerInputSnapshot) { + view.gamepadMonitor.processSteamSnapshot(deviceID: "steam-input-test", playerIndex: 2, snapshot: snapshot) + } + + func recordedInputs() async throws -> [UserInputEvent] { + let marker = UUID().uuidString + dispatcher.enqueue(.text(deviceID: "input-test-barrier", value: marker, timestamp: MediaTimestamp(nanoseconds: 0))) + do { + return try await recorder.snapshot(at: marker) + } catch { + Issue.record("Input barrier cancelled: pending=\(dispatcher.pendingInputCount) \(routingDiagnostics)") + throw error + } + } + + func close() { + view.remoteInputEnabled = false + view.gamepadMonitor.stop() + view.setPointerLocked(false) + view.onInputEvent = nil + view.onCommand = nil + model.cancelNativeShortcutTasks() + model.isConnected = false + model.nativeView = nil + model.path = nil + model.inputDispatcher = nil + dispatcher.cancel() + window.contentView = nil + window.close() + defaults.removePersistentDomain(forName: defaultsDomain) + } +} diff --git a/Tests/Stream/CouchCoopNativeInputTests.swift b/Tests/Stream/CouchCoopNativeInputTests.swift new file mode 100644 index 000000000..c01726311 --- /dev/null +++ b/Tests/Stream/CouchCoopNativeInputTests.swift @@ -0,0 +1,303 @@ +import AppKit +import GameController +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized, .streamLifecycleExclusive, .timeLimit(.minutes(1))) +struct CouchCoopNativeInputTests { + @Test func focusLossReleasesSharedMappedDesktopHoldsBeforeUnlockWithoutResettingPads() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + let mapped = GCController.withExtendedGamepad() + let physical = GCController.withExtendedGamepad() + let mappedPad = try #require(mapped.extendedGamepad) + let physicalPad = try #require(physical.extendedGamepad) + fixture.configureNativeControllers([ + ("mapped-native", mapped, fixture.store.profile(for: .generic)), + ("physical-native", physical, nil), + ]) + fixture.view.setPointerLocked(true) + mappedPad.buttonA.setValue(1) + mappedPad.buttonB.setValue(1) + mappedPad.buttonX.setValue(1) + physicalPad.buttonA.setValue(1) + physicalPad.leftThumbstick.xAxis.setValue(0.5) + fixture.pollNativeControllers() + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east, .west])) + fixture.view.gamepadMonitor.refreshInputState() + let initial = try await fixture.recordedInputs() + #expect(desktopInputs(initial).count == 2) + #expect(gamepads(initial).count == 3, "\(fixture.routingDiagnostics); received=\(gamepads(initial))") + #expect(fixture.view.pressedMouseButtons.isEmpty) + #expect(fixture.view.pressedKeyboardEvents.isEmpty) + + let releases = CouchCoopFocusReleaseRecorder(view: fixture.view) + fixture.focus.isWindowKey = false + fixture.view.handleFocusLoss() + fixture.view.handleFocusLoss() + + let released = try await fixture.recordedInputs() + #expect(releases.sequence == ["key-release", "mouse-release", "unlock"]) + #expect(releases.releaseCaptureStates == [true, true]) + #expect(desktopInputs(Array(released.dropFirst(initial.count))).count == 2) + #expect(gamepads(released) == gamepads(initial)) + #expect(fixture.view.activeGamepadStates[0]?.buttons == .north) + #expect(fixture.view.activeGamepadStates[1]?.leftStickX == 0.5) + #expect(fixture.view.activeGamepadStates[2]?.buttons == .north) + #expect(fixture.view.gamepadMonitor.mappingsEnabled) + + fixture.view.gamepadMonitor.refreshInputState() + physicalPad.leftThumbstick.xAxis.setValue(0.75) + fixture.pollNativeControllers() + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east, .west], leftTrigger: 0.75)) + let background = Array(try await fixture.recordedInputs().dropFirst(released.count)) + #expect(desktopInputs(background).isEmpty) + #expect(gamepads(background).contains { $0.deviceID == "physical-native" && $0.leftStickX == 0.75 }, "\(fixture.routingDiagnostics)") + #expect(gamepads(background).contains { $0.deviceID == "mapped-native" && $0.buttons == .north }, "\(fixture.routingDiagnostics)") + #expect(gamepads(background).contains { $0.deviceID == "steam-input-test" && $0.buttons == .north }) + #expect(gamepads(background).allSatisfy { !$0.buttons.isEmpty }) + } + + @Test func returningFocusDoesNotReplayDesktopPressesHeldInTheBackground() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + let held = ControllerInputSnapshot(buttons: [.south, .east, .west]) + fixture.view.setPointerLocked(true) + fixture.sendSteam(held) + fixture.focus.isWindowKey = false + fixture.view.handleFocusLoss() + fixture.sendSteam(ControllerInputSnapshot()) + fixture.sendSteam(held) + let background = try await fixture.recordedInputs() + #expect(desktopInputs(background).count == 4) + + fixture.focus.isWindowKey = true + fixture.view.locksPointerWhenRelativeModeSelected = true + fixture.view.handleFocusGain() + fixture.sendSteam(held) + let restored = try await fixture.recordedInputs() + #expect(desktopInputs(restored) == desktopInputs(background)) + #expect(fixture.view.isPointerLocked) + fixture.sendSteam(ControllerInputSnapshot()) + fixture.sendSteam(held) + let pressedAgain = try await fixture.recordedInputs() + #expect(desktopInputs(Array(pressedAgain.dropFirst(restored.count))).count == 2) + } + + @Test func aMappedRelativeReleaseReachesTheHostAfterUnlockWithoutRecapturing() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.view.setPointerLocked(true) + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east])) + let pressed = try await fixture.recordedInputs() + #expect(desktopInputs(pressed).count == 2) + fixture.view.setPointerLocked(false) + fixture.focus.isWindowKey = false + fixture.sendSteam(ControllerInputSnapshot()) + let released = Array(try await fixture.recordedInputs().dropFirst(pressed.count)) + + #expect(desktopInputs(released).count == 2) + #expect(desktopInputs(released).allSatisfy(NativeNVSTInputDispatcher.isNeutralizing)) + #expect(fixture.view.mouseInputMode == .relative) + #expect(!fixture.view.isPointerLocked) + #expect(fixture.cursorAssociations == [false, true]) + } + + @Test(arguments: BlockingOverlay.allCases) + func overlayTransitionsReleaseMappedHoldsButRejectNewGameInput(_ overlay: BlockingOverlay) async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.view.setPointerLocked(true) + fixture.sendSteam(ControllerInputSnapshot(buttons: [.south, .east, .west])) + let initial = try await fixture.recordedInputs() + switch overlay { + case .hud: + fixture.model.unifiedHUDVisible = true + fixture.view.remoteInputEnabled = false + case .quitMenu: + fixture.model.streamControlsVisible = true + fixture.view.remoteInputEnabled = false + case .keyboard: + fixture.view.localOverlayCapturesInput = true + fixture.view.setPointerLocked(false) + } + let transition = Array(try await fixture.recordedInputs().dropFirst(initial.count)) + #expect(desktopInputs(transition).count == 2) + #expect(desktopInputs(transition).allSatisfy(NativeNVSTInputDispatcher.isNeutralizing)) + #expect(!fixture.view.gamepadMonitor.mappingsEnabled) + let stamp = MediaTimestamp(nanoseconds: 1) + fixture.model.routeInputEvent(.mouse(.button(deviceID: "blocked", button: .right, isPressed: true, timestamp: stamp)), view: fixture.view) + fixture.model.routeInputEvent(.keyboard(KeyboardEvent(deviceID: "blocked", keyCode: 0, scanCode: 0, isPressed: true, timestamp: stamp)), view: fixture.view) + fixture.model.routeInputEvent(.gamepad(GamepadState(deviceID: "blocked", playerIndex: 3, buttons: .south, timestamp: stamp)), view: fixture.view) + let blocked = try await fixture.recordedInputs() + #expect(blocked.count == initial.count + transition.count) + #expect(!fixture.view.isPointerLocked) + #expect(fixture.cursorAssociations == [false, true]) + } + + @Test func backgroundSteamAndNativeCaptureCommandsNeverTakeTheOtherStreamsPointer() async throws { + let background = try CouchCoopNativeInputFixture() + let foreground = try CouchCoopNativeInputFixture() + defer { + background.close() + foreground.close() + } + background.focus.isWindowKey = false + background.view.handleFocusLoss() + foreground.view.setPointerLocked(true) + let native = GCController.withExtendedGamepad() + let pad = try #require(native.extendedGamepad) + background.configureNativeControllers([("native-command", native, background.store.profile(for: .generic))]) + background.sendSteam(ControllerInputSnapshot(buttons: [.north, .west])) + pad.buttonY.setValue(1) + pad.buttonX.setValue(1) + background.pollNativeControllers() + background.model.handleNativeCommand(.togglePointerCapture) + background.view.setRemoteCursorVisible(false) + background.view.cursorPolicy = .stream + + #expect(!background.view.isCursorCaptured) + #expect(!background.view.manualPointerCaptureOverride) + #expect(background.cursorAssociations.isEmpty) + #expect(foreground.view.isPointerLocked) + #expect(foreground.cursorAssociations == [false]) + let admitted = gamepads(try await background.recordedInputs()) + #expect(admitted.contains { $0.deviceID == "steam-input-test" && $0.buttons == .north }) + #expect(admitted.contains { $0.deviceID == "native-command" && $0.buttons == .north }) + + background.sendSteam(ControllerInputSnapshot(buttons: .quickAccess)) + #expect(background.model.unifiedHUDVisible) + #expect(!background.view.remoteInputEnabled) + #expect(foreground.view.isPointerLocked) + #expect(background.cursorAssociations.isEmpty) + let afterQuickAccess = gamepads(try await background.recordedInputs()) + #expect(afterQuickAccess.allSatisfy { !$0.buttons.contains(.quickAccess) }) + } + + @Test(arguments: CaptureBlocker.allCases) + func everyCaptureEntryPointHonorsOwnershipWhileUnlockRemainsPossible(_ blocker: CaptureBlocker) throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.view.setPointerLocked(true) + switch blocker { + case .inactiveApplication: fixture.focus.isApplicationActive = false + case .otherKeyWindow: fixture.focus.isWindowKey = false + case .remoteInputDisabled: fixture.view.remoteInputEnabled = false + case .keyboardOverlay: fixture.view.localOverlayCapturesInput = true + case .pictureInPicture: fixture.view.isPictureInPictureMode = true + case .actualNonKeyWindow: + fixture.view.inputFocusProvider = nil + #expect(!fixture.window.isKeyWindow) + } + fixture.view.setPointerLocked(false) + #expect(!fixture.view.isCursorCaptured) + #expect(fixture.cursorAssociations == [false, true]) + fixture.view.setPointerLocked(true) + fixture.view.enablePointerLock() + fixture.view.setManualPointerCapture(true) + fixture.view.setRemoteCursorVisible(false) + fixture.view.restoreInputFocus() + fixture.view.mouseInputMode = .absolute + fixture.view.confinesCursorToWindowInAbsoluteMode = true + fixture.view.captureAbsoluteCursorIfNeeded() + + #expect(fixture.cursorAssociations == [false, true]) + #expect(!fixture.view.isCursorCaptured) + #expect(!fixture.view.manualPointerCaptureOverride) + } + + @Test func aFrontmostRealInputRetriesRejectedCaptureAndNativeCommandsStillToggleIt() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + var associations: [Bool] = [] + fixture.view.cursorAssociationHandler = { isAssociated in + associations.append(isAssociated) + return associations.count == 1 ? .failure : .success + } + fixture.view.locksPointerWhenRelativeModeSelected = true + fixture.view.handleFocusGain() + #expect(!fixture.view.isPointerLocked) + let motion = UserInputEvent.mouse(.moved(deviceID: "mouse", deltaX: 3, deltaY: -2, timestamp: MediaTimestamp(nanoseconds: 1))) + fixture.model.routeInputEvent(motion, view: fixture.view) + + #expect(fixture.view.isPointerLocked) + #expect(associations == [false, false]) + #expect(try await fixture.recordedInputs() == [motion]) + fixture.model.handleNativeCommand(.togglePointerCapture) + #expect(!fixture.view.isPointerLocked) + fixture.model.handleNativeCommand(.togglePointerCapture) + #expect(fixture.view.isPointerLocked) + #expect(fixture.view.manualPointerCaptureOverride) + } + + @Test func pictureInPictureStillAdmitsAnUnfocusedPhysicalPadWithoutCapturing() async throws { + let fixture = try CouchCoopNativeInputFixture() + defer { fixture.close() } + fixture.focus.isWindowKey = false + fixture.view.allowsBackgroundGamepadInput = false + fixture.view.isPictureInPictureMode = true + fixture.model.isPictureInPicture = true + #expect(try await fixture.recordedInputs().isEmpty) + let native = GCController.withExtendedGamepad() + let pad = try #require(native.extendedGamepad) + fixture.configureNativeControllers([("pip-pad", native, nil)]) + pad.buttonA.setValue(1) + pad.leftThumbstick.xAxis.setValue(0.75) + fixture.pollNativeControllers() + fixture.model.handleNativeCommand(.togglePointerCapture) + + let admitted = gamepads(try await fixture.recordedInputs()) + #expect(admitted.count == 1) + #expect(admitted.first?.buttons == .south) + #expect(admitted.first?.leftStickX == 0.75) + #expect(!fixture.view.isCursorCaptured) + #expect(fixture.cursorAssociations.isEmpty) + } + + @Test func aRecordingBarrierReturnsItsOrderedPrefixEvenWhenTheReaderRunsLater() async throws { + let recorder = CouchCoopNativeInputRecorder() + let stamp = MediaTimestamp(nanoseconds: 1) + let first = UserInputEvent.gamepad(GamepadState(deviceID: "first", playerIndex: 0, buttons: .south, timestamp: stamp)) + let later = UserInputEvent.gamepad(GamepadState(deviceID: "later", playerIndex: 1, buttons: .east, timestamp: stamp)) + await recorder.append(.event(first)) + await recorder.append(.event(.text(deviceID: "input-test-barrier", value: "first-prefix", timestamp: stamp))) + await recorder.append(.event(later)) + await recorder.append(.event(.text(deviceID: "input-test-barrier", value: "second-prefix", timestamp: stamp))) + + #expect(try await recorder.snapshot(at: "first-prefix") == [first]) + #expect(try await recorder.snapshot(at: "second-prefix") == [first, later]) + } + + @Test func cancellingARecordingBarrierPropagatesCancellation() async { + let recorder = CouchCoopNativeInputRecorder() + let waiter = Task { try await recorder.snapshot(at: "cancelled-prefix") } + waiter.cancel() + + await #expect(throws: CancellationError.self) { try await waiter.value } + } + + private func desktopInputs(_ inputs: [UserInputEvent]) -> [UserInputEvent] { + inputs.filter { + switch $0 { + case .keyboard, .mouse: true + default: false + } + } + } + + private func gamepads(_ inputs: [UserInputEvent]) -> [GamepadState] { + inputs.compactMap { + guard case .gamepad(let state) = $0 else { return nil } + return state + } + } + + enum BlockingOverlay: CaseIterable, Sendable { + case hud, quitMenu, keyboard + } + + enum CaptureBlocker: CaseIterable, Sendable { + case inactiveApplication, otherKeyWindow, remoteInputDisabled, keyboardOverlay, pictureInPicture, actualNonKeyWindow + } +} diff --git a/Tests/Stream/NativeNVSTInputParityTests.swift b/Tests/Stream/NativeNVSTInputParityTests.swift index a6a80f7e2..09ae505ca 100644 --- a/Tests/Stream/NativeNVSTInputParityTests.swift +++ b/Tests/Stream/NativeNVSTInputParityTests.swift @@ -271,8 +271,17 @@ private actor ControlledNativeInputRecorder { @Test @MainActor func focusLossNeutralizesEveryActiveDeviceBeforePointerUnlock() throws { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) + view.inputFocusProvider = { (true, true) } + view.cursorAssociationHandler = { _ in .success } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false + window.isReleasedWhenClosed = false window.contentView = view + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } var sequence: [String] = [] view.onInputEvent = { event in switch event { diff --git a/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift b/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift index 5a938856a..6739d1c1c 100644 --- a/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift +++ b/Tests/Stream/NativeStreamViewCaptureLifecycleTests.swift @@ -13,6 +13,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.captureAbsoluteCursorIfNeeded() try #require(view.isAbsoluteCursorConfined) @@ -54,6 +55,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.captureAbsoluteCursorIfNeeded() try #require(view.isAbsoluteCursorConfined) @@ -79,6 +81,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.captureAbsoluteCursorIfNeeded() view.setRemoteCursorVisible(true) @@ -94,6 +97,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.remoteInputEnabled = false view.locksPointerWhenRelativeModeSelected = true @@ -110,7 +114,9 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } + view.inputFocusProvider = nil try #require(!window.isKeyWindow) view.locksPointerWhenRelativeModeSelected = true view.mouseInputMode = .relative @@ -125,6 +131,7 @@ import Testing defer { view.setPointerLocked(false) window.contentView = nil + window.close() } view.localOverlayCapturesInput = true view.locksPointerWhenRelativeModeSelected = true diff --git a/Tests/Stream/NativeStreamViewCursorFixtures.swift b/Tests/Stream/NativeStreamViewCursorFixtures.swift index ff63b3149..64767c187 100644 --- a/Tests/Stream/NativeStreamViewCursorFixtures.swift +++ b/Tests/Stream/NativeStreamViewCursorFixtures.swift @@ -17,6 +17,8 @@ import AppKit defer: false ) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) + window.isReleasedWhenClosed = false + view.inputFocusProvider = { (true, true) } let center = CGPoint(x: window.frame.midX, y: window.frame.midY) view.cursorLocationProvider = { center } view.cursorWarpHandler = { _ in } diff --git a/Tests/Stream/NativeStreamViewCursorPolicyTests.swift b/Tests/Stream/NativeStreamViewCursorPolicyTests.swift index b1a845ac2..370641c86 100644 --- a/Tests/Stream/NativeStreamViewCursorPolicyTests.swift +++ b/Tests/Stream/NativeStreamViewCursorPolicyTests.swift @@ -80,10 +80,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setManualPointerCapture(true) @@ -118,10 +125,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setManualPointerCapture(true) // Teardown and in-place reconnect both drop remote input, which releases the pointer. @@ -155,10 +169,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } // The game is in mouselook, so the client followed it into relative mode. view.setRemoteCursorVisible(false) @@ -188,10 +209,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setManualPointerCapture(true) view.setRemoteCursorVisible(false) @@ -282,10 +310,17 @@ import Testing let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 480, height: 270), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .relative + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } // The game is in mouselook, so the client is holding the pointer. view.setRemoteCursorVisible(false) diff --git a/Tests/Stream/NativeStreamViewMouseTests.swift b/Tests/Stream/NativeStreamViewMouseTests.swift index 5106db784..d2b3705e6 100644 --- a/Tests/Stream/NativeStreamViewMouseTests.swift +++ b/Tests/Stream/NativeStreamViewMouseTests.swift @@ -53,8 +53,16 @@ private struct MouseButtonTransition: Equatable { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false + window.isReleasedWhenClosed = false window.contentView = view + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } var events: [UserInputEvent] = [] var releaseObservedWhileLocked = false view.onInputEvent = { event in @@ -83,13 +91,20 @@ private struct MouseButtonTransition: Equatable { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false + window.isReleasedWhenClosed = false window.contentView = view var events: [UserInputEvent] = [] view.onInputEvent = { events.append($0) } let mouseDown = try #require(makeMouseEvent(type: .leftMouseDown)) let mouseUp = try #require(makeMouseEvent(type: .leftMouseUp)) - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.mouseDown(with: mouseDown) view.mouseUp(with: mouseUp) @@ -108,10 +123,17 @@ private struct MouseButtonTransition: Equatable { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720), styleMask: .borderless, backing: .buffered, defer: false) let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) view.cursorAssociationHandler = { _ in .success } + view.inputFocusProvider = { (true, true) } + view.cursorWarpHandler = { _ in } view.hidesCursorWhilePointerLocked = false view.mouseInputMode = .absolute + window.isReleasedWhenClosed = false window.contentView = view - defer { view.setPointerLocked(false) } + defer { + view.setPointerLocked(false) + window.contentView = nil + window.close() + } view.setRemoteCursorVisible(false) @@ -187,7 +209,8 @@ private struct MouseButtonTransition: Equatable { let (window, view) = makeAbsoluteCursorCaptureWindow() defer { view.setPointerLocked(false) - withExtendedLifetime(window) {} + window.contentView = nil + window.close() } var events: [UserInputEvent] = [] view.onInputEvent = { events.append($0) } diff --git a/Tests/Stream/StreamWindowLayoutMembershipTests.swift b/Tests/Stream/StreamWindowLayoutMembershipTests.swift new file mode 100644 index 000000000..ece29a484 --- /dev/null +++ b/Tests/Stream/StreamWindowLayoutMembershipTests.swift @@ -0,0 +1,201 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized, .streamLifecycleExclusive, + .disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) +struct StreamWindowLayoutMembershipTests { + @Test func draggingOneOfTwoTilesKeepsTheReadersPlacementAndReleasesTheSurvivor() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let moved = fixture.windows[0] + let survivor = fixture.windows[1] + #expect(moved.layoutState.isAutoTiled && survivor.layoutState.isAutoTiled) + + moved.setFrame(moved.frame.offsetBy(dx: 28, dy: 32), display: true) + let readerFrame = moved.frame + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(moved.frame == readerFrame) + #expect(moved.layoutState.isManuallyPlaced && !moved.layoutState.isAutoTiled) + #expect(!moved.layoutState.isBorderless && moved.level == .normal) + #expect(moved.standardWindowButton(.closeButton)?.isHidden == false) + #expect(survivor.frame == fixture.originalFrames[1]) + #expect(!survivor.layoutState.isAutoTiled && !survivor.layoutState.isBorderless) + #expect(!survivor.layoutState.isManuallyPlaced && survivor.level == .normal) + #expect(!moved.hasPendingLayoutChange && !survivor.hasPendingLayoutChange) + } + + @Test func resizingOneOfThreeTilesRetilesOnlyTheSurvivorsWithoutAProgrammaticFeedbackLoop() async throws { + let fixture = try StreamWindowLayoutFixture(count: 3) + defer { fixture.close() } + await settleStreamWindowChanges() + let resized = fixture.windows[0] + let survivors = Array(fixture.windows.dropFirst()) + var changes = 0 + let center = NotificationCenter.default + let tokens = survivors.flatMap { window in + [NSWindow.didMoveNotification, NSWindow.didResizeNotification].map { name in + center.addObserver(forName: name, object: window, queue: .main) { _ in + MainActor.assumeIsolated { changes += 1 } + } + } + } + defer { for token in tokens { center.removeObserver(token) } } + var readerFrame = resized.frame + readerFrame.size.height -= 60 + resized.setFrame(readerFrame, display: true) + readerFrame = resized.frame + await settleStreamWindowChanges() + let settledChanges = changes + let expected = fixture.expectedTiles(count: 2) + await settleStreamWindowChanges() + + #expect(resized.frame == readerFrame && resized.layoutState.isManuallyPlaced) + #expect(!resized.layoutState.isAutoTiled && resized.level == .normal) + #expect(survivors.map(\.frame) == expected) + #expect(survivors.allSatisfy { $0.layoutState.isAutoTiled && !$0.layoutState.isManuallyPlaced }) + #expect(survivors.allSatisfy { $0.level == OPNStreamBorderless.coveringLevel && !$0.hasPendingLayoutChange }) + #expect(changes == settledChanges) + } + + @Test func enteringPictureInPictureNotifiesTheOwningPresenterBeforeQueuedRestorationLands() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let picture = fixture.windows[0] + let survivor = fixture.windows[1] + OPNStreamPictureInPicture.enter(picture, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + + #expect(picture.isPictureInPicture && picture.frame.size == NSSize(width: 640, height: 360)) + #expect(picture.windowedState?.frame == fixture.originalFrames[0]) + #expect(survivor.frame == fixture.originalFrames[1]) + #expect(!survivor.layoutState.isAutoTiled && !survivor.layoutState.isBorderless) + #expect(survivor.level == .normal) + OPNStreamPictureInPicture.exit(picture) + await settleStreamWindowChanges() + + #expect(picture.frame == fixture.originalFrames[0]) + #expect(picture.layoutState.isManuallyPlaced && !picture.layoutState.isAutoTiled) + #expect(survivor.frame == fixture.originalFrames[1] && !survivor.layoutState.isAutoTiled) + } + + @Test func theReadersBorderlessToggleReleasesTheOtherTileThroughItsOwner() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let window = fixture.windows[0] + let survivor = fixture.windows[1] + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + + #expect(window.frame == fixture.originalFrames[0] && window.layoutState.isManuallyPlaced) + #expect(survivor.frame == fixture.originalFrames[1]) + #expect(!survivor.layoutState.isAutoTiled && survivor.level == .normal) + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + + #expect(window.layoutState.isBorderless && !window.layoutState.isAutoTiled) + #expect(window.frame == OPNStreamBorderless.targetFrame(for: window)) + #expect(survivor.frame == fixture.originalFrames[1] && !survivor.layoutState.isAutoTiled) + } + + @Test func nativeFullScreenNotificationsReleaseSurvivorsWithoutRestoringOverTheTransition() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let transitioning = fixture.windows[0] + let tileFrame = transitioning.frame + let survivor = fixture.windows[1] + let center = NotificationCenter.default + center.post(name: NSWindow.willEnterFullScreenNotification, object: transitioning) + fixture.presenter.retileWindows() + await settleStreamWindowChanges() + + #expect(transitioning.frame == tileFrame) + #expect(transitioning.layoutState.isManuallyPlaced && !transitioning.hasPendingLayoutChange) + #expect(!transitioning.layoutState.isBorderless && !transitioning.layoutState.isAutoTiled) + #expect(survivor.frame == fixture.originalFrames[1] && survivor.level == .normal) + center.post(name: NSWindow.didEnterFullScreenNotification, object: transitioning) + center.post(name: NSWindow.willExitFullScreenNotification, object: transitioning) + center.post(name: NSWindow.didExitFullScreenNotification, object: transitioning) + await settleStreamWindowChanges() + + #expect(transitioning.frame == tileFrame && !transitioning.layoutState.isAutoTiled) + #expect(survivor.frame == fixture.originalFrames[1] && !survivor.layoutState.isAutoTiled) + } + + @Test func minimizingATileDoesNotBringItBackWhenTheSurvivorIsReleased() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let minimized = fixture.windows[0] + minimized.miniaturize(nil) + await settleStreamWindowChanges() + fixture.presenter.retileWindows() + await settleStreamWindowChanges() + + #expect(minimized.isMiniaturized) + #expect(minimized.layoutState.isManuallyPlaced && !minimized.layoutState.isAutoTiled) + #expect(fixture.windows[1].frame == fixture.originalFrames[1]) + #expect(!fixture.windows[1].layoutState.isAutoTiled && fixture.windows[1].level == .normal) + minimized.deminiaturize(nil) + await settleStreamWindowChanges() + #expect(!minimized.layoutState.isAutoTiled && !fixture.windows[1].layoutState.isAutoTiled) + } + + @Test func coverageAndOrientationChangesRemainProgrammaticPlacements() async throws { + let fixture = try StreamWindowLayoutFixture(count: 3) + defer { fixture.close() } + await settleStreamWindowChanges() + fixture.presenter.setTileCoverage(.visibleArea) + OPNStreamTileOrientation.store(.topAndBottom) + fixture.presenter.retileWindows() + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(fixture.windows.map(\.frame) == fixture.expectedTiles(count: 3)) + #expect(fixture.windows.allSatisfy { $0.layoutState.isAutoTiled && !$0.layoutState.isManuallyPlaced }) + #expect(fixture.windows.allSatisfy { $0.level == .normal && !$0.hasPendingLayoutChange }) + } + + @Test func flagOffLeavesOrdinaryWindowsUntiledAcrossPlacementAndModeChanges() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2, isCouchCoopEnabled: false) + defer { fixture.close() } + await settleStreamWindowChanges() + let moved = fixture.windows[0] + moved.setFrame(moved.frame.offsetBy(dx: 20, dy: 0), display: true) + let readerFrame = moved.frame + OPNStreamPictureInPicture.enter(fixture.windows[1], aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + OPNStreamPictureInPicture.exit(fixture.windows[1]) + await settleStreamWindowChanges() + + #expect(moved.frame == readerFrame) + #expect(fixture.windows[1].frame == fixture.originalFrames[1]) + #expect(fixture.windows.allSatisfy { !$0.layoutState.isAutoTiled && !$0.layoutState.isBorderless }) + #expect(fixture.windows.allSatisfy { $0.level == .normal }) + } + + @Test func dismissingDuringDeferredPictureInPictureCancelsItsGeometryAndReleasesTheSurvivor() async throws { + let fixture = try StreamWindowLayoutFixture(count: 2) + defer { fixture.close() } + await settleStreamWindowChanges() + let ending = fixture.windows[0] + fixture.fixtures[0].geometryView.isResizeHeld = true + OPNStreamPictureInPicture.enter(ending, aspectRatio: 16.0 / 9.0) + fixture.presenter.dismiss(fixture.sessions[0]) + fixture.fixtures[0].geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(fixture.presenter.windows[fixture.sessions[0]] == nil) + #expect(!ending.isVisible && ending.contentView == nil) + #expect(!ending.isPictureInPicture && !ending.isPictureInPicturePending && !ending.hasPendingLayoutChange) + #expect(fixture.windows[1].frame == fixture.originalFrames[1]) + #expect(!fixture.windows[1].layoutState.isAutoTiled && fixture.windows[1].level == .normal) + } +} diff --git a/Tests/Stream/StreamWindowModeTransitionTests.swift b/Tests/Stream/StreamWindowModeTransitionTests.swift new file mode 100644 index 000000000..28910a8e2 --- /dev/null +++ b/Tests/Stream/StreamWindowModeTransitionTests.swift @@ -0,0 +1,388 @@ +import AppKit +import Testing +@testable import OpenNOW + +@MainActor +@Suite(.serialized, .streamLifecycleExclusive, + .disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) +struct StreamWindowModeTransitionTests { + private let tile = NSRect(x: 100, y: 100, width: 800, height: 450) + + @Test func theHostRestoresBorderlessBeforePictureInPictureCapturesItsReturnPlacement() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + let originalStyle = window.styleMask + let originalBehavior = window.collectionBehavior + let carrier = makeCarrier(for: window) + let model = makeHost(in: window) + let configuredAspectRatio = CGFloat( + OPNStreamPreferences.launchProfile(forGame: model.configuration.applicationID, + capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio + ) + let expectedSize = OPNStreamPictureInPicture.contentSize(aspectRatio: configuredAspectRatio) + window.contentAspectRatio = NSSize(width: configuredAspectRatio, height: 1) + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + #expect(window.frame == tile) + + model.togglePictureInPicture() + #expect(window.isPictureInPicturePending) + await settleStreamWindowChanges() + + #expect(window.isPictureInPicture && model.isPictureInPicture) + #expect(model.nativeView?.isPictureInPictureMode == true) + #expect(abs(window.frame.width - expectedSize.width) < 1) + #expect(abs(window.frame.height - expectedSize.height) < 1) + #expect(window.windowedState?.frame == original) + #expect(window.windowedState?.level == .normal) + #expect(window.level == .floating) + #expect(window.collectionBehavior == [.canJoinAllSpaces]) + #expect(carrier.collectionBehavior.contains(.canJoinAllSpaces)) + #expect(carrier.level.rawValue >= window.level.rawValue) + #expect(window.standardWindowButton(.closeButton)?.isHidden == true) + #expect(window.styleMask == originalStyle) + #expect(!window.isPictureInPicturePending && !window.hasPendingLayoutChange) + #expect(OPNStreamWindowFrameStore.rememberedFrame(isPictureInPicture: false, defaults: fixture.defaults) == original) + + model.togglePictureInPicture() + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture) + #expect(window.frame == original) + #expect(window.level == .normal && window.collectionBehavior == originalBehavior) + #expect(window.standardWindowButton(.closeButton)?.isHidden == false) + #expect(!carrier.collectionBehavior.contains(.canJoinAllSpaces)) + #expect(carrier.collectionBehavior.contains(.ignoresCycle)) + #expect(!carrier.collectionBehavior.contains(.fullScreenPrimary)) + } + + @Test func pictureInPictureWaitsForLiveResizeWithoutChangingTheWindowOrLosingItsOriginalFrame() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + var isCompleted = false + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0, completion: { isCompleted = true }) + + await settleStreamWindowChanges() + + #expect(!isCompleted && !window.isPictureInPicture) + #expect(window.isPictureInPicturePending && window.hasPendingLayoutChange) + #expect(window.frame == tile && window.level == OPNStreamBorderless.coveringLevel) + #expect(window.borderlessRestoreState?.frame == original) + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(isCompleted && window.isPictureInPicture) + #expect(window.frame.size == NSSize(width: 640, height: 360)) + #expect(window.windowedState?.frame == original) + #expect(!window.hasPendingLayoutChange) + } + + @Test func aSupersededTileCannotOverwritePictureInPictureAfterQueuedWorkLands() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile) + OPNStreamBorderless.enter(window, frame: tile.offsetBy(dx: 50, dy: 0), coversMenuBar: true) + var entryKeyWindow: NSWindow? + OPNStreamPictureInPicture.enter(window, aspectRatio: 4.0 / 3.0, prepareForEntry: { + entryKeyWindow = NSApp.keyWindow + return true + }, completion: { + #expect(NSApp.keyWindow === entryKeyWindow) + #expect(window.canBecomeKey) + }) + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(window.frame.size == NSSize(width: 640, height: 480)) + #expect(window.windowedState?.frame == original) + #expect(window.level == .floating && !window.layoutState.isBorderless) + #expect(window.resizeIncrements == NSSize(width: 1, height: 1)) + #expect(window.contentResizeIncrements == NSSize(width: 1, height: 1)) + OPNStreamPictureInPicture.exit(window) + await settleStreamWindowChanges() + #expect(window.frame == original) + } + + @Test func aNewBorderlessRequestSupersedesPendingPictureInPictureWithoutLosingTheRestoreState() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + var isObsoleteEntryCompleted = false + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0, completion: { isObsoleteEntryCompleted = true }) + let replacement = tile.offsetBy(dx: 30, dy: 0) + OPNStreamBorderless.enter(window, frame: replacement, coversMenuBar: true) + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(!isObsoleteEntryCompleted && !window.isPictureInPicturePending) + #expect(!window.isPictureInPicture && window.layoutState.isBorderless) + #expect(window.frame == replacement) + #expect(window.borderlessRestoreState?.frame == original) + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + #expect(window.isPictureInPicture && window.windowedState?.frame == original) + } + + @Test func cancellingADeferredEntryRestoresWindowedModeAndAllowsTheNextEntry() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + OPNStreamPictureInPicture.exit(window) + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !window.isPictureInPicturePending) + #expect(window.frame == original && !window.layoutState.isBorderless) + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + #expect(window.isPictureInPicture && window.windowedState?.frame == original) + } + + @Test func rapidBorderlessTogglesReverseThePendingExitWithoutReplacingTheOriginalReturnFrame() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + OPNStreamBorderless.toggleByReader(window) + #expect(window.isBorderlessExitPending) + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + + #expect(window.layoutState.isBorderless && !window.isBorderlessExitPending) + #expect(window.frame == OPNStreamBorderless.targetFrame(for: window)) + #expect(window.borderlessRestoreState?.frame == original) + OPNStreamBorderless.toggleByReader(window) + await settleStreamWindowChanges() + #expect(window.frame == original && !window.layoutState.isBorderless) + } + + @Test(arguments: [false, true]) + func closingOrEndingTheSessionCancelsADeferredEntry(isClosing: Bool) async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + fixture.geometryView.isResizeHeld = true + var isCompleted = false + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0, completion: { isCompleted = true }) + if isClosing { window.close() } + if !isClosing { window.endPresentation() } + fixture.geometryView.isResizeHeld = false + await settleStreamWindowChanges() + + #expect(!isCompleted && !window.isPictureInPicture && !window.isPictureInPicturePending) + #expect(!window.hasPendingLayoutChange && window.frame == tile) + #expect(window.windowedState == nil) + } + + @Test func anInvalidatedHostCannotEnterAfterItsBorderlessRestorationCompletes() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let original = window.frame + let model = makeHost(in: window) + OPNStreamBorderless.enter(window, frame: tile) + await settleStreamWindowChanges() + model.togglePictureInPicture() + model.isEnding = true + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture) + #expect(!window.isPictureInPicturePending && window.frame == original) + } + + @Test func cancellingTheHostsFullScreenSequenceDoesNotEnterPictureInPictureLater() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let model = makeHost(in: window) + let original = window.frame + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + #expect(window.isPictureInPicturePending) + model.togglePictureInPicture() + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture) + #expect(!window.isPictureInPicturePending && window.frame == original) + } + + @Test func aNativeFullScreenPictureInPictureRequestSurvivesRetilingAfterASecondStreamLaunches() async throws { + let fixture = try StreamWindowFullScreenFixture() + defer { fixture.close() } + let window = fixture.fullScreen.window + let survivor = fixture.windowed.window + let survivorFrame = survivor.frame + let model = makeHost(in: window) + #expect(fixture.isNativeFullScreen && !window.isVisible) + #expect(!window.styleMask.contains(.fullScreen)) + #expect(!window.layoutState.isManuallyPlaced && !window.layoutState.isAutoTiled) + #expect(!survivor.layoutState.isAutoTiled) + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + let requestID = window.pictureInPictureRequestID + #expect(requestID != nil) + fixture.beginExit() + fixture.landExit() + await settleStreamWindowChanges() + fixture.layout.presenter.retileWindows() + await settleStreamWindowChanges() + + #expect(window.isPictureInPicturePending && window.pictureInPictureRequestID == requestID) + #expect(!fixture.isNativeFullScreen && !window.styleMask.contains(.fullScreen)) + #expect(!window.layoutState.isManuallyPlaced && !window.layoutState.isAutoTiled) + #expect(window.frame == fixture.returnFrame && !window.hasPendingLayoutChange) + #expect(survivor.frame == survivorFrame && !survivor.layoutState.isAutoTiled) + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + let expectedSize = OPNStreamPictureInPicture.contentSize(aspectRatio: CGFloat( + OPNStreamPreferences.launchProfile(forGame: model.configuration.applicationID, + capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio + )) + #expect(window.isPictureInPicture && model.isPictureInPicture) + #expect(model.nativeView?.isPictureInPictureMode == true) + #expect(window.windowedState?.frame == fixture.returnFrame) + #expect(abs(window.frame.width - expectedSize.width) < 1) + #expect(abs(window.frame.height - expectedSize.height) < 1) + #expect(window.level == .floating && window.collectionBehavior == [.canJoinAllSpaces]) + #expect(!window.isPictureInPicturePending && !window.layoutState.isAutoTiled && !window.hasPendingLayoutChange) + #expect(survivor.frame == survivorFrame && survivor.level == .normal) + #expect(!survivor.layoutState.isAutoTiled && !survivor.layoutState.isManuallyPlaced) + } + + @Test(arguments: [false, true]) + func anAbortedFullScreenPictureInPictureRequestReturnsItsPlacementToThePresenter(isHostInvalidated: Bool) async throws { + let fixture = try StreamWindowFullScreenFixture() + defer { fixture.close() } + let window = fixture.fullScreen.window + let survivor = fixture.windowed.window + let model = makeHost(in: window) + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + fixture.beginExit() + fixture.landExit() + await settleStreamWindowChanges() + #expect(window.isPictureInPicturePending && !window.layoutState.isManuallyPlaced) + + if isHostInvalidated { model.nativeView = nil } + if !isHostInvalidated { model.togglePictureInPicture() } + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicture && !model.isPictureInPicture && !window.isPictureInPicturePending) + #expect(!window.layoutState.isManuallyPlaced && !survivor.layoutState.isManuallyPlaced) + #expect(window.layoutState.isAutoTiled && survivor.layoutState.isAutoTiled) + #expect([window.frame, survivor.frame] == fixture.layout.expectedTiles(count: 2)) + #expect(!window.hasPendingLayoutChange && !survivor.hasPendingLayoutChange) + } + + @Test func anUnconfirmedFullScreenExitReleasesItsPlacementWhenPictureInPictureRetriesExpire() async throws { + let fixture = try StreamWindowFullScreenFixture() + defer { fixture.close() } + let window = fixture.fullScreen.window + let survivor = fixture.windowed.window + let model = makeHost(in: window) + model.isFullScreenTransitioning = true + model.togglePictureInPicture() + fixture.beginExit() + fixture.landExit() + await settleStreamWindowChanges() + #expect(window.isPictureInPicturePending && !window.layoutState.isAutoTiled) + + for _ in 0..<(NativeNVSTHostViewModel.sessionReadyFullScreenAttemptLimit * 2) { + guard window.isPictureInPicturePending else { break } + try await Task.sleep(for: NativeNVSTHostViewModel.sessionReadyFullScreenRetryDelay) + } + model.isFullScreenTransitioning = false + await settleStreamWindowChanges() + await settleStreamWindowChanges() + + #expect(!window.isPictureInPicturePending && !window.isPictureInPicture && !model.isPictureInPicture) + #expect(!window.layoutState.isManuallyPlaced && !survivor.layoutState.isManuallyPlaced) + #expect(window.layoutState.isAutoTiled && survivor.layoutState.isAutoTiled) + #expect([window.frame, survivor.frame] == fixture.layout.expectedTiles(count: 2)) + #expect(!window.hasPendingLayoutChange && !survivor.hasPendingLayoutChange) + } + + @Test func theAccountHandoffFollowsSettledCoveringLevelsThroughEntryPictureInPictureAndClosure() async throws { + let fixture = try StreamWindowTransitionFixture() + defer { fixture.close() } + let window = fixture.window + let main = NSWindow(contentRect: tile, styleMask: [.titled], backing: .buffered, defer: false) + main.isReleasedWhenClosed = false + let handoff = OPNMainWindowAccountHandoff(mainWindow: { nil }, coveringLevels: { + window.isVisible && window.layoutState.isBorderless ? [window.level] : [] + }) + defer { + handoff.end() + main.close() + } + handoff.begin(from: nil) + handoff.attach(main) + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + + #expect(window.level == OPNStreamBorderless.coveringLevel) + #expect(main.level.rawValue > window.level.rawValue) + OPNStreamPictureInPicture.enter(window, aspectRatio: 16.0 / 9.0) + await settleStreamWindowChanges() + #expect(window.isPictureInPicture && main.level == .normal) + OPNStreamPictureInPicture.exit(window) + OPNStreamBorderless.enter(window, frame: tile, coversMenuBar: true) + await settleStreamWindowChanges() + #expect(main.level.rawValue > window.level.rawValue) + window.close() + await settleStreamWindowChanges() + + #expect(!window.isVisible && main.level == .normal) + } + + private func makeHost(in window: OPNStreamWindow) -> NativeNVSTHostViewModel { + let model = NativeNVSTHostViewModel( + configuration: StreamLaunchConfiguration(title: "Geometry", applicationID: "100", accessToken: "token", accountLinked: true, selectedStore: "steam"), + sessionProvider: StubNativeNVSTSessionProvider(), preventDisplaySleep: false, + onProgress: nil, onEnd: { _, _, _ in } + ) + let view = NativeStreamView(frame: window.contentView?.bounds ?? .zero) + view.cursorAssociationHandler = { _ in .success } + view.cursorWarpHandler = { _ in } + view.hidesCursorWhilePointerLocked = false + window.contentView = view + model.nativeView = view + return model + } + + private func makeCarrier(for window: OPNStreamWindow) -> NSWindow { + let carrier = NSWindow(contentRect: tile, styleMask: .borderless, backing: .buffered, defer: false) + carrier.isReleasedWhenClosed = false + carrier.alphaValue = 0 + carrier.ignoresMouseEvents = true + window.addChildWindow(carrier, ordered: .above) + return carrier + } +} diff --git a/Tests/Stream/StreamWindowTransitionFixtures.swift b/Tests/Stream/StreamWindowTransitionFixtures.swift new file mode 100644 index 000000000..78bf38e4d --- /dev/null +++ b/Tests/Stream/StreamWindowTransitionFixtures.swift @@ -0,0 +1,168 @@ +import AppKit +import GameController +import Testing +@testable import OpenNOW + +@MainActor +final class StreamWindowGeometryDeferralView: NSView { + var isResizeHeld = false + + override var inLiveResize: Bool { isResizeHeld || super.inLiveResize } +} + +@MainActor +final class StreamWindowTransitionFixture { + let window: OPNStreamWindow + let geometryView: StreamWindowGeometryDeferralView + let defaults: UserDefaults + private let suite: String + + init(isShown: Bool = true) throws { + _ = NSApplication.shared + let suite = "OpenNOWTests.WindowTransition.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + let window = OPNStreamWindowFactory.make(sessionID: UUID(), defaults: defaults) + let geometryView = StreamWindowGeometryDeferralView(frame: window.contentView?.bounds ?? .zero) + self.suite = suite + self.defaults = defaults + self.window = window + self.geometryView = geometryView + window.contentView = geometryView + window.alphaValue = 0 + if isShown { window.orderFront(nil) } + } + + func close() { + window.stopPersistingFrame() + for child in window.childWindows ?? [] { + window.removeChildWindow(child) + child.close() + } + window.close() + defaults.removePersistentDomain(forName: suite) + } +} + +@MainActor +final class StreamWindowLayoutFixture { + let presenter: OPNStreamWindowPresenter + let sessions: [UUID] + let fixtures: [StreamWindowTransitionFixture] + let originalFrames: [NSRect] + private let previousFlag = OPNLabs.isCouchCoopEnabled + private let previousCoverage = OPNStreamTileCoverage.stored + private let previousOrientation = OPNStreamTileOrientation.stored + private let wasMonitoringBackgroundEvents = GCController.shouldMonitorBackgroundEvents + + var windows: [OPNStreamWindow] { fixtures.map(\.window) } + + init(count: Int, isCouchCoopEnabled: Bool = true, + isNativeFullScreen: @escaping @MainActor (OPNStreamWindow) -> Bool = { $0.styleMask.contains(.fullScreen) }) throws { + presenter = OPNStreamWindowPresenter(isNativeFullScreen: isNativeFullScreen) + #expect(!NSScreen.screens.isEmpty) + let fixtures = try (0.. [NSRect] { + OPNStreamTileLayout.tiles( + screens: OPNStreamTileCoverage.stored.areas( + frames: NSScreen.screens.map(\.frame), visibleFrames: NSScreen.screens.map(\.visibleFrame) + ), + windowCount: count, + orientation: OPNStreamTileOrientation.stored + ).map(\.frame) + } +} + +@MainActor +final class StreamWindowFullScreenFixture { + let layout: StreamWindowLayoutFixture + let fullScreen: StreamWindowTransitionFixture + let windowed: StreamWindowTransitionFixture + let returnFrame: NSRect + private let fullScreenSessionID: UUID + private let windowedSessionID: UUID + private let fullScreenStatus: StreamWindowFullScreenStatus + + var isNativeFullScreen: Bool { fullScreenStatus.isFullScreen } + + init() throws { + let fullScreen = try StreamWindowTransitionFixture(isShown: false) + let fullScreenStatus = StreamWindowFullScreenStatus() + let layout = try StreamWindowLayoutFixture(count: 0, isNativeFullScreen: { window in + if window === fullScreen.window { return fullScreenStatus.isFullScreen } + return window.styleMask.contains(.fullScreen) + }) + let fullScreenSessionID = UUID() + let windowedSessionID = UUID() + let returnFrame = fullScreen.window.frame + #expect(!fullScreen.window.isVisible) + #expect(!fullScreen.window.styleMask.contains(.fullScreen)) + layout.presenter.register(fullScreen.window, for: fullScreenSessionID) + NotificationCenter.default.post(name: NSWindow.willEnterFullScreenNotification, object: fullScreen.window) + NotificationCenter.default.post(name: NSWindow.didEnterFullScreenNotification, object: fullScreen.window) + let windowed = try StreamWindowTransitionFixture() + layout.presenter.register(windowed.window, for: windowedSessionID) + layout.presenter.retileWindows() + self.layout = layout + self.fullScreen = fullScreen + self.windowed = windowed + self.returnFrame = returnFrame + self.fullScreenSessionID = fullScreenSessionID + self.windowedSessionID = windowedSessionID + self.fullScreenStatus = fullScreenStatus + } + + func beginExit() { + NotificationCenter.default.post(name: NSWindow.willExitFullScreenNotification, object: fullScreen.window) + } + + func landExit() { + #expect(!fullScreen.window.isVisible) + #expect(!fullScreen.window.styleMask.contains(.fullScreen)) + fullScreenStatus.isFullScreen = false + NotificationCenter.default.post(name: NSWindow.didExitFullScreenNotification, object: fullScreen.window) + } + + func close() { + if isNativeFullScreen { + beginExit() + landExit() + } + layout.presenter.dismiss(fullScreenSessionID) + layout.presenter.dismiss(windowedSessionID) + fullScreen.close() + windowed.close() + layout.close() + } +} + +@MainActor +private final class StreamWindowFullScreenStatus { + var isFullScreen = true +} + +@MainActor +func settleStreamWindowChanges() async { + try? await Task.sleep(for: .milliseconds(200)) + await withCheckedContinuation { continuation in + DispatchQueue.main.async { continuation.resume() } + } +} diff --git a/View/Stream/OPNStreamWindowPresenter.swift b/View/Stream/OPNStreamWindowPresenter.swift index 2bcdf9ce1..3d05c94a4 100644 --- a/View/Stream/OPNStreamWindowPresenter.swift +++ b/View/Stream/OPNStreamWindowPresenter.swift @@ -27,6 +27,11 @@ final class OPNStreamWindowPresenter { /// Held so the observation is not dropped the moment it is installed. private var sessionChangeObserver: NSObjectProtocol? private var screenChangeObserver: NSObjectProtocol? + private let isNativeFullScreen: @MainActor (OPNStreamWindow) -> Bool + + init(isNativeFullScreen: @escaping @MainActor (OPNStreamWindow) -> Bool = { $0.styleMask.contains(.fullScreen) }) { + self.isNativeFullScreen = isNativeFullScreen + } /// Per-window full-screen bookkeeping, tracked from `present` so a dismissal that lands /// mid-enter is recognised before AppKit sets `.fullScreen`. @@ -162,12 +167,9 @@ final class OPNStreamWindowPresenter { } OPNStreamWindowCloseGuard.install(on: window) - windows[session.id] = window - windowOrder.append(session.id) + register(window, for: session.id) hostingViews[session.id] = hostingView presentedConfigurationIDs[session.id] = configuration.id - // Tracked for the window's whole life so a mid-enter dismissal sees the transition. - track(window) // A launch is not a "show me" action. `OPNMainWindow.present(activating:)` sets the rule the // whole app follows - a session launch leaves the app where it is, and the Session Ready // preference keeps the only say over whether OpenNOW comes forward - and an unconditional @@ -183,15 +185,27 @@ final class OPNStreamWindowPresenter { OPNLog.info(.launch, "Stream window presented for \(configuration.applicationID) session=\(session.id)") } + func register(_ window: OPNStreamWindow, for sessionID: UUID) { + guard windows[sessionID] !== window else { return } + dismiss(sessionID) + windows[sessionID] = window + windowOrder.append(sessionID) + track(window) + window.layoutMembershipDidChange = { [weak self, weak window] in + guard let self, let window, self.windows[sessionID] === window else { return } + self.retileWindows() + } + } + /// Takes one session's window away. A close button or a cancellation can both land here for the /// same ending; the second call has nothing left to do. func dismiss(_ sessionID: UUID) { guard let window = windows[sessionID] else { return } + window.endPresentation() presentedConfigurationIDs[sessionID] = nil hostingViews[sessionID] = nil windows[sessionID] = nil windowOrder.removeAll { $0 == sessionID } - OPNStreamBorderless.postCoveringStateChange() OPNStreamWindowCloseGuard.uninstall(from: window) window.closeRequestHandler = nil window.sessionSurface = nil @@ -298,8 +312,11 @@ final class OPNStreamWindowPresenter { func retileWindows() { let ordered = windowOrder.compactMap { windows[$0] } let placeable = OPNLabs.isCouchCoopEnabled - ? ordered.filter { Self.isPlaceableByLayout($0) } + ? ordered.filter { isPlaceableByLayout($0) } : [] + for window in ordered where window.layoutState.isAutoTiled && !placeable.contains(where: { $0 === window }) { + releaseTile(window) + } guard placeable.count >= 2 else { for window in ordered where window.layoutState.isAutoTiled { releaseTile(window) } return @@ -315,9 +332,6 @@ final class OPNStreamWindowPresenter { window.layoutState.isAutoTiled = true OPNStreamBorderless.enter(window, frame: tile.frame, coversMenuBar: coverage == .fullScreen) } - for window in ordered where window.layoutState.isAutoTiled && !placeable.contains(where: { $0 === window }) { - releaseTile(window) - } } /// Switches tiled windows between sitting below the menu bar and filling their screens, and @@ -329,8 +343,11 @@ final class OPNStreamWindowPresenter { var hasTiledWindows: Bool { windows.values.contains { $0.layoutState.isAutoTiled } } - private static func isPlaceableByLayout(_ window: OPNStreamWindow) -> Bool { - !window.layoutState.isManuallyPlaced && !window.isPictureInPicture && !window.styleMask.contains(.fullScreen) + private func isPlaceableByLayout(_ window: OPNStreamWindow) -> Bool { + let presentation = presentations[ObjectIdentifier(window)] + return !window.layoutState.isManuallyPlaced && !window.isPictureInPicture && !window.isPictureInPicturePending && + !window.isMiniaturized && !window.isPresentationEnding && !isNativeFullScreen(window) && + presentation?.transition == FullScreenTransition.none } private func releaseTile(_ window: OPNStreamWindow) { @@ -367,7 +384,18 @@ final class OPNStreamWindowPresenter { MainActor.assumeIsolated { window.map(OPNStreamWindowPresenter.noteFrameChange) } } } - presentation.observerTokens = [willEnter, didEnter, willExit, didExit] + layoutTokens + let willMiniaturize = center.addObserver(forName: NSWindow.willMiniaturizeNotification, object: window, queue: .main) { [weak window] _ in + MainActor.assumeIsolated { + guard let window, !window.isApplyingLayoutChange else { return } + OPNStreamBorderless.takePlacement(window) + } + } + let miniaturizationTokens = [NSWindow.didMiniaturizeNotification, NSWindow.didDeminiaturizeNotification].map { name in + center.addObserver(forName: name, object: window, queue: .main) { [weak self] _ in + MainActor.assumeIsolated { self?.retileWindows() } + } + } + presentation.observerTokens = [willEnter, didEnter, willExit, didExit, willMiniaturize] + layoutTokens + miniaturizationTokens presentations[id] = presentation return presentation } @@ -377,14 +405,19 @@ final class OPNStreamWindowPresenter { static func noteFrameChange(_ window: OPNStreamWindow) { // No layout frame yet means the placement is still waiting on the geometry gate, so a move // that lands before it is not the reader's. - guard window.layoutState.isAutoTiled, window.layoutFrame != nil, !window.isAtLayoutFrame else { return } - window.layoutState.isAutoTiled = false - window.layoutState.isManuallyPlaced = true - window.clearLayoutFrame() + guard !window.isApplyingLayoutChange, window.layoutState.isAutoTiled, + window.layoutFrame != nil, !window.isAtLayoutFrame else { return } + OPNStreamBorderless.takePlacement(window) } private func setTransition(_ transition: FullScreenTransition, for id: ObjectIdentifier) { - presentations[id]?.transition = transition + guard let presentation = presentations[id] else { return } + presentation.transition = transition + let window = presentation.window + if window.layoutState.isAutoTiled || window.layoutState.isBorderless { + OPNStreamBorderless.takePlacement(window) + } + retileWindows() } private func endTransition(_ transition: FullScreenTransition, for id: ObjectIdentifier) { @@ -392,6 +425,7 @@ final class OPNStreamWindowPresenter { guard presentation.transition == transition else { return } presentation.transition = .none advanceDismissal(presentation) + retileWindows() } private func removeTransitionObservers(from presentation: WindowPresentation) { @@ -412,11 +446,13 @@ final class OPNStreamWindowPresenter { private func tearDownWindow(_ window: OPNStreamWindow) { window.stopPersistingFrame() window.orderOut(nil) + OPNStreamBorderless.postCoveringStateChange(for: window) window.contentView = nil // A late re-presentation must not leave the contentless window on screen. Task { @MainActor [weak window] in guard let window, window.isVisible else { return } window.orderOut(nil) + OPNStreamBorderless.postCoveringStateChange(for: window) } } diff --git a/ViewModel/Stream/NativeNVSTHostInput.swift b/ViewModel/Stream/NativeNVSTHostInput.swift index 8d7a2edb5..4f0a56cf7 100644 --- a/ViewModel/Stream/NativeNVSTHostInput.swift +++ b/ViewModel/Stream/NativeNVSTHostInput.swift @@ -146,11 +146,13 @@ extension NativeNVSTHostViewModel { } return } - guard path != nil, isConnected, !unifiedHUDVisible, !streamControlsVisible, !isEnding, !didEnd else { return } - if view.remoteInputEnabled, !NativeNVSTInputDispatcher.isNeutralizing(event), + guard path != nil, isConnected, !isEnding, !didEnd else { return } + let isNeutralizing = NativeNVSTInputDispatcher.isNeutralizing(event) + guard isNeutralizing || (view.remoteInputEnabled && !view.localOverlayCapturesInput && !unifiedHUDVisible && !streamControlsVisible) else { return } + if !isNeutralizing, !Self.acceptsWhileNotFrontmost(event, isPictureInPictureMode: view.isPictureInPictureMode, allowsBackgroundGamepad: view.allowsBackgroundGamepadInput) { - guard NSApplication.shared.isActive, view.window?.isKeyWindow == true else { return } + guard view.isFrontmostInputTarget else { return } } lastAcceptedStreamInputAt = Date() // The left button's edges bracket a text selection: everything between press and release is @@ -160,16 +162,13 @@ extension NativeNVSTHostViewModel { clipboard.pointerSelection.noteLeftButton(isPressed: isPressed, at: Date()) } if case .mouse = event { - if view.mouseInputMode == .relative, !view.isPointerLocked { + if view.mouseInputMode == .relative, !view.isPointerLocked, !isNeutralizing { // The seat asked for mouselook but the capture is not held — an association macOS // refused, or a focus change that released it. Dropping the event here is a mouse // that does nothing at all, so a real user event retries the capture: a transient // failure heals itself instead of stranding the session until the seat changes its // mind. A neutralizing release, background input, or the HUD must never retake it. - let canRetakeCapture = view.remoteInputEnabled - && view.isFrontmostInputTarget - && !NativeNVSTInputDispatcher.isNeutralizing(event) - guard canRetakeCapture else { return } + guard view.canAcquirePointerCapture else { return } view.setPointerLocked(true) guard view.isPointerLocked else { return } } diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift index b7fbcbbbe..cbfb21ba2 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Borderless.swift @@ -53,7 +53,8 @@ extension NativeNVSTHostViewModel { showNativeTransientStreamMessage(coverage == .fullScreen ? "Tiles fill the screen" : "Tiles below the menu bar") return } + let isLeavingBorderless = window.layoutState.isBorderless && !window.isBorderlessExitPending OPNStreamBorderless.toggleByReader(window) - showNativeTransientStreamMessage(window.layoutState.isBorderless ? "Borderless full screen" : "Leaving borderless full screen") + showNativeTransientStreamMessage(isLeavingBorderless ? "Leaving borderless full screen" : "Borderless full screen") } } diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift index 0ec74066b..3e62216af 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift @@ -361,9 +361,6 @@ extension NativeNVSTHostViewModel { // resuming into a session that cannot receive them - until the next path update. nativeView?.remoteInputEnabled = networkPathAvailable nativeView?.restoreInputFocus() - // Same gate as the line above, and for the same reason: `enablePointerLock` does not - // consult `remoteInputEnabled`, so an unguarded restore would take the pointer back - // during an outage, behind the recovery overlay. if restoreManualCapture, networkPathAvailable, nativeView?.isFrontmostInputTarget == true { nativeView?.setManualPointerCapture(true) } @@ -419,87 +416,6 @@ extension NativeNVSTHostViewModel { ) } - /// PiP is a mode of the stream window, so this is a window change and nothing else: one surface, - /// one session, never a second window and never a reparented view. - /// - /// Pressed while the window is full screen it does what the user meant - leave full screen, then - /// enter PiP - because the exit is animated and the mode change would otherwise land mid - /// animation, which is the same reason `enterNativeFullScreenWhenSessionReady` waits and retries. - func togglePictureInPicture() { - guard let window = nativeView?.window as? OPNStreamWindow, !isEnding, !didEnd else { return } - guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } - if window.layoutState.isBorderless { OPNStreamBorderless.releaseToReader(window) } - switch OPNStreamPictureInPicture.request( - isPictureInPicture: isPictureInPicture, - isFullScreen: window.styleMask.contains(.fullScreen), - isFullScreenTransitioning: isFullScreenTransitioning - ) { - case .leave: leavePictureInPicture(window) - case .leaveFullScreenThenEnter: leaveFullScreenThenEnterPictureInPicture(window, attempt: 0) - case .enter: enterPictureInPicture(window) - } - } - - /// The window is small and floating from here on, so every surface anchored to the window's top - /// corners has to go. The dock alone is 344pt wide in a 640pt window. - private func enterPictureInPicture(_ window: OPNStreamWindow) { - unifiedHUDVisible = false - isShortcutsHelpVisible = false - isHUDCustomizeVisible = false - nativeStatsVisible = false - closeHUDDropdown() - hudFocusID = nil - isHUDGamepadFocusVisible = false - if onScreenKeyboardVisible { setOnScreenKeyboardVisible(false) } - // Input stays live. The picture is in the game, so the mouse and keyboard have to keep - // reaching it while the window is small - the same gate the HUD uses on its way out, which - // is `networkPathAvailable` rather than an unconditional `true`: the network monitor blocks - // input while the path is down and `isConnected` stays true through a drop. - // - // The mode itself, though, must not hold the pointer: PiP is a cursor-sized picture, there - // is nothing to aim with, and a captured cursor is the one thing the user cannot use to - // reach the strip or whatever else they moved to. Setting the mode releases it, and keeps - // the controller alive while the window is not frontmost. - nativeView?.isPictureInPictureMode = true - nativeView?.remoteInputEnabled = networkPathAvailable - nativeView?.restoreInputFocus() - let aspectRatio = CGFloat(OPNStreamPreferences.launchProfile(forGame: configuration.applicationID, capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio) - OPNStreamPictureInPicture.enter(window, aspectRatio: aspectRatio) - isPictureInPicture = true - showNativeTransientStreamMessage("Picture in Picture") - OPNStreamTelemetry.capture("nvst.ui.pip.enter", level: .info, message: "Native NVST stream entered Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) - } - - private func leavePictureInPicture(_ window: OPNStreamWindow) { - OPNStreamPictureInPicture.exit(window) - isPictureInPicture = false - nativeView?.isPictureInPictureMode = false - nativeView?.remoteInputEnabled = networkPathAvailable - nativeView?.restoreInputFocus() - OPNStreamTelemetry.capture("nvst.ui.pip.exit", level: .info, message: "Native NVST stream left Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) - } - - /// Full screen first, PiP second, one retry beat at a time. Mirrors the session-ready helper - /// below: the exit is animated and AppKit refuses a second style-mask change while it runs. - private func leaveFullScreenThenEnterPictureInPicture(_ window: OPNStreamWindow, attempt: Int) { - guard attempt < Self.sessionReadyFullScreenAttemptLimit else { return } - Task { @MainActor [weak self] in - try? await Task.sleep(for: NativeNVSTHostViewModel.sessionReadyFullScreenRetryDelay) - guard let self, !self.isEnding, !self.didEnd else { return } - guard let window = self.nativeView?.window as? OPNStreamWindow else { return } - if self.isFullScreenTransitioning || StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) { - self.leaveFullScreenThenEnterPictureInPicture(window, attempt: attempt + 1) - return - } - if window.styleMask.contains(.fullScreen) { - window.toggleFullScreen(nil) - self.leaveFullScreenThenEnterPictureInPicture(window, attempt: attempt + 1) - return - } - self.enterPictureInPicture(window) - } - } - func startNativeStatsPolling(path: NativeNVSTStreamingPath) { nativeStatsTask?.cancel() nativeStatsTask = Task { diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift b/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift new file mode 100644 index 000000000..7ee6544fb --- /dev/null +++ b/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift @@ -0,0 +1,96 @@ +// swiftlint:disable:next no_appkit_in_view_model +import AppKit +import Foundation + +@MainActor +extension NativeNVSTHostViewModel { + func togglePictureInPicture() { + guard let window = nativeView?.window as? OPNStreamWindow, !isEnding, !didEnd else { return } + if window.isPictureInPicturePending { + OPNStreamPictureInPicture.exit(window) + return + } + guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } + switch OPNStreamPictureInPicture.request( + isPictureInPicture: isPictureInPicture, + isFullScreen: window.styleMask.contains(.fullScreen), + isFullScreenTransitioning: isFullScreenTransitioning + ) { + case .leave: leavePictureInPicture(window) + case .leaveFullScreenThenEnter: + let requestID = UUID() + window.pictureInPictureRequestID = requestID + window.layoutMembershipDidChange?() + leaveFullScreenThenEnterPictureInPicture(window, requestID: requestID, attempt: 0) + case .enter: enterPictureInPicture(window) + } + } + + private func enterPictureInPicture(_ window: OPNStreamWindow) { + let aspectRatio = CGFloat(OPNStreamPreferences.launchProfile(forGame: configuration.applicationID, capabilities: OPNStreamPreferences.loadDeviceCapabilities()).aspectRatio) + OPNStreamPictureInPicture.enter(window, aspectRatio: aspectRatio, prepareForEntry: { [weak self, weak window] in + guard let self, let window, !self.isEnding, !self.didEnd, self.nativeView?.window === window else { return false } + self.preparePictureInPictureEntry() + return true + }, completion: { [weak self] in self?.completePictureInPictureEntry() }) + } + + private func preparePictureInPictureEntry() { + unifiedHUDVisible = false + isShortcutsHelpVisible = false + isHUDCustomizeVisible = false + nativeStatsVisible = false + closeHUDDropdown() + hudFocusID = nil + isHUDGamepadFocusVisible = false + if onScreenKeyboardVisible { setOnScreenKeyboardVisible(false) } + nativeView?.isPictureInPictureMode = true + nativeView?.remoteInputEnabled = networkPathAvailable + nativeView?.restoreInputFocus() + } + + private func completePictureInPictureEntry() { + isPictureInPicture = true + showNativeTransientStreamMessage("Picture in Picture") + OPNStreamTelemetry.capture("nvst.ui.pip.enter", level: .info, message: "Native NVST stream entered Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) + } + + private func leavePictureInPicture(_ window: OPNStreamWindow) { + OPNStreamPictureInPicture.exit(window) + isPictureInPicture = false + nativeView?.isPictureInPictureMode = false + nativeView?.remoteInputEnabled = networkPathAvailable + nativeView?.restoreInputFocus() + OPNStreamTelemetry.capture("nvst.ui.pip.exit", level: .info, message: "Native NVST stream left Picture in Picture.", attributes: ["applicationID": configuration.applicationID]) + } + + private func leaveFullScreenThenEnterPictureInPicture(_ window: OPNStreamWindow, requestID: UUID, attempt: Int) { + guard attempt < Self.sessionReadyFullScreenAttemptLimit else { + if window.pictureInPictureRequestID == requestID { + window.pictureInPictureRequestID = nil + window.layoutMembershipDidChange?() + } + return + } + Task { @MainActor [weak self, weak window] in + try? await Task.sleep(for: NativeNVSTHostViewModel.sessionReadyFullScreenRetryDelay) + guard let window, window.pictureInPictureRequestID == requestID else { return } + guard let self, !self.isEnding, !self.didEnd, !window.isPresentationEnding, + self.nativeView?.window === window else { + window.pictureInPictureRequestID = nil + window.layoutMembershipDidChange?() + return + } + if self.isFullScreenTransitioning || StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) { + self.leaveFullScreenThenEnterPictureInPicture(window, requestID: requestID, attempt: attempt + 1) + return + } + if window.styleMask.contains(.fullScreen) { + window.toggleFullScreen(nil) + self.leaveFullScreenThenEnterPictureInPicture(window, requestID: requestID, attempt: attempt + 1) + return + } + self.enterPictureInPicture(window) + } + } +} From e12d89d995dd0b570b2b3aae245c32b68b1c7957 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 17:50:25 +0100 Subject: [PATCH 15/22] fix: retile the remaining co-op windows when one is taken by the reader or goes to PiP, and don't restore detached session windows Co-Authored-By: Claude Sonnet 5.5 --- OPNApp.swift | 1 + Tests/Stream/CouchCoopBorderlessTests.swift | 6 +++--- View/Stream/OPNStreamWindowPresenter.swift | 16 +++++++++++----- .../NativeNVSTHostViewModel+Controls.swift | 5 ++++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/OPNApp.swift b/OPNApp.swift index d83f5d8d8..6f40e0ead 100644 --- a/OPNApp.swift +++ b/OPNApp.swift @@ -317,6 +317,7 @@ struct OPNApp: App { .environmentObject(systemAppearance) } .defaultSize(width: 1100, height: 680) + .restorationBehavior(.disabled) .modelContainer(sharedModelContainer) Window("Join Remote Co-Op", id: "remote-coop-guest") { diff --git a/Tests/Stream/CouchCoopBorderlessTests.swift b/Tests/Stream/CouchCoopBorderlessTests.swift index 9c20718f4..e73d5b1f5 100644 --- a/Tests/Stream/CouchCoopBorderlessTests.swift +++ b/Tests/Stream/CouchCoopBorderlessTests.swift @@ -139,7 +139,7 @@ import Testing let window = OPNStreamWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled], backing: .buffered, defer: true) window.layoutState.isAutoTiled = true - OPNStreamWindowPresenter.noteFrameChange(window) + #expect(!OPNStreamWindowPresenter.noteFrameChange(window)) #expect(window.layoutState.isAutoTiled) #expect(!window.layoutState.isManuallyPlaced) @@ -150,11 +150,11 @@ import Testing window.layoutState.isAutoTiled = true window.setFrameFromLayout(NSRect(x: 50, y: 50, width: 640, height: 360)) - OPNStreamWindowPresenter.noteFrameChange(window) + #expect(!OPNStreamWindowPresenter.noteFrameChange(window)) #expect(window.layoutState.isAutoTiled) window.setFrame(NSRect(x: 90, y: 50, width: 640, height: 360), display: false) - OPNStreamWindowPresenter.noteFrameChange(window) + #expect(OPNStreamWindowPresenter.noteFrameChange(window)) #expect(!window.layoutState.isAutoTiled) #expect(window.layoutState.isManuallyPlaced) diff --git a/View/Stream/OPNStreamWindowPresenter.swift b/View/Stream/OPNStreamWindowPresenter.swift index 2bcdf9ce1..5bf81345e 100644 --- a/View/Stream/OPNStreamWindowPresenter.swift +++ b/View/Stream/OPNStreamWindowPresenter.swift @@ -363,8 +363,11 @@ final class OPNStreamWindowPresenter { } } let layoutTokens = [NSWindow.didMoveNotification, NSWindow.didResizeNotification].map { name in - center.addObserver(forName: name, object: window, queue: .main) { [weak window] _ in - MainActor.assumeIsolated { window.map(OPNStreamWindowPresenter.noteFrameChange) } + center.addObserver(forName: name, object: window, queue: .main) { [weak self, weak window] _ in + MainActor.assumeIsolated { + guard let window, OPNStreamWindowPresenter.noteFrameChange(window) else { return } + self?.retileWindows() + } } } presentation.observerTokens = [willEnter, didEnter, willExit, didExit] + layoutTokens @@ -373,14 +376,17 @@ final class OPNStreamWindowPresenter { } /// A tiled window that is no longer where the layout put it was taken by the reader, and stays - /// theirs: the layout stops placing it. - static func noteFrameChange(_ window: OPNStreamWindow) { + /// theirs: the layout stops placing it. Returns whether the window was taken, so the caller can + /// retile the rest. + @discardableResult + static func noteFrameChange(_ window: OPNStreamWindow) -> Bool { // No layout frame yet means the placement is still waiting on the geometry gate, so a move // that lands before it is not the reader's. - guard window.layoutState.isAutoTiled, window.layoutFrame != nil, !window.isAtLayoutFrame else { return } + guard window.layoutState.isAutoTiled, window.layoutFrame != nil, !window.isAtLayoutFrame else { return false } window.layoutState.isAutoTiled = false window.layoutState.isManuallyPlaced = true window.clearLayoutFrame() + return true } private func setTransition(_ transition: FullScreenTransition, for id: ObjectIdentifier) { diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift index 0ec74066b..b845df98a 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+Controls.swift @@ -428,7 +428,10 @@ extension NativeNVSTHostViewModel { func togglePictureInPicture() { guard let window = nativeView?.window as? OPNStreamWindow, !isEnding, !didEnd else { return } guard !StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) else { return } - if window.layoutState.isBorderless { OPNStreamBorderless.releaseToReader(window) } + if window.layoutState.isBorderless { + OPNStreamBorderless.releaseToReader(window) + OPNStreamWindowPresenter.shared.retileWindows() + } switch OPNStreamPictureInPicture.request( isPictureInPicture: isPictureInPicture, isFullScreen: window.styleMask.contains(.fullScreen), From bdc86da5ebc3573a1f402781fc235b2af09cda9d Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Sun, 25 Oct 2026 18:02:16 +0100 Subject: [PATCH 16/22] test: cover retiling when a tiled window is taken by the reader or goes to Picture in Picture Co-Authored-By: Claude Sonnet 5.5 --- .../Stream/StreamWindowConcurrencyTests.swift | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/Tests/Stream/StreamWindowConcurrencyTests.swift b/Tests/Stream/StreamWindowConcurrencyTests.swift index eecdccf80..f9b204541 100644 --- a/Tests/Stream/StreamWindowConcurrencyTests.swift +++ b/Tests/Stream/StreamWindowConcurrencyTests.swift @@ -115,6 +115,71 @@ import Testing #expect(!windowA.layoutState.isBorderless) } + /// The reader taking one tiled window hands the layout back: the survivor is the only placeable + /// window left, which is not a layout, so it goes back to its own placement too. + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func movingOneTiledWindowRetilesTheOther() async throws { + try await withTiledPair { windowA, windowB in + windowA.setFrame(windowA.frame.offsetBy(dx: 40, dy: -40), display: false) + try await Task.sleep(for: .milliseconds(300)) + + #expect(windowA.layoutState.isManuallyPlaced) + #expect(!windowA.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isBorderless) + } + } + + /// Sending a tiled window to Picture in Picture releases it the way the HUD does, and the other + /// window is retiled rather than left holding a half-screen frame. + @Test(.disabled(if: CIWindowTestGate.isHostedRunner, Comment(rawValue: CIWindowTestGate.skipReason))) + func releasingATiledWindowToPictureInPictureRetilesTheOther() async throws { + try await withTiledPair { windowA, windowB in + OPNStreamBorderless.releaseToReader(windowA) + windowA.isPictureInPicture = true + OPNStreamWindowPresenter.shared.retileWindows() + try await Task.sleep(for: .milliseconds(300)) + + #expect(!windowA.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isAutoTiled) + #expect(!windowB.layoutState.isBorderless) + } + } + + /// Runs `body` with two streams tiled by couch co-op, and puts the flag and sessions back after. + private func withTiledPair(_ body: (OPNStreamWindow, OPNStreamWindow) async throws -> Void) async throws { + let previousFlag = OPNLabs.isCouchCoopEnabled + defer { OPNLabs.setEnabled(OPNLabs.couchCoop, previousFlag) } + let registry = OPNGameSessionRegistry.shared + let accountA = makeLoginAccountForTesting(email: "a@example.com", userId: "user-a") + let accountB = makeLoginAccountForTesting(email: "b@example.com", userId: "user-b", isActive: false) + let sessionA = makeLoginSessionForTesting(accountEmail: accountA.email, id: "retile-a", userId: "user-a") + let sessionB = makeLoginSessionForTesting(accountEmail: accountB.email, id: "retile-b", userId: "user-b") + defer { + sessionA.purgeTokens() + sessionB.purgeTokens() + } + let gameA = try #require(begin(registry: registry, account: accountA, session: sessionA)) + let gameB = try #require(begin(registry: registry, account: accountB, session: sessionB)) + defer { + OPNStreamWindowPresenter.shared.dismiss(gameA.id) + OPNStreamWindowPresenter.shared.dismiss(gameB.id) + registry.end(gameA) + registry.end(gameB) + } + gameA.configuration = Self.configuration(title: "Cyberpunk 2077") + gameB.configuration = Self.configuration(title: "Manor Lords") + + OPNLabs.setEnabled(OPNLabs.couchCoop, true) + OPNStreamWindowPresenter.shared.syncWithOwnedSessions() + let windowA = try #require(OPNStreamWindowPresenter.shared.windows[gameA.id]) + let windowB = try #require(OPNStreamWindowPresenter.shared.windows[gameB.id]) + try await Task.sleep(for: .milliseconds(300)) + try #require(windowA.layoutState.isAutoTiled && windowB.layoutState.isAutoTiled) + + try await body(windowA, windowB) + } + private static func configuration(title: String) -> StreamLaunchConfiguration { StreamLaunchConfiguration( title: title, From 60884b6f7c3eb37085f3e732d5daa59928f6ecad Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Mon, 26 Oct 2026 06:36:54 +0100 Subject: [PATCH 17/22] fix: send an account switch to sign-in when its saved tokens are rejected, and fall back to the refresh token Switching accounts from the catalog did nothing when the saved session was rejected with a 401: the failure message only shows on the login wall, so the click looked like a reload. The switch now opens sign-in for that account. A rejected client token also no longer ends the refresh. The refresh token is tried next, and only when both are rejected is the account treated as signed out. Co-Authored-By: Claude Haiku 5.5 --- GFN/Jarvis/JarvisAuthService.swift | 20 ++++++++++++------- Tests/GFN/Jarvis/JarvisTests.swift | 31 ++++++++++++++++++++++++++++++ ViewModel/LoginViewModel.swift | 17 +++++++++++++--- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/GFN/Jarvis/JarvisAuthService.swift b/GFN/Jarvis/JarvisAuthService.swift index 2ddee6aa9..4f73a68f1 100644 --- a/GFN/Jarvis/JarvisAuthService.swift +++ b/GFN/Jarvis/JarvisAuthService.swift @@ -138,13 +138,19 @@ public actor JarvisAuthService { guard isSessionRefreshable else { throw JarvisAuthError.noRefreshMechanism } if !session.clientToken.isEmpty { let body = JarvisOAuthRequestFactory.clientTokenGrantBody(clientToken: session.clientToken, userId: session.userId, configuration: configuration) - let refreshed = merge(saved: session, refreshed: try await requestSession(body: body, operation: .getSessionToken)) - let enriched = try await ensureClientToken(refreshed) - setSessionInMemory(enriched) - try await persistSessionIfAutomatic(enriched) - telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "success", "grant_type": "client_token"]) - span.finish(success: true) - return enriched + do { + let refreshed = merge(saved: session, refreshed: try await requestSession(body: body, operation: .getSessionToken)) + let enriched = try await ensureClientToken(refreshed) + setSessionInMemory(enriched) + try await persistSessionIfAutomatic(enriched) + telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "success", "grant_type": "client_token"]) + span.finish(success: true) + return enriched + } catch let error as JarvisAuthError where error.category == .authorization && !session.refreshToken.isEmpty { + // A rejected client token does not say the refresh token is dead: the client token + // is the short-lived one. Only when that grant fails too is the account signed out. + telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "fallback", "grant_type": "client_token"]) + } } let refreshed = try await refreshWithOAuthToken() telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "success", "grant_type": "refresh_token"]) diff --git a/Tests/GFN/Jarvis/JarvisTests.swift b/Tests/GFN/Jarvis/JarvisTests.swift index 5799752aa..199da079d 100644 --- a/Tests/GFN/Jarvis/JarvisTests.swift +++ b/Tests/GFN/Jarvis/JarvisTests.swift @@ -187,6 +187,37 @@ private func jsonBody(_ request: URLRequest) throws -> [String: Any] { #expect(await authService.status == .authorizationError) } +@Test func jarvisAuthServiceFallsBackToTheRefreshTokenWhenTheClientTokenIsRejected() async throws { + let session = JarvisSession( + accessToken: "access", + refreshToken: "refresh", + userId: "user", + isAuthenticated: true, + clientToken: "client", + accessTokenExpiry: JarvisSession.currentEpochMs() - 1_000 + ) + let transport = SequencedJarvisTransport([ + .success((status: 401, json: ["error": "unauthorized"])), + .success((status: 200, json: ["access_token": "new-access", "refresh_token": "new-refresh", "expires_in": 3600])), + .success((status: 200, json: ["client_token": "new-client", "expires_in": 240])), + ]) + let service = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: transport, session: session) + let refreshed = try await service.refreshSession(force: true) + #expect(refreshed.accessToken == "new-access") + #expect(refreshed.clientToken == "new-client") + #expect(await transport.requestCount == 3) + + // Both grants rejected: the error still surfaces so the caller can send the user to sign in. + let rejecting = SequencedJarvisTransport([ + .success((status: 401, json: ["error": "unauthorized"])), + .success((status: 401, json: ["error": "unauthorized"])), + ]) + let deadService = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: rejecting, session: session) + await #expect(throws: JarvisAuthError.httpStatus(401)) { + _ = try await deadService.refreshSession(force: true) + } +} + @Test func jarvisOAuthCallbackValidationMatchesVendorStateFlow() async throws { let service = JarvisAuthService(transport: MockJarvisTransport { _ in [:] }) let callback = try await service.parseCallback(query: "code=abc&state=expected", expectedState: "expected") diff --git a/ViewModel/LoginViewModel.swift b/ViewModel/LoginViewModel.swift index 4da88dbba..7bc6b1a59 100644 --- a/ViewModel/LoginViewModel.swift +++ b/ViewModel/LoginViewModel.swift @@ -242,7 +242,18 @@ final class LoginViewModel: ObservableObject { return } Task { - guard await restoreAccountSession(account) else { return } + // A sign-out or cancellation during the restore advances this; that outcome is the + // user's, not a dead credential, and must not raise a sign-in. + let generation = loginLaunchGeneration + guard await restoreAccountSession(account) else { + // The saved tokens can be rejected (an HTTP 401 refresh) even though the row looked + // restorable. The restore's message lands on the login wall, which this catalog + // switch never shows, so without this the click looks like a reload that changed nothing. + if generation == loginLaunchGeneration, !account.isActive { + beginReauthentication(for: account, because: "\(account.displayName)'s saved session was rejected. Sign in again to switch to it.") + } + return + } announceRunningGameContinues(with: account) } } @@ -270,11 +281,11 @@ final class LoginViewModel: ObservableObject { /// Another account is still signed in, so cancelling the re-sign-in has a session to return to. var canCancelReauthentication: Bool { activeSession != nil } - func beginReauthentication(for account: LoginAccount) { + func beginReauthentication(for account: LoginAccount, because reason: String? = nil) { selectRememberedAccount(account) refreshLoginProviders() successMessage = "" - validationMessage = "\(account.displayName) is signed out. Sign in again to switch to it." + validationMessage = reason ?? "\(account.displayName) is signed out. Sign in again to switch to it." signInRequest = .reauthenticate(email: account.email) OPNLog.info(.auth, "Account switch needs re-authentication account=\(account.email)") } From 48d04f803bcacfd32b164790c9aa9c1a6bd1ad10 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Mon, 26 Oct 2026 06:52:13 +0100 Subject: [PATCH 18/22] fix: drop the rejected client token before the refresh-token fallback, and only prompt sign-in for rejected credentials The fallback kept the rejected client token in the session, and the refresh-token merge copied it and its expiry back, so later refreshes could keep sending the rejected grant. It is now cleared first so a fresh one is minted. The restore now reports why it failed. Only an authorization failure or a missing saved session opens sign-in; a network failure shows a retry notice instead of claiming the session was rejected. Co-Authored-By: Claude Sonnet 5.5 --- GFN/Jarvis/JarvisAuthService.swift | 5 +++++ Tests/GFN/Jarvis/JarvisTests.swift | 3 +++ ViewModel/LoginAccountChooser.swift | 7 +++++- ViewModel/LoginOAuthFlow.swift | 34 +++++++++++++++++++++++------ ViewModel/LoginViewModel.swift | 17 ++++++++------- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/GFN/Jarvis/JarvisAuthService.swift b/GFN/Jarvis/JarvisAuthService.swift index 4f73a68f1..ee149dbc5 100644 --- a/GFN/Jarvis/JarvisAuthService.swift +++ b/GFN/Jarvis/JarvisAuthService.swift @@ -150,6 +150,11 @@ public actor JarvisAuthService { // A rejected client token does not say the refresh token is dead: the client token // is the short-lived one. Only when that grant fails too is the account signed out. telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "fallback", "grant_type": "client_token"]) + // Drop the rejected token so the refresh-token merge cannot copy it (and its expiry) + // back into the session; the fallback then mints a fresh one. + session.clientToken = "" + session.clientTokenExpiry = 0 + session.clientTokenExpiryLength = 0 } } let refreshed = try await refreshWithOAuthToken() diff --git a/Tests/GFN/Jarvis/JarvisTests.swift b/Tests/GFN/Jarvis/JarvisTests.swift index 199da079d..8067fa390 100644 --- a/Tests/GFN/Jarvis/JarvisTests.swift +++ b/Tests/GFN/Jarvis/JarvisTests.swift @@ -194,6 +194,8 @@ private func jsonBody(_ request: URLRequest) throws -> [String: Any] { userId: "user", isAuthenticated: true, clientToken: "client", + clientTokenExpiry: JarvisSession.currentEpochMs() + 3_600_000, + clientTokenExpiryLength: 3_600_000, accessTokenExpiry: JarvisSession.currentEpochMs() - 1_000 ) let transport = SequencedJarvisTransport([ @@ -204,6 +206,7 @@ private func jsonBody(_ request: URLRequest) throws -> [String: Any] { let service = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: transport, session: session) let refreshed = try await service.refreshSession(force: true) #expect(refreshed.accessToken == "new-access") + // The rejected token is still unexpired, so only dropping it makes the fallback mint a new one. #expect(refreshed.clientToken == "new-client") #expect(await transport.requestCount == 3) diff --git a/ViewModel/LoginAccountChooser.swift b/ViewModel/LoginAccountChooser.swift index 7c34e674e..611e1d044 100644 --- a/ViewModel/LoginAccountChooser.swift +++ b/ViewModel/LoginAccountChooser.swift @@ -51,7 +51,12 @@ extension LoginViewModel { let games = owners.count == 1 ? "Your current game continues using \(owners[0])." : "Your current games continue using \(owners.formatted(.list(type: .and)))." - accountSwitchNotice = "Browsing now uses \(account.displayName). \(games)" + showAccountSwitchNotice("Browsing now uses \(account.displayName). \(games)") + } + + /// Shows `text` in the switch notice and clears it after a while. + func showAccountSwitchNotice(_ text: String) { + accountSwitchNotice = text accountSwitchNoticeTask?.cancel() accountSwitchNoticeTask = Task { [weak self] in try? await Task.sleep(for: .seconds(12)) diff --git a/ViewModel/LoginOAuthFlow.swift b/ViewModel/LoginOAuthFlow.swift index 5290b86dc..f6d167b2a 100644 --- a/ViewModel/LoginOAuthFlow.swift +++ b/ViewModel/LoginOAuthFlow.swift @@ -164,8 +164,24 @@ extension LoginViewModel { } } + /// Why a restore did not leave the account signed in. Only `rejected` says the saved tokens are + /// dead; the others must not send the user through a fresh sign-in. + enum AccountRestoreOutcome: Equatable { + case restored + /// No saved session, or the auth server refused the saved grants. + case rejected + /// The refresh failed for a reason that says nothing about the tokens, such as no network. + case unavailable + /// A sign-out or cancellation ran during the restore. + case cancelled + } + @discardableResult func restoreAccountSession(_ account: LoginAccount) async -> Bool { + await restoreAccountSessionOutcome(account) == .restored + } + + func restoreAccountSessionOutcome(_ account: LoginAccount) async -> AccountRestoreOutcome { validationMessage = "" successMessage = "" email = account.email @@ -175,7 +191,7 @@ extension LoginViewModel { guard let storedSession = sessions.first(where: { $0.accountEmail == account.email && !$0.accessToken.isEmpty }) else { OPNLog.warning(.auth, "Session restore failed because no saved session exists for account=\(account.email)") validationMessage = "No saved session exists for this account. Sign in again." - return false + return .rejected } isAuthenticating = true @@ -210,25 +226,29 @@ extension LoginViewModel { let refreshed = try await jarvisAuthService.refreshSession(force: !jarvisSession.isIdTokenValid) guard generation == loginLaunchGeneration else { OPNLog.info(.auth, "Discarded a session refresh that completed after sign-out account=\(account.email)") - return false + return .cancelled } persistSignedInSession(session: refreshed, userInfo: nil, authMethod: Jarvis.Operation.getSessionToken.rawValue) successMessage = "Session refreshed for \(account.displayName)." OPNLog.info(.auth, "Session refreshed account=\(account.email)") - return true + return .restored } catch { if storedSession.canContinueOffline && !storedSession.isExpired { - guard generation == loginLaunchGeneration else { return false } + guard generation == loginLaunchGeneration else { return .cancelled } markActive(accountEmail: account.email) trySave() refreshSignedOutAccounts() successMessage = "Using saved offline session for \(account.displayName)." OPNLog.warning(.auth, "Using offline saved session account=\(account.email) refreshError=\(error.localizedDescription)") - return true + return .restored } else { - validationMessage = "Saved session expired. Sign in again." OPNLog.warning(.auth, "Session restore failed account=\(account.email) error=\(error.localizedDescription)") - return false + if (error as? JarvisAuthError)?.category == .authorization { + validationMessage = "Saved session expired. Sign in again." + return .rejected + } + validationMessage = "Couldn't refresh the saved session for \(account.displayName). Check your connection and try again." + return .unavailable } } } diff --git a/ViewModel/LoginViewModel.swift b/ViewModel/LoginViewModel.swift index 7bc6b1a59..aeb239a25 100644 --- a/ViewModel/LoginViewModel.swift +++ b/ViewModel/LoginViewModel.swift @@ -242,15 +242,16 @@ final class LoginViewModel: ObservableObject { return } Task { - // A sign-out or cancellation during the restore advances this; that outcome is the - // user's, not a dead credential, and must not raise a sign-in. - let generation = loginLaunchGeneration - guard await restoreAccountSession(account) else { - // The saved tokens can be rejected (an HTTP 401 refresh) even though the row looked - // restorable. The restore's message lands on the login wall, which this catalog - // switch never shows, so without this the click looks like a reload that changed nothing. - if generation == loginLaunchGeneration, !account.isActive { + let outcome = await restoreAccountSessionOutcome(account) + guard outcome == .restored else { + // Only dead tokens warrant a sign-in. A cancellation is the user's own, and an + // unreachable server says nothing about the tokens. The restore's message lands on + // the login wall, which this catalog switch never shows, so a rejection has to raise + // the sign-in itself or the click looks like a reload that changed nothing. + if outcome == .rejected { beginReauthentication(for: account, because: "\(account.displayName)'s saved session was rejected. Sign in again to switch to it.") + } else if outcome == .unavailable { + showAccountSwitchNotice(validationMessage) } return } From f8db63354b7c4ac52d0b9de1a6515e39c58e313f Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Wed, 28 Oct 2026 07:11:56 +0100 Subject: [PATCH 19/22] fix: stop the refresh-token fallback from acting on another account after a mid-refresh switch Co-Authored-By: Claude Sonnet 5.5 --- GFN/Jarvis/JarvisAuthService.swift | 6 +++++ Tests/GFN/Jarvis/JarvisTests.swift | 39 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/GFN/Jarvis/JarvisAuthService.swift b/GFN/Jarvis/JarvisAuthService.swift index ee149dbc5..fb0887786 100644 --- a/GFN/Jarvis/JarvisAuthService.swift +++ b/GFN/Jarvis/JarvisAuthService.swift @@ -137,6 +137,7 @@ public actor JarvisAuthService { } guard isSessionRefreshable else { throw JarvisAuthError.noRefreshMechanism } if !session.clientToken.isEmpty { + let initiating = session let body = JarvisOAuthRequestFactory.clientTokenGrantBody(clientToken: session.clientToken, userId: session.userId, configuration: configuration) do { let refreshed = merge(saved: session, refreshed: try await requestSession(body: body, operation: .getSessionToken)) @@ -147,6 +148,11 @@ public actor JarvisAuthService { span.finish(success: true) return enriched } catch let error as JarvisAuthError where error.category == .authorization && !session.refreshToken.isEmpty { + // The actor can switch accounts while the request is in flight; the fallback must not + // clear or refresh the other account's tokens on behalf of this caller. + guard session.userId == initiating.userId, session.refreshToken == initiating.refreshToken else { + throw CancellationError() + } // A rejected client token does not say the refresh token is dead: the client token // is the short-lived one. Only when that grant fails too is the account signed out. telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "fallback", "grant_type": "client_token"]) diff --git a/Tests/GFN/Jarvis/JarvisTests.swift b/Tests/GFN/Jarvis/JarvisTests.swift index 8067fa390..f7145753b 100644 --- a/Tests/GFN/Jarvis/JarvisTests.swift +++ b/Tests/GFN/Jarvis/JarvisTests.swift @@ -221,6 +221,45 @@ private func jsonBody(_ request: URLRequest) throws -> [String: Any] { } } +private actor AccountSwitchingJarvisTransport: JarvisHTTPTransport { + private(set) var count = 0 + var onFirstRequest: (@Sendable () async -> Void)? + + func setOnFirstRequest(_ action: @escaping @Sendable () async -> Void) { onFirstRequest = action } + + func send(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) { + count += 1 + if count == 1 { await onFirstRequest?() } + let data = try JSONSerialization.data(withJSONObject: ["error": "unauthorized"]) + return (data, HTTPURLResponse(url: request.url ?? URL(string: "https://login.nvidia.com")!, statusCode: 401, httpVersion: nil, headerFields: nil)!) + } +} + +@Test func jarvisAuthServiceDoesNotFallBackOntoAnotherAccountAfterASwitchMidRefresh() async throws { + func makeSession(_ id: String) -> JarvisSession { + JarvisSession( + accessToken: "access-\(id)", + refreshToken: "refresh-\(id)", + userId: id, + isAuthenticated: true, + clientToken: "client-\(id)", + clientTokenExpiry: JarvisSession.currentEpochMs() + 3_600_000, + clientTokenExpiryLength: 3_600_000, + accessTokenExpiry: JarvisSession.currentEpochMs() - 1_000 + ) + } + let transport = AccountSwitchingJarvisTransport() + let service = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: transport, session: makeSession("a")) + let other = makeSession("b") + await transport.setOnFirstRequest { await service.setSession(other) } + await #expect(throws: CancellationError.self) { + _ = try await service.refreshSession(force: true) + } + // Account B keeps its client token and no refresh request was spent on it. + #expect(await service.session.clientToken == "client-b") + #expect(await transport.count == 1) +} + @Test func jarvisOAuthCallbackValidationMatchesVendorStateFlow() async throws { let service = JarvisAuthService(transport: MockJarvisTransport { _ in [:] }) let callback = try await service.parseCallback(query: "code=abc&state=expected", expectedState: "expected") From 98c11dc377e452846dc39690e243c538408df590 Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Wed, 28 Oct 2026 07:13:21 +0100 Subject: [PATCH 20/22] fix: treat a 400 invalid_grant as a rejected grant, and drop explanatory comments flagged in review Co-Authored-By: Claude Sonnet 5.5 --- GFN/Jarvis/JarvisAuthService.swift | 15 ++++++++------- Tests/GFN/Jarvis/JarvisTests.swift | 13 +++++++++++-- Tests/Stream/OPNStreamPictureInPictureTests.swift | 2 -- ViewModel/LoginViewModel.swift | 4 ---- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/GFN/Jarvis/JarvisAuthService.swift b/GFN/Jarvis/JarvisAuthService.swift index fb0887786..d9c9b264f 100644 --- a/GFN/Jarvis/JarvisAuthService.swift +++ b/GFN/Jarvis/JarvisAuthService.swift @@ -148,16 +148,10 @@ public actor JarvisAuthService { span.finish(success: true) return enriched } catch let error as JarvisAuthError where error.category == .authorization && !session.refreshToken.isEmpty { - // The actor can switch accounts while the request is in flight; the fallback must not - // clear or refresh the other account's tokens on behalf of this caller. guard session.userId == initiating.userId, session.refreshToken == initiating.refreshToken else { throw CancellationError() } - // A rejected client token does not say the refresh token is dead: the client token - // is the short-lived one. Only when that grant fails too is the account signed out. telemetry.recordCounter(name: "jarvis.auth.refresh.count", attributes: ["outcome": "fallback", "grant_type": "client_token"]) - // Drop the rejected token so the refresh-token merge cannot copy it (and its expiry) - // back into the session; the fallback then mints a fresh one. session.clientToken = "" session.clientTokenExpiry = 0 session.clientTokenExpiryLength = 0 @@ -540,7 +534,7 @@ extension JarvisAuthService { } guard response.statusCode == 200 else { - let error = JarvisAuthError.httpStatus(response.statusCode) + let error = Self.rejectedGrantError(statusCode: response.statusCode, body: data) ?? JarvisAuthError.httpStatus(response.statusCode) telemetry.recordError(error, operation: operation, attributes: ["status_code": String(response.statusCode), "category": error.category.rawValue]) throw error } @@ -551,6 +545,13 @@ extension JarvisAuthService { return json } + private static func rejectedGrantError(statusCode: Int, body: Data) -> JarvisAuthError? { + guard statusCode == 400, + let json = try? JSONSerialization.jsonObject(with: body) as? [String: Any], + json["error"] as? String == "invalid_grant" else { return nil } + return .oauthError("invalid_grant") + } + private func parseUserInfo(_ json: [String: Any], isNetworkCall: Bool) -> JarvisUserInfo { let consent = json["consent"] as? [String: String] ?? [:] let userId = json["sub"] as? String ?? (json["userId"] as? String ?? "") diff --git a/Tests/GFN/Jarvis/JarvisTests.swift b/Tests/GFN/Jarvis/JarvisTests.swift index f7145753b..36af0bcdd 100644 --- a/Tests/GFN/Jarvis/JarvisTests.swift +++ b/Tests/GFN/Jarvis/JarvisTests.swift @@ -206,11 +206,9 @@ private func jsonBody(_ request: URLRequest) throws -> [String: Any] { let service = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: transport, session: session) let refreshed = try await service.refreshSession(force: true) #expect(refreshed.accessToken == "new-access") - // The rejected token is still unexpired, so only dropping it makes the fallback mint a new one. #expect(refreshed.clientToken == "new-client") #expect(await transport.requestCount == 3) - // Both grants rejected: the error still surfaces so the caller can send the user to sign in. let rejecting = SequencedJarvisTransport([ .success((status: 401, json: ["error": "unauthorized"])), .success((status: 401, json: ["error": "unauthorized"])), @@ -219,6 +217,17 @@ private func jsonBody(_ request: URLRequest) throws -> [String: Any] { await #expect(throws: JarvisAuthError.httpStatus(401)) { _ = try await deadService.refreshSession(force: true) } + #expect(await rejecting.requestCount == 2) + + let invalidGrant = SequencedJarvisTransport([ + .success((status: 400, json: ["error": "invalid_grant"])), + .success((status: 200, json: ["access_token": "new-access", "refresh_token": "new-refresh", "expires_in": 3600])), + .success((status: 200, json: ["client_token": "new-client", "expires_in": 240])), + ]) + let grantService = JarvisAuthService(retryPolicy: JarvisRetryPolicy(maxRetries: 0, baseDelayMs: 0), transport: invalidGrant, session: session) + let regranted = try await grantService.refreshSession(force: true) + #expect(regranted.accessToken == "new-access") + #expect(await invalidGrant.requestCount == 3) } private actor AccountSwitchingJarvisTransport: JarvisHTTPTransport { diff --git a/Tests/Stream/OPNStreamPictureInPictureTests.swift b/Tests/Stream/OPNStreamPictureInPictureTests.swift index e758e408d..2a1779c0e 100644 --- a/Tests/Stream/OPNStreamPictureInPictureTests.swift +++ b/Tests/Stream/OPNStreamPictureInPictureTests.swift @@ -196,8 +196,6 @@ struct OPNStreamPictureInPictureTests { window.makeKeyAndOrderFront(nil) await settleRunLoop() - // What the close button sends. `performClose(_:)` itself is not driven here: on macOS 26 it - // ends the test host's process once the run loop turns, taking the rest of the run with it. #expect(window.delegate?.windowShouldClose?(window) == false) await settleRunLoop() diff --git a/ViewModel/LoginViewModel.swift b/ViewModel/LoginViewModel.swift index aeb239a25..b69c11522 100644 --- a/ViewModel/LoginViewModel.swift +++ b/ViewModel/LoginViewModel.swift @@ -244,10 +244,6 @@ final class LoginViewModel: ObservableObject { Task { let outcome = await restoreAccountSessionOutcome(account) guard outcome == .restored else { - // Only dead tokens warrant a sign-in. A cancellation is the user's own, and an - // unreachable server says nothing about the tokens. The restore's message lands on - // the login wall, which this catalog switch never shows, so a rejection has to raise - // the sign-in itself or the click looks like a reload that changed nothing. if outcome == .rejected { beginReauthentication(for: account, because: "\(account.displayName)'s saved session was rejected. Sign in again to switch to it.") } else if outcome == .unavailable { From 35e54423ce6d3129044256d1fbf2db971c64f92a Mon Sep 17 00:00:00 2001 From: Sheldon Els Date: Wed, 28 Oct 2026 08:21:12 +0100 Subject: [PATCH 21/22] fix: bring the Jarvis auth service and account restore back under the lint length limits Co-Authored-By: Claude Sonnet 5.5 --- GFN/Jarvis/JarvisAuthService.swift | 9 +----- GFN/Jarvis/JarvisService.swift | 7 +++++ ViewModel/LoginOAuthFlow.swift | 45 +++++++++++++++++------------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/GFN/Jarvis/JarvisAuthService.swift b/GFN/Jarvis/JarvisAuthService.swift index d9c9b264f..dd5904ef7 100644 --- a/GFN/Jarvis/JarvisAuthService.swift +++ b/GFN/Jarvis/JarvisAuthService.swift @@ -534,7 +534,7 @@ extension JarvisAuthService { } guard response.statusCode == 200 else { - let error = Self.rejectedGrantError(statusCode: response.statusCode, body: data) ?? JarvisAuthError.httpStatus(response.statusCode) + let error = JarvisAuthError.rejectedGrant(statusCode: response.statusCode, body: data) ?? JarvisAuthError.httpStatus(response.statusCode) telemetry.recordError(error, operation: operation, attributes: ["status_code": String(response.statusCode), "category": error.category.rawValue]) throw error } @@ -545,13 +545,6 @@ extension JarvisAuthService { return json } - private static func rejectedGrantError(statusCode: Int, body: Data) -> JarvisAuthError? { - guard statusCode == 400, - let json = try? JSONSerialization.jsonObject(with: body) as? [String: Any], - json["error"] as? String == "invalid_grant" else { return nil } - return .oauthError("invalid_grant") - } - private func parseUserInfo(_ json: [String: Any], isNetworkCall: Bool) -> JarvisUserInfo { let consent = json["consent"] as? [String: String] ?? [:] let userId = json["sub"] as? String ?? (json["userId"] as? String ?? "") diff --git a/GFN/Jarvis/JarvisService.swift b/GFN/Jarvis/JarvisService.swift index ab4bcf772..23f60adb8 100644 --- a/GFN/Jarvis/JarvisService.swift +++ b/GFN/Jarvis/JarvisService.swift @@ -466,6 +466,13 @@ public enum JarvisAuthError: LocalizedError, Equatable, Sendable { } } + static func rejectedGrant(statusCode: Int, body: Data) -> JarvisAuthError? { + guard statusCode == 400, + let json = try? JSONSerialization.jsonObject(with: body) as? [String: Any], + json["error"] as? String == "invalid_grant" else { return nil } + return .oauthError("invalid_grant") + } + public static func transportFailure(_ error: Error) -> JarvisAuthError { let nsError = error as NSError let category: JarvisAuthFailureCategory diff --git a/ViewModel/LoginOAuthFlow.swift b/ViewModel/LoginOAuthFlow.swift index f6d167b2a..20a57b4f5 100644 --- a/ViewModel/LoginOAuthFlow.swift +++ b/ViewModel/LoginOAuthFlow.swift @@ -181,25 +181,7 @@ extension LoginViewModel { await restoreAccountSessionOutcome(account) == .restored } - func restoreAccountSessionOutcome(_ account: LoginAccount) async -> AccountRestoreOutcome { - validationMessage = "" - successMessage = "" - email = account.email - selectedProvider = providerOption(idpId: account.providerIdpId, fallbackName: account.providerName) - rememberSession = account.rememberSession - - guard let storedSession = sessions.first(where: { $0.accountEmail == account.email && !$0.accessToken.isEmpty }) else { - OPNLog.warning(.auth, "Session restore failed because no saved session exists for account=\(account.email)") - validationMessage = "No saved session exists for this account. Sign in again." - return .rejected - } - - isAuthenticating = true - defer { isAuthenticating = false } - // Captured before the network round trips. A sign-out or cancellation during them advances - // this, and the restore then discards its result instead of reactivating the account. - let generation = loginLaunchGeneration - + private func restorableJarvisSession(for account: LoginAccount, stored storedSession: LoginSession) -> JarvisSession { var jarvisSession = JarvisSession( accessToken: storedSession.accessToken, idToken: storedSession.idToken, @@ -219,6 +201,29 @@ extension LoginViewModel { if jarvisSession.idTokenExpiry == 0 { jarvisSession.idTokenExpiry = JarvisSessionParser.idTokenExpiry(storedSession.idToken) } + return jarvisSession + } + + func restoreAccountSessionOutcome(_ account: LoginAccount) async -> AccountRestoreOutcome { + validationMessage = "" + successMessage = "" + email = account.email + selectedProvider = providerOption(idpId: account.providerIdpId, fallbackName: account.providerName) + rememberSession = account.rememberSession + + guard let storedSession = sessions.first(where: { $0.accountEmail == account.email && !$0.accessToken.isEmpty }) else { + OPNLog.warning(.auth, "Session restore failed because no saved session exists for account=\(account.email)") + validationMessage = "No saved session exists for this account. Sign in again." + return .rejected + } + + isAuthenticating = true + defer { isAuthenticating = false } + // Captured before the network round trips. A sign-out or cancellation during them advances + // this, and the restore then discards its result instead of reactivating the account. + let generation = loginLaunchGeneration + + let jarvisSession = restorableJarvisSession(for: account, stored: storedSession) do { OPNLog.info(.auth, "Refreshing saved session account=\(account.email)") @@ -232,6 +237,8 @@ extension LoginViewModel { successMessage = "Session refreshed for \(account.displayName)." OPNLog.info(.auth, "Session refreshed account=\(account.email)") return .restored + } catch is CancellationError { + return .cancelled } catch { if storedSession.canContinueOffline && !storedSession.isExpired { guard generation == loginLaunchGeneration else { return .cancelled } From cb27dcb94e118ec5c04032d1b802d47001dd3919 Mon Sep 17 00:00:00 2001 From: Anderson Shindy Oki Date: Thu, 29 Oct 2026 09:42:49 +0900 Subject: [PATCH 22/22] refactor: clarify PR 82 account and stream code --- App/OPNMainAccountActions.swift | 14 ++++----- .../ControllerBindingOutputLedger.swift | 18 +++++------ OPN/Core/OPNLaunchEntitlements.swift | 6 ++-- .../OPNGameService+Provider.swift | 30 +++++++++---------- OPN/Services/OPNMainWindowCloseGuard.swift | 25 +++++++++------- OPN/Stream/NativeStreamViewInput.swift | 4 +-- OPN/Stream/NativeStreamViewPointer.swift | 4 +-- OPN/Stream/OPNStreamBorderless.swift | 12 ++++---- OPN/Stream/OPNStreamPictureInPicture.swift | 5 ++-- OPN/Stream/OPNStreamWindow.swift | 27 +++++++++-------- View/Stream/OPNStreamWindowPresenter.swift | 13 ++++---- .../DetachedSessionViewModel.swift | 16 +++++----- ViewModel/Stream/NativeNVSTHostInput.swift | 18 ++++------- ...veNVSTHostViewModel+PictureInPicture.swift | 14 +++++---- 14 files changed, 101 insertions(+), 105 deletions(-) diff --git a/App/OPNMainAccountActions.swift b/App/OPNMainAccountActions.swift index 71b91ab4e..c0f088984 100644 --- a/App/OPNMainAccountActions.swift +++ b/App/OPNMainAccountActions.swift @@ -45,10 +45,10 @@ final class OPNMainAccountActions { @discardableResult func bind(login: LoginViewModel) -> Task { self.login = login - let requests = pendingRequests + let queuedRequests = pendingRequests pendingRequests.removeAll() return Task { - for request in requests { + for request in queuedRequests { await perform(request, login: login) } } @@ -96,12 +96,12 @@ final class OPNMainAccountActions { } private func reload(login: LoginViewModel) throws { - guard let context = login.modelContext else { throw ActionError.missingContext } + guard let modelContext = login.modelContext else { throw ActionError.missingContext } login.update( - modelContext: context, - accounts: try context.fetch(FetchDescriptor()), - sessions: try context.fetch(FetchDescriptor()), - devices: try context.fetch(FetchDescriptor()) + modelContext: modelContext, + accounts: try modelContext.fetch(FetchDescriptor()), + sessions: try modelContext.fetch(FetchDescriptor()), + devices: try modelContext.fetch(FetchDescriptor()) ) } diff --git a/Model/Stream/ControllerBindingOutputLedger.swift b/Model/Stream/ControllerBindingOutputLedger.swift index 33f22ab17..ed2753458 100644 --- a/Model/Stream/ControllerBindingOutputLedger.swift +++ b/Model/Stream/ControllerBindingOutputLedger.swift @@ -25,19 +25,19 @@ struct ControllerBindingOutputLedger { } mutating func releaseHeldOutputs(timestamp: MediaTimestamp) -> [UserInputEvent] { - var events: [UserInputEvent] = [] - for code in keys.keys.sorted() { - guard let owner = keys[code]?.keys.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } - events.append(.keyboard(KeyboardEvent(deviceID: owner, keyCode: code, scanCode: code, - modifiers: [], isPressed: false, timestamp: timestamp))) + var releaseEvents: [UserInputEvent] = [] + for keyCode in keys.keys.sorted() { + guard let ownerDeviceID = keys[keyCode]?.keys.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } + releaseEvents.append(.keyboard(KeyboardEvent(deviceID: ownerDeviceID, keyCode: keyCode, scanCode: keyCode, + modifiers: [], isPressed: false, timestamp: timestamp))) } - for button in mouseButtons.keys.sorted(by: { $0.rawValue < $1.rawValue }) { - guard let owner = mouseButtons[button]?.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } - events.append(.mouse(.button(deviceID: owner, button: button, isPressed: false, timestamp: timestamp))) + for mouseButton in mouseButtons.keys.sorted(by: { $0.rawValue < $1.rawValue }) { + guard let ownerDeviceID = mouseButtons[mouseButton]?.sorted(by: { $0.rawValue < $1.rawValue }).first else { continue } + releaseEvents.append(.mouse(.button(deviceID: ownerDeviceID, button: mouseButton, isPressed: false, timestamp: timestamp))) } keys.removeAll() mouseButtons.removeAll() - return events + return releaseEvents } private mutating func keyboardEvents(for event: KeyboardEvent) -> [UserInputEvent] { diff --git a/OPN/Core/OPNLaunchEntitlements.swift b/OPN/Core/OPNLaunchEntitlements.swift index d5209d2b0..2ccfbcd60 100644 --- a/OPN/Core/OPNLaunchEntitlements.swift +++ b/OPN/Core/OPNLaunchEntitlements.swift @@ -52,9 +52,9 @@ struct OPNLaunchEntitlements: Equatable, Sendable { } var metadataValue: String { - guard let encoded = try? JSONSerialization.data(withJSONObject: storedDictionary, options: .sortedKeys), - let value = String(data: encoded, encoding: .utf8) else { return "{}" } - return value + guard let encodedEntitlements = try? JSONSerialization.data(withJSONObject: storedDictionary, options: .sortedKeys), + let entitlementsJSON = String(data: encodedEntitlements, encoding: .utf8) else { return "{}" } + return entitlementsJSON } } diff --git a/OPN/GameServices/OPNGameService+Provider.swift b/OPN/GameServices/OPNGameService+Provider.swift index c32fbd577..0cc73c927 100644 --- a/OPN/GameServices/OPNGameService+Provider.swift +++ b/OPN/GameServices/OPNGameService+Provider.swift @@ -4,36 +4,36 @@ import Foundation extension OPNGameService { func fetchProviderInfo(idpId: String, completion: @escaping OPNProviderInfoCallback) { let requestID = UUID() - let token = accessToken - let requestingUserID = userId + let requestedAccessToken = accessToken + let requestedUserID = userId providerContextLock.withLock { providerRequestID = requestID } - let delivery: OPNProviderInfoCallback = { [weak self] success, info, endpoint, error in + let deliverProviderInfo: OPNProviderInfoCallback = { [weak self] isSuccessful, providerInfo, providerEndpoint, errorMessage in guard let self else { return } - let isCurrent = self.providerContextLock.withLock { - guard self.providerRequestID == requestID, self.accessToken == token, - self.userId == requestingUserID else { return false } - self.storedProviderStreamingBaseURL = endpoint.streamingServiceUrl + let isCurrentProviderRequest = self.providerContextLock.withLock { + guard self.providerRequestID == requestID, self.accessToken == requestedAccessToken, + self.userId == requestedUserID else { return false } + self.storedProviderStreamingBaseURL = providerEndpoint.streamingServiceUrl return true } - guard isCurrent else { - completion(false, info, endpoint, "Provider lookup superseded") + guard isCurrentProviderRequest else { + completion(false, providerInfo, providerEndpoint, "Provider lookup superseded") return } - completion(success, info, endpoint, error) + completion(isSuccessful, providerInfo, providerEndpoint, errorMessage) } Self.referenceDataLock.lock() if let entry = Self.providerInfoCache[idpId], Date().timeIntervalSince(entry.timestamp) <= Self.referenceDataFreshSeconds { Self.referenceDataLock.unlock() let (info, endpoint) = entry.value - dispatchProviderInfo(delivery, true, info, endpoint, "") + dispatchProviderInfo(deliverProviderInfo, true, info, endpoint, "") return } if Self.pendingProviderInfoCallbacks[idpId] != nil { - Self.pendingProviderInfoCallbacks[idpId]?.append(delivery) + Self.pendingProviderInfoCallbacks[idpId]?.append(deliverProviderInfo) Self.referenceDataLock.unlock() return } - Self.pendingProviderInfoCallbacks[idpId] = [delivery] + Self.pendingProviderInfoCallbacks[idpId] = [deliverProviderInfo] Self.referenceDataLock.unlock() fetchProviderInfoUncached(idpId: idpId) { success, info, endpoint, error in @@ -73,8 +73,8 @@ extension OPNGameService { let info = self.parseGameProviderInfo(json) var endpoint = self.selectGameProviderEndpoint(info, idpId: idpId) if endpoint.streamingServiceUrl.isEmpty { endpoint.streamingServiceUrl = Self.defaultStreamingBaseUrl } - let normalized = self.normalizeStreamingBaseUrl(endpoint.streamingServiceUrl) - endpoint.streamingServiceUrl = normalized.isEmpty ? Self.defaultStreamingBaseUrl : normalized + let normalizedStreamingBaseURL = self.normalizeStreamingBaseUrl(endpoint.streamingServiceUrl) + endpoint.streamingServiceUrl = normalizedStreamingBaseURL.isEmpty ? Self.defaultStreamingBaseUrl : normalizedStreamingBaseURL self.dispatchProviderInfo(completion, true, info, endpoint, "") }.resume() } diff --git a/OPN/Services/OPNMainWindowCloseGuard.swift b/OPN/Services/OPNMainWindowCloseGuard.swift index dde71f1a0..7e2acb9a6 100644 --- a/OPN/Services/OPNMainWindowCloseGuard.swift +++ b/OPN/Services/OPNMainWindowCloseGuard.swift @@ -115,17 +115,7 @@ final class OPNMainWindowAccountHandoff { func attach(_ window: NSWindow) { guard isRequested else { return } - if self.window !== window { - restoreLevel() - if let closeObserver { NotificationCenter.default.removeObserver(closeObserver) } - self.window = window - originalLevel = window.level - closeObserver = NotificationCenter.default.addObserver( - forName: NSWindow.willCloseNotification, object: window, queue: .main - ) { [weak self] _ in - MainActor.assumeIsolated { self?.end() } - } - } + bindWindow(window) applyLevel() } @@ -143,6 +133,19 @@ final class OPNMainWindowAccountHandoff { if isReturningFocus, let returnWindow, returnWindow.isVisible { returnWindow.makeKeyAndOrderFront(nil) } } + private func bindWindow(_ window: NSWindow) { + guard self.window !== window else { return } + restoreLevel() + if let closeObserver { NotificationCenter.default.removeObserver(closeObserver) } + self.window = window + originalLevel = window.level + closeObserver = NotificationCenter.default.addObserver( + forName: NSWindow.willCloseNotification, object: window, queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.end() } + } + } + private func applyLevel() { guard let window, let originalLevel else { return } let coveringLevel = coveringLevels().map(\.rawValue).max() ?? NSWindow.Level.normal.rawValue diff --git a/OPN/Stream/NativeStreamViewInput.swift b/OPN/Stream/NativeStreamViewInput.swift index 900efb092..b8fd2936c 100644 --- a/OPN/Stream/NativeStreamViewInput.swift +++ b/OPN/Stream/NativeStreamViewInput.swift @@ -291,10 +291,10 @@ extension NativeStreamView { } func updateControllerMappingFocus() { - let focus = inputFocus + let inputFocusState = inputFocus gamepadMonitor.setKeyboardMouseInputEnabled(isFrontmostInputTarget && remoteInputEnabled && !localOverlayCapturesInput) gamepadMonitor.setMappingsEnabled(ControllerMappingFocusPolicy.allowsMappings( - appIsActive: focus.isApplicationActive, windowIsKey: focus.isWindowKey, + appIsActive: inputFocusState.isApplicationActive, windowIsKey: inputFocusState.isWindowKey, remoteInputEnabled: remoteInputEnabled, overlayCapturesInput: localOverlayCapturesInput, allowsBackgroundPads: allowsBackgroundGamepadInput )) diff --git a/OPN/Stream/NativeStreamViewPointer.swift b/OPN/Stream/NativeStreamViewPointer.swift index e01e0ccbf..dfb0be2b0 100644 --- a/OPN/Stream/NativeStreamViewPointer.swift +++ b/OPN/Stream/NativeStreamViewPointer.swift @@ -10,8 +10,8 @@ extension NativeStreamView { /// A cursor rect and a pointer capture only mean anything for the key window of the active app. /// Taking either while another app is frontmost steals a pointer this window cannot even draw in. public var isFrontmostInputTarget: Bool { - let focus = inputFocus - return focus.isApplicationActive && focus.isWindowKey + let inputFocusState = inputFocus + return inputFocusState.isApplicationActive && inputFocusState.isWindowKey } var inputFocus: (isApplicationActive: Bool, isWindowKey: Bool) { diff --git a/OPN/Stream/OPNStreamBorderless.swift b/OPN/Stream/OPNStreamBorderless.swift index c9a996bb1..6ee51c76b 100644 --- a/OPN/Stream/OPNStreamBorderless.swift +++ b/OPN/Stream/OPNStreamBorderless.swift @@ -68,23 +68,23 @@ enum OPNStreamBorderless { return } window.scheduleLayoutChange { window in - restoreWindowedState(window, isFrameRestored: true) + restoreWindowedState(window, isFrameRestorationEnabled: true) guard !Task.isCancelled, !window.isPresentationEnding else { return } completion?() } window.isBorderlessExitPending = window.layoutState.isBorderless } - private static func restoreWindowedState(_ window: OPNStreamWindow, isFrameRestored: Bool) { + private static func restoreWindowedState(_ window: OPNStreamWindow, isFrameRestorationEnabled: Bool) { guard window.layoutState.isBorderless || window.borderlessRestoreState != nil else { window.clearLayoutFrame() return } - let wasBorderless = window.layoutState.isBorderless + let isBorderlessBeforeRestoration = window.layoutState.isBorderless window.layoutState.isBorderless = false defer { window.clearLayoutFrame() - if wasBorderless { postCoveringStateChange(for: window) } + if isBorderlessBeforeRestoration { postCoveringStateChange(for: window) } } OPNStreamWindowChrome.applyBorderless(to: window, isBorderless: false) guard let state = window.borderlessRestoreState else { @@ -95,7 +95,7 @@ enum OPNStreamBorderless { window.borderlessRestoreState = nil window.level = state.level for child in window.childWindows ?? [] { child.level = state.level } - guard isFrameRestored else { return } + guard isFrameRestorationEnabled else { return } window.setFrameFromLayout(state.frame) if state.isMiniaturized { window.miniaturize(nil) } } @@ -134,7 +134,7 @@ enum OPNStreamBorderless { window.cancelPendingLayout() window.layoutState.isManuallyPlaced = true window.layoutState.isAutoTiled = false - restoreWindowedState(window, isFrameRestored: false) + restoreWindowedState(window, isFrameRestorationEnabled: false) window.layoutMembershipDidChange?() } diff --git a/OPN/Stream/OPNStreamPictureInPicture.swift b/OPN/Stream/OPNStreamPictureInPicture.swift index 3942e3161..8c86bcf3f 100644 --- a/OPN/Stream/OPNStreamPictureInPicture.swift +++ b/OPN/Stream/OPNStreamPictureInPicture.swift @@ -75,9 +75,8 @@ enum OPNStreamPictureInPicture { enterRestoredWindow(window, aspectRatio: aspectRatio) completion?() } - if !window.isPictureInPicture, window.hasPendingLayoutChange { - window.pictureInPictureRequestID = UUID() - } + guard !window.isPictureInPicture, window.hasPendingLayoutChange else { return } + window.pictureInPictureRequestID = UUID() } private static func enterRestoredWindow(_ window: OPNStreamWindow, aspectRatio: CGFloat) { diff --git a/OPN/Stream/OPNStreamWindow.swift b/OPN/Stream/OPNStreamWindow.swift index 518938fb5..02a8b4c50 100644 --- a/OPN/Stream/OPNStreamWindow.swift +++ b/OPN/Stream/OPNStreamWindow.swift @@ -68,28 +68,29 @@ final class OPNStreamWindow: NSWindow { isBorderlessExitPending = false } - func scheduleLayoutChange(_ body: @escaping @MainActor (OPNStreamWindow) -> Void) { + func scheduleLayoutChange(_ applyLayoutChange: @escaping @MainActor (OPNStreamWindow) -> Void) { cancelPendingLayout() guard !isPresentationEnding else { return } - let requestID = layoutRequestID + let scheduledLayoutRequestID = layoutRequestID layoutTask = Task { @MainActor [weak self] in guard let self else { return } - defer { - if self.layoutRequestID == requestID { - self.layoutTask = nil - self.pictureInPictureRequestID = nil - self.isBorderlessExitPending = false - } - } + defer { self.clearFinishedLayoutRequest(scheduledLayoutRequestID) } guard await StreamWindowGeometryGate.waitUntilGeometryIsMutable(for: self), - self.layoutRequestID == requestID, !self.isPresentationEnding, + self.layoutRequestID == scheduledLayoutRequestID, !self.isPresentationEnding, !self.styleMask.contains(.fullScreen) else { return } self.isApplyingLayoutChange = true defer { self.isApplyingLayoutChange = false } - body(self) + applyLayoutChange(self) } } + private func clearFinishedLayoutRequest(_ finishedLayoutRequestID: UUID) { + guard layoutRequestID == finishedLayoutRequestID else { return } + layoutTask = nil + pictureInPictureRequestID = nil + isBorderlessExitPending = false + } + func endPresentation() { isPresentationEnding = true cancelPendingLayout() @@ -103,9 +104,9 @@ final class OPNStreamWindow: NSWindow { } func setFrameFromLayout(_ frame: NSRect) { - let wasApplyingLayoutChange = isApplyingLayoutChange + let isApplyingEnclosingLayoutChange = isApplyingLayoutChange isApplyingLayoutChange = true - defer { isApplyingLayoutChange = wasApplyingLayoutChange } + defer { isApplyingLayoutChange = isApplyingEnclosingLayoutChange } layoutFrame = frame setFrame(frame, display: true) } diff --git a/View/Stream/OPNStreamWindowPresenter.swift b/View/Stream/OPNStreamWindowPresenter.swift index 33b14e964..15a5f8cd4 100644 --- a/View/Stream/OPNStreamWindowPresenter.swift +++ b/View/Stream/OPNStreamWindowPresenter.swift @@ -344,10 +344,10 @@ final class OPNStreamWindowPresenter { var hasTiledWindows: Bool { windows.values.contains { $0.layoutState.isAutoTiled } } private func isPlaceableByLayout(_ window: OPNStreamWindow) -> Bool { - let presentation = presentations[ObjectIdentifier(window)] + let windowPresentation = presentations[ObjectIdentifier(window)] return !window.layoutState.isManuallyPlaced && !window.isPictureInPicture && !window.isPictureInPicturePending && !window.isMiniaturized && !window.isPresentationEnding && !isNativeFullScreen(window) && - presentation?.transition == FullScreenTransition.none + windowPresentation?.transition == FullScreenTransition.none } private func releaseTile(_ window: OPNStreamWindow) { @@ -387,24 +387,23 @@ final class OPNStreamWindowPresenter { } } } - let willMiniaturize = center.addObserver(forName: NSWindow.willMiniaturizeNotification, object: window, queue: .main) { [weak window] _ in + let willMiniaturizeObserver = center.addObserver(forName: NSWindow.willMiniaturizeNotification, object: window, queue: .main) { [weak window] _ in MainActor.assumeIsolated { guard let window, !window.isApplyingLayoutChange else { return } OPNStreamBorderless.takePlacement(window) } } - let miniaturizationTokens = [NSWindow.didMiniaturizeNotification, NSWindow.didDeminiaturizeNotification].map { name in + let miniaturizationObserverTokens = [NSWindow.didMiniaturizeNotification, NSWindow.didDeminiaturizeNotification].map { name in center.addObserver(forName: name, object: window, queue: .main) { [weak self] _ in MainActor.assumeIsolated { self?.retileWindows() } } } - presentation.observerTokens = [willEnter, didEnter, willExit, didExit, willMiniaturize] + layoutTokens + miniaturizationTokens + presentation.observerTokens = [willEnter, didEnter, willExit, didExit, willMiniaturizeObserver] + layoutTokens + miniaturizationObserverTokens presentations[id] = presentation return presentation } - /// A tiled window displaced by the reader stays theirs. Returns whether ownership changed; - /// the window's owning presenter retiles the rest. + /// The owner callback retiles survivors; the observer must not issue a second retile. @discardableResult static func noteFrameChange(_ window: OPNStreamWindow) -> Bool { // No layout frame yet means the placement is still waiting on the geometry gate, so a move diff --git a/ViewModel/DetachedSession/DetachedSessionViewModel.swift b/ViewModel/DetachedSession/DetachedSessionViewModel.swift index 6d6c74c79..34b616825 100644 --- a/ViewModel/DetachedSession/DetachedSessionViewModel.swift +++ b/ViewModel/DetachedSession/DetachedSessionViewModel.swift @@ -45,10 +45,10 @@ final class DetachedSessionViewModel { func confirm(row: DetachedAccountRow, accountID: OPNAccountID) { guard row.availability.isSelectable else { return } discardPendingSelection() - let selection = DetachedSessionModel.selection(disabled: disabledControllers, takesPadsFromOtherStreams: takesPadsFromOtherStreams) - preset.store(selection, forAccount: accountID.rawValue) + let controllerSelection = DetachedSessionModel.selection(disabled: disabledControllers, takesPadsFromOtherStreams: takesPadsFromOtherStreams) + preset.store(controllerSelection, forAccount: accountID.rawValue) pendingAccountID = accountID - pendingSelection = selection + pendingSelection = controllerSelection selectedEmail = row.email phase = .catalog(email: row.email) } @@ -62,12 +62,12 @@ final class DetachedSessionViewModel { /// A choice made for a stream that never started must not reach a later launch by the same /// account. Once the account owns a game the stream has taken it, so there is nothing to drop. func discardPendingSelection() { - let accountID = pendingAccountID - let selection = pendingSelection + let selectionAccountID = pendingAccountID + let controllerSelection = pendingSelection pendingAccountID = nil pendingSelection = nil - guard let accountID, let selection, !registry.isOwned(by: accountID), - preset.peek(forAccount: accountID.rawValue) == selection else { return } - preset.store(OPNStreamControllerSelection(), forAccount: accountID.rawValue) + guard let selectionAccountID, let controllerSelection, !registry.isOwned(by: selectionAccountID), + preset.peek(forAccount: selectionAccountID.rawValue) == controllerSelection else { return } + preset.store(OPNStreamControllerSelection(), forAccount: selectionAccountID.rawValue) } } diff --git a/ViewModel/Stream/NativeNVSTHostInput.swift b/ViewModel/Stream/NativeNVSTHostInput.swift index 4f0a56cf7..e1771a183 100644 --- a/ViewModel/Stream/NativeNVSTHostInput.swift +++ b/ViewModel/Stream/NativeNVSTHostInput.swift @@ -161,19 +161,11 @@ extension NativeNVSTHostViewModel { if case .mouse(.button(_, let button, let isPressed, _)) = event, button == .left { clipboard.pointerSelection.noteLeftButton(isPressed: isPressed, at: Date()) } - if case .mouse = event { - if view.mouseInputMode == .relative, !view.isPointerLocked, !isNeutralizing { - // The seat asked for mouselook but the capture is not held — an association macOS - // refused, or a focus change that released it. Dropping the event here is a mouse - // that does nothing at all, so a real user event retries the capture: a transient - // failure heals itself instead of stranding the session until the seat changes its - // mind. A neutralizing release, background input, or the HUD must never retake it. - guard view.canAcquirePointerCapture else { return } - view.setPointerLocked(true) - guard view.isPointerLocked else { return } - } - inputDispatcher?.enqueue(event) - return + if case .mouse = event, view.mouseInputMode == .relative, !view.isPointerLocked, !isNeutralizing { + // Real mouse input retries transient capture failures; neutral releases must never recapture. + guard view.canAcquirePointerCapture else { return } + view.setPointerLocked(true) + guard view.isPointerLocked else { return } } inputDispatcher?.enqueue(event) } diff --git a/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift b/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift index 7ee6544fb..2d9e95b41 100644 --- a/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift +++ b/ViewModel/Stream/NativeNVSTHostViewModel+PictureInPicture.swift @@ -66,10 +66,8 @@ extension NativeNVSTHostViewModel { private func leaveFullScreenThenEnterPictureInPicture(_ window: OPNStreamWindow, requestID: UUID, attempt: Int) { guard attempt < Self.sessionReadyFullScreenAttemptLimit else { - if window.pictureInPictureRequestID == requestID { - window.pictureInPictureRequestID = nil - window.layoutMembershipDidChange?() - } + guard window.pictureInPictureRequestID == requestID else { return } + Self.cancelPictureInPictureRequest(in: window) return } Task { @MainActor [weak self, weak window] in @@ -77,8 +75,7 @@ extension NativeNVSTHostViewModel { guard let window, window.pictureInPictureRequestID == requestID else { return } guard let self, !self.isEnding, !self.didEnd, !window.isPresentationEnding, self.nativeView?.window === window else { - window.pictureInPictureRequestID = nil - window.layoutMembershipDidChange?() + NativeNVSTHostViewModel.cancelPictureInPictureRequest(in: window) return } if self.isFullScreenTransitioning || StreamWindowGeometryGate.shouldDeferGeometryMutation(for: window) { @@ -93,4 +90,9 @@ extension NativeNVSTHostViewModel { self.enterPictureInPicture(window) } } + + private static func cancelPictureInPictureRequest(in window: OPNStreamWindow) { + window.pictureInPictureRequestID = nil + window.layoutMembershipDidChange?() + } }