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 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ 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.LocalKeyboardOverlapHeight
import androidx.compose.ui.unit.dp

private val ZeroInsets = WindowInsets(0, 0, 0, 0)
Expand Down
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.foundation.text.selection.HandleReferencePoint.TopLeft
import androidx.compose.foundation.text.selection.HandleReferencePoint.TopMiddle
import androidx.compose.foundation.text.selection.HandleReferencePoint.TopRight
import androidx.compose.runtime.Composable
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
Expand All @@ -35,6 +36,7 @@ import androidx.compose.ui.geometry.takeOrElse
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.ResolvedTextDirection
import androidx.compose.ui.uikit.LocalTextSelectionHandlersOffset
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
Expand All @@ -60,6 +62,7 @@ private val RADIUS = 6.dp
*/
private val THICKNESS = 2.dp

@OptIn(InternalComposeApi::class)
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
@Composable
internal actual fun SelectionHandle(
offsetProvider: OffsetProvider,
Expand Down Expand Up @@ -148,15 +151,21 @@ internal fun Modifier.drawSelectionHandle(
}
}

@OptIn(InternalComposeApi::class)
@Composable
internal fun HandlePopup(
offset: Offset,
positionProvider: OffsetProvider,
handleReferencePoint: HandleReferencePoint,
content: @Composable () -> Unit
) {
val popupPositionProvider = remember(handleReferencePoint, positionProvider, offset) {
HandlePositionProvider(handleReferencePoint, positionProvider, offset)
val cursorOffset = with(LocalDensity.current) {
LocalTextSelectionHandlersOffset.current.dp.toPx()
}
val handleOffset = offset.copy(y = offset.y - cursorOffset)

val popupPositionProvider = remember(handleReferencePoint, positionProvider, handleOffset) {
HandlePositionProvider(handleReferencePoint, positionProvider, handleOffset)
}
Popup(
popupPositionProvider = popupPositionProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.uikit.LocalKeyboardOverlapHeight
import androidx.compose.ui.uikit.LocalTextSelectionHandlersOffset
import androidx.compose.ui.unit.dp

/**
Expand Down Expand Up @@ -52,10 +53,12 @@ private object SafeAreaInsetsConfig : InsetsConfig {
val safeArea = LocalSafeArea.current
val layoutMargins = LocalLayoutMargins.current
val keyboardOverlapHeight = LocalKeyboardOverlapHeight.current
val selectionHandlersOffset = LocalTextSelectionHandlersOffset.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,
LocalTextSelectionHandlersOffset provides if (ime) 0f else selectionHandlersOffset,
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 @@ -51,6 +51,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.LocalTextSelectionHandlersOffset
import androidx.compose.ui.uikit.systemDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.DpOffset
Expand All @@ -65,17 +66,16 @@ 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.ComposeSceneKeyboardOffsetManager
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.SkikoRenderDelegate
import androidx.compose.ui.window.UITouchesEventPhase
import kotlin.coroutines.CoroutineContext
import kotlin.math.roundToInt
import kotlinx.cinterop.CValue
import kotlinx.cinterop.ObjCAction
import kotlinx.cinterop.readValue
import kotlinx.cinterop.useContents
import org.jetbrains.skia.Canvas
Expand All @@ -89,20 +89,18 @@ import platform.CoreGraphics.CGRect
import platform.CoreGraphics.CGRectMake
import platform.CoreGraphics.CGRectZero
import platform.CoreGraphics.CGSize
import platform.Foundation.NSTimeInterval
import platform.Foundation.NSNotification
import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSSelectorFromString
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 @@ -190,22 +188,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 Down Expand Up @@ -236,8 +218,8 @@ internal class ComposeSceneMediator(
coroutineContext: CoroutineContext
) -> ComposeScene
) {
private val focusable: Boolean get() = focusStack != null
private val keyboardOverlapHeightState: MutableState<Float> = mutableStateOf(0f)
private val textSelectionHandlersOffsetState: MutableState<Float> = mutableStateOf(0f)
private var _layout: SceneLayout = SceneLayout.Undefined
private var constraints: List<NSLayoutConstraint> = emptyList()
set(value) {
Expand Down Expand Up @@ -269,7 +251,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(interopContext, renderDelegate)
Expand Down Expand Up @@ -352,14 +334,14 @@ internal class ComposeSceneMediator(
)
}

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

Expand All @@ -377,8 +359,8 @@ internal class ComposeSceneMediator(
renderingView.setNeedsDisplay() // redraw on next frame
CATransaction.flush() // clear all animations
},
rootViewProvider = { container },
densityProvider = { container.systemDensity },
rootViewProvider = { rootView },
densityProvider = { rootView.systemDensity },
viewConfiguration = viewConfiguration,
focusStack = focusStack,
keyboardEventHandler = keyboardEventHandler
Expand Down Expand Up @@ -518,13 +500,16 @@ internal class ComposeSceneMediator(
LocalUIKitInteropContext provides interopContext,
LocalUIKitInteropContainer provides interopViewContainer,
LocalKeyboardOverlapHeight provides keyboardOverlapHeightState.value,
LocalTextSelectionHandlersOffset provides textSelectionHandlersOffsetState.value,
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 @@ -654,37 +639,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 @@ -235,12 +235,12 @@ internal class UIViewComposeSceneLayer(
)
}

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.staticCompositionLocalOf
import androidx.compose.runtime.compositionLocalOf


/**
* Composition local for height that is overlapped with keyboard over Compose view.
*/
@InternalComposeApi
val LocalKeyboardOverlapHeight = staticCompositionLocalOf { 0f }
val LocalKeyboardOverlapHeight = compositionLocalOf { 0f }

/**
* Composition local for Selection Handlers vertical offset.
* Applied when [OnFocusBehavior.FocusableAboveKeyboard] used and
* [ComposeUIViewControllerConfiguration.platformLayers] are enabled.
*/
@InternalComposeApi
val LocalTextSelectionHandlersOffset = compositionLocalOf { 0f }
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 @@ -245,19 +246,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 @@ -334,7 +337,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 @@ -350,14 +353,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 @@ -377,10 +385,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