Skip to content

Commit

Permalink
fix: Maximize toggle not working on Windows, see JetBrains/compose-mu…
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Apr 15, 2024
1 parent 4c8ce7f commit be1e9bc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fun WindowScope.BetterWindowDraggableArea(
val topBar = TopBarProvider.current
WindowDraggableArea(modifier.pointerInput(Unit) {
detectDragGestures(onDragStart = {
topBar.windowState.placement = WindowPlacement.Floating
topBar.ensureFloating()
}) { _, _ -> }
}) {
content()
Expand Down
64 changes: 60 additions & 4 deletions src/main/kotlin/com/mineinabyss/launchy/ui/state/TopBarState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.WindowScope
import androidx.compose.ui.window.WindowState
import com.mineinabyss.launchy.util.OS
import java.awt.Dimension
import java.awt.Point
import java.awt.Toolkit

val TopBarProvider = compositionLocalOf<TopBarState> { error("No top bar provided") }
val TopBar: TopBarState
Expand All @@ -20,9 +24,61 @@ class TopBarState(
val windowState: WindowState,
val windowScope: WindowScope,
) {
fun toggleMaximized() {
if (windowState.placement != WindowPlacement.Maximized)
windowState.placement = WindowPlacement.Maximized
else windowState.placement = WindowPlacement.Floating
var floatingWindowSize: WindowSize? = null

fun ensureMaximized() {
when (OS.get()) {
OS.WINDOWS -> {
if (floatingWindowSize != null) return
val window = windowScope.window
val graphicsConfiguration = window.graphicsConfiguration
val insets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration)
val bounds = graphicsConfiguration.bounds
floatingWindowSize = WindowSize(window.size, window.location)
window.setSize(bounds.width, bounds.height - insets.bottom)
window.setLocation(bounds.x, bounds.y)
}

else -> {
windowState.placement = WindowPlacement.Maximized
}

}
}

fun ensureFloating() {
when (OS.get()) {
OS.WINDOWS -> {
if (floatingWindowSize == null) return
val window = windowScope.window
floatingWindowSize?.let {
window.size = it.size
window.location = it.location
floatingWindowSize = null
}
}

else -> {
windowState.placement = WindowPlacement.Floating
}
}
}

fun toggleMaximized() = when (OS.get()) {
OS.WINDOWS -> {
if (floatingWindowSize == null) ensureMaximized()
else ensureFloating()
}

else -> {
if (windowState.placement == WindowPlacement.Maximized)
ensureFloating()
else ensureMaximized()
}
}
}

class WindowSize(
val size: Dimension,
val location: Point,
)

0 comments on commit be1e9bc

Please sign in to comment.