Skip to content

Commit

Permalink
mergeKeyItems function ( cherrypicked from dessalines#700 )
Browse files Browse the repository at this point in the history
  • Loading branch information
bluedrink9 authored and zworek committed Mar 20, 2024
1 parent bff95ab commit 7d3b3c0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/src/main/java/com/dessalines/thumbkey/keyboards/CommonKeys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,37 @@ val SPACEBAR_FRENCH_TYPESPLIT_BOTTOM_KEY_ITEM =
KeyAction.ReplaceLastText(" ; ", trimCount = 3),
),
)

// Dummy items used for creating variants
val DUMMY_KEY =
KeyC(
action = KeyAction.CommitText(""),
display = KeyDisplay.TextDisplay(""),
)

val DUMMY_SKIP_KEY =
KeyC(
action = KeyAction.CommitText(""),
display = KeyDisplay.TextDisplay(""),
)

val SHIFTED_OVERRIDE_KEYITEM =
KeyItemC(
center = DUMMY_KEY,
swipes =
mapOf(
SwipeDirection.BOTTOM to
KeyC(
display = KeyDisplay.IconDisplay(Icons.Outlined.ArrowDropDown),
action = KeyAction.ToggleShiftMode(false),
color = ColorVariant.MUTED,
),
SwipeDirection.TOP to
KeyC(
display = KeyDisplay.IconDisplay(Icons.Outlined.KeyboardCapslock),
capsModeDisplay = KeyDisplay.IconDisplay(Icons.Outlined.Copyright),
action = KeyAction.ToggleCapsLock,
color = ColorVariant.MUTED,
),
),
)
35 changes: 35 additions & 0 deletions app/src/main/java/com/dessalines/thumbkey/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import com.dessalines.thumbkey.IMEService
import com.dessalines.thumbkey.MainActivity
import com.dessalines.thumbkey.R
import com.dessalines.thumbkey.db.DEFAULT_KEYBOARD_LAYOUT
import com.dessalines.thumbkey.keyboards.DUMMY_KEY
import com.dessalines.thumbkey.keyboards.DUMMY_SKIP_KEY
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -1037,3 +1039,36 @@ fun lastColKeysToFirst(board: KeyboardC): KeyboardC {
}
return KeyboardC(newArr)
}

fun mergeKeyItems(
source: KeyItemC,
variant: KeyItemC,
): KeyItemC {
// Returns merge of keyItems.
// If source has swipes of same direction as variant, they'll be replaced.
// Same with center, unless it's a dummy key (since a centre key is
// mandatory).
// DUMMY_SKIP_KEYs will be removed (if you want to blank a spot). Not supported for
// center keys, only for swipes.

// Determine the new center. If the variant's center is a 'dummy', keep the source's center.
val newCenter = if (variant.center == DUMMY_KEY) source.center else variant.center

// Merge swipes, with variant's swipes overriding source's swipes for the same SwipeDirection
val newSwipes =
source.swipes.orEmpty().toMutableMap().apply {
variant.swipes?.forEach { (direction, keyC) ->
if (keyC == DUMMY_SKIP_KEY) {
this.remove(direction)
} else {
this[direction] = keyC
}
}
}

return source.copy(
center = newCenter,
// if newSwipes is empty, it means all swipes were removed from source.
swipes = if (newSwipes.isNotEmpty()) newSwipes else null,
)
}

0 comments on commit 7d3b3c0

Please sign in to comment.