perf: pause the home page's ambient motion after 20 s without input (#1550)

* perf: pause the home page's ambient motion after 20 s without input

* fix: restart the idle timer when a touch gesture ends
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-25 11:41:39 -04:00
1 parent e62698e395
commit 709c041b24
5 files changed
+81 -29

No files matched your search

@@ -1272,6 +1272,23 @@ class FrontEndActivity : ComponentActivity() {
}
}
override fun onUserInteraction() {
super.onUserInteraction()
com.droiddeck.launcher.ui.Ambient.poke()
}
override fun dispatchTouchEvent(event: android.view.MotionEvent): Boolean {
if (event.actionMasked == android.view.MotionEvent.ACTION_UP || event.actionMasked == android.view.MotionEvent.ACTION_CANCEL) {
com.droiddeck.launcher.ui.Ambient.poke()
}
return super.dispatchTouchEvent(event)
}
override fun dispatchGenericMotionEvent(event: android.view.MotionEvent): Boolean {
com.droiddeck.launcher.ui.Ambient.poke()
return super.dispatchGenericMotionEvent(event)
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.keyCode == KeyEvent.KEYCODE_BUTTON_A) com.droiddeck.launcher.ui.HeldKeys.confirm = event.action == KeyEvent.ACTION_DOWN
if (showComponents && (event.keyCode == KeyEvent.KEYCODE_BUTTON_L1 || event.keyCode == KeyEvent.KEYCODE_BUTTON_R1)) {
@@ -1297,6 +1314,7 @@ class FrontEndActivity : ComponentActivity() {
com.droiddeck.launcher.stores.StoresState.refresh(this)
com.droiddeck.launcher.stores.StoresState.libraryListener = { refreshAddedGames(); refresh() }
com.droiddeck.launcher.ui.Motion.refresh(this)
com.droiddeck.launcher.ui.Ambient.poke()
com.droiddeck.launcher.ui.QuitFlood.take()?.let { c ->
returning = ReturningFlood(c, com.droiddeck.launcher.ui.LaunchOrigin.takeReturn())
}
@@ -254,6 +254,12 @@ internal object Motion {
if (scale == 0f) snap() else spring(damping, stiffness)
}
internal object Ambient {
var poked by mutableStateOf(0L)
private set
fun poke() { poked = android.os.SystemClock.uptimeMillis() }
}
private val Shape10 = RoundedCornerShape(10.dp)
internal val Shape12 = RoundedCornerShape(12.dp)
@@ -641,11 +641,7 @@ internal fun LiftHero(
}
@Composable
internal fun Handle(height: Dp, above: Boolean = false) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val shape = if (above) RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp) else RoundedCornerShape(bottomStart = 12.dp, bottomEnd = 12.dp)
val still = Motion.scale == 0f
private fun handleBob(): Float {
val bob = androidx.compose.animation.core.rememberInfiniteTransition(label = "handle")
val lift by bob.animateFloat(
0f, 0f,
@@ -660,6 +656,16 @@ internal fun Handle(height: Dp, above: Boolean = false) {
),
label = "handleBob",
)
return lift
}
@Composable
internal fun Handle(height: Dp, above: Boolean = false) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val shape = if (above) RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp) else RoundedCornerShape(bottomStart = 12.dp, bottomEnd = 12.dp)
val still = Motion.scale == 0f || !ambientAwake()
val lift = if (still) 0f else handleBob()
Row(
verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp),
modifier = Modifier.height(height).clip(shape).background(colors.surface)
@@ -222,7 +222,7 @@ fun LoadingOverlay(
.background(colors.background)
.blockTouches(),
) {
CapsuleWall(games, driftMs = 65_000, modifier = Modifier.alpha(0.3f))
CapsuleWall(games, driftMs = 65_000, modifier = Modifier.alpha(0.3f), pauseWhenIdle = false)
Box(
Modifier.fillMaxSize().drawBehind {
drawRect(
@@ -5,12 +5,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import com.droiddeck.launcher.R
import androidx.compose.ui.res.stringResource
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.runtime.State
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
@@ -76,6 +70,38 @@ private const val WALL_MIN_GAMES = 4
private const val WALL_DISTINCT = 40
private const val DRIFT_STEP_NS = 15_000_000L
private const val AMBIENT_IDLE_NS = 20_000_000_000L
@Composable
internal fun ambientAwake(): Boolean {
val poked = Ambient.poked
var awake by remember { mutableStateOf(true) }
LaunchedEffect(poked) {
awake = true
kotlinx.coroutines.delay(AMBIENT_IDLE_NS / 1_000_000)
awake = false
}
return awake
}
@Composable
internal fun AmbientFrames(pauseWhenIdle: Boolean = true, step: (Long) -> Unit) {
val onStep = androidx.compose.runtime.rememberUpdatedState(step)
val poked = if (pauseWhenIdle) Ambient.poked else 0L
if (Motion.scale != 0f) LaunchedEffect(poked, pauseWhenIdle) {
var shown = 0L
var end = Long.MAX_VALUE
while (shown < end) withFrameNanos { now ->
if (shown == 0L) {
shown = now
if (pauseWhenIdle) end = now + AMBIENT_IDLE_NS
} else if (now - shown >= DRIFT_STEP_NS) {
onStep.value(now - shown)
shown = now
}
}
}
}
@Composable
internal fun SteamHome(s: FrontEndState, a: FrontEndActions, modifier: Modifier) {
@@ -179,6 +205,7 @@ internal fun CapsuleWall(
modifier: Modifier = Modifier,
owned: List<Pair<Int, java.io.File>> = emptyList(),
featured: List<com.droiddeck.launcher.frontend.SteamFeatured.Game> = emptyList(),
pauseWhenIdle: Boolean = true,
) {
val pal = LocalPalette.current
val blank = remember(pal) { WallCapsule(Brush.linearGradient(listOf(pal.surface, pal.background)), edge = pal.line) }
@@ -188,7 +215,7 @@ internal fun CapsuleWall(
val store = if (owned.isEmpty()) featured.filter { shown.add(it.appId) }.map { it.name to it.art } else emptyList()
(library + store).take((WALL_DISTINCT - games.size).coerceAtLeast(0))
}
TiltedWall(games.size + padding.size, driftMs, modifier) { i ->
TiltedWall(games.size + padding.size, driftMs, modifier, pauseWhenIdle = pauseWhenIdle) { i ->
when {
i < games.size -> WallCapsule(artBrush(hueOf(games[i].name)), games[i].art)
i - games.size < padding.size -> padding[i - games.size].let { (seed, art) -> WallCapsule(artBrush(hueOf(seed)), art) }
@@ -208,20 +235,13 @@ internal fun TiltedWall(
capH: androidx.compose.ui.unit.Dp = 168.dp,
gap: androidx.compose.ui.unit.Dp = 14.dp,
corner: androidx.compose.ui.unit.Dp = 10.dp,
pauseWhenIdle: Boolean = true,
capsule: (index: Int) -> WallCapsule,
) {
val still = Motion.scale == 0f
val drift = remember { mutableFloatStateOf(0f) }
val period = androidx.compose.runtime.rememberUpdatedState(driftMs)
if (!still) LaunchedEffect(Unit) {
var shown = 0L
while (true) withFrameNanos { now ->
if (shown == 0L) shown = now
else if (now - shown >= DRIFT_STEP_NS) {
drift.floatValue = ((drift.floatValue + (now - shown) / (period.value * 1_000_000.0 * Motion.scale)) % 1.0).toFloat()
shown = now
}
}
AmbientFrames(pauseWhenIdle) { dt ->
drift.floatValue = ((drift.floatValue + dt / (driftMs * 1_000_000.0 * Motion.scale)) % 1.0).toFloat()
}
val density = LocalDensity.current
BoxWithConstraints(modifier.fillMaxSize()) {
@@ -307,10 +327,9 @@ private fun DrawScope.drawCapsule(look: WallCapsule, art: Painter?, radius: Floa
private fun CapsuleFan(games: List<Library.SteamGame>) {
val pal = LocalPalette.current
val density = LocalDensity.current
val transition = rememberInfiniteTransition(label = "fan")
val floats = listOf(9_000, 11_000, 13_000).map { ms ->
transition.animateFloat(0f, 1f, infiniteRepeatable(tween(ms, easing = FastOutSlowInEasing), RepeatMode.Reverse), label = "float")
}
val clock = remember { androidx.compose.runtime.mutableLongStateOf(0L) }
AmbientFrames { clock.longValue += it }
val floatMs = listOf(9_000, 11_000, 13_000)
val still = Motion.scale == 0f
val slots = listOf(
FanSlot(0.54f, 0.14f, 0.62f, -9f),
@@ -331,7 +350,7 @@ private fun CapsuleFan(games: List<Library.SteamGame>) {
for (i in games.indices.reversed()) {
val slot = slots[i]
val h = maxHeight * slot.height
val v: State<Float> = floats[i]
val ms = floatMs[i]
val look = WallCapsule(artBrush(hueOf(games[i].name)), games[i].art)
val art = look.art?.let { rememberCapsuleArt(it, with(density) { Size((h * 2f / 3f).toPx(), h.toPx()) }, radius) }
Spacer(
@@ -339,7 +358,10 @@ private fun CapsuleFan(games: List<Library.SteamGame>) {
.offset(x = maxWidth * slot.x, y = maxHeight * slot.y)
.size(h * 2f / 3f, h)
.graphicsLayer {
val t = if (still) 0f else v.value
val t = if (still) 0f else {
val phase = (clock.longValue / (ms * 1_000_000.0 * Motion.scale)) % 2.0
FastOutSlowInEasing.transform((if (phase < 1.0) phase else 2.0 - phase).toFloat())
}
rotationZ = slot.lean + 2f * t
translationY = -12.dp.toPx() * t
shadowElevation = 24.dp.toPx()