Compare commits

...
3 changed files with 159 additions and 46 deletions

No files matched your search

@@ -267,11 +267,12 @@ class DroidDeckHudHost(context: Context) {
val h = if (strip) maxOf(v.height.toFloat(), STRIP_PULL_OFF_DP * density) else v.height.toFloat()
val x = v.x + dx
val y = v.y + dy
val b = HudBounds.of(p, v.width.toFloat(), v.height.toFloat())
return mapOf(
DroidDeckHudDock.LEFT to -x / w,
DroidDeckHudDock.RIGHT to (x + v.width - p.width) / w,
DroidDeckHudDock.TOP to -y / h,
DroidDeckHudDock.BOTTOM to (y + v.height - p.height) / h,
DroidDeckHudDock.LEFT to (b.left - x) / w,
DroidDeckHudDock.RIGHT to (x + v.width - b.right) / w,
DroidDeckHudDock.TOP to (b.top - y) / h,
DroidDeckHudDock.BOTTOM to (y + v.height - b.bottom) / h,
).maxByOrNull { it.value }
}
@@ -279,19 +280,19 @@ class DroidDeckHudHost(context: Context) {
val p = hud.parent as? View ?: return
stopMotion(hud)
snapping = false
hud.x = x.coerceIn(0f, (p.width - hud.width).toFloat().coerceAtLeast(0f))
hud.y = y.coerceIn(0f, (p.height - hud.height).toFloat().coerceAtLeast(0f))
val b = HudBounds.of(p, hud.width.toFloat(), hud.height.toFloat())
hud.x = x.coerceIn(b.left, b.maxX)
hud.y = y.coerceIn(b.top, b.maxY)
}
private fun dropTab(hud: DroidDeckHudView) {
val p = hud.parent as? View ?: return
val maxX = (p.width - hud.width).toFloat().coerceAtLeast(0f)
val maxY = (p.height - hud.height).toFloat().coerceAtLeast(0f)
val b = HudBounds.of(p, hud.width.toFloat(), hud.height.toFloat())
val pulled = when (hud.dock) {
DroidDeckHudDock.LEFT -> hud.x / hud.width
DroidDeckHudDock.RIGHT -> (maxX - hud.x) / hud.width
DroidDeckHudDock.TOP -> hud.y / hud.height
DroidDeckHudDock.BOTTOM -> (maxY - hud.y) / hud.height
DroidDeckHudDock.LEFT -> (hud.x - b.left) / hud.width
DroidDeckHudDock.RIGHT -> (b.maxX - hud.x) / hud.width
DroidDeckHudDock.TOP -> (hud.y - b.top) / hud.height
DroidDeckHudDock.BOTTOM -> (b.maxY - hud.y) / hud.height
DroidDeckHudDock.NONE, DroidDeckHudDock.FLOAT -> 0f
}
if (pulled >= 1f) { setDock(hud, DroidDeckHudDock.NONE); return }
@@ -339,12 +340,11 @@ class DroidDeckHudHost(context: Context) {
/** Sends the HUD to a side the way a chat head goes: by where the throw would carry it, springing on from its speed. */
private fun snapToAnchor(hud: DroidDeckHudView, vx: Float, vy: Float) {
val p = hud.parent as? View ?: return
val maxX = (p.width - hud.width).toFloat().coerceAtLeast(0f)
val maxY = (p.height - hud.height).toFloat().coerceAtLeast(0f)
val right = com.droiddeck.launcher.ui.EdgeFling.toRight(hud.x + hud.width / 2f, p.width.toFloat(), vx, context.resources.displayMetrics.density)
val b = HudBounds.of(p, hud.width.toFloat(), hud.height.toFloat())
val right = com.droiddeck.launcher.ui.EdgeFling.toRight(hud.x - b.left + hud.width / 2f, b.right - b.left, vx, context.resources.displayMetrics.density)
anchorX = if (right) 1f else 0f
wantX = anchorX * maxX
wantY = com.droiddeck.launcher.ui.EdgeFling.restY(hud.y, vy, 0f, maxY)
wantX = b.anchoredX(anchorX)
wantY = com.droiddeck.launcher.ui.EdgeFling.restY(hud.y, vy, b.top, b.maxY)
SessionPrefs.setDroidDeckHudPosition(context, wantX, wantY)
stopMotion(hud)
hud.scaleX = 1f; hud.scaleY = 1f
@@ -387,7 +387,8 @@ class DroidDeckHudHost(context: Context) {
val at = IntArray(2).also { p.getLocationOnScreen(it) }
val x = rawX - at[0]
val y = rawY - at[1]
val edge = if (snaps) nearestEdge(x, y, p.width.toFloat(), p.height.toFloat()) else DroidDeckHudDock.FLOAT
val b = HudBounds.of(p, 0f, 0f)
val edge = if (snaps) nearestEdge(x - b.left, y - b.top, b.right - b.left, b.bottom - b.top) else DroidDeckHudDock.FLOAT
setDock(hud, edge, Pair(x, y))
}
@@ -404,8 +405,9 @@ class DroidDeckHudHost(context: Context) {
if (edge != DroidDeckHudDock.NONE) {
val p = hud.parent as? View
if (p != null) {
wantX = wantX.coerceIn(0f, p.width.toFloat())
wantY = wantY.coerceIn(0f, p.height.toFloat())
val b = HudBounds.of(p, 0f, 0f)
wantX = wantX.coerceIn(b.left, b.right)
wantY = wantY.coerceIn(b.top, b.bottom)
}
SessionPrefs.setDroidDeckHudPosition(context, wantX, wantY)
}
@@ -417,12 +419,11 @@ class DroidDeckHudHost(context: Context) {
}
private fun settleOutOfTab(hud: DroidDeckHudView, tabX: Float, tabY: Float) {
val p = hud.parent as? View
val maxX = if (p == null) Float.MAX_VALUE else (p.width - hud.contentWidth).coerceAtLeast(0f)
val maxY = if (p == null) Float.MAX_VALUE else (p.height - hud.contentHeight).coerceAtLeast(0f)
wantX = tabX.coerceIn(0f, maxX); wantY = tabY.coerceIn(0f, maxY)
anchorX = if (snaps && !hud.isStrip && p != null) (if (wantX < maxX / 2f) 0f else 1f) else Float.NaN
if (!anchorX.isNaN()) wantX = anchorX * maxX
val b = (hud.parent as? View)?.let { HudBounds.of(it, hud.contentWidth, hud.contentHeight) }
wantX = if (b == null) tabX.coerceAtLeast(0f) else tabX.coerceIn(b.left, b.maxX)
wantY = if (b == null) tabY.coerceAtLeast(0f) else tabY.coerceIn(b.top, b.maxY)
anchorX = if (snaps && !hud.isStrip && b != null) b.anchorFor(wantX) else Float.NaN
if (b != null && !anchorX.isNaN()) wantX = b.anchoredX(anchorX)
SessionPrefs.setDroidDeckHudPosition(context, wantX, wantY)
}
@@ -493,13 +494,13 @@ class DroidDeckHudHost(context: Context) {
if (scale > 0.999f) from else (to - from * scale) / (1f - scale)
private fun settled(p: View, dock: DroidDeckHudDock, w: Float, h: Float): Pair<Float, Float> {
val maxX = (p.width - w).coerceAtLeast(0f); val maxY = (p.height - h).coerceAtLeast(0f)
var x = wantX.coerceIn(0f, maxX); var y = wantY.coerceIn(0f, maxY)
val b = HudBounds.of(p, w, h)
var x = wantX.coerceIn(b.left, b.maxX); var y = wantY.coerceIn(b.top, b.maxY)
when (dock) {
DroidDeckHudDock.LEFT -> x = 0f
DroidDeckHudDock.RIGHT -> x = maxX
DroidDeckHudDock.TOP -> y = 0f
DroidDeckHudDock.BOTTOM -> y = maxY
DroidDeckHudDock.LEFT -> x = b.left
DroidDeckHudDock.RIGHT -> x = b.maxX
DroidDeckHudDock.TOP -> y = b.top
DroidDeckHudDock.BOTTOM -> y = b.maxY
DroidDeckHudDock.NONE, DroidDeckHudDock.FLOAT -> {}
}
return Pair(x, y)
@@ -510,21 +511,18 @@ class DroidDeckHudHost(context: Context) {
val p = v.parent as? View ?: return
if (p.width <= 0 || p.height <= 0 || v.width <= 0 || v.height <= 0) return
if (wantX.isNaN()) { wantX = v.x; wantY = v.y }
val maxX = (p.width - v.width).toFloat().coerceAtLeast(0f)
val maxY = (p.height - v.height).toFloat().coerceAtLeast(0f)
val b = HudBounds.of(p, v.width.toFloat(), v.height.toFloat())
val anchored = (v as? DroidDeckHudView)?.let { it.dock == DroidDeckHudDock.NONE && !it.isStrip } == true
if (snaps && anchored && anchorX.isNaN()) {
anchorX = if (wantX.coerceIn(0f, maxX) < maxX / 2f) 0f else 1f
}
if (snaps && anchored && !anchorX.isNaN()) wantX = anchorX * maxX
var x = wantX.coerceIn(0f, maxX)
var y = wantY.coerceIn(0f, maxY)
if ((v as? DroidDeckHudView)?.isStrip == true) y = if (y + v.height / 2f < p.height / 2f) 0f else maxY
if (snaps && anchored && anchorX.isNaN()) anchorX = b.anchorFor(wantX)
if (snaps && anchored && !anchorX.isNaN()) wantX = b.anchoredX(anchorX)
var x = wantX.coerceIn(b.left, b.maxX)
var y = wantY.coerceIn(b.top, b.maxY)
if ((v as? DroidDeckHudView)?.isStrip == true) y = if (y + v.height / 2f < (b.top + b.bottom) / 2f) b.top else b.maxY
when (v.let { it as? DroidDeckHudView }?.dock) {
DroidDeckHudDock.LEFT -> x = 0f
DroidDeckHudDock.RIGHT -> x = maxX
DroidDeckHudDock.TOP -> y = 0f
DroidDeckHudDock.BOTTOM -> y = maxY
DroidDeckHudDock.LEFT -> x = b.left
DroidDeckHudDock.RIGHT -> x = b.maxX
DroidDeckHudDock.TOP -> y = b.top
DroidDeckHudDock.BOTTOM -> y = b.maxY
else -> {}
}
if (v.x != x) v.x = x
@@ -551,3 +549,25 @@ class DroidDeckHudHost(context: Context) {
}.getOrNull()?.takeIf { it.isNotBlank() }
}
}
internal class HudBounds(val left: Float, val top: Float, val right: Float, val bottom: Float, w: Float, h: Float) {
val maxX = (right - w).coerceAtLeast(left)
val maxY = (bottom - h).coerceAtLeast(top)
fun anchorFor(x: Float): Float = if (x.coerceIn(left, maxX) - left < (maxX - left) / 2f) 0f else 1f
fun anchoredX(anchor: Float): Float = left + anchor * (maxX - left)
companion object {
fun of(p: View, w: Float, h: Float): HudBounds {
val left = p.paddingLeft.toFloat()
val top = p.paddingTop.toFloat()
return HudBounds(
left, top,
(p.width - p.paddingRight).toFloat().coerceAtLeast(left),
(p.height - p.paddingBottom).toFloat().coerceAtLeast(top),
w, h,
)
}
}
}
@@ -934,7 +934,7 @@ class DroidDeckHudView(context: Context) : View(context) {
val rowPx = sp(11.5f); val unitPx = rowPx * 0.8f; val fpsPx = sp(15f)
val padX = sp(10f); val groupGap = sp(13f)
val h = sp(26f)
val w = ((parent as? View)?.width?.takeIf { it > 0 } ?: resources.displayMetrics.widthPixels).toFloat()
val w = ((parent as? View)?.let { it.width - it.paddingLeft - it.paddingRight }?.takeIf { it > 0 } ?: resources.displayMetrics.widthPixels).toFloat()
val mid = h / 2f
val baseline = mid + capHeight(fpsPx) / 2f
@@ -0,0 +1,93 @@
package com.droiddeck.launcher.hud
import android.os.Looper
import android.provider.Settings
import android.view.View
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
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33], qualifiers = "xhdpi")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class DroidDeckHudHostInsetTest {
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 show(size: String, x: Float, y: Float, padLeft: Int, padRight: Int): DroidDeckHudView {
val app = RuntimeEnvironment.getApplication()
SessionPrefs.setDroidDeckHudConfig(app, DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudSnap" to "1") + ("hudSize" to size)))
SessionPrefs.setDroidDeckHudPosition(app, x, y)
val root = FrameLayout(app)
root.setPadding(padLeft, 0, padRight, 0)
DroidDeckHudHost(app).attach(root, 0)
val hud = root.getChildAt(0) as DroidDeckHudView
hud.visibility = View.VISIBLE
val exactly = { s: Int -> View.MeasureSpec.makeMeasureSpec(s, View.MeasureSpec.EXACTLY) }
root.measure(exactly(2000), exactly(1000))
root.layout(0, 0, 2000, 1000)
hud.showForTest(DroidDeckHudPanelTest.SNAPSHOT, "DXVK", 0L)
hud.requestLayout()
root.measure(exactly(2000), exactly(1000))
root.layout(0, 0, 2000, 1000)
shadowOf(Looper.getMainLooper()).idle()
return hud
}
@Test fun theStripSpansOnlyTheAreaInsideTheNotchPadding() {
val hud = show("strip", 0f, 0f, padLeft = 120, padRight = 40)
assertEquals(120f, hud.x, 0.5f)
assertEquals(2000 - 120 - 40, hud.width)
}
@Test fun aHudSnappedLeftSitsBesideTheNotchInsteadOfUnderIt() {
val hud = show("full", 0f, 300f, padLeft = 120, padRight = 0)
assertEquals(120f, hud.x, 0.5f)
}
@Test fun aHudSnappedRightStopsAtTheRightPadding() {
val hud = show("full", 1990f, 300f, padLeft = 0, padRight = 90)
assertEquals(2000f - 90f, hud.x + hud.width, 0.5f)
}
@Test fun withoutPaddingTheHudStillReachesTheScreenEdges() {
assertEquals(0f, show("full", 0f, 300f, 0, 0).x, 0.5f)
val strip = show("strip", 0f, 0f, 0, 0)
assertEquals(0f, strip.x, 0.5f)
assertEquals(2000, strip.width)
}
@Test fun boundsAnchorBetweenThePaddedEdges() {
val b = HudBounds(120f, 0f, 2000f, 1000f, 400f, 200f)
assertEquals(1600f, b.maxX, 0f)
assertEquals(800f, b.maxY, 0f)
assertEquals(120f, b.anchoredX(0f), 0f)
assertEquals(1600f, b.anchoredX(1f), 0f)
assertEquals(0f, b.anchorFor(0f), 0f)
assertEquals(0f, b.anchorFor(850f), 0f)
assertEquals(1f, b.anchorFor(870f), 0f)
}
@Test fun boundsNeverInvertWhenTheHudIsWiderThanTheArea() {
val b = HudBounds(120f, 50f, 600f, 300f, 900f, 900f)
assertEquals(120f, b.maxX, 0f)
assertEquals(50f, b.maxY, 0f)
}
}