Skip to content

Commit

Permalink
Fix countInteropComponentsBefore call before addInteropView
Browse files Browse the repository at this point in the history
  • Loading branch information
MatkovIvan committed Feb 29, 2024
1 parent 780ad80 commit 0d5d784
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ internal class SwingInteropContainer(
private var interopComponents = mutableMapOf<Component, InteropComponent>()

override var rootModifier: TrackInteropModifierNode<InteropComponent>? = null
override val interopViews: Set<InteropComponent>
get() = interopComponents.values.toSet()

override fun addInteropView(nativeView: InteropComponent) {
val component = nativeView.container
val nonInteropComponents = container.componentCount - interopComponents.size
// AWT uses the reverse order for drawing and events, so index = size - count
val index = maxOf(0, interopComponents.size - countInteropComponentsBefore(nativeView))
val index = interopComponents.size - countInteropComponentsBefore(nativeView)
interopComponents[component] = nativeView
container.add(component, if (placeInteropAbove) {
index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private class SwingPanelContainer(
private val focusComponent: Component
): JPanel() {
init {
name = "SwingPanel #$key"
name = "SwingPanel #${key.toString(MaxSupportedRadix)}"
layout = null
focusTraversalPolicy = object : LayoutFocusTraversalPolicy() {
override fun getComponentAfter(aContainer: Container?, aComponent: Component?): Component? {
Expand Down Expand Up @@ -398,3 +398,8 @@ private class InteropPointerInputModifier<T : Component>(
return SwingUtilities.getDeepestComponentAt(parent, point.x, point.y)
}
}

/**
* The maximum radix available for conversion to and from strings.
*/
private val MaxSupportedRadix = 36
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.node.TraversableNode.Companion.TraverseDescendantsAct
*/
internal interface InteropContainer<T> {
var rootModifier: TrackInteropModifierNode<T>?
val interopViews: Set<T>

fun addInteropView(nativeView: T)
fun removeInteropView(nativeView: T)
Expand All @@ -44,7 +45,11 @@ internal fun <T> InteropContainer<T>.countInteropComponentsBefore(nativeView: T)
var componentsBefore = 0
rootModifier?.traverseDescendants {
if (it.nativeView != nativeView) {
componentsBefore++
// It might be inside Compose tree before adding in InteropContainer in case
// if it was initiated out of scroll visible bounds for example.
if (it.nativeView in interopViews) {
componentsBefore++
}
ContinueTraversal
} else {
CancelTraversal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ internal val LocalUIKitInteropContainer = staticCompositionLocalOf<UIKitInteropC
internal class UIKitInteropContainer: InteropContainer<UIView> {
val containerView: UIView = UIKitInteropContainerView()
override var rootModifier: TrackInteropModifierNode<UIView>? = null
override var interopViews = mutableSetOf<UIView>()
private set

override fun addInteropView(nativeView: UIView) {
val index = countInteropComponentsBefore(nativeView)
interopViews.add(nativeView)
containerView.insertSubview(nativeView, index.toLong())
}

override fun removeInteropView(nativeView: UIView) {
nativeView.removeFromSuperview()
interopViews.remove(nativeView)
}
}

Expand Down

0 comments on commit 0d5d784

Please sign in to comment.