Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/application.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions apple-tests/editor/CapturePresentationProbe.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
158 changes: 158 additions & 0 deletions swift/JournalCapturePresentation.swift
Original file line number Diff line number Diff line change
@@ -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<AnyView> {
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
7 changes: 7 additions & 0 deletions swift/JournalChrome.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
Loading