mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
Keep oversized floating HUD panels from clipping (#3663)
Fit the entire floating panel to its viewport without changing saved HUD scale.
This commit is contained in:
1 parent
b81d7d796f
commit
2aad4fbb1b
2 files changed
+106
-8
No files matched your search
@@ -187,6 +187,12 @@ class DroidDeckHudView(context: Context) : View(context) {
|
||||
private var graphRect: RectF? = null
|
||||
private var contentW = 0f
|
||||
private var contentH = 0f
|
||||
private var viewportW = Float.POSITIVE_INFINITY
|
||||
private var viewportH = Float.POSITIVE_INFINITY
|
||||
|
||||
private val panelFit: Float
|
||||
get() = if (dPanel == null || contentW <= 0f || contentH <= 0f) 1f
|
||||
else minOf(1f, viewportW / contentW, viewportH / contentH)
|
||||
|
||||
private var onMovedListener: BiConsumer<Float, Float>? = null
|
||||
private var onSizeCycledListener: Consumer<String>? = null
|
||||
@@ -993,13 +999,20 @@ class DroidDeckHudView(context: Context) : View(context) {
|
||||
}
|
||||
|
||||
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
||||
viewportW = viewportLimit(widthMeasureSpec)
|
||||
viewportH = viewportLimit(heightMeasureSpec)
|
||||
if (stripRect != null) { setMeasuredDimension(contentW.roundToInt(), resolveSize(contentH.roundToInt(), heightMeasureSpec)); return }
|
||||
val fit = panelFit
|
||||
setMeasuredDimension(
|
||||
resolveSize(contentW.roundToInt(), widthMeasureSpec),
|
||||
resolveSize(contentH.roundToInt(), heightMeasureSpec),
|
||||
resolveSize((contentW * fit).roundToInt(), widthMeasureSpec),
|
||||
resolveSize((contentH * fit).roundToInt(), heightMeasureSpec),
|
||||
)
|
||||
}
|
||||
|
||||
private fun viewportLimit(spec: Int): Float =
|
||||
if (MeasureSpec.getMode(spec) == MeasureSpec.UNSPECIFIED) Float.POSITIVE_INFINITY
|
||||
else MeasureSpec.getSize(spec).toFloat()
|
||||
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
if (contentW <= 0f || contentH <= 0f) { lockController.drawBadge(canvas); return }
|
||||
val blend = if (dock == DroidDeckHudDock.NONE && previewEdge != DroidDeckHudDock.NONE) previewBlend else 0f
|
||||
@@ -1034,8 +1047,10 @@ class DroidDeckHudView(context: Context) : View(context) {
|
||||
}
|
||||
|
||||
private fun drawContent(canvas: Canvas) {
|
||||
|
||||
dPanel?.let { panel -> panelFrame(panel)?.let { canvas.drawBitmap(it, 0f, 0f, null) } }
|
||||
val fit = panelFit
|
||||
dPanel?.let { panel -> panelFrame(panel, fit)?.let { canvas.drawBitmap(it, 0f, 0f, null) } }
|
||||
val saved = canvas.save()
|
||||
if (fit != 1f) canvas.scale(fit, fit)
|
||||
|
||||
if (tileRects.isNotEmpty()) {
|
||||
tilePaint.color = Color.argb((14 * bgOpacity).roundToInt().coerceIn(8, 40), 255, 255, 255)
|
||||
@@ -1065,6 +1080,7 @@ class DroidDeckHudView(context: Context) : View(context) {
|
||||
if (stripRect != null && layerType != LAYER_TYPE_SOFTWARE) drawCachedGraph(canvas, it) else drawGraph(canvas, it)
|
||||
}
|
||||
|
||||
canvas.restoreToCount(saved)
|
||||
lockController.drawBadge(canvas)
|
||||
}
|
||||
|
||||
@@ -1177,13 +1193,14 @@ class DroidDeckHudView(context: Context) : View(context) {
|
||||
return if (dy <= 0f) p.right - stroke else if (dy >= r) cx else cx + sqrt(r * r - dy * dy)
|
||||
}
|
||||
|
||||
private fun panelFrame(panel: RectF): Bitmap? {
|
||||
private fun panelFrame(panel: RectF, fit: Float): Bitmap? {
|
||||
if (width <= 0 || height <= 0) return null
|
||||
val stroke = outlineWidth()
|
||||
val key = "$width,$height,$panel,$dRadius,$dDividerTop,$stroke,$accent,$surfaceRgb,$bgOpacity,${outlineColor()}"
|
||||
val key = "$width,$height,$fit,$panel,$dRadius,$dDividerTop,$stroke,$accent,$surfaceRgb,$bgOpacity,${outlineColor()}"
|
||||
if (key == dFrameKey) dFrame?.let { return it }
|
||||
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
||||
val c = Canvas(bmp)
|
||||
c.scale(fit, fit)
|
||||
val corner = sp(6f)
|
||||
val body = dShape(panel.left, panel.top, panel.right, panel.bottom, dRadius, corner)
|
||||
bgPaint.color = panelColor()
|
||||
@@ -1256,8 +1273,8 @@ class DroidDeckHudView(context: Context) : View(context) {
|
||||
|
||||
internal val isStrip: Boolean get() = stripRect != null
|
||||
|
||||
internal val contentWidth: Float get() = contentW
|
||||
internal val contentHeight: Float get() = contentH
|
||||
internal val contentWidth: Float get() = contentW * panelFit
|
||||
internal val contentHeight: Float get() = contentH * panelFit
|
||||
|
||||
internal val isDragging: Boolean get() = lockController.isDragging() || tabDragging
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.droiddeck.launcher.hud
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.view.View
|
||||
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
import android.widget.FrameLayout
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.GraphicsMode
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33], qualifiers = "xhdpi")
|
||||
@GraphicsMode(GraphicsMode.Mode.NATIVE)
|
||||
class DroidDeckHudClippingTest {
|
||||
@Test fun aLargerLayoutDrawsItsWholePanelAfterBeingMovedBackOnScreen() {
|
||||
val app = RuntimeEnvironment.getApplication()
|
||||
val root = FrameLayout(app)
|
||||
val hud = DroidDeckHudView(app)
|
||||
root.addView(hud, FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT))
|
||||
val config = DroidDeckHudConfig.DEFAULTS + ("hudScale" to "200")
|
||||
fun select(size: String) {
|
||||
hud.applyConfig(DroidDeckHudConfig.encode(config + ("hudSize" to size)))
|
||||
hud.showForTest(DroidDeckHudPanelTest.SNAPSHOT, "DXVK", 0L)
|
||||
}
|
||||
fun layout(w: Int, h: Int) {
|
||||
root.measure(View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY),
|
||||
View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY))
|
||||
root.layout(0, 0, w, h)
|
||||
}
|
||||
fun frame() = Bitmap.createBitmap(root.width, root.height, Bitmap.Config.ARGB_8888)
|
||||
.also { root.draw(Canvas(it)) }
|
||||
|
||||
select("tiles")
|
||||
layout(3000, 2000)
|
||||
val naturalWidth = hud.width
|
||||
val naturalHeight = hud.height
|
||||
val viewportWidth = naturalWidth + 30
|
||||
val viewportHeight = naturalHeight / 2
|
||||
|
||||
select("pill")
|
||||
layout(viewportWidth, viewportHeight)
|
||||
hud.y = viewportHeight - 10f
|
||||
select("tiles")
|
||||
layout(viewportWidth, viewportHeight)
|
||||
hud.x = 10f
|
||||
hud.y = 0f
|
||||
|
||||
val afterMove = frame()
|
||||
val middle = hud.x.toInt() + hud.width / 2
|
||||
assertTrue("the HUD is drawn", Color.alpha(afterMove.getPixel(middle, hud.height / 2)) > 0)
|
||||
val lastDrawnRow = (hud.height - 1 downTo 0).first { Color.alpha(afterMove.getPixel(middle, it)) > 0 }
|
||||
assertTrue("the bottom edge and its outer inset are visible after the move; cutoff row $lastDrawnRow",
|
||||
lastDrawnRow < hud.height - 1)
|
||||
assertTrue("vertical fitting preserves the panel aspect instead of clipping it", hud.width < naturalWidth)
|
||||
|
||||
layout(3000, 2000)
|
||||
assertEquals(naturalWidth, hud.width)
|
||||
assertEquals(naturalHeight, hud.height)
|
||||
}
|
||||
|
||||
@Test fun aNarrowViewportFitsBothDimensionsAndKeepsThePanelAspect() {
|
||||
val hud = DroidDeckHudView(RuntimeEnvironment.getApplication())
|
||||
hud.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudSize" to "tiles")))
|
||||
hud.showForTest(DroidDeckHudPanelTest.SNAPSHOT, "DXVK", 0L)
|
||||
val unlimited = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
|
||||
hud.measure(unlimited, unlimited)
|
||||
val naturalWidth = hud.measuredWidth
|
||||
val naturalHeight = hud.measuredHeight
|
||||
val narrow = View.MeasureSpec.makeMeasureSpec(naturalWidth / 2, View.MeasureSpec.AT_MOST)
|
||||
hud.measure(narrow, unlimited)
|
||||
assertEquals(naturalWidth / 2, hud.measuredWidth)
|
||||
assertEquals(naturalHeight / 2f, hud.measuredHeight.toFloat(), 2f)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user