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 @@ -24,7 +24,7 @@ import androidx.compose.ui.platform.LocalSafeArea
import androidx.compose.ui.platform.PlatformInsets
import androidx.compose.ui.uikit.InterfaceOrientation
import androidx.compose.ui.uikit.LocalInterfaceOrientation
import androidx.compose.ui.uikit.LocalKeyboardOverlapHeight
import androidx.compose.ui.uikit.LocalKeyboardSceneDisplayParameters
import androidx.compose.ui.unit.dp

private val ZeroInsets = WindowInsets(0, 0, 0, 0)
Expand Down Expand Up @@ -80,7 +80,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 = LocalKeyboardSceneDisplayParameters.current.imeBottomInset.dp)

/**
* 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 @@ -36,7 +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.uikit.LocalKeyboardSceneDisplayParameters
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
Expand Down Expand Up @@ -160,7 +160,7 @@ internal fun HandlePopup(
content: @Composable () -> Unit
) {
val cursorOffset = with(LocalDensity.current) {
LocalTextSelectionHandlersOffset.current.dp.toPx()
LocalKeyboardSceneDisplayParameters.current.textSelectionHandlersOffset.dp.toPx()
}
val handleOffset = offset.copy(y = offset.y - cursorOffset)

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.uikit.LocalTextSelectionHandlersOffset
import androidx.compose.ui.uikit.KeyboardSceneDisplayParameters
import androidx.compose.ui.uikit.LocalKeyboardSceneDisplayParameters
import androidx.compose.ui.unit.dp

/**
Expand All @@ -42,7 +42,9 @@ private object SafeAreaInsetsConfig : InsetsConfig {
@Composable get() = LocalSafeArea.current

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

@Composable
override fun excludeInsets(
Expand All @@ -52,13 +54,11 @@ private object SafeAreaInsetsConfig : InsetsConfig {
) {
val safeArea = LocalSafeArea.current
val layoutMargins = LocalLayoutMargins.current
val keyboardOverlapHeight = LocalKeyboardOverlapHeight.current
val selectionHandlersOffset = LocalTextSelectionHandlersOffset.current
val keyboardSceneDisplayParameters = LocalKeyboardSceneDisplayParameters.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,
LocalKeyboardSceneDisplayParameters provides if (ime) KeyboardSceneDisplayParameters.initial else keyboardSceneDisplayParameters,
content = content
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +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.LocalTextSelectionHandlersOffset
import androidx.compose.ui.uikit.KeyboardSceneDisplayParameters
import androidx.compose.ui.uikit.LocalKeyboardSceneDisplayParameters
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 Down Expand Up @@ -89,10 +87,6 @@ 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
Expand Down Expand Up @@ -218,8 +212,9 @@ internal class ComposeSceneMediator(
coroutineContext: CoroutineContext
) -> ComposeScene
) {
private val keyboardOverlapHeightState: MutableState<Float> = mutableStateOf(0f)
private val textSelectionHandlersOffsetState: MutableState<Float> = mutableStateOf(0f)
private val keyboardSceneDisplayParameters = mutableStateOf(
KeyboardSceneDisplayParameters.initial
)
private var _layout: SceneLayout = SceneLayout.Undefined
private var constraints: List<NSLayoutConstraint> = emptyList()
set(value) {
Expand Down Expand Up @@ -337,8 +332,7 @@ internal class ComposeSceneMediator(
private val keyboardManager by lazy {
ComposeSceneKeyboardOffsetManager(
configuration = configuration,
keyboardOverlapHeightState = keyboardOverlapHeightState,
textSelectionHandlersOffsetState = textSelectionHandlersOffsetState,
keyboardSceneDisplayParameters = keyboardSceneDisplayParameters,
viewProvider = { rootView },
densityProvider = { rootView.systemDensity },
composeSceneMediatorProvider = { this }
Expand Down Expand Up @@ -499,8 +493,7 @@ internal class ComposeSceneMediator(
CompositionLocalProvider(
LocalUIKitInteropContext provides interopContext,
LocalUIKitInteropContainer provides interopViewContainer,
LocalKeyboardOverlapHeight provides keyboardOverlapHeightState.value,
LocalTextSelectionHandlersOffset provides textSelectionHandlersOffsetState.value,
LocalKeyboardSceneDisplayParameters provides keyboardSceneDisplayParameters.value,
LocalSafeArea provides safeAreaState.value,
LocalLayoutMargins provides layoutMarginsState.value,
content = content
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.uikit

import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.compositionLocalOf


data class KeyboardSceneDisplayParameters(
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
/**
* Height that is overlapped with keyboard over Compose view.
*/
val imeBottomInset: Float,
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved

