Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use positionOnScreen on Android #503

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions haze/src/androidMain/kotlin/dev/chrisbanes/haze/Utils.android.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2025, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.positionOnScreen

/**
* We use positionOnScreen on Android, to support dialogs, popup windows, etc.
*/
internal actual fun LayoutCoordinates.positionForHaze(): Offset = positionOnScreen()
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class HazeEffectNode(
}

private fun onPositioned(coordinates: LayoutCoordinates, source: String) {
positionOnScreen = coordinates.positionOnScreenCatching()
positionOnScreen = coordinates.positionForHaze()
size = coordinates.size.toSize()
log(TAG) { "$source: positionOnScreen=$positionOnScreen, size=$size" }
updateEffect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class HazeSourceNode(
}

private fun onPositioned(coordinates: LayoutCoordinates, source: String) {
area.positionOnScreen = coordinates.positionOnScreenCatching()
area.positionOnScreen = coordinates.positionForHaze()
area.size = coordinates.size.toSize()

log(TAG) {
Expand Down
12 changes: 1 addition & 11 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.geometry.takeOrElse
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.positionOnScreen
import kotlin.math.hypot

internal fun calculateLength(
Expand Down Expand Up @@ -37,13 +36,4 @@ internal inline fun <T> unsynchronizedLazy(noinline initializer: () -> T): Lazy<
return lazy(mode = LazyThreadSafetyMode.NONE, initializer)
}

/**
* Workaround for https://youtrack.jetbrains.com/issue/CMP-7380
*/
internal fun LayoutCoordinates.positionOnScreenCatching(): Offset {
return try {
positionOnScreen()
} catch (t: Throwable) {
Offset.Unspecified
}
}
internal expect fun LayoutCoordinates.positionForHaze(): Offset
18 changes: 18 additions & 0 deletions haze/src/skikoMain/kotlin/dev/chrisbanes/haze/Utils.skiko.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2025, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.positionInWindow

/**
* We only look at window coordinates on Desktop. There's currently no external windows used
* on Skiko platforms (AFAIK), so there's no need to look at screen coordinates.
*/
internal actual fun LayoutCoordinates.positionForHaze(): Offset = try {
positionInWindow()
} catch (t: Throwable) {
Offset.Unspecified
}