refactor: rebuild desktop app with Tauri

This commit is contained in:
leookun committed 2026-08-24 02:49:00 +08:00
1 parent a3ec2a0dfc
commit 4053a7fb20
808 files changed
+87359 -181491

No files matched your search

@@ -0,0 +1,23 @@
@use "../../styles/typography" as type;
.row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 16px;
& + & {
border-top: 1px solid var(--vscode-widget-border);
}
> div {
display: grid;
gap: 5px;
}
small {
color: var(--vscode-descriptionForeground);
font-size: type.$font-size-xs;
}
}
@@ -0,0 +1,116 @@
import { useEffect, useRef, useState } from "react";
import type { Update } from "@tauri-apps/plugin-updater";
import {
checkForUpdate,
currentAppVersion,
hasNativeAppLifecycle,
installUpdate,
readAutostart,
writeAutostart,
} from "../../native/appLifecycle";
import { Button } from "../ui/Button";
import { Switch } from "../ui/Switch";
import { TitledCard } from "../ui/TitledCard";
import { useMessage } from "../ui/message";
import styles from "./AppLifecycleSettingsCard.module.scss";
export function AppLifecycleSettingsCard() {
const message = useMessage();
const native = hasNativeAppLifecycle();
const updateRef = useRef<Update | null>(null);
const [version, setVersion] = useState("…");
const [autostart, setAutostart] = useState(false);
const [loadingAutostart, setLoadingAutostart] = useState(native);
const [checking, setChecking] = useState(false);
const [installing, setInstalling] = useState(false);
const [availableVersion, setAvailableVersion] = useState<string | null>(null);
useEffect(() => {
let disposed = false;
void currentAppVersion().then((next) => { if (!disposed) setVersion(next); });
if (native) {
void readAutostart()
.then((enabled) => { if (!disposed) setAutostart(enabled); })
.catch((cause) => message(cause instanceof Error ? cause.message : String(cause)))
.finally(() => { if (!disposed) setLoadingAutostart(false); });
}
return () => {
disposed = true;
const update = updateRef.current;
updateRef.current = null;
if (update) void update.close();
};
}, [message, native]);
const toggleAutostart = async (enabled: boolean) => {
try {
setLoadingAutostart(true);
await writeAutostart(enabled);
setAutostart(await readAutostart());
message(enabled ? t("已开启开机启动") : t("已关闭开机启动"));
} catch (cause) {
message(cause instanceof Error ? cause.message : String(cause));
} finally {
setLoadingAutostart(false);
}
};
const checkUpdate = async () => {
try {
setChecking(true);
const previous = updateRef.current;
updateRef.current = null;
if (previous) await previous.close();
const update = await checkForUpdate();
updateRef.current = update;
setAvailableVersion(update?.version ?? null);
message(update ? t("发现新版本 {version}", { version: update.version }) : t("当前已是最新版本"));
} catch (cause) {
message(cause instanceof Error ? cause.message : String(cause));
} finally {
setChecking(false);
}
};
const updateNow = async () => {
const update = updateRef.current;
if (!update) return;
try {
setInstalling(true);
await installUpdate(update);
} catch (cause) {
setInstalling(false);
message(cause instanceof Error ? cause.message : String(cause));
}
};
return <TitledCard title={t("应用设置")}>
<div className={styles.row}>
<div>
<strong>{t("开机启动")}</strong>
<small>{t("登录系统后自动启动 Cursor BYOK。")}</small>
</div>
<Switch
checked={autostart}
disabled={!native || loadingAutostart}
label={t("开机启动")}
onChange={(enabled) => void toggleAutostart(enabled)}
/>
</div>
<div className={styles.row}>
<div>
<strong>{t("软件更新")}</strong>
<small>{availableVersion
? t("版本 {version} 可以安装", { version: availableVersion })
: t("当前版本 {version}", { version })}</small>
</div>
{availableVersion
? <Button size="small" variant="primary" disabled={installing} onClick={() => void updateNow()}>
{installing ? t("安装中…") : t("下载并安装")}
</Button>
: <Button size="small" disabled={!native || checking} onClick={() => void checkUpdate()}>
{checking ? t("检查中…") : t("检查更新")}
</Button>}
</div>
</TitledCard>;
}
@@ -0,0 +1,53 @@
@use "../../styles/typography" as type;
.content,
.customFields {
display: grid;
gap: 14px;
}
.content {
padding: 16px;
}
.row {
min-width: 0;
min-height: 34px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
}
.control {
width: min(420px, 62%);
}
.value {
min-width: 0;
max-width: 70%;
color: var(--vscode-descriptionForeground);
overflow-wrap: anywhere;
text-align: right;
}
.headerAction {
padding: 4px;
color: var(--vscode-textLink-foreground);
background: transparent;
border: 0;
font-size: type.$font-size-xs;
&:hover { color: var(--vscode-textLink-activeForeground); }
&:disabled { opacity: .5; }
}
.actionGroup {
display: flex;
align-items: center;
gap: 6px;
}
@media (max-width: 680px) {
.control { width: 68%; }
}
@@ -0,0 +1,81 @@
import type { ProxySettings, ProxySettingsInput } from "../../api";
import { Button } from "../ui/Button";
import { Checkbox } from "../ui/Checkbox";
import { TextInput } from "../ui/FormControls";
import { Select } from "../ui/Select";
import { TitledCard } from "../ui/TitledCard";
import styles from "./ProxySettingsCard.module.scss";
export function ProxySettingsCard({
settings,
draft,
editing,
saving,
onDraftChange,
onEdit,
onCancel,
onSave,
}: {
settings: ProxySettings | null;
draft: ProxySettingsInput;
editing: boolean;
saving: boolean;
onDraftChange: (draft: ProxySettingsInput) => void;
onEdit: () => void;
onCancel: () => void;
onSave: () => void;
}) {
const custom = draft.mode === "custom";
const modeLabel = (mode: ProxySettingsInput["mode"]) => mode === "system" ? t("使用系统代理") : t("自定义");
const action = editing ? (
<div className={styles.actionGroup}>
<Button size="small" disabled={saving} onClick={onCancel}>{t("取消")}</Button>
<Button variant="primary" size="small" disabled={saving} onClick={onSave}>{saving ? t("保存中…") : t("保存")}</Button>
</div>
) : (
<button type="button" className={styles.headerAction} disabled={!settings} onClick={onEdit}>
{t("编辑")}
</button>
);
return <TitledCard title={t("代理设置")} action={action}>
<div className={styles.content}>
{editing ? <>
<div className={styles.row}>
<strong>{t("代理方式")}</strong>
<div className={styles.control}><Select ariaLabel={t("代理方式")} value={draft.mode} options={[{ value: "system", label: t("使用系统代理") }, { value: "custom", label: t("自定义") }]} onChange={(mode) => onDraftChange({ ...draft, mode: mode as ProxySettingsInput["mode"] })} /></div>
</div>
{custom && <div className={styles.customFields}>
<div className={styles.row}>
<strong>{t("代理地址")}</strong>
<div className={styles.control}><TextInput value={draft.address} placeholder="http://127.0.0.1:7890" onChange={(event) => onDraftChange({ ...draft, address: event.target.value })} /></div>
</div>
<div className={styles.row}>
<strong>{t("认证")}</strong>
<Checkbox checked={draft.auth_enabled} label={t("代理需要认证")} onChange={(auth_enabled) => onDraftChange({ ...draft, auth_enabled })} />
</div>
{draft.auth_enabled && <div className={styles.customFields}>
<div className={styles.row}>
<strong>{t("用户名")}</strong>
<div className={styles.control}><TextInput value={draft.username} autoComplete="off" onChange={(event) => onDraftChange({ ...draft, username: event.target.value })} /></div>
</div>
<div className={styles.row}>
<strong>{t("密码")}</strong>
<div className={styles.control}><TextInput type="password" value={draft.password ?? ""} autoComplete="new-password" placeholder={settings?.has_password ? t("留空表示保留当前密码") : ""} onChange={(event) => onDraftChange({ ...draft, password: event.target.value })} /></div>
</div>
</div>}
</div>}
</> : <>
<div className={styles.row}><strong>{t("代理方式")}</strong><span className={styles.value}>{settings ? modeLabel(settings.mode) : t("加载中…")}</span></div>
{settings?.mode === "custom" && <div className={styles.customFields}>
<div className={styles.row}><strong>{t("代理地址")}</strong><span className={styles.value}>{settings.address}</span></div>
<div className={styles.row}><strong>{t("认证")}</strong><span className={styles.value}>{settings.auth_enabled ? t("已启用") : t("未启用")}</span></div>
{settings.auth_enabled && <>
<div className={styles.row}><strong>{t("用户名")}</strong><span className={styles.value}>{settings.username || "—"}</span></div>
<div className={styles.row}><strong>{t("密码")}</strong><span className={styles.value}>{settings.has_password ? t("已设置") : t("未设置")}</span></div>
</>}
</div>}
</>}
</div>
</TitledCard>;
}