Skip to content

Commit

Permalink
Refactor KeyEvent methods to properties
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lamba92 committed Oct 31, 2023
1 parent 3b8b088 commit e049459
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,72 +24,72 @@ interface SelectableColumnKeybindings {
/**
* 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() {
Expand Down Expand Up @@ -119,44 +119,45 @@ open class DefaultSelectableColumnKeybindings : SelectableColumnKeybindings {

companion object : DefaultSelectableColumnKeybindings()

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

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

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

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

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

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

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

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

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

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

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

override fun KeyEvent.scrollPageDownAndExtendSelection() =
key == Key.PageDown && isContiguousSelectionKeyPressed
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 && isMultiSelectionKeyPressed
override val KeyEvent.isSelectAll: Boolean
get() = key == Key.A && isMultiSelectionKeyPressed
}
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 && !isContiguousSelectionKeyPressed
override val KeyEvent.isSelectParent
get() = key == Key.DirectionLeft && !isContiguousSelectionKeyPressed

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

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

override fun KeyEvent.extendSelectionToChild() =
key == Key.DirectionRight && isContiguousSelectionKeyPressed
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 && !isContiguousSelectionKeyPressed
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ class DefaultTreeViewKeyActions(
Log.d(keyEvent.key.keyCode.toString())
if (selectionMode == SelectionMode.None) return@lambda false
when {
selectParent() ?: false -> onSelectParent(keys, state)
selectChild() ?: false -> onSelectChild(keys, state)
isSelectParent -> onSelectParent(keys, state)
isSelectChild -> onSelectChild(keys, state)
super.handleOnKeyEvent(event, keys, state, selectionMode)
.invoke(keyEvent) -> return@lambda true

Expand Down Expand Up @@ -280,51 +280,25 @@ open class DefaultSelectableLazyColumnKeyActions(
selectionMode: SelectionMode,
): Boolean {
when {
selectNextItem() ?: false -> {
onSelectNextItem(keys, state)
}

selectPreviousItem() ?: false -> onSelectPreviousItem(keys, state)
selectFirstItem() ?: false -> onSelectFirstItem(keys, state)
selectLastItem() ?: false -> onSelectLastItem(keys, state)
edit() ?: false -> onEdit()
extendSelectionToFirstItem() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onExtendSelectionToFirst(keys, state)
}

extendSelectionToLastItem() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onExtendSelectionToLastItem(keys, state)
}

extendSelectionWithNextItem() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onExtendSelectionWithNextItem(keys, state)
}

extendSelectionWithPreviousItem() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onExtendSelectionWithPreviousItem(keys, state)
}

scrollPageDownAndExtendSelection() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onScrollPageDownAndExtendSelection(keys, state)
}

scrollPageDownAndSelectItem() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onScrollPageDownAndSelectItem(keys, state)
}

scrollPageUpAndExtendSelection() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onScrollPageUpAndExtendSelection(keys, state)
}

scrollPageUpAndSelectItem() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onScrollPageUpAndSelectItem(keys, state)
}

selectAll() ?: false -> {
if (selectionMode == SelectionMode.Multiple) onSelectAll(keys, state)
isSelectNextItem -> onSelectNextItem(keys, state)
isSelectPreviousItem -> onSelectPreviousItem(keys, state)
isSelectFirstItem -> onSelectFirstItem(keys, state)
isSelectLastItem -> onSelectLastItem(keys, state)
isEdit -> onEdit()
}
if (selectionMode == SelectionMode.Single) {
when {
isExtendSelectionToFirstItem -> onExtendSelectionToFirst(keys, state)
isExtendSelectionToLastItem -> onExtendSelectionToLastItem(keys, state)
isExtendSelectionWithNextItem -> onExtendSelectionWithNextItem(keys, state)
isExtendSelectionWithPreviousItem -> onExtendSelectionWithPreviousItem(keys, state)
isScrollPageDownAndExtendSelection -> onScrollPageDownAndExtendSelection(keys, state)
isScrollPageDownAndSelectItem -> onScrollPageDownAndSelectItem(keys, state)
isScrollPageUpAndExtendSelection -> onScrollPageUpAndExtendSelection(keys, state)
isScrollPageUpAndSelectItem -> onScrollPageUpAndSelectItem(keys, state)
isSelectAll -> onSelectAll(keys, state)
else -> return false
}

else -> return false
}
return true
}
Expand Down

0 comments on commit e049459

Please sign in to comment.