feat(hud): long press collapses or expands the HUD, locking becomes an option (#2723)

* feat(hud): long press collapses or expands the HUD, locking becomes an option

* fix(hud): keep the collapsing tab at the press point while layout runs

The layout listener could run keepOnScreen during the dock animation and replace the press-centered target with the anchored edge position, which was then saved as the tab position. The dock animation now holds the same guard the other HUD animations use until the dock is committed.
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-16 15:29:17 -05:00
1 parent 8e514ada62
commit fde3089900
41 files changed
+369 -51

No files matched your search

@@ -0,0 +1,99 @@
package com.droiddeck.launcher.hud
import android.os.Looper
import android.os.SystemClock
import android.provider.Settings
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.FrameLayout
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.ui.Motion
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import java.time.Duration
import java.util.function.BiConsumer
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33], qualifiers = "xhdpi")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class DroidDeckHudHostCollapseTest {
private fun animate(scale: Float) {
val app = RuntimeEnvironment.getApplication()
Settings.Global.putFloat(app.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, scale)
Motion.refresh(app)
}
@Before fun noAnimation() = animate(0f)
@After fun restoreAnimation() = animate(1f)
private fun collapse(snap: String, hudX: Float, hudY: Float, pressX: Float, pressY: Float, layoutDuringCollapse: Boolean = false): Triple<DroidDeckHudView, String, Pair<Float, Float>> {
val app = RuntimeEnvironment.getApplication()
SessionPrefs.setDroidDeckHudConfig(app, DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudSnap" to snap) + ("hudSize" to "full")))
SessionPrefs.setDroidDeckHudPosition(app, hudX, hudY)
val root = FrameLayout(app)
val host = DroidDeckHudHost(app)
host.attach(root, 0)
val hud = root.getChildAt(0) as DroidDeckHudView
hud.visibility = View.VISIBLE
hud.showForTest(DroidDeckHudPanelTest.SNAPSHOT, "DXVK", 0L)
val exactly = { size: Int -> View.MeasureSpec.makeMeasureSpec(size, View.MeasureSpec.EXACTLY) }
root.measure(exactly(2000), exactly(1000))
root.layout(0, 0, 2000, 1000)
hud.x = hudX; hud.y = hudY
if (layoutDuringCollapse) {
@Suppress("UNCHECKED_CAST")
val collapse = DroidDeckHudView::class.java.getDeclaredField("onHoldListener").also { it.isAccessible = true }.get(hud) as BiConsumer<Float, Float>
hud.setOnHoldListener { x, y ->
collapse.accept(x, y)
hud.requestLayout()
root.measure(exactly(2000), exactly(1000))
root.layout(0, 0, 2000, 1000)
}
}
val down = SystemClock.uptimeMillis()
MotionEvent.obtain(down, down, MotionEvent.ACTION_DOWN, pressX, pressY, 0).also { root.dispatchTouchEvent(it); it.recycle() }
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ViewConfiguration.getLongPressTimeout() + 300L))
MotionEvent.obtain(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, pressX, pressY, 0).also { root.dispatchTouchEvent(it); it.recycle() }
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1000))
root.measure(exactly(2000), exactly(1000))
root.layout(0, 0, 2000, 1000)
shadowOf(Looper.getMainLooper()).idle()
val stored = DroidDeckHudConfig.load(app)["hudDock"].orEmpty()
return Triple(hud, stored, hud.x to hud.y)
}
@Test fun withSnappingTheHudCollapsesIntoTheEdgeNearestWhereThePressBegan() {
assertEquals("top", collapse("1", 400f, 40f, 500f, 60f).second)
assertEquals("left", collapse("1", 20f, 300f, 40f, 500f).second)
assertEquals("right", collapse("1", 1500f, 300f, 1900f, 500f).second)
assertEquals("bottom", collapse("1", 400f, 700f, 500f, 980f).second)
}
@Test fun withoutSnappingTheHudCollapsesWhereItWasTouched() {
val (hud, stored, at) = collapse("0", 600f, 300f, 700f, 350f)
assertEquals("float", stored)
assertEquals(DroidDeckHudDock.FLOAT, hud.dock)
val (tabW, tabH) = hud.dockTabSize()
assertEquals(700f - tabW / 2f, at.first, 2f)
assertEquals(350f - tabH / 2f, at.second, 2f)
}
@Test fun aLayoutPassDuringTheCollapseKeepsTheTabWhereThePressPutIt() {
animate(1f)
val (hud, stored, at) = collapse("1", 600f, 300f, 700f, 350f, layoutDuringCollapse = true)
val (tabW, tabH) = hud.dockTabSize()
assertEquals("top", stored)
assertEquals(700f - tabW / 2f, SessionPrefs.droidDeckHudPosition(RuntimeEnvironment.getApplication())!!.first, 2f)
assertEquals(700f - tabW / 2f, at.first, 2f)
}
}
@@ -38,6 +38,74 @@ class DroidDeckHudPanelTest {
assertEquals("1", DroidDeckHudConfig.DEFAULTS["hudLocked"])
}
@Test fun aLongPressCollapsesTheHudUnlessTheUserChoseToLockWithIt() {
assertEquals("collapse", DroidDeckHudConfig.DEFAULTS["hudHold"])
for ((hold, locked) in listOf("collapse" to "1", "collapse" to "0", "lock" to "1", "lock" to "0")) {
val app = RuntimeEnvironment.getApplication()
val hud = DroidDeckHudView(app)
val parent = FrameLayout(app)
parent.addView(hud, FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT))
hud.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudHold" to hold) + ("hudLocked" to locked)))
hud.showForTest(SNAPSHOT, "DXVK", 0L)
val exactly = View.MeasureSpec.makeMeasureSpec(2000, View.MeasureSpec.EXACTLY)
parent.measure(exactly, exactly)
parent.layout(0, 0, 2000, 2000)
val holds = ArrayList<Pair<Float, Float>>()
val locks = ArrayList<Boolean>()
hud.setOnHoldListener { x, y -> holds += x to y }
hud.setOnLockChangedListener { locks += it }
val down = SystemClock.uptimeMillis()
MotionEvent.obtain(down, down, MotionEvent.ACTION_DOWN, 30f, 20f, 0).also { hud.dispatchTouchEvent(it); it.recycle() }
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ViewConfiguration.getLongPressTimeout() + 300L))
MotionEvent.obtain(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 30f, 20f, 0).also { hud.dispatchTouchEvent(it); it.recycle() }
shadowOf(Looper.getMainLooper()).idle()
if (hold == "collapse") {
assertEquals("$hold/$locked collapses from where the press began", listOf(30f to 20f), holds)
assertTrue("$hold/$locked leaves the lock alone", locks.isEmpty())
} else {
assertTrue("$hold/$locked does not collapse", holds.isEmpty())
assertEquals("$hold/$locked toggles the lock", listOf(locked != "1"), locks)
}
}
}
@Test fun releasingTheFingerThatCollapsedTheHudDoesNotExpandItAgain() {
val app = RuntimeEnvironment.getApplication()
val hud = DroidDeckHudView(app)
val parent = FrameLayout(app)
parent.addView(hud, FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT))
val config = DroidDeckHudConfig.DEFAULTS + ("hudSize" to "full")
hud.applyConfig(DroidDeckHudConfig.encode(config))
hud.showForTest(SNAPSHOT, "DXVK", 0L)
val exactly = View.MeasureSpec.makeMeasureSpec(2000, View.MeasureSpec.EXACTLY)
parent.measure(exactly, exactly)
parent.layout(0, 0, 2000, 2000)
val docks = ArrayList<String>()
hud.setOnDockChangedListener { docks += it }
hud.setOnHoldListener { _, _ -> hud.applyConfig(DroidDeckHudConfig.encode(config + ("hudDock" to "float"))) }
val down = SystemClock.uptimeMillis()
MotionEvent.obtain(down, down, MotionEvent.ACTION_DOWN, 30f, 20f, 0).also { hud.dispatchTouchEvent(it); it.recycle() }
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ViewConfiguration.getLongPressTimeout() + 300L))
assertEquals(DroidDeckHudDock.FLOAT, hud.dock)
MotionEvent.obtain(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 30f, 20f, 0).also { hud.dispatchTouchEvent(it); it.recycle() }
shadowOf(Looper.getMainLooper()).idle()
assertTrue("the release of the collapsing press is not a tap on the tab: $docks", docks.isEmpty())
val next = SystemClock.uptimeMillis()
MotionEvent.obtain(next, next, MotionEvent.ACTION_DOWN, 5f, 5f, 0).also { hud.dispatchTouchEvent(it); it.recycle() }
MotionEvent.obtain(next, next + 40, MotionEvent.ACTION_UP, 5f, 5f, 0).also { hud.dispatchTouchEvent(it); it.recycle() }
assertEquals("a later tap on the tab expands it", listOf("none"), docks)
}
@Test fun aFloatingTabIsATabWithoutAnEdge() {
assertEquals(DroidDeckHudDock.FLOAT, DroidDeckHudDock.from("float"))
val hud = DroidDeckHudView(RuntimeEnvironment.getApplication())
hud.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudSize" to "full") + ("hudDock" to "float")))
hud.showForTest(SNAPSHOT, "DXVK", 0L)
val unspecified = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
hud.measure(unspecified, unspecified)
assertTrue("a tab, not the panel: ${hud.measuredWidth}x${hud.measuredHeight}", hud.measuredWidth < 400 && hud.measuredHeight < 200)
}
@Test fun lowMetricsCanBeHiddenIndependentlyInEveryLayoutThatShowsThem() {
val metrics = listOf("showLow1" to "1%", "showLow01" to "0.1%", "showLow001" to "0.01%")
for (size in DroidDeckHudSize.values().filter { it != DroidDeckHudSize.PILL }) {
@@ -331,7 +399,7 @@ class DroidDeckHudPanelTest {
hud.passesTouchAt = { _, _ -> control }
val locks = ArrayList<Boolean>()
hud.setOnLockChangedListener { locks += it }
hud.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudLocked" to "0")))
hud.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudLocked" to "0") + ("hudHold" to "lock")))
hud.showForTest(SNAPSHOT, "DXVK", 0L)
val exactly = View.MeasureSpec.makeMeasureSpec(2000, View.MeasureSpec.EXACTLY)
parent.measure(exactly, exactly)
@@ -348,7 +416,7 @@ class DroidDeckHudPanelTest {
control = true
touch()
assertEquals("an unlocked HUD keeps the touch (tap, drag, long press)", 0, reached)
hud.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudLocked" to "1")))
hud.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudLocked" to "1") + ("hudHold" to "lock")))
touch()
assertEquals("a locked HUD passes it to the control under it", 1, reached)
control = false
@@ -377,7 +445,7 @@ class DroidDeckHudPanelTest {
assertEquals("a locked, docked HUD still answers a tap", listOf("none"), docks)
}
private companion object {
internal companion object {
val SNAPSHOT = HudMetrics.Snapshot(64, 52, 3302, 1050, 89, 79, 10_800_000_000L, 16_000_000_000L, 67f,
1_800_000_000L, HudMetrics.Battery(16f, false, 79, 34, null),
intArrayOf(91, 91, 48, 50, 48, 19, 17, 73), intArrayOf(2265, 2265, 3148, 3148, 3148, 2956, 2956, 3302),