Skip to content

Commit

Permalink
Refactor key-bindings and pointer event handlers and fixed meta press…
Browse files Browse the repository at this point in the history
…ed on macos (#233)

* Refactor key-bindings and pointer event handlers and fixed meta pressed on macos.

Key-bindings and pointer event handlers in SelectableLazyColumn and related tree views have been refactored for improved clarity and usability. This includes changes in default keybindings for selectable columns and adjustments in the hierarchical structure of key event handling for better organization and logic flow. Pivotal changes were made in the naming and function calls related to multi-selection and contiguous selection key events.

* Refactor KeyEvent methods to properties

This commit refactor methods related to KeyEvent in different classes to properties. Operator functions for key event handling were replaced with their equivalent properties, for better clarity and readability. This includes alterations in naming conventions like 'selectParent()' to 'isSelectParent' which are more expressive and intuitive to understand their underlying functionality.

* Refactor KeyBindingActions to KeyActions

Updated KeyBindingActions interface to KeyActions and renamed methods to properties for improved clarity. This change better conveys what these properties are doing and makes their functionality more intuitive to understand. This encompasses modifications in method names such as 'selectParent()' to 'isSelectParent'.
  • Loading branch information
lamba92 authored Oct 31, 2023
1 parent 614934c commit 59ff624
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 224 deletions.
158 changes: 79 additions & 79 deletions foundation/api/foundation.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,137 +7,157 @@ import androidx.compose.ui.input.key.isMetaPressed
import androidx.compose.ui.input.key.isShiftPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.pointer.PointerKeyboardModifiers
import androidx.compose.ui.input.pointer.isCtrlPressed
import androidx.compose.ui.input.pointer.isMetaPressed
import androidx.compose.ui.input.pointer.isShiftPressed

interface SelectableColumnKeybindings {

val KeyEvent.isKeyboardMultiSelectionKeyPressed: Boolean
get() = isShiftPressed
val KeyEvent.isContiguousSelectionKeyPressed: Boolean

val PointerKeyboardModifiers.isKeyboardMultiSelectionKeyPressed: Boolean
get() = isShiftPressed
val PointerKeyboardModifiers.isContiguousSelectionKeyPressed: Boolean

val KeyEvent.isKeyboardCtrlMetaKeyPressed: Boolean
get() = isCtrlPressed || isMetaPressed
val KeyEvent.isMultiSelectionKeyPressed: Boolean

val PointerKeyboardModifiers.isMultiSelectionKeyPressed: Boolean

/**
* Select First Node
*/
fun KeyEvent.selectFirstItem(): Boolean?
val KeyEvent.isSelectFirstItem: Boolean

/**
* Extend Selection to First Node inherited from Move Caret to Text Start with Selection
*/
fun KeyEvent.extendSelectionToFirstItem(): Boolean?
val KeyEvent.isExtendSelectionToFirstItem: Boolean

/**
* Select Last Node inherited from Move Caret to Text End
*/
fun KeyEvent.selectLastItem(): Boolean?
val KeyEvent.isSelectLastItem: Boolean

/**
* Extend Selection to Last Node inherited from Move Caret to Text End with Selection
*/
fun KeyEvent.extendSelectionToLastItem(): Boolean?
val KeyEvent.isExtendSelectionToLastItem: Boolean

/**
* Select Previous Node inherited from Up
*/
fun KeyEvent.selectPreviousItem(): Boolean?
val KeyEvent.isSelectPreviousItem: Boolean

/**
* Extend Selection with Previous Node inherited from Up with Selection
*/
fun KeyEvent.extendSelectionWithPreviousItem(): Boolean?
val KeyEvent.isExtendSelectionWithPreviousItem: Boolean

/**
* Select Next Node inherited from Down
*/
fun KeyEvent.selectNextItem(): Boolean?
val KeyEvent.isSelectNextItem: Boolean

/**
* Extend Selection with Next Node inherited from Down with Selection
*/
fun KeyEvent.extendSelectionWithNextItem(): Boolean?
val KeyEvent.isExtendSelectionWithNextItem: Boolean

/**
* Scroll Page Up and Select Node inherited from Page Up
*/
fun KeyEvent.scrollPageUpAndSelectItem(): Boolean?
val KeyEvent.isScrollPageUpAndSelectItem: Boolean

/**
* Scroll Page Up and Extend Selection inherited from Page Up with Selection
*/
fun KeyEvent.scrollPageUpAndExtendSelection(): Boolean?
val KeyEvent.isScrollPageUpAndExtendSelection: Boolean

/**
* Scroll Page Down and Select Node inherited from Page Down
*/
fun KeyEvent.scrollPageDownAndSelectItem(): Boolean?
val KeyEvent.isScrollPageDownAndSelectItem: Boolean

/**
* Scroll Page Down and Extend Selection inherited from Page Down with Selection
*/
fun KeyEvent.scrollPageDownAndExtendSelection(): Boolean?
val KeyEvent.isScrollPageDownAndExtendSelection: Boolean

/**
* Edit item
*/
fun KeyEvent.edit(): Boolean?
val KeyEvent.isEdit: Boolean

/**
* SelectAll
*/
fun KeyEvent.selectAll(): Boolean?
val KeyEvent.isSelectAll: Boolean
}

open class DefaultMacOsSelectableColumnKeybindings : DefaultSelectableColumnKeybindings() {

companion object : DefaultMacOsSelectableColumnKeybindings()

override val KeyEvent.isMultiSelectionKeyPressed: Boolean
get() = isMetaPressed

override val PointerKeyboardModifiers.isMultiSelectionKeyPressed: Boolean
get() = isMetaPressed
}

open class DefaultSelectableColumnKeybindings : SelectableColumnKeybindings {
companion object : DefaultSelectableColumnKeybindings()

override val KeyEvent.isKeyboardMultiSelectionKeyPressed: Boolean
override val KeyEvent.isContiguousSelectionKeyPressed: Boolean
get() = isShiftPressed

override val PointerKeyboardModifiers.isKeyboardMultiSelectionKeyPressed: Boolean
override val PointerKeyboardModifiers.isContiguousSelectionKeyPressed: Boolean
get() = isShiftPressed

override fun KeyEvent.selectFirstItem() =
key == Key.Home && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isMultiSelectionKeyPressed: Boolean
get() = isCtrlPressed

override val PointerKeyboardModifiers.isMultiSelectionKeyPressed: Boolean
get() = isCtrlPressed

companion object : DefaultSelectableColumnKeybindings()

override val KeyEvent.isSelectFirstItem
get() = key == Key.Home && !isContiguousSelectionKeyPressed

override fun KeyEvent.extendSelectionToFirstItem() =
key == Key.Home && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isExtendSelectionToFirstItem
get() = key == Key.Home && isContiguousSelectionKeyPressed

override fun KeyEvent.selectLastItem() =
key == Key.MoveEnd && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isSelectLastItem
get() = key == Key.MoveEnd && !isContiguousSelectionKeyPressed

override fun KeyEvent.extendSelectionToLastItem() =
key == Key.MoveEnd && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isExtendSelectionToLastItem
get() = key == Key.MoveEnd && isContiguousSelectionKeyPressed

override fun KeyEvent.selectPreviousItem() =
key == Key.DirectionUp && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isSelectPreviousItem
get() = key == Key.DirectionUp && !isContiguousSelectionKeyPressed

override fun KeyEvent.extendSelectionWithPreviousItem() =
key == Key.DirectionUp && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isExtendSelectionWithPreviousItem
get() = key == Key.DirectionUp && isContiguousSelectionKeyPressed

override fun KeyEvent.selectNextItem() =
key == Key.DirectionDown && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isSelectNextItem
get() = key == Key.DirectionDown && !isContiguousSelectionKeyPressed

override fun KeyEvent.extendSelectionWithNextItem() =
key == Key.DirectionDown && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isExtendSelectionWithNextItem
get() = key == Key.DirectionDown && isContiguousSelectionKeyPressed

override fun KeyEvent.scrollPageUpAndSelectItem() =
key == Key.PageUp && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isScrollPageUpAndSelectItem
get() = key == Key.PageUp && !isContiguousSelectionKeyPressed

override fun KeyEvent.scrollPageUpAndExtendSelection() =
key == Key.PageUp && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isScrollPageUpAndExtendSelection
get() = key == Key.PageUp && isContiguousSelectionKeyPressed

override fun KeyEvent.scrollPageDownAndSelectItem() =
key == Key.PageDown && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isScrollPageDownAndSelectItem
get() = key == Key.PageDown && !isContiguousSelectionKeyPressed

override fun KeyEvent.scrollPageDownAndExtendSelection() =
key == Key.PageDown && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isScrollPageDownAndExtendSelection
get() = key == Key.PageDown && isContiguousSelectionKeyPressed

override fun KeyEvent.edit() = false
override val KeyEvent.isEdit
get() = false

override fun KeyEvent.selectAll(): Boolean? =
key == Key.A && isKeyboardCtrlMetaKeyPressed
override val KeyEvent.isSelectAll: Boolean
get() = key == Key.A && isMultiSelectionKeyPressed
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlinx.coroutines.launch
import org.jetbrains.jewel.foundation.lazy.SelectableLazyListScopeContainer.Entry
import org.jetbrains.jewel.foundation.lazy.tree.DefaultSelectableLazyColumnEventAction
import org.jetbrains.jewel.foundation.lazy.tree.DefaultSelectableLazyColumnKeyActions
import org.jetbrains.jewel.foundation.lazy.tree.KeyBindingActions
import org.jetbrains.jewel.foundation.lazy.tree.KeyActions
import org.jetbrains.jewel.foundation.lazy.tree.PointerEventActions

/**
Expand All @@ -45,7 +45,7 @@ fun SelectableLazyColumn(
verticalArrangement: Arrangement.Vertical = if (!reverseLayout) Arrangement.Top else Arrangement.Bottom,
horizontalAlignment: Alignment.Horizontal = Alignment.Start,
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
keyActions: KeyBindingActions = DefaultSelectableLazyColumnKeyActions(),
keyActions: KeyActions = DefaultSelectableLazyColumnKeyActions,
pointerEventActions: PointerEventActions = DefaultSelectableLazyColumnEventAction(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: SelectableLazyListScope.() -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun <T> BasicLazyTree(
onElementDoubleClick: (Tree.Element<T>) -> Unit,
onSelectionChange: (List<Tree.Element<T>>) -> Unit,
platformDoubleClickDelay: Duration = 500.milliseconds,
keyActions: KeyBindingActions = DefaultTreeViewKeyActions(treeState),
keyActions: KeyActions = DefaultTreeViewKeyActions(treeState),
pointerEventScopedActions: PointerEventActions = remember {
DefaultTreeViewPointerEventAction(treeState)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,58 @@ open class DefaultTreeViewKeybindings : DefaultSelectableColumnKeybindings(), Tr

companion object : DefaultTreeViewKeybindings()

override fun KeyEvent.selectParent() =
key == Key.DirectionLeft && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isSelectParent
get() = key == Key.DirectionLeft && !isContiguousSelectionKeyPressed

override fun KeyEvent.extendSelectionToParent() =
key == Key.DirectionLeft && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isExtendSelectionToParent
get() = key == Key.DirectionLeft && isContiguousSelectionKeyPressed

override fun KeyEvent.selectChild() =
key == Key.DirectionRight && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isSelectChild
get() = key == Key.DirectionRight && !isContiguousSelectionKeyPressed

override fun KeyEvent.extendSelectionToChild() =
key == Key.DirectionRight && isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isExtendSelectionToChild
get() = key == Key.DirectionRight && isContiguousSelectionKeyPressed

override fun KeyEvent.selectNextSibling() = null
override val KeyEvent.isSelectNextSibling
get() = false

override fun KeyEvent.selectPreviousSibling() = null
override val KeyEvent.isSelectPreviousSibling
get() = false

override fun KeyEvent.edit() =
key == Key.F2 && !isKeyboardMultiSelectionKeyPressed
override val KeyEvent.isEdit get() = key == Key.F2 && !isContiguousSelectionKeyPressed
}

interface TreeViewKeybindings : SelectableColumnKeybindings {

/**
* Select Parent Node
*/
fun KeyEvent.selectParent(): Boolean?
val KeyEvent.isSelectParent: Boolean

/**
* Extend Selection to Parent Node inherited from Left with Selection
*/
fun KeyEvent.extendSelectionToParent(): Boolean?
val KeyEvent.isExtendSelectionToParent: Boolean

/**
* Select Child Node inherited from Right
*/
fun KeyEvent.selectChild(): Boolean?
val KeyEvent.isSelectChild: Boolean

/**
* Extend Selection to Child Node inherited from Right with Selection
*/
fun KeyEvent.extendSelectionToChild(): Boolean?
val KeyEvent.isExtendSelectionToChild: Boolean

/**
* Select Next Sibling Node
*/
fun KeyEvent.selectNextSibling(): Boolean?
val KeyEvent.isSelectNextSibling: Boolean

/**
* Select Previous Sibling Node
*/
fun KeyEvent.selectPreviousSibling(): Boolean?
val KeyEvent.isSelectPreviousSibling: Boolean
}

@Suppress("unused")
Expand All @@ -83,9 +84,9 @@ typealias TreeViewClickModifierHandler = PointerKeyboardModifiers.() -> Boolean
open class DefaultMacOsTreeColumnKeybindings : DefaultTreeViewKeybindings() {
companion object : DefaultMacOsTreeColumnKeybindings()

override val KeyEvent.isKeyboardMultiSelectionKeyPressed: Boolean
override val KeyEvent.isMultiSelectionKeyPressed: Boolean
get() = isMetaPressed

override val PointerKeyboardModifiers.isKeyboardMultiSelectionKeyPressed: Boolean
override val PointerKeyboardModifiers.isMultiSelectionKeyPressed: Boolean
get() = isMetaPressed
}
Loading

0 comments on commit 59ff624

Please sign in to comment.