fix(@desktop): Persist application window state & geometry

Close #3073
This commit is contained in:
mishkarogachev 2022-05-16 09:57:51 +03:00 committed by Mikhail Rogachev
parent 7b001b5099
commit 0417105a03
2 changed files with 72 additions and 63 deletions

View File

@ -7,12 +7,9 @@ const LAS_KEY_LOCALE* = "global/locale"
const DEFAULT_LOCALE = "en"
const LAS_KEY_THEME* = "global/theme"
const DEFAULT_THEME = 2 #system theme, from qml
const LAS_KEY_APP_WIDTH = "global/app_width"
const DEFAULT_APP_WIDTH = 1232
const LAS_KEY_APP_HEIGHT = "global/app_height"
const DEFAULT_APP_HEIGHT = 770
const LAS_KEY_APP_SIZE_INITIALIZED = "global/app_size_initialized"
const DEFAULT_APP_SIZE_INITIALIZED = false
const LAS_KEY_GEOMETRY = "global/app_geometry"
const LAS_KEY_VISIBILITY = "global/app_visibility"
const DEFAULT_VISIBILITY = 2 #windowed visibility, from qml
QtObject:
type LocalAppSettings* = ref object of QObject
@ -58,41 +55,32 @@ QtObject:
write = setTheme
notify = themeChanged
proc appWidthChanged*(self: LocalAppSettings) {.signal.}
proc getAppWidth*(self: LocalAppSettings): int {.slot.} =
self.settings.value(LAS_KEY_APP_WIDTH, newQVariant(DEFAULT_APP_WIDTH)).intVal
proc setAppWidth*(self: LocalAppSettings, value: int) {.slot.} =
self.settings.setValue(LAS_KEY_APP_WIDTH, newQVariant(value))
self.appWidthChanged()
QtProperty[int] appWidth:
read = getAppWidth
write = setAppWidth
notify = appWidthChanged
proc geometryChanged*(self: LocalAppSettings) {.signal.}
proc getGeometry*(self: LocalAppSettings): QVariant {.slot.} =
self.settings.value(LAS_KEY_GEOMETRY)
proc setGeometry*(self: LocalAppSettings, value: QVariant) {.slot.} =
self.settings.setValue(LAS_KEY_GEOMETRY, newQVariant(value))
self.geometryChanged()
proc appHeightChanged*(self: LocalAppSettings) {.signal.}
proc getAppHeight*(self: LocalAppSettings): int {.slot.} =
self.settings.value(LAS_KEY_APP_HEIGHT, newQVariant(DEFAULT_APP_HEIGHT)).intVal
proc setAppHeight*(self: LocalAppSettings, value: int) {.slot.} =
self.settings.setValue(LAS_KEY_APP_HEIGHT, newQVariant(value))
self.appHeightChanged()
QtProperty[QVariant] geometry:
read = getGeometry
write = setGeometry
notify = geometryChanged
QtProperty[int] appHeight:
read = getAppHeight
write = setAppHeight
notify = appHeightChanged
proc appSizeInitializedChanged*(self: LocalAppSettings) {.signal.}
proc isAppSizeInitialized*(self: LocalAppSettings): bool {.slot.} =
self.settings.value(LAS_KEY_APP_SIZE_INITIALIZED, newQVariant(DEFAULT_APP_SIZE_INITIALIZED)).boolVal
proc setAppSizeInitialized*(self: LocalAppSettings, value: bool) {.slot.} =
self.settings.setValue(LAS_KEY_APP_SIZE_INITIALIZED, newQVariant(value))
self.appSizeInitializedChanged()
proc visibilityChanged*(self: LocalAppSettings) {.signal.}
proc getVisibility*(self: LocalAppSettings): int {.slot.} =
self.settings.value(LAS_KEY_VISIBILITY, newQVariant(DEFAULT_VISIBILITY)).intVal
proc setVisibility*(self: LocalAppSettings, value: int) {.slot.} =
self.settings.setValue(LAS_KEY_VISIBILITY, newQVariant(value))
self.visibilityChanged()
QtProperty[int] visibility:
read = getVisibility
write = setVisibility
notify = visibilityChanged
QtProperty[bool] appSizeInitialized:
read = isAppSizeInitialized
write = setAppSizeInitialized
notify = appSizeInitializedChanged
proc removeKey*(self: LocalAppSettings, key: string) =
if(self.settings.isNil):
@ -102,4 +90,6 @@ QtObject:
case key:
of LAS_KEY_LOCALE: self.localeChanged()
of LAS_KEY_THEME: self.themeChanged()
of LAS_KEY_THEME: self.themeChanged()
of LAS_KEY_GEOMETRY: self.geometryChanged()
of LAS_KEY_VISIBILITY: self.visibilityChanged()

