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

Fix IME window insets and view offset when keyboard appears #1199

Merged
merged 16 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ package androidx.compose.foundation.layout

import androidx.compose.runtime.Composable
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.State
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.platform.LocalLayoutMargins
import androidx.compose.ui.platform.LocalSafeArea
import androidx.compose.ui.platform.PlatformInsets
import androidx.compose.ui.uikit.*
import androidx.compose.ui.uikit.InterfaceOrientation
import androidx.compose.ui.uikit.LocalInterfaceOrientation
import androidx.compose.ui.uikit.LocalKeyboardBottomInset
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp

private val ZeroInsets = WindowInsets(0, 0, 0, 0)
Expand All @@ -35,6 +40,20 @@ private fun PlatformInsets.toWindowInsets() = WindowInsets(
bottom = bottom,
)

private class ImeWindowInsets(
val keyboardBottomInset: State<Float>
): WindowInsets {
override fun getLeft(density: Density, layoutDirection: LayoutDirection): Int = 0

override fun getTop(density: Density): Int = 0

override fun getRight(density: Density, layoutDirection: LayoutDirection): Int = 0

override fun getBottom(density: Density): Int = with(density) {
keyboardBottomInset.value.dp.roundToPx()
}
}

/**
* This insets represents iOS SafeAreas.
*/
Expand Down Expand Up @@ -78,7 +97,7 @@ actual val WindowInsets.Companion.displayCutout: WindowInsets
actual val WindowInsets.Companion.ime: WindowInsets
@Composable
@OptIn(InternalComposeApi::class)
get() = WindowInsets(bottom = LocalKeyboardOverlapHeight.current.dp)
get() = ImeWindowInsets(LocalKeyboardBottomInset.current)

