From 7fe7b8f9bcd700550a985cec0c47d7eef118b3ad Mon Sep 17 00:00:00 2001 From: tiny-craft <137850705+tiny-craft@users.noreply.github.com> Date: Sat, 26 Aug 2023 00:49:44 +0800 Subject: [PATCH] perf: optimize language loading and switching timing. --- backend/storage/preferences.go | 20 +++++++++++++++++++ frontend/src/App.vue | 8 +++++++- .../components/dialogs/PreferencesDialog.vue | 6 ------ frontend/src/components/sidebar/NavMenu.vue | 4 ++-- frontend/src/langs/en.json | 2 +- frontend/src/langs/zh-cn.json | 2 +- frontend/src/stores/preferences.js | 3 ++- 7 files changed, 33 insertions(+), 12 deletions(-) diff --git a/backend/storage/preferences.go b/backend/storage/preferences.go index d0672db..6726672 100644 --- a/backend/storage/preferences.go +++ b/backend/storage/preferences.go @@ -79,6 +79,26 @@ func (p *PreferencesStorage) GetPreferences() (ret map[string]any) { return } +func (p *PreferencesStorage) Value(keys ...string) any { + kv := p.getPreferences() + var ok bool + var a any + total := len(keys) + for i, key := range keys { + if a, ok = kv[key]; !ok { + return nil + } + if i == total-1 { + // last key, return value + return a + } + if kv, ok = a.(map[string]any); !ok { + return nil + } + } + return nil +} + func (p *PreferencesStorage) setPreferences(pf map[string]any, key string, value any) error { keyPath := strings.Split(key, ".") if len(keyPath) <= 0 { diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 9d5cc2d..ee5654e 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -30,7 +30,6 @@ const initializing = ref(false) onMounted(async () => { try { initializing.value = true - i18n.locale.value = prefStore.currentLanguage await connectionStore.initConnections() if (prefStore.autoCheckUpdate) { prefStore.checkForUpdate() @@ -40,10 +39,17 @@ onMounted(async () => { } }) +// watch theme and dynamically switch watch( () => prefStore.isDark, (isDark) => (isDark ? WindowSetDarkTheme() : WindowSetLightTheme()), ) + +// watch language and dynamically switch +watch( + () => prefStore.general.language, + (lang) => (i18n.locale.value = prefStore.currentLanguage), +)