diff --git a/package.json b/package.json index 416c18770..7f02f9bfb 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.23.1", + "tauri-plugin-autostart-api": "github:tauri-apps/tauri-plugin-autostart#v1", "tauri-plugin-clipboard-api": "^1.0.1", "valtio": "^1.13.2", "valtio-persist": "^1.0.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 003dc375b..dd403c670 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,6 +32,9 @@ dependencies: react-router-dom: specifier: ^6.23.1 version: 6.23.1(react-dom@18.3.1)(react@18.3.1) + tauri-plugin-autostart-api: + specifier: github:tauri-apps/tauri-plugin-autostart#v1 + version: github.com/tauri-apps/tauri-plugin-autostart/a8455f120ad801d828f91528f1b4ca47d00c8ba3 tauri-plugin-clipboard-api: specifier: ^1.0.1 version: 1.0.1 @@ -1329,6 +1332,11 @@ packages: dev: true optional: true + /@tauri-apps/api@1.5.3: + resolution: {integrity: sha512-zxnDjHHKjOsrIzZm6nO5Xapb/BxqUq1tc7cGkFXsFkGTsSWgCPH1D8mm0XS9weJY2OaR73I3k3S+b7eSzJDfqA==} + engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'} + dev: false + /@tauri-apps/api@1.5.6: resolution: {integrity: sha512-LH5ToovAHnDVe5Qa9f/+jW28I6DeMhos8bNDtBOmmnaDpPmJmYLyHdeDblAWWWYc7KKRDg9/66vMuKyq0WIeFA==} engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'} @@ -4912,3 +4920,11 @@ packages: /zod@3.23.8: resolution: {integrity: sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==} dev: false + + github.com/tauri-apps/tauri-plugin-autostart/a8455f120ad801d828f91528f1b4ca47d00c8ba3: + resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-autostart/tar.gz/a8455f120ad801d828f91528f1b4ca47d00c8ba3} + name: tauri-plugin-autostart-api + version: 0.0.0 + dependencies: + '@tauri-apps/api': 1.5.3 + dev: false diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 55d937274..0cd554cab 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -352,6 +352,17 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "auto-launch" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f012b8cc0c850f34117ec8252a44418f2e34a2cf501de89e29b241ae5f79471" +dependencies = [ + "dirs", + "thiserror", + "winreg 0.10.1", +] + [[package]] name = "autocfg" version = "1.3.0" @@ -998,6 +1009,15 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs-next" version = "2.0.0" @@ -1008,6 +1028,17 @@ dependencies = [ "dirs-sys-next", ] +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dirs-sys-next" version = "0.1.2" @@ -1069,6 +1100,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tauri-plugin-autostart", "tauri-plugin-clipboard", "tauri-plugin-single-instance", "tauri-plugin-theme", @@ -4480,6 +4512,19 @@ dependencies = [ "tauri-utils", ] +[[package]] +name = "tauri-plugin-autostart" +version = "0.0.0" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#10a4d76fb3e76dc0fe240dcb4530886985c45135" +dependencies = [ + "auto-launch", + "log", + "serde", + "serde_json", + "tauri", + "thiserror", +] + [[package]] name = "tauri-plugin-clipboard" version = "0.6.10" @@ -4498,7 +4543,7 @@ dependencies = [ [[package]] name = "tauri-plugin-single-instance" version = "0.0.0" -source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#5e3900e682e13f3759b439116ae2f77a6d389ca2" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#10a4d76fb3e76dc0fe240dcb4530886985c45135" dependencies = [ "log", "serde", @@ -5873,6 +5918,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" +dependencies = [ + "winapi", +] + [[package]] name = "winreg" version = "0.50.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 6b1e27b0d..e55627231 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" [build-dependencies] tauri-build = { version = "1", features = [] } + [dependencies] tauri = { version = "1", features = [ "system-tray", "macos-private-api", "api-all"] } serde = { version = "1", features = ["derive"] } @@ -18,6 +19,7 @@ tauri-plugin-clipboard = "0.6.10" window-vibrancy = "0.4.3" tauri-plugin-theme = "0.2.0" tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } +tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } [features] # This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!! diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e4015aee7..24a7d87cc 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,6 +5,7 @@ use tauri::{generate_context, generate_handler, ActivationPolicy, Builder, Manag use tauri_plugin_theme::ThemePlugin; mod tray; mod window; +use tauri_plugin_autostart::MacosLauncher; use window::{ create_window, frosted_window, hide_window, quit_app, show_window, MAIN_WINDOW_LABEL, }; @@ -37,6 +38,11 @@ fn main() { show_window(window); })) + // app 自启动:https://github.com/tauri-apps/tauri-plugin-autostart + .plugin(tauri_plugin_autostart::init( + MacosLauncher::LaunchAgent, + Some(vec!["--flag1", "--flag2"]), + )) // 系统托盘:https://tauri.app/v1/guides/features/system-tray .system_tray(tray::menu()) .on_system_tray_event(tray::handler) diff --git a/src/pages/Settings/components/Autostart/index.tsx b/src/pages/Settings/components/Autostart/index.tsx new file mode 100644 index 000000000..fe46ed115 --- /dev/null +++ b/src/pages/Settings/components/Autostart/index.tsx @@ -0,0 +1,31 @@ +import { Switch } from "antd"; +import { disable, enable, isEnabled } from "tauri-plugin-autostart-api"; +import { useSnapshot } from "valtio"; + +const Autostart = () => { + const { autostart } = useSnapshot(store); + + useMount(async () => { + const enabled = await isEnabled(); + + if (enabled === autostart) return; + + handleChange(); + }); + + useUpdateEffect(() => { + if (autostart) { + enable(); + } else { + disable(); + } + }, [autostart]); + + const handleChange = () => { + store.autostart = !store.autostart; + }; + + return ; +}; + +export default Autostart; diff --git a/src/pages/Settings/index.tsx b/src/pages/Settings/index.tsx index 942d528b8..69fb11e6e 100644 --- a/src/pages/Settings/index.tsx +++ b/src/pages/Settings/index.tsx @@ -1,5 +1,6 @@ import { appWindow } from "@tauri-apps/api/window"; -import { Card, Flex, Segmented, Switch } from "antd"; +import { Card, Flex, Segmented } from "antd"; +import Autostart from "./components/Autostart"; import ThemeMode from "./components/ThemeMode"; const Settings = () => { @@ -17,7 +18,7 @@ const Settings = () => { 开机自启: - + {/* 唤醒窗口: diff --git a/src/stores/index.tsx b/src/stores/index.tsx index 145832208..687663f80 100644 --- a/src/stores/index.tsx +++ b/src/stores/index.tsx @@ -5,7 +5,7 @@ import proxyWithPersist, { PersistStrategy } from "valtio-persist"; import { subscribeKey } from "valtio/utils"; export const store = proxyWithPersist({ - name: "EcoCopy", + name: "global", initialState: { theme: "auto", isDark: false,