Skip to content

Commit

Permalink
Replace Compose KeyEvent with custom
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelenskiy committed Apr 14, 2024
1 parent 41a2475 commit 9c8b52f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.*
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
Expand Down Expand Up @@ -152,7 +151,23 @@ public fun combineKeyboardEventFilters(vararg filters: KeyboardEventFilter?): Ke

public interface KeyboardEvent

public data class PhysicalKeyboardEvent(val keyEvent: KeyEvent) : KeyboardEvent
public data class PhysicalKeyboardEvent(
val key: Key, val utf16CodePoint: Int, val type: KeyEventType,
val isShiftPressed: Boolean = false,
val isCtrlPressed: Boolean = false,
val isAltPressed: Boolean = false,
val isMetaPressed: Boolean = false,
) : KeyboardEvent {
public constructor(keyEvent: KeyEvent) : this(
key = keyEvent.key,
utf16CodePoint = keyEvent.utf16CodePoint,
type = keyEvent.type,
isShiftPressed = keyEvent.isShiftPressed,
isCtrlPressed = keyEvent.isCtrlPressed,
isAltPressed = keyEvent.isAltPressed,
isMetaPressed = keyEvent.isMetaPressed,
)
}

public sealed class UniversalKeyboardEvent : KeyboardEvent {
public data object NonTextEvent : UniversalKeyboardEvent()
Expand Down
15 changes: 2 additions & 13 deletions editor/src/commonMain/kotlin/editor/basic/KeyEventHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,14 @@ public fun PhysicalKeyboardEventFilter.asKeyboardEventFilter(): KeyboardEventFil
it is PhysicalKeyboardEvent && invoke(it)
}

public typealias ComposeKeyEventHandler = (KeyEvent) -> TextFieldValue?
public typealias ComposeKeyEventFilter = (KeyEvent) -> Boolean

@JvmName("composeKeyboardEventHandlerAsKeyboardEventHandler")
public fun ComposeKeyEventHandler.asKeyboardEventHandler(): KeyboardEventHandler =
{ event: PhysicalKeyboardEvent -> invoke(event.keyEvent) }.asKeyboardEventHandler()

@JvmName("composeKeyboardEventFilterAsKeyboardEventFilter")
public fun ComposeKeyEventFilter.asKeyboardEventFilter(): KeyboardEventFilter =
{ event: PhysicalKeyboardEvent -> invoke(event.keyEvent) }.asKeyboardEventFilter()

public fun <T : Token> handleMovingOffsets(
state: BasicSourceCodeTextFieldState<T>,
indent: String = " ".repeat(4),
moveForwardFilter: KeyboardEventFilter = { keyEvent: KeyEvent ->
moveForwardFilter: KeyboardEventFilter = { keyEvent: PhysicalKeyboardEvent ->
!keyEvent.isShiftPressed && keyEvent.key == Key.Tab && keyEvent.type == KeyEventType.KeyDown &&
!keyEvent.isAltPressed && !keyEvent.isCtrlPressed && !keyEvent.isMetaPressed
}.asKeyboardEventFilter(),
moveBackwardFilter: KeyboardEventFilter = { keyEvent: KeyEvent ->
moveBackwardFilter: KeyboardEventFilter = { keyEvent: PhysicalKeyboardEvent ->
keyEvent.isShiftPressed && keyEvent.key == Key.Tab && keyEvent.type == KeyEventType.KeyDown &&
!keyEvent.isAltPressed && !keyEvent.isCtrlPressed && !keyEvent.isMetaPressed
}.asKeyboardEventFilter(),
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ development=true

#Publication
group=com.zhelenskiy
version=0.0.13
version=0.0.14

0 comments on commit 9c8b52f

Please sign in to comment.