/**
* Selection Handlers vertical offset when keyboard is visible.
* Applied when [OnFocusBehavior.FocusableAboveKeyboard] used and
* [ComposeUIViewControllerConfiguration.platformLayers] are enabled.
*/
val textSelectionHandlersOffset: Float
) {
companion object {
val initial = KeyboardSceneDisplayParameters(
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
imeBottomInset = 0f,
textSelectionHandlersOffset = 0f
)
}
}

/**
* Composition local for keyboard display parameters for the current scene.
*/
@InternalComposeApi
val LocalKeyboardSceneDisplayParameters = compositionLocalOf {
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
KeyboardSceneDisplayParameters.initial
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.runtime.ExperimentalComposeApi
import androidx.compose.runtime.MutableState
import androidx.compose.ui.scene.ComposeSceneMediator
import androidx.compose.ui.uikit.ComposeUIViewControllerConfiguration
import androidx.compose.ui.uikit.KeyboardSceneDisplayParameters
import androidx.compose.ui.uikit.OnFocusBehavior
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.toDpRect
Expand Down Expand Up @@ -51,8 +52,7 @@ import platform.darwin.sel_registerName

internal class ComposeSceneKeyboardOffsetManager(
private val configuration: ComposeUIViewControllerConfiguration,
private val keyboardOverlapHeightState: MutableState<Float>,
private val textSelectionHandlersOffsetState: MutableState<Float>,
private val keyboardSceneDisplayParameters: MutableState<KeyboardSceneDisplayParameters>,
private val viewProvider: () -> UIView,
private val densityProvider: () -> Density,
private val composeSceneMediatorProvider: () -> ComposeSceneMediator?
Expand All @@ -62,10 +62,7 @@ internal class ComposeSceneKeyboardOffsetManager(

fun start() {
KeyboardVisibilityListener.addObserver(this)
adjustKeyboardBounds()
}

fun adjustKeyboardBounds() {
adjustViewBounds(
KeyboardVisibilityListener.keyboardFrame,
KeyboardVisibilityListener.keyboardFrame,
Expand Down Expand Up @@ -147,6 +144,7 @@ internal class ComposeSceneKeyboardOffsetManager(
)
}

@OptIn(ExperimentalComposeApi::class)
private fun animateKeyboard(
previousKeyboardHeight: Double,
keyboardHeight: Double,
Expand All @@ -167,10 +165,20 @@ internal class ComposeSceneKeyboardOffsetManager(
val currentHeight = previousKeyboardHeight +
(keyboardHeight - previousKeyboardHeight) * progress
val currentOverlapHeight = max(0.0, currentHeight - viewBottomIndent)
keyboardOverlapHeightState.value = currentOverlapHeight.toFloat()

val targetBottomOffset = calcFocusedBottomOffsetY(currentOverlapHeight)
viewBottomOffset += (targetBottomOffset - viewBottomOffset) * progress

val textSelectionHandlersOffset = if (configuration.platformLayers) {
viewBottomOffset.toFloat()
} else {
0f
}

keyboardSceneDisplayParameters.value = KeyboardSceneDisplayParameters(
imeBottomInset = currentOverlapHeight.toFloat(),
textSelectionHandlersOffset = textSelectionHandlersOffset
)
}

fun completeAnimation() {
Expand Down Expand Up @@ -255,13 +263,9 @@ internal class ComposeSceneKeyboardOffsetManager(
}
}

@OptIn(ExperimentalComposeApi::class)
private var viewBottomOffset: Double = 0.0
set(newValue) {
field = newValue
if (configuration.platformLayers) {
textSelectionHandlersOffsetState.value = newValue.toFloat()
}
view.layer.setAffineTransform(CGAffineTransformMakeTranslation(0.0, -newValue))
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading