[fix] auto launch persist state
This commit is contained in:
@@ -1,16 +1,11 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import { useAppStore } from "@/store/app"
|
import { useAppStore } from "@/store/app"
|
||||||
import { Switch } from "@heroui/react"
|
import { Switch } from "@heroui/react"
|
||||||
import { enable, isEnabled, disable } from "@tauri-apps/plugin-autostart"
|
import { isEnabled } from "@tauri-apps/plugin-autostart"
|
||||||
import { useEffect, useState } from "react"
|
import { useEffect } from "react"
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const app = useAppStore()
|
const app = useAppStore()
|
||||||
const [autoStartEnabled, setAutoStartEnabled] = useState(false)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
void isEnabled().then(setAutoStartEnabled)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col items-start gap-3 pt-2 pb-1">
|
<div className="flex flex-col items-start gap-3 pt-2 pb-1">
|
||||||
@@ -18,17 +13,11 @@ export default function Page() {
|
|||||||
<p>是否有更新:{app.state.hasUpdate ? "有" : "无"}</p>
|
<p>是否有更新:{app.state.hasUpdate ? "有" : "无"}</p>
|
||||||
<p>是否使用镜像源:{app.state.useMirror ? "是" : "否"}</p>
|
<p>是否使用镜像源:{app.state.useMirror ? "是" : "否"}</p>
|
||||||
<Switch
|
<Switch
|
||||||
checked={autoStartEnabled}
|
isSelected={app.state.autoStart}
|
||||||
size="sm"
|
size="sm"
|
||||||
onChange={(e) => {
|
onChange={(e) => app.setAutoStart(e.target.checked)}
|
||||||
if (e.target.checked) {
|
|
||||||
void enable().then(() => setAutoStartEnabled(true))
|
|
||||||
} else {
|
|
||||||
void disable().then(() => setAutoStartEnabled(false))
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
开机自启动
|
开机自启动 {app.state.autoStart ? "开" : "关"}
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { store } from "@tauri-store/valtio"
|
import { store } from "@tauri-store/valtio"
|
||||||
import { useSnapshot } from "valtio"
|
import { useSnapshot } from "valtio"
|
||||||
import { DEFAULT_STORE_CONFIG } from "./config"
|
import { DEFAULT_STORE_CONFIG } from "./config"
|
||||||
|
import { enable, isEnabled, disable } from "@tauri-apps/plugin-autostart"
|
||||||
|
|
||||||
const defaultValue = {
|
const defaultValue = {
|
||||||
version: "0.0.1",
|
version: "0.0.1",
|
||||||
@@ -8,6 +9,7 @@ const defaultValue = {
|
|||||||
inited: false,
|
inited: false,
|
||||||
notice: "",
|
notice: "",
|
||||||
useMirror: true,
|
useMirror: true,
|
||||||
|
autoStart: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const appStore = store("app", { ...defaultValue }, DEFAULT_STORE_CONFIG)
|
export const appStore = store("app", { ...defaultValue }, DEFAULT_STORE_CONFIG)
|
||||||
@@ -24,6 +26,7 @@ export const useAppStore = () => {
|
|||||||
setInited,
|
setInited,
|
||||||
setNotice,
|
setNotice,
|
||||||
setUseMirror,
|
setUseMirror,
|
||||||
|
setAutoStart,
|
||||||
resetAppStore,
|
resetAppStore,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,10 +47,20 @@ const setUseMirror = (useMirror: boolean) => {
|
|||||||
appStore.state.useMirror = useMirror
|
appStore.state.useMirror = useMirror
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setAutoStart = (autoStart: boolean) => {
|
||||||
|
if (autoStart) {
|
||||||
|
void enable()
|
||||||
|
} else {
|
||||||
|
void disable()
|
||||||
|
}
|
||||||
|
appStore.state.autoStart = autoStart
|
||||||
|
}
|
||||||
|
|
||||||
const resetAppStore = () => {
|
const resetAppStore = () => {
|
||||||
setVersion(defaultValue.version)
|
setVersion(defaultValue.version)
|
||||||
setHasUpdate(defaultValue.hasUpdate)
|
setHasUpdate(defaultValue.hasUpdate)
|
||||||
setInited(defaultValue.inited)
|
setInited(defaultValue.inited)
|
||||||
setNotice(defaultValue.notice)
|
setNotice(defaultValue.notice)
|
||||||
setUseMirror(defaultValue.useMirror)
|
setUseMirror(defaultValue.useMirror)
|
||||||
|
setAutoStart(defaultValue.autoStart)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user