-
Notifications
You must be signed in to change notification settings - Fork 80
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
Send correct window size on resize - this fixes popup crashes that happened #1166
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2024 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. | ||
*/ | ||
|
||
package androidx.compose.mpp.demo.bug | ||
|
||
import androidx.compose.mpp.demo.Screen | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.* | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
internal val ResizePopupCrashOnJS = Screen.Example("Resize Popup") { | ||
MaterialTheme { | ||
var expanded by remember { mutableStateOf(false) } | ||
val items = listOf("Resize", "The", "Window") | ||
var selectedOptionText by remember { mutableStateOf(items[0]) } | ||
ExposedDropdownMenuBox( | ||
expanded = expanded, | ||
onExpandedChange = { | ||
expanded = it | ||
}, | ||
) { | ||
OutlinedTextField( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(top = 16.dp), | ||
value = selectedOptionText, | ||
onValueChange = {}, | ||
readOnly = true | ||
) | ||
ExposedDropdownMenu( | ||
expanded = expanded, | ||
onDismissRequest = { | ||
expanded = false | ||
}, | ||
) { | ||
items.forEach { selectionOption -> | ||
DropdownMenuItem( | ||
onClick = { | ||
selectedOptionText = selectionOption | ||
expanded = false | ||
}, | ||
) { | ||
Text(selectionOption) | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,14 +171,9 @@ private class ComposeWindow( | |
} | ||
|
||
init { | ||
layer.layer.attachTo(canvas) | ||
initEvents(canvas) | ||
|
||
canvas.setAttribute("tabindex", "0") | ||
layer.layer.needRedraw() | ||
|
||
_windowInfo.containerSize = IntSize(canvas.width, canvas.height) | ||
layer.setSize(canvas.width, canvas.height) | ||
|
||
layer.setDensity(density) | ||
layer.setContent { | ||
|
@@ -189,12 +184,22 @@ private class ComposeWindow( | |
} | ||
} | ||
|
||
fun resize(newSize: IntSize) { | ||
canvas.width = newSize.width | ||
canvas.height = newSize.height | ||
_windowInfo.containerSize = IntSize(canvas.width, canvas.height) | ||
fun resize(boxSize: IntSize) { | ||
val density = density.density.toInt() | ||
|
||
val width = boxSize.width * density | ||
val height = boxSize.height * density | ||
|
||
// TODO: What we actually should do is to pass this new dimensions to attachTo without setting them directly on canvas. | ||
// We should be HTMLCanvas-agnostic in that sense | ||
canvas.width = boxSize.width | ||
canvas.height = boxSize.height | ||
|
||
|
||
_windowInfo.containerSize = IntSize(width, height) | ||
|
||
layer.layer.attachTo(canvas) | ||
layer.setSize(canvas.width, canvas.height) | ||
layer.setSize(width, height) | ||
layer.layer.needRedraw() | ||
} | ||
|
||
|
@@ -247,6 +252,11 @@ fun CanvasBasedWindow( | |
) | ||
} | ||
|
||
fun getParentContainerBox(): IntSize { | ||
val documentElement = document.documentElement ?: return IntSize(0, 0) | ||
return IntSize(documentElement.clientWidth, documentElement.clientHeight) | ||
} | ||
|
||
val actualRequestResize: suspend () -> IntSize = if (requestResize != null) { | ||
requestResize | ||
} else { | ||
|
@@ -258,23 +268,16 @@ fun CanvasBasedWindow( | |
// because the default behaviour expects that the Canvas takes the entire window space, | ||
// so the app has the same lifecycle as the browser tab. | ||
window.addEventListener("resize", { _ -> | ||
val w = document.documentElement?.clientWidth ?: 0 | ||
val h = document.documentElement?.clientHeight ?: 0 | ||
channel.trySend(IntSize(w, h)) | ||
channel.trySend(getParentContainerBox()) | ||
}) | ||
|
||
channel.trySend(getParentContainerBox()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We didn't have this before. Is it necessary to fix the issue? Or is it here for other reasons? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We trigger exactly the same event the very first time as we trigger on resize - thus the logic of resolving/assigning dimensions is uniform and less prone for bugs when this logic is slightly different the very first time and on resize (this is exactly what happened before actually) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need Can we just write:
(we need to rewrite the public API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We intentionally do not react on every resize event anyway (there's a lot of them when we manually resize the browser window), because it's quite expensive. So we throttle the events anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we receive 1 resize event per frame, or it can be 10 events per frame? If it is the first, it shouldn't cause lags in theory. We resize synchronously on desktop on every event. It is done intentionally, as the main relayout shouldn't be expensive - it just relayouts the top layout, and doesn't relayout the children. We strive to make resize as fast as the window resize, so users don't see black/white bars. We also have multiple reports that our resize still isn't ideal (because of AWT implementation we still have white bars). As for resize on init - is there any reason to not make it synchronous? It isn't visible to user comparing to desktop (because the source code is loaded asynchronously), but I would still do that to avoid passing zero size on the first recomposition/layout - it can cause races with other init code. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A separate thing that is also important to mention (but not so important to fix) - if we still want async resize, better to subscribe on it inside a separate coroutine inside
|
||
suspend { | ||
channel.receive() | ||
} | ||
} | ||
|
||
if (requestResize == null) { | ||
(document.getElementById(canvasElementId) as? HTMLCanvasElement)?.let { | ||
it.width = document.documentElement?.clientWidth ?: 0 | ||
it.height = document.documentElement?.clientHeight ?: 0 | ||
} | ||
} | ||
|
||
var composeWindow: ComposeWindow? = null | ||
composeWindow = ComposeWindow( | ||
canvasId = canvasElementId, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we should aim to be HTMLCanvas-agnostic in CavnasBasedWindow implementations.
It's easier to implement the details here rather than in skiko, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by agnostic I meant that ideally there should be only one place where we are setting canvas dimensions physically. Now we do it in three different places and this can get out of hand. If this place will be somewhere in compose this is indeed an issue of design. But I'd rather definitely width/height setting to one place.