Skip to content

Commit

Permalink
Fix position issue (#52)
Browse files Browse the repository at this point in the history
* Fix wrong position issue

* Run spotless apply
  • Loading branch information
MohamedRejeb authored Dec 10, 2023
1 parent e16b2c4 commit 289c34c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class HazeArea {
val isEmpty: Boolean get() = bounds.isEmpty
}

internal fun HazeArea.boundsInLocal(boundsInRoot: Rect): Rect {
return bounds.translate(-boundsInRoot.left, -boundsInRoot.top)
internal fun HazeArea.boundsInLocal(positionInRoot: Offset): Rect {
return bounds.translate(-positionInRoot.x, -positionInRoot.y)
}

internal fun HazeArea.createOutline(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package dev.chrisbanes.haze

import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.node.LayoutAwareModifierNode
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo
Expand Down Expand Up @@ -51,7 +52,15 @@ private data class HazeChildNode(
) : Modifier.Node(), LayoutAwareModifierNode {
override fun onPlaced(coordinates: LayoutCoordinates) {
// After we've been placed, update the state with our new bounds (in root coordinates)
state.updateArea(key, coordinates.boundsInRoot(), shape)
val positionInRoot = coordinates.positionInRoot()
val size = coordinates.size
val bounds = Rect(
left = positionInRoot.x,
top = positionInRoot.y,
right = positionInRoot.x + size.width,
bottom = positionInRoot.y + size.height,
)
state.updateArea(key, bounds, shape)
}

override fun onReset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.graphics.RenderNode
import android.graphics.Shader
import android.graphics.Shader.TileMode.REPEAT
import androidx.annotation.RequiresApi
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Color
Expand All @@ -26,7 +27,7 @@ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.node.CompositionLocalConsumerModifierNode
import androidx.compose.ui.node.DrawModifierNode
import androidx.compose.ui.node.LayoutAwareModifierNode
Expand Down Expand Up @@ -63,7 +64,7 @@ internal class HazeNode31(

private var effectsDirty = true
private var effects: List<EffectHolder> = emptyList()
private var boundsInRoot = Rect.Zero
private var positionInRoot = Offset.Zero

private var noiseTexture: Bitmap? = null
private var noiseTextureFactor: Float = Float.MIN_VALUE
Expand All @@ -79,9 +80,9 @@ internal class HazeNode31(
}

override fun onPlaced(coordinates: LayoutCoordinates) {
val newBoundsInRoot = coordinates.boundsInRoot()
if (boundsInRoot != newBoundsInRoot) {
boundsInRoot = newBoundsInRoot
val newPositionInRoot = coordinates.positionInRoot()
if (positionInRoot != newPositionInRoot) {
positionInRoot = newPositionInRoot

effectsDirty = true
invalidateDraw()
Expand Down Expand Up @@ -167,7 +168,7 @@ internal class HazeNode31(

// We create a RenderNode for each of the areas we need to apply our effect to
return state.areas.asSequence().map { area ->
val bounds = area.boundsInLocal(boundsInRoot)
val bounds = area.boundsInLocal(positionInRoot)

// We expand the area where our effect is applied to. This is necessary so that the blur
// effect is applied evenly to all edges. If we don't do this, the blur effect is much less
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

package dev.chrisbanes.haze

import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.node.CompositionLocalConsumerModifierNode
import androidx.compose.ui.node.DrawModifierNode
import androidx.compose.ui.node.LayoutAwareModifierNode
Expand Down Expand Up @@ -38,7 +38,7 @@ internal class HazeNodeBase(

private val path = Path()
private var pathDirty = false
private var boundsInRoot = Rect.Zero
private var positionInRoot = Offset.Zero

override fun onUpdate() {
invalidateDraw()
Expand All @@ -54,9 +54,9 @@ internal class HazeNodeBase(
}

override fun onPlaced(coordinates: LayoutCoordinates) {
val newBoundsInRoot = coordinates.boundsInRoot()
if (boundsInRoot != newBoundsInRoot) {
boundsInRoot = newBoundsInRoot
val newPositionInRoot = coordinates.positionInRoot()
if (positionInRoot != newPositionInRoot) {
positionInRoot = newPositionInRoot
markPathAsDirty()
}
}
Expand Down
12 changes: 6 additions & 6 deletions haze/src/androidMain/kotlin/dev/chrisbanes/haze/HazeNodeBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

package dev.chrisbanes.haze

import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.node.CompositionLocalConsumerModifierNode
import androidx.compose.ui.node.DrawModifierNode
import androidx.compose.ui.node.LayoutAwareModifierNode
Expand Down Expand Up @@ -38,7 +38,7 @@ internal class HazeNodeBase(

private val path = Path()
private var pathDirty = false
private var boundsInRoot = Rect.Zero
private var positionInRoot = Offset.Zero

override fun onUpdate() {
invalidateDraw()
Expand All @@ -54,9 +54,9 @@ internal class HazeNodeBase(
}

override fun onPlaced(coordinates: LayoutCoordinates) {
val newBoundsInRoot = coordinates.boundsInRoot()
if (boundsInRoot != newBoundsInRoot) {
boundsInRoot = newBoundsInRoot
val newPositionInRoot = coordinates.positionInRoot()
if (positionInRoot != newPositionInRoot) {
positionInRoot = newPositionInRoot
markPathAsDirty()
}
}
Expand Down
4 changes: 2 additions & 2 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/Haze.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class HazeArea {
val isEmpty: Boolean get() = bounds.isEmpty
}

internal fun HazeArea.boundsInLocal(boundsInRoot: Rect): Rect {
return bounds.translate(-boundsInRoot.left, -boundsInRoot.top)
internal fun HazeArea.boundsInLocal(positionInRoot: Offset): Rect {
return bounds.translate(-positionInRoot.x, -positionInRoot.y)
}

internal fun HazeArea.createOutline(
Expand Down
13 changes: 11 additions & 2 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/HazeChild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package dev.chrisbanes.haze

import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.node.LayoutAwareModifierNode
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo
Expand Down Expand Up @@ -51,7 +52,15 @@ private data class HazeChildNode(
) : Modifier.Node(), LayoutAwareModifierNode {
override fun onPlaced(coordinates: LayoutCoordinates) {
// After we've been placed, update the state with our new bounds (in root coordinates)
state.updateArea(key, coordinates.boundsInRoot(), shape)
val positionInRoot = coordinates.positionInRoot()
val size = coordinates.size
val bounds = Rect(
left = positionInRoot.x,
top = positionInRoot.y,
right = positionInRoot.x + size.width,
bottom = positionInRoot.y + size.height,
)
state.updateArea(key, bounds, shape)
}

override fun onReset() {
Expand Down
14 changes: 7 additions & 7 deletions haze/src/skikoMain/kotlin/dev/chrisbanes/haze/HazeNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package dev.chrisbanes.haze

import androidx.compose.foundation.shape.CornerBasedShape
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.BlurEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RenderEffect
import androidx.compose.ui.graphics.asComposeRenderEffect
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.node.CompositionLocalConsumerModifierNode
import androidx.compose.ui.node.LayoutModifierNode
import androidx.compose.ui.node.ObserverModifierNode
Expand Down Expand Up @@ -127,22 +127,22 @@ private class SkiaHazeNode(
val placeable = measurable.measure(constraints)
return layout(placeable.width, placeable.height) {
placeable.placeWithLayer(x = 0, y = 0) {
renderEffect = getOrCreateRenderEffect(coordinates?.boundsInRoot() ?: Rect.Zero)
renderEffect = getOrCreateRenderEffect(coordinates?.positionInRoot() ?: Offset.Zero)
}
}
}

private fun getOrCreateRenderEffect(boundsInRoot: Rect): RenderEffect? {
private fun getOrCreateRenderEffect(positionInRoot: Offset): RenderEffect? {
if (renderEffectDirty) {
observeReads {
hazeRenderEffect = createHazeRenderEffect(boundsInRoot)
hazeRenderEffect = createHazeRenderEffect(positionInRoot)
}
renderEffectDirty = false
}
return hazeRenderEffect
}

private fun createHazeRenderEffect(boundsInRoot: Rect): RenderEffect? {
private fun createHazeRenderEffect(positionInRoot: Offset): RenderEffect? {
if (state.areas.isEmpty()) {
return null
}
Expand All @@ -153,7 +153,7 @@ private class SkiaHazeNode(

val filters = state.areas.asSequence().map { area ->
val compositeShaderBuilder = RuntimeShaderBuilder(RUNTIME_SHADER).apply {
val areaLocalBounds = area.boundsInLocal(boundsInRoot)
val areaLocalBounds = area.boundsInLocal(positionInRoot)
uniform(
"rectangle",
areaLocalBounds.left,
Expand Down

0 comments on commit 289c34c

Please sign in to comment.