forked from Bin-Huang/chatbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
55 lines (46 loc) · 1.74 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as api from '@tauri-apps/api'
export const getVersion = async () => {
return api.app.getVersion()
}
export const openLink = async (url: string) => {
return api.shell.open(url)
}
export const writeStore = async (key: string, value: any) => {
const config = await readConfig()
config[key] = value
await api.fs.writeTextFile('config.json', JSON.stringify(config), { dir: api.fs.Dir.AppConfig })
}
export const readStore = async (key: string) => {
const config = await readConfig()
return config[key]
}
async function readConfig() {
const dirExists = await api.fs.exists('', { dir: api.fs.Dir.AppConfig })
if (!dirExists) {
try {
await api.fs.createDir('', { dir: api.fs.Dir.AppConfig })
} catch (e) {
console.log('ensure app config dir', e)
}
}
const configExists = await api.fs.exists('config.json', { dir: api.fs.Dir.AppConfig })
if (!configExists) {
try {
// 从旧版本迁移
const oldConfig = await api.fs.readTextFile('chatbox/config.json', { dir: api.fs.Dir.LocalData })
await api.fs.writeTextFile('config.json', oldConfig, { dir: api.fs.Dir.AppConfig })
} catch (e) {
console.log(e)
await api.fs.writeTextFile('config.json', '{}', { dir: api.fs.Dir.AppConfig })
}
}
const configJson = await api.fs.readTextFile('config.json', { dir: api.fs.Dir.AppConfig })
return JSON.parse(configJson)
}
export const shouldUseDarkColors = async (): Promise<boolean> => {
const theme = await api.window.appWindow.theme()
return theme === 'dark'
}
export async function onSystemThemeChange(callback: () => void) {
return api.window.appWindow.onThemeChanged(callback)
}