Every menu, dialog and overlay on Jetpack Compose

The main screen, the loading overlay, the HUD line, the drawer and the three
dialogs are composables now, driven by state the activities own. The session's
SurfaceView and the on-screen pad stay Views underneath a single Compose layer,
which is what they are in any Compose app: a compositor surface and a canvas,
not menus. The XML layouts are gone; the activities extend ComponentActivity
and Back goes through its dispatcher.
This commit is contained in:
The412Banner committed 2026-09-19 16:05:54 -04:00
1 parent 18364845ad
commit aa7c71bfb6
15 files changed
+783 -741

No files matched your search

@@ -1,11 +1,10 @@
package com.steamdeck.launcher
import android.app.Activity
import android.hardware.input.InputManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.hardware.input.InputManager
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.KeyEvent
@@ -15,19 +14,32 @@ import android.view.SurfaceView
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.ComposeView
import com.steamdeck.launcher.core.FileUtils
import com.steamdeck.launcher.gpu.FrameGen
import com.steamdeck.launcher.gpu.LsfgNative
import com.steamdeck.launcher.gpu.TurnipDriver
import com.steamdeck.launcher.input.EvdevKeys
import com.steamdeck.launcher.input.OnScreenControls
import com.steamdeck.launcher.input.PadBridge
import com.steamdeck.launcher.runtime.LinuxRuntime
import com.steamdeck.launcher.session.LoadingPanel
import com.steamdeck.launcher.session.LoadingState
import com.steamdeck.launcher.session.PerfHud
import com.steamdeck.launcher.session.SessionDrawer
import com.steamdeck.launcher.session.SessionPrefs
import com.steamdeck.launcher.session.SessionService
import com.steamdeck.launcher.session.SessionState
import com.steamdeck.launcher.ui.DrawerActions
import com.steamdeck.launcher.ui.FrameGenDialog
import com.steamdeck.launcher.ui.HudText
import com.steamdeck.launcher.ui.LoadingOverlay
import com.steamdeck.launcher.ui.SessionDrawer
import com.steamdeck.launcher.ui.SteamDeckTheme
import com.steamdeck.launcher.wayland.CompositorHost
import com.steamdeck.launcher.wayland.WaylandCompositor
import java.io.File
@@ -39,18 +51,26 @@ import java.io.File
* user leave Big Picture for another app and come back to it still signed in and still
* downloading.
*
* So there are only three jobs here: hold a Surface under the compositor, forward input, and show
* the session's milestones until its first frame arrives.
* Three layers: the SurfaceView the compositor draws into, the on-screen pad (a canvas View,
* since it is input rather than a menu), and one Compose layer on top for everything else —
* the HUD line, the loading overlay, the drawer and its dialogs.
*/
class SessionActivity : Activity(), SurfaceHolder.Callback {
class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private lateinit var surfaceView: SurfaceView
private lateinit var loading: LoadingPanel
private lateinit var loading: LoadingState
private lateinit var hud: PerfHud
private lateinit var drawer: SessionDrawer
private var padBridge: PadBridge? = null
private var onScreenControls: OnScreenControls? = null
private var watching = true
// Compose reads these; the activity writes them.
private var drawerOpen by mutableStateOf(false)
private var showFrameGen by mutableStateOf(false)
private var hudOn by mutableStateOf(true)
private var frameGenLabel by mutableStateOf("Off")
private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO)
private var shapeMode by mutableStateOf(SessionPrefs.SHAPE_AUTO)
/**
* Shows the on-screen pad when nothing is plugged in and takes it away the moment something
* is — a user with a controller in their hands should not be looking at buttons they cannot
@@ -77,44 +97,94 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
surfaceView = SurfaceView(this)
surfaceView.holder.addCallback(this)
root.addView(surfaceView)
// Until the client's first frame arrives there is nothing on screen for a minute or more
// on a first run, so the session's own progress is shown instead of a black panel.
val panel = layoutInflater.inflate(R.layout.session_loading, root, false)
root.addView(panel)
loading = LoadingPanel(panel)
// The performance line, above everything but the loading panel; it starts with the
// client's first frame, since before that there is nothing to count.
hud = PerfHud(this)
root.addView(hud.view, root.indexOfChild(panel))
// Back opens this; it sits above everything, the loading panel included.
drawer = SessionDrawer(this, root,
onHudChanged = { hud.refresh() },
onOscChanged = { updateOnScreenControls() },
onFrameGenChanged = { applyFrameGen() },
onBackground = { moveTaskToBack(true) },
onStop = { SessionService.stop(this); finish() })
// A session that has already drawn is past its milestones; do not cover its picture.
if (SessionState.running && SessionState.firstFrameSeen) {
loading.hide()
hud.start()
} else {
loading.show()
}
setContentView(root)
val bridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input"))
padBridge = bridge
onScreenControls = OnScreenControls(this, bridge).also { root.addView(it, 1) }
onScreenControls = OnScreenControls(this, bridge).also { root.addView(it) }
loading = LoadingState(this)
hud = PerfHud(this)
// A session that has already drawn is past its milestones; do not cover its picture.
if (SessionState.running && SessionState.firstFrameSeen) {
loading.visible = false
hud.start()
}
readPrefs()
// One Compose layer for every menu and overlay. Touches nothing in it consumes fall
// through to the pad and the game underneath.
root.addView(ComposeView(this).apply {
setContent {
SteamDeckTheme {
if (hud.text.isNotEmpty()) HudText(hud.text)
if (loading.visible) LoadingOverlay(loading.step, loading.percent, loading.elapsed, loading.hint, loading.ended)
if (drawerOpen) SessionDrawer(DrawerActions(
hudOn = hudOn, frameGenLabel = frameGenLabel, oscMode = oscMode,
shapeMode = if (shapeMode == SessionPrefs.SHAPE_WIDE) "16:9" else "panel",
onHud = { on -> SessionPrefs.setHudEnabled(this@SessionActivity, on); hudOn = on; hud.refresh() },
onFrameGen = { showFrameGen = true },
onOsc = {
val next = when (SessionPrefs.oscMode(this@SessionActivity)) {
SessionPrefs.OSC_AUTO -> SessionPrefs.OSC_ALWAYS
SessionPrefs.OSC_ALWAYS -> SessionPrefs.OSC_NEVER
else -> SessionPrefs.OSC_AUTO
}
SessionPrefs.setOscMode(this@SessionActivity, next)
readPrefs()
updateOnScreenControls()
},
onShape = {
val next = if (SessionPrefs.shapeMode(this@SessionActivity) == SessionPrefs.SHAPE_WIDE)
SessionPrefs.SHAPE_AUTO else SessionPrefs.SHAPE_WIDE
SessionPrefs.setShapeMode(this@SessionActivity, next)
readPrefs()
},
onBackground = { drawerOpen = false; moveTaskToBack(true) },
onStop = { drawerOpen = false; SessionService.stop(this@SessionActivity); finish() },
onClose = { drawerOpen = false },
))
if (showFrameGen) FrameGenDialog(
engine = FrameGen.engine(this@SessionActivity),
multiplier = FrameGen.multiplier(this@SessionActivity),
lsfgReady = LsfgNative.isInstalled(this@SessionActivity),
onPick = { engine, multiplier ->
FrameGen.set(this@SessionActivity, engine, multiplier)
showFrameGen = false
readPrefs()
applyFrameGen()
},
onDismiss = { showFrameGen = false },
)
}
}
})
setContentView(root)
updateOnScreenControls()
WaylandCompositor.setFirstFrameListener {
SessionState.firstFrameSeen = true
runOnUiThread {
loading.hide()
loading.visible = false
hud.start()
}
}
SessionState.endListener = { status -> onSessionEnded(status) }
watchSessionLog()
// Back opens the drawer (and closes it again). Leaving the session running in the
// background and ending it are both actions in there, so neither can happen by accident
// from a button a game might also be reading.
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (showFrameGen) showFrameGen = false else drawerOpen = !drawerOpen
}
})
watchSession()
}
private fun readPrefs() {
hudOn = SessionPrefs.hudEnabled(this)
frameGenLabel = FrameGen.label(this)
oscMode = SessionPrefs.oscMode(this)
shapeMode = SessionPrefs.shapeMode(this)
}
// ── Compositor ──────────────────────────────────────────────────────────────────────────
@@ -133,8 +203,8 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
val size = outputSize()
SessionState.outputSize = size
SessionState.refreshHz = refreshHz()
// Letterbox, never stretch or crop: the output above can be a different shape from the
// panel now, and a game's picture must keep its proportions with bars, not lose its edges.
// Letterbox, never stretch or crop: the output can be a different shape from the panel,
// and a game's picture must keep its proportions with bars, not lose its edges.
WaylandCompositor.nativeSetScaleMode(SCALE_FIT, ALIGN_CENTER)
CompositorHost.startOrAttach(
holder.surface, runtimeDir.path,
@@ -164,8 +234,7 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
*/
private fun outputSize(): Pair<Int, Int> {
// The panel, not the window: resources.displayMetrics is what is left after the system
// bars and the cutout are taken out, which sized the first session 1920x968 on a 1080p
// device and had gamescope patch its EDID to match.
// bars and the cutout are taken out.
val bounds = if (Build.VERSION.SDK_INT >= 30) {
windowManager.maximumWindowMetrics.bounds
} else {
@@ -177,10 +246,10 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
val panelW = maxOf(bounds.width(), bounds.height()).toFloat()
val panelH = minOf(bounds.width(), bounds.height()).toFloat()
// Never narrower than 16:9. A foldable's inner panel is nearly square, and a game handed a
// square display draws for the frame it was made for and cuts the sides off itself
// (FlatOut on a Fold lost the edges of its own menus). Wider than 16:9 is fine — games and
// the client cope with a phone's 20:9 — so the panel's aspect is kept above that and the
// compositor letterboxes the 16:9 picture onto a squarer panel.
// square display draws for the frame it was made for and cuts the sides off itself.
// Wider than 16:9 is fine — games and the client cope with a phone's 20:9 — so the
// panel's aspect is kept above that, unless the user pinned 16:9 for a foldable, and the
// compositor letterboxes onto a squarer panel.
val aspect = if (SessionPrefs.shapeMode(this) == SessionPrefs.SHAPE_WIDE) 16f / 9f
else maxOf(panelW / panelH, 16f / 9f)
// 1080 tall at most: the client's CEF is the heaviest thing in the session, and above
@@ -197,15 +266,31 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
return if (hz > 1f) hz else 60f
}
/**
* The saved frame-generation setting, pushed to the compositor. Off the main thread: LSFG's
* first use translates the shader chain out of Lossless.dll, which takes seconds.
*/
private fun applyFrameGen() {
val hz = refreshHz()
Thread({
val problem = FrameGen.apply(this, hz)
if (problem != null) runOnUiThread { Toast.makeText(this, problem, Toast.LENGTH_LONG).show() }
}, "frame-gen").start()
}
// ── Session state ───────────────────────────────────────────────────────────────────────
/** Keeps the loading panel current from the session log while it is showing. */
private fun watchSessionLog() {
/** Keeps the loading overlay current from the session log, and its clock moving. */
private fun watchSession() {
val handler = Handler(Looper.getMainLooper())
var ticks = 0
val poll = object : Runnable {
override fun run() {
if (!watching) return
if (loading.isVisible) loading.update(SessionState.logFile)
if (loading.visible && !loading.ended) {
loading.update(this@SessionActivity, SessionState.logFile)
if (ticks++ % 2 == 0) loading.tick()
}
handler.postDelayed(this, 500)
}
}
@@ -216,8 +301,7 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
runOnUiThread {
if (isFinishing || isDestroyed) return@runOnUiThread
if (status != 0) {
loading.showEnded(getString(R.string.session_ended, status,
SessionState.logFile?.path ?: "-"))
loading.showEnded("The session ended ($status)\n${SessionState.logFile?.path ?: "-"}")
// A moment on screen, so a failure is readable rather than a flash of black.
Handler(Looper.getMainLooper()).postDelayed({ finish() }, 4000)
} else {
@@ -230,14 +314,12 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (padBridge?.onKeyEvent(event) == true) return true
// A hardware keyboard, forwarded to the compositor's wl_keyboard. This is how an account
// name and password get typed on the client's first run; the client's own on-screen
// keyboard covers a device without one, driven by touch or the pad.
if (CompositorHost.isStarted && event.device != null && !PadBridge.isFromController(event.device)) {
// A hardware keyboard, forwarded to the compositor's wl_keyboard. Back is left to the
// activity, which opens the drawer.
if (CompositorHost.isStarted && event.keyCode != KeyEvent.KEYCODE_BACK
&& event.device != null && !PadBridge.isFromController(event.device)) {
val down = event.action == KeyEvent.ACTION_DOWN
if (down || event.action == KeyEvent.ACTION_UP) {
// An unmapped key may still be a real one on a foreign layout: its scan code is
// the evdev code the kernel gave Android in the first place.
var evdev = EvdevKeys.fromKeyCode(event.keyCode)
if (evdev < 0 && event.scanCode > 0) evdev = event.scanCode
if (evdev > 0) {
@@ -256,8 +338,9 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
/**
* Touch as a mouse: where you touch is where the pointer goes, and a tap is a left click.
* The compositor's pointer space is a fixed 1920x1080 whatever the output size, so the view's
* coordinates are scaled into it.
* The compositor's pointer space is a fixed 1920x1080, and the picture is letterboxed inside
* the view when the panel is a different shape from the output, so a touch is mapped through
* the fitted rectangle.
*/
override fun onTouchEvent(event: MotionEvent): Boolean {
val width = surfaceView.width.takeIf { it > 0 } ?: return false
@@ -268,8 +351,6 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> 2
else -> return false
}
// The picture is letterboxed inside the view when the panel is a different shape from the
// output, so a touch is mapped through the fitted rectangle, not the whole view.
val out = SessionState.outputSize
val scale = minOf(width / out.first.toFloat(), height / out.second.toFloat())
val drawnW = out.first * scale
@@ -282,18 +363,6 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
return true
}
// ── Lifecycle ───────────────────────────────────────────────────────────────────────────
/**
* Back opens the drawer (and closes it again). Leaving the session running in the background
* and ending it are both actions in there, so neither can happen by accident from a button
* a game might also be reading.
*/
@Suppress("DEPRECATION")
override fun onBackPressed() {
drawer.toggle()
}
/**
* The drawer's mode ("always" / "never") decides outright; on "auto" the controls follow
* what is attached. `steamdeck-osc` in Downloads still overrides, for a device we cannot reach.
@@ -315,26 +384,15 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
Log.i(TAG, "on-screen controls " + (if (show) "shown" else "hidden"))
}
/**
* The saved frame-generation setting, pushed to the compositor. Off the main thread: LSFG's
* first use translates the shader chain out of Lossless.dll, which takes seconds.
*/
private fun applyFrameGen() {
val hz = refreshHz()
Thread({
val problem = FrameGen.apply(this, hz)
if (problem != null) runOnUiThread {
android.widget.Toast.makeText(this, problem, android.widget.Toast.LENGTH_LONG).show()
}
}, "frame-gen").start()
}
// ── Lifecycle ───────────────────────────────────────────────────────────────────────────
override fun onResume() {
super.onResume()
if (CompositorHost.isStarted) applyFrameGen()
(getSystemService(INPUT_SERVICE) as? InputManager)
?.registerInputDeviceListener(deviceListener, Handler(Looper.getMainLooper()))
updateOnScreenControls()
readPrefs()
if (CompositorHost.isStarted) applyFrameGen()
}
override fun onPause() {
@@ -369,7 +427,7 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
// Deliberately does NOT end the session: this activity can be destroyed while the user is
// in another app, and the whole point of the service is that Steam survives that.
watching = false
hud.stop()
if (::hud.isInitialized) hud.stop()
padBridge?.stop()
SessionState.endListener = null
WaylandCompositor.setFirstFrameListener(null)