mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(setup): arrange tabs in one list with drag handles, a More line and a shown switch (#2627)
This commit is contained in:
1 parent
aa3a599ec6
commit
c847659789
37 files changed
+269
-269
No files matched your search
@@ -108,7 +108,7 @@ private fun HudKeyHints(grabbed: String?, resetFill: Float) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun KeyHint(key: String, label: String, fill: Float = 0f) {
|
||||
internal fun KeyHint(key: String, label: String, fill: Float = 0f) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(HINT_GAP),
|
||||
|
||||
@@ -69,23 +69,18 @@ internal object NavTabs {
|
||||
return if (more(s).isEmpty()) shown else shown + MORE
|
||||
}
|
||||
|
||||
fun move(context: Context, s: FrontEndState, key: String, step: Int) {
|
||||
val visible = listed(s)
|
||||
val at = visible.indexOf(key)
|
||||
val other = visible.getOrNull(at + step) ?: return
|
||||
val next = order.toMutableList()
|
||||
val i = next.indexOf(key)
|
||||
val j = next.indexOf(other)
|
||||
next[i] = other
|
||||
next[j] = key
|
||||
order = next
|
||||
SessionPrefs.setNavTabOrder(context, next)
|
||||
}
|
||||
|
||||
fun setPlace(context: Context, key: String, place: TabPlace) {
|
||||
shelved = if (place == TabPlace.MORE) shelved + key else shelved - key
|
||||
hidden = if (place == TabPlace.HIDDEN && hideable(key) && key !in FEATURES) hidden + key else hidden - key
|
||||
SessionPrefs.setNavTabShelved(context, shelved)
|
||||
fun setHidden(context: Context, key: String, hide: Boolean) {
|
||||
hidden = if (hide && hideable(key) && key !in FEATURES) hidden + key else hidden - key
|
||||
SessionPrefs.setNavTabHidden(context, hidden)
|
||||
}
|
||||
|
||||
fun rearrange(context: Context, s: FrontEndState, bar: List<String>, more: List<String>) {
|
||||
val placed = bar + more
|
||||
if (placed.toSet() != listed(s).toSet()) return
|
||||
val next = placed.iterator()
|
||||
order = order.map { if (it in placed) next.next() else it }
|
||||
shelved = shelved - placed.toSet() + more
|
||||
SessionPrefs.setNavTabOrder(context, order)
|
||||
SessionPrefs.setNavTabShelved(context, shelved)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.LocalIndication
|
||||
import android.view.KeyEvent
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.focusable
|
||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||
import androidx.compose.foundation.gestures.awaitFirstDown
|
||||
import androidx.compose.foundation.hoverable
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
@@ -17,34 +19,57 @@ import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.relocation.BringIntoViewRequester
|
||||
import androidx.compose.foundation.relocation.bringIntoViewRequester
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.KeyboardArrowDown
|
||||
import androidx.compose.material.icons.outlined.KeyboardArrowUp
|
||||
import androidx.compose.material.icons.filled.DragHandle
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.key
|
||||
import androidx.compose.runtime.mutableFloatStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.focus.focusProperties
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.input.InputMode
|
||||
import androidx.compose.ui.input.key.onPreviewKeyEvent
|
||||
import androidx.compose.ui.input.pointer.PointerEventPass
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.LocalInputModeManager
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.zIndex
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.agent.AgentTab
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
private const val MORE_LINE = "|more|"
|
||||
private val TAB_ROW = 52.dp
|
||||
private val TAB_HANDLE = 44.dp
|
||||
|
||||
internal fun tabRows(bar: List<String>, more: List<String>) = bar + MORE_LINE + more
|
||||
|
||||
internal fun moveTabRow(rows: List<String>, key: String, step: Int): List<String>? {
|
||||
val from = rows.indexOf(key)
|
||||
val to = from + step
|
||||
if (from < 0 || to !in rows.indices) return null
|
||||
return rows.toMutableList().apply { add(to, removeAt(from)) }
|
||||
}
|
||||
|
||||
@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
@@ -57,86 +82,188 @@ internal fun TabsSettingsGroup(host: MenuHost, s: FrontEndState, a: FrontEndActi
|
||||
androidx.compose.runtime.withFrameNanos { }
|
||||
reveal.bringIntoView()
|
||||
}
|
||||
val shown = stringResource(R.string.tabs_place_bar)
|
||||
val more = stringResource(R.string.tabs_place_more)
|
||||
val hidden = stringResource(R.string.tabs_place_hidden)
|
||||
val keys = NavTabs.listed(s)
|
||||
fun pick(key: String, place: TabPlace) {
|
||||
when (key) {
|
||||
"stores" -> if ((place != TabPlace.HIDDEN) != s.gameStoresEnabled) a.onGameStoresEnabled(place != TabPlace.HIDDEN)
|
||||
"soundtracks" -> if ((place != TabPlace.HIDDEN) != s.soundtracksEnabled) a.onSoundtracksEnabled(place != TabPlace.HIDDEN)
|
||||
"agent" -> if ((place != TabPlace.HIDDEN) != AgentTab.shown) AgentTab.set(ctx, place != TabPlace.HIDDEN)
|
||||
}
|
||||
NavTabs.setPlace(ctx, key, place)
|
||||
val pad = LocalInputModeManager.current.inputMode == InputMode.Keyboard
|
||||
val state by rememberUpdatedState(s)
|
||||
val rows = {
|
||||
val keys = NavTabs.listed(state)
|
||||
tabRows(keys.filter { it !in NavTabs.shelved }, keys.filter { it in NavTabs.shelved })
|
||||
}
|
||||
val commit = { next: List<String> ->
|
||||
val split = next.indexOf(MORE_LINE)
|
||||
NavTabs.rearrange(ctx, state, next.subList(0, split), next.subList(split + 1, next.size))
|
||||
}
|
||||
fun setShown(key: String, show: Boolean) {
|
||||
when (key) {
|
||||
"stores" -> if (show != s.gameStoresEnabled) a.onGameStoresEnabled(show)
|
||||
"soundtracks" -> if (show != s.soundtracksEnabled) a.onSoundtracksEnabled(show)
|
||||
"agent" -> if (show != AgentTab.shown) AgentTab.set(ctx, show)
|
||||
}
|
||||
NavTabs.setHidden(ctx, key, !show)
|
||||
}
|
||||
|
||||
var dragging by remember { mutableStateOf<String?>(null) }
|
||||
var dragOffset by remember { mutableFloatStateOf(0f) }
|
||||
var grabbed by remember { mutableStateOf<String?>(null) }
|
||||
var before by remember { mutableStateOf<List<String>>(emptyList()) }
|
||||
var focused by remember { mutableStateOf<String?>(null) }
|
||||
val rowPx = with(LocalDensity.current) { TAB_ROW.toPx() }
|
||||
val handlePx = with(LocalDensity.current) { TAB_HANDLE.toPx() }
|
||||
val list = rows()
|
||||
|
||||
Box(Modifier.bringIntoViewRequester(reveal)) {
|
||||
SettingsGroup(stringResource(R.string.setup_group_tabs)) {
|
||||
keys.forEachIndexed { index, tab ->
|
||||
key(tab) {
|
||||
val options = buildList {
|
||||
add(TabPlace.BAR to shown)
|
||||
add(TabPlace.MORE to more)
|
||||
if (NavTabs.hideable(tab)) add(TabPlace.HIDDEN to hidden)
|
||||
Column(
|
||||
Modifier.fillMaxWidth().pointerInput(Unit) {
|
||||
awaitEachGesture {
|
||||
val down = awaitFirstDown(requireUnconsumed = false)
|
||||
val start = (down.position.y / rowPx).toInt()
|
||||
val key = rows().getOrNull(start)
|
||||
if (key == null || key == MORE_LINE || down.position.x > handlePx || grabbed != null) return@awaitEachGesture
|
||||
down.consume()
|
||||
dragging = key
|
||||
dragOffset = 0f
|
||||
while (true) {
|
||||
val change = awaitPointerEvent(PointerEventPass.Initial).changes.firstOrNull { it.id == down.id } ?: break
|
||||
change.consume()
|
||||
if (!change.pressed) break
|
||||
val travel = change.position.y - down.position.y
|
||||
val current = rows()
|
||||
val target = (start + (travel / rowPx).roundToInt()).coerceIn(0, current.lastIndex)
|
||||
var at = current.indexOf(key)
|
||||
var next = current
|
||||
while (target != at) {
|
||||
next = moveTabRow(next, key, if (target > at) 1 else -1) ?: break
|
||||
at = next.indexOf(key)
|
||||
}
|
||||
if (next !== current) commit(next)
|
||||
dragOffset = travel - (at - start) * rowPx
|
||||
}
|
||||
dragging = null
|
||||
dragOffset = 0f
|
||||
}
|
||||
},
|
||||
) {
|
||||
list.forEachIndexed { index, tab ->
|
||||
key(tab) {
|
||||
if (tab == MORE_LINE) {
|
||||
MoreLine()
|
||||
} else {
|
||||
val shown = NavTabs.place(s, tab) != TabPlace.HIDDEN
|
||||
val held = tab == dragging || tab == grabbed
|
||||
val bring = remember { BringIntoViewRequester() }
|
||||
LaunchedEffect(index, grabbed) { if (grabbed == tab) bring.bringIntoView() }
|
||||
TabRow(
|
||||
tab, shown, NavTabs.hideable(tab), held,
|
||||
offset = if (tab == dragging) dragOffset else 0f,
|
||||
hint = if (pad && focused == tab) stringResource(if (grabbed == tab) R.string.hud_hint_drop else R.string.hud_hint_grab) else null,
|
||||
modifier = Modifier.bringIntoViewRequester(bring)
|
||||
.onPreviewKeyEvent { event ->
|
||||
val e = event.nativeKeyEvent
|
||||
val down = e.action == KeyEvent.ACTION_DOWN
|
||||
val first = down && e.repeatCount == 0
|
||||
val mine = grabbed == tab
|
||||
when (e.keyCode) {
|
||||
KeyEvent.KEYCODE_BUTTON_Y -> {
|
||||
if (first) {
|
||||
if (mine) grabbed = null
|
||||
else if (grabbed == null) { before = rows(); grabbed = tab }
|
||||
}
|
||||
true
|
||||
}
|
||||
KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN -> {
|
||||
if (mine && down) moveTabRow(rows(), tab, if (e.keyCode == KeyEvent.KEYCODE_DPAD_UP) -1 else 1)?.let(commit)
|
||||
mine
|
||||
}
|
||||
KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT -> mine
|
||||
KeyEvent.KEYCODE_BUTTON_B, KeyEvent.KEYCODE_BACK -> {
|
||||
if (mine && first) { commit(before); grabbed = null }
|
||||
mine
|
||||
}
|
||||
KeyEvent.KEYCODE_BUTTON_A, KeyEvent.KEYCODE_ENTER, KeyEvent.KEYCODE_SPACE -> {
|
||||
if (first) {
|
||||
if (mine) grabbed = null
|
||||
else if (NavTabs.hideable(tab)) setShown(tab, !shown)
|
||||
}
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
},
|
||||
onFocus = { if (it) focused = tab else if (focused == tab) { focused = null; if (grabbed == tab) grabbed = null } },
|
||||
) { host.open = null; setShown(tab, it) }
|
||||
}
|
||||
}
|
||||
TabRow(
|
||||
host, tab, NavTabs.place(s, tab), options,
|
||||
canUp = index > 0, canDown = index < keys.lastIndex,
|
||||
onMove = { step -> NavTabs.move(ctx, s, tab, step) },
|
||||
onPick = { pick(tab, it) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun MoreLine() {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
Column(Modifier.fillMaxWidth().height(TAB_ROW).testTag("tab-more-line")) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||
modifier = Modifier.fillMaxWidth().weight(1f).background(colors.surfaceVariant.copy(alpha = 0.35f)).padding(horizontal = 14.dp),
|
||||
) {
|
||||
Icon(railLabelIcon(NavTabs.MORE).second, null, tint = colors.onSurfaceVariant, modifier = Modifier.size(18.dp))
|
||||
Text(
|
||||
stringResource(R.string.rail_more).uppercase(), fontSize = 12.sp, fontWeight = FontWeight.SemiBold,
|
||||
letterSpacing = 1.5.sp, color = colors.onSurfaceVariant,
|
||||
)
|
||||
Box(Modifier.weight(1f).height(1.dp).background(pal.line2))
|
||||
}
|
||||
Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line))
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TabRow(
|
||||
host: MenuHost, tab: String, place: TabPlace, options: List<Pair<TabPlace, String>>,
|
||||
canUp: Boolean, canDown: Boolean, onMove: (Int) -> Unit, onPick: (TabPlace) -> Unit,
|
||||
tab: String, shown: Boolean, hideable: Boolean, held: Boolean, offset: Float, hint: String?,
|
||||
modifier: Modifier, onFocus: (Boolean) -> Unit, onShown: (Boolean) -> Unit,
|
||||
) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val (labelRes, icon) = railLabelIcon(tab)
|
||||
val label = stringResource(labelRes)
|
||||
val open = host.open == "tab-$tab"
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
.background(if (open) pal.signal.copy(alpha = 0.10f) else androidx.compose.ui.graphics.Color.Transparent)
|
||||
.padding(start = 14.dp, end = 10.dp, top = 6.dp, bottom = 6.dp),
|
||||
val src = remember { MutableInteractionSource() }
|
||||
val hot = rememberHot(src)
|
||||
Column(
|
||||
modifier.fillMaxWidth().height(TAB_ROW).testTag("tab-row-$tab")
|
||||
.zIndex(if (held) 1f else 0f)
|
||||
.graphicsLayer { translationY = offset }
|
||||
.background(if (held) colors.surfaceVariant else colors.surface)
|
||||
.glideBorder(hot, Shape12, pal.signal)
|
||||
.onFocusChanged { onFocus(it.isFocused) }
|
||||
.hoverable(src)
|
||||
.focusable(interactionSource = src),
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.weight(1f).alpha(if (place == TabPlace.HIDDEN) 0.5f else 1f),
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
modifier = Modifier.fillMaxWidth().weight(1f).padding(end = 10.dp),
|
||||
) {
|
||||
Icon(icon, contentDescription = null, tint = colors.onSurfaceVariant, modifier = Modifier.size(20.dp))
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Text(label, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
Box(contentAlignment = Alignment.Center, modifier = Modifier.size(width = TAB_HANDLE, height = TAB_ROW).testTag("tab-handle-$tab")) {
|
||||
Icon(Icons.Filled.DragHandle, null, tint = if (held) pal.signal else colors.onSurfaceVariant, modifier = Modifier.size(20.dp))
|
||||
}
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.weight(1f).alpha(if (shown) 1f else 0.5f),
|
||||
) {
|
||||
Icon(icon, contentDescription = null, tint = colors.onSurfaceVariant, modifier = Modifier.size(20.dp))
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Text(label, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
}
|
||||
if (hint != null) KeyHint("Y", hint)
|
||||
ToggleSwitch(
|
||||
shown, hideable, label,
|
||||
Modifier.testTag("tab-shown-$tab").focusProperties { canFocus = false },
|
||||
onChange = onShown,
|
||||
)
|
||||
}
|
||||
MoveButton(Icons.Outlined.KeyboardArrowUp, stringResource(R.string.tabs_move_up, label), canUp, "tab-up-$tab") { onMove(-1) }
|
||||
MoveButton(Icons.Outlined.KeyboardArrowDown, stringResource(R.string.tabs_move_down, label), canDown, "tab-down-$tab") { onMove(1) }
|
||||
ChoiceChip(host, "tab-$tab", label, options, place, onPick = onPick)
|
||||
Box(Modifier.fillMaxWidth().height(1.dp).background(if (held) Color.Transparent else pal.line))
|
||||
}
|
||||
Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line))
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun MoveButton(icon: ImageVector, description: String, enabled: Boolean, tag: String, onClick: () -> Unit) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val src = remember { MutableInteractionSource() }
|
||||
val hot = rememberHot(src)
|
||||
Box(
|
||||
contentAlignment = Alignment.Center,
|
||||
modifier = Modifier.testTag(tag).size(40.dp).clip(CircleShape)
|
||||
.background(colors.surfaceVariant)
|
||||
.glideBorder(hot, CircleShape, pal.signal, pal.line2)
|
||||
.alpha(if (enabled) 1f else 0.35f)
|
||||
.hoverable(src)
|
||||
.clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button) { if (enabled) onClick() }
|
||||
.controllerConfirm { if (enabled) onClick() }
|
||||
.semantics { contentDescription = description },
|
||||
) { Icon(icon, contentDescription = null, tint = if (hot) pal.signal else colors.onBackground, modifier = Modifier.size(20.dp)) }
|
||||
}
|
||||
Reference in new issue
Block a user