From f41e423666c6f437884e7ed1ca714f3ed421b638 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Fri, 27 Nov 2026 14:29:03 +0800 Subject: [PATCH] Fix capture presentation fallback ordering --- app/application.ml | 7 +- .../editor/CapturePresentationProbe.swift | 69 ++++++++ swift/JournalCapturePresentation.swift | 158 ++++++++++++++++++ swift/JournalChrome.swift | 7 + 4 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 apple-tests/editor/CapturePresentationProbe.swift create mode 100644 swift/JournalCapturePresentation.swift diff --git a/app/application.ml b/app/application.ml index 058b29b4..2b964972 100644 --- a/app/application.ml +++ b/app/application.ml @@ -8262,7 +8262,12 @@ let start match state.modal with | Capture_sheet when state.environment.platform = "ios" -> Option.map - (fun content -> V.column ~spacing:0. [ content ]) + (fun content -> + V.of_lui + (Journal_lui_native.mount ~key:"journal-capture-presentation" + ~payload:{|{"mode":"capture-presentation"}|} + ~children:[ Journal_view.mount content ] + Journal_lui_native.chrome_identifier)) (capture_composer state ~render:composer_content) | Append_sheet when state.environment.platform = "ios" -> append_composer state ~render:composer_content diff --git a/apple-tests/editor/CapturePresentationProbe.swift b/apple-tests/editor/CapturePresentationProbe.swift new file mode 100644 index 00000000..d18ae427 --- /dev/null +++ b/apple-tests/editor/CapturePresentationProbe.swift @@ -0,0 +1,69 @@ +import GameController +import SwiftUI +import UIKit + +@main final class CapturePresentationProbe: UIResponder, UIApplicationDelegate { + var window: UIWindow? + + func application(_ application: UIApplication, didFinishLaunchingWithOptions options: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + let window = UIWindow(frame: UIScreen.main.bounds) + let root = UIViewController() + window.rootViewController = root + self.window = window + window.makeKeyAndVisible() + Task { await run(root: root, window: window) } + return true + } + + private func run(root: UIViewController, window: UIWindow) async { + let getter = class_getClassMethod(GCKeyboard.self, #selector(getter: GCKeyboard.coalesced))! + let disconnected: @convention(block) (AnyObject) -> GCKeyboard? = { _ in nil } + let replacement = imp_implementationWithBlock(disconnected) + let original = method_setImplementation(getter, replacement) + defer { + method_setImplementation(getter, original) + imp_removeBlock(replacement) + } + precondition(!UIAccessibility.isReduceMotionEnabled && !UIAccessibility.isVoiceOverRunning) + var failures = 0 + func expect(_ condition: Bool, _ message: String) { + if !condition { failures += 1 } + NSLog("CAPTURE TEST %@: %@", condition ? "PASS" : "FAIL", message) + } + for scenario in ["unclassified", "docked", "early input", "missing editing"] { + let host = JournalCapturePresentation.Host(content: AnyView(Text("Draft")), interactive: true) + root.addChild(host) + host.view.frame = CGRect(x: 0, y: window.bounds.maxY - window.safeAreaInsets.bottom - 100, width: window.bounds.width, height: 100) + root.view.addSubview(host.view) + host.didMove(toParent: root) + let editor = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 30)) + host.view.addSubview(editor) + expect(host.view.layer.opacity == 0, "\(scenario) starts hidden") + if scenario != "missing editing" { + NotificationCenter.default.post(name: UITextView.textDidBeginEditingNotification, object: editor) + } + if scenario == "early input" { + NotificationCenter.default.post(name: UITextView.textDidChangeNotification, object: editor) + expect(host.view.layer.opacity == 1, "text input reveals synchronously") + } else if scenario == "docked" { + try? await Task.sleep(for: .milliseconds(115)) + let frame = CGRect(x: 0, y: window.bounds.maxY - 300, width: window.bounds.width, height: 300) + NotificationCenter.default.post(name: UIResponder.keyboardWillChangeFrameNotification, object: window.screen, + userInfo: [UIResponder.keyboardFrameEndUserInfoKey: NSValue(cgRect: window.convert(frame, to: window.screen.coordinateSpace))]) + try? await Task.sleep(for: .milliseconds(65)) + expect(host.view.layer.opacity == 0, "docked keyboard at rest stays hidden past the editing window") + host.view.frame.origin.y -= 75 + try? await Task.sleep(for: .milliseconds(50)) + expect(host.view.layer.opacity == 1, "docked keyboard reveals on lift") + } else { + try? await Task.sleep(for: .milliseconds(scenario == "unclassified" ? 190 : 270)) + expect(host.view.layer.opacity == 1, "\(scenario) fails open before the old watchdog") + } + host.stopMonitoring() + host.willMove(toParent: nil) + host.view.removeFromSuperview() + host.removeFromParent() + } + NSLog("CAPTURE TEST END failures=%d", failures) + } +} diff --git a/swift/JournalCapturePresentation.swift b/swift/JournalCapturePresentation.swift new file mode 100644 index 00000000..5bae4243 --- /dev/null +++ b/swift/JournalCapturePresentation.swift @@ -0,0 +1,158 @@ +#if os(iOS) +import GameController +import SwiftUI +import UIKit + +struct JournalCapturePresentation: UIViewControllerRepresentable { + let content: AnyView + let interactive: Bool + + func makeUIViewController(context: Context) -> Host { + Host(content: content, interactive: interactive) + } + + func updateUIViewController(_ controller: Host, context: Context) { + controller.rootView = content + if !interactive { controller.reveal() } + } + + func sizeThatFits(_ proposal: ProposedViewSize, uiViewController: Host, context: Context) -> CGSize? { + guard let width = proposal.width, width.isFinite, width > 0 else { return nil } + let size = uiViewController.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude)) + return CGSize(width: width, height: size.height) + } + + static func dismantleUIViewController(_ controller: Host, coordinator: ()) { + controller.stopMonitoring() + } + + final class Host: UIHostingController { + private var waiting: Bool + private var pendingKeyboardFrame: (screen: UIScreen, frame: CGRect)? + private var keyboardOpening = false + private var displayLink: CADisplayLink? + private var loadedAt: ContinuousClock.Instant? + private var editingDeadline: ContinuousClock.Instant? + + init(content: AnyView, interactive: Bool) { + waiting = interactive + super.init(rootView: content) + } + + @MainActor required dynamic init?(coder: NSCoder) { return nil } + + override func viewDidLoad() { + super.viewDidLoad() + view.backgroundColor = .clear + guard waiting else { return } + guard !shouldBypass else { waiting = false; return } + setPaint(visible: false) + loadedAt = .now + + let center = NotificationCenter.default + center.addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), + name: UIResponder.keyboardWillChangeFrameNotification, object: nil) + for name in [UITextView.textDidBeginEditingNotification, UITextField.textDidBeginEditingNotification] { + center.addObserver(self, selector: #selector(editingBegan(_:)), name: name, object: nil) + } + for name in [UITextView.textDidChangeNotification, UITextField.textDidChangeNotification] { + center.addObserver(self, selector: #selector(textChanged(_:)), name: name, object: nil) + } + let link = CADisplayLink(target: self, selector: #selector(sampleLift)) + link.add(to: .main, forMode: .common) + displayLink = link + } + + private var shouldBypass: Bool { + UIAccessibility.isVoiceOverRunning || UIAccessibility.isReduceMotionEnabled + || GCKeyboard.coalesced != nil + } + + private func ownsEditor(_ notification: Notification) -> Bool { + guard let editor = notification.object as? UIView else { return false } + return editor.isDescendant(of: view) + } + + @objc private func editingBegan(_ notification: Notification) { + guard waiting, editingDeadline == nil, ownsEditor(notification) else { return } + editingDeadline = ContinuousClock.now + .milliseconds(150) + } + + @objc private func textChanged(_ notification: Notification) { + guard waiting, !keyboardOpening, pendingKeyboardFrame == nil, ownsEditor(notification) else { return } + reveal() + } + + @objc private func keyboardWillChangeFrame(_ notification: Notification) { + guard waiting, let screen = notification.object as? UIScreen, + let frame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue + else { return } + guard let window = view.window, window.isKeyWindow else { + if view.window == nil || view.window?.screen === screen { + pendingKeyboardFrame = (screen, frame) + } + return + } + guard screen === window.screen else { return } + pendingKeyboardFrame = nil + applyKeyboardFrame(frame, in: window) + } + + private func applyKeyboardFrame(_ frame: CGRect, in window: UIWindow) { + let overlap = window.bounds.intersection(window.convert(frame, from: window.screen.coordinateSpace)) + let docked = !overlap.isNull && overlap.height > window.safeAreaInsets.bottom + 1 + && overlap.maxY >= window.bounds.maxY - 1 + if docked { + keyboardOpening = true + } else { + reveal() + } + } + + @objc private func sampleLift() { + guard waiting else { return } + if shouldBypass { reveal(); return } + if let window = view.window, window.isKeyWindow, let pending = pendingKeyboardFrame { + pendingKeyboardFrame = nil + if pending.screen === window.screen { applyKeyboardFrame(pending.frame, in: window) } + } + guard waiting, let loadedAt else { return } + let limit = loadedAt + .milliseconds(keyboardOpening ? 300 : 240) + let deadline = keyboardOpening ? limit : min(limit, editingDeadline ?? limit) + if ContinuousClock.now >= deadline { reveal(); return } + guard keyboardOpening, let window = view.window, window.isKeyWindow, + let presented = view.layer.presentation(), !presented.bounds.isEmpty, + let target = window.layer.presentation() + else { return } + let bottom = presented.convert(presented.bounds, to: target).maxY + let restBottom = window.bounds.maxY - window.safeAreaInsets.bottom + if bottom < restBottom - 0.25 { reveal() } + } + + func reveal() { + guard waiting else { return } + waiting = false + if isViewLoaded { setPaint(visible: true) } + stopMonitoring() + } + + private func setPaint(visible: Bool) { + CATransaction.begin() + CATransaction.setDisableActions(true) + view.layer.opacity = visible ? 1 : 0 + CATransaction.commit() + view.accessibilityElementsHidden = !visible + view.isUserInteractionEnabled = visible + } + + func stopMonitoring() { + pendingKeyboardFrame = nil + displayLink?.invalidate() + displayLink = nil + loadedAt = nil + editingDeadline = nil + NotificationCenter.default.removeObserver(self) + } + } +} +#endif diff --git a/swift/JournalChrome.swift b/swift/JournalChrome.swift index 4c18cedb..f3805015 100644 --- a/swift/JournalChrome.swift +++ b/swift/JournalChrome.swift @@ -244,6 +244,7 @@ struct JournalSheetDismissButton: SwiftUI.View { case graphPicker = "graph-picker" case toolbarControl = "toolbar-control" case bottomBar = "bottom-bar" + case capturePresentation = "capture-presentation" case detailText = "detail-text" case boundedText = "bounded-text" case filterOverlay = "filter-overlay" @@ -1743,6 +1744,12 @@ struct JournalSheetDismissButton: SwiftUI.View { } } #endif + case .capturePresentation: + #if os(iOS) + JournalCapturePresentation(content: child(0), interactive: context.isUserInteractionEnabled) + #else + child(0) + #endif case .toolbarControl: if context.childIDs.count == 1 { child(0).fixedSize()