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 6 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,8 +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.unit.dp
import androidx.compose.ui.uikit.InterfaceOrientation
import androidx.compose.ui.uikit.LocalInterfaceOrientation
import androidx.compose.ui.uikit.LocalSoftwareKeyboardState

private val ZeroInsets = WindowInsets(0, 0, 0, 0)

Expand Down Expand Up @@ -78,7 +79,7 @@ actual val WindowInsets.Companion.displayCutout: WindowInsets
actual val WindowInsets.Companion.ime: WindowInsets
@Composable
@OptIn(InternalComposeApi::class)
get() = WindowInsets(bottom = LocalKeyboardOverlapHeight.current.dp)
get() = WindowInsets(bottom = LocalSoftwareKeyboardState.current.imeBottomInset)

/**
* These insets represent the space where system gestures have priority over application gestures.
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.LocalSoftwareKeyboardState
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) {
LocalSoftwareKeyboardState.current.textSelectionHandlersOffset.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 @@ -20,8 +20,8 @@ import androidx.compose.runtime.Composable
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.unit.dp
import androidx.compose.ui.uikit.LocalSoftwareKeyboardState
import androidx.compose.ui.uikit.SoftwareKeyboardState

/**
* Composition local for SafeArea of ComposeUIViewController
Expand All @@ -41,7 +41,9 @@ private object SafeAreaInsetsConfig : InsetsConfig {
@Composable get() = LocalSafeArea.current

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

@Composable
override fun excludeInsets(
Expand All @@ -51,11 +53,11 @@ private object SafeAreaInsetsConfig : InsetsConfig {
) {
val safeArea = LocalSafeArea.current
val layoutMargins = LocalLayoutMargins.current
val keyboardOverlapHeight = LocalKeyboardOverlapHeight.current
val softwareKeyboardState = LocalSoftwareKeyboardState.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,
LocalSoftwareKeyboardState provides if (ime) SoftwareKeyboardState.Initial else softwareKeyboardState,
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 @@ -50,11 +50,10 @@ import androidx.compose.ui.platform.ViewConfiguration
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.LocalSoftwareKeyboardState
import androidx.compose.ui.uikit.SoftwareKeyboardState
import androidx.compose.ui.uikit.systemDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.DpRect
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.asCGRect
Expand All @@ -65,17 +64,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 +87,14 @@ 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.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 +182,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 +212,10 @@ internal class ComposeSceneMediator(
coroutineContext: CoroutineContext
) -> ComposeScene
) {
private val focusable: Boolean get() = focusStack != null
private val keyboardOverlapHeightState: MutableState<Float> = mutableStateOf(0f)
@OptIn(InternalComposeApi::class)
private val softwareKeyboardState = mutableStateOf(
SoftwareKeyboardState.Initial
)
private var _layout: SceneLayout = SceneLayout.Undefined
private var constraints: List<NSLayoutConstraint> = emptyList()
set(value) {
Expand Down Expand Up @@ -269,7 +247,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 +330,14 @@ internal class ComposeSceneMediator(
)
}

private val keyboardVisibilityListener by lazy {
KeyboardVisibilityListenerImpl(
@OptIn(InternalComposeApi::class)
private val keyboardManager by lazy {
ComposeSceneKeyboardOffsetManager(
configuration = configuration,
keyboardOverlapHeightState = keyboardOverlapHeightState,
viewProvider = { container },
densityProvider = { container.systemDensity },
composeSceneMediatorProvider = { this },
focusManager = focusManager,
softwareKeyboardState = softwareKeyboardState,
viewProvider = { rootView },
densityProvider = { rootView.systemDensity },
composeSceneMediatorProvider = { this }
)
}

Expand All @@ -377,8 +355,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 @@ -517,14 +495,16 @@ internal class ComposeSceneMediator(
CompositionLocalProvider(
LocalUIKitInteropContext provides interopContext,
LocalUIKitInteropContainer provides interopViewContainer,
LocalKeyboardOverlapHeight provides keyboardOverlapHeightState.value,
LocalSoftwareKeyboardState provides softwareKeyboardState.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 +634,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

This file was deleted.

Loading
Loading