Skip to content

Commit

Permalink
feat: 支持 app 开机自启动
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Jun 4, 2024
1 parent d5b8cf9 commit 7e68518
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 55 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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!!
Expand Down
6 changes: 6 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions src/pages/Settings/components/Autostart/index.tsx
Original file line number Diff line number Diff line change
@@ -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 <Switch checked={autostart} onChange={handleChange} />;
};

export default Autostart;
5 changes: 3 additions & 2 deletions src/pages/Settings/index.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand All @@ -17,7 +18,7 @@ const Settings = () => {
<Flex vertical gap="large">
<Flex align="center">
<span>开机自启:</span>
<Switch />
<Autostart />
</Flex>
{/* <Flex align="center">
<span>唤醒窗口:</span>
Expand Down
2 changes: 1 addition & 1 deletion src/stores/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import proxyWithPersist, { PersistStrategy } from "valtio-persist";
import { subscribeKey } from "valtio/utils";

export const store = proxyWithPersist<GlobalStore>({
name: "EcoCopy",
name: "global",
initialState: {
theme: "auto",
isDark: false,
Expand Down

0 comments on commit 7e68518

Please sign in to comment.