-
Notifications
You must be signed in to change notification settings - Fork 81
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
iOS fix keyboard crash #1383
Changes from 3 commits
0e7a43a
85af7b6
e1df536
48d48e4
3717f3d
557d42a
79a771e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please reuse our internal helper There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel hesitant to introduce this change in this PR:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add explicit TODO at least There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
) | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 localkey
.