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

Correct virtual keyboard mode resolution #1295

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -26,20 +26,16 @@ import androidx.compose.ui.text.input.PlatformTextInputService
import androidx.compose.ui.text.input.TextFieldValue

internal interface InputAwareInputService {
fun resolveInputMode(): InputMode
fun getOffset(rect: Rect): Offset
fun isVirtualKeyboard(): Boolean
}

internal abstract class WebTextInputService : PlatformTextInputService, InputAwareInputService {
private val webImeInputService = WebImeInputService(this)
private val webKeyboardInputService = WebKeyboardInputService()

private fun delegatedService(): PlatformTextInputService {
return when (resolveInputMode()) {
InputMode.Touch -> webImeInputService
InputMode.Keyboard -> webKeyboardInputService
else -> webKeyboardInputService
}
return if (isVirtualKeyboard()) webImeInputService else webKeyboardInputService
}

override fun startInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ internal interface ComposeWindowState {
}
}

private sealed interface KeyboardModeState {
companion object {
object Virtual: KeyboardModeState
object Hardware: KeyboardModeState
}
}

internal class DefaultWindowState(private val viewportContainer: Element) : ComposeWindowState {
private val channel = Channel<IntSize>(CONFLATED)

Expand Down Expand Up @@ -161,13 +168,16 @@ internal class ComposeWindow(

private val canvasEvents = EventTargetListener(canvas)

private var keyboardModeState: KeyboardModeState = KeyboardModeState.Companion.Hardware

private val platformContext: PlatformContext = object : PlatformContext {
override val windowInfo get() = _windowInfo

override val inputModeManager: InputModeManager = DefaultInputModeManager()

override val textInputService = object : WebTextInputService() {
override fun resolveInputMode() = inputModeManager.inputMode
override fun isVirtualKeyboard() = keyboardModeState == KeyboardModeState.Companion.Virtual
eymar marked this conversation as resolved.
Show resolved Hide resolved

override fun getOffset(rect: Rect): Offset {
val viewportRect = canvas.getBoundingClientRect()
val offsetX = viewportRect.left.toFloat().coerceAtLeast(0f) + (rect.left / density.density)
Expand Down Expand Up @@ -344,6 +354,7 @@ internal class ComposeWindow(
event: TouchEvent,
offset: Offset,
) {
keyboardModeState = KeyboardModeState.Companion.Virtual
val eventType = when (event.type) {
"touchstart" -> PointerEventType.Press
"touchmove" -> PointerEventType.Move
Expand Down Expand Up @@ -375,6 +386,7 @@ internal class ComposeWindow(
private fun ComposeLayer.onMouseEvent(
event: MouseEvent,
) {
keyboardModeState = KeyboardModeState.Companion.Hardware
val eventType = when (event.type) {
"mousedown" -> PointerEventType.Press
"mousemove" -> PointerEventType.Move
Expand All @@ -401,6 +413,7 @@ internal class ComposeWindow(
private fun ComposeLayer.onWheelEvent(
event: WheelEvent,
) {
keyboardModeState = KeyboardModeState.Companion.Hardware
onMouseEvent(
eventType = PointerEventType.Scroll,
position = event.offset,
Expand Down
Loading