/**
* These insets represent the space where system gestures have priority over application gestures.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package androidx.compose.ui.platform
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.uikit.LocalKeyboardBottomInset
import androidx.compose.ui.uikit.LocalKeyboardOverlapHeight
import androidx.compose.ui.unit.dp

Expand All @@ -41,7 +43,7 @@ private object SafeAreaInsetsConfig : InsetsConfig {
@Composable get() = LocalSafeArea.current

override val ime: PlatformInsets
@Composable get() = PlatformInsets(bottom = LocalKeyboardOverlapHeight.current.dp)
@Composable get() = PlatformInsets(bottom = LocalKeyboardBottomInset.current.value.dp)

@Composable
override fun excludeInsets(
Expand All @@ -52,10 +54,12 @@ private object SafeAreaInsetsConfig : InsetsConfig {
val safeArea = LocalSafeArea.current
val layoutMargins = LocalLayoutMargins.current
val keyboardOverlapHeight = LocalKeyboardOverlapHeight.current
val keyboardInsets = LocalKeyboardBottomInset.current
CompositionLocalProvider(
LocalSafeArea provides if (safeInsets) PlatformInsets() else safeArea,
LocalLayoutMargins provides if (safeInsets) layoutMargins.exclude(safeArea) else layoutMargins,
LocalKeyboardOverlapHeight provides if (ime) 0f else keyboardOverlapHeight,
LocalKeyboardBottomInset provides if (ime) mutableStateOf(0f) else keyboardInsets,
content = content
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ internal class UIKitTextInputService(

textUIView?.removeFromSuperview()
textUIView = IntermediateTextInputUIView(
keyboardEventHandler = keyboardEventHandler,
viewConfiguration = viewConfiguration
).also {
it.keyboardEventHandler = keyboardEventHandler
rootView.addSubview(it)
it.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints(
Expand All @@ -136,8 +136,10 @@ internal class UIKitTextInputService(

textUIView?.inputTraits = EmptyInputTraits
textUIView?.input = null
textUIView?.keyboardEventHandler = null
textUIView?.let { view ->
mainScope.launch {
view.resignFirstResponder()
view.removeFromSuperview()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import androidx.compose.ui.platform.WindowInfo
import androidx.compose.ui.semantics.SemanticsOwner
import androidx.compose.ui.uikit.ComposeUIViewControllerConfiguration
import androidx.compose.ui.uikit.LocalKeyboardOverlapHeight
import androidx.compose.ui.uikit.LocalKeyboardBottomInset
import androidx.compose.ui.uikit.systemDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.DpOffset
Expand All @@ -66,18 +67,17 @@ import androidx.compose.ui.unit.round
import androidx.compose.ui.unit.roundToIntRect
import androidx.compose.ui.unit.toDpRect
import androidx.compose.ui.unit.toOffset
import androidx.compose.ui.window.ComposeContainerKeyboardManager
import androidx.compose.ui.window.FocusStack
import androidx.compose.ui.window.InteractionUIView
import androidx.compose.ui.window.KeyboardEventHandler
import androidx.compose.ui.window.KeyboardVisibilityListenerImpl
import androidx.compose.ui.window.RenderingUIView
import androidx.compose.ui.window.UITouchesEventPhase
import kotlin.coroutines.CoroutineContext
import kotlin.math.floor
import kotlin.math.roundToInt
import kotlin.math.roundToLong
import kotlinx.cinterop.CValue
import kotlinx.cinterop.ObjCAction
import kotlinx.cinterop.readValue
import kotlinx.cinterop.useContents
import org.jetbrains.skia.Canvas
Expand All @@ -91,21 +91,15 @@ import platform.CoreGraphics.CGRect
import platform.CoreGraphics.CGRectMake
import platform.CoreGraphics.CGRectZero
import platform.CoreGraphics.CGSize
import platform.Foundation.NSNotification
import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSSelectorFromString
import platform.Foundation.NSTimeInterval
import platform.QuartzCore.CATransaction
import platform.UIKit.NSLayoutConstraint
import platform.UIKit.UIEvent
import platform.UIKit.UIKeyboardWillHideNotification
import platform.UIKit.UIKeyboardWillShowNotification
import platform.UIKit.UITouch
import platform.UIKit.UITouchPhase
import platform.UIKit.UIView
import platform.UIKit.UIViewControllerTransitionCoordinatorProtocol
import platform.UIKit.UIWindow
import platform.darwin.NSObject

/**
* Layout of sceneView on the screen
Expand Down Expand Up @@ -191,22 +185,6 @@ private class RenderingUIViewDelegateImpl(
}
}

private class NativeKeyboardVisibilityListener(
private val keyboardVisibilityListener: KeyboardVisibilityListenerImpl
) : NSObject() {
@Suppress("unused")
@ObjCAction
fun keyboardWillShow(arg: NSNotification) {
keyboardVisibilityListener.keyboardWillShow(arg)
}

@Suppress("unused")
@ObjCAction
fun keyboardWillHide(arg: NSNotification) {
keyboardVisibilityListener.keyboardWillHide(arg)
}
}

private class ComposeSceneMediatorRootUIView : UIView(CGRectZero.readValue()) {
override fun hitTest(point: CValue<CGPoint>, withEvent: UIEvent?): UIView? {
// forwards touches forward to the children, is never a target for a touch
Expand All @@ -233,7 +211,6 @@ internal class ComposeSceneMediator(
coroutineContext: CoroutineContext
) -> ComposeScene
) {
private val focusable: Boolean get() = focusStack != null
private val keyboardOverlapHeightState: MutableState<Float> = mutableStateOf(0f)
private var _layout: SceneLayout = SceneLayout.Undefined
private var constraints: List<NSLayoutConstraint> = emptyList()
Expand Down Expand Up @@ -266,7 +243,7 @@ internal class ComposeSceneMediator(
set(value) {
scene.compositionLocalContext = value
}
private val focusManager get() = scene.focusManager
val focusManager get() = scene.focusManager

private val renderingView by lazy {
renderingUIViewFactory(renderDelegate)
Expand Down Expand Up @@ -342,14 +319,13 @@ internal class ComposeSceneMediator(
)
}

private val keyboardVisibilityListener by lazy {
KeyboardVisibilityListenerImpl(
private val keyboardManager by lazy {
ComposeContainerKeyboardManager(
configuration = configuration,
keyboardOverlapHeightState = keyboardOverlapHeightState,
viewProvider = { container },
densityProvider = { container.systemDensity },
composeSceneMediatorProvider = { this },
focusManager = focusManager,
viewProvider = { rootView },
densityProvider = { rootView.systemDensity },
composeSceneMediatorProvider = { this }
)
}

Expand Down Expand Up @@ -508,13 +484,16 @@ internal class ComposeSceneMediator(
LocalUIKitInteropContext provides interopContext,
LocalUIKitInteropContainer provides interopViewContainer,
LocalKeyboardOverlapHeight provides keyboardOverlapHeightState.value,
LocalKeyboardBottomInset provides keyboardOverlapHeightState,
LocalSafeArea provides safeAreaState.value,
LocalLayoutMargins provides layoutMarginsState.value,
content = content
)

fun dispose() {
uiKitTextInputService.stopInput()
focusStack?.popUntilNext(renderingView)
keyboardManager.stop()
renderingView.dispose()
interactionView.dispose()
rootView.removeFromSuperview()
Expand Down Expand Up @@ -646,37 +625,12 @@ internal class ComposeSceneMediator(
)
}

private val nativeKeyboardVisibilityListener = NativeKeyboardVisibilityListener(
keyboardVisibilityListener
)

fun viewDidAppear(animated: Boolean) {
NSNotificationCenter.defaultCenter.addObserver(
observer = nativeKeyboardVisibilityListener,
selector = NSSelectorFromString(nativeKeyboardVisibilityListener::keyboardWillShow.name + ":"),
name = UIKeyboardWillShowNotification,
`object` = null
)
NSNotificationCenter.defaultCenter.addObserver(
observer = nativeKeyboardVisibilityListener,
selector = NSSelectorFromString(nativeKeyboardVisibilityListener::keyboardWillHide.name + ":"),
name = UIKeyboardWillHideNotification,
`object` = null
)
fun sceneDidAppear() {
keyboardManager.start()
}

// viewDidUnload() is deprecated and not called.
fun viewWillDisappear(animated: Boolean) {
NSNotificationCenter.defaultCenter.removeObserver(
observer = nativeKeyboardVisibilityListener,
name = UIKeyboardWillShowNotification,
`object` = null
)
NSNotificationCenter.defaultCenter.removeObserver(
observer = nativeKeyboardVisibilityListener,
name = UIKeyboardWillHideNotification,
`object` = null
)
fun sceneWillDisappear() {
keyboardManager.stop()
}

fun getViewHeight(): Double = renderingView.frame.useContents {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ internal class UIViewComposeSceneLayer(
return positionInWindow
}

fun viewDidAppear(animated: Boolean) {
mediator.viewDidAppear(animated)
fun sceneDidAppear() {
mediator.sceneDidAppear()
}

fun viewWillDisappear(animated: Boolean) {
mediator.viewWillDisappear(animated)
fun sceneWillDisappear() {
mediator.sceneWillDisappear()
}

fun viewSafeAreaInsetsDidChange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
package androidx.compose.ui.uikit

import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.staticCompositionLocalOf


/**
* Composition local for height that is overlapped with keyboard over Compose view.
*/
@Deprecated("Use LocalWindowKeyboardInsets instead")
@InternalComposeApi
val LocalKeyboardOverlapHeight = staticCompositionLocalOf { 0f }
ASalavei marked this conversation as resolved.
Show resolved Hide resolved

@InternalComposeApi
val LocalKeyboardBottomInset = staticCompositionLocalOf<State<Float>> {
ASalavei marked this conversation as resolved.
Show resolved Hide resolved
ASalavei marked this conversation as resolved.
Show resolved Hide resolved
mutableStateOf(0f)
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ internal class ComposeContainer(
private var mediator: ComposeSceneMediator? = null
private val layers: MutableList<UIViewComposeSceneLayer> = mutableListOf()
private val layoutDirection get() = getLayoutDirection()
private var isViewAppeared: Boolean = false

@OptIn(ExperimentalComposeApi::class)
private val windowContainer: UIView
Expand Down Expand Up @@ -239,19 +240,21 @@ internal class ComposeContainer(

override fun viewDidAppear(animated: Boolean) {
super.viewDidAppear(animated)
mediator?.viewDidAppear(animated)
isViewAppeared = true
mediator?.sceneDidAppear()
layers.fastForEach {
it.viewDidAppear(animated)
it.sceneDidAppear()
}
updateWindowContainer()
configuration.delegate.viewDidAppear(animated)
}

override fun viewWillDisappear(animated: Boolean) {
super.viewWillDisappear(animated)
mediator?.viewWillDisappear(animated)
isViewAppeared = false
mediator?.sceneWillDisappear()
layers.fastForEach {
it.viewWillDisappear(animated)
it.sceneWillDisappear()
}
configuration.delegate.viewWillDisappear(animated)
}
Expand Down Expand Up @@ -327,7 +330,7 @@ internal class ComposeContainer(
windowContext = windowContext,
coroutineContext = coroutineDispatcher,
renderingUIViewFactory = ::createSkikoUIView,
composeSceneFactory = ::createComposeScene,
composeSceneFactory = ::createComposeScene
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
)
mediator.setContent {
ProvideContainerCompositionLocals(this, content)
Expand All @@ -342,14 +345,19 @@ internal class ComposeContainer(
layers.fastForEach {
it.close()
}

}

fun attachLayer(layer: UIViewComposeSceneLayer) {
layers.add(layer)
if (isViewAppeared) {
layer.sceneDidAppear()
}
}

fun detachLayer(layer: UIViewComposeSceneLayer) {
if (isViewAppeared) {
layer.sceneWillDisappear()
}
layers.remove(layer)
}

Expand All @@ -369,10 +377,9 @@ internal class ComposeContainer(
configuration = configuration,
focusStack = if (focusable) focusStack else null,
windowContext = windowContext,
compositionContext = compositionContext,
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
compositionContext = compositionContext
)
}

}

private fun UIViewController.checkIfInsideSwiftUI(): Boolean {
Expand Down
Loading
Loading