forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 143
Window Insets support for Compose for Web #3202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Konstantin (terrakok)
wants to merge
8
commits into
jb-main
Choose a base branch
from
CMP-10141
base: jb-main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6a55490
Web: Add `enableBrowserWindowInsets` flag to manage system window ins…
terrakok 532ce54
Web: Add check for `viewport-fit=cover` when `enableBrowserWindowInse…
terrakok cae8fe8
Web: Add `WebWindowInsetsManager` to manage browser window insets
terrakok 2ab59e2
Web: Add IME tracking support to `WebWindowInsetsManager`
terrakok 0e0c908
Web: Expose `windowInsets` via `WebWindowInsetsManager` and implement…
terrakok 6f8a099
Web: Improve `WebWindowInsetsManager` to handle canvas resizing and p…
terrakok 31b37c9
Web: Add `WebWindowInsetsTest` for safe area and insets handling
terrakok 163846f
Review: add a comment to clarify the logic of WebWindowInsetsManager
terrakok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
217 changes: 217 additions & 0 deletions
217
compose/ui/ui/src/webMain/kotlin/androidx/compose/ui/platform/WebWindowInsetsManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| @file:OptIn(ExperimentalWasmJsInterop::class) | ||
|
|
||
| package androidx.compose.ui.platform | ||
|
|
||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.ui.events.EventTargetListener | ||
| import androidx.compose.ui.unit.Density | ||
| import androidx.compose.ui.unit.dp | ||
| import kotlin.js.ExperimentalWasmJsInterop | ||
| import kotlin.js.js | ||
| import kotlinx.browser.window | ||
| import org.w3c.dom.DOMRect | ||
| import org.w3c.dom.Element | ||
| import org.w3c.dom.events.EventTarget | ||
|
|
||
| private class WebWindowInsets( | ||
| private val safeArea: () -> PlatformInsets, | ||
| private val keyboard: () -> PlatformInsets, | ||
| ) : PlatformWindowInsets { | ||
| override val statusBars: PlatformInsets | ||
| get() = PlatformInsets(getTop = { safeArea().top }) | ||
| override val navigationBars: PlatformInsets | ||
| get() = PlatformInsets(getBottom = { safeArea().bottom }) | ||
| override val systemBars: PlatformInsets | ||
| get() = safeArea() | ||
| override val displayCutout: PlatformInsets | ||
| get() = safeArea() | ||
| override val ime: PlatformInsets | ||
| get() = keyboard() | ||
| override val systemGestures: PlatformInsets | ||
| get() = safeArea() | ||
| override val mandatorySystemGestures: PlatformInsets | ||
| get() = PlatformInsets(getTop = { safeArea().top }, getBottom = { safeArea().bottom }) | ||
| override val tappableElement: PlatformInsets | ||
| get() = PlatformInsets(getTop = { safeArea().top }) | ||
| } | ||
|
|
||
| /** | ||
| * Reads system window insets (safe area and IME) from the browser and exposes them as Compose | ||
| * state. | ||
| * | ||
| * Safe area insets are read from CSS `env(safe-area-inset-*)` environment variables via CSS custom | ||
| * properties, and re-read on each window resize event. | ||
| * | ||
| * IME (virtual keyboard) insets are tracked using: | ||
| * - **VirtualKeyboard API** when available — the most precise source. | ||
| * - **VisualViewport API** as a fallback for Safari and Firefox — derived from the difference | ||
| * between `window.innerHeight` and `visualViewport.height`. | ||
| * | ||
| * All insets are clipped to the portion of the system UI zones that the [composeScene] actually | ||
| * overlaps. For example, if the canvas is positioned below the status bar, the top inset will be | ||
| * zero; if it extends into the navigation bar area, the bottom inset will reflect the overlap. | ||
| * | ||
| */ | ||
| internal class WebWindowInsetsManager( | ||
| private val density: Density, | ||
| canvas: Element | ||
| ) { | ||
| private var canvasRect: DOMRect = canvas.getBoundingClientRect() | ||
| set(value) { | ||
| field = value | ||
| readAndUpdateSafeArea() | ||
| readAndUpdateIme() | ||
| } | ||
|
|
||
| private val safeAreaInsets = mutableStateOf(PlatformInsets.Zero) | ||
| private val imeInsets = mutableStateOf(PlatformInsets.Zero) | ||
|
|
||
| val windowInsets: PlatformWindowInsets = WebWindowInsets( | ||
| safeArea = { safeAreaInsets.value }, | ||
| keyboard = { imeInsets.value }, | ||
| ) | ||
|
|
||
| private val hasVirtualKeyboardApi: Boolean = hasVirtualKeyboard() | ||
|
|
||
| private val imeEventsListener: EventTargetListener? | ||
|
|
||
| init { | ||
| installSafeAreaCssProperties() | ||
| imeEventsListener = initImeTracking() | ||
| } | ||
|
|
||
| fun dispose() { | ||
| imeEventsListener?.dispose() | ||
| } | ||
|
|
||
| fun onCanvasResized(canvas: Element) { | ||
| canvasRect = canvas.getBoundingClientRect() | ||
| } | ||
|
|
||
| private fun initImeTracking(): EventTargetListener? { | ||
| return if (hasVirtualKeyboardApi) { | ||
| enableVirtualKeyboardOverlay() | ||
| val vk = getVirtualKeyboard() ?: return null | ||
| EventTargetListener(vk).apply { | ||
| addDisposableEvent("geometrychange") { readAndUpdateIme() } | ||
| } | ||
| } else { | ||
| val vv = getVisualViewport() ?: return null | ||
| EventTargetListener(vv).apply { | ||
| addDisposableEvent("resize") { readAndUpdateIme() } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun readAndUpdateSafeArea() { | ||
| val vw = window.innerWidth.toFloat() | ||
| val vh = window.innerHeight.toFloat() | ||
| val adjustedLeft = maxOf(0f, readCssVarLeft() - canvasRect.left.toFloat()) | ||
| val adjustedTop = maxOf(0f, readCssVarTop() - canvasRect.top.toFloat()) | ||
| val adjustedRight = maxOf(0f, readCssVarRight() - (vw - canvasRect.right.toFloat())) | ||
| val adjustedBottom = maxOf(0f, readCssVarBottom() - (vh - canvasRect.bottom.toFloat())) | ||
|
|
||
| safeAreaInsets.value = with(density) { | ||
| PlatformInsets( | ||
| left = adjustedLeft.dp.roundToPx(), | ||
| top = adjustedTop.dp.roundToPx(), | ||
| right = adjustedRight.dp.roundToPx(), | ||
| bottom = adjustedBottom.dp.roundToPx() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun readAndUpdateIme() { | ||
| val rawHeight = if (hasVirtualKeyboardApi) { | ||
| readVirtualKeyboardHeight() | ||
| } else { | ||
| readVisualViewportImeHeight() | ||
| } | ||
| val vh = window.innerHeight.toFloat() | ||
| val adjustedBottom = maxOf(0f, rawHeight - (vh - canvasRect.bottom.toFloat())) | ||
|
|
||
| imeInsets.value = with(density) { | ||
| PlatformInsets(bottom = adjustedBottom.dp.roundToPx()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Installs CSS custom properties on `document.documentElement` that mirror `env(safe-area-inset-*)`. | ||
| * | ||
| * Setting them on the root element (rather than inside a canvas shadow root) works around a WebKit | ||
| * bug where `env()` values return 0 in canvas-based shadow roots on some iOS versions. | ||
| */ | ||
| // language=js | ||
| private fun installSafeAreaCssProperties(): Unit = js( | ||
| """(function() { | ||
| let s = document.documentElement.style; | ||
| s.setProperty('--cmp-safe-top', 'env(safe-area-inset-top, 0px)'); | ||
| s.setProperty('--cmp-safe-right', 'env(safe-area-inset-right, 0px)'); | ||
| s.setProperty('--cmp-safe-bottom', 'env(safe-area-inset-bottom, 0px)'); | ||
| s.setProperty('--cmp-safe-left', 'env(safe-area-inset-left, 0px)'); | ||
| })()""" | ||
| ) | ||
|
|
||
| // language=js | ||
| private fun readCssVarTop(): Float = | ||
| js("(parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--cmp-safe-top')) || 0)") | ||
|
|
||
| // language=js | ||
| private fun readCssVarRight(): Float = | ||
| js("(parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--cmp-safe-right')) || 0)") | ||
|
|
||
| // language=js | ||
| private fun readCssVarBottom(): Float = | ||
| js("(parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--cmp-safe-bottom')) || 0)") | ||
|
|
||
| // language=js | ||
| private fun readCssVarLeft(): Float = | ||
| js("(parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--cmp-safe-left')) || 0)") | ||
|
|
||
| // language=js | ||
| private fun hasVirtualKeyboard(): Boolean = js("('virtualKeyboard' in navigator)") | ||
|
|
||
| // language=js | ||
| private fun enableVirtualKeyboardOverlay(): Unit = | ||
| js("(navigator.virtualKeyboard.overlaysContent = true)") | ||
|
|
||
| // language=js | ||
| private fun getVirtualKeyboard(): EventTarget? = js("navigator.virtualKeyboard") | ||
|
|
||
| /** Returns the current keyboard height in CSS pixels (0 when keyboard is hidden). */ | ||
| // language=js | ||
| private fun readVirtualKeyboardHeight(): Float = | ||
| js("(navigator.virtualKeyboard.boundingRect.height || 0)") | ||
|
|
||
| // --- IME: VisualViewport API fallback (Safari, Firefox) --- | ||
|
|
||
| // language=js | ||
| private fun getVisualViewport(): EventTarget? = js("(window.visualViewport || null)") | ||
|
|
||
| /** | ||
| * Estimates the IME height in CSS pixels from the visual viewport geometry. | ||
| * Returns 0 when the keyboard is not visible. | ||
| */ | ||
| // language=js | ||
| private fun readVisualViewportImeHeight(): Float = js("""(function() { | ||
| let vv = window.visualViewport; | ||
| if (!vv) return 0; | ||
| return Math.max(0, window.innerHeight - vv.height - vv.offsetTop); | ||
| })()""") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.