From 3f6bb665366de86c3c75b9b5493622e6c6b8c0e9 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Wed, 20 Oct 2021 12:55:10 +0200 Subject: [PATCH] refactor(@desktop/general): `globalSettings` moved to Nim --- src/app/boot/app_controller.nim | 26 +++++--- src/app/core/global_singleton.nim | 12 +++- src/app/core/local_app_settings.nim | 64 +++++++++++++++++++ ui/app/AppLayouts/Chat/ChatLayout.qml | 2 +- .../Chat/controls/FetchMoreMessagesButton.qml | 2 +- .../AppLayouts/Chat/panels/ChatTimePanel.qml | 2 +- .../Profile/popups/LanguageModal.qml | 5 +- .../Profile/views/AppearanceView.qml | 8 +-- .../AppLayouts/Profile/views/LanguageView.qml | 2 +- .../Wallet/controls/AssetDelegate.qml | 2 +- .../Wallet/controls/TransactionDelegate.qml | 2 +- .../Wallet/controls/WalletDelegate.qml | 2 +- .../AppLayouts/Wallet/views/LeftTabView.qml | 2 +- .../AppLayouts/WalletV2/views/AssetsView.qml | 2 +- .../AppLayouts/WalletV2/views/LeftTabView.qml | 4 +- ui/main.qml | 16 +---- 16 files changed, 110 insertions(+), 43 deletions(-) create mode 100644 src/app/core/local_app_settings.nim diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index b625c4b3e0..0b24383952 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -45,14 +45,17 @@ elif (defined(macosx)): elif (defined(linux)): i18nPath = joinPath(getAppDir(), "../i18n") -var currentLanguageCode: string +proc setLanguage(locale: string) = + let shouldRetranslate = not defined(linux) + singletonInstance.engine.setTranslationPackage(joinPath(i18nPath, fmt"qml_{locale}.qm"), shouldRetranslate) + proc changeLanguage(locale: string) = + let currentLanguageCode = singletonInstance.localAppSettings.getLocale() if (locale == currentLanguageCode): return - currentLanguageCode = locale - let shouldRetranslate = not defined(linux) - singletonInstance.engine.setTranslationPackage( - joinPath(i18nPath, fmt"qml_{locale}.qm"), shouldRetranslate) + + singletonInstance.localAppSettings.setLocale(locale) + setLanguage(locale) type AppController* = ref object of RootObj @@ -76,6 +79,7 @@ type aboutService: about_service.Service # Core + localAppSettingsVariant: QVariant localAccountSettingsVariant: QVariant localAccountSensitiveSettingsVariant: QVariant @@ -143,6 +147,7 @@ proc newAppController*(appService: AppService): AppController = result.aboutService = about_service.newService() # Core + result.localAppSettingsVariant = newQVariant(singletonInstance.localAppSettings) result.localAccountSettingsVariant = newQVariant(singletonInstance.localAccountSettings) result.localAccountSensitiveSettingsVariant = newQVariant(singletonInstance.localAccountSensitiveSettings) # Modules @@ -206,6 +211,7 @@ proc delete*(self: AppController) = self.profile.delete ################################################# + self.localAppSettingsVariant.delete self.localAccountSettingsVariant.delete self.localAccountSensitiveSettingsVariant.delete @@ -228,14 +234,14 @@ proc startupDidLoad*(self: AppController) = singletonInstance.engine.setRootContextProperty("profileModel", self.profile.variant) ################################################# - # We're applying default language before we load qml. Also we're aware that - # switch language at runtime will have some impact to cpu usage. - # https://doc.qt.io/archives/qtjambi-4.5.2_01/com/trolltech/qt/qtjambi-linguist-programmers.html - changeLanguage("en") - + singletonInstance.engine.setRootContextProperty("localAppSettings", self.localAppSettingsVariant) singletonInstance.engine.setRootContextProperty("localAccountSettings", self.localAccountSettingsVariant) singletonInstance.engine.load(newQUrl("qrc:///main.qml")) + # We need to set a language once qml is loaded + let locale = singletonInstance.localAppSettings.getLocale() + setLanguage(locale) + proc mainDidLoad*(self: AppController) = self.appService.onLoggedIn() self.startupModule.moveToAppState() diff --git a/src/app/core/global_singleton.nim b/src/app/core/global_singleton.nim index d4b4e6e0d8..7aa03ae184 100644 --- a/src/app/core/global_singleton.nim +++ b/src/app/core/global_singleton.nim @@ -2,9 +2,11 @@ import NimQml import local_account_settings import local_account_sensitive_settings +import local_app_settings export local_account_settings export local_account_sensitive_settings +export local_app_settings type GlobalSingleton = object @@ -34,7 +36,15 @@ proc localAccountSensitiveSettings*(self: GlobalSingleton): LocalAccountSensitiv return localAccountSensitiveSettings +proc localAppSettings*(self: GlobalSingleton): LocalAppSettings = + var localAppSettings {.global.}: LocalAppSettings + if (localAppSettings.isNil): + localAppSettings = newLocalAppSettings("global") + + return localAppSettings + proc delete*(self: GlobalSingleton) = self.engine.delete() self.localAccountSettings.delete() - self.localAccountSensitiveSettings.delete() \ No newline at end of file + self.localAccountSensitiveSettings.delete() + self.localAppSettings.delete() \ No newline at end of file diff --git a/src/app/core/local_app_settings.nim b/src/app/core/local_app_settings.nim new file mode 100644 index 0000000000..10e54d5963 --- /dev/null +++ b/src/app/core/local_app_settings.nim @@ -0,0 +1,64 @@ +import NimQml, os + +import ../../constants + +# Local App Settings keys: +const LAS_KEY_LOCALE* = "global/locale" +const DEFAULT_LOCALE = "en" +const LAS_KEY_THEME* = "global/theme" +const DEFAULT_THEME = 2 #system theme, from qml + +QtObject: + type LocalAppSettings* = ref object of QObject + settings: QSettings + + proc setup(self: LocalAppSettings) = + self.QObject.setup + + proc delete*(self: LocalAppSettings) = + self.settings.delete + + self.QObject.delete + + proc newLocalAppSettings*(fileName: string): LocalAppSettings = + new(result, delete) + result.setup + let filePath = os.joinPath(DATADIR, "qt", fileName) + result.settings = newQSettings(filePath, QSettingsFormat.IniFormat) + + + proc localeChanged*(self: LocalAppSettings) {.signal.} + proc getLocale*(self: LocalAppSettings): string {.slot.} = + self.settings.value(LAS_KEY_LOCALE, newQVariant(DEFAULT_LOCALE)).stringVal + proc setLocale*(self: LocalAppSettings, value: string) {.slot.} = + self.settings.setValue(LAS_KEY_LOCALE, newQVariant(value)) + self.localeChanged() + + QtProperty[string] locale: + read = getLocale + write = setLocale + notify = localeChanged + + + proc themeChanged*(self: LocalAppSettings) {.signal.} + proc getTheme*(self: LocalAppSettings): int {.slot.} = + self.settings.value(LAS_KEY_THEME, newQVariant(DEFAULT_THEME)).intVal + proc setTheme*(self: LocalAppSettings, value: int) {.slot.} = + self.settings.setValue(LAS_KEY_THEME, newQVariant(value)) + self.themeChanged() + + QtProperty[int] theme: + read = getTheme + write = setTheme + notify = themeChanged + + + proc removeKey*(self: LocalAppSettings, key: string) = + if(self.settings.isNil): + return + + self.settings.remove(key) + + case key: + of LAS_KEY_LOCALE: self.localeChanged() + of LAS_KEY_THEME: self.themeChanged() \ No newline at end of file diff --git a/ui/app/AppLayouts/Chat/ChatLayout.qml b/ui/app/AppLayouts/Chat/ChatLayout.qml index 2bd41e7eb4..4f35126013 100644 --- a/ui/app/AppLayouts/Chat/ChatLayout.qml +++ b/ui/app/AppLayouts/Chat/ChatLayout.qml @@ -82,7 +82,7 @@ StatusAppThreePanelLayout { searchResults: root.rootStore.chatsModelInst.messageSearchViewController.resultModel formatTimestampFn: function (ts) { - return new Date(parseInt(ts, 10)).toLocaleString(Qt.locale(globalSettings.locale)) + return new Date(parseInt(ts, 10)).toLocaleString(Qt.locale(localAppSettings.locale)) } onSearchTextChanged: { diff --git a/ui/app/AppLayouts/Chat/controls/FetchMoreMessagesButton.qml b/ui/app/AppLayouts/Chat/controls/FetchMoreMessagesButton.qml index 11af8acc98..2d403a52c0 100644 --- a/ui/app/AppLayouts/Chat/controls/FetchMoreMessagesButton.qml +++ b/ui/app/AppLayouts/Chat/controls/FetchMoreMessagesButton.qml @@ -70,7 +70,7 @@ Item { horizontalAlignment: Text.AlignHCenter color: Style.current.secondaryText //% "before %1" - text: qsTrId("before--1").arg((nextMessageIndex > -1 ? new Date(nextMsgTimestamp * 1) : new Date()).toLocaleString(Qt.locale(globalSettings.locale))) + text: qsTrId("before--1").arg((nextMessageIndex > -1 ? new Date(nextMsgTimestamp * 1) : new Date()).toLocaleString(Qt.locale(localAppSettings.locale))) } Separator { anchors.top: fetchDate.bottom diff --git a/ui/app/AppLayouts/Chat/panels/ChatTimePanel.qml b/ui/app/AppLayouts/Chat/panels/ChatTimePanel.qml index 9fe0dd6b38..7c9b8184cf 100644 --- a/ui/app/AppLayouts/Chat/panels/ChatTimePanel.qml +++ b/ui/app/AppLayouts/Chat/panels/ChatTimePanel.qml @@ -14,7 +14,7 @@ StyledText { StatusQ.StatusToolTip { visible: hhandler.hovered - text: new Date(parseInt(timestamp, 10)).toLocaleString(Qt.locale(globalSettings.locale)) + text: new Date(parseInt(timestamp, 10)).toLocaleString(Qt.locale(localAppSettings.locale)) maxWidth: 350 } diff --git a/ui/app/AppLayouts/Profile/popups/LanguageModal.qml b/ui/app/AppLayouts/Profile/popups/LanguageModal.qml index 1103bc3bf8..30ae072aa5 100644 --- a/ui/app/AppLayouts/Profile/popups/LanguageModal.qml +++ b/ui/app/AppLayouts/Profile/popups/LanguageModal.qml @@ -74,10 +74,9 @@ ModalPopup { anchors.leftMargin: 0 title: modelData.name buttonGroup: languageGroup - checked: globalSettings.locale === modelData.locale + checked: localAppSettings.locale === modelData.locale onCheckedChanged: { - if (checked && globalSettings.locale !== modelData.locale) { - globalSettings.locale = modelData.locale + if (checked && localAppSettings.locale !== modelData.locale) { if (Qt.platform.os === Constants.linux) { languageChangeConfirmationDialog.item.newLocale = modelData.locale languageChangeConfirmationDialog.item.open() diff --git a/ui/app/AppLayouts/Profile/views/AppearanceView.qml b/ui/app/AppLayouts/Profile/views/AppearanceView.qml index 3709b2cf83..98e106c1c5 100644 --- a/ui/app/AppLayouts/Profile/views/AppearanceView.qml +++ b/ui/app/AppLayouts/Profile/views/AppearanceView.qml @@ -31,7 +31,7 @@ ScrollView { } function updateTheme(theme) { - globalSettings.theme = theme + localAppSettings.theme = theme Style.changeTheme(theme, systemPalette.isCurrentSystemThemeDark()) } @@ -312,7 +312,7 @@ ScrollView { image.height: 128 //% "Light" control.text: qsTrId("light") - control.checked: globalSettings.theme === Universal.Light + control.checked: localAppSettings.theme === Universal.Light onRadioCheckedChanged: { if (checked) { root.updateTheme(Universal.Light) @@ -328,7 +328,7 @@ ScrollView { image.height: 128 //% "Dark" control.text: qsTrId("dark") - control.checked: globalSettings.theme === Universal.Dark + control.checked: localAppSettings.theme === Universal.Dark onRadioCheckedChanged: { if (checked) { root.updateTheme(Universal.Dark) @@ -344,7 +344,7 @@ ScrollView { image.height: 128 //% "System" control.text: qsTrId("system") - control.checked: globalSettings.theme === Universal.System + control.checked: localAppSettings.theme === Universal.System onRadioCheckedChanged: { if (checked) { root.updateTheme(Universal.System) diff --git a/ui/app/AppLayouts/Profile/views/LanguageView.qml b/ui/app/AppLayouts/Profile/views/LanguageView.qml index 3633df165a..5326480ac6 100644 --- a/ui/app/AppLayouts/Profile/views/LanguageView.qml +++ b/ui/app/AppLayouts/Profile/views/LanguageView.qml @@ -36,7 +36,7 @@ Item { StatusListItem { //% "Language" title: qsTrId("language") - label: globalSettings.locale === "" ? qsTrId("default") : globalSettings.locale + label: localAppSettings.locale === "" ? qsTrId("default") : localAppSettings.locale components: [ StatusIcon { icon: "chevron-down" diff --git a/ui/app/AppLayouts/Wallet/controls/AssetDelegate.qml b/ui/app/AppLayouts/Wallet/controls/AssetDelegate.qml index f9e1a22756..41c3c1b16e 100644 --- a/ui/app/AppLayouts/Wallet/controls/AssetDelegate.qml +++ b/ui/app/AppLayouts/Wallet/controls/AssetDelegate.qml @@ -58,7 +58,7 @@ Item { StyledText { id: assetFiatValue color: Style.current.secondaryText - text: Utils.toLocaleString(fiatBalance, globalSettings.locale) + " " + assetDelegate.defaultCurrency.toUpperCase() + text: Utils.toLocaleString(fiatBalance, localAppSettings.locale) + " " + assetDelegate.defaultCurrency.toUpperCase() anchors.right: parent.right anchors.rightMargin: 0 anchors.bottom: parent.bottom diff --git a/ui/app/AppLayouts/Wallet/controls/TransactionDelegate.qml b/ui/app/AppLayouts/Wallet/controls/TransactionDelegate.qml index 0a97cdabb8..0ea7f0a21e 100644 --- a/ui/app/AppLayouts/Wallet/controls/TransactionDelegate.qml +++ b/ui/app/AppLayouts/Wallet/controls/TransactionDelegate.qml @@ -141,7 +141,7 @@ Rectangle { } StyledText { id: timeValue - text: new Date(timestamp).toLocaleString(globalSettings.locale) + text: new Date(timestamp).toLocaleString(localAppSettings.locale) font.pixelSize: Style.current.primaryTextFontSize anchors.rightMargin: Style.current.smallPadding } diff --git a/ui/app/AppLayouts/Wallet/controls/WalletDelegate.qml b/ui/app/AppLayouts/Wallet/controls/WalletDelegate.qml index 58501c6919..4122f66409 100644 --- a/ui/app/AppLayouts/Wallet/controls/WalletDelegate.qml +++ b/ui/app/AppLayouts/Wallet/controls/WalletDelegate.qml @@ -78,7 +78,7 @@ Rectangle { } StyledText { id: walletBalance - text: isLoading ? "..." : Utils.toLocaleString(fiatBalance, globalSettings.locale, {"currency": true}) + " " + walletDelegate.defaultCurrency.toUpperCase() + text: isLoading ? "..." : Utils.toLocaleString(fiatBalance, localAppSettings.locale, {"currency": true}) + " " + walletDelegate.defaultCurrency.toUpperCase() anchors.top: parent.top anchors.topMargin: Style.current.smallPadding anchors.right: parent.right diff --git a/ui/app/AppLayouts/Wallet/views/LeftTabView.qml b/ui/app/AppLayouts/Wallet/views/LeftTabView.qml index 0fd5e53dff..32b36e88a3 100644 --- a/ui/app/AppLayouts/Wallet/views/LeftTabView.qml +++ b/ui/app/AppLayouts/Wallet/views/LeftTabView.qml @@ -48,7 +48,7 @@ Rectangle { StyledTextEdit { id: walletAmountValue color: Style.current.textColor - text: Utils.toLocaleString(RootStore.totalFiatBalance, globalSettings.locale, {"currency": true}) + " " + RootStore.defaultCurrency.toUpperCase() + text: Utils.toLocaleString(RootStore.totalFiatBalance, localAppSettings.locale, {"currency": true}) + " " + RootStore.defaultCurrency.toUpperCase() selectByMouse: true cursorVisible: true readOnly: true diff --git a/ui/app/AppLayouts/WalletV2/views/AssetsView.qml b/ui/app/AppLayouts/WalletV2/views/AssetsView.qml index a46d88dff4..47a0ad7398 100644 --- a/ui/app/AppLayouts/WalletV2/views/AssetsView.qml +++ b/ui/app/AppLayouts/WalletV2/views/AssetsView.qml @@ -63,7 +63,7 @@ Item { StatusBaseText { id: assetFiatValue color: Style.current.secondaryText - text: Utils.toLocaleString(fiatBalance, globalSettings.locale) + " " + walletModel.balanceView.defaultCurrency.toUpperCase() + text: Utils.toLocaleString(fiatBalance, localAppSettings.locale) + " " + walletModel.balanceView.defaultCurrency.toUpperCase() anchors.right: parent.right anchors.rightMargin: 0 anchors.bottom: parent.bottom diff --git a/ui/app/AppLayouts/WalletV2/views/LeftTabView.qml b/ui/app/AppLayouts/WalletV2/views/LeftTabView.qml index c68952a22f..abc2cf0f53 100644 --- a/ui/app/AppLayouts/WalletV2/views/LeftTabView.qml +++ b/ui/app/AppLayouts/WalletV2/views/LeftTabView.qml @@ -48,7 +48,7 @@ Rectangle { font.weight: Font.Medium font.pixelSize: 30 //TOOD improve this to not use dynamic scoping - text: Utils.toLocaleString("0.00", globalSettings.locale, {"currency": true}) + " " + "USD" + text: Utils.toLocaleString("0.00", localAppSettings.locale, {"currency": true}) + " " + "USD" } StyledText { @@ -134,7 +134,7 @@ Rectangle { font.pixelSize: 15 font.weight: Font.Medium color: Style.current.textColor - text: isLoading ? "..." : Utils.toLocaleString(fiatBalance, globalSettings.locale, {"currency": true}) + " " + "USD" + text: isLoading ? "..." : Utils.toLocaleString(fiatBalance, localAppSettings.locale, {"currency": true}) + " " + "USD" } MouseArea { anchors.fill: parent diff --git a/ui/main.qml b/ui/main.qml index c2039f5c8f..87eb9aa414 100644 --- a/ui/main.qml +++ b/ui/main.qml @@ -28,18 +28,6 @@ StatusWindow { Universal.theme: Universal.System - Settings { - id: globalSettings - category: "global" - fileName: profileModel.globalSettingsFile - property string locale: "en" - property int theme: 2 - - Component.onCompleted: { - profileModel.changeLocale(locale) - } - } - id: applicationWindow objectName: "mainWindow" minimumWidth: 900 @@ -154,11 +142,11 @@ StatusWindow { } function changeThemeFromOutside() { - Style.changeTheme(globalSettings.theme, systemPalette.isCurrentSystemThemeDark()) + Style.changeTheme(localAppSettings.theme, systemPalette.isCurrentSystemThemeDark()) } Component.onCompleted: { - Style.changeTheme(globalSettings.theme, systemPalette.isCurrentSystemThemeDark()) + Style.changeTheme(localAppSettings.theme, systemPalette.isCurrentSystemThemeDark()) setX(Qt.application.screens[0].width / 2 - width / 2); setY(Qt.application.screens[0].height / 2 - height / 2);