From 04ca90a7d0a7d57c9323d35b73f4e9f48e64508c Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Thu, 12 Aug 2021 13:52:04 +0200 Subject: [PATCH] fix(@desktop/general): Changing System Appearance on the fly is not working App is responsive to the OS theme change event. Now we're following system set theme when the app is started. Corresponding part on the side on nimqml is added. Corresponding part on the side on dotherside is added. On the side of dother side we had kind of a memory leak, cause objects added to the filter were not deleted ever. When the app is closing, it just removes filters, but doesn't delete them. I faced a logical issue, that we were sending qmlengine pointer to the installEventFilter method, instead object which may or may not rely on the qqmlengine instance, that is fixed also. Fixes: #1725 --- src/nim_status_client.nim | 9 ++++++++- .../Profile/Sections/AppearanceContainer.qml | 2 +- ui/imports/Style.qml | 14 ++++++++------ ui/main.qml | 17 ++++++++++++++++- vendor/DOtherSide | 2 +- vendor/nimqml | 2 +- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/nim_status_client.nim b/src/nim_status_client.nim index 82a7628bf8..18389a7ef0 100644 --- a/src/nim_status_client.nim +++ b/src/nim_status_client.nim @@ -92,7 +92,14 @@ proc mainProc() = defer: engine.delete() engine.addImportPath("qrc:/./StatusQ/src") engine.setNetworkAccessManagerFactory(networkAccessFactory) - app.installEventFilter(engine) + + # Register events objects + let dockShowAppEvent = newStatusDockShowAppEventObject(engine) + defer: dockShowAppEvent.delete() + let osThemeEvent = newStatusOSThemeEventObject(engine) + defer: osThemeEvent.delete() + app.installEventFilter(dockShowAppEvent) + app.installEventFilter(osThemeEvent) let netAccMgr = newQNetworkAccessManager(engine.getNetworkAccessManager()) diff --git a/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml b/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml index 48204d899a..821def2a53 100644 --- a/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml +++ b/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml @@ -22,7 +22,7 @@ ScrollView { function updateTheme(theme) { globalSettings.theme = theme - Style.changeTheme(theme) + Style.changeTheme(theme, systemPalette.isCurrentSystemThemeDark()) } function updateFontSize(fontSize) { diff --git a/ui/imports/Style.qml b/ui/imports/Style.qml index 4ed7489ee5..b6101a1ce1 100644 --- a/ui/imports/Style.qml +++ b/ui/imports/Style.qml @@ -15,20 +15,22 @@ QtObject { property ThemePalette statusQLightTheme: StatusLightTheme {} property ThemePalette statusQDarkTheme: StatusDarkTheme {} + property var changeTheme: function (theme, isCurrentSystemThemeDark) { - property var changeTheme: function (theme) { switch (theme) { case Universal.Light: - current = lightTheme; + current = lightTheme; Theme.palette = statusQLightTheme break; case Universal.Dark: - current = darkTheme; + current = darkTheme; Theme.palette = statusQDarkTheme break; - default: - current = lightTheme; - Theme.palette = statusQLightTheme + case Universal.System: + current = isCurrentSystemThemeDark? darkTheme : lightTheme; + Theme.palette = isCurrentSystemThemeDark? statusQDarkTheme : statusQLightTheme + break; + default: console.log('Unknown theme. Valid themes are "light" and "dark"') } } diff --git a/ui/main.qml b/ui/main.qml index 5c6073d9dd..33944a0595 100644 --- a/ui/main.qml +++ b/ui/main.qml @@ -39,6 +39,7 @@ StatusWindow { } id: applicationWindow + objectName: "mainWindow" minimumWidth: 900 minimumHeight: 600 width: 1232 @@ -120,8 +121,22 @@ StatusWindow { } } + // The easiest way to get current system theme (is it light or dark) without using + // OS native methods is to check lightness (0 - 1.0) of the window color. + // If it's too high (0.85+) means light theme is an active. + SystemPalette { + id: systemPalette + function isCurrentSystemThemeDark() { + return window.hslLightness < 0.85 + } + } + + function changeThemeFromOutside() { + Style.changeTheme(globalSettings.theme, systemPalette.isCurrentSystemThemeDark()) + } + Component.onCompleted: { - Style.changeTheme(globalSettings.theme) + Style.changeTheme(globalSettings.theme, systemPalette.isCurrentSystemThemeDark()) setX(Qt.application.screens[0].width / 2 - width / 2); setY(Qt.application.screens[0].height / 2 - height / 2); diff --git a/vendor/DOtherSide b/vendor/DOtherSide index df432d4016..11adba2283 160000 --- a/vendor/DOtherSide +++ b/vendor/DOtherSide @@ -1 +1 @@ -Subproject commit df432d4016d6c16791d89971f2c6bc386e3f2a7d +Subproject commit 11adba22838f448b34645bed33fdec86848c50b4 diff --git a/vendor/nimqml b/vendor/nimqml index 3a2026ebbc..dbabf0b297 160000 --- a/vendor/nimqml +++ b/vendor/nimqml @@ -1 +1 @@ -Subproject commit 3a2026ebbc8a98ef1ffe15693685feea38286552 +Subproject commit dbabf0b2979263e7df8d4bb7a3ddca244b4b682a