forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
67 changes: 67 additions & 0 deletions
67
compose/mpp/demo/src/commonMain/kotlin/androidx/compose/mpp/demo/bug/ResizePopupCrashOnJS.kt
This file contains 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,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) | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -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,20 @@ 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: We can remove this as soon as it'll been fixed on skiko side | ||
canvas.width = width | ||
canvas.height = 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 +250,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 +266,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. 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, | ||
|
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.
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need
channel
here? It is asynchronous, and we can have 1-frame lag/blink because of that.Can we just write:
(we need to rewrite the public API
requestResize
as well, so it is a suggestion for the future)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.
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 comment
The 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.