refactor(@desktop/general): `globalSettings` moved to Nim

This commit is contained in:
Sale Djenic 2021-10-20 12:55:10 +02:00 committed by Iuri Matias
parent f0b2ed6cca
commit 3f6bb66536
16 changed files with 110 additions and 43 deletions

View File

@ -45,14 +45,17 @@ elif (defined(macosx)):
elif (defined(linux)): elif (defined(linux)):
i18nPath = joinPath(getAppDir(), "../i18n") 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) = proc changeLanguage(locale: string) =
let currentLanguageCode = singletonInstance.localAppSettings.getLocale()
if (locale == currentLanguageCode): if (locale == currentLanguageCode):
return return
currentLanguageCode = locale
let shouldRetranslate = not defined(linux) singletonInstance.localAppSettings.setLocale(locale)
singletonInstance.engine.setTranslationPackage( setLanguage(locale)
joinPath(i18nPath, fmt"qml_{locale}.qm"), shouldRetranslate)
type type
AppController* = ref object of RootObj AppController* = ref object of RootObj
@ -76,6 +79,7 @@ type
aboutService: about_service.Service aboutService: about_service.Service
# Core # Core
localAppSettingsVariant: QVariant
localAccountSettingsVariant: QVariant localAccountSettingsVariant: QVariant
localAccountSensitiveSettingsVariant: QVariant localAccountSensitiveSettingsVariant: QVariant
@ -143,6 +147,7 @@ proc newAppController*(appService: AppService): AppController =
result.aboutService = about_service.newService() result.aboutService = about_service.newService()
# Core # Core
result.localAppSettingsVariant = newQVariant(singletonInstance.localAppSettings)
result.localAccountSettingsVariant = newQVariant(singletonInstance.localAccountSettings) result.localAccountSettingsVariant = newQVariant(singletonInstance.localAccountSettings)
result.localAccountSensitiveSettingsVariant = newQVariant(singletonInstance.localAccountSensitiveSettings) result.localAccountSensitiveSettingsVariant = newQVariant(singletonInstance.localAccountSensitiveSettings)
# Modules # Modules
@ -206,6 +211,7 @@ proc delete*(self: AppController) =
self.profile.delete self.profile.delete
################################################# #################################################
self.localAppSettingsVariant.delete
self.localAccountSettingsVariant.delete self.localAccountSettingsVariant.delete
self.localAccountSensitiveSettingsVariant.delete self.localAccountSensitiveSettingsVariant.delete
@ -228,14 +234,14 @@ proc startupDidLoad*(self: AppController) =
singletonInstance.engine.setRootContextProperty("profileModel", self.profile.variant) singletonInstance.engine.setRootContextProperty("profileModel", self.profile.variant)
################################################# #################################################
# We're applying default language before we load qml. Also we're aware that singletonInstance.engine.setRootContextProperty("localAppSettings", self.localAppSettingsVariant)
# 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("localAccountSettings", self.localAccountSettingsVariant) singletonInstance.engine.setRootContextProperty("localAccountSettings", self.localAccountSettingsVariant)
singletonInstance.engine.load(newQUrl("qrc:///main.qml")) 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) = proc mainDidLoad*(self: AppController) =
self.appService.onLoggedIn() self.appService.onLoggedIn()
self.startupModule.moveToAppState() self.startupModule.moveToAppState()

View File

@ -2,9 +2,11 @@ import NimQml
import local_account_settings import local_account_settings
import local_account_sensitive_settings import local_account_sensitive_settings
import local_app_settings
export local_account_settings export local_account_settings
export local_account_sensitive_settings export local_account_sensitive_settings
export local_app_settings
type type
GlobalSingleton = object GlobalSingleton = object
@ -34,7 +36,15 @@ proc localAccountSensitiveSettings*(self: GlobalSingleton): LocalAccountSensitiv
return localAccountSensitiveSettings return localAccountSensitiveSettings
proc localAppSettings*(self: GlobalSingleton): LocalAppSettings =
var localAppSettings {.global.}: LocalAppSettings
if (localAppSettings.isNil):
localAppSettings = newLocalAppSettings("global")
return localAppSettings
proc delete*(self: GlobalSingleton) = proc delete*(self: GlobalSingleton) =
self.engine.delete() self.engine.delete()
self.localAccountSettings.delete() self.localAccountSettings.delete()
self.localAccountSensitiveSettings.delete() self.localAccountSensitiveSettings.delete()
self.localAppSettings.delete()

