Repository navigation
Expand file tree
/
Copy pathRunLoop+Presentation.swift
More file actions
209 lines (193 loc) · 7.09 KB
/
Copy pathRunLoop+Presentation.swift
File metadata and controls
209 lines (193 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import SwiftTUICore
import SwiftTUIViews
package enum PresentationSurfaceRoleError: Error, Equatable, Sendable {
case missingTerminalCommandSurface
case missingRasterPresentationSurface
}
extension RunLoop {
package var usesTerminalCursorForTextInput: Bool {
runtimeConfiguration.output == .tui
&& presentationSurface is any TerminalCursorFocusPresentationSurface
}
package func presentCommittedFrame(
_ artifacts: FrameArtifacts,
damage: PresentationDamage?,
geometry: HostGeometryStamp? = nil
) throws -> TerminalPresentationMetrics {
if runtimeConfiguration.output == .json {
return try presentJSONFrame(
artifacts,
focusedIdentity: focusTracker.currentFocusIdentity
)
}
let metrics: TerminalPresentationMetrics
if let semanticHostFrameSurface =
presentationSurface as? any SemanticHostFramePresentationSurface
{
let sequence = nextSemanticHostFrameSequence
nextSemanticHostFrameSequence &+= 1
var frame = SemanticHostFrame(
sequence: sequence,
raster: artifacts.rasterSurface,
semantics: semanticSnapshotWithScrollOffsets(artifacts.semanticSnapshot),
focusedIdentity: focusTracker.currentFocusIdentity,
rasterDamage: damage,
preferredLayoutSize: preferredHostLayoutSize(for: artifacts)
)
frame.hostGeometryStamp = geometry
metrics = try semanticHostFrameSurface.present(frame)
} else if let damageAwareHost = presentationSurface as? any DamageAwarePresentationSurface {
metrics = try damageAwareHost.present(
artifacts.rasterSurface,
damage: damage
)
} else if let rasterSurface = presentationSurface as? any RasterPresentationSurface {
metrics = try rasterSurface.present(artifacts.rasterSurface)
} else {
throw PresentationSurfaceRoleError.missingRasterPresentationSurface
}
try applyTerminalCursorFocusPolicy(semanticSnapshot: artifacts.semanticSnapshot)
return metrics
}
/// Returns the snapshot with each scroll route's `contentOffset` populated
/// from the live scroll-position registry, so every `SemanticHostFrame` host
/// (web, Android, native SwiftUI) can publish per-region scroll-extent
/// metadata: viewport rect, live offset, and content size. Hosts use it for
/// scroll-chaining (web) and may use it for native nested-scroll routing.
/// Returns the snapshot unchanged when it has no scrollable regions. Applied
/// only on the `SemanticHostFrame` presentation path; the stored/committed
/// snapshot is left untouched so frame-reuse equality is unaffected. See
/// `docs/proposals/EMBEDDED_WEB_SCROLL_CHAINING.md` and
/// `docs/plans/2026-06-19-001-cross-host-scrolling-plan.md` in the
/// coordination root.
private func semanticSnapshotWithScrollOffsets(
_ snapshot: SemanticSnapshot
) -> SemanticSnapshot {
var enriched = snapshot
enriched.accessibilityActionResponse = latestAccessibilityActionResponse
guard !snapshot.scrollRoutes.isEmpty else { return enriched }
enriched.scrollRoutes = localScrollPositionRegistry.routesWithCurrentOffsets(
snapshot.scrollRoutes
)
return enriched
}
private func applyTerminalCursorFocusPolicy(
semanticSnapshot: SemanticSnapshot
) throws {
guard runtimeConfiguration.output == .tui else {
return
}
guard
let terminalSurface = presentationSurface as? any TerminalCursorFocusPresentationSurface
else {
return
}
let cursorPoint: CellPoint?
if runtimeConfiguration.cursorFollowsFocus {
cursorPoint = AccessibilityRuntimePolicy().focusedCursorPoint(
in: semanticSnapshot,
focusedIdentity: focusTracker.currentFocusIdentity
)
} else {
// By default the hardware cursor is only a text caret. Show it at the
// focused text input's caret; otherwise leave it untouched, except to
// hide a caret shown on an earlier frame so it does not linger where
// focus left. Editing controls without a caret (Slider, Stepper,
// Picker) and authored cursor anchors (used only when cursor-following)
// write nothing.
cursorPoint = focusedTextInputCaret(in: semanticSnapshot)
guard cursorPoint != nil || terminalCursorFocusShowsCursor else {
return
}
}
try terminalSurface.presentAccessibilityCursorFocus(at: cursorPoint)
terminalCursorFocusShowsCursor = cursorPoint != nil
}
/// The focused text input's caret cell. A SecureField withholds its
/// `textInput` value but still anchors the cursor at its caret.
private func focusedTextInputCaret(
in semanticSnapshot: SemanticSnapshot
) -> CellPoint? {
guard let focusedIdentity = focusTracker.currentFocusIdentity,
let node = semanticSnapshot.accessibilityNodes.first(where: {
($0.actionIdentity ?? $0.identity) == focusedIdentity
}),
node.textInput != nil || node.role == .secureField
else {
return nil
}
return node.cursorAnchor
}
private func presentJSONFrame(
_ artifacts: FrameArtifacts,
focusedIdentity: Identity?
) throws -> TerminalPresentationMetrics {
let output = JSONFrameRenderer().render(
surface: artifacts.rasterSurface,
semanticSnapshot: artifacts.semanticSnapshot,
focusedIdentity: focusedIdentity
)
guard
let terminalCommandSurface =
presentationSurface as? any TerminalCommandPresentationSurface
else {
throw PresentationSurfaceRoleError.missingTerminalCommandSurface
}
try terminalCommandSurface.write(output)
return metrics(forWrittenOutput: output)
}
private func metrics(
forWrittenOutput output: String
) -> TerminalPresentationMetrics {
let bytesWritten = output.utf8.count
let linesTouched = output.utf8.reduce(0) { partial, byte in
partial + (byte == 0x0A ? 1 : 0)
}
return TerminalPresentationMetrics(
bytesWritten: bytesWritten,
linesTouched: linesTouched,
cellsChanged: max(0, bytesWritten - linesTouched),
strategy: .fullRepaint
)
}
}
private func preferredHostLayoutSize(
for artifacts: FrameArtifacts
) -> CellSize? {
preferredWindowContentSize(
resolved: artifacts.resolvedTree,
measured: artifacts.measuredTree
)
}
private func preferredWindowContentSize(
resolved: ResolvedNode,
measured: MeasuredNode
) -> CellSize? {
if isWindowHostLayout(resolved) {
let childMeasurements = measured.childMeasurements
guard !childMeasurements.isEmpty else {
return measured.measuredSize
}
return childMeasurements.reduce(into: CellSize.zero) { partial, child in
partial.width = max(partial.width, child.measuredSize.width)
partial.height = max(partial.height, child.measuredSize.height)
}
}
for (resolvedChild, measuredChild) in zip(resolved.children, measured.childMeasurements) {
if let size = preferredWindowContentSize(
resolved: resolvedChild,
measured: measuredChild
) {
return size
}
}
return nil
}
private func isWindowHostLayout(
_ node: ResolvedNode
) -> Bool {
guard case .view("WindowHostLayout") = node.kind else {
return false
}
return true
}