Skip to content

Commit bc8f318

Browse files
committed
Reuse view update results when views are fixed size
1 parent 37cf136 commit bc8f318

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Sources/SwiftCrossUI/ViewGraph/ViewGraphNode.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,34 @@ public class ViewGraphNode<NodeView: View, Backend: AppBackend> {
210210
// since the last update cycle (checked via`!sizeCache.isEmpty`) to
211211
// ensure that the view has been updated at least once with the
212212
// current view state.
213-
if dryRun, let currentResult, !resultCache.isEmpty,
214-
((Double(lastProposedSize.x) >= currentResult.size.maximumWidth
213+
if dryRun, let currentResult, !resultCache.isEmpty {
214+
// If both the previous and current proposed sizes are larger than
215+
// the view's previously computed maximum size, reuse the previous
216+
// result (currentResult).
217+
if ((Double(lastProposedSize.x) >= currentResult.size.maximumWidth
215218
&& Double(proposedSize.x) >= currentResult.size.maximumWidth)
216219
|| proposedSize.x == lastProposedSize.x)
217220
&& ((Double(lastProposedSize.y) >= currentResult.size.maximumHeight
218221
&& Double(proposedSize.y) >= currentResult.size.maximumHeight)
219222
|| proposedSize.y == lastProposedSize.y)
220-
{
221-
return currentResult
223+
{
224+
return currentResult
225+
}
226+
227+
// If the view has already been updated this update cycle and claims
228+
// to be fixed size (maximumSize == minimumSize) then reuse the current
229+
// result.
230+
let maximumSize = SIMD2(
231+
currentResult.size.maximumWidth,
232+
currentResult.size.maximumHeight
233+
)
234+
let minimumSize = SIMD2(
235+
Double(currentResult.size.minimumWidth),
236+
Double(currentResult.size.minimumHeight)
237+
)
238+
if maximumSize == minimumSize {
239+
return currentResult
240+
}
222241
}
223242

224243
parentEnvironment = environment

0 commit comments

Comments
 (0)