Hand the gesture bar's height to the page
Browse files at this commit206f72a97bdfd3baa0eb6a1a6f00d97b55dbe9c9Parent: 57ff7592 changed files +45 -1
frontendmodified+1 -1
| @@ -1 +1 @@ | |||
| 1 | -Subproject commit 3392c985678fa6f0b6a0abf5676ad7f0c718988d | 1 | +Subproject commit d92d9c6add1380d291341821e27fde0c80880294 |
src-tauri/gen/android/app/src/main/java/co/hezarfen/app/MainActivity.ktmodified+44 -0
| @@ -1,11 +1,55 @@ | |||
| 1 | package co.hezarfen.app | 1 | package co.hezarfen.app |
| 2 | 2 | ||
| 3 | import android.os.Bundle | 3 | import android.os.Bundle |
| 4 | +import android.webkit.WebView | ||
| 4 | import androidx.activity.enableEdgeToEdge | 5 | import androidx.activity.enableEdgeToEdge |
| 6 | +import androidx.core.view.ViewCompat | ||
| 7 | +import androidx.core.view.WindowInsetsCompat | ||
| 5 | 8 | ||
| 6 | class MainActivity : TauriActivity() { | 9 | class MainActivity : TauriActivity() { |
| 10 | + private var webView: WebView? = null | ||
| 11 | + | ||
| 7 | override fun onCreate(savedInstanceState: Bundle?) { | 12 | override fun onCreate(savedInstanceState: Bundle?) { |
| 8 | enableEdgeToEdge() | 13 | enableEdgeToEdge() |
| 9 | super.onCreate(savedInstanceState) | 14 | super.onCreate(savedInstanceState) |
| 10 | } | 15 | } |
| 16 | + | ||
| 17 | + // On Android, CSS env(safe-area-inset-*) only reports the display cutout, so | ||
| 18 | + // the page can see the camera notch but not the gesture bar — the bottom tab | ||
| 19 | + // bar would render underneath it. Only the system knows that height, so read | ||
| 20 | + // it here and hand it to the page as a custom property its layout can pad by. | ||
| 21 | + // Keeping the padding inside the page (rather than insetting the webview) | ||
| 22 | + // means the tab bar's own background covers the strip, so it stays the colour | ||
| 23 | + // of whichever theme the user picked in the app. | ||
| 24 | + // | ||
| 25 | + // The insets are read from the view rather than through an | ||
| 26 | + // OnApplyWindowInsetsListener on purpose: installing that listener takes over | ||
| 27 | + // the webview's inset handling, and the page then loses | ||
| 28 | + // env(safe-area-inset-top) — the cutout is only reported to CSS while the | ||
| 29 | + // webview applies the insets itself. | ||
| 30 | + override fun onWebViewCreate(webView: WebView) { | ||
| 31 | + this.webView = webView | ||
| 32 | + webView.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> publishBottomInset() } | ||
| 33 | + // The first layout can land before the page has a document to set the | ||
| 34 | + // property on; this catches that case. | ||
| 35 | + webView.postDelayed({ publishBottomInset() }, 500) | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + override fun onResume() { | ||
| 39 | + super.onResume() | ||
| 40 | + // Re-applied after a reload or a return from the background, when the | ||
| 41 | + // property set above no longer exists on the new document. | ||
| 42 | + publishBottomInset() | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + private fun publishBottomInset() { | ||
| 46 | + val view = webView ?: return | ||
| 47 | + val insets = ViewCompat.getRootWindowInsets(view) ?: return | ||
| 48 | + val bottom = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom | ||
| 49 | + val dp = bottom / view.resources.displayMetrics.density | ||
| 50 | + view.evaluateJavascript( | ||
| 51 | + "document.documentElement.style.setProperty('--android-nav-inset', '${dp}px')", | ||
| 52 | + null, | ||
| 53 | + ) | ||
| 54 | + } | ||
| 11 | } | 55 | } |