View File

@ -28,8 +28,6 @@ StatusWindow {
objectName: "mainWindow"
minimumWidth: 900
minimumHeight: 600
width: localAppSettings.appWidth
height: localAppSettings.appHeight
color: Style.current.background
title: {
// Set application settings
@ -41,20 +39,51 @@ StatusWindow {
}
visible: true
function storeWidth() {
if(!applicationWindow.appIsReady)
return
localAppSettings.appWidth = width
function restoreAppState() {
let geometry = localAppSettings.geometry;
let visibility = localAppSettings.visibility;
if (visibility !== Window.Windowed &&
visibility !== Window.Maximized &&
visibility !== Window.FullScreen) {
visibility = Window.Windowed;
}
if (geometry === undefined) {
let screen = Qt.application.screens[0];
geometry = Qt.rect(0,
0,
Math.min(Screen.desktopAvailableWidth - 125, 1400),
Math.min(Screen.desktopAvailableHeight - 125, 840));
geometry.x = (screen.width - geometry.width) / 2;
geometry.y = (screen.height - geometry.height) / 2;
}
applicationWindow.visibility = visibility;
if (visibility == Window.Windowed) {
applicationWindow.x = geometry.x;
applicationWindow.y = geometry.y;
applicationWindow.width = geometry.width;
applicationWindow.height = geometry.height;
}
}
function storeHeight() {
if(!applicationWindow.appIsReady)
return
localAppSettings.appHeight = height
function storeAppState() {
if (!applicationWindow.appIsReady)
return;
localAppSettings.visibility = applicationWindow.visibility;
if (applicationWindow.visibility == Window.Windowed) {
localAppSettings.geometry = Qt.rect(applicationWindow.x, applicationWindow.y,
applicationWindow.width, applicationWindow.height);
}
}
onWidthChanged: Qt.callLater(storeWidth)
onHeightChanged: Qt.callLater(storeHeight)
onXChanged: Qt.callLater(storeAppState)
onYChanged: Qt.callLater(storeAppState)
onWidthChanged: Qt.callLater(storeAppState)
onHeightChanged: Qt.callLater(storeAppState)
Action {
shortcut: StandardKey.FullScreen
@ -98,9 +127,8 @@ StatusWindow {
target: startupModule
onStartUpUIRaised: {
applicationWindow.appIsReady = true
applicationWindow.storeWidth()
applicationWindow.storeHeight()
applicationWindow.appIsReady = true;
applicationWindow.storeAppState();
}
onAppStateChanged: {
@ -184,18 +212,9 @@ StatusWindow {
Component.onCompleted: {
Global.applicationWindow = this;
Style.changeTheme(localAppSettings.theme, systemPalette.isCurrentSystemThemeDark())
Style.changeTheme(localAppSettings.theme, systemPalette.isCurrentSystemThemeDark());
if (!localAppSettings.appSizeInitialized) {
width = Math.min(Screen.desktopAvailableWidth - 125, 1400)
height = Math.min(Screen.desktopAvailableHeight - 125, 840)
localAppSettings.appSizeInitialized = true
}
setX(Qt.application.screens[0].width / 2 - width / 2);
setY(Qt.application.screens[0].height / 2 - height / 2);
applicationWindow.updatePosition();
restoreAppState();
}
signal navigateTo(string path)