Skip to content

Commit

Permalink
Reuse more existing code in UIKitView
Browse files Browse the repository at this point in the history
  • Loading branch information
MatkovIvan committed Feb 28, 2024
1 parent 5772f53 commit 156b2ee
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.InteropViewCatchPointerModifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasurePolicy
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.layout.EmptyLayout
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.uikit.toUIColor
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.asCGRect
Expand All @@ -51,7 +48,6 @@ import kotlinx.cinterop.CValue
import platform.CoreGraphics.CGRect
import platform.CoreGraphics.CGRectMake
import platform.Foundation.NSThread
import platform.UIKit.UIColor
import platform.UIKit.UIView
import platform.UIKit.UIViewController
import platform.UIKit.addChildViewController
Expand Down Expand Up @@ -99,7 +95,7 @@ fun <T : UIView> UIKitView(
var localToWindowOffset: IntOffset by remember { mutableStateOf(IntOffset.Zero) }
val interopContext = LocalUIKitInteropContext.current

Place(
EmptyLayout(
modifier.onGloballyPositioned { coordinates ->
localToWindowOffset = coordinates.positionInRoot().round()
val newRectInPixels = IntRect(localToWindowOffset, coordinates.size)
Expand Down Expand Up @@ -199,7 +195,7 @@ fun <T : UIViewController> UIKitViewController(
var localToWindowOffset: IntOffset by remember { mutableStateOf(IntOffset.Zero) }
val interopContext = LocalUIKitInteropContext.current

Place(
EmptyLayout(
modifier.onGloballyPositioned { coordinates ->
localToWindowOffset = coordinates.positionInRoot().round()
val newRectInPixels = IntRect(localToWindowOffset, coordinates.size)
Expand Down Expand Up @@ -260,25 +256,6 @@ fun <T : UIViewController> UIKitViewController(
}
}

@Composable
private fun Place(modifier: Modifier) {
Layout({}, measurePolicy = PlaceMeasurePolicy, modifier = modifier)
}

private object PlaceMeasurePolicy : MeasurePolicy {
override fun MeasureScope.measure(measurables: List<Measurable>, constraints: Constraints) =
layout(constraints.maxWidth, constraints.maxHeight) {}
}

private fun parseColor(color: Color): UIColor {
return UIColor(
red = color.red.toDouble(),
green = color.green.toDouble(),
blue = color.blue.toDouble(),
alpha = color.alpha.toDouble()
)
}

private abstract class EmbeddedInteropComponent<T : Any>(
val interopContainer: UIKitInteropContainer,
val onRelease: (T) -> Unit
Expand All @@ -291,7 +268,7 @@ private abstract class EmbeddedInteropComponent<T : Any>(
if (color == Color.Unspecified) {
container.backgroundColor = interopContainer.containerView.backgroundColor
} else {
container.backgroundColor = parseColor(color)
container.backgroundColor = color.toUIColor()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.platform.PlatformContext
import androidx.compose.ui.platform.PlatformWindowContext
import androidx.compose.ui.uikit.ComposeUIViewControllerConfiguration
import androidx.compose.ui.uikit.toUIColor
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
Expand All @@ -44,7 +45,6 @@ import platform.CoreGraphics.CGPoint
import platform.CoreGraphics.CGRectZero
import platform.CoreGraphics.CGSize
import platform.UIKit.NSLayoutConstraint
import platform.UIKit.UIColor
import platform.UIKit.UIEvent
import platform.UIKit.UITouch
import platform.UIKit.UIView
Expand Down Expand Up @@ -222,10 +222,3 @@ internal class UIViewComposeSceneLayer(
}

}

private fun Color.toUIColor() = UIColor(
red = red.toDouble(),
green = green.toDouble(),
blue = blue.toDouble(),
alpha = alpha.toDouble(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package androidx.compose.ui.uikit

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Density
import androidx.compose.ui.window.uiContentSizeCategoryToFontScaleMap
import platform.UIKit.UIColor
import platform.UIKit.UIContentSizeCategoryUnspecified
import platform.UIKit.UIScreen
import platform.UIKit.UITraitEnvironmentProtocol
Expand All @@ -31,3 +33,10 @@ internal val UITraitEnvironmentProtocol.systemDensity: Density
fontScale = uiContentSizeCategoryToFontScaleMap[contentSizeCategory] ?: 1.0f
)
}

internal fun Color.toUIColor() = UIColor(
red = red.toDouble(),
green = green.toDouble(),
blue = blue.toDouble(),
alpha = alpha.toDouble(),
)

0 comments on commit 156b2ee

Please sign in to comment.