Skip to content
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

Size UndecoratedWindowResizer to the size of the window #388

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ import androidx.compose.runtime.CompositionLocalContext
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.util.fastForEachIndexed
import androidx.compose.ui.window.LocalWindow
import androidx.compose.ui.window.UndecoratedWindowResizer
import androidx.compose.ui.window.WindowExceptionHandler
import androidx.compose.ui.window.density
import org.jetbrains.skiko.ClipComponent
import org.jetbrains.skiko.GraphicsApi
import org.jetbrains.skiko.OS
Expand All @@ -38,6 +43,7 @@ import java.awt.event.MouseListener
import java.awt.event.MouseMotionListener
import java.awt.event.MouseWheelListener
import javax.swing.JLayeredPane
import kotlin.math.max
import org.jetbrains.skiko.SkiaLayerAnalytics

internal class ComposeWindowDelegate(
Expand Down Expand Up @@ -143,12 +149,81 @@ internal class ComposeWindowDelegate(
LocalWindow provides window,
LocalLayerContainer provides _pane
) {
content()
undecoratedWindowResizer.Content()
WindowContentLayout(content)
}
}
}

@Composable
private fun WindowContentLayout(
content: @Composable () -> Unit
){
Layout(
{
WindowUserContentLayout(content)
undecoratedWindowResizer.Content()
},
measurePolicy = { measurables, constraints ->
// Measure the content
val contentMeasurable = measurables[0]
val contentPlaceable = contentMeasurable.measure(constraints)
val width: Int = max(constraints.minWidth, contentPlaceable.width)
val height: Int = max(constraints.minHeight, contentPlaceable.height)

val resizerMeasurable = measurables.getOrNull(1)
val resizerPlaceable = resizerMeasurable?.let{
val density = layer.component.density.density
val resizerWidth = (window.width * density).toInt()
val resizerHeight = (window.height * density).toInt()
it.measure(
Constraints(
minWidth = resizerWidth,
minHeight = resizerHeight,
maxWidth = resizerWidth,
maxHeight = resizerHeight
)
)
}

layout(width, height){
contentPlaceable.place(0, 0)
resizerPlaceable?.place(0, 0)
}
}
)
}

/**
* Wraps the user's content placed in the window into a single placeable, so that
* [WindowContentLayout] can find it and the [undecoratedWindowResizer] in the list of
* measurables.
*/
@Composable
private fun WindowUserContentLayout(
content: @Composable () -> Unit
){
Layout(
{ content() },
measurePolicy = { measurables, constraints ->
val placeables = arrayOfNulls<Placeable>(measurables.size)
var width = constraints.minWidth
var height = constraints.minHeight
measurables.fastForEachIndexed { index, measurable ->
val placeable = measurable.measure(constraints)
placeables[index] = placeable
width = max(width, placeable.width)
height = max(height, placeable.height)
}

layout(width, height) {
placeables.forEach { placeable ->
placeable?.place(0, 0)
}
}
}
)
}

fun dispose() {
if (!isDisposed) {
layer.dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
Expand All @@ -50,6 +51,7 @@ import java.awt.Dimension
import java.awt.GraphicsEnvironment
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import kotlin.test.assertEquals
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -522,4 +524,28 @@ class WindowTest {
assertThat(isApplicationEffectEnded).isTrue()
assertThat(isWindowEffectEnded).isTrue()
}

@Test
fun `undecorated resizable window with unspecified size`() = runApplicationTest {
var window: ComposeWindow? = null

launchApplication {
Window(
onCloseRequest = ::exitApplication,
state = rememberWindowState(width = Dp.Unspecified, height = Dp.Unspecified),
undecorated = true,
resizable = true,
) {
window = this.window
Box(Modifier.size(32.dp))
}
}

awaitIdle()
assertEquals(32, window?.width)
assertEquals(32, window?.height)

window?.dispatchEvent(WindowEvent(window, WindowEvent.WINDOW_CLOSING))
}

}