refactor(@desktop/general): `globalSettings` moved to Nim
This commit is contained in:
parent
f0b2ed6cca
commit
3f6bb66536
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
self.localAppSettings.delete()
|
|
@ -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()
|
|
@ -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: {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
16
ui/main.qml
16
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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue