From a43c13462ac3cf9f3e82d8802a63d3651864526f Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Fri, 17 Jun 2022 03:14:21 +0200 Subject: [PATCH] The app now remembers the window state. The resolution won't be saved to avoid issues when adding or removing screens with different resolution. Fixes https://github.com/JetpackDuba/Gitnuro/issues/8 --- src/main/kotlin/app/App.kt | 17 +++++++--- src/main/kotlin/app/AppStateManager.kt | 1 + src/main/kotlin/app/di/AppComponent.kt | 2 +- src/main/kotlin/app/di/TabComponent.kt | 1 - .../kotlin/app/extensions/WindowPlacement.kt | 31 +++++++++++++++++++ .../app/{ => preferences}/AppPreferences.kt | 14 ++++++++- .../preferences/WindowsPlacementPreference.kt | 4 +++ .../kotlin/app/ui/dialogs/SettingsDialog.kt | 2 +- src/main/kotlin/app/ui/log/Log.kt | 12 ++++--- .../kotlin/app/updates/UpdatesRepository.kt | 1 - .../kotlin/app/viewmodels/LogViewModel.kt | 2 +- 11 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/app/extensions/WindowPlacement.kt rename src/main/kotlin/app/{ => preferences}/AppPreferences.kt (85%) create mode 100644 src/main/kotlin/app/preferences/WindowsPlacementPreference.kt diff --git a/src/main/kotlin/app/App.kt b/src/main/kotlin/app/App.kt index b2a9716..c24225f 100644 --- a/src/main/kotlin/app/App.kt +++ b/src/main/kotlin/app/App.kt @@ -20,6 +20,9 @@ import androidx.compose.ui.window.WindowPlacement import androidx.compose.ui.window.application import androidx.compose.ui.window.rememberWindowState import app.di.DaggerAppComponent +import app.extensions.preferenceValue +import app.extensions.toWindowPlacement +import app.preferences.AppPreferences import app.theme.AppTheme import app.theme.primaryTextColor import app.theme.secondaryTextColor @@ -47,12 +50,21 @@ class App { private val tabsFlow = MutableStateFlow>(emptyList()) fun start() { + val windowPlacement = appPreferences.windowPlacement.toWindowPlacement + appStateManager.loadRepositoriesTabs() loadTabs() application { var isOpen by remember { mutableStateOf(true) } val theme by appPreferences.themeState.collectAsState() + val windowState = rememberWindowState( + placement = windowPlacement, + size = DpSize(1280.dp, 720.dp) + ) + + // Save window state for next time the Window is started + appPreferences.windowPlacement = windowState.placement.preferenceValue if (isOpen) { Window( @@ -60,10 +72,7 @@ class App { onCloseRequest = { isOpen = false }, - state = rememberWindowState( - placement = WindowPlacement.Maximized, - size = DpSize(1280.dp, 720.dp) - ), + state = windowState, icon = painterResource("logo.svg"), ) { var showSettingsDialog by remember { mutableStateOf(false) } diff --git a/src/main/kotlin/app/AppStateManager.kt b/src/main/kotlin/app/AppStateManager.kt index d8b19e5..ec1d539 100644 --- a/src/main/kotlin/app/AppStateManager.kt +++ b/src/main/kotlin/app/AppStateManager.kt @@ -1,5 +1,6 @@ package app +import app.preferences.AppPreferences import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex import kotlinx.serialization.decodeFromString diff --git a/src/main/kotlin/app/di/AppComponent.kt b/src/main/kotlin/app/di/AppComponent.kt index 15c4cf7..5b19dc4 100644 --- a/src/main/kotlin/app/di/AppComponent.kt +++ b/src/main/kotlin/app/di/AppComponent.kt @@ -1,7 +1,7 @@ package app.di import app.App -import app.AppPreferences +import app.preferences.AppPreferences import app.AppStateManager import dagger.Component import javax.inject.Singleton diff --git a/src/main/kotlin/app/di/TabComponent.kt b/src/main/kotlin/app/di/TabComponent.kt index b2c624b..99f11ae 100644 --- a/src/main/kotlin/app/di/TabComponent.kt +++ b/src/main/kotlin/app/di/TabComponent.kt @@ -1,6 +1,5 @@ package app.di -import app.AppPreferences import app.di.modules.NetworkModule import app.ui.components.TabInformation import dagger.Component diff --git a/src/main/kotlin/app/extensions/WindowPlacement.kt b/src/main/kotlin/app/extensions/WindowPlacement.kt new file mode 100644 index 0000000..7052907 --- /dev/null +++ b/src/main/kotlin/app/extensions/WindowPlacement.kt @@ -0,0 +1,31 @@ +package app.extensions + +import androidx.compose.ui.window.WindowPlacement +import app.preferences.WindowsPlacementPreference + + +private val windowPlacementFloating = WindowsPlacementPreference(0) +private val windowPlacementMaximized = WindowsPlacementPreference(1) +private val windowPlacementFullscreen = WindowsPlacementPreference(2) + +val defaultWindowPlacement = windowPlacementMaximized + +val WindowPlacement.preferenceValue: WindowsPlacementPreference + get() { + return when (this) { + WindowPlacement.Floating -> windowPlacementFloating + WindowPlacement.Maximized -> windowPlacementMaximized + WindowPlacement.Fullscreen -> windowPlacementFullscreen + } + } + +val WindowsPlacementPreference.toWindowPlacement: WindowPlacement + get() { + return when (this) { + windowPlacementFloating -> WindowPlacement.Floating + windowPlacementFullscreen -> WindowPlacement.Fullscreen + else -> WindowPlacement.Maximized + } + } + + diff --git a/src/main/kotlin/app/AppPreferences.kt b/src/main/kotlin/app/preferences/AppPreferences.kt similarity index 85% rename from src/main/kotlin/app/AppPreferences.kt rename to src/main/kotlin/app/preferences/AppPreferences.kt index 14c7b9c..828d4bd 100644 --- a/src/main/kotlin/app/AppPreferences.kt +++ b/src/main/kotlin/app/preferences/AppPreferences.kt @@ -1,5 +1,6 @@ -package app +package app.preferences +import app.extensions.defaultWindowPlacement import app.theme.Themes import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -14,6 +15,7 @@ private const val PREF_LAST_OPENED_REPOSITORIES_PATH = "lastOpenedRepositoriesLi private const val PREF_THEME = "theme" private const val PREF_COMMITS_LIMIT = "commitsLimit" private const val PREF_COMMITS_LIMIT_ENABLED = "commitsLimitEnabled" +private const val PREF_WINDOW_PLACEMENT = "windowsPlacement" private const val DEFAULT_COMMITS_LIMIT = 1000 private const val DEFAULT_COMMITS_LIMIT_ENABLED = true @@ -75,4 +77,14 @@ class AppPreferences @Inject constructor() { preferences.putInt(PREF_COMMITS_LIMIT, value) _commitsLimitFlow.value = value } + + var windowPlacement: WindowsPlacementPreference + get() { + val placement = preferences.getInt(PREF_WINDOW_PLACEMENT, defaultWindowPlacement.value) + + return WindowsPlacementPreference(placement) + } + set(placement) { + preferences.putInt(PREF_WINDOW_PLACEMENT, placement.value) + } } \ No newline at end of file diff --git a/src/main/kotlin/app/preferences/WindowsPlacementPreference.kt b/src/main/kotlin/app/preferences/WindowsPlacementPreference.kt new file mode 100644 index 0000000..8c27f62 --- /dev/null +++ b/src/main/kotlin/app/preferences/WindowsPlacementPreference.kt @@ -0,0 +1,4 @@ +package app.preferences + +@JvmInline +value class WindowsPlacementPreference(val value: Int) \ No newline at end of file diff --git a/src/main/kotlin/app/ui/dialogs/SettingsDialog.kt b/src/main/kotlin/app/ui/dialogs/SettingsDialog.kt index b7b1ad7..e41305a 100644 --- a/src/main/kotlin/app/ui/dialogs/SettingsDialog.kt +++ b/src/main/kotlin/app/ui/dialogs/SettingsDialog.kt @@ -10,7 +10,7 @@ import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import app.AppPreferences +import app.preferences.AppPreferences import app.DropDownOption import app.theme.outlinedTextFieldColors import app.theme.primaryTextColor diff --git a/src/main/kotlin/app/ui/log/Log.kt b/src/main/kotlin/app/ui/log/Log.kt index 7aac413..86253ae 100644 --- a/src/main/kotlin/app/ui/log/Log.kt +++ b/src/main/kotlin/app/ui/log/Log.kt @@ -79,6 +79,8 @@ private const val MARGIN_GRAPH_LANES = 2 private const val LANE_WIDTH = 30f private const val DIVIDER_WIDTH = 8 +private const val LINE_HEIGHT = 40 + // TODO Min size for message column @OptIn( ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class @@ -363,7 +365,7 @@ fun MessagesList( Box( modifier = Modifier .padding(start = graphWidth + 24.dp) - .height(40.dp), + .height(LINE_HEIGHT.dp), contentAlignment = Alignment.CenterStart, ) { Text( @@ -425,7 +427,7 @@ fun GraphList( if (hasUncommitedChanges) { item { Row( - modifier = Modifier.height(40.dp).fillMaxWidth(), + modifier = Modifier.height(LINE_HEIGHT.dp).fillMaxWidth(), ) { UncommitedChangesGraphNode( modifier = Modifier.fillMaxSize(), @@ -441,7 +443,7 @@ fun GraphList( Row( modifier = Modifier - .height(40.dp) + .height(LINE_HEIGHT.dp) .fillMaxWidth(), ) { CommitsGraphLine( @@ -458,7 +460,7 @@ fun GraphList( item { Box( modifier = Modifier - .height(40.dp), + .height(LINE_HEIGHT.dp), ) } } @@ -712,7 +714,7 @@ fun CommitLine( modifier = Modifier .clickable { onRevCommitSelected() } .padding(start = graphWidth) - .height(40.dp) + .height(LINE_HEIGHT.dp) .backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected) ) { diff --git a/src/main/kotlin/app/updates/UpdatesRepository.kt b/src/main/kotlin/app/updates/UpdatesRepository.kt index a4ba301..f4b6c78 100644 --- a/src/main/kotlin/app/updates/UpdatesRepository.kt +++ b/src/main/kotlin/app/updates/UpdatesRepository.kt @@ -1,7 +1,6 @@ package app.updates import app.AppConstants -import app.AppPreferences import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.serialization.decodeFromString diff --git a/src/main/kotlin/app/viewmodels/LogViewModel.kt b/src/main/kotlin/app/viewmodels/LogViewModel.kt index 65a8251..5db24ce 100644 --- a/src/main/kotlin/app/viewmodels/LogViewModel.kt +++ b/src/main/kotlin/app/viewmodels/LogViewModel.kt @@ -1,7 +1,7 @@ package app.viewmodels import androidx.compose.foundation.lazy.LazyListState -import app.AppPreferences +import app.preferences.AppPreferences import app.extensions.delayedStateChange import app.git.* import app.git.graph.GraphCommitList