[feat] power plan get and set completed
This commit is contained in:
@@ -2,10 +2,16 @@ import { BatteryCharge, Refresh } from "@icon-park/react"
|
||||
import { invoke } from "@tauri-apps/api/core"
|
||||
import { Card, CardBody, CardHeader, CardIcon, CardTool } from "../window/Card"
|
||||
import { ToolButton } from "../window/ToolButton"
|
||||
import { Tabs, Tab } from "@heroui/react"
|
||||
import { Tabs, Tab, addToast } from "@heroui/react"
|
||||
import { Key } from "@react-types/shared"
|
||||
import { useToolStore } from "@/store/tool"
|
||||
import { useEffect } from "react"
|
||||
|
||||
const PowerPlans = [
|
||||
{
|
||||
id: "0",
|
||||
title: "其他",
|
||||
},
|
||||
{
|
||||
id: "1",
|
||||
title: "节能",
|
||||
@@ -25,10 +31,30 @@ const PowerPlans = [
|
||||
]
|
||||
|
||||
const PowerPlan = () => {
|
||||
const handleSelectionChange = (key: Key) => {
|
||||
console.log(key)
|
||||
const tool = useToolStore()
|
||||
const setPowerPlan = async (key: Key) => {
|
||||
await tool.store.start()
|
||||
const plan: number = Number(key)
|
||||
|
||||
await invoke("set_powerplan", { plan: plan })
|
||||
const current = await invoke<number>("get_powerplan")
|
||||
tool.setPowerPlan(current)
|
||||
|
||||
addToast({ title: "电源计划已切换 → " + PowerPlans[current].title })
|
||||
}
|
||||
|
||||
const getPowerPlan = async (toast: boolean) => {
|
||||
await tool.store.start()
|
||||
const current = await invoke<number>("get_powerplan")
|
||||
tool.setPowerPlan(current)
|
||||
|
||||
if (toast) addToast({ title: "电源计划已切换 → " + PowerPlans[current].title })
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
void getPowerPlan(false)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
@@ -36,38 +62,23 @@ const PowerPlan = () => {
|
||||
<BatteryCharge /> 电源计划
|
||||
</CardIcon>
|
||||
<CardTool>
|
||||
<ToolButton>
|
||||
<ToolButton onClick={() => getPowerPlan(true)}>
|
||||
<Refresh />
|
||||
刷新
|
||||
</ToolButton>
|
||||
</CardTool>
|
||||
</CardHeader>
|
||||
<CardBody>
|
||||
{/* <div className="flex p-0.5 bg-black/5 gap-2 rounded-full">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => invoke("set_power_plan", { index: 1 })}
|
||||
className="flex-1 px-2 py-1 transition rounded-full select-none bg-black/5"
|
||||
>
|
||||
高性能
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => invoke("set_power_plan", { index: 2 })}
|
||||
className="flex-1 px-2 py-1 transition rounded-full select-none"
|
||||
>
|
||||
平衡
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => invoke("set_power_plan", { index: 3 })}
|
||||
className="flex-1 px-2 py-1 transition rounded-full select-none"
|
||||
>
|
||||
节能
|
||||
</button>
|
||||
</div> */}
|
||||
<Tabs aria-label="Power Plan Tabs" size="md" radius="full" fullWidth defaultSelectedKey="0" onSelectionChange={handleSelectionChange}>
|
||||
{PowerPlans.map((item) => (
|
||||
<Tabs
|
||||
selectedKey={String(tool.state.powerPlan)}
|
||||
onSelectionChange={setPowerPlan}
|
||||
aria-label="Power Plan Tabs"
|
||||
size="md"
|
||||
radius="full"
|
||||
fullWidth
|
||||
defaultSelectedKey="0"
|
||||
>
|
||||
{PowerPlans.slice(1).map((item) => (
|
||||
<Tab key={item.id} title={item.title}></Tab>
|
||||
))}
|
||||
</Tabs>
|
||||
|
||||
Reference in New Issue
Block a user