From 8fd2ff3cc4066be8fe57b46845a1a6d42b8f92a3 Mon Sep 17 00:00:00 2001 From: Thomas Andres Gomez Date: Wed, 5 Feb 2025 11:39:44 +0100 Subject: [PATCH] Reword the windows height management to avoid too big windows. --- .../kotlin/com/pixelized/desktop/lwa/App.kt | 8 ++++- .../lwa/navigation/window/WindowNavHost.kt | 20 +++++++++++- .../destination/CharacterSheetEditWindow.kt | 17 +++++----- .../destination/CharacterSheetWindow.kt | 20 ++++++------ .../window/destination/RollHistoryWindow.kt | 31 ++++++++++++------- .../detail/CharacterSheetPage.kt | 7 ++++- .../desktop/lwa/screen/main/MainPage.kt | 3 +- 7 files changed, 71 insertions(+), 35 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt index 8bd7241..0f54cc1 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/App.kt @@ -25,8 +25,10 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Shape +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.min import androidx.compose.ui.window.ApplicationScope import androidx.compose.ui.window.Window import androidx.compose.ui.window.rememberWindowState @@ -40,6 +42,7 @@ import com.pixelized.desktop.lwa.navigation.window.WindowsNavHost import com.pixelized.desktop.lwa.navigation.window.destination.CharacterSheetEditWindow import com.pixelized.desktop.lwa.navigation.window.destination.CharacterSheetWindow import com.pixelized.desktop.lwa.navigation.window.destination.RollHistoryWindow +import com.pixelized.desktop.lwa.navigation.window.rememberMaxWindowHeight import com.pixelized.desktop.lwa.repository.network.NetworkRepository import com.pixelized.desktop.lwa.repository.network.NetworkRepository.Status import com.pixelized.desktop.lwa.screen.characterSheet.CharacterSheetMainNavHost @@ -52,6 +55,7 @@ import lwacharactersheet.composeapp.generated.resources.network__disconnect__mes import org.jetbrains.compose.resources.getString import org.jetbrains.compose.ui.tooling.preview.Preview import org.koin.compose.koinInject +import java.awt.Toolkit val LocalWindowController = compositionLocalOf { error("Local Window Controller is not yet ready") @@ -68,9 +72,11 @@ val LocalErrorSnackHost = compositionLocalOf { @Composable @Preview fun ApplicationScope.App() { + + val maxWindowHeight = rememberMaxWindowHeight() val snackHostState = remember { SnackbarHostState() } val errorSnackHostState = remember { SnackbarHostState() } - val windowController = remember { WindowController() } + val windowController = remember { WindowController(maxWindowHeight) } val keyEventHandlers = remember { mutableStateListOf() } CompositionLocalProvider( diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/WindowNavHost.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/WindowNavHost.kt index 3b78df6..a4ae0b7 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/WindowNavHost.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/WindowNavHost.kt @@ -9,12 +9,16 @@ import androidx.compose.runtime.key import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Window import androidx.compose.ui.window.WindowState import androidx.compose.ui.window.rememberWindowState import com.pixelized.desktop.lwa.composable.key.KeyEventHandler import com.pixelized.desktop.lwa.composable.key.LocalKeyEventHandlers import com.pixelized.desktop.lwa.navigation.window.destination.Window +import java.awt.Toolkit val LocalWindow = compositionLocalOf { error("Local Window is not yet ready") @@ -24,7 +28,9 @@ val LocalWindowState = compositionLocalOf { } @Stable -class WindowController { +class WindowController( + val maxWindowHeight: Dp, +) { private val _windows = mutableStateOf>(emptyMap()) val windows: State> get() = _windows @@ -41,6 +47,18 @@ class WindowController { } } +@Composable +@Stable +fun rememberMaxWindowHeight(): Dp { + val density = LocalDensity.current + val maxWindowHeight = remember(density) { + val screenSize = Toolkit.getDefaultToolkit().screenSize + val screenHeight = screenSize.height + with(density) { screenHeight.toDp() - 128.dp } + } + return maxWindowHeight +} + @Composable fun WindowsNavHost( controller: WindowController, diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetEditWindow.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetEditWindow.kt index 9edafd6..8cb9d64 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetEditWindow.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetEditWindow.kt @@ -7,25 +7,26 @@ import com.pixelized.desktop.lwa.navigation.window.WindowController @Stable class CharacterSheetEditWindow( - title: String, val sheetId: String?, + title: String, + size: DpSize, ) : Window( title = title, size = size, -) { - companion object { - val size = DpSize(600.dp, 900.dp) - } -} +) fun WindowController.navigateToCharacterSheetEdit( - title: String, characterId: String?, + title: String, ) { showWindow( window = CharacterSheetEditWindow( - title = title, sheetId = characterId, + title = title, + size = DpSize( + width = 600.dp, + height = maxWindowHeight, + ), ) ) } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetWindow.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetWindow.kt index 7cc4e17..697577c 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetWindow.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/CharacterSheetWindow.kt @@ -7,28 +7,26 @@ import com.pixelized.desktop.lwa.navigation.window.WindowController @Stable class CharacterSheetWindow( - title: String, val characterId: String, + title: String, + size: DpSize, ) : Window( title = title, size = size, -) { - companion object { - val size = DpSize( - width = 400.dp + 64.dp, - height = 900.dp, - ) - } -} +) fun WindowController.navigateToCharacterSheet( - title: String, characterId: String, + title: String, ) { showWindow( window = CharacterSheetWindow( - title = title, characterId = characterId, + title = title, + size = DpSize( + width = 400.dp + 64.dp, + height = maxWindowHeight, + ), ) ) } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/RollHistoryWindow.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/RollHistoryWindow.kt index ea9e41d..050e6b5 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/RollHistoryWindow.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/navigation/window/destination/RollHistoryWindow.kt @@ -1,21 +1,28 @@ package com.pixelized.desktop.lwa.navigation.window.destination +import androidx.compose.runtime.Stable import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp import com.pixelized.desktop.lwa.navigation.window.WindowController -class RollHistoryWindow: Window( - title = "", +@Stable +class RollHistoryWindow( + title: String, + size: DpSize, +) : Window( + title = title, size = size, -) { - companion object { - val size = DpSize( - width = 400.dp + 64.dp, - height = 900.dp, - ) - } -} +) -fun WindowController.navigateToRollHistory() { - showWindow(window = RollHistoryWindow()) +fun WindowController.navigateToRollHistory( + title: String = "", +) { + showWindow( + window = RollHistoryWindow( + title = title, size = DpSize( + width = 400.dp + 64.dp, + height = maxWindowHeight, + ) + ) + ) } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/characterSheet/detail/CharacterSheetPage.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/characterSheet/detail/CharacterSheetPage.kt index 9519b2e..c0dc63e 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/characterSheet/detail/CharacterSheetPage.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/characterSheet/detail/CharacterSheetPage.kt @@ -49,10 +49,13 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.min import com.pixelized.desktop.lwa.LocalWindowController import com.pixelized.desktop.lwa.composable.blur.BlurContent import com.pixelized.desktop.lwa.composable.blur.BlurContentController @@ -83,6 +86,7 @@ import org.jetbrains.compose.resources.getString import org.jetbrains.compose.resources.painterResource import org.jetbrains.compose.resources.stringResource import org.koin.compose.viewmodel.koinViewModel +import java.awt.Toolkit @Stable data class CharacterSheetPageUio( @@ -125,8 +129,9 @@ fun CharacterSheetPage( rollViewModel: RollViewModel = koinViewModel(), ) { val windowController = LocalWindowController.current - val window = LocalWindow.current val screen = LocalScreenController.current + val window = LocalWindow.current + val density = LocalDensity.current val scope = rememberCoroutineScope() val blurController = remember { BlurContentController() } diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/main/MainPage.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/main/MainPage.kt index 210d9e6..608441a 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/main/MainPage.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/screen/main/MainPage.kt @@ -22,6 +22,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp import com.pixelized.desktop.lwa.LocalWindowController import com.pixelized.desktop.lwa.navigation.screen.LocalScreenController @@ -79,8 +80,8 @@ fun MainPage( }, onCreateCharacter = { window.navigateToCharacterSheetEdit( - title = runBlocking { getString(Res.string.character_sheet_edit__create__title) }, characterId = null, + title = runBlocking { getString(Res.string.character_sheet_edit__create__title) }, ) }, onRollHistory = {