mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 22:05:59 +00:00
feat: on-screen keys for any key or key combo (#1305)
This commit is contained in:
1 parent
6e67dbb5be
commit
9f4794769c
48 files changed
+3169
-47
No files matched your search
@@ -1,7 +1,9 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import com.droiddeck.launcher.R
|
||||
import androidx.annotation.StringRes
|
||||
import com.droiddeck.launcher.input.PC_KEY_ROWS
|
||||
import com.droiddeck.launcher.input.PC_MODIFIERS
|
||||
import com.droiddeck.launcher.input.PcKey
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import android.view.KeyEvent
|
||||
import androidx.compose.animation.animateColorAsState
|
||||
@@ -54,46 +56,6 @@ import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
private data class PcKey(val label: String, val code: Int, val weight: Float = 1f, val shifted: String? = null, @StringRes val labelRes: Int = 0)
|
||||
|
||||
private fun k(label: String, code: Int, weight: Float = 1f, shifted: String? = null) = PcKey(label, code, weight, shifted)
|
||||
|
||||
private fun k(@StringRes labelRes: Int, code: Int, weight: Float = 1f) = PcKey("", code, weight, null, labelRes)
|
||||
|
||||
private val ROWS: List<List<PcKey>> = listOf(
|
||||
listOf(
|
||||
k(R.string.pckb_key_esc, 1), k("F1", 59), k("F2", 60), k("F3", 61), k("F4", 62), k("F5", 63), k("F6", 64),
|
||||
k("F7", 65), k("F8", 66), k("F9", 67), k("F10", 68), k("F11", 87), k("F12", 88),
|
||||
k(R.string.pckb_key_ins, 110), k(R.string.pckb_key_del, 111), k(R.string.pckb_key_home, 102), k(R.string.pckb_key_end, 107), k(R.string.pckb_key_pgup, 104), k(R.string.pckb_key_pgdn, 109),
|
||||
),
|
||||
listOf(
|
||||
k("`", 41, shifted = "~"), k("1", 2, shifted = "!"), k("2", 3, shifted = "@"), k("3", 4, shifted = "#"),
|
||||
k("4", 5, shifted = "$"), k("5", 6, shifted = "%"), k("6", 7, shifted = "^"), k("7", 8, shifted = "&"),
|
||||
k("8", 9, shifted = "*"), k("9", 10, shifted = "("), k("0", 11, shifted = ")"), k("-", 12, shifted = "_"),
|
||||
k("=", 13, shifted = "+"), k(R.string.pckb_key_backspace, 14, 2f),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_tab, 15, 1.5f), k("q", 16), k("w", 17), k("e", 18), k("r", 19), k("t", 20), k("y", 21),
|
||||
k("u", 22), k("i", 23), k("o", 24), k("p", 25), k("[", 26, shifted = "{"), k("]", 27, shifted = "}"),
|
||||
k("\\", 43, 1.5f, shifted = "|"),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_caps, 58, 1.75f), k("a", 30), k("s", 31), k("d", 32), k("f", 33), k("g", 34), k("h", 35),
|
||||
k("j", 36), k("k", 37), k("l", 38), k(";", 39, shifted = ":"), k("'", 40, shifted = "\""),
|
||||
k(R.string.pckb_key_enter, 28, 2.25f),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_shift, 42, 2.25f), k("z", 44), k("x", 45), k("c", 46), k("v", 47), k("b", 48), k("n", 49),
|
||||
k("m", 50), k(",", 51, shifted = "<"), k(".", 52, shifted = ">"), k("/", 53, shifted = "?"),
|
||||
k(R.string.pckb_key_shift, 54, 1.75f), k("↑", 103),
|
||||
),
|
||||
listOf(
|
||||
k(R.string.pckb_key_ctrl, 29, 1.5f), k(R.string.pckb_key_super, 125, 1.25f), k(R.string.pckb_key_alt, 56, 1.25f), k(R.string.pckb_key_space, 57, 6.25f),
|
||||
k(R.string.pckb_key_altgr, 100, 1.25f), k(R.string.pckb_key_ctrl, 97, 1.25f), k("←", 105), k("↓", 108), k("→", 106),
|
||||
),
|
||||
)
|
||||
|
||||
private val MODIFIERS = setOf(42, 54, 29, 97, 56, 100, 125)
|
||||
private const val MOD_OFF = 0
|
||||
private const val MOD_ONCE = 1
|
||||
private const val MOD_LOCKED = 2
|
||||
@@ -115,7 +77,7 @@ fun PcKeyboard(
|
||||
for ((code, state) in mods.toMap()) if (state == MOD_ONCE) { sendKey(code, false); mods[code] = MOD_OFF }
|
||||
}
|
||||
fun press(key: PcKey) {
|
||||
if (key.code in MODIFIERS) {
|
||||
if (key.code in PC_MODIFIERS) {
|
||||
when (mods[key.code] ?: MOD_OFF) {
|
||||
MOD_OFF -> { sendKey(key.code, true); mods[key.code] = MOD_ONCE }
|
||||
MOD_ONCE -> mods[key.code] = MOD_LOCKED
|
||||
@@ -126,7 +88,7 @@ fun PcKeyboard(
|
||||
sendKey(key.code, true)
|
||||
}
|
||||
fun release(key: PcKey) {
|
||||
if (key.code in MODIFIERS) return
|
||||
if (key.code in PC_MODIFIERS) return
|
||||
sendKey(key.code, false)
|
||||
if (key.code == 58) capsOn = !capsOn
|
||||
releaseOnceModifiers()
|
||||
@@ -153,7 +115,7 @@ fun PcKeyboard(
|
||||
Spacer(Modifier.width(8.dp))
|
||||
HeaderButton(stringResource(R.string.pckb_hide), onClose)
|
||||
}
|
||||
ROWS.forEachIndexed { r, row ->
|
||||
PC_KEY_ROWS.forEachIndexed { r, row ->
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(3.dp), modifier = Modifier.fillMaxWidth().height(rowHeight)) {
|
||||
row.forEachIndexed { i, key ->
|
||||
val state = mods[key.code] ?: MOD_OFF
|
||||
|
||||
@@ -0,0 +1,368 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.gestures.detectDragGestures
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.offset
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.DragIndicator
|
||||
import androidx.compose.material.icons.filled.Image
|
||||
import androidx.compose.material.icons.filled.Keyboard
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Slider
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_CIRCLE
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_DPAD
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_PILL
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_RECT
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_ROUND
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_STICK
|
||||
import com.droiddeck.launcher.input.SCREEN_KEY_TRIGGER
|
||||
import com.droiddeck.launcher.input.ScreenKeys
|
||||
import com.droiddeck.launcher.input.pcKeyLabel
|
||||
import com.droiddeck.launcher.input.screenKeyDirections
|
||||
import java.io.File
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
private val PANEL_WIDTH = 600.dp
|
||||
private val CHIP_PADDING = androidx.compose.foundation.layout.PaddingValues(horizontal = 6.dp, vertical = 2.dp)
|
||||
|
||||
@Composable
|
||||
fun ScreenKeyEditor(
|
||||
codes: List<Int>,
|
||||
keyY: Float,
|
||||
size: Int,
|
||||
opacity: Float,
|
||||
shape: Int,
|
||||
toggle: Boolean,
|
||||
labels: Boolean,
|
||||
text: String,
|
||||
hasImage: Boolean,
|
||||
onResize: (Int) -> Unit,
|
||||
onOpacity: (Float) -> Unit,
|
||||
onShape: (Int) -> Unit,
|
||||
onToggle: (Boolean) -> Unit,
|
||||
onLabels: (Boolean) -> Unit,
|
||||
onText: (String) -> Unit,
|
||||
onImage: (String) -> Unit,
|
||||
onClearImage: () -> Unit,
|
||||
onBind: () -> Unit,
|
||||
onTextFocus: () -> Unit,
|
||||
onTextBlur: () -> Unit,
|
||||
onDirection: (Int) -> Unit,
|
||||
onRemove: () -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
val p = LocalPalette.current
|
||||
val context = LocalContext.current
|
||||
var offset by remember(codes) { mutableStateOf(Offset.Zero) }
|
||||
val density = LocalDensity.current
|
||||
val screen = LocalConfiguration.current
|
||||
val panelWidthPx = with(density) { PANEL_WIDTH.toPx() }
|
||||
val screenWidthPx = screen.screenWidthDp * density.density
|
||||
val screenHeightPx = screen.screenHeightDp * density.density
|
||||
val atTop = keyY > 0.45f
|
||||
val button = shape <= SCREEN_KEY_RECT
|
||||
val directions = screenKeyDirections(shape)
|
||||
val pickImage = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri != null) importImage(context, uri, codes)?.let(onImage)
|
||||
}
|
||||
Box(
|
||||
Modifier.fillMaxSize().systemBarsPadding().padding(horizontal = 10.dp, vertical = 6.dp),
|
||||
contentAlignment = if (atTop) Alignment.TopCenter else Alignment.BottomCenter,
|
||||
) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = PANEL_WIDTH).fillMaxWidth()
|
||||
.offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) },
|
||||
shape = RoundedCornerShape(18.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column(Modifier.padding(start = 10.dp, end = 6.dp, top = 2.dp, bottom = 6.dp)) {
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
Icons.Default.DragIndicator, null,
|
||||
tint = p.onBackground.copy(alpha = 0.45f),
|
||||
modifier = Modifier.pointerInput(panelWidthPx, screenWidthPx, screenHeightPx) {
|
||||
detectDragGestures { change, drag ->
|
||||
change.consume()
|
||||
offset = Offset(
|
||||
(offset.x + drag.x).coerceIn(
|
||||
-(screenWidthPx - panelWidthPx) / 2f + 8f,
|
||||
(screenWidthPx - panelWidthPx) / 2f - 8f,
|
||||
),
|
||||
(offset.y + drag.y).coerceIn(
|
||||
-screenHeightPx * if (atTop) 0f else 0.75f,
|
||||
screenHeightPx * if (atTop) 0.75f else 0f,
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
Box(
|
||||
Modifier.weight(1f).height(36.dp)
|
||||
.background(p.line2.copy(alpha = 0.24f), RoundedCornerShape(9.dp))
|
||||
.padding(horizontal = 10.dp),
|
||||
contentAlignment = Alignment.CenterStart,
|
||||
) {
|
||||
BasicTextField(
|
||||
value = text,
|
||||
onValueChange = { onText(it.take(24)) },
|
||||
singleLine = true,
|
||||
textStyle = TextStyle(
|
||||
color = p.onBackground, fontSize = 13.sp,
|
||||
fontWeight = FontWeight.Medium,
|
||||
),
|
||||
cursorBrush = androidx.compose.ui.graphics.SolidColor(p.primary),
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
|
||||
modifier = Modifier.fillMaxWidth().onFocusChanged {
|
||||
if (it.isFocused) onTextFocus() else onTextBlur()
|
||||
},
|
||||
)
|
||||
if (text.isEmpty()) {
|
||||
Text(
|
||||
stringResource(R.string.screen_key_text),
|
||||
color = p.onBackground.copy(alpha = 0.4f),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
}
|
||||
}
|
||||
if (button) {
|
||||
IconButton(onClick = onBind, modifier = Modifier.width(38.dp)) {
|
||||
Icon(
|
||||
Icons.Default.Keyboard, stringResource(R.string.screen_key_bind),
|
||||
tint = p.onBackground.copy(alpha = 0.8f),
|
||||
)
|
||||
}
|
||||
}
|
||||
IconButton(onClick = { pickImage.launch("image/*") }, modifier = Modifier.width(38.dp)) {
|
||||
Icon(
|
||||
Icons.Default.Image, stringResource(R.string.screen_key_image),
|
||||
tint = p.onBackground.copy(alpha = 0.8f),
|
||||
)
|
||||
}
|
||||
if (hasImage) IconButton(onClick = onClearImage, modifier = Modifier.width(34.dp)) {
|
||||
Icon(Icons.Default.Close, stringResource(R.string.screen_key_image_clear), tint = p.error)
|
||||
}
|
||||
IconButton(onClick = onRemove, modifier = Modifier.width(38.dp)) {
|
||||
Icon(Icons.Default.Delete, stringResource(R.string.common_delete), tint = p.error)
|
||||
}
|
||||
IconButton(onClick = onDismiss, modifier = Modifier.width(34.dp)) {
|
||||
Icon(Icons.Default.Close, null, tint = p.onBackground)
|
||||
}
|
||||
}
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(vertical = 2.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
listOf<Pair<Int?, Int>>(
|
||||
(if (button) SCREEN_KEY_PILL else null) to R.string.screen_key_type_button,
|
||||
SCREEN_KEY_TRIGGER to R.string.screen_key_shape_trigger,
|
||||
SCREEN_KEY_DPAD to R.string.touch_dpad,
|
||||
SCREEN_KEY_STICK to R.string.deck_surface_stick,
|
||||
).forEach { (value, label) ->
|
||||
val selected = if (value == null) button else shape == value
|
||||
Chip(stringResource(label), selected, Modifier.weight(1f)) {
|
||||
onShape(if (value == null) (if (button) shape else SCREEN_KEY_PILL) else value)
|
||||
}
|
||||
}
|
||||
Box(Modifier.width(1.dp).height(20.dp).background(p.line2))
|
||||
listOf(
|
||||
SCREEN_KEY_PILL to R.string.screen_key_shape_pill,
|
||||
SCREEN_KEY_ROUND to R.string.screen_key_shape_round,
|
||||
SCREEN_KEY_CIRCLE to R.string.screen_key_shape_circle,
|
||||
SCREEN_KEY_RECT to R.string.screen_key_shape_rect,
|
||||
).forEach { (value, label) ->
|
||||
Chip(
|
||||
stringResource(label), button && shape == value, Modifier.weight(1f), enabled = button,
|
||||
) { onShape(value) }
|
||||
}
|
||||
Box(Modifier.width(1.dp).height(20.dp).background(p.line2))
|
||||
Text(
|
||||
stringResource(R.string.screen_key_toggle), Modifier.padding(start = 6.dp, end = 2.dp),
|
||||
style = MaterialTheme.typography.bodySmall, maxLines = 1,
|
||||
)
|
||||
Switch(checked = toggle, onCheckedChange = onToggle, modifier = Modifier.height(30.dp))
|
||||
}
|
||||
if (directions.isNotEmpty()) {
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(vertical = 2.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
directions.forEachIndexed { index, arrow ->
|
||||
val code = codes.getOrNull(index) ?: 0
|
||||
Chip(
|
||||
if (code == 0) arrow else "$arrow ${pcKeyLabel(context, code)}",
|
||||
code != 0, Modifier.weight(1f),
|
||||
) { onDirection(index) }
|
||||
}
|
||||
Text(
|
||||
stringResource(R.string.screen_key_text), Modifier.padding(start = 6.dp, end = 2.dp),
|
||||
style = MaterialTheme.typography.bodySmall, maxLines = 1,
|
||||
)
|
||||
Switch(checked = labels, onCheckedChange = onLabels, modifier = Modifier.height(30.dp))
|
||||
}
|
||||
}
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(top = 2.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
KeySlider(
|
||||
label = stringResource(R.string.screen_key_size),
|
||||
value = "$size",
|
||||
current = size.toFloat(),
|
||||
min = ScreenKeys.MIN_SIZE.toFloat(),
|
||||
max = ScreenKeys.MAX_SIZE.toFloat(),
|
||||
modifier = Modifier.weight(1f),
|
||||
) { onResize(it.roundToInt()) }
|
||||
KeySlider(
|
||||
label = stringResource(R.string.screen_key_opacity),
|
||||
value = "${(opacity * 100).roundToInt()}%",
|
||||
current = opacity,
|
||||
min = 0.2f,
|
||||
max = 1f,
|
||||
modifier = Modifier.weight(1f),
|
||||
onChange = onOpacity,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun KeySlider(
|
||||
label: String,
|
||||
value: String,
|
||||
current: Float,
|
||||
min: Float,
|
||||
max: Float,
|
||||
modifier: Modifier,
|
||||
onChange: (Float) -> Unit,
|
||||
) {
|
||||
Row(modifier, verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(label, style = MaterialTheme.typography.bodySmall, maxLines = 1)
|
||||
Slider(current, onChange, Modifier.weight(1f).height(28.dp), valueRange = min..max)
|
||||
Text(
|
||||
value, Modifier.width(32.dp), style = MaterialTheme.typography.bodySmall,
|
||||
fontWeight = FontWeight.SemiBold, maxLines = 1,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Chip(
|
||||
label: String,
|
||||
selected: Boolean,
|
||||
modifier: Modifier = Modifier,
|
||||
enabled: Boolean = true,
|
||||
onClick: () -> Unit,
|
||||
) {
|
||||
val p = LocalPalette.current
|
||||
Box(
|
||||
modifier
|
||||
.background(
|
||||
if (selected) p.primary.copy(alpha = 0.18f) else p.line2.copy(alpha = 0.28f),
|
||||
CircleShape,
|
||||
)
|
||||
.clickable(enabled = enabled, onClick = onClick)
|
||||
.padding(vertical = 6.dp),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text(
|
||||
label, style = MaterialTheme.typography.labelSmall, maxLines = 1, overflow = TextOverflow.Ellipsis,
|
||||
color = when {
|
||||
!enabled -> p.onBackground.copy(alpha = 0.28f)
|
||||
selected -> p.primary
|
||||
else -> p.onBackground.copy(alpha = 0.7f)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun importImage(context: Context, uri: Uri, codes: List<Int>): String? {
|
||||
val dir = File(context.filesDir, ScreenKeys.IMAGE_DIR).apply { mkdirs() }
|
||||
val name = "key_" + codes.joinToString("_") + "_" + System.currentTimeMillis() + ".png"
|
||||
val longest = (ScreenKeys.MAX_SIZE * context.resources.displayMetrics.density).toInt()
|
||||
return runCatching {
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
context.contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, bounds) }
|
||||
val source = maxOf(bounds.outWidth, bounds.outHeight)
|
||||
if (source <= 0) return@runCatching null
|
||||
var sample = 1
|
||||
while (source / (sample * 2) >= longest) sample *= 2
|
||||
val bitmap = context.contentResolver.openInputStream(uri)?.use {
|
||||
BitmapFactory.decodeStream(it, null, BitmapFactory.Options().apply { inSampleSize = sample })
|
||||
} ?: return@runCatching null
|
||||
val scale = longest.toFloat() / maxOf(bitmap.width, bitmap.height)
|
||||
val scaled = if (scale < 1f) {
|
||||
Bitmap.createScaledBitmap(
|
||||
bitmap,
|
||||
(bitmap.width * scale).toInt().coerceAtLeast(1),
|
||||
(bitmap.height * scale).toInt().coerceAtLeast(1),
|
||||
true,
|
||||
)
|
||||
} else {
|
||||
bitmap
|
||||
}
|
||||
try {
|
||||
val saved = File(dir, name).outputStream().use { scaled.compress(Bitmap.CompressFormat.PNG, 100, it) }
|
||||
if (saved) name else null
|
||||
} finally {
|
||||
if (scaled !== bitmap) bitmap.recycle()
|
||||
scaled.recycle()
|
||||
}
|
||||
}.getOrNull().also { if (it == null) File(dir, name).delete() }
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
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.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.input.PC_KEYS
|
||||
import com.droiddeck.launcher.input.PC_KEY_ROWS
|
||||
import com.droiddeck.launcher.input.PC_MODIFIERS
|
||||
import com.droiddeck.launcher.input.PcKey
|
||||
import com.droiddeck.launcher.input.pcKeyLabel
|
||||
|
||||
@Composable
|
||||
fun ScreenKeyPicker(
|
||||
onDismiss: () -> Unit,
|
||||
onAdd: (List<Int>) -> Unit,
|
||||
initial: List<Int> = emptyList(),
|
||||
sequence: Boolean = false,
|
||||
sequenceLimit: Int = 4,
|
||||
slots: List<String> = emptyList(),
|
||||
) {
|
||||
var picked by remember { mutableStateOf(initial) }
|
||||
val p = LocalPalette.current
|
||||
val toggle: (Int) -> Unit = { code ->
|
||||
picked = if (sequence) {
|
||||
if (code in picked) picked - code else (picked + code).take(sequenceLimit)
|
||||
} else if (code in PC_MODIFIERS) {
|
||||
if (code in picked) picked - code else picked + code
|
||||
} else {
|
||||
picked.filter { it in PC_MODIFIERS } + code
|
||||
}
|
||||
}
|
||||
val rows = remember {
|
||||
val functionRow = PC_KEY_ROWS[0]
|
||||
val nav = listOfNotNull(PC_KEYS[110], PC_KEYS[111], PC_KEYS[102], PC_KEYS[107], PC_KEYS[104], PC_KEYS[109]) +
|
||||
listOfNotNull(PC_KEYS[103], PC_KEYS[105], PC_KEYS[108], PC_KEYS[106])
|
||||
listOf(functionRow.take(13), nav) + PC_KEY_ROWS.drop(1)
|
||||
}
|
||||
val unit = remember { rows.maxOf { row -> row.fold(0f) { sum, key -> sum + key.weight } } }
|
||||
Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) {
|
||||
Box(Modifier.fillMaxSize().systemBarsPadding().padding(10.dp), contentAlignment = Alignment.Center) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = 820.dp).fillMaxWidth().fillMaxHeight(), shape = RoundedCornerShape(20.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column {
|
||||
Row(Modifier.fillMaxWidth().padding(start = 16.dp, end = 6.dp), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(stringResource(R.string.screen_keys_add), Modifier.weight(1f), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
|
||||
IconButton(onClick = onDismiss) { Icon(Icons.Default.Close, null) }
|
||||
}
|
||||
Row(
|
||||
Modifier.fillMaxWidth().heightIn(min = 26.dp).horizontalScroll(rememberScrollState()).padding(horizontal = 16.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
if (sequence && slots.size >= sequenceLimit) {
|
||||
slots.take(sequenceLimit).forEachIndexed { index, arrow ->
|
||||
val code = picked.getOrNull(index)
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
color = if (code == null) p.line2.copy(alpha = 0.2f) else p.primary.copy(alpha = 0.16f),
|
||||
modifier = if (code == null) Modifier else Modifier.clickable { picked = picked - code },
|
||||
) {
|
||||
Text(
|
||||
if (code == null) arrow else "$arrow ${pcKeyLabel(LocalContext.current, code)}",
|
||||
Modifier.padding(horizontal = 8.dp, vertical = 2.dp),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
picked.forEach { code ->
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp), color = p.primary.copy(alpha = 0.16f),
|
||||
modifier = Modifier.clickable { picked = picked - code },
|
||||
) {
|
||||
Text(pcKeyLabel(LocalContext.current, code), Modifier.padding(horizontal = 8.dp, vertical = 2.dp), style = MaterialTheme.typography.labelMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
LazyColumn(Modifier.weight(1f).fillMaxWidth(), contentPadding = PaddingValues(vertical = 6.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
items(rows.size) { index ->
|
||||
val row = rows[index]
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp),
|
||||
) {
|
||||
row.forEach { key -> KeyCell(key, key.code in picked, Modifier.weight(key.weight)) { toggle(key.code) } }
|
||||
if (row.sumOf { it.weight.toDouble() } < unit) {
|
||||
Spacer(Modifier.weight(unit - row.fold(0f) { sum, key -> sum + key.weight }))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Button(
|
||||
onClick = { onAdd(picked) },
|
||||
enabled = picked.isNotEmpty(),
|
||||
modifier = Modifier.weight(1f),
|
||||
) {
|
||||
Text(stringResource(R.string.screen_keys_add))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun KeyCell(key: PcKey, selected: Boolean, modifier: Modifier, onClick: () -> Unit) {
|
||||
val p = LocalPalette.current
|
||||
val label = pcKeyLabel(LocalContext.current, key.code)
|
||||
Surface(
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
color = if (selected) p.primary.copy(alpha = 0.2f) else p.surface,
|
||||
border = BorderStroke(1.dp, if (selected) p.primary else p.line),
|
||||
modifier = modifier.height(36.dp).clickable(onClick = onClick),
|
||||
) {
|
||||
Box(Modifier.fillMaxSize().padding(horizontal = 2.dp), contentAlignment = Alignment.Center) {
|
||||
Text(
|
||||
label, style = MaterialTheme.typography.labelMedium, maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
fontSize = if (label.length > 4) 10.sp else 12.sp,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.systemBarsPadding
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import com.droiddeck.launcher.R
|
||||
|
||||
@Composable
|
||||
fun ScreenLayoutPanel(
|
||||
layouts: List<String>,
|
||||
onSave: (String) -> Unit,
|
||||
onLoad: (String) -> Unit,
|
||||
onDelete: (String) -> Unit,
|
||||
onImport: (String) -> Unit,
|
||||
exportText: () -> String,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
val p = LocalPalette.current
|
||||
val context = LocalContext.current
|
||||
var naming by remember { mutableStateOf(false) }
|
||||
var name by remember { mutableStateOf("") }
|
||||
|
||||
val importer = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
val text = runCatching {
|
||||
context.contentResolver.openInputStream(uri)?.use { it.readBytes().toString(Charsets.UTF_8) }
|
||||
}.getOrNull()
|
||||
if (text != null) onImport(text)
|
||||
}
|
||||
val exporter = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
runCatching { context.contentResolver.openOutputStream(uri)?.use { it.write(exportText().toByteArray()) } }
|
||||
}
|
||||
|
||||
Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) {
|
||||
Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column(Modifier.padding(start = 20.dp, end = 8.dp, top = 6.dp, bottom = 12.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
stringResource(R.string.screen_key_layout), Modifier.weight(1f),
|
||||
style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
IconButton(onClick = onDismiss) { Icon(Icons.Default.Close, null) }
|
||||
}
|
||||
Row(Modifier.fillMaxWidth().padding(end = 12.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
TextButton(onClick = { naming = true }) { Text(stringResource(R.string.screen_key_layout_save)) }
|
||||
TextButton(onClick = { importer.launch(arrayOf("application/json", "text/plain", "*/*")) }) {
|
||||
Text(stringResource(R.string.boot_movie_import))
|
||||
}
|
||||
TextButton(onClick = { exporter.launch("droiddeck-layout.icp") }) {
|
||||
Text(stringResource(R.string.screen_key_layout_export))
|
||||
}
|
||||
}
|
||||
Column(Modifier.padding(end = 12.dp).heightIn(max = 320.dp).verticalScroll(rememberScrollState())) {
|
||||
layouts.forEach { layout ->
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
layout, Modifier.weight(1f).clickable { onLoad(layout) }.padding(vertical = 14.dp),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
)
|
||||
TextButton(onClick = { onDelete(layout) }) {
|
||||
Text(stringResource(R.string.common_delete), color = p.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (naming) {
|
||||
Dialog(onDismissRequest = { naming = false }, properties = DialogProperties(usePlatformDefaultWidth = false)) {
|
||||
Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) {
|
||||
Surface(
|
||||
Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp),
|
||||
color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2),
|
||||
) {
|
||||
Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
OutlinedTextField(
|
||||
name, { name = it.take(24) }, Modifier.fillMaxWidth(), singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
|
||||
)
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
TextButton(onClick = { naming = false }) { Text(stringResource(R.string.common_cancel)) }
|
||||
Button(onClick = { onSave(name.trim()); naming = false }, enabled = name.isNotBlank()) {
|
||||
Text(stringResource(R.string.screen_key_layout_save))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -261,6 +261,10 @@ class DrawerActions(
|
||||
val onLowLatencyTouch: (Boolean) -> Unit = {},
|
||||
val onTouch: (String) -> Unit,
|
||||
val onGestureSettings: () -> Unit = {},
|
||||
val onScreenKeys: (() -> Unit)? = null,
|
||||
val onScreenLayouts: (() -> Unit)? = null,
|
||||
val screenKeysShown: Boolean = true,
|
||||
val onScreenKeysShown: (Boolean) -> Unit = {},
|
||||
val onFexPreset: (String) -> Unit,
|
||||
val onUpscaler: (Int) -> Unit = {},
|
||||
val onUpscaleSharpness: (Int) -> Unit = {},
|
||||
@@ -555,6 +559,23 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
|
||||
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"))
|
||||
if (a.onScreenKeys != null) {
|
||||
SettingsRow(stringResource(R.string.screen_keys), null) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||
) {
|
||||
SecondaryButton(stringResource(R.string.ctrl_edit),
|
||||
modifier = focus.track(page, "screen-keys"), onClick = a.onScreenKeys)
|
||||
a.onScreenLayouts?.let { open ->
|
||||
SecondaryButton(stringResource(R.string.screen_key_layout),
|
||||
modifier = focus.track(page, "screen-key-layouts"), onClick = open)
|
||||
}
|
||||
ToggleSwitch(a.screenKeysShown, true,
|
||||
modifier = focus.track(page, "screen-keys-shown"), onChange = a.onScreenKeysShown)
|
||||
}
|
||||
}
|
||||
}
|
||||
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)),
|
||||
|
||||
Reference in new issue
Block a user