View File

@ -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()

View File

@ -82,7 +82,7 @@ StatusAppThreePanelLayout {
searchResults: root.rootStore.chatsModelInst.messageSearchViewController.resultModel searchResults: root.rootStore.chatsModelInst.messageSearchViewController.resultModel
formatTimestampFn: function (ts) { 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: { onSearchTextChanged: {

View File

@ -70,7 +70,7 @@ Item {
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
color: Style.current.secondaryText color: Style.current.secondaryText
//% "before %1" //% "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 { Separator {
anchors.top: fetchDate.bottom anchors.top: fetchDate.bottom

View File

@ -14,7 +14,7 @@ StyledText {
StatusQ.StatusToolTip { StatusQ.StatusToolTip {
visible: hhandler.hovered 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 maxWidth: 350
} }

View File

@ -74,10 +74,9 @@ ModalPopup {
anchors.leftMargin: 0 anchors.leftMargin: 0
title: modelData.name title: modelData.name
buttonGroup: languageGroup buttonGroup: languageGroup
checked: globalSettings.locale === modelData.locale checked: localAppSettings.locale === modelData.locale
onCheckedChanged: { onCheckedChanged: {
if (checked && globalSettings.locale !== modelData.locale) { if (checked && localAppSettings.locale !== modelData.locale) {
globalSettings.locale = modelData.locale
if (Qt.platform.os === Constants.linux) { if (Qt.platform.os === Constants.linux) {
languageChangeConfirmationDialog.item.newLocale = modelData.locale languageChangeConfirmationDialog.item.newLocale = modelData.locale
languageChangeConfirmationDialog.item.open() languageChangeConfirmationDialog.item.open()

View File

@ -31,7 +31,7 @@ ScrollView {
} }
function updateTheme(theme) { function updateTheme(theme) {
globalSettings.theme = theme localAppSettings.theme = theme
Style.changeTheme(theme, systemPalette.isCurrentSystemThemeDark()) Style.changeTheme(theme, systemPalette.isCurrentSystemThemeDark())
} }
@ -312,7 +312,7 @@ ScrollView {
image.height: 128 image.height: 128
//% "Light" //% "Light"
control.text: qsTrId("light") control.text: qsTrId("light")
control.checked: globalSettings.theme === Universal.Light control.checked: localAppSettings.theme === Universal.Light
onRadioCheckedChanged: { onRadioCheckedChanged: {
if (checked) { if (checked) {
root.updateTheme(Universal.Light) root.updateTheme(Universal.Light)
@ -328,7 +328,7 @@ ScrollView {
image.height: 128 image.height: 128
//% "Dark" //% "Dark"
control.text: qsTrId("dark") control.text: qsTrId("dark")
control.checked: globalSettings.theme === Universal.Dark control.checked: localAppSettings.theme === Universal.Dark
onRadioCheckedChanged: { onRadioCheckedChanged: {
if (checked) { if (checked) {
root.updateTheme(Universal.Dark) root.updateTheme(Universal.Dark)
@ -344,7 +344,7 @@ ScrollView {
image.height: 128 image.height: 128
//% "System" //% "System"
control.text: qsTrId("system") control.text: qsTrId("system")
control.checked: globalSettings.theme === Universal.System control.checked: localAppSettings.theme === Universal.System
onRadioCheckedChanged: { onRadioCheckedChanged: {
if (checked) { if (checked) {
root.updateTheme(Universal.System) root.updateTheme(Universal.System)

View File

@ -36,7 +36,7 @@ Item {
StatusListItem { StatusListItem {
//% "Language" //% "Language"
title: qsTrId("language") title: qsTrId("language")
label: globalSettings.locale === "" ? qsTrId("default") : globalSettings.locale label: localAppSettings.locale === "" ? qsTrId("default") : localAppSettings.locale
components: [ components: [
StatusIcon { StatusIcon {
icon: "chevron-down" icon: "chevron-down"

View File

@ -58,7 +58,7 @@ Item {
StyledText { StyledText {
id: assetFiatValue id: assetFiatValue
color: Style.current.secondaryText 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.right: parent.right
anchors.rightMargin: 0 anchors.rightMargin: 0
anchors.bottom: parent.bottom anchors.bottom: parent.bottom

View File

@ -141,7 +141,7 @@ Rectangle {
} }
StyledText { StyledText {
id: timeValue id: timeValue
text: new Date(timestamp).toLocaleString(globalSettings.locale) text: new Date(timestamp).toLocaleString(localAppSettings.locale)
font.pixelSize: Style.current.primaryTextFontSize font.pixelSize: Style.current.primaryTextFontSize
anchors.rightMargin: Style.current.smallPadding anchors.rightMargin: Style.current.smallPadding
} }

View File

@ -78,7 +78,7 @@ Rectangle {
} }
StyledText { StyledText {
id: walletBalance 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.top: parent.top
anchors.topMargin: Style.current.smallPadding anchors.topMargin: Style.current.smallPadding
anchors.right: parent.right anchors.right: parent.right

View File

@ -48,7 +48,7 @@ Rectangle {
StyledTextEdit { StyledTextEdit {
id: walletAmountValue id: walletAmountValue
color: Style.current.textColor 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 selectByMouse: true
cursorVisible: true cursorVisible: true
readOnly: true readOnly: true

View File

@ -63,7 +63,7 @@ Item {
StatusBaseText { StatusBaseText {
id: assetFiatValue id: assetFiatValue
color: Style.current.secondaryText 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.right: parent.right
anchors.rightMargin: 0 anchors.rightMargin: 0
anchors.bottom: parent.bottom anchors.bottom: parent.bottom

View File

@ -48,7 +48,7 @@ Rectangle {
font.weight: Font.Medium font.weight: Font.Medium
font.pixelSize: 30 font.pixelSize: 30
//TOOD improve this to not use dynamic scoping //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 { StyledText {
@ -134,7 +134,7 @@ Rectangle {
font.pixelSize: 15 font.pixelSize: 15
font.weight: Font.Medium font.weight: Font.Medium
color: Style.current.textColor 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 { MouseArea {
anchors.fill: parent anchors.fill: parent

View File

@ -28,18 +28,6 @@ StatusWindow {
Universal.theme: Universal.System 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 id: applicationWindow
objectName: "mainWindow" objectName: "mainWindow"
minimumWidth: 900 minimumWidth: 900
@ -154,11 +142,11 @@ StatusWindow {
} }
function changeThemeFromOutside() { function changeThemeFromOutside() {
Style.changeTheme(globalSettings.theme, systemPalette.isCurrentSystemThemeDark()) Style.changeTheme(localAppSettings.theme, systemPalette.isCurrentSystemThemeDark())
} }
Component.onCompleted: { Component.onCompleted: {
Style.changeTheme(globalSettings.theme, systemPalette.isCurrentSystemThemeDark()) Style.changeTheme(localAppSettings.theme, systemPalette.isCurrentSystemThemeDark())
setX(Qt.application.screens[0].width / 2 - width / 2); setX(Qt.application.screens[0].width / 2 - width / 2);
setY(Qt.application.screens[0].height / 2 - height / 2); setY(Qt.application.screens[0].height / 2 - height / 2);