2023-07-11 18:06:44 +08:00
|
|
|
import { defineStore } from 'pinia'
|
2023-08-02 17:57:39 +08:00
|
|
|
import { lang } from '@/langs/index.js'
|
2023-07-21 14:37:39 +08:00
|
|
|
import { camelCase, clone, find, get, isEmpty, isObject, map, set, snakeCase, split } from 'lodash'
|
2023-07-12 11:48:08 +08:00
|
|
|
import {
|
2023-08-26 02:40:30 +08:00
|
|
|
CheckForUpdate,
|
2023-07-12 11:48:08 +08:00
|
|
|
GetFontList,
|
|
|
|
GetPreferences,
|
|
|
|
RestorePreferences,
|
|
|
|
SetPreferences,
|
2023-08-02 17:57:39 +08:00
|
|
|
} from 'wailsjs/go/services/preferencesService.js'
|
2023-08-20 15:33:51 +08:00
|
|
|
import { BrowserOpenURL } from 'wailsjs/runtime/runtime.js'
|
|
|
|
import { i18nGlobal } from '@/utils/i18n.js'
|
2023-09-02 21:08:15 +08:00
|
|
|
import { enUS, useOsTheme, zhCN } from 'naive-ui'
|
2023-07-11 18:06:44 +08:00
|
|
|
|
2023-08-24 00:44:19 +08:00
|
|
|
const osTheme = useOsTheme()
|
2023-07-11 18:06:44 +08:00
|
|
|
const usePreferencesStore = defineStore('preferences', {
|
2023-07-12 11:48:08 +08:00
|
|
|
/**
|
|
|
|
* @typedef {Object} FontItem
|
|
|
|
* @property {string} name
|
|
|
|
* @property {string} path
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @typedef {Object} Preferences
|
|
|
|
* @property {Object} general
|
|
|
|
* @property {Object} editor
|
|
|
|
* @property {FontItem[]} fontList
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {Preferences}
|
|
|
|
*/
|
2023-07-11 18:06:44 +08:00
|
|
|
state: () => ({
|
|
|
|
general: {
|
2023-08-25 00:07:29 +08:00
|
|
|
theme: 'auto',
|
2023-07-11 18:06:44 +08:00
|
|
|
language: 'en',
|
|
|
|
font: '',
|
|
|
|
fontSize: 14,
|
|
|
|
useSysProxy: false,
|
|
|
|
useSysProxyHttp: false,
|
|
|
|
checkUpdate: false,
|
2023-07-13 15:46:12 +08:00
|
|
|
asideWidth: 300,
|
2023-07-11 18:06:44 +08:00
|
|
|
},
|
|
|
|
editor: {
|
|
|
|
font: '',
|
|
|
|
fontSize: 14,
|
|
|
|
},
|
2023-07-12 11:48:08 +08:00
|
|
|
lastPref: {},
|
|
|
|
fontList: [],
|
2023-07-11 18:06:44 +08:00
|
|
|
}),
|
|
|
|
getters: {
|
|
|
|
getSeparator() {
|
|
|
|
return ':'
|
|
|
|
},
|
|
|
|
|
2023-07-13 15:46:12 +08:00
|
|
|
themeOption() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
value: 'light',
|
2023-08-20 15:33:51 +08:00
|
|
|
label: i18nGlobal.t('theme_light'),
|
2023-07-13 15:46:12 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'dark',
|
2023-08-20 15:33:51 +08:00
|
|
|
label: i18nGlobal.t('theme_dark'),
|
2023-07-13 15:46:12 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'auto',
|
2023-08-20 15:33:51 +08:00
|
|
|
label: i18nGlobal.t('theme_auto'),
|
2023-07-13 15:46:12 +08:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2023-07-11 18:06:44 +08:00
|
|
|
/**
|
|
|
|
* all available language
|
|
|
|
* @returns {{label: string, value: string}[]}
|
|
|
|
*/
|
|
|
|
langOption() {
|
2023-07-21 14:37:39 +08:00
|
|
|
const options = Object.entries(lang).map(([key, value]) => ({
|
2023-07-11 18:06:44 +08:00
|
|
|
value: key,
|
2023-08-26 00:49:44 +08:00
|
|
|
label: value['name'],
|
2023-07-11 18:06:44 +08:00
|
|
|
}))
|
2023-07-21 14:37:39 +08:00
|
|
|
options.splice(0, 0, {
|
|
|
|
value: 'auto',
|
2023-08-20 15:33:51 +08:00
|
|
|
label: i18nGlobal.t('system_lang'),
|
2023-07-21 14:37:39 +08:00
|
|
|
})
|
|
|
|
return options
|
2023-07-11 18:06:44 +08:00
|
|
|
},
|
2023-07-12 11:48:08 +08:00
|
|
|
|
2023-07-13 15:46:12 +08:00
|
|
|
/**
|
|
|
|
* all system font list
|
|
|
|
* @returns {{path: string, label: string, value: string}[]}
|
|
|
|
*/
|
2023-07-12 11:48:08 +08:00
|
|
|
fontOption() {
|
|
|
|
const option = map(this.fontList, (font) => ({
|
|
|
|
value: font.name,
|
|
|
|
label: font.name,
|
|
|
|
path: font.path,
|
|
|
|
}))
|
|
|
|
option.splice(0, 0, {
|
|
|
|
value: '',
|
2023-08-20 15:33:51 +08:00
|
|
|
label: i18nGlobal.t('default'),
|
2023-07-12 11:48:08 +08:00
|
|
|
path: '',
|
|
|
|
})
|
|
|
|
return option
|
|
|
|
},
|
|
|
|
|
2023-07-13 15:46:12 +08:00
|
|
|
/**
|
|
|
|
* current font selection
|
|
|
|
* @returns {{fontSize: string}}
|
|
|
|
*/
|
2023-07-12 11:48:08 +08:00
|
|
|
generalFont() {
|
|
|
|
const fontStyle = {
|
|
|
|
fontSize: this.general.fontSize + 'px',
|
|
|
|
}
|
|
|
|
if (!isEmpty(this.general.font) && this.general.font !== 'none') {
|
|
|
|
const font = find(this.fontList, { name: this.general.font })
|
|
|
|
if (font != null) {
|
|
|
|
fontStyle['fontFamily'] = `${font.name}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fontStyle
|
|
|
|
},
|
2023-07-21 14:37:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get current language setting
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
currentLanguage() {
|
|
|
|
let lang = get(this.general, 'language', 'auto')
|
|
|
|
if (lang === 'auto') {
|
|
|
|
const systemLang = navigator.language || navigator.userLanguage
|
|
|
|
lang = split(systemLang, '-')[0]
|
|
|
|
}
|
|
|
|
return lang || 'en'
|
|
|
|
},
|
2023-08-20 15:46:06 +08:00
|
|
|
|
2023-08-24 00:44:19 +08:00
|
|
|
isDark() {
|
|
|
|
const th = get(this.general, 'theme', 'auto')
|
2023-08-25 00:07:29 +08:00
|
|
|
if (th !== 'auto') {
|
|
|
|
return th === 'dark'
|
|
|
|
} else {
|
|
|
|
return osTheme.value === 'dark'
|
2023-08-24 00:44:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-08-28 11:56:51 +08:00
|
|
|
themeLocale() {
|
|
|
|
const lang = this.currentLanguage
|
|
|
|
switch (lang) {
|
|
|
|
case 'zh':
|
|
|
|
return zhCN
|
|
|
|
default:
|
|
|
|
return enUS
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-08-20 15:46:06 +08:00
|
|
|
autoCheckUpdate() {
|
|
|
|
return get(this.general, 'checkUpdate', false)
|
|
|
|
},
|
2023-07-11 18:06:44 +08:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
_applyPreferences(data) {
|
|
|
|
for (const key in data) {
|
|
|
|
const keys = map(split(key, '.'), camelCase)
|
|
|
|
set(this, keys, data[key])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* load preferences from local
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async loadPreferences() {
|
|
|
|
const { success, data } = await GetPreferences()
|
|
|
|
if (success) {
|
2023-07-12 11:48:08 +08:00
|
|
|
this.lastPref = clone(data)
|
2023-07-11 18:06:44 +08:00
|
|
|
this._applyPreferences(data)
|
2023-08-26 00:49:44 +08:00
|
|
|
i18nGlobal.locale.value = this.currentLanguage
|
2023-07-11 18:06:44 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-07-12 11:48:08 +08:00
|
|
|
/**
|
|
|
|
* load system font list
|
|
|
|
* @returns {Promise<string[]>}
|
|
|
|
*/
|
|
|
|
async loadFontList() {
|
|
|
|
const { success, data } = await GetFontList()
|
|
|
|
if (success) {
|
|
|
|
const { fonts = [] } = data
|
|
|
|
this.fontList = fonts
|
|
|
|
} else {
|
|
|
|
this.fontList = []
|
|
|
|
}
|
|
|
|
return this.fontList
|
|
|
|
},
|
|
|
|
|
2023-07-11 18:06:44 +08:00
|
|
|
/**
|
|
|
|
* save preferences to local
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async savePreferences() {
|
|
|
|
const obj2Map = (prefix, obj) => {
|
|
|
|
const result = {}
|
|
|
|
for (const key in obj) {
|
|
|
|
if (isObject(obj[key])) {
|
2023-07-12 11:48:08 +08:00
|
|
|
const subResult = obj2Map(`${prefix}.${snakeCase(key)}`, obj[key])
|
|
|
|
Object.assign(result, subResult)
|
2023-07-11 18:06:44 +08:00
|
|
|
} else {
|
|
|
|
result[`${prefix}.${snakeCase(key)}`] = obj[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
const pf = Object.assign({}, obj2Map('general', this.general), obj2Map('editor', this.editor))
|
|
|
|
const { success, msg } = await SetPreferences(pf)
|
|
|
|
return success === true
|
|
|
|
},
|
|
|
|
|
2023-07-12 11:48:08 +08:00
|
|
|
/**
|
|
|
|
* reset to last loaded preferences
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async resetToLastPreferences() {
|
|
|
|
if (!isEmpty(this.lastPref)) {
|
|
|
|
this._applyPreferences(this.lastPref)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-07-11 18:06:44 +08:00
|
|
|
/**
|
|
|
|
* restore preferences to default
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async restorePreferences() {
|
|
|
|
const { success, data } = await RestorePreferences()
|
|
|
|
if (success === true) {
|
|
|
|
const { pref } = data
|
|
|
|
this._applyPreferences(pref)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2023-07-12 15:48:49 +08:00
|
|
|
|
2023-07-13 15:46:12 +08:00
|
|
|
setAsideWidth(width) {
|
|
|
|
this.general.asideWidth = width
|
2023-07-12 15:48:49 +08:00
|
|
|
},
|
2023-08-20 15:46:06 +08:00
|
|
|
|
|
|
|
async checkForUpdate(manual = false) {
|
|
|
|
let msgRef = null
|
|
|
|
if (manual) {
|
2023-08-24 00:44:19 +08:00
|
|
|
msgRef = $message.loading('Retrieving for new version', { duration: 0 })
|
2023-08-20 15:46:06 +08:00
|
|
|
}
|
|
|
|
try {
|
2023-08-26 02:40:30 +08:00
|
|
|
const { success, data = {} } = await CheckForUpdate()
|
|
|
|
if (success) {
|
|
|
|
const { version, latest, pageUrl } = data
|
|
|
|
if (latest > version) {
|
|
|
|
$dialog.warning(i18nGlobal.t('new_version_tip'), () => {
|
|
|
|
BrowserOpenURL(pageUrl)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manual) {
|
|
|
|
$message.info(i18nGlobal.t('no_update'))
|
2023-08-20 15:46:06 +08:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (msgRef != null) {
|
|
|
|
msgRef.destroy()
|
|
|
|
msgRef = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-07-11 18:06:44 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export default usePreferencesStore
|