Skip to content

Commit

Permalink
feat: 新增 通用设置 > 应用设置 > 静默启动 配置项 (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Oct 17, 2024
1 parent 2dbe92a commit ffe1dee
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 43 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ tauri-plugin-eco-locale = { path = "./src-tauri/src/plugins/locale" }
tauri-plugin-eco-clipboard = { path = "./src-tauri/src/plugins/clipboard" }
tauri-plugin-eco-ocr = { path = "./src-tauri/src/plugins/ocr" }
tauri-plugin-eco-paste = { path = "./src-tauri/src/plugins/paste" }
tauri-plugin-eco-window-state = { path = "./src-tauri/src/plugins/window-state" }
tauri-plugin-eco-window-state = { path = "./src-tauri/src/plugins/window-state" }
tauri-plugin-eco-autostart = { path = "./src-tauri/src/plugins/autostart" }
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tauri-plugin-eco-clipboard.workspace = true
tauri-plugin-eco-ocr.workspace = true
tauri-plugin-eco-paste.workspace = true
tauri-plugin-eco-window-state.workspace = true
tauri-plugin-eco-autostart.workspace = true

[target."cfg(target_os = \"macos\")".dependencies]
cocoa.workspace = true
3 changes: 2 additions & 1 deletion src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"eco-locale:default",
"eco-clipboard:default",
"eco-ocr:default",
"eco-paste:default"
"eco-paste:default",
"eco-autostart:default"
]
}
15 changes: 2 additions & 13 deletions src-tauri/src/core/setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::AUTO_LAUNCH_ARG;
use std::env;
use tauri::{App, Manager, WebviewWindow};
use tauri_plugin_eco_window::show_preference_window;
use tauri::{App, WebviewWindow};

#[cfg(target_os = "macos")]
mod mac;
Expand All @@ -22,15 +19,7 @@ pub use win::*;
pub use linux::*;

