first
This commit is contained in:
73
src/stores/settings.ts
Normal file
73
src/stores/settings.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
// filepath: src/stores/settings.ts
|
||||
import { defineStore } from 'pinia'
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export interface Settings {
|
||||
download_path: string
|
||||
theme: 'light' | 'dark' | 'system'
|
||||
last_updated: string | null
|
||||
}
|
||||
|
||||
export const useSettingsStore = defineStore('settings', () => {
|
||||
const settings = ref<Settings>({
|
||||
download_path: '',
|
||||
theme: 'system',
|
||||
last_updated: null
|
||||
})
|
||||
|
||||
const ytdlpVersion = ref('Checking...')
|
||||
const isInitializing = ref(true)
|
||||
|
||||
async function loadSettings() {
|
||||
try {
|
||||
const s = await invoke<Settings>('get_settings')
|
||||
settings.value = s
|
||||
applyTheme(s.theme)
|
||||
} catch (e) {
|
||||
console.error("Failed to load settings", e)
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
try {
|
||||
await invoke('save_settings', { settings: settings.value })
|
||||
applyTheme(settings.value.theme)
|
||||
} catch (e) {
|
||||
console.error("Failed to save settings", e)
|
||||
}
|
||||
}
|
||||
|
||||
async function initYtdlp() {
|
||||
try {
|
||||
isInitializing.value = true
|
||||
// check/download
|
||||
await invoke('init_ytdlp')
|
||||
ytdlpVersion.value = await invoke('get_ytdlp_version')
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
ytdlpVersion.value = 'Error'
|
||||
} finally {
|
||||
isInitializing.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function applyTheme(theme: string) {
|
||||
const root = document.documentElement
|
||||
const isDark = theme === 'dark' || (theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)
|
||||
if (isDark) {
|
||||
root.classList.add('dark')
|
||||
} else {
|
||||
root.classList.remove('dark')
|
||||
}
|
||||
}
|
||||
|
||||
// Watch system preference changes if theme is system
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
|
||||
if (settings.value.theme === 'system') {
|
||||
applyTheme('system')
|
||||
}
|
||||
})
|
||||
|
||||
return { settings, loadSettings, save, initYtdlp, ytdlpVersion, isInitializing }
|
||||
})
|
||||
Reference in New Issue
Block a user