Skip to content

Commit

Permalink
Track max inflate of draw bounds to stabilize it during animation
Browse files Browse the repository at this point in the history
  • Loading branch information
MatkovIvan committed Mar 14, 2024
1 parent 5f1364b commit 421156a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ internal abstract class DesktopComposeSceneLayer(
protected abstract val mediator: ComposeSceneMediator?

/**
* Bounds of real drawings from previous render.
* Bounds of real drawings based on previous renders.
*/
protected var drawBounds = IntRect.Zero

/**
* The maximum amount to inflate the [drawBounds] comparing to [boundsInWindow].
*/
private var maxDrawInflate = IntRect.Zero

private var outsidePointerCallback: ((eventType: PointerEventType) -> Unit)? = null
private var isClosed = false

Expand Down Expand Up @@ -127,9 +132,14 @@ internal abstract class DesktopComposeSceneLayer(
protected fun recordDrawBounds(skikoView: SkikoView) =
RecordDrawRectSkikoViewDecorator(skikoView) { canvasBoundsInPx ->
val currentCanvasOffset = drawBounds.topLeft
val boundsInWindow = canvasBoundsInPx.roundToIntRect().translate(currentCanvasOffset)
.union(boundsInWindow)
drawBounds = boundsInWindow
val drawBoundsInWindow = canvasBoundsInPx.roundToIntRect().translate(currentCanvasOffset)
maxDrawInflate = maxInflate(boundsInWindow, drawBoundsInWindow, maxDrawInflate)
drawBounds = IntRect(
left = boundsInWindow.left - maxDrawInflate.left,
top = boundsInWindow.top - maxDrawInflate.top,
right = boundsInWindow.right + maxDrawInflate.right,
bottom = boundsInWindow.bottom + maxDrawInflate.bottom
)
onUpdateBounds()
}

Expand Down Expand Up @@ -258,9 +268,9 @@ internal abstract class DesktopComposeSceneLayer(
private fun MouseEvent.isMainAction() =
button == MouseEvent.BUTTON1

private fun IntRect.union(other: IntRect) = IntRect(
left = min(left, other.left),
top = min(top, other.top),
bottom = max(bottom, other.bottom),
right = max(right, other.right)
private fun maxInflate(baseBounds: IntRect, currentBounds: IntRect, maxInflate: IntRect) = IntRect(
left = max(baseBounds.left - currentBounds.left, maxInflate.left),
top = max(baseBounds.top - currentBounds.top, maxInflate.top),
right = max(currentBounds.right - baseBounds.right, maxInflate.right),
bottom = max(currentBounds.bottom - baseBounds.bottom, maxInflate.bottom)
)
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@ internal class UIViewComposeSceneLayer(
}

/**
* Bounds of real drawings from previous render.
* Bounds of real drawings based on previous renders.
*/
private var drawBounds = IntRect.Zero

/**
* The maximum amount to inflate the [drawBounds] comparing to [boundsInWindow].
*/
private var maxDrawInflate = IntRect.Zero

init {
backgroundView.translatesAutoresizingMaskIntoConstraints = false
rootView.addSubview(backgroundView)
Expand Down Expand Up @@ -210,9 +215,14 @@ internal class UIViewComposeSceneLayer(
private fun recordDrawBounds(renderDelegate: SkikoRenderDelegate) =
RecordDrawRectSkikoViewDecorator(renderDelegate) { canvasBoundsInPx ->
val currentCanvasOffset = drawBounds.topLeft
val boundsInWindow = canvasBoundsInPx.roundToIntRect().translate(currentCanvasOffset)
.union(boundsInWindow)
drawBounds = boundsInWindow
val drawBoundsInWindow = canvasBoundsInPx.roundToIntRect().translate(currentCanvasOffset)
maxDrawInflate = maxInflate(boundsInWindow, drawBoundsInWindow, maxDrawInflate)
drawBounds = IntRect(
left = boundsInWindow.left - maxDrawInflate.left,
top = boundsInWindow.top - maxDrawInflate.top,
right = boundsInWindow.right + maxDrawInflate.right,
bottom = boundsInWindow.bottom + maxDrawInflate.bottom
)
updateBounds()
}

Expand Down Expand Up @@ -249,9 +259,9 @@ internal class UIViewComposeSceneLayer(
}
}

private fun IntRect.union(other: IntRect) = IntRect(
left = min(left, other.left),
top = min(top, other.top),
bottom = max(bottom, other.bottom),
right = max(right, other.right)
private fun maxInflate(baseBounds: IntRect, currentBounds: IntRect, maxInflate: IntRect) = IntRect(
left = max(baseBounds.left - currentBounds.left, maxInflate.left),
top = max(baseBounds.top - currentBounds.top, maxInflate.top),
right = max(currentBounds.right - baseBounds.right, maxInflate.right),
bottom = max(currentBounds.bottom - baseBounds.bottom, maxInflate.bottom)
)

0 comments on commit 421156a

Please sign in to comment.