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

iOS fix keyboard crash #1383

Merged
merged 7 commits into from
Jun 5, 2024
Merged
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 @@ -24,28 +24,52 @@ import platform.UIKit.UIKeyModifierShift
import platform.UIKit.UIPress
import platform.UIKit.UIPressPhase.UIPressPhaseBegan
import platform.UIKit.UIPressPhase.UIPressPhaseEnded
import platform.UIKit.UIPressTypeDownArrow
import platform.UIKit.UIPressTypeLeftArrow
import platform.UIKit.UIPressTypePageDown
import platform.UIKit.UIPressTypePageUp
import platform.UIKit.UIPressTypeRightArrow
import platform.UIKit.UIPressTypeUpArrow

internal fun UIPress.toComposeEvent(): KeyEvent {
// TODO: https://developer.apple.com/documentation/uikit/uipress/3526315-key
// can be potentially nil on TVOS, this will cause a crash
val uiKey = requireNotNull(key) {
"UIPress with null key is not supported"
val pressKey = key
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if we don't need any additional check then (why btw?) then this variable is redundant and we can just use key

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, removed extra variable and avoided shadowing this.key with local key.


val keyEventType = when (phase) {
UIPressPhaseBegan -> KeyEventType.KeyDown
UIPressPhaseEnded -> KeyEventType.KeyUp
else -> KeyEventType.Unknown
}

// UIPress has special types for arrow keys and page up/down and has null `key`
// In other cases the `type` returns an int value that doesn't match any UIPressType.
val specialTypeKey = when (type) {
UIPressTypeUpArrow -> Key.DirectionUp
UIPressTypeDownArrow -> Key.DirectionDown
UIPressTypeLeftArrow -> Key.DirectionLeft
UIPressTypeRightArrow -> Key.DirectionRight
UIPressTypePageDown -> Key.PageDown
UIPressTypePageUp -> Key.PageUp
else -> null
}

val key = specialTypeKey ?: pressKey?.keyCode?.let { Key(it) } ?: Key.Unknown
val codePoint = pressKey?.characters?.firstOrNull()?.code ?: 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reuse our internal helper String.codePointAt(index: Int): CodePoint instead of firstOrNull

Copy link
Author

@elijah-semyonov elijah-semyonov Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel hesitant to introduce this change in this PR:

  1. I'm not owning this piece of code.
  2. This is out of the scope of the fix.
  3. So far we have no issues related to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add explicit TODO at least

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


val modifiers = pressKey?.let {
PointerKeyboardModifiers(
isCtrlPressed = it.modifierFlags and UIKeyModifierControl != 0L,
isMetaPressed = it.modifierFlags and UIKeyModifierCommand != 0L,
isAltPressed = it.modifierFlags and UIKeyModifierAlternate != 0L,
isShiftPressed = it.modifierFlags and UIKeyModifierShift != 0L,
)
} ?: PointerKeyboardModifiers()
elijah-semyonov marked this conversation as resolved.
Show resolved Hide resolved

return KeyEvent(
nativeKeyEvent = InternalKeyEvent(
key = Key(uiKey.keyCode),
type = when (phase) {
UIPressPhaseBegan -> KeyEventType.KeyDown
UIPressPhaseEnded -> KeyEventType.KeyUp
else -> KeyEventType.Unknown
},
codePoint = uiKey.characters.firstOrNull()?.code ?: 0,
modifiers = PointerKeyboardModifiers(
isCtrlPressed = uiKey.modifierFlags and UIKeyModifierControl != 0L,
isMetaPressed = uiKey.modifierFlags and UIKeyModifierCommand != 0L,
isAltPressed = uiKey.modifierFlags and UIKeyModifierAlternate != 0L,
isShiftPressed = uiKey.modifierFlags and UIKeyModifierShift != 0L,
),
key = key,
type = keyEventType,
codePoint = codePoint,
modifiers = modifiers,
nativeEvent = this
)
)
Expand Down
Loading