Skip to content

Commit

Permalink
Bring CMP Android and Jetpack Compose code structure closer
Browse files Browse the repository at this point in the history
Will make code copying easier
  • Loading branch information
chrisbanes committed Dec 7, 2023
1 parent 7b1b61b commit 67398e0
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 66 deletions.
2 changes: 1 addition & 1 deletion haze-jetpack-compose/api/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ package dev.chrisbanes.haze {
property public final java.util.Map<java.lang.Object,dev.chrisbanes.haze.HazeArea> areas;
}

public final class RoundRectExtKt {
public final class PlatformKt {
}

}
Expand Down
29 changes: 7 additions & 22 deletions haze-jetpack-compose/src/main/kotlin/dev/chrisbanes/haze/Haze.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package dev.chrisbanes.haze

import android.os.Build
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
Expand Down Expand Up @@ -144,27 +143,13 @@ internal data class HazeNodeElement(
val blurRadius: Dp,
val noiseFactor: Float,
) : ModifierNodeElement<HazeNode>() {
override fun create(): HazeNode = when {
Build.VERSION.SDK_INT >= 31 -> {
HazeNode31(
state = state,
backgroundColor = backgroundColor,
tint = tint,
blurRadius = blurRadius,
noiseFactor = noiseFactor,
)
}

else -> {
HazeNodeBase(
state = state,
backgroundColor = backgroundColor,
tint = tint,
blurRadius = blurRadius,
noiseFactor = noiseFactor,
)
}
}
override fun create(): HazeNode = createHazeNode(
state = state,
backgroundColor = backgroundColor,
tint = tint,
blurRadius = blurRadius,
noiseFactor = noiseFactor,
)

override fun update(node: HazeNode) {
node.state = state
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import android.os.Build
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp

internal fun createHazeNode(
state: HazeState,
backgroundColor: Color,
tint: Color,
blurRadius: Dp,
noiseFactor: Float,
): HazeNode = when {
Build.VERSION.SDK_INT >= 31 -> {
HazeNode31(
state = state,
backgroundColor = backgroundColor,
tint = tint,
blurRadius = blurRadius,
noiseFactor = noiseFactor,
)
}

else -> {
HazeNodeBase(
state = state,
backgroundColor = backgroundColor,
tint = tint,
blurRadius = blurRadius,
noiseFactor = noiseFactor,
)
}
}

This file was deleted.

6 changes: 3 additions & 3 deletions haze/api/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ package dev.chrisbanes.haze {
public final class HazeNodeKt {
}

public final class HazeNodeKt {
}

@androidx.compose.runtime.Stable public final class HazeState {
ctor public HazeState();
method public java.util.Map<java.lang.Object,dev.chrisbanes.haze.HazeArea> getAreas();
method public void updateArea(Object key, androidx.compose.ui.geometry.Rect bounds, androidx.compose.ui.graphics.Shape shape);
property public final java.util.Map<java.lang.Object,dev.chrisbanes.haze.HazeArea> areas;
}

public final class PlatformKt {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,10 @@ import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection

internal actual fun createHazeNode(
state: HazeState,
backgroundColor: Color,
tint: Color,
blurRadius: Dp,
noiseFactor: Float,
): HazeNode = AndroidHazeNode(state, backgroundColor, tint, blurRadius, noiseFactor)

/**
* With CMP + Android, we can't do much other than display a transparent scrim.
* See `:haze-jetpack-compose` for a working blur on Android, but we need Compose 1.6.0 APIs,
* which are not available in CMP (yet).
* On older platforms, we draw a translucent scrim over the content
*/
private class AndroidHazeNode(
internal class HazeNodeBase(
state: HazeState,
backgroundColor: Color,
tint: Color,
Expand Down
15 changes: 15 additions & 0 deletions haze/src/androidMain/kotlin/dev/chrisbanes/haze/Platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp

internal actual fun createHazeNode(
state: HazeState,
backgroundColor: Color,
tint: Color,
blurRadius: Dp,
noiseFactor: Float,
): HazeNode = HazeNodeBase(state, backgroundColor, tint, blurRadius, noiseFactor)
11 changes: 1 addition & 10 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/Haze.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.addOutline
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.unit.Density
Expand Down Expand Up @@ -57,7 +56,7 @@ internal fun HazeState.updatePath(
}
}

private fun Path.addOutline(outline: Outline, offset: Offset) = when (outline) {
internal fun Path.addOutline(outline: Outline, offset: Offset) = when (outline) {
is Outline.Rectangle -> addRect(outline.rect.translate(offset))
is Outline.Rounded -> addRoundRect(outline.roundRect.translate(offset))
is Outline.Generic -> addPath(outline.path, offset)
Expand Down Expand Up @@ -170,14 +169,6 @@ internal data class HazeNodeElement(
}
}

internal expect fun createHazeNode(
state: HazeState,
backgroundColor: Color,
tint: Color,
blurRadius: Dp,
noiseFactor: Float,
): HazeNode

internal abstract class HazeNode(
var state: HazeState,
var backgroundColor: Color,
Expand Down
15 changes: 15 additions & 0 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/Platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp

internal expect fun createHazeNode(
state: HazeState,
backgroundColor: Color,
tint: Color,
blurRadius: Dp,
noiseFactor: Float,
): HazeNode

0 comments on commit 67398e0

Please sign in to comment.