perf: optimize language loading and switching timing.
This commit is contained in:
parent
83ffb0aaed
commit
7fe7b8f9bc
|
@ -79,6 +79,26 @@ func (p *PreferencesStorage) GetPreferences() (ret map[string]any) {
|
||||||
return
|
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 {
|
func (p *PreferencesStorage) setPreferences(pf map[string]any, key string, value any) error {
|
||||||
keyPath := strings.Split(key, ".")
|
keyPath := strings.Split(key, ".")
|
||||||
if len(keyPath) <= 0 {
|
if len(keyPath) <= 0 {
|
||||||
|
|
|
@ -30,7 +30,6 @@ const initializing = ref(false)
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
try {
|
try {
|
||||||
initializing.value = true
|
initializing.value = true
|
||||||
i18n.locale.value = prefStore.currentLanguage
|
|
||||||
await connectionStore.initConnections()
|
await connectionStore.initConnections()
|
||||||
if (prefStore.autoCheckUpdate) {
|
if (prefStore.autoCheckUpdate) {
|
||||||
prefStore.checkForUpdate()
|
prefStore.checkForUpdate()
|
||||||
|
@ -40,10 +39,17 @@ onMounted(async () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// watch theme and dynamically switch
|
||||||
watch(
|
watch(
|
||||||
() => prefStore.isDark,
|
() => prefStore.isDark,
|
||||||
(isDark) => (isDark ? WindowSetDarkTheme() : WindowSetLightTheme()),
|
(isDark) => (isDark ? WindowSetDarkTheme() : WindowSetLightTheme()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// watch language and dynamically switch
|
||||||
|
watch(
|
||||||
|
() => prefStore.general.language,
|
||||||
|
(lang) => (i18n.locale.value = prefStore.currentLanguage),
|
||||||
|
)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
|
@ -44,12 +44,6 @@ const onSavePreferences = async () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch language and dynamically switch
|
|
||||||
watch(
|
|
||||||
() => prefStore.general.language,
|
|
||||||
(lang) => (i18n.locale.value = prefStore.currentLanguage),
|
|
||||||
)
|
|
||||||
|
|
||||||
const onClose = () => {
|
const onClose = () => {
|
||||||
// restore to old preferences
|
// restore to old preferences
|
||||||
prefStore.resetToLastPreferences()
|
prefStore.resetToLastPreferences()
|
||||||
|
|
|
@ -32,7 +32,7 @@ const emit = defineEmits(['update:value'])
|
||||||
|
|
||||||
const iconSize = computed(() => Math.floor(props.width * 0.4))
|
const iconSize = computed(() => Math.floor(props.width * 0.4))
|
||||||
const renderIcon = (icon) => {
|
const renderIcon = (icon) => {
|
||||||
return () => h(NIcon, null, { default: () => h(icon) })
|
return () => h(NIcon, null, { default: () => h(icon, { strokeWidth: 4 }) })
|
||||||
}
|
}
|
||||||
|
|
||||||
const connectionStore = useConnectionStore()
|
const connectionStore = useConnectionStore()
|
||||||
|
@ -122,7 +122,7 @@ const openGithub = () => {
|
||||||
:render-label="renderContextLabel"
|
:render-label="renderContextLabel"
|
||||||
trigger="click"
|
trigger="click"
|
||||||
@select="onSelectPreferenceMenu">
|
@select="onSelectPreferenceMenu">
|
||||||
<icon-button :icon="Config" :size="iconSize" class="nav-menu-button" />
|
<icon-button :icon="Config" :size="iconSize" stroke-width="4" class="nav-menu-button" />
|
||||||
</n-dropdown>
|
</n-dropdown>
|
||||||
<icon-button :icon="Github" :size="iconSize" class="nav-menu-button" @click="openGithub" />
|
<icon-button :icon="Github" :size="iconSize" class="nav-menu-button" @click="openGithub" />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"lang_name": "English",
|
"name": "English",
|
||||||
"system_lang": "Use System Language",
|
"system_lang": "Use System Language",
|
||||||
"confirm": "Confirm",
|
"confirm": "Confirm",
|
||||||
"cancel": "Cancel",
|
"cancel": "Cancel",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"lang_name": "简体中文",
|
"name": "简体中文",
|
||||||
"system_lang": "使用系统语言",
|
"system_lang": "使用系统语言",
|
||||||
"confirm": "确认",
|
"confirm": "确认",
|
||||||
"cancel": "取消",
|
"cancel": "取消",
|
||||||
|
|
|
@ -75,7 +75,7 @@ const usePreferencesStore = defineStore('preferences', {
|
||||||
langOption() {
|
langOption() {
|
||||||
const options = Object.entries(lang).map(([key, value]) => ({
|
const options = Object.entries(lang).map(([key, value]) => ({
|
||||||
value: key,
|
value: key,
|
||||||
label: value['lang_name'],
|
label: value['name'],
|
||||||
}))
|
}))
|
||||||
options.splice(0, 0, {
|
options.splice(0, 0, {
|
||||||
value: 'auto',
|
value: 'auto',
|
||||||
|
@ -162,6 +162,7 @@ const usePreferencesStore = defineStore('preferences', {
|
||||||
if (success) {
|
if (success) {
|
||||||
this.lastPref = clone(data)
|
this.lastPref = clone(data)
|
||||||
this._applyPreferences(data)
|
this._applyPreferences(data)
|
||||||
|
i18nGlobal.locale.value = this.currentLanguage
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue