Skip to content

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

iOS shell: every input event reaches the library, and the echo guard on the remaining buttons

Haseenesss42cb01edf26f
diff --git a/frontend-ios/Sources/ShellDemo/ShellDemoApp.swift b/frontend-ios/Sources/ShellDemo/ShellDemoApp.swift
index 5b7eba5d..c1cb619e 100644
--- a/frontend-ios/Sources/ShellDemo/ShellDemoApp.swift
+++ b/frontend-ios/Sources/ShellDemo/ShellDemoApp.swift
@@ -24,7 +24,7 @@ struct DemoScreen: View {
@State private var padSnapshot: PadSnapshot?
@State private var games = SampleData.games
@State private var notice: String?
- @State private var input: ShellInput?
+ @State private var input = ShellInputLog()
@State private var unfolded = ProcessInfo.processInfo.arguments.contains("--unfolded")
/// Real controllers, through the reusable adapter in ShellControllers.
@State private var gamepads = GamepadAdapter()
@@ -72,6 +72,7 @@ struct DemoScreen: View {
} message: { Text(notice ?? "") }
.onAppear {
gamepads.onInput = { action, device in send(action, from: device) }
+ gamepads.onRelease = { action, device in input.append(action, device: device, released: true) }
gamepads.onControllersChange = { connected = $0 }
gamepads.start()
}
@@ -129,7 +130,7 @@ struct DemoScreen: View {
}
}
private func send(_ action: ShellInput.Action, from device: InputDevice = .controller(.neutral)) {
- input = ShellInput(sequence: (input?.sequence ?? 0) + 1, action: action, device: device)
+ input.append(action, device: device)
}
/// The real controllers when any are connected, the sample ones otherwise.
private var controllers: [ControllerInfo] {
diff --git a/frontend-ios/Sources/ShellHost/ShellHostModel.swift b/frontend-ios/Sources/ShellHost/ShellHostModel.swift
index defc89d8..d152fcd1 100644
--- a/frontend-ios/Sources/ShellHost/ShellHostModel.swift
+++ b/frontend-ios/Sources/ShellHost/ShellHostModel.swift
@@ -52,7 +52,7 @@ public struct ShellHostConfig: Sendable {
public private(set) var details: [GameDetail] = []
public private(set) var entries: [CatalogueEntry] = []
public private(set) var controllers: [ControllerInfo] = []
- public private(set) var input: ShellInput?
+ public private(set) var input = ShellInputLog()
/// A short message the host view shows as an alert.
public var notice: String?
/// The log the host view shows in a read-only sheet.
@@ -505,12 +505,9 @@ public struct ShellHostConfig: Sendable {
public func startControllers() {
adapter.onInput = { [weak self] action, device in
guard let self else { return }
- self.input = ShellInput(sequence: (self.input?.sequence ?? 0) + 1, action: action, device: device)
- }
- adapter.onRelease = { [weak self] action, device in
- guard let self else { return }
- self.input = ShellInput(sequence: (self.input?.sequence ?? 0) + 1, action: action, device: device, released: true)
+ self.input.append(action, device: device)
}
+ adapter.onRelease = { [weak self] action, device in self?.input.append(action, device: device, released: true) }
adapter.onControllersChange = { [weak self] connected in
self?.controllers = connected.map(Self.info(for:))
}
diff --git a/frontend-ios/Sources/ShellUI/ButtonTestView.swift b/frontend-ios/Sources/ShellUI/ButtonTestView.swift
index 391f0c9f..af39ea05 100644
--- a/frontend-ios/Sources/ShellUI/ButtonTestView.swift
+++ b/frontend-ios/Sources/ShellUI/ButtonTestView.swift
@@ -11,6 +11,7 @@ struct ButtonTestView: View {
@State private var holding = false
@State private var holdTask: Task<Void, Never>?
@Environment(\.accessibilityReduceMotion) private var reduceMotion
+ @Environment(\.shellIgnoresPress) private var ignoresPress
var body: some View {
VStack(alignment: .leading, spacing: 0) {
@@ -22,7 +23,9 @@ struct ButtonTestView: View {
.shellType(ShellType.note).foregroundStyle(.white.opacity(0.5)).lineLimit(1)
}
Spacer(minLength: 8)
- Button(action: close) {
+ // The system's echo of a controller button does not close the test. Holding B, which goes past this
+ // button, still does.
+ Button { if !ignoresPress() { close() } } label: {
Image(systemName: "xmark").font(.system(size: 14, weight: .semibold)).frame(width: 44, height: 44)
.background(.white.opacity(0.08), in: Circle()).contentShape(Circle())
}
diff --git a/frontend-ios/Sources/ShellUI/GameDetailView.swift b/frontend-ios/Sources/ShellUI/GameDetailView.swift
index 90d844b0..1a0f5801 100644
--- a/frontend-ios/Sources/ShellUI/GameDetailView.swift
+++ b/frontend-ios/Sources/ShellUI/GameDetailView.swift
@@ -182,6 +182,7 @@ struct SettingControl: View, Equatable {
nonisolated static func == (lhs: Self, rhs: Self) -> Bool { lhs.gameID == rhs.gameID && lhs.row == rhs.row }
@Environment(\.dynamicTypeSize) private var typeSize
@Environment(\.shellPickChoice) private var pickChoice
+ @Environment(\.shellIgnoresPress) private var ignoresPress
/// The row's width, once measured, for the segmented-or-list rule.
@State private var width: CGFloat?
var body: some View {
@@ -249,7 +250,7 @@ struct SettingControl: View, Equatable {
}
private func rangeButton(_ symbol: String, min: Double, max: Double, delta: Double) -> some View {
let action = { change(String(Swift.min(max, Swift.max(min, (Double(row.value) ?? min) + delta)))) }
- return Button(action: action) {
+ return Button { if !ignoresPress() { action() } } label: {
Image(systemName: symbol).font(.system(size: 14, weight: .semibold)).frame(width: 44, height: 44)
}
.buttonStyle(.plain).background(.white.opacity(0.055), in: Circle())
@@ -268,13 +269,15 @@ struct SegmentedChoice: View {
let targetPrefix: String
var enabled = true
let change: (String) -> Void
+ @Environment(\.shellIgnoresPress) private var ignoresPress
var body: some View {
VStack(spacing: 2) {
ForEach(Array(rows.enumerated()), id: \.offset) { _, options in
EqualRow(spacing: 2) {
ForEach(options) { option in
let chosen = value == option.id
- Button { change(option.id) } label: {
+ // The system's echo of a controller button picks nothing (the shell's focus took the button).
+ Button { if !ignoresPress() { change(option.id) } } label: {
Text(option.label).shellText(13.5, chosen ? .bold : .medium).lineLimit(1)
.padding(.horizontal, 14).frame(maxWidth: .infinity, minHeight: 40)
.foregroundStyle(chosen ? .black : .white.opacity(0.75))
diff --git a/frontend-ios/Sources/ShellUI/InputGate.swift b/frontend-ios/Sources/ShellUI/InputGate.swift
index 3ee74d87..b89b1e79 100644
--- a/frontend-ios/Sources/ShellUI/InputGate.swift
+++ b/frontend-ios/Sources/ShellUI/InputGate.swift
@@ -16,13 +16,11 @@ struct SystemEchoWindow: Equatable {
static let heldLimit: TimeInterval = 30
/// When a controller button last went down or came up.
private(set) var controllerAt: TimeInterval?
- /// How many controller buttons are down. Directions are not counted: held, they repeat.
+ /// How many controller buttons are down.
private(set) var held = 0
/// When a finger last came down anywhere on the screen.
private(set) var touchAt: TimeInterval?
- /// A controller direction moved, or a direction repeated.
- mutating func controller(at time: TimeInterval) { controllerAt = time }
/// A controller button went down.
mutating func buttonDown(at time: TimeInterval) { controllerAt = time; held += 1 }
/// A controller button came up. The echo comes after this, so the window starts again here.
@@ -41,20 +39,33 @@ struct SystemEchoWindow: Equatable {
/// Marks the actions a controller or key press causes, so they are not taken for touch, and keeps the window for the
/// system's echo of a controller button. Not observed.
@MainActor final class InputGate {
+ /// The echo is iPadOS's: a Mac sends none, and a click there has no probe to end the window.
+ static let platformEchoes: Bool = {
+ #if os(iOS)
+ true
+ #else
+ false
+ #endif
+ }()
/// True while `handle` acts on a controller or key press.
var handling = false
private var window = SystemEchoWindow()
private let clock: () -> TimeInterval
+ private let echoes: Bool
- init(clock: @escaping () -> TimeInterval = { ProcessInfo.processInfo.systemUptime }) { self.clock = clock }
+ init(clock: @escaping () -> TimeInterval = { ProcessInfo.processInfo.systemUptime }, echoes: Bool = InputGate.platformEchoes) {
+ self.clock = clock
+ self.echoes = echoes
+ }
- /// A controller action came in: a button going down, or a direction (which repeats while held).
+ /// A controller action came in. Only buttons count: the system's press follows a button (A), and a direction
+ /// repeats while held, which would end a finger's tap that began before it.
func controller(_ action: ShellInput.Action) {
- if Self.isDirection(action) { window.controller(at: clock()) } else { window.buttonDown(at: clock()) }
+ if !Self.isDirection(action) { window.buttonDown(at: clock()) }
}
/// A controller button or direction came up.
func controllerUp(_ action: ShellInput.Action) {
- if Self.isDirection(action) { window.controller(at: clock()) } else { window.buttonUp(at: clock()) }
+ if !Self.isDirection(action) { window.buttonUp(at: clock()) }
}
private static func isDirection(_ action: ShellInput.Action) -> Bool {
switch action {
@@ -64,7 +75,7 @@ struct SystemEchoWindow: Equatable {
}
func touch() { window.touch(at: clock()) }
/// A press acted on now, outside `handle`, is the system's echo of a controller button.
- var isSystemEcho: Bool { !handling && window.isEcho(at: clock()) }
+ var isSystemEcho: Bool { echoes && !handling && window.isEcho(at: clock()) }
}
/// What sits on top of the library and owns the D-pad, the topmost first. Each one's focus targets start with its
diff --git a/frontend-ios/Sources/ShellUI/LibraryView.swift b/frontend-ios/Sources/ShellUI/LibraryView.swift
index 591783a0..13b4f1f1 100644
--- a/frontend-ios/Sources/ShellUI/LibraryView.swift
+++ b/frontend-ios/Sources/ShellUI/LibraryView.swift
@@ -35,11 +35,15 @@ public struct LibraryView: View {
/// The controller's state while the button test is open, from the host.
public let padSnapshot: PadSnapshot?
public var actions: ShellActions
- public var input: ShellInput?
+ public var input: ShellInputLog
+ /// The sequence of the last input event acted on.
+ @State private var inputSeen: Int
@State private var selectedID: String?
@State private var settingsOpen: Bool
/// The settings pane has finished sliding in: its focus ring shows only then, not ahead of it where it will stop.
@State private var settingsArrived: Bool
+ /// Counts the opening and closing of the settings pane, so a wait for one opening does not act on a later one.
+ @State private var settingsOpenings = 0
@State private var shellSettingsOpen: Bool
@State private var controllersOpen: Bool
/// The game whose How to fix or How to update explanation is open.
@@ -75,7 +79,7 @@ public struct LibraryView: View {
@State private var probedSideBar = SideBar(edge: nil)
public init(games: [GameDetail], controllers: [ControllerInfo] = [], actions: ShellActions = ShellActions(),
- input: ShellInput? = nil, initialSelection: String? = nil, initialDetailOpen: Bool = false,
+ input: ShellInputLog = ShellInputLog(), initialSelection: String? = nil, initialDetailOpen: Bool = false,
initialControllersOpen: Bool = false, initialShellSettingsOpen: Bool = false, initialFocusVisible: Bool = false,
globalPadMap: PadMap = PadMap(), padSnapshot: PadSnapshot? = nil,
initialPadMapping: String? = nil, initialButtonTestOpen: Bool = false) {
@@ -87,6 +91,7 @@ public struct LibraryView: View {
_buttonTestOpen = State(initialValue: initialButtonTestOpen)
self.actions = actions
self.input = input
+ _inputSeen = State(initialValue: input.last)
let first = initialSelection ?? games.first?.id
_selectedID = State(initialValue: first)
_settingsOpen = State(initialValue: initialDetailOpen)
@@ -316,11 +321,14 @@ public struct LibraryView: View {
.environment(\.shellInputDevice, device)
.foregroundStyle(.white)
.font(ShellFonts.font(15)).tint(ShellPalette.cream).preferredColorScheme(.dark)
- .onChange(of: input) { _, event in
- guard let event else { return }
- // A button let go only restarts the window for the system's echo, which comes as the button comes up.
- if event.released { if case .controller = event.device { gate.controllerUp(event.action) }; return }
- handle(event.action, from: event.device)
+ .onChange(of: input) { _, log in
+ // Every event since the last update, in order: several can come between two updates.
+ for event in log.since(inputSeen) {
+ // A button let go only restarts the window for the system's echo, which comes as the button comes up.
+ if event.released { if case .controller = event.device { gate.controllerUp(event.action) }; continue }
+ handle(event.action, from: event.device)
+ }
+ inputSeen = log.last
}
// A hardware keyboard, as on the Mac: arrows move, Return activates, Escape goes back, O opens game settings,
// Y opens controllers.
@@ -338,9 +346,14 @@ public struct LibraryView: View {
.onDisappear { targets.values.removeAll() }
.onChange(of: settingsOpen) { _, open in
settingsArrived = false
+ settingsOpenings += 1
guard open else { return }
- // The pane slides in over 0.3 s (the animation on settingsOpen).
- DispatchQueue.main.asyncAfter(deadline: .now() + (reduceMotion ? 0 : 0.3)) { if settingsOpen { settingsArrived = true } }
+ // The pane slides in over 0.3 s (the animation on settingsOpen). Only for this opening: closed and opened
+ // again within the 0.3 s, the first opening's wait would show the ring while the pane still slides.
+ let opening = settingsOpenings
+ DispatchQueue.main.asyncAfter(deadline: .now() + (reduceMotion ? 0 : 0.3)) {
+ if settingsOpen && settingsOpenings == opening { settingsArrived = true }
+ }
}
.onChange(of: games.map(\.id)) { _, ids in
if !ids.contains(selectedID ?? "") { selectedID = ids.first; settingsOpen = false }
diff --git a/frontend-ios/Sources/ShellUI/ShellInputs.swift b/frontend-ios/Sources/ShellUI/ShellInputs.swift
index 719a5333..f5f6466d 100644
--- a/frontend-ios/Sources/ShellUI/ShellInputs.swift
+++ b/frontend-ios/Sources/ShellUI/ShellInputs.swift
@@ -156,7 +156,7 @@ public enum InputDevice: Equatable, Sendable {
case keyboard
}
-/// An optional event supplied by the host's input adapter. Increment sequence for repeated presses.
+/// One event from the host's input adapter: a press, or a button or direction let go. `sequence` orders them.
public struct ShellInput: Equatable, Sendable {
public enum Action: Sendable { case up, down, left, right, activate, back, settings, controllers }
public var sequence: Int
@@ -173,6 +173,23 @@ public struct ShellInput: Equatable, Sendable {
}
}
+/// The host's input events in order, the last `kept` of them. The host appends each press and release; the library acts
+/// on every event it has not seen yet. One value would keep only the last event written between two view updates, so a
+/// press could be lost, or a release, which left a button counted as held (`SystemEchoWindow`).
+public struct ShellInputLog: Equatable, Sendable {
+ public static let kept = 64
+ public private(set) var events: [ShellInput] = []
+ public init() {}
+ /// The sequence of the last event, 0 with none yet.
+ public var last: Int { events.last?.sequence ?? 0 }
+ public mutating func append(_ action: ShellInput.Action, device: InputDevice = .controller(.neutral), released: Bool = false) {
+ events.append(ShellInput(sequence: last + 1, action: action, device: device, released: released))
+ if events.count > Self.kept { events.removeFirst(events.count - Self.kept) }
+ }
+ /// The events after `sequence`, oldest first.
+ public func since(_ sequence: Int) -> [ShellInput] { events.filter { $0.sequence > sequence } }
+}
+
/// A controller element the button test lights up, named by its place on an Xbox controller (GameController's names).
public enum PadElement: String, CaseIterable, Sendable {
case a, b, x, y, leftBumper, rightBumper, leftTrigger, rightTrigger, leftStick, rightStick, view, menu, home, up, down, left, right
diff --git a/frontend-ios/Sources/ShellUI/TouchLayoutEditorView.swift b/frontend-ios/Sources/ShellUI/TouchLayoutEditorView.swift
index 62507203..6ab23525 100644
--- a/frontend-ios/Sources/ShellUI/TouchLayoutEditorView.swift
+++ b/frontend-ios/Sources/ShellUI/TouchLayoutEditorView.swift
@@ -14,6 +14,8 @@ struct TouchLayoutEditorView: View {
var columnAnchor: UnitPoint = .top
@Environment(\.shellFocus) private var focus
@Environment(\.accessibilityReduceMotion) private var reduceMotion
+ /// The system's echo of a controller button: the editor's buttons do nothing for it (the shell's focus took it).
+ @Environment(\.shellIgnoresPress) private var ignoresPress
/// The drag in progress: the control, where its offset started, and the spot drawn under the finger when the engine refuses it.
@State private var drag: DragState?
@State private var bounce: CGSize = .zero
@@ -83,7 +85,7 @@ struct TouchLayoutEditorView: View {
Text(ShellStrings.text("Arrange controls")).shellType(ShellType.pageTitle)
.lineLimit(1).minimumScaleFactor(0.7).accessibilityAddTraits(.isHeader)
Spacer(minLength: 8)
- Button(action: close) { Text(ShellStrings.text("Done")) }
+ Button { if !ignoresPress() { close() } } label: { Text(ShellStrings.text("Done")) }
.buttonStyle(ChipButtonStyle())
.accessibilityIdentifier("touchLayout.done")
.shellTarget("lay.done", radius: 22, action: close)
@@ -348,7 +350,7 @@ struct TouchLayoutEditorView: View {
private var resets: some View {
VStack(alignment: .leading, spacing: 8) {
let canPutBack = model.selected.map { model.editing.isMoved($0) } ?? false
- Button { model.putBack() } label: { resetLabel("Reset this control", "arrow.uturn.backward") }
+ Button { if !ignoresPress() { model.putBack() } } label: { resetLabel("Reset this control", "arrow.uturn.backward") }
.buttonStyle(ShellButtonStyle(fills: true))
.disabled(!canPutBack)
.accessibilityIdentifier("touchLayout.putBack")
@@ -358,11 +360,11 @@ struct TouchLayoutEditorView: View {
Text(ShellStrings.text("Put every control of the \(model.kind.full ? "3D" : "2D") layout back in its built-in place?"))
.shellType(ShellType.note).foregroundStyle(.white.opacity(0.85)).fixedSize(horizontal: false, vertical: true)
.accessibilityIdentifier("touchLayout.resetQuestion")
- Button { model.reset() } label: { resetLabel("Reset all controls", "arrow.counterclockwise") }
+ Button { if !ignoresPress() { model.reset() } } label: { resetLabel("Reset all controls", "arrow.counterclockwise") }
.buttonStyle(ShellButtonStyle(fills: true, destructive: true))
.accessibilityIdentifier("touchLayout.resetConfirm")
.shellTarget("lay.resetConfirm") { model.reset() }
- Button { model.cancelReset() } label: { Text(ShellStrings.text("Cancel")).lineLimit(1).minimumScaleFactor(0.8) }
+ Button { if !ignoresPress() { model.cancelReset() } } label: { Text(ShellStrings.text("Cancel")).lineLimit(1).minimumScaleFactor(0.8) }
.buttonStyle(ShellButtonStyle(fills: true))
.accessibilityIdentifier("touchLayout.resetCancel")
.shellTarget("lay.reset") { model.cancelReset() }
@@ -371,7 +373,7 @@ struct TouchLayoutEditorView: View {
.background(DeleteStyle.red.opacity(0.08), in: RoundedRectangle(cornerRadius: 12))
.overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(DeleteStyle.red.opacity(0.35)))
} else {
- Button { model.askReset() } label: { resetLabel("Reset all controls", "arrow.counterclockwise") }
+ Button { if !ignoresPress() { model.askReset() } } label: { resetLabel("Reset all controls", "arrow.counterclockwise") }
.buttonStyle(ShellButtonStyle(fills: true, destructive: true))
.disabled(model.layoutIsBuiltIn)
.accessibilityIdentifier("touchLayout.reset")
@@ -439,7 +441,7 @@ struct TouchLayoutEditorView: View {
}
private func arrow(_ symbol: String, _ label: String.LocalizationValue, _ id: String, _ dx: Int, _ dy: Int) -> some View {
- Button { model.nudge(dx: dx, dy: dy) } label: {
+ Button { if !ignoresPress() { model.nudge(dx: dx, dy: dy) } } label: {
Image(systemName: symbol).font(.system(size: 18, weight: .semibold)).frame(width: Self.arrowSize, height: Self.arrowSize)
}
.buttonStyle(RoundIconButtonStyle(size: Self.arrowSize))
diff --git a/frontend-ios/Tests/ShellUITests/ShellInputLogTests.swift b/frontend-ios/Tests/ShellUITests/ShellInputLogTests.swift
new file mode 100644
index 00000000..e5942499
--- /dev/null
+++ b/frontend-ios/Tests/ShellUITests/ShellInputLogTests.swift
@@ -0,0 +1,50 @@
+import XCTest
+@testable import ShellUI
+
+/// The events between two view updates all reach the library, in order: one value kept only the last one written.
+final class ShellInputLogTests: XCTestCase {
+ func testEveryEventSinceTheLastSeenInOrder() {
+ var log = ShellInputLog()
+ XCTAssertEqual(log.last, 0)
+ log.append(.activate)
+ log.append(.activate, released: true)
+ log.append(.back)
+ log.append(.back, released: true)
+ let seen = log.since(0)
+ XCTAssertEqual(seen.map(\.action), [.activate, .activate, .back, .back])
+ XCTAssertEqual(seen.map(\.released), [false, true, false, true])
+ XCTAssertEqual(seen.map(\.sequence), [1, 2, 3, 4])
+ XCTAssertEqual(log.since(2).map(\.sequence), [3, 4])
+ XCTAssertEqual(log.since(4), [])
+ }
+ /// Two buttons let go in one controller change, and a press with a release in one: each one counts on the gate.
+ @MainActor func testReleasesInOneUpdateKeepTheHeldCountRight() {
+ var log = ShellInputLog()
+ var now: TimeInterval = 10
+ let gate = InputGate(clock: { now }, echoes: true)
+ var seen = 0
+ func update() {
+ for event in log.since(seen) {
+ if event.released { gate.controllerUp(event.action) } else { gate.controller(event.action) }
+ }
+ seen = log.last
+ }
+ log.append(.activate)
+ log.append(.back)
+ update()
+ log.append(.activate, released: true)
+ log.append(.back, released: true)
+ log.append(.left, released: true)
+ log.append(.up)
+ update()
+ now = 11.1
+ XCTAssertFalse(gate.isSystemEcho, "both buttons came up: nothing held, the window is over")
+ }
+ func testOnlyTheLastEventsAreKept() {
+ var log = ShellInputLog()
+ for _ in 0..<(ShellInputLog.kept + 10) { log.append(.down) }
+ XCTAssertEqual(log.events.count, ShellInputLog.kept)
+ XCTAssertEqual(log.last, ShellInputLog.kept + 10)
+ XCTAssertEqual(log.since(0).first?.sequence, 11)
+ }
+}
diff --git a/frontend-ios/Tests/ShellUITests/SystemEchoTests.swift b/frontend-ios/Tests/ShellUITests/SystemEchoTests.swift
index 46496713..8cbfea84 100644
--- a/frontend-ios/Tests/ShellUITests/SystemEchoTests.swift
+++ b/frontend-ios/Tests/ShellUITests/SystemEchoTests.swift
@@ -12,20 +12,23 @@ final class SystemEchoWindowTests: XCTestCase {
}
func testInsideTheWindowIsTheEcho() {
var w = SystemEchoWindow()
- w.controller(at: 100)
+ w.buttonDown(at: 99.9)
+ w.buttonUp(at: 100) // a quick press, up at 100
XCTAssertTrue(w.isEcho(at: 100))
XCTAssertTrue(w.isEcho(at: 100.025), "the echo comes about 25 ms after A comes up")
XCTAssertTrue(w.isEcho(at: 100.999))
}
func testAfterTheWindowIsNot() {
var w = SystemEchoWindow()
- w.controller(at: 100)
+ w.buttonDown(at: 99.9)
+ w.buttonUp(at: 100) // a quick press, up at 100
XCTAssertFalse(w.isEcho(at: 101))
XCTAssertFalse(w.isEcho(at: 160))
}
func testATouchAfterTheButtonEndsTheWindow() {
var w = SystemEchoWindow()
- w.controller(at: 100)
+ w.buttonDown(at: 99.9)
+ w.buttonUp(at: 100) // a quick press, up at 100
w.touch(at: 100.2)
XCTAssertFalse(w.isEcho(at: 100.3), "a tap right after a controller button acts at once")
w.touch(at: 100)
@@ -34,15 +37,22 @@ final class SystemEchoWindowTests: XCTestCase {
func testATouchBeforeTheButtonDoesNot() {
var w = SystemEchoWindow()
w.touch(at: 99.5)
- w.controller(at: 100)
+ w.buttonDown(at: 99.9)
+ w.buttonUp(at: 100) // a quick press, up at 100
XCTAssertTrue(w.isEcho(at: 100.1))
}
func testAClockThatWentBackIsNeverTheEcho() {
var w = SystemEchoWindow()
- w.controller(at: 100)
+ w.buttonDown(at: 99.9)
+ w.buttonUp(at: 100) // a quick press, up at 100
XCTAssertFalse(w.isEcho(at: 99.9))
XCTAssertFalse(w.isEcho(at: 0))
}
+ @MainActor func testNoEchoWhereThePlatformSendsNone() {
+ let gate = InputGate(clock: { 10 }, echoes: false)
+ gate.controller(.activate)
+ XCTAssertFalse(gate.isSystemEcho, "a Mac click right after a controller button acts")
+ }
func testAButtonHeldLongerThanTheWindow() {
var w = SystemEchoWindow()
w.buttonDown(at: 100)
@@ -85,7 +95,7 @@ final class SystemEchoWindowTests: XCTestCase {
@MainActor func testTheGateUsesItsClockAndNeverMarksItsOwnInput() {
var now: TimeInterval = 10
- let gate = InputGate(clock: { now })
+ let gate = InputGate(clock: { now }, echoes: true)
XCTAssertFalse(gate.isSystemEcho)
gate.controller(.activate)
now = 12
@@ -98,9 +108,12 @@ final class SystemEchoWindowTests: XCTestCase {
gate.handling = false
gate.touch()
XCTAssertFalse(gate.isSystemEcho)
+ now = 13
gate.controller(.right)
- now = 13.6
- XCTAssertFalse(gate.isSystemEcho, "a held direction repeats and is not counted as held")
+ gate.controller(.right)
+ XCTAssertFalse(gate.isSystemEcho, "a direction, held and repeating, does not end a tap that began before it")
+ gate.controllerUp(.right)
+ XCTAssertFalse(gate.isSystemEcho)
}
}