Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
App windows: give every app a screen shape it knows
Bee31380e0133d4
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.kt b/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.ktindex a0dfad4..d5d8572 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/mirror/AppWindows.kt@@ -31,7 +31,6 @@ import java.util.concurrent.ConcurrentHashMapimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.launch-import kotlin.math.maximport kotlin.math.roundToInt/**@@ -162,11 +161,8 @@ class AppWindows(/** The PC resized an app window: fit to encoder limits and tell the helper. */fun resize(pc: String, session: UInt, width: Int, height: Int) {val window = windows[pc to session] ?: return- val w = width.coerceAtLeast(240)- val h = height.coerceAtLeast(240)- val scale = minOf(1f, window.maxSize.toFloat() / max(w, h))- fun fit(v: Int) = max(240, ((v * scale) / 16).roundToInt() * 16)- val (newW, newH) = fit(w) to fit(h)+ val (phoneW, phoneH) = screen()+ val (newW, newH) = WindowShape.fit(width, height, window.maxSize, phoneW, phoneH)if (newW == window.width && newH == window.height) returnwindow.width = newWwindow.height = newH@@ -187,10 +183,10 @@ class AppWindows(Elevated.handleOn(window.display, window.width.toFloat(), window.height.toFloat(), event)}- /** The phone's screen size scaled to fit [maxSize] (multiples of 16, for encoders), and its density likewise. */- private fun size(maxSize: Int): Triple<Int, Int, Int> {+ /** The phone's screen, in pixels. */+ private fun screen(): Pair<Int, Int> {val windows = context.getSystemService(WindowManager::class.java)- val (w, h) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {windows.maximumWindowMetrics.bounds.let { it.width() to it.height() }} else {val metrics = android.util.DisplayMetrics()@@ -198,12 +194,16 @@ class AppWindows(windows.defaultDisplay.getRealMetrics(metrics)metrics.widthPixels to metrics.heightPixels}- // Upright, like a phone held normally.+ }++ /** The phone's screen shape scaled to fit [maxSize] (see [WindowShape]), upright, and its density likewise. */+ private fun size(maxSize: Int): Triple<Int, Int, Int> {+ val (w, h) = screen()val (shortSide, longSide) = minOf(w, h) to maxOf(w, h)- val scale = minOf(1f, maxSize.coerceAtLeast(320).toFloat() / longSide)- fun fit(v: Int) = max(16, ((v * scale) / 16).toInt() * 16)+ val (fitW, fitH) = WindowShape.fit(shortSide, longSide, maxSize.coerceAtLeast(320), w, h)+ val scale = fitH.toFloat() / longSideval dpi = (context.resources.displayMetrics.densityDpi * scale).roundToInt().coerceIn(120, 640)- return Triple(fit(shortSide), fit(longSide), dpi)+ return Triple(fitW, fitH, dpi)}/** The PC turned keeping the phone awake, or its screen off, on or off. */diff --git a/android/app/src/main/kotlin/app/nectarlink/android/mirror/WindowShape.kt b/android/app/src/main/kotlin/app/nectarlink/android/mirror/WindowShape.ktnew file mode 100644index 0000000..acc84c4--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/mirror/WindowShape.kt@@ -0,0 +1,59 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.mirror++import kotlin.math.abs+import kotlin.math.ln++/**+ * The size of an app window's display: an exact, common screen shape, close+ * to the window's. Apps check their display's shape against the ones they+ * know (Samsung's Camera throws on 880x1920, the phone's 1080x2340 rounded+ * to encoder-friendly sizes), so a window never gets an odd one; the PC+ * shows the picture letterboxed in what is left over.+ */+object WindowShape {+ /** Sides are multiples of this, for encoders. */+ private const val ALIGN = 16+ private const val MIN_SIDE = 240++ /** Common shapes, as long side : short side. */+ private val COMMON = listOf(1 to 1, 4 to 3, 3 to 2, 16 to 10, 16 to 9, 2 to 1, 20 to 9, 21 to 9)++ /**+ * The largest display of a common shape (or the phone's own, [phoneW] x+ * [phoneH]) that fits [width] x [height] scaled down to [maxSize], in the+ * same orientation, with sides that are multiples of 16.+ */+ fun fit(width: Int, height: Int, maxSize: Int, phoneW: Int, phoneH: Int): Pair<Int, Int> {+ val w = width.coerceAtLeast(MIN_SIDE)+ val h = height.coerceAtLeast(MIN_SIDE)+ val scale = minOf(1.0, maxSize.toDouble() / maxOf(w, h))+ val (boxW, boxH) = w * scale to h * scale+ val portrait = h > w+ val wanted = maxOf(w, h).toDouble() / minOf(w, h)+ val shape = shapes(phoneW, phoneH).minBy { (long, short) -> abs(ln(long.toDouble() / short / wanted)) }+ // One step of the shape: the smallest exact size with aligned sides.+ val step = ALIGN / gcd(ALIGN, gcd(shape.first, shape.second))+ val (unitLong, unitShort) = shape.first * step to shape.second * step+ val (unitW, unitH) = if (portrait) unitShort to unitLong else unitLong to unitShort+ val n = minOf(boxW / unitW, boxH / unitH).toInt()+ val minN = (MIN_SIDE + minOf(unitW, unitH) - 1) / minOf(unitW, unitH)+ val count = n.coerceAtLeast(minN.coerceAtLeast(1))+ return unitW * count to unitH * count+ }++ /** [COMMON] and the phone's own shape, each reduced (long : short). */+ private fun shapes(phoneW: Int, phoneH: Int): List<Pair<Int, Int>> {+ val own = if (phoneW > 0 && phoneH > 0) {+ val (long, short) = maxOf(phoneW, phoneH) to minOf(phoneW, phoneH)+ val g = gcd(long, short)+ listOf(long / g to short / g)+ } else {+ emptyList()+ }+ // A phone's odd shape (e.g. 2400:1081) would need a huge step; keep only reasonable ones.+ return (COMMON + own.filter { (long, short) -> long <= 64 && short <= 64 }).distinct()+ }++ private tailrec fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)+}diff --git a/android/app/src/test/kotlin/app/nectarlink/android/mirror/WindowShapeTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/mirror/WindowShapeTest.ktnew file mode 100644index 0000000..d4d812a--- /dev/null+++ b/android/app/src/test/kotlin/app/nectarlink/android/mirror/WindowShapeTest.kt@@ -0,0 +1,56 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.mirror++import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test++class WindowShapeTest {+ // A Galaxy A35's screen.+ private val phoneW = 1080+ private val phoneH = 2340++ private fun fit(w: Int, h: Int, max: Int = 1920) = WindowShape.fit(w, h, max, phoneW, phoneH)++ private fun assertAligned(size: Pair<Int, Int>) {+ assertEquals("width $size", 0, size.first % 16)+ assertEquals("height $size", 0, size.second % 16)+ }++ @Test+ fun aWindowLikeThePhoneGetsThePhonesExactShape() {+ // Rounding 1080x2340 to multiples of 16 gave 880x1920 (2.18), which+ // Samsung's Camera refused to start on.+ val size = fit(1080, 2340)+ assertEquals(864 to 1872, size)+ assertEquals(2340.0 / 1080, size.second.toDouble() / size.first, 1e-9)+ }++ @Test+ fun theCrashingSizesBecomeExactShapes() {+ for ((w, h) in listOf(1920 to 880, 1472 to 672, 672 to 1472)) {+ val (fw, fh) = fit(w, h)+ assertEquals("$w x $h keeps its orientation", w > h, fw > fh)+ assertEquals(13.0 / 6, maxOf(fw, fh).toDouble() / minOf(fw, fh), 1e-9)+ assertAligned(fw to fh)+ assertTrue("fits $w x $h", fw <= w && fh <= h)+ }+ }++ @Test+ fun commonShapesStayExact() {+ assertEquals(1792 to 1008, fit(1920, 1080)) // 16:9+ assertEquals(1024 to 768, fit(1024, 768)) // 4:3+ assertEquals(800 to 800, fit(810, 800)) // nearly square+ }++ @Test+ fun aLargeWindowIsScaledToTheLimitAndASmallOneStaysUsable() {+ val big = fit(3840, 2160, max = 1280)+ assertTrue(big.first <= 1280 && big.second <= 1280)+ assertEquals(16.0 / 9, big.first.toDouble() / big.second, 1e-9)+ val small = fit(100, 100)+ assertTrue(small.first >= 240 && small.second >= 240)+ assertAligned(small)+ }+}