diff --git a/src/main/kotlin/com/mineinabyss/launchy/ui/elements/LaunchyWindowDraggableArea.kt b/src/main/kotlin/com/mineinabyss/launchy/ui/elements/LaunchyWindowDraggableArea.kt index 54e0b4f..96a84ac 100644 --- a/src/main/kotlin/com/mineinabyss/launchy/ui/elements/LaunchyWindowDraggableArea.kt +++ b/src/main/kotlin/com/mineinabyss/launchy/ui/elements/LaunchyWindowDraggableArea.kt @@ -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() diff --git a/src/main/kotlin/com/mineinabyss/launchy/ui/state/TopBarState.kt b/src/main/kotlin/com/mineinabyss/launchy/ui/state/TopBarState.kt index f97a73e..b40e89e 100644 --- a/src/main/kotlin/com/mineinabyss/launchy/ui/state/TopBarState.kt +++ b/src/main/kotlin/com/mineinabyss/launchy/ui/state/TopBarState.kt @@ -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 { error("No top bar provided") } val TopBar: TopBarState @@ -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, +)