From cd316690685bbb93a22d435b1b183a75072e3fbb Mon Sep 17 00:00:00 2001 From: "Evgeniy.Zhelenskiy" Date: Mon, 1 Apr 2024 03:10:53 +0200 Subject: [PATCH] Make key events customizable --- .../kotlin/CorrectBracketSequence.kt | 10 ++++---- .../editor/basic/BasicSourceCodeTextField.kt | 1 + .../kotlin/editor/basic/CharEventHandlers.kt | 4 ++-- .../kotlin/editor/basic/KeyEventHandlers.kt | 23 ++++++++++++------- gradle.properties | 2 +- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/CorrectBracketSequence.kt b/composeApp/src/commonMain/kotlin/CorrectBracketSequence.kt index 7df2057..a485d02 100644 --- a/composeApp/src/commonMain/kotlin/CorrectBracketSequence.kt +++ b/composeApp/src/commonMain/kotlin/CorrectBracketSequence.kt @@ -255,25 +255,25 @@ fun CorrectBracketSequence() { textFieldState = codeTextFieldState, matchedBrackets = matchedBrackets, ), - removeIndentBackPressCharEventHandler( + removeIndentBackspaceCharEventHandler( textFieldState = codeTextFieldState, ), - removeEmptyBracesBackPressCharEventHandler( + removeEmptyBracesBackspaceCharEventHandler( textFieldState = codeTextFieldState, openingBracket = "[", closingBracket = "]", ), - removeEmptyBracesBackPressCharEventHandler( + removeEmptyBracesBackspaceCharEventHandler( textFieldState = codeTextFieldState, openingBracket = "(", closingBracket = ")", ), - removeEmptyBracesBackPressCharEventHandler( + removeEmptyBracesBackspaceCharEventHandler( textFieldState = codeTextFieldState, openingBracket = "<", closingBracket = ">", ), - removeEmptyBracesBackPressCharEventHandler( + removeEmptyBracesBackspaceCharEventHandler( textFieldState = codeTextFieldState, openingBracket = "{", closingBracket = "}", diff --git a/editor/src/commonMain/kotlin/editor/basic/BasicSourceCodeTextField.kt b/editor/src/commonMain/kotlin/editor/basic/BasicSourceCodeTextField.kt index e817fca..ab97e37 100644 --- a/editor/src/commonMain/kotlin/editor/basic/BasicSourceCodeTextField.kt +++ b/editor/src/commonMain/kotlin/editor/basic/BasicSourceCodeTextField.kt @@ -145,6 +145,7 @@ public fun combineCharEventHandlers(vararg handlers: CharEventHandler?): CharEve { event -> handlers.firstNotNullOfOrNull { it?.invoke(event) } } public typealias KeyEventHandler = (KeyEvent) -> TextFieldValue? +public typealias KeyEventFilter = (KeyEvent) -> Boolean public fun combineKeyEventHandlers(vararg handlers: KeyEventHandler?): KeyEventHandler = { event -> handlers.firstNotNullOfOrNull { it?.invoke(event) } } diff --git a/editor/src/commonMain/kotlin/editor/basic/CharEventHandlers.kt b/editor/src/commonMain/kotlin/editor/basic/CharEventHandlers.kt index 243eee8..a682f31 100644 --- a/editor/src/commonMain/kotlin/editor/basic/CharEventHandlers.kt +++ b/editor/src/commonMain/kotlin/editor/basic/CharEventHandlers.kt @@ -226,7 +226,7 @@ public inline fun newL ) } -public fun removeIndentBackPressCharEventHandler( +public fun removeIndentBackspaceCharEventHandler( textFieldState: BasicSourceCodeTextFieldState, indent: String = " ".repeat(4), ): CharEventHandler = f@{ keyEvent -> @@ -252,7 +252,7 @@ public fun removeIndentBackPressCharEventHandler( ) } -public fun removeEmptyBracesBackPressCharEventHandler( +public fun removeEmptyBracesBackspaceCharEventHandler( textFieldState: BasicSourceCodeTextFieldState, openingBracket: String, closingBracket: String, diff --git a/editor/src/commonMain/kotlin/editor/basic/KeyEventHandlers.kt b/editor/src/commonMain/kotlin/editor/basic/KeyEventHandlers.kt index d6160a7..a38caf4 100644 --- a/editor/src/commonMain/kotlin/editor/basic/KeyEventHandlers.kt +++ b/editor/src/commonMain/kotlin/editor/basic/KeyEventHandlers.kt @@ -6,13 +6,19 @@ import androidx.compose.ui.text.input.TextFieldValue public fun handleMovingOffsets( state: BasicSourceCodeTextFieldState, - indent: String = " ".repeat(4) + indent: String = " ".repeat(4), + moveForwardFilter: KeyEventFilter = { keyEvent -> + !keyEvent.isShiftPressed && keyEvent.key == Key.Tab && keyEvent.type == KeyEventType.KeyDown && + !keyEvent.isAltPressed && !keyEvent.isCtrlPressed && !keyEvent.isMetaPressed + }, + moveBackwardFilter: KeyEventFilter = { keyEvent -> + keyEvent.isShiftPressed && keyEvent.key == Key.Tab && keyEvent.type == KeyEventType.KeyDown && + !keyEvent.isAltPressed && !keyEvent.isCtrlPressed && !keyEvent.isMetaPressed + }, ): KeyEventHandler = f@{ keyEvent: KeyEvent -> - if ( - state.selection.collapsed && !keyEvent.isShiftPressed || keyEvent.key != Key.Tab || - keyEvent.type != KeyEventType.KeyDown || keyEvent.isAltPressed || keyEvent.isCtrlPressed || - keyEvent.isMetaPressed - ) return@f null + val moveForward = !state.selection.collapsed && moveForwardFilter(keyEvent) + val moveBackward = moveBackwardFilter(keyEvent) + if (moveBackward == moveForward) return@f null val selectionLines = state.sourceCodePositions[state.selection.min].line..state.sourceCodePositions[state.selection.max].line val offsetBefore = state.offsets.getOrNull(selectionLines.first - 1)?.last() @@ -21,7 +27,7 @@ public fun handleMovingOffsets( var newSelectionEnd = state.selection.end var newCompositionStart = state.composition?.start var newCompositionEnd = state.composition?.end - if (keyEvent.isShiftPressed) { + if (moveBackward) { fun increment(length: Int, oldOffsetStart: Int) { if (state.selection.start >= oldOffsetStart) newSelectionStart -= minOf(length, state.selection.start - oldOffsetStart) @@ -31,7 +37,8 @@ public fun handleMovingOffsets( if (state.composition != null) { if (state.composition.start >= oldOffsetStart) - newCompositionStart = newCompositionStart!! - minOf(length, state.composition.start - oldOffsetStart) + newCompositionStart = + newCompositionStart!! - minOf(length, state.composition.start - oldOffsetStart) if (state.composition.end >= oldOffsetStart) newCompositionEnd = newCompositionEnd!! - minOf(length, state.composition.end - oldOffsetStart) diff --git a/gradle.properties b/gradle.properties index 030baa8..2751022 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,4 +20,4 @@ development=true #Publication group=com.zhelenskiy -version=0.0.3 +version=0.0.4