mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 05:46:03 +00:00
feat(dual-screen): swap screens - game on the bottom screen, panel on top (#2006)
Swap screens for dual-screen handhelds: the game moves to the built-in bottom screen while the top screen shows the Dashboard or the KDE desktop. Swap button in the Dashboard and Desktop bars and a Show the game on row in the session menu. The desktop resizes to the screen it is on, Follow screen and Match screen sessions take the bottom screen's shape, the keyboard opens on the top screen, and the game swaps back on session end, picture-in-picture, glasses, or when the bottom screen is taken. Review follow-ups cover desktop restart, refresh pacing, key and pointer ownership, touch cancellation and bottom surface recovery.
This commit is contained in:
1 parent
3fbd86c89d
commit
8d3616eccd
14 files changed
+515
-71
No files matched your search
@@ -20,6 +20,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Close
|
||||
import androidx.compose.material.icons.outlined.Menu
|
||||
import androidx.compose.material.icons.outlined.SwapVert
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
@@ -84,10 +85,11 @@ fun BottomDashboard(
|
||||
pages: List<BottomPage>,
|
||||
remembered: String?,
|
||||
onPage: (String) -> Unit,
|
||||
onClose: () -> Unit,
|
||||
onClose: (() -> Unit)?,
|
||||
onMenu: (() -> Unit)?,
|
||||
gameStarts: Int = 0,
|
||||
strip: (@Composable (String?) -> Unit)? = null,
|
||||
onSwap: (() -> Unit)? = null,
|
||||
) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
var current by remember { mutableStateOf(pickBottomPage(remembered, pages.map { it.id })) }
|
||||
@@ -100,7 +102,7 @@ fun BottomDashboard(
|
||||
val labels = bottomTabsWithLabels(maxWidth.value, pages.size)
|
||||
Column(Modifier.fillMaxSize().padding(horizontal = 12.dp, vertical = 10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
Row(Modifier.fillMaxWidth().height(48.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
BarButton(Icons.Outlined.Close, stringResource(R.string.deck_close), "bottom-close", onClose)
|
||||
if (onClose != null) BarButton(Icons.Outlined.Close, stringResource(R.string.deck_close), "bottom-close", onClose)
|
||||
Row(Modifier.weight(1f).horizontalScroll(rememberScrollState()), horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
pages.forEach { p ->
|
||||
PageTab(p, p.id == page?.id, labels) {
|
||||
@@ -109,6 +111,7 @@ fun BottomDashboard(
|
||||
}
|
||||
}
|
||||
}
|
||||
if (onSwap != null) BarButton(Icons.Outlined.SwapVert, stringResource(R.string.swap_screens), "bottom-swap", onSwap)
|
||||
if (onMenu != null) BarButton(Icons.Outlined.Menu, stringResource(R.string.touch_droiddeck_menu), "bottom-menu", onMenu)
|
||||
}
|
||||
strip?.invoke(page?.id)
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.graphics.Color
|
||||
import android.os.Build
|
||||
import android.os.SystemClock
|
||||
import android.graphics.Typeface
|
||||
import android.view.Gravity
|
||||
@@ -13,6 +14,7 @@ import android.view.SurfaceHolder
|
||||
import android.view.SurfaceView
|
||||
import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import android.view.WindowInsets
|
||||
import android.graphics.RectF
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
@@ -33,9 +35,11 @@ import com.droiddeck.launcher.session.BottomDesktop
|
||||
@SuppressLint("ViewConstructor")
|
||||
class BottomDesktopPanel(
|
||||
context: Context,
|
||||
onClose: () -> Unit,
|
||||
onClose: (() -> Unit)?,
|
||||
onMenu: (() -> Unit)?,
|
||||
steamButtons: Pair<() -> Unit, () -> Unit>?,
|
||||
onSwap: (() -> Unit)? = null,
|
||||
private val fullResolution: Boolean = false,
|
||||
) : LinearLayout(context), SurfaceHolder.Callback {
|
||||
private lateinit var surfaceView: SurfaceView
|
||||
private val area = FrameLayout(context).apply { clipChildren = true }
|
||||
@@ -66,7 +70,7 @@ class BottomDesktopPanel(
|
||||
setBackgroundColor(Color.BLACK)
|
||||
isFocusable = true
|
||||
isFocusableInTouchMode = true
|
||||
addView(bar(onClose, onMenu, steamButtons), LayoutParams(-1, -2))
|
||||
addView(bar(onClose, onMenu, steamButtons, onSwap), LayoutParams(-1, -2))
|
||||
area.addView(keyboardHost)
|
||||
addSurface()
|
||||
area.addView(status, FrameLayout.LayoutParams(-1, -1))
|
||||
@@ -83,7 +87,7 @@ class BottomDesktopPanel(
|
||||
area.addView(surfaceView, 1, FrameLayout.LayoutParams(fullWidth.takeIf { it > 0 } ?: -1, fullHeight.takeIf { it > 0 } ?: -1))
|
||||
}
|
||||
|
||||
private fun bar(onClose: () -> Unit, onMenu: (() -> Unit)?, steamButtons: Pair<() -> Unit, () -> Unit>?): View {
|
||||
private fun bar(onClose: (() -> Unit)?, onMenu: (() -> Unit)?, steamButtons: Pair<() -> Unit, () -> Unit>?, onSwap: (() -> Unit)?): View {
|
||||
val bar = LinearLayout(context).apply {
|
||||
orientation = HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
@@ -91,7 +95,7 @@ class BottomDesktopPanel(
|
||||
setBackgroundColor(Color.BLACK)
|
||||
}
|
||||
fun gap() = View(context).apply { layoutParams = LayoutParams(dp(6), 1) }
|
||||
bar.addView(DeckControlsPanel.roundButton(context, "×", context.getString(R.string.deck_close), onClose))
|
||||
onClose?.let { bar.addView(DeckControlsPanel.roundButton(context, "×", context.getString(R.string.deck_close), it)) }
|
||||
title = TextView(context).apply {
|
||||
textSize = 14f
|
||||
typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
|
||||
@@ -106,6 +110,10 @@ class BottomDesktopPanel(
|
||||
bar.addView(gap())
|
||||
bar.addView(DeckControlsPanel.iconButton(context, R.drawable.ic_bottom_keyboard,
|
||||
context.getString(R.string.second_show_keyboard)) { keyboardHost.toggle() })
|
||||
onSwap?.let {
|
||||
bar.addView(gap())
|
||||
bar.addView(DeckControlsPanel.iconButton(context, R.drawable.ic_bottom_swap, context.getString(R.string.swap_screens), it))
|
||||
}
|
||||
onMenu?.let {
|
||||
bar.addView(gap())
|
||||
bar.addView(DeckControlsPanel.menuButton(context, it))
|
||||
@@ -187,7 +195,9 @@ class BottomDesktopPanel(
|
||||
addSurface()
|
||||
return
|
||||
}
|
||||
if (attached) attach(surfaceView.holder)
|
||||
if (!attached) return
|
||||
attached = false
|
||||
syncSurface(surfaceView.holder)
|
||||
}
|
||||
|
||||
private fun showState() {
|
||||
@@ -208,13 +218,28 @@ class BottomDesktopPanel(
|
||||
if (w != fullWidth || h > fullHeight || !keyboardHost.shown) {
|
||||
fullWidth = w
|
||||
fullHeight = h
|
||||
surfaceView.layoutParams = FrameLayout.LayoutParams(w, h)
|
||||
val (desktopW, desktopH) = BottomDesktop.outputSizeFor(w, h, fullResolution)
|
||||
val (fitW, fitH) = if (w - desktopW in 0..1 && h - desktopH in 0..1) desktopW to desktopH
|
||||
else fitDesktop(w, h, desktopW, desktopH)
|
||||
surfaceView.layoutParams = if (fitW == w && fitH == h) FrameLayout.LayoutParams(w, h)
|
||||
else FrameLayout.LayoutParams(fitW, fitH, Gravity.CENTER_HORIZONTAL).apply { topMargin = (h - fitH) / 2 }
|
||||
surfaceView.post { syncSurface(surfaceView.holder) }
|
||||
}
|
||||
pan()
|
||||
}
|
||||
|
||||
private fun imeOverlap(): Int {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return 0
|
||||
if (area.height < fullHeight) return 0
|
||||
val ime = rootWindowInsets?.getInsets(WindowInsets.Type.ime())?.bottom ?: return 0
|
||||
if (ime <= 0) return 0
|
||||
val at = IntArray(2)
|
||||
area.getLocationInWindow(at)
|
||||
return (at[1] + area.height - (rootView.height - ime)).coerceIn(0, area.height)
|
||||
}
|
||||
|
||||
private fun pan() {
|
||||
val visible = area.height
|
||||
val visible = area.height - imeOverlap()
|
||||
val hidden = (fullHeight - visible).coerceAtLeast(0)
|
||||
val anchor = if (directTouch) lastTouchY else trackpad.y
|
||||
surfaceView.translationY = if (hidden == 0 || !keyboardHost.shown) 0f
|
||||
@@ -223,24 +248,27 @@ class BottomDesktopPanel(
|
||||
|
||||
override fun surfaceCreated(holder: SurfaceHolder) = Unit
|
||||
|
||||
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
|
||||
if (!attached) attach(holder)
|
||||
}
|
||||
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) = syncSurface(holder)
|
||||
|
||||
override fun surfaceDestroyed(holder: SurfaceHolder) {
|
||||
if (holder !== surfaceView.holder) return
|
||||
attached = false
|
||||
surfaceUsed = false
|
||||
BottomDesktop.detach()
|
||||
BottomDesktop.detach(holder.surface)
|
||||
}
|
||||
|
||||
private fun attach(holder: SurfaceHolder) {
|
||||
val frame = holder.surfaceFrame
|
||||
val (width, height) = BottomDesktop.outputSizeFor(fullWidth.takeIf { it > 0 } ?: frame.width(),
|
||||
fullHeight.takeIf { it > 0 } ?: frame.height())
|
||||
private fun syncSurface(holder: SurfaceHolder) {
|
||||
if (holder !== surfaceView.holder || fullWidth <= 0 || fullHeight <= 0 || !holder.surface.isValid) return
|
||||
val (width, height) = BottomDesktop.outputSizeFor(fullWidth, fullHeight, fullResolution)
|
||||
if (width <= 0 || height <= 0) return
|
||||
val frame = holder.surfaceFrame
|
||||
if (frame.width() != width || frame.height() != height) {
|
||||
holder.setFixedSize(width, height)
|
||||
val params = surfaceView.layoutParams
|
||||
if (params.width == width && params.height == height) holder.setSizeFromLayout() else holder.setFixedSize(width, height)
|
||||
return
|
||||
}
|
||||
if (attached) {
|
||||
BottomDesktop.resize(holder.surface, width, height)
|
||||
return
|
||||
}
|
||||
attached = true
|
||||
@@ -299,7 +327,7 @@ class BottomDesktopPanel(
|
||||
override fun onWheel(steps: Int) = BottomDesktop.sendScene(SCENE_WHEEL, steps)
|
||||
|
||||
override fun onScroll(dx: Float, dy: Float) {
|
||||
val (outW, outH) = BottomDesktop.outputSizeFor(surfaceView.width, surfaceView.height)
|
||||
val (outW, outH) = BottomDesktop.outputSize
|
||||
scrollRestX += dx * outW / surfaceView.width.coerceAtLeast(1) * AXIS_UNITS_PER_PIXEL
|
||||
scrollRestY += dy * outH / surfaceView.height.coerceAtLeast(1) * AXIS_UNITS_PER_PIXEL
|
||||
val h = scrollRestX.toInt()
|
||||
@@ -343,3 +371,11 @@ class BottomDesktopPanel(
|
||||
const val TEXT_FIELD_TAP_MS = 1_000L
|
||||
}
|
||||
}
|
||||
|
||||
internal fun fitDesktop(areaW: Int, areaH: Int, desktopW: Int, desktopH: Int): Pair<Int, Int> {
|
||||
if (areaW <= 0 || areaH <= 0 || desktopW <= 0 || desktopH <= 0) return areaW to areaH
|
||||
val widthLimited = areaW.toLong() * desktopH <= areaH.toLong() * desktopW
|
||||
val w = if (widthLimited) areaW else (desktopW.toLong() * areaH / desktopH).toInt()
|
||||
val h = if (widthLimited) (desktopH.toLong() * areaW / desktopW).toInt() else areaH
|
||||
return if (areaW - w <= 4 && areaH - h <= 4) areaW to areaH else w to h
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Presentation
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.view.Display
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import android.view.SurfaceHolder
|
||||
import android.view.SurfaceView
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.WindowManager
|
||||
import android.widget.FrameLayout
|
||||
|
||||
class BottomGameScreen(
|
||||
private val activity: Activity,
|
||||
display: Display,
|
||||
private val callback: SurfaceHolder.Callback,
|
||||
private val onTouch: (MotionEvent) -> Boolean,
|
||||
) : Presentation(activity, display) {
|
||||
val surfaceView = SurfaceView(getContext())
|
||||
val holder: SurfaceHolder get() = surfaceView.holder
|
||||
|
||||
val hz: Float get() = display.mode.refreshRate
|
||||
|
||||
init {
|
||||
setCancelable(false)
|
||||
setCanceledOnTouchOutside(false)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val root = FrameLayout(context).apply { setBackgroundColor(Color.BLACK) }
|
||||
root.addView(surfaceView, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
||||
setContentView(root)
|
||||
window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
|
||||
@Suppress("DEPRECATION")
|
||||
window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
||||
or View.SYSTEM_UI_FLAG_FULLSCREEN
|
||||
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||||
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||||
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
|
||||
surfaceView.setOnTouchListener { _, event -> onTouch(event) }
|
||||
surfaceView.holder.addCallback(callback)
|
||||
}
|
||||
|
||||
override fun dispatchKeyEvent(event: KeyEvent): Boolean =
|
||||
activity.dispatchKeyEvent(event) || super.dispatchKeyEvent(event)
|
||||
|
||||
override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean =
|
||||
activity.dispatchGenericMotionEvent(event) || super.dispatchGenericMotionEvent(event)
|
||||
}
|
||||
|
||||
object GameScreenSwap {
|
||||
fun wanted(
|
||||
requested: Boolean,
|
||||
activityStarted: Boolean,
|
||||
sessionEnded: Boolean,
|
||||
inPip: Boolean,
|
||||
glassesOutput: Boolean,
|
||||
bottomScreenFree: Boolean,
|
||||
): Boolean = requested && activityStarted && !sessionEnded && !inPip && !glassesOutput && bottomScreenFree
|
||||
}
|
||||
@@ -58,6 +58,7 @@ class SecondScreenPresentation(
|
||||
private val onMenu: (() -> Unit)? = null,
|
||||
private val menu: (@androidx.compose.runtime.Composable () -> Unit)? = null,
|
||||
private val dashboard: (@androidx.compose.runtime.Composable () -> Unit)? = null,
|
||||
private val onSwap: (() -> Unit)? = null,
|
||||
) : Presentation(sessionActivity, display) {
|
||||
private val frame = FrameLayout(context)
|
||||
private var menuView: androidx.compose.ui.platform.ComposeView? = null
|
||||
@@ -246,7 +247,7 @@ class SecondScreenPresentation(
|
||||
SecondScreenMode.DESKTOP -> {
|
||||
ownComposeViews()
|
||||
root.addView(BottomDesktopPanel(context, onClose, onMenu,
|
||||
(onSteamMenu to onQam).takeIf { SessionState.mode == SessionService.MODE_STEAM }), weightParams())
|
||||
(onSteamMenu to onQam).takeIf { SessionState.mode == SessionService.MODE_STEAM }, onSwap), weightParams())
|
||||
}
|
||||
SecondScreenMode.NONE -> Unit
|
||||
}
|
||||
|
||||
@@ -245,6 +245,9 @@ class DrawerActions(
|
||||
val textureAnisotropy: Int? = 0,
|
||||
val textureLodBias: String? = TextureFiltering.LOD_BIAS_OFF,
|
||||
val secondScreenMode: SecondScreenMode,
|
||||
val gameOnBottom: Boolean = false,
|
||||
val gameSwapAvailable: Boolean = false,
|
||||
val onGameOnBottom: (Boolean) -> Unit = {},
|
||||
val dashboardAvailable: Boolean = false,
|
||||
val desktopAvailable: Boolean = false,
|
||||
val secondScreenDisplays: List<SecondScreenDisplay>,
|
||||
@@ -1176,6 +1179,11 @@ private fun SecondScreenGroup(host: MenuHost, a: DrawerActions, track: (String)
|
||||
a.secondScreenDisplays.map { it.id to it.label }, a.selectedSecondScreenDisplay,
|
||||
chipModifier = track("second-screen-display"), onPick = a.onSecondScreenDisplay)
|
||||
}
|
||||
if (a.gameSwapAvailable || a.gameOnBottom) SettingsGroup(stringResource(R.string.swap_game_screen_group)) {
|
||||
ChoiceRow(host, "game-screen", stringResource(R.string.swap_game_screen), null,
|
||||
listOf(false to stringResource(R.string.swap_game_screen_top), true to stringResource(R.string.swap_game_screen_bottom)),
|
||||
a.gameOnBottom, chipModifier = track("game-screen"), onPick = a.onGameOnBottom)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
Reference in new issue
Block a user