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

Make MouseEvent.nativeEvent and KeyEvent.nativeKeyEvent as Any? #87

Merged
merged 1 commit into from Nov 18, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,6 +16,7 @@

package androidx.compose.foundation.text

import androidx.compose.ui.awt.awtEvent
import androidx.compose.ui.input.key.KeyEvent

private fun Char.isPrintable(): Boolean {
Expand All @@ -27,5 +28,5 @@ private fun Char.isPrintable(): Boolean {
}

actual val KeyEvent.isTypedEvent: Boolean
get() = nativeKeyEvent.id == java.awt.event.KeyEvent.KEY_TYPED &&
nativeKeyEvent.keyChar.isPrintable()
get() = awtEvent.id == java.awt.event.KeyEvent.KEY_TYPED &&
awtEvent.keyChar.isPrintable()
@@ -0,0 +1,29 @@
package androidx.compose.ui.awt

import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.pointer.PointerEvent


// TODO(demin): new API, which is not merged to AOSP
/**
* The original raw native event from AWT
*/
val PointerEvent.awtEvent: java.awt.event.MouseEvent get() {
require(nativeEvent is java.awt.event.MouseEvent) {
"nativeEvent was sent not by AWT. Make sure, that you use AWT backed API" +
" (from androidx.compose.ui.awt.* or from androidx.compose.ui.window.*)"
}
return nativeEvent
}

// TODO(demin): new API, which is not merged to AOSP
/**
* The original raw native event from AWT
*/
val KeyEvent.awtEvent: java.awt.event.KeyEvent get() {
require(nativeKeyEvent is java.awt.event.KeyEvent) {
"nativeKeyEvent was sent not by AWT. Make sure, that you use AWT backed API" +
" (from androidx.compose.ui.awt.* or from androidx.compose.ui.window.*)"
}
return nativeKeyEvent
}
Expand Up @@ -16,19 +16,20 @@

package androidx.compose.ui.input.key

import androidx.compose.ui.awt.awtEvent
import java.awt.event.KeyEvent.KEY_PRESSED
import java.awt.event.KeyEvent.KEY_RELEASED

/**
* The native desktop [KeyEvent][KeyEventAwt].
*/
actual typealias NativeKeyEvent = java.awt.event.KeyEvent
actual typealias NativeKeyEvent = Any

/**
* The key that was pressed.
*/
actual val KeyEvent.key: Key
get() = Key(nativeKeyEvent.keyCode, nativeKeyEvent.keyLocation)
get() = Key(awtEvent.keyCode, awtEvent.keyLocation)

/**
* The UTF16 value corresponding to the key event that was pressed. The unicode character
Expand All @@ -47,13 +48,13 @@ actual val KeyEvent.key: Key
* second from the low-surrogates range (\uDC00-\uDFFF).
*/
actual val KeyEvent.utf16CodePoint: Int
get() = nativeKeyEvent.keyChar.code
get() = awtEvent.keyChar.code

/**
* The [type][KeyEventType] of key event.
*/
actual val KeyEvent.type: KeyEventType
get() = when (nativeKeyEvent.id) {
get() = when (awtEvent.id) {
KEY_PRESSED -> KeyEventType.KeyDown
KEY_RELEASED -> KeyEventType.KeyUp
else -> KeyEventType.Unknown
Expand All @@ -63,22 +64,22 @@ actual val KeyEvent.type: KeyEventType
* Indicates whether the Alt key is pressed.
*/
actual val KeyEvent.isAltPressed: Boolean
get() = nativeKeyEvent.isAltDown || nativeKeyEvent.isAltGraphDown
get() = awtEvent.isAltDown || awtEvent.isAltGraphDown

/**
* Indicates whether the Ctrl key is pressed.
*/
actual val KeyEvent.isCtrlPressed: Boolean
get() = nativeKeyEvent.isControlDown
get() = awtEvent.isControlDown

/**
* Indicates whether the Meta key is pressed.
*/
actual val KeyEvent.isMetaPressed: Boolean
get() = nativeKeyEvent.isMetaDown
get() = awtEvent.isMetaDown

/**
* Indicates whether the Shift key is pressed.
*/
actual val KeyEvent.isShiftPressed: Boolean
get() = nativeKeyEvent.isShiftDown
get() = awtEvent.isShiftDown
Expand Up @@ -34,11 +34,22 @@ actual data class PointerEvent internal constructor(
*/
actual val changes: List<PointerInputChange>,

// TODO(demin): new API, which is not merged to AOSP
/**
* Original raw native event from AWT
* The original raw native event which is sent by the platform.
*/
val mouseEvent: MouseEvent?
val nativeEvent: Any?
) {
/**
* The original raw native event from AWT
*/
@Deprecated(
"Use androidx.compose.ui.awt.awtEvent",
replaceWith = ReplaceWith("awtEvent", "androidx.compose.ui.awt.awtEvent"
)
)
val mouseEvent: MouseEvent? get() = nativeEvent as MouseEvent?

internal actual constructor(
changes: List<PointerInputChange>,
internalPointerEvent: InternalPointerEvent?
Expand All @@ -53,7 +64,7 @@ actual data class PointerEvent internal constructor(
/**
* @param changes The changes.
*/
actual constructor(changes: List<PointerInputChange>) : this(changes, mouseEvent = null)
actual constructor(changes: List<PointerInputChange>) : this(changes, nativeEvent = null)

actual var type: PointerEventType = PointerEventType.Unknown
internal set
Expand Down
Expand Up @@ -17,6 +17,7 @@
package androidx.compose.ui.platform

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.awt.awtEvent
import androidx.compose.ui.input.key.KeyInputModifier
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
Expand All @@ -31,7 +32,7 @@ internal actual fun sendKeyEvent(
keyEvent: KeyEvent
): Boolean {
when {
keyEvent.nativeKeyEvent.id == java.awt.event.KeyEvent.KEY_TYPED ->
keyEvent.awtEvent.id == java.awt.event.KeyEvent.KEY_TYPED ->
platformInputService.charKeyPressed = true
keyEvent.type == KeyEventType.KeyUp ->
platformInputService.charKeyPressed = false
Expand Down