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: the left-edge swipe closes game settings and Controllers

v0lumehi70b1d4c5c29a
diff --git a/frontend-ios/Sources/ShellUI/EdgeSwipeBack.swift b/frontend-ios/Sources/ShellUI/EdgeSwipeBack.swift
new file mode 100644
index 00000000..afd52c13
--- /dev/null
+++ b/frontend-ios/Sources/ShellUI/EdgeSwipeBack.swift
@@ -0,0 +1,80 @@
+import SwiftUI
+
+#if canImport(UIKit)
+import UIKit
+
+/// The system's left-edge swipe for the screens that slide in over the library (game settings, Controllers). Those are
+/// overlays, not a navigation stack, so UIKit's own back swipe does not exist for them. This is the same recognizer
+/// UIKit uses for it, a `UIScreenEdgePanGestureRecognizer` on the left edge, so it starts only at the screen edge and
+/// never competes with a slider or a carousel on the page. It reports the finger's travel while it drags and where it
+/// lets go.
+struct EdgeSwipeBack: UIViewRepresentable {
+ var enabled: Bool
+ var changed: (CGFloat) -> Void
+ /// The travel to the right and the horizontal velocity when the finger lifts.
+ var ended: (CGFloat, CGFloat) -> Void
+ var cancelled: () -> Void
+
+ func makeCoordinator() -> Coordinator { Coordinator(self) }
+
+ func makeUIView(context: Context) -> AnchorView {
+ let view = AnchorView()
+ view.isUserInteractionEnabled = false
+ view.coordinator = context.coordinator
+ return view
+ }
+
+ func updateUIView(_ view: AnchorView, context: Context) {
+ context.coordinator.parent = self
+ context.coordinator.recognizer.isEnabled = enabled
+ }
+
+ static func dismantleUIView(_ view: AnchorView, coordinator: Coordinator) {
+ coordinator.recognizer.view?.removeGestureRecognizer(coordinator.recognizer)
+ }
+
+ /// Hangs the recognizer on the window, so the edge touch reaches it wherever the page is drawn.
+ final class AnchorView: UIView {
+ weak var coordinator: Coordinator?
+ override func didMoveToWindow() {
+ super.didMoveToWindow()
+ guard let coordinator else { return }
+ coordinator.recognizer.view?.removeGestureRecognizer(coordinator.recognizer)
+ window?.addGestureRecognizer(coordinator.recognizer)
+ }
+ }
+
+ @MainActor final class Coordinator: NSObject {
+ var parent: EdgeSwipeBack
+ let recognizer: UIScreenEdgePanGestureRecognizer
+
+ init(_ parent: EdgeSwipeBack) {
+ self.parent = parent
+ recognizer = UIScreenEdgePanGestureRecognizer()
+ recognizer.edges = .left
+ super.init()
+ recognizer.addTarget(self, action: #selector(pan(_:)))
+ recognizer.isEnabled = parent.enabled
+ }
+
+ @objc private func pan(_ gesture: UIScreenEdgePanGestureRecognizer) {
+ let travel = max(0, gesture.translation(in: gesture.view).x)
+ switch gesture.state {
+ case .began, .changed: parent.changed(travel)
+ case .ended: parent.ended(travel, gesture.velocity(in: gesture.view).x)
+ case .cancelled, .failed: parent.cancelled()
+ default: break
+ }
+ }
+ }
+}
+#else
+/// There is no edge swipe off UIKit.
+struct EdgeSwipeBack: View {
+ var enabled: Bool
+ var changed: (CGFloat) -> Void
+ var ended: (CGFloat, CGFloat) -> Void
+ var cancelled: () -> Void
+ var body: some View { Color.clear.frame(width: 0, height: 0) }
+}
+#endif
diff --git a/frontend-ios/Sources/ShellUI/LibraryView.swift b/frontend-ios/Sources/ShellUI/LibraryView.swift
index 044986bc..a24ba691 100644
--- a/frontend-ios/Sources/ShellUI/LibraryView.swift
+++ b/frontend-ios/Sources/ShellUI/LibraryView.swift
@@ -43,6 +43,8 @@ public struct LibraryView: View {
@State private var focus: FocusAppearance
@State private var targets = FocusTargetBuffer()
@State private var settingsPosition = ScrollPosition()
+ /// How far the left-edge swipe has dragged the open settings or Controllers to the right.
+ @State private var edgeDrag: CGFloat = 0
@State private var libraryPosition = ScrollPosition()
@State private var layout: ShellLayout = .column
@State private var returnFocus: String?
@@ -94,13 +96,13 @@ public struct LibraryView: View {
} else if let selected {
library(selected, layout: layout, size: geometry.size, insets: insets)
.frame(width: width, height: geometry.size.height)
- .opacity(paneCovers ? 0 : 1)
+ .opacity(paneCovers && edgeDrag == 0 ? 0 : 1)
.allowsHitTesting(!settingsOpen)
.accessibilityHidden(settingsOpen || helpGameID != nil)
.transformPreference(FocusTargets.self) { if settingsOpen { $0.removeAll() } }
if panelOverLibrary {
// The same dimming and dismissal as the Controllers overlay.
- Color.black.opacity(0.55).ignoresSafeArea()
+ Color.black.opacity(0.55 * (settingsOpen && !controllersOpen ? 1 - min(1, edgeDrag / pane) : 1)).ignoresSafeArea()
.onTapGesture { closeSettings() }
.accessibilityHidden(true)
.transition(.opacity)
@@ -117,7 +119,7 @@ public struct LibraryView: View {
.overlay(alignment: .leading) {
if floating { Rectangle().fill(ShellPalette.hairline).frame(width: 1).ignoresSafeArea() }
}
- .offset(x: settingsOpen ? 0 : pane + 40)
+ .offset(x: settingsOpen ? (controllersOpen ? 0 : edgeDrag) : pane + 40)
.opacity(settingsOpen ? 1 : 0)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .trailing)
.allowsHitTesting(settingsOpen)
@@ -149,13 +151,14 @@ public struct LibraryView: View {
.transition(.opacity.combined(with: .scale(scale: 0.96)))
}
if controllersOpen {
- Color.black.opacity(0.55).ignoresSafeArea().accessibilityHidden(true)
+ Color.black.opacity(0.55 * (1 - min(1, edgeDrag / min(width, 520)))).ignoresSafeArea().accessibilityHidden(true)
.onTapGesture { back() }
ControllersView(controllers: controllers, onBack: back)
.frame(width: min(width, 520))
.frame(maxHeight: .infinity)
.background { ShellPalette.background.ignoresSafeArea() }
.overlay(alignment: .leading) { Rectangle().fill(ShellPalette.hairline).frame(width: 1).ignoresSafeArea() }
+ .offset(x: edgeDrag)
.frame(maxWidth: .infinity, alignment: .trailing)
.accessibilityAddTraits(.isModal)
.transition(.move(edge: .trailing).combined(with: .opacity))
@@ -170,6 +173,12 @@ public struct LibraryView: View {
}
}
.coordinateSpace(name: "shell")
+ .background {
+ EdgeSwipeBack(enabled: (settingsOpen || controllersOpen) && choice == nil && helpGameID == nil,
+ changed: { edgeDrag = $0 },
+ ended: { finishEdgeSwipe(travel: $0, velocity: $1, span: controllersOpen ? min(width, 520) : pane) },
+ cancelled: cancelEdgeSwipe)
+ }
.onPreferenceChange(FocusTargets.self) { all in targets.values = all.filter { $0.frame.width > 1 } }
.onAppear { self.layout = layout }
.onChange(of: layout) { _, new in self.layout = new }
@@ -292,6 +301,21 @@ public struct LibraryView: View {
actions.onSelect(id)
settingsPosition.scrollTo(edge: .top)
}
+ /// The swipe lets go: past a third of the panel or flung to the right, it finishes the slide out from where the
+ /// finger is and then closes without a second animation. Otherwise the panel springs back.
+ private func finishEdgeSwipe(travel: CGFloat, velocity: CGFloat, span: CGFloat) {
+ guard travel > span / 3 || velocity > 600 else { cancelEdgeSwipe(); return }
+ let close = {
+ var transaction = Transaction()
+ transaction.disablesAnimations = true
+ withTransaction(transaction) { back(); edgeDrag = 0 }
+ }
+ if reduceMotion { close(); return }
+ withAnimation(.smooth(duration: 0.25), completionCriteria: .logicallyComplete) { edgeDrag = span + 40 } completion: { close() }
+ }
+ private func cancelEdgeSwipe() {
+ withAnimation(reduceMotion ? nil : .smooth(duration: 0.25)) { edgeDrag = 0 }
+ }
private func openSettings() {
noteTouch()
settingsOpen = true
diff --git a/frontend-ios/UITests/ShellDemoUITests.swift b/frontend-ios/UITests/ShellDemoUITests.swift
index ba0a6e1a..876b0562 100644
--- a/frontend-ios/UITests/ShellDemoUITests.swift
+++ b/frontend-ios/UITests/ShellDemoUITests.swift
@@ -41,6 +41,36 @@ import XCTest
app.buttons["controllers.close"].tap()
XCTAssertFalse(app.staticTexts["Pro Controller"].exists)
}
+ /// Swipes from the left screen edge to `fraction` of the width. A slow release at a short distance cancels.
+ private func edgeSwipe(to fraction: CGFloat, fling: Bool) {
+ let start = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0.5)).withOffset(CGVector(dx: 1, dy: 0))
+ let end = app.coordinate(withNormalizedOffset: CGVector(dx: fraction, dy: 0.5))
+ start.press(forDuration: 0.05, thenDragTo: end, withVelocity: fling ? .fast : .slow, thenHoldForDuration: fling ? 0 : 0.5)
+ }
+ func testEdgeSwipeClosesSettingsAndCanBeCancelled() {
+ XCUIDevice.shared.orientation = .portrait
+ launch("library")
+ app.buttons["settings.open"].tap()
+ XCTAssertTrue(app.buttons["library.back"].waitForExistence(timeout: 5))
+ edgeSwipe(to: 0.1, fling: false)
+ XCTAssertTrue(app.buttons["library.back"].waitForExistence(timeout: 5), "A short swipe let go slowly springs back")
+ XCTAssertTrue(app.buttons["library.back"].isHittable)
+ edgeSwipe(to: 0.9, fling: true)
+ expectation(for: NSPredicate(format: "isHittable == false"), evaluatedWith: app.buttons["library.back"])
+ waitForExpectations(timeout: 5)
+ XCTAssertTrue(app.buttons["settings.open"].isHittable)
+ capture("edge-swipe-back-to-library")
+ }
+ func testEdgeSwipeClosesControllers() {
+ XCUIDevice.shared.orientation = .portrait
+ launch("library")
+ app.buttons["controllers.open"].tap()
+ XCTAssertTrue(app.buttons["controllers.close"].waitForExistence(timeout: 5))
+ edgeSwipe(to: 0.9, fling: true)
+ expectation(for: NSPredicate(format: "exists == false"), evaluatedWith: app.buttons["controllers.close"])
+ waitForExpectations(timeout: 5)
+ XCTAssertTrue(app.buttons["controllers.open"].isHittable)
+ }
func testShortShelfSelects() {
XCUIDevice.shared.orientation = .landscapeLeft
extraArguments = ["--games", "3"]