mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 14:26:00 +00:00
feat: add configurable touchscreen gestures (#940)
This commit is contained in:
1 parent
3a8d6aaabb
commit
4c81c98f12
55 files changed
+7404
-14
No files matched your search
@@ -0,0 +1,128 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.droiddeck.launcher.R
|
||||
import java.text.Collator
|
||||
|
||||
internal enum class GestureActionGroup(@StringRes val title: Int) {
|
||||
MOUSE(R.string.gesture_group_mouse), APP(R.string.gesture_group_app),
|
||||
NAVIGATION(R.string.gesture_group_navigation), LETTERS(R.string.gesture_group_letters),
|
||||
NUMBERS(R.string.gesture_group_numbers), FUNCTION(R.string.gesture_group_function),
|
||||
MODIFIERS(R.string.gesture_group_modifiers), NUMPAD(R.string.gesture_group_numpad),
|
||||
SYMBOLS(R.string.gesture_group_symbols),
|
||||
}
|
||||
|
||||
internal data class GestureAction(val id: String, val title: String, val group: GestureActionGroup)
|
||||
|
||||
internal fun gestureActionGroup(id: String): GestureActionGroup {
|
||||
if (id == "show_keyboard") return GestureActionGroup.APP
|
||||
if (id in setOf("left_click", "right_click", "middle_click", "left_click_drag", "right_click_drag",
|
||||
"middle_mouse_pan", "inverted_middle_mouse_pan", "scroll_wheel")) return GestureActionGroup.MOUSE
|
||||
val key = id.removePrefix("key_")
|
||||
return when {
|
||||
key.startsWith("KP_") -> GestureActionGroup.NUMPAD
|
||||
key.length == 1 && key[0] in 'A'..'Z' -> GestureActionGroup.LETTERS
|
||||
key.length == 1 && key[0].isDigit() -> GestureActionGroup.NUMBERS
|
||||
key.matches(Regex("F(?:[1-9]|1[0-2])")) -> GestureActionGroup.FUNCTION
|
||||
key.startsWith("CTRL_") || key.startsWith("SHIFT_") || key.startsWith("ALT_") || key.endsWith("_LOCK") -> GestureActionGroup.MODIFIERS
|
||||
key in setOf("MINUS", "EQUAL", "BRACKET_LEFT", "BRACKET_RIGHT", "SEMICOLON", "APOSTROPHE",
|
||||
"GRAVE", "TILDE", "BACKSLASH", "COMMA", "PERIOD", "SLASH") || id == "plus_minus" -> GestureActionGroup.SYMBOLS
|
||||
else -> GestureActionGroup.NAVIGATION
|
||||
}
|
||||
}
|
||||
|
||||
internal fun filterGestureActions(actions: List<GestureAction>, query: String, group: GestureActionGroup?): List<GestureAction> {
|
||||
val words = query.trim().split(Regex("\\s+")).filter { it.isNotEmpty() }
|
||||
val collator = Collator.getInstance()
|
||||
return actions.filter { action ->
|
||||
(group == null || action.group == group) && words.all { word ->
|
||||
action.title.contains(word, ignoreCase = true) || action.id.removePrefix("key_").replace('_', ' ').contains(word, ignoreCase = true)
|
||||
}
|
||||
}.sortedWith(compareBy<GestureAction> { it.group.ordinal }.thenComparator { a, b ->
|
||||
// Function keys follow their numeric sequence; names use the UI locale.
|
||||
if (a.group == GestureActionGroup.FUNCTION) {
|
||||
a.id.removePrefix("key_F").toInt().compareTo(b.id.removePrefix("key_F").toInt())
|
||||
} else collator.compare(a.title, b.title)
|
||||
}.thenBy { it.id })
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun gestureActionTitle(action: String): String {
|
||||
val named = when (action) {
|
||||
"left_click" -> R.string.gesture_left_click
|
||||
"right_click" -> R.string.gesture_right_click
|
||||
"middle_click" -> R.string.gesture_middle_click
|
||||
"show_keyboard" -> R.string.gesture_keyboard
|
||||
"left_click_drag" -> R.string.gesture_left_drag
|
||||
"right_click_drag" -> R.string.gesture_right_drag
|
||||
"middle_mouse_pan" -> R.string.gesture_middle_pan
|
||||
"inverted_middle_mouse_pan" -> R.string.gesture_middle_pan_inverted
|
||||
"arrow_keys" -> R.string.gesture_arrows
|
||||
"inverted_arrow_keys" -> R.string.gesture_arrows_inverted
|
||||
"inverted_wasd" -> R.string.gesture_wasd_inverted
|
||||
"scroll_wheel" -> R.string.gesture_scroll
|
||||
else -> null
|
||||
}
|
||||
if (named != null) return stringResource(named)
|
||||
return when (action) {
|
||||
"wasd" -> "WASD"
|
||||
"plus_minus" -> "+ / −"
|
||||
"page_up_down" -> stringResource(R.string.gesture_key_page_up) + " / " + stringResource(R.string.gesture_key_page_down)
|
||||
else -> gestureKeyTitle(action.removePrefix("key_"))
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun gestureKeyTitle(key: String): String {
|
||||
if (key.startsWith("KP_")) return stringResource(R.string.gesture_key_numpad, gestureKeyTitle(key.removePrefix("KP_")))
|
||||
val named = when (key) {
|
||||
"ESC" -> R.string.gesture_key_escape
|
||||
"BKSP" -> R.string.gesture_key_backspace
|
||||
"ENTER" -> R.string.gesture_key_enter
|
||||
"TAB" -> R.string.gesture_key_tab
|
||||
"SPACE" -> R.string.gesture_key_space
|
||||
"DEL" -> R.string.gesture_key_delete
|
||||
"INSERT" -> R.string.gesture_key_insert
|
||||
"HOME" -> R.string.gesture_key_home
|
||||
"END" -> R.string.gesture_key_end
|
||||
"PRIOR" -> R.string.gesture_key_page_up
|
||||
"NEXT" -> R.string.gesture_key_page_down
|
||||
"PRTSCN" -> R.string.gesture_key_print_screen
|
||||
"UP" -> R.string.gesture_key_up
|
||||
"DOWN" -> R.string.gesture_key_down
|
||||
"LEFT" -> R.string.gesture_key_left_arrow
|
||||
"RIGHT" -> R.string.gesture_key_right_arrow
|
||||
"CAPS_LOCK" -> R.string.gesture_key_caps_lock
|
||||
"NUM_LOCK" -> R.string.gesture_key_num_lock
|
||||
"SCROLL_LOCK" -> R.string.gesture_key_scroll_lock
|
||||
else -> null
|
||||
}
|
||||
if (named != null) return stringResource(named)
|
||||
if (key.startsWith("CTRL_") || key.startsWith("SHIFT_") || key.startsWith("ALT_")) {
|
||||
val name = stringResource(when (key.substringBefore('_')) {
|
||||
"CTRL" -> R.string.pckb_key_ctrl
|
||||
"SHIFT" -> R.string.gesture_key_shift
|
||||
else -> R.string.pckb_key_alt
|
||||
})
|
||||
return stringResource(if (key.endsWith("_L")) R.string.gesture_key_left else R.string.gesture_key_right, name)
|
||||
}
|
||||
return when (key) {
|
||||
"MINUS", "SUBTRACT" -> "−"
|
||||
"EQUAL" -> "="
|
||||
"ADD" -> "+"
|
||||
"MULTIPLY" -> "×"
|
||||
"DIVIDE", "SLASH" -> "/"
|
||||
"BRACKET_LEFT" -> "["
|
||||
"BRACKET_RIGHT" -> "]"
|
||||
"SEMICOLON" -> ";"
|
||||
"APOSTROPHE" -> "'"
|
||||
"GRAVE" -> "`"
|
||||
"TILDE" -> "~"
|
||||
"BACKSLASH" -> "\\"
|
||||
"COMMA" -> ","
|
||||
"PERIOD" -> "."
|
||||
else -> key.replace('_', ' ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.input.TouchscreenGestures.Gesture
|
||||
|
||||
@Composable
|
||||
fun GestureFeedback(gesture: Gesture?) {
|
||||
Box(Modifier.fillMaxSize()) {
|
||||
if (gesture != null) Text(stringResource(when (gesture) {
|
||||
Gesture.TAP -> R.string.gesture_tap
|
||||
Gesture.DRAG -> R.string.gesture_drag
|
||||
Gesture.LONG_PRESS -> R.string.gesture_long_press
|
||||
Gesture.DOUBLE_TAP -> R.string.gesture_double_tap
|
||||
Gesture.TWO_FINGER_TAP -> R.string.gesture_two_tap
|
||||
Gesture.TWO_FINGER_DRAG -> R.string.gesture_two_drag
|
||||
Gesture.TWO_FINGER_HOLD -> R.string.gesture_two_hold
|
||||
Gesture.PINCH -> R.string.gesture_pinch
|
||||
Gesture.THREE_FINGER_TAP -> R.string.gesture_three_tap
|
||||
Gesture.THREE_FINGER_DRAG -> R.string.gesture_three_drag
|
||||
Gesture.THREE_FINGER_HOLD -> R.string.gesture_three_hold
|
||||
}),
|
||||
Modifier.align(Alignment.TopCenter).padding(top = 24.dp)
|
||||
.background(MaterialTheme.colorScheme.surface).padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
color = MaterialTheme.colorScheme.onSurface)
|
||||
}
|
||||
}
|
||||
@@ -250,7 +250,7 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras:
|
||||
SettingsGroup(if (steam) stringResource(R.string.mode_touch_controls) else stringResource(R.string.mode_touch)) {
|
||||
ChoiceRow(
|
||||
host, "touch", stringResource(R.string.mode_touch), null,
|
||||
listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.common_auto), SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_direct), SessionPrefs.TOUCH_OFF to stringResource(R.string.widgets_off)), s.touchMode,
|
||||
listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.common_auto), SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_direct), SessionPrefs.TOUCH_GESTURES to stringResource(R.string.mode_touch_gestures), SessionPrefs.TOUCH_OFF to stringResource(R.string.widgets_off)), s.touchMode,
|
||||
note = stringResource(R.string.mode_touch_note),
|
||||
onPick = a.onTouch,
|
||||
)
|
||||
|
||||
@@ -245,6 +245,7 @@ class DrawerActions(
|
||||
val onLowLatencyController: (Boolean) -> Unit = {},
|
||||
val onLowLatencyTouch: (Boolean) -> Unit = {},
|
||||
val onTouch: (String) -> Unit,
|
||||
val onGestureSettings: () -> Unit = {},
|
||||
val onFexPreset: (String) -> Unit,
|
||||
val onUpscaler: (Int) -> Unit = {},
|
||||
val onUpscaleSharpness: (Int) -> Unit = {},
|
||||
@@ -500,8 +501,11 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
|
||||
type, chipModifier = focus.track(page, "pad-type"), onPick = a.onPadType)
|
||||
}
|
||||
ChoiceRow(host, "touch", stringResource(R.string.mode_touch), null,
|
||||
listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.drawer_touch_auto, a.touchAuto), SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_direct), SessionPrefs.TOUCH_OFF to stringResource(R.string.widgets_off)),
|
||||
listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.drawer_touch_auto, a.touchAuto), SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_direct), SessionPrefs.TOUCH_GESTURES to stringResource(R.string.mode_touch_gestures), SessionPrefs.TOUCH_OFF to stringResource(R.string.widgets_off)),
|
||||
a.touchMode, chipModifier = focus.track(page, "touch"), onPick = a.onTouch)
|
||||
ActionRow(stringResource(R.string.gesture_settings), stringResource(R.string.gesture_settings_hint),
|
||||
stringResource(R.string.ctrl_edit), a.onGestureSettings,
|
||||
modifier = focus.track(page, "gesture-settings"))
|
||||
ChoiceRow(host, "osc", stringResource(R.string.mode_osc), null,
|
||||
if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never))
|
||||
else listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)),
|
||||
|
||||
@@ -648,8 +648,8 @@ fun MultiRow(
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ActionRow(label: String, hint: String?, button: String, onClick: () -> Unit, progress: Float? = null) {
|
||||
SettingsRow(label, hint) { SecondaryButton(button, progress = progress, onClick = onClick) }
|
||||
fun ActionRow(label: String, hint: String?, button: String, onClick: () -> Unit, progress: Float? = null, modifier: Modifier = Modifier) {
|
||||
SettingsRow(label, hint) { SecondaryButton(button, modifier = modifier, progress = progress, onClick = onClick) }
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@@ -0,0 +1,345 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.selection.selectable
|
||||
import androidx.compose.foundation.selection.toggleable
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowForward
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Keyboard
|
||||
import androidx.compose.material.icons.filled.Mouse
|
||||
import androidx.compose.material.icons.filled.Search
|
||||
import androidx.compose.material.icons.filled.TouchApp
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.LayoutDirection
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.input.GestureKeys
|
||||
import com.droiddeck.launcher.input.GesturePrefs
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.ACTION_LEFT_CLICK
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.ACTION_RIGHT_CLICK
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.ACTION_MIDDLE_CLICK
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.ACTION_SHOW_KEYBOARD
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.ACTION_KEY_TILDE
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.PAN_ACTIONS
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.ZOOM_ACTIONS
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.MOUSE_BEHAVIOR_HOLD
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.MOUSE_BEHAVIOR_CLICK
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.MOUSE_DRAG_MOVEMENT_DIRECT
|
||||
import com.droiddeck.launcher.input.TouchGestureConfig.Companion.MOUSE_DRAG_MOVEMENT_RELATIVE
|
||||
|
||||
@Composable
|
||||
fun TouchGestureDialog(game: String?, onDismiss: () -> Unit, onSaved: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
// Keep an open editor attached to the game it was opened for.
|
||||
val targetGame = remember { game }
|
||||
val initialDefaults = remember { GesturePrefs.read(context) }
|
||||
val initialGame = remember { targetGame?.takeIf { GesturePrefs.hasOverride(context, it) }?.let { GesturePrefs.read(context, it) } }
|
||||
var defaults by remember { mutableStateOf(initialDefaults) }
|
||||
var gameDraft by remember { mutableStateOf(initialGame) }
|
||||
var forGame by rememberSaveable { mutableStateOf(targetGame != null) }
|
||||
val config = if (forGame) gameDraft ?: defaults else defaults
|
||||
val change: (TouchGestureConfig) -> Unit = { if (forGame) gameDraft = it else defaults = it }
|
||||
var tab by rememberSaveable { mutableIntStateOf(0) }
|
||||
val tabs = listOf(R.string.gesture_tab_one, R.string.gesture_tab_two, R.string.gesture_tab_three, R.string.gesture_tab_options)
|
||||
val mouse = listOf(ACTION_LEFT_CLICK, ACTION_RIGHT_CLICK, ACTION_MIDDLE_CLICK)
|
||||
val actions = mouse + ACTION_SHOW_KEYBOARD + GestureKeys.codes.keys.map { "key_$it" } + ACTION_KEY_TILDE
|
||||
val p = LocalPalette.current
|
||||
|
||||
GestureDialogFrame(stringResource(R.string.gesture_settings), stringResource(R.string.gesture_editor_subtitle), onDismiss,
|
||||
footer = {
|
||||
Row(Modifier.fillMaxWidth().padding(horizontal = 20.dp, vertical = 12.dp), horizontalArrangement = Arrangement.End, verticalAlignment = Alignment.CenterVertically) {
|
||||
TextButton(onClick = onDismiss) { Text(stringResource(R.string.gesture_cancel)) }
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Button(onClick = {
|
||||
if (defaults != initialDefaults) GesturePrefs.save(context, null, defaults)
|
||||
if (targetGame != null && gameDraft != initialGame) {
|
||||
gameDraft?.let { GesturePrefs.save(context, targetGame, it) } ?: GesturePrefs.clear(context, targetGame)
|
||||
}
|
||||
onSaved()
|
||||
}, modifier = Modifier.testTag("gesture-save")) { Text(stringResource(R.string.gesture_save_changes)) }
|
||||
}
|
||||
}) {
|
||||
Column(Modifier.padding(horizontal = 20.dp)) {
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
GestureFilterChip(!forGame, onClick = { forGame = false }, label = { Text(stringResource(R.string.gesture_defaults)) }, modifier = Modifier.testTag("gesture-scope-default"))
|
||||
if (targetGame != null) GestureFilterChip(forGame, onClick = { forGame = true }, label = { Text(stringResource(R.string.gesture_game)) }, modifier = Modifier.testTag("gesture-scope-game"))
|
||||
if (forGame && gameDraft != null) TextButton(onClick = { gameDraft = null }, modifier = Modifier.testTag("gesture-inherit")) { Text(stringResource(R.string.gesture_use_defaults)) }
|
||||
}
|
||||
Text(stringResource(if (!forGame) R.string.gesture_scope_default else if (gameDraft == null) R.string.gesture_inheriting else R.string.gesture_scope_game),
|
||||
color = p.onSurfaceVariant, style = MaterialTheme.typography.bodySmall, modifier = Modifier.padding(bottom = 12.dp))
|
||||
}
|
||||
ScrollableTabRow(tab, edgePadding = 12.dp, containerColor = p.surface, divider = { HorizontalDivider(color = p.line) }) {
|
||||
tabs.forEachIndexed { index, title ->
|
||||
Tab(tab == index, onClick = { tab = index }, text = { Text(stringResource(title)) }, selectedContentColor = p.onBackground,
|
||||
unselectedContentColor = p.onSurfaceVariant, modifier = Modifier.testTag("gesture-tab-$index"))
|
||||
}
|
||||
}
|
||||
key(tab) {
|
||||
Column(Modifier.weight(1f).fillMaxWidth().verticalScroll(rememberScrollState()).padding(20.dp), verticalArrangement = Arrangement.spacedBy(14.dp)) {
|
||||
when (tab) {
|
||||
0 -> {
|
||||
GestureSection(R.string.gesture_tab_one, R.string.gesture_one_hint)
|
||||
GestureCard(R.string.gesture_tap, config.tapEnabled, { change(config.copy(tapEnabled = it)) }) {
|
||||
GestureActionField("tap", R.string.gesture_tap, config.tapAction, actions) { change(config.copy(tapAction = it)) }
|
||||
}
|
||||
GestureCard(R.string.gesture_drag, config.dragEnabled, { change(config.copy(dragEnabled = it)) }) {
|
||||
GestureActionField("drag", R.string.gesture_drag, config.dragAction, PAN_ACTIONS) { change(config.copy(dragAction = it)) }
|
||||
}
|
||||
GestureCard(R.string.gesture_long_press, config.longPressEnabled, { change(config.copy(longPressEnabled = it)) }) {
|
||||
GestureActionField("long-press", R.string.gesture_long_press, config.longPressAction, actions) { change(config.copy(longPressAction = it)) }
|
||||
GestureHoldOptions(config.longPressAction, config.longPressMouseBehavior, config.longPressDelay, { change(config.copy(longPressMouseBehavior = it)) }, { change(config.copy(longPressDelay = it)) })
|
||||
}
|
||||
GestureCard(R.string.gesture_double_tap, config.doubleTapEnabled, { change(config.copy(doubleTapEnabled = it)) }) {
|
||||
GestureDelay(stringResource(R.string.gesture_double_delay), config.doubleTapDelay) { change(config.copy(doubleTapDelay = it)) }
|
||||
}
|
||||
}
|
||||
1 -> {
|
||||
GestureSection(R.string.gesture_tab_two, R.string.gesture_two_hint)
|
||||
GestureCard(R.string.gesture_two_tap, config.twoFingerTapEnabled, { change(config.copy(twoFingerTapEnabled = it)) }) {
|
||||
GestureActionField("two-tap", R.string.gesture_two_tap, config.twoFingerTapAction, actions) { change(config.copy(twoFingerTapAction = it)) }
|
||||
}
|
||||
GestureCard(R.string.gesture_two_drag, config.twoFingerDragEnabled, { change(config.copy(twoFingerDragEnabled = it)) }) {
|
||||
GestureActionField("two-drag", R.string.gesture_two_drag, config.twoFingerDragAction, PAN_ACTIONS) { change(config.copy(twoFingerDragAction = it)) }
|
||||
}
|
||||
GestureCard(R.string.gesture_two_hold, config.twoFingerHoldEnabled, { change(config.copy(twoFingerHoldEnabled = it)) }) {
|
||||
GestureActionField("two-hold", R.string.gesture_two_hold, config.twoFingerHoldAction, actions) { change(config.copy(twoFingerHoldAction = it)) }
|
||||
GestureHoldOptions(config.twoFingerHoldAction, config.twoFingerHoldMouseBehavior, config.twoFingerHoldDelay, { change(config.copy(twoFingerHoldMouseBehavior = it)) }, { change(config.copy(twoFingerHoldDelay = it)) })
|
||||
}
|
||||
GestureCard(R.string.gesture_pinch, config.pinchEnabled, { change(config.copy(pinchEnabled = it)) }) {
|
||||
GestureActionField("pinch", R.string.gesture_pinch, config.pinchAction, ZOOM_ACTIONS) { change(config.copy(pinchAction = it)) }
|
||||
}
|
||||
}
|
||||
2 -> {
|
||||
GestureSection(R.string.gesture_tab_three, R.string.gesture_three_hint)
|
||||
GestureCard(R.string.gesture_three_tap, config.threeFingerTapEnabled, { change(config.copy(threeFingerTapEnabled = it)) }) {
|
||||
GestureActionField("three-tap", R.string.gesture_three_tap, config.threeFingerTapAction, actions) { change(config.copy(threeFingerTapAction = it)) }
|
||||
}
|
||||
GestureCard(R.string.gesture_three_drag, config.threeFingerDragEnabled, { change(config.copy(threeFingerDragEnabled = it)) }) {
|
||||
GestureActionField("three-drag", R.string.gesture_three_drag, config.threeFingerDragAction, PAN_ACTIONS) { change(config.copy(threeFingerDragAction = it)) }
|
||||
}
|
||||
GestureCard(R.string.gesture_three_hold, config.threeFingerHoldEnabled, { change(config.copy(threeFingerHoldEnabled = it)) }) {
|
||||
GestureActionField("three-hold", R.string.gesture_three_hold, config.threeFingerHoldAction, actions) { change(config.copy(threeFingerHoldAction = it)) }
|
||||
GestureHoldOptions(config.threeFingerHoldAction, config.threeFingerHoldMouseBehavior, config.threeFingerHoldDelay, { change(config.copy(threeFingerHoldMouseBehavior = it)) }, { change(config.copy(threeFingerHoldDelay = it)) })
|
||||
}
|
||||
}
|
||||
3 -> {
|
||||
GestureSection(R.string.gesture_tab_options, R.string.gesture_options_hint)
|
||||
GestureSettingsCard(R.string.gesture_movement) {
|
||||
Text(stringResource(R.string.gesture_movement_hint), color = p.onSurfaceVariant, style = MaterialTheme.typography.bodyMedium)
|
||||
GestureChoiceChips(config.mouseDragMovementMode, listOf(MOUSE_DRAG_MOVEMENT_DIRECT to stringResource(R.string.gesture_direct), MOUSE_DRAG_MOVEMENT_RELATIVE to stringResource(R.string.gesture_relative))) { change(config.copy(mouseDragMovementMode = it)) }
|
||||
Text(stringResource(R.string.gesture_middle_relative_hint), color = p.onSurfaceVariant, style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
GestureSettingsCard(R.string.gesture_recognition) {
|
||||
Text(stringResource(R.string.gesture_threshold, config.gestureThreshold), style = MaterialTheme.typography.bodyMedium)
|
||||
Slider(config.gestureThreshold.toFloat(), { change(config.copy(gestureThreshold = it.toInt())) }, valueRange = 10f..100f, steps = 17)
|
||||
Text(stringResource(R.string.gesture_threshold_hint), color = p.onSurfaceVariant, style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
GestureSettingsCard(R.string.gesture_feedback) {
|
||||
GestureToggle(R.string.gesture_debug, config.showGestureDebugOverlay) { change(config.copy(showGestureDebugOverlay = it)) }
|
||||
}
|
||||
OutlinedButton(onClick = { change(TouchGestureConfig()) }, modifier = Modifier.fillMaxWidth()) { Text(stringResource(R.string.gesture_reset)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureDialogFrame(title: String, subtitle: String, onDismiss: () -> Unit,
|
||||
footer: @Composable () -> Unit = {}, content: @Composable ColumnScope.() -> Unit) {
|
||||
val p = LocalPalette.current
|
||||
// The app window does not opt into RTL, so these dialogs follow their resource locale explicitly.
|
||||
val direction = if (LocalConfiguration.current.layoutDirection == android.view.View.LAYOUT_DIRECTION_RTL) LayoutDirection.Rtl else LayoutDirection.Ltr
|
||||
Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false, decorFitsSystemWindows = false)) {
|
||||
CompositionLocalProvider(LocalLayoutDirection provides direction) {
|
||||
Box(Modifier.fillMaxSize().systemBarsPadding().imePadding().padding(12.dp), contentAlignment = Alignment.Center) {
|
||||
Surface(Modifier.widthIn(max = 1040.dp).fillMaxWidth().fillMaxHeight(), shape = RoundedCornerShape(22.dp), color = p.background,
|
||||
contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2)) {
|
||||
Column {
|
||||
Row(Modifier.fillMaxWidth().padding(start = 20.dp, end = 8.dp, top = 14.dp, bottom = 12.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Surface(shape = RoundedCornerShape(12.dp), color = p.primary.copy(alpha = 0.14f)) {
|
||||
Icon(Icons.Default.TouchApp, null, tint = p.primary, modifier = Modifier.padding(10.dp).size(24.dp))
|
||||
}
|
||||
Column(Modifier.weight(1f).padding(horizontal = 14.dp), verticalArrangement = Arrangement.spacedBy(3.dp)) {
|
||||
Text(title, style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.SemiBold)
|
||||
Text(subtitle, style = MaterialTheme.typography.bodySmall, color = p.onSurfaceVariant)
|
||||
}
|
||||
IconButton(onClick = onDismiss) { Icon(Icons.Default.Close, stringResource(R.string.gesture_close)) }
|
||||
}
|
||||
content()
|
||||
HorizontalDivider(color = p.line)
|
||||
footer()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureSection(@StringRes title: Int, @StringRes hint: Int) {
|
||||
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
Text(stringResource(title), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
|
||||
Text(stringResource(hint), style = MaterialTheme.typography.bodyMedium, color = LocalPalette.current.onSurfaceVariant)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureCard(@StringRes title: Int, checked: Boolean, onToggle: (Boolean) -> Unit,
|
||||
content: @Composable ColumnScope.() -> Unit) {
|
||||
val p = LocalPalette.current
|
||||
Surface(Modifier.fillMaxWidth(), shape = RoundedCornerShape(16.dp), color = p.surface,
|
||||
border = BorderStroke(1.dp, if (checked) p.line2 else p.line)) {
|
||||
Column {
|
||||
Row(Modifier.fillMaxWidth().toggleable(checked, role = Role.Switch, onValueChange = onToggle)
|
||||
.padding(horizontal = 16.dp, vertical = 10.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Column(Modifier.weight(1f).padding(end = 12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
Text(stringResource(title), style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold)
|
||||
}
|
||||
Switch(checked, onCheckedChange = null)
|
||||
}
|
||||
if (checked) Column(Modifier.padding(start = 16.dp, end = 16.dp, bottom = 16.dp), verticalArrangement = Arrangement.spacedBy(12.dp), content = content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureActionField(id: String, @StringRes gesture: Int, selected: String, choices: List<String>, onChoose: (String) -> Unit) {
|
||||
var open by remember { mutableStateOf(false) }
|
||||
val p = LocalPalette.current
|
||||
Surface(onClick = { open = true }, modifier = Modifier.fillMaxWidth().testTag("gesture-choice-$id"), shape = RoundedCornerShape(10.dp),
|
||||
color = p.surfaceVariant, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2)) {
|
||||
Row(Modifier.padding(horizontal = 14.dp, vertical = 12.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(3.dp)) {
|
||||
Text(stringResource(R.string.gesture_action_label), color = p.onSurfaceVariant, style = MaterialTheme.typography.labelSmall)
|
||||
Text(gestureActionTitle(selected), style = MaterialTheme.typography.bodyLarge, fontWeight = FontWeight.Medium)
|
||||
}
|
||||
Icon(Icons.AutoMirrored.Filled.ArrowForward, null, modifier = Modifier.size(19.dp), tint = p.onSurfaceVariant)
|
||||
}
|
||||
}
|
||||
if (open) GestureActionPicker(choices, selected, stringResource(gesture), onDismiss = { open = false }) { open = false; onChoose(it) }
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun GestureActionPicker(choices: List<String>, selected: String, gesture: String, onDismiss: () -> Unit, onChoose: (String) -> Unit) {
|
||||
var query by rememberSaveable { mutableStateOf("") }
|
||||
var category by remember { mutableStateOf<GestureActionGroup?>(null) }
|
||||
val catalog = choices.distinct().map { GestureAction(it, gestureActionTitle(it), gestureActionGroup(it)) }
|
||||
val groups = GestureActionGroup.entries.filter { group -> catalog.any { it.group == group } }
|
||||
val shown = filterGestureActions(catalog, query, category)
|
||||
val p = LocalPalette.current
|
||||
GestureDialogFrame(stringResource(R.string.gesture_action), stringResource(R.string.gesture_action_for, gesture), onDismiss) {
|
||||
OutlinedTextField(query, onValueChange = { query = it }, modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp).testTag("gesture-action-search"),
|
||||
singleLine = true, shape = RoundedCornerShape(12.dp), placeholder = { Text(stringResource(R.string.gesture_search_actions)) },
|
||||
leadingIcon = { Icon(Icons.Default.Search, null) }, trailingIcon = {
|
||||
if (query.isNotEmpty()) IconButton(onClick = { query = "" }) { Icon(Icons.Default.Close, stringResource(R.string.gesture_clear_search)) }
|
||||
})
|
||||
Row(Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()).padding(horizontal = 20.dp, vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
GestureFilterChip(category == null, onClick = { category = null }, label = { Text(stringResource(R.string.gesture_all_actions)) })
|
||||
if (groups.size > 1) groups.forEach { group ->
|
||||
GestureFilterChip(category == group, onClick = { category = group }, label = { Text(stringResource(group.title)) }, modifier = Modifier.testTag("gesture-category-${group.name}"))
|
||||
}
|
||||
}
|
||||
if (shown.isEmpty()) Box(Modifier.weight(1f).fillMaxWidth().padding(24.dp), contentAlignment = Alignment.Center) {
|
||||
Text(stringResource(R.string.gesture_no_actions), color = p.onSurfaceVariant)
|
||||
} else LazyColumn(Modifier.weight(1f).fillMaxWidth().testTag("gesture-action-list"), contentPadding = PaddingValues(start = 20.dp, end = 20.dp, bottom = 16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
groups.forEach { group ->
|
||||
val entries = shown.filter { it.group == group }
|
||||
if (entries.isNotEmpty()) {
|
||||
item(key = group.name) { Text(stringResource(group.title), style = MaterialTheme.typography.labelLarge, color = p.onSurfaceVariant, modifier = Modifier.padding(top = 12.dp, bottom = 5.dp)) }
|
||||
items(entries, key = { it.id }) { action ->
|
||||
val isSelected = action.id == selected
|
||||
Surface(shape = RoundedCornerShape(12.dp), color = if (isSelected) p.primary.copy(alpha = 0.12f) else p.surface,
|
||||
border = BorderStroke(1.dp, if (isSelected) p.primary else p.line)) {
|
||||
Row(Modifier.fillMaxWidth().selectable(isSelected, role = Role.RadioButton, onClick = { onChoose(action.id) })
|
||||
.testTag("gesture-action-${action.id}").padding(horizontal = 14.dp, vertical = 9.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(when (group) { GestureActionGroup.MOUSE -> Icons.Default.Mouse; GestureActionGroup.APP -> Icons.Default.TouchApp; else -> Icons.Default.Keyboard },
|
||||
null, tint = if (isSelected) p.primary else p.onSurfaceVariant, modifier = Modifier.size(22.dp))
|
||||
Text(action.title, Modifier.weight(1f).padding(horizontal = 14.dp), style = MaterialTheme.typography.bodyLarge)
|
||||
RadioButton(isSelected, onClick = null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureHoldOptions(action: String, behavior: String, delay: Int, onBehavior: (String) -> Unit, onDelay: (Int) -> Unit) {
|
||||
if (TouchGestureConfig.isMouseButtonAction(action)) {
|
||||
Text(stringResource(R.string.gesture_mouse_behavior), style = MaterialTheme.typography.labelMedium, color = LocalPalette.current.onSurfaceVariant)
|
||||
GestureChoiceChips(behavior, listOf(MOUSE_BEHAVIOR_HOLD to stringResource(R.string.gesture_hold_button), MOUSE_BEHAVIOR_CLICK to stringResource(R.string.gesture_click_once)), onBehavior)
|
||||
}
|
||||
GestureDelay(stringResource(R.string.gesture_hold_delay), delay, onDelay)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureFilterChip(selected: Boolean, onClick: () -> Unit, label: @Composable () -> Unit, modifier: Modifier = Modifier) {
|
||||
val p = LocalPalette.current
|
||||
FilterChip(selected, onClick, label, modifier = modifier, colors = FilterChipDefaults.filterChipColors(
|
||||
containerColor = p.surface, labelColor = p.onSurfaceVariant,
|
||||
selectedContainerColor = p.primary.copy(alpha = 0.16f), selectedLabelColor = p.onBackground))
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureChoiceChips(selected: String, options: List<Pair<String, String>>, choose: (String) -> Unit) {
|
||||
Row(Modifier.horizontalScroll(rememberScrollState()), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
options.forEach { (id, label) -> GestureFilterChip(selected == id, onClick = { choose(id) }, label = { Text(label) }) }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureSettingsCard(@StringRes title: Int, content: @Composable ColumnScope.() -> Unit) {
|
||||
val p = LocalPalette.current
|
||||
Surface(shape = RoundedCornerShape(16.dp), color = p.surface, border = BorderStroke(1.dp, p.line)) {
|
||||
Column(Modifier.fillMaxWidth().padding(16.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
Text(stringResource(title), style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold)
|
||||
content()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureToggle(@StringRes title: Int, checked: Boolean, change: (Boolean) -> Unit) {
|
||||
Row(Modifier.fillMaxWidth().toggleable(checked, role = Role.Switch, onValueChange = change).padding(vertical = 6.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(stringResource(title), Modifier.weight(1f).padding(end = 12.dp), style = MaterialTheme.typography.bodyMedium)
|
||||
Switch(checked, null)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun GestureDelay(label: String, value: Int, change: (Int) -> Unit) {
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(label, Modifier.weight(1f), style = MaterialTheme.typography.bodyMedium)
|
||||
Text(stringResource(R.string.gesture_ms, value), Modifier.background(LocalPalette.current.surfaceVariant, RoundedCornerShape(6.dp)).padding(horizontal = 10.dp, vertical = 5.dp), style = MaterialTheme.typography.labelLarge)
|
||||
}
|
||||
Slider(value.toFloat().coerceIn(100f, 1000f), { change((it / 50).toInt() * 50) }, valueRange = 100f..1000f, steps = 17)
|
||||
}
|
||||
Reference in new issue
Block a user