pub fn default(app: &mut App, main_window: WebviewWindow, preference_window: WebviewWindow) {
let app_handle = app.app_handle();

// 判断是否为自动启动
let args: Vec<String> = env::args().collect();
if !args.contains(&AUTO_LAUNCH_ARG.to_string()) {
show_preference_window(&app_handle);
}

// 自动打开控制台:https://tauri.app/develop/debug
// 开发模式自动打开控制台:https://tauri.app/develop/debug
#[cfg(any(dev, debug_assertions))]
main_window.open_devtools();

Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_eco_window::{show_main_window, MAIN_WINDOW_LABEL, PREFERENCE_WINDOW_LABEL};
use tauri_plugin_log::{Target, TargetKind};

pub const AUTO_LAUNCH_ARG: &str = "--auto-launch";

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let app = Builder::default()
Expand All @@ -31,7 +29,7 @@ pub fn run() {
// app 自启动:https://github.com/tauri-apps/tauri-plugin-autostart/tree/v2
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
Some(vec![AUTO_LAUNCH_ARG]),
Some(vec!["--auto-launch"]),
))
// 数据库:https://github.com/tauri-apps/tauri-plugin-sql/tree/v2
.plugin(tauri_plugin_sql::Builder::default().build())
Expand Down Expand Up @@ -77,6 +75,8 @@ pub fn run() {
.plugin(tauri_plugin_eco_macos_permissions::init())
// 自定义保存和恢复窗口状态的插件
.plugin(tauri_plugin_eco_window_state::init())
// 自定义判断是否自动启动的插件
.plugin(tauri_plugin_eco_autostart::init())
.on_window_event(|window, event| match event {
// 让 app 保持在后台运行:https://tauri.app/v1/guides/features/system-tray/#preventing-the-app-from-closing
WindowEvent::CloseRequested { api, .. } => {
Expand Down
15 changes: 15 additions & 0 deletions src-tauri/src/plugins/autostart/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "tauri-plugin-eco-autostart"
version = "0.1.0"
authors = []
description = ""
edition = "2021"
rust-version = "1.77.2"
links = "tauri-plugin-eco-autostart"

[dependencies]
tauri.workspace = true
serde.workspace = true

[build-dependencies]
tauri-plugin.workspace = true
5 changes: 5 additions & 0 deletions src-tauri/src/plugins/autostart/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const COMMANDS: &[&str] = &["is_autostart"];

fn main() {
tauri_plugin::Builder::new(COMMANDS).build();
}
5 changes: 5 additions & 0 deletions src-tauri/src/plugins/autostart/permissions/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"$schema" = "schemas/schema.json"

[default]
description = "Default permissions for the plugin"
permissions = ["allow-is-autostart"]
10 changes: 10 additions & 0 deletions src-tauri/src/plugins/autostart/src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::env::args;
use tauri::command;

// 是否为自动启动
#[command]
pub async fn is_autostart() -> bool {
let args: Vec<String> = args().collect();

return args.contains(&"--auto-launch".to_string());
}
13 changes: 13 additions & 0 deletions src-tauri/src/plugins/autostart/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use tauri::{
generate_handler,
plugin::{Builder, TauriPlugin},
Runtime,
};

mod commands;

pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("eco-autostart")
.invoke_handler(generate_handler![commands::is_autostart])
.build()
}
41 changes: 18 additions & 23 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ export const GITHUB_LINK = "https://github.com/EcoPasteHub/EcoPaste";

export const GITHUB_ISSUES_LINK = `${GITHUB_LINK}/issues`;

export const TRAY_ID = "app-tray";

export const UPDATE_MESSAGE_KEY = "app-update-message";

export const WINDOW_LABEL = {
MAIN: "main",
PREFERENCE: "preference",
} as const;

export const LANGUAGE = {
ZH_CN: "zh-CN",
ZH_TW: "zh-TW",
EN_US: "en-US",
JA_JP: "ja-JP",
} as const;

export const LISTEN_KEY = {
STORE_CHANGED: "store-changed",
UPDATE_APP: "update-app",
Expand Down Expand Up @@ -56,24 +72,12 @@ export const OCR_PLUGIN = {
SYSTEM_OCR: "plugin:eco-ocr|system_ocr",
};

export const THEME_PLUGIN = {
GET_THEME: "plugin:theme|get_theme",
SET_THEME: "plugin:theme|set_theme",
};

export const BACKUP_PLUGIN = {
EXPORT_DATA: "plugin:eco-backup|export_data",
IMPORT_DATA: "plugin:eco-backup|import_data",
MOVE_DATA: "plugin:eco-backup|move_data",
};

export const LANGUAGE = {
ZH_CN: "zh-CN",
ZH_TW: "zh-TW",
EN_US: "en-US",
JA_JP: "ja-JP",
} as const;

export const LOCALE_PLUGIN = {
GET_LOCALE: "plugin:eco-locale|get_locale",
};
Expand All @@ -91,15 +95,6 @@ export const MACOS_PERMISSIONS_PLUGIN = {
"plugin:eco-macos-permissions|request_full_disk_access_permissions",
};

export const WINDOW_LABEL = {
MAIN: "main",
PREFERENCE: "preference",
} as const;

export const UPDATER_PLUGIN = {
CHECK_UPDATE: "plugin:updater|check_update",
export const AUTOSTART_PLUGIN = {
IS_AUTOSTART: "plugin:eco-autostart|is_autostart",
};

export const TRAY_ID = "app-tray";

export const UPDATE_MESSAGE_KEY = "app-update-message";
10 changes: 8 additions & 2 deletions src/layouts/Preference/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import { subscribe, useSnapshot } from "valtio";

const Preference = () => {
const { pathname } = useLocation();
const { shortcut } = useSnapshot(globalStore);
const { shortcut, app } = useSnapshot(globalStore);
const { t } = useTranslation();

useMount(() => {
useMount(async () => {
const autostart = await isAutostart();

if (!autostart && !app.silentStart) {
showWindow();
}

const appWindow = getCurrentWebviewWindow();

// 监听全局配置项变化
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@
"title": "App Settings",
"label": {
"auto_start": "Start at Login",
"silent_start": "Silent Start",
"show_menubar_icon": "Show Menubar Icon",
"show_taskbar_icon": "Show Taskbar Icon"
},
"hints": {
"silent_start": "Hide windows when launching applications manually"
}
},
"appearance_settings": {
Expand Down
4 changes: 4 additions & 0 deletions src/locales/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@
"title": "ソフト設定",
"label": {
"auto_start": "ログイン時に起動",
"silent_start": "サイレントブート",
"show_menubar_icon": "メニューバーアイコンを表示する",
"show_taskbar_icon": "タスクバーアイコンを表示する"
},
"hints": {
"silent_start": "アプリケーションの手動起動時にウィンドウを隠す"
}
},
"appearance_settings": {
Expand Down
4 changes: 4 additions & 0 deletions src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@
"title": "应用设置",
"label": {
"auto_start": "登录时启动",
"silent_start": "静默启动",
"show_menubar_icon": "显示菜单栏图标",
"show_taskbar_icon": "显示任务栏图标"
},
"hints": {
"silent_start": "手动启动应用时隐藏窗口"
}
},
"appearance_settings": {
Expand Down
4 changes: 4 additions & 0 deletions src/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@
"title": "應用設定",
"label": {
"auto_start": "登入時啟動",
"silent_start": "靜默啟動",
"show_menubar_icon": "顯示功能表列圖標",
"show_taskbar_icon": "顯示工作列圖標"
},
"hints": {
"silent_start": "手動啟動應用時隱藏視窗"
}
},
"appearance_settings": {
Expand Down
9 changes: 9 additions & 0 deletions src/pages/General/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const General = () => {
}}
/>

<ProSwitch
title={t("preference.settings.app_settings.label.silent_start")}
description={t("preference.settings.app_settings.hints.silent_start")}
value={app.silentStart}
onChange={(value) => {
globalStore.app.silentStart = value;
}}
/>

<ProSwitch
title={t("preference.settings.app_settings.label.show_menubar_icon")}
value={app.showMenubarIcon}
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/autostart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { invoke } from "@tauri-apps/api/core";

/**
* 是否为开机自动启动
*/
export const isAutostart = () => {
return invoke<boolean>(AUTOSTART_PLUGIN.IS_AUTOSTART);
};
1 change: 1 addition & 0 deletions src/stores/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const persistStrategies = PersistStrategy.MultiFile;
export const GLOBAL_STORE_INITIAL_STATE: GlobalStore = {
app: {
autoStart: false,
silentStart: false,
showMenubarIcon: true,
showTaskbarIcon: false,
},
Expand Down
1 change: 1 addition & 0 deletions src/types/store.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface GlobalStore {
// 应用设置
app: {
autoStart: boolean;
silentStart: boolean;
showMenubarIcon: boolean;
showTaskbarIcon: boolean;
};
Expand Down

0 comments on commit ffe1dee

Please sign in to comment.