diff --git a/docs/contract-for-agents.md b/docs/contract-for-agents.md index bb7a809a4..43c985c1a 100644 --- a/docs/contract-for-agents.md +++ b/docs/contract-for-agents.md @@ -2404,6 +2404,7 @@ lists any literal colour, font size or weight an app's `.contract` files still w | a pushed route with no such `header` | a bar holding the system Back button alone (LLP 1115.000 D5.2) | | a route with `navigationBar="none"` | no navigation bar: a full-bleed screen that draws its own way back; the edge swipe still goes back | | a route with `navigationPresentation="modal"` | a sheet | +| a route pushed after a sheet, with no `navigationPresentation` of its own | pushed inside the sheet, with the system Back (the web: in the sheet's frame) | A grouped list section's `box-shadow`, including class and state values, paints around its card, excluding the header and footer. A section without a card diff --git a/docs/contract-for-humans.md b/docs/contract-for-humans.md index 9f63e842f..1797b5de0 100644 --- a/docs/contract-for-humans.md +++ b/docs/contract-for-humans.md @@ -1474,8 +1474,8 @@ A route pushed while a sheet is on top is shown inside that sheet, which keeps t detents of the route that opened it, and Back returns to the route before it in the sheet. iOS and macOS show it in the sheet's own navigation stack, the web and Android lay it out in the sheet's box, and desktop Linux in the box the sheet's -own route was laid out in. On Android a drag down or a tap on the scrim closes the -sheet with what was pushed inside it. A route pushed over a `fullscreen` route is +own route was laid out in. On iOS and Android a drag down or a tap on the scrim +closes the sheet with what was pushed inside it. A route pushed over a `fullscreen` route is a page of its own. `path("item", value)` checks the route and encodes its parameters. Always build diff --git a/host/apple/Sources/ExactKit/IOS/BackIOS.swift b/host/apple/Sources/ExactKit/IOS/BackIOS.swift index 520c56898..6e1f6958c 100644 --- a/host/apple/Sources/ExactKit/IOS/BackIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/BackIOS.swift @@ -74,7 +74,7 @@ extension NavigationHost { inAnyRoute: { control in routes.contains { control === $0 || control.isDescendant(of: $0) } }) } - private var selectedRoute: NodeView? { + var selectedRoute: NodeView? { guard let key = selectedKey else { return nil } return routeIDs.compactMap({ presenter.views[$0] }).first(where: { $0.props["navigationKey"] == key }) } diff --git a/host/apple/Sources/ExactKit/IOS/ModalIOS.swift b/host/apple/Sources/ExactKit/IOS/ModalIOS.swift index db67126d3..14009a3b7 100644 --- a/host/apple/Sources/ExactKit/IOS/ModalIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/ModalIOS.swift @@ -490,7 +490,7 @@ final class ModalHost: NSObject, UIAdaptivePresentationControllerDelegate { } func dismissByBackdrop(_ id: UInt32) { guard canDismissByBackdrop(id), let route = layers.last?.route else { return } - presenter.navigation.invokeBack(from: route) + goBack(from: route) } /// The agent's `back` on the top sheet (LLP 1116.006 D4): its swipe /// down, refused as UIKit's is (`presentationControllerShouldDismiss`) @@ -501,7 +501,7 @@ final class ModalHost: NSObject, UIAdaptivePresentationControllerDelegate { if inTransition { return "the sheet is still moving: `clock settle` first" } if NavigationRules.modalRefusesDismissal(closedby: route.props["closedby"]) { return "its closedby is \"none\"" } if refusesDismissal(of: route) { return presenter.navigation.backRefusal } - presenter.navigation.invokeBack(from: route) + goBack(from: route) return "" } @@ -812,8 +812,17 @@ final class ModalHost: NSObject, UIAdaptivePresentationControllerDelegate { } private func refusesDismissal(of route: NodeView) -> Bool { - presenter.views[route.id] !== route || presenter.navigation.isInactiveRoute(containing: route) || - NavigationRules.modalRefusesDismissal(closedby: route.props["closedby"]) || !presenter.navigation.canInvokeBack + if presenter.views[route.id] !== route || NavigationRules.modalRefusesDismissal(closedby: route.props["closedby"]) { return true } + // A route pushed inside the sheet leaves its own route inactive; the dismissal goes from the sheet's. + if presenter.navigation.isInactiveRoute(containing: route) { return presenter.navigation.sheetDismissal(route) == nil } + return !presenter.navigation.canInvokeBack + } + + /// The sheet's route goes back as its dismissal ends: the whole sheet, with + /// whatever was pushed inside it. + private func goBack(from route: NodeView) { + if let dismissal = presenter.navigation.sheetDismissal(route) { dismissal() } + else { presenter.navigation.invokeBack(from: route) } } func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool { @@ -850,8 +859,10 @@ final class ModalHost: NSObject, UIAdaptivePresentationControllerDelegate { // owner before dispatch, so an action may safely keep the same route. DispatchQueue.main.async { [weak self, layer] in guard let self, layers.last === layer else { return } + // Read while the sheet's navigation still holds what was pushed inside it. + let dismissal = presenter.navigation.sheetDismissal(layer.route) closeTop(animated: false, refit: false) - presenter.navigation.invokeBack(from: layer.route) + if let dismissal { dismissal() } else { presenter.navigation.invokeBack(from: layer.route) } presenter.navigation.modalDidDismiss() fit() } @@ -884,4 +895,28 @@ extension ModalHost: UISheetPresentationControllerDelegate { layers.first { $0.controller.sheetPresentationController === sheetPresentationController }?.controller.paintSurface() } } + +extension NavigationHost { + /// How a sheet's dismissal (its swipe down, a tap behind it, the agent's + /// `back`) goes back while a route pushed inside the sheet is selected: + /// from the sheet's own route, so the whole sheet goes, as on the Mac + /// (BackMac.swift `dismiss`). Its Back control is pressed, else the stack + /// goes back to the route beneath the sheet. nil when the selected route + /// is not pushed inside `sheet`, or when that way back is refused. + func sheetDismissal(_ sheet: NodeView) -> (() -> Void)? { + guard let root = container, let selected = selectedRoute, selected !== sheet, + let nav = controllers[sheet.id]?.navigationController, controllers[selected.id]?.navigationController === nav, + let key = selected.props["navigationKey"], let index = routeIDs.firstIndex(of: sheet.id), index > 0, + let beneath = presenter.views[routeIDs[index - 1]]?.props["navigationKey"] else { return nil } + let routes = routeIDs.compactMap { presenter.views[$0] } + let control = NavigationBack.control(named: root.props["navigationBack"], among: presenter.carrying("id"), + id: \.id, htmlID: { $0.props["id"] }, pressable: { $0.handlers.contains("press") }, disabled: \.disabled, + inActiveRoute: { $0 === sheet || $0.isDescendant(of: sheet) }, + inAnyRoute: { control in routes.contains { control === $0 || control.isDescendant(of: $0) } }) + let declared = presenter.carrying("id").contains { $0.props["id"] == root.props["navigationBack"] && ($0 === sheet || $0.isDescendant(of: sheet)) } + if let control { return { [weak self] in self?.presenter.press(control.id) } } + if declared { return nil } + return { [weak self] in self?.goBack(from: key, to: beneath) } + } +} #endif diff --git a/host/apple/tests/ExactKitTests/ModalStackIOSTests.swift b/host/apple/tests/ExactKitTests/ModalStackIOSTests.swift index 1b76a0473..f9918a6db 100644 --- a/host/apple/tests/ExactKitTests/ModalStackIOSTests.swift +++ b/host/apple/tests/ExactKitTests/ModalStackIOSTests.swift @@ -128,6 +128,30 @@ final class ModalStackIOSTests: XCTestCase { XCTAssertEqual(p.navigation.observation()["nativeBack"] as? String, backgroundBack) } + /// A sheet dismissed while a route is pushed inside it goes as a whole, back + /// to the route beneath the sheet, as its swipe down does. + func testDismissingASheetFromARoutePushedInsideItLeavesTheWholeSheet() throws { + guard UIApplication.shared.connectedScenes.contains(where: { $0 is UIWindowScene }) else { + throw missing("scene", "the secondary navigation stack uses UIKit's presented sheet") + } + let session = try fixture("pushed-inside-dismissal", plan: "EXACT_PUSHED_TITLE_PLAN", fullScreen: true) + let p = session.presenter, agent = Agent(session: session) + try press(session, "home-open-inline") + until("the background stack settles") { !agent.nativeInFlight() } + let primary = try XCTUnwrap(p.navigation.primaryNavigation) + let background = primary.viewControllers + let inlineKey = try XCTUnwrap((primary.topViewController as? RouteController)?.key) + try press(session, "inline-open-sheet") + until("the sheet's own navigation settles") { p.navigation.modalNavigation != nil && !agent.nativeInFlight() } + let secondary = try XCTUnwrap(p.navigation.modalNavigation) + try press(session, "sheet-open-long") + until("the push inside the sheet settles") { secondary.viewControllers.count == 2 && !agent.nativeInFlight() } + XCTAssertEqual(p.modals.agentDismiss(), "", "the sheet is dismissed from the route pushed inside it") + until("the sheet dismisses") { p.navigation.modalNavigation == nil && !agent.nativeInFlight() } + XCTAssertEqual(p.navigation.selectedKey, inlineKey) + XCTAssertEqual(primary.viewControllers, background) + } + func testBackHistoryUsesTheChosenDestinationInThePrimaryStack() throws { guard UIApplication.shared.connectedScenes.contains(where: { $0 is UIWindowScene }) else { throw missing("scene", "native Back history uses UIKit's completed transition")