diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 83ae90cb2..4438329a6 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -173,6 +173,8 @@ class AppPreferences { val connectRemoteViaMulticastAuto = mkBoolPreference(SHARED_PREFS_CONNECT_REMOTE_VIA_MULTICAST_AUTO, true) val offerRemoteMulticast = mkBoolPreference(SHARED_PREFS_OFFER_REMOTE_MULTICAST, true) + val desktopWindowState = mkStrPreference(SHARED_PREFS_DESKTOP_WINDOW_STATE, null) + private fun mkIntPreference(prefName: String, default: Int) = SharedPreference( get = fun() = settings.getInt(prefName, default), @@ -317,6 +319,7 @@ class AppPreferences { private const val SHARED_PREFS_CONNECT_REMOTE_VIA_MULTICAST = "ConnectRemoteViaMulticast" private const val SHARED_PREFS_CONNECT_REMOTE_VIA_MULTICAST_AUTO = "ConnectRemoteViaMulticastAuto" private const val SHARED_PREFS_OFFER_REMOTE_MULTICAST = "OfferRemoteMulticast" + private const val SHARED_PREFS_DESKTOP_WINDOW_STATE = "DesktopWindowState" } } diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt index 33453f5e8..d6cc9c7bb 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt @@ -15,7 +15,6 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.window.* import chat.simplex.common.model.ChatController import chat.simplex.common.model.ChatModel -import chat.simplex.common.platform.desktopPlatform import chat.simplex.common.ui.theme.DEFAULT_START_MODAL_WIDTH import chat.simplex.common.ui.theme.SimpleXTheme import chat.simplex.common.views.TerminalView @@ -31,10 +30,30 @@ import java.io.File val simplexWindowState = SimplexWindowState() fun showApp() = application { - // For some reason on Linux actual width will be 10.dp less after specifying it here. If we specify 1366, - // it will show 1356. But after that we can still update it to 1366 by changing window state. Just making it +10 now here - val width = if (desktopPlatform.isLinux()) 1376.dp else 1366.dp - val windowState = rememberWindowState(placement = WindowPlacement.Floating, width = width, height = 768.dp) + // Creates file if not exists; comes with proper defaults + val state = getStoredWindowState() + + val windowState: WindowState = rememberWindowState( + placement = WindowPlacement.Floating, + width = state.width.dp, + height = state.height.dp, + position = WindowPosition(state.x.dp, state.y.dp) + ) + + LaunchedEffect( + windowState.position.x.value, + windowState.position.y.value, + windowState.size.width.value, + windowState.size.height.value + ) { + storeWindowState(WindowPositionSize( + x = windowState.position.x.value.toInt(), + y = windowState.position.y.value.toInt(), + width = windowState.size.width.value.toInt(), + height = windowState.size.height.value.toInt() + )) + } + simplexWindowState.windowState = windowState // Reload all strings in all @Composable's after language change at runtime if (remember { ChatController.appPrefs.appLanguage.state }.value != "") { diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/StoreWindowState.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/StoreWindowState.kt new file mode 100644 index 000000000..2a1a26df9 --- /dev/null +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/StoreWindowState.kt @@ -0,0 +1,36 @@ +package chat.simplex.common + +import chat.simplex.common.model.json +import chat.simplex.common.platform.appPreferences +import chat.simplex.common.platform.desktopPlatform +import kotlinx.serialization.* + +@Serializable +data class WindowPositionSize( + val width: Int = 1366, + val height: Int = 768, + val x: Int = 0, + val y: Int = 0, +) + +fun getStoredWindowState(): WindowPositionSize = + try { + val str = appPreferences.desktopWindowState.get() + var state = if (str == null) { + WindowPositionSize() + } else { + json.decodeFromString(str) + } + + // For some reason on Linux actual width will be 10.dp less after specifying it here. If we specify 1366, + // it will show 1356. But after that we can still update it to 1366 by changing window state. Just making it +10 now here + if (desktopPlatform.isLinux() && state.width == 1366) { + state = state.copy(width = 1376) + } + state + } catch (e: Throwable) { + WindowPositionSize() + } + +fun storeWindowState(state: WindowPositionSize) = + appPreferences.desktopWindowState.set(json.encodeToString(state))