From ff850226693c151c21b733239b5a2c8f74758fca Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Tue, 6 Feb 2024 22:16:51 +0700 Subject: [PATCH] desktop: change width and height to safe values for storing and restoring --- .../kotlin/chat/simplex/common/StoreWindowState.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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 index 2a1a26df95..9d8e5850b7 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/StoreWindowState.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/StoreWindowState.kt @@ -11,7 +11,15 @@ data class WindowPositionSize( val height: Int = 768, val x: Int = 0, val y: Int = 0, -) +) { + fun safeValues(): WindowPositionSize = + copy( + x = x.coerceIn(0, 10000), + y = x.coerceIn(0, 10000), + width = width.coerceIn(100, 10000), + height = height.coerceIn(100, 10000) + ) +} fun getStoredWindowState(): WindowPositionSize = try { @@ -19,7 +27,7 @@ fun getStoredWindowState(): WindowPositionSize = var state = if (str == null) { WindowPositionSize() } else { - json.decodeFromString(str) + json.decodeFromString(str).safeValues() } // For some reason on Linux actual width will be 10.dp less after specifying it here. If we specify 1366, @@ -33,4 +41,4 @@ fun getStoredWindowState(): WindowPositionSize = } fun storeWindowState(state: WindowPositionSize) = - appPreferences.desktopWindowState.set(json.encodeToString(state)) + appPreferences.desktopWindowState.set(json.encodeToString(state.safeValues()))