Skip to content

Commit 21775bc

Browse files
committed
支持开机启动后最小化
1 parent 17867c2 commit 21775bc

File tree

12 files changed

+360
-66
lines changed

12 files changed

+360
-66
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "one-click-launch",
33
"private": true,
4-
"version": "1.0.1",
4+
"version": "1.0.3",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "one-click-launch"
3-
version = "1.0.2"
3+
version = "1.0.3"
44
description = "One Click Launch"
55
authors = ["Silwings"]
66
edition = "2024"

src-tauri/src/api/setting_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ where
6262
}
6363
}
6464

65-
fn string_to_bool(s: &str) -> bool {
65+
pub fn string_to_bool(s: &str) -> bool {
6666
matches!(s.trim().to_lowercase().as_str(), "true")
6767
}

src-tauri/src/api/window_api.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use crate::{
2222
use super::{launcher_api, setting_api};
2323

2424
pub fn hide_window(app: &AppHandle) -> Result<(), OneClickLaunchError> {
25-
let window = app.get_webview_window("main").unwrap();
25+
let window = app
26+
.get_webview_window(constants::MAIN_WINDOW_LABEL)
27+
.unwrap();
2628
window.hide()?;
2729
Ok(())
2830
}
@@ -57,7 +59,7 @@ pub async fn refresh_tray(app: AppHandle) -> Result<(), OneClickLaunchError> {
5759
}
5860

5961
pub fn change_windows_theme(app: &AppHandle, theme: &str) -> Result<(), OneClickLaunchError> {
60-
if let Some(window) = app.get_webview_window("main") {
62+
if let Some(window) = app.get_webview_window(constants::MAIN_WINDOW_LABEL) {
6163
window.set_theme(match theme {
6264
"dark" => Some(Theme::Dark),
6365
"light" => Some(Theme::Light),
@@ -133,20 +135,6 @@ pub fn handle_window_event(window: &tauri::Window, event: &tauri::WindowEvent) {
133135
}
134136
_ => {}
135137
},
136-
tauri::WindowEvent::Resized(physical_size) => {
137-
if physical_size.width == 0 && physical_size.height == 0 {
138-
// 页面最小化时忽略
139-
return;
140-
}
141-
// 如果窗口大小过小,强制调整到正常大小
142-
if physical_size.width < WINDOW_MIN_WIDTH || physical_size.height < WINDOW_MIN_HEIGHT {
143-
let _ = window.set_size(tauri::Size::Logical(tauri::LogicalSize {
144-
width: WINDOW_MIN_WIDTH as f64,
145-
height: WINDOW_MIN_HEIGHT as f64,
146-
}));
147-
debug!("窗口大小过小,触发窗口大小重置: ps: {:?}", physical_size);
148-
}
149-
}
150138
_ => {}
151139
}
152140
}
@@ -169,7 +157,7 @@ pub fn setup_tray(app: &AppHandle) -> Result<()> {
169157
} => {
170158
// 当用户点击系统托盘时使应用程序取消最小化,显示并聚焦
171159
let app = tray.app_handle();
172-
if let Some(window) = app.get_webview_window("main") {
160+
if let Some(window) = app.get_webview_window(constants::MAIN_WINDOW_LABEL) {
173161
let _ = window.unminimize();
174162
let _ = window.show();
175163
let _ = window.set_focus();

src-tauri/src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ lazy_static! {
55
}
66

77
pub static APPLICATION_NAME: &str = "一键启动";
8+
pub static MAIN_WINDOW_LABEL: &str = "main";
89
pub static THEME_KEY: &str = "theme";
910
pub static CLOSE_MAIN_PANEL_KEY: &str = "close_main_panel";
11+
pub static AUTO_START_LAUNCHER_IDS_KEY: &str = "auto_start_launcher_ids";
12+
pub static HIDE_AFTER_AUTO_START_KEY: &str = "hide_after_auto_start";
1013
pub static CLOSE_MAIN_PANEL_EXIT: &str = "m2";
1114
pub static WINDOW_MIN_WIDTH: u32 = 800;
1215
pub static WINDOW_MIN_HEIGHT: u32 = 600;

src-tauri/src/db/settings.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ where
4646
.bind(key)
4747
.fetch_optional(executor)
4848
.await?;
49-
println!("s :{:?}", settings);
5049
Ok(settings)
5150
}
5251

src-tauri/src/events/system_listeners.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ use tracing::{debug, error};
33

44
use crate::{
55
DatabaseManager,
6-
api::{launcher_api, setting_api::check_launch_then_exit, window_api},
7-
constants::{AUTO_START_FLAG, THEME_KEY},
6+
api::{
7+
launcher_api,
8+
setting_api::{self, check_launch_then_exit},
9+
window_api,
10+
},
11+
constants::{
12+
self, AUTO_START_FLAG, AUTO_START_LAUNCHER_IDS_KEY, HIDE_AFTER_AUTO_START_KEY, THEME_KEY,
13+
},
814
db::{launcher_resource, settings},
915
events::EventDispatcher,
1016
};
@@ -50,6 +56,7 @@ fn register_application_startup_complete_listeners(app: &AppHandle) {
5056
let app_cloned = app.clone();
5157
EventSystem::register_listener(app, ApplicationStartupComplete, move |payload| {
5258
debug!("application_startup_complete_listeners 处理中");
59+
hide_after_auto_start(&app_cloned, &payload);
5360
refresh_tray(&app_cloned);
5461
launch_auto_start_launchers(&app_cloned, &payload);
5562
debug!("application_startup_complete_listeners 处理完成");
@@ -78,7 +85,46 @@ fn refresh_tray(app: &AppHandle) {
7885
});
7986
}
8087

81-
/// 注册应用程序启动完成监听器: 启动自启动启动器
88+
/// 应用程序自动启动后隐藏
89+
fn hide_after_auto_start(app: &AppHandle, payload: &ApplicationStartupCompletePayload) {
90+
if payload.args.contains(&AUTO_START_FLAG) {
91+
debug!(
92+
"hide_after_auto_start 判断为自动启动, 命令行参数: {:?}",
93+
payload.args
94+
);
95+
96+
let inner_app = app.clone();
97+
98+
tauri::async_runtime::spawn(async move {
99+
let app_cloned = inner_app.clone();
100+
101+
let db_manager = app_cloned.state::<DatabaseManager>();
102+
103+
let window = app_cloned
104+
.get_webview_window(constants::MAIN_WINDOW_LABEL)
105+
.unwrap();
106+
107+
match settings::read(&db_manager.pool, HIDE_AFTER_AUTO_START_KEY)
108+
.await
109+
.ok()
110+
.flatten()
111+
{
112+
Some(settings) if setting_api::string_to_bool(&settings.value) => {
113+
let _ = window.hide();
114+
}
115+
_ => {
116+
let _ = window.show();
117+
}
118+
}
119+
});
120+
} else {
121+
let window = app
122+
.get_webview_window(constants::MAIN_WINDOW_LABEL)
123+
.unwrap();
124+
let _ = window.show();
125+
}
126+
}
127+
82128
fn launch_auto_start_launchers(app: &AppHandle, payload: &ApplicationStartupCompletePayload) {
83129
// 检查启动参数, 当命令包含`--auto`时表示是操作系统触发的自动启动
84130
if payload.args.contains(&AUTO_START_FLAG) {
@@ -95,7 +141,7 @@ fn launch_auto_start_launchers(app: &AppHandle, payload: &ApplicationStartupComp
95141
let db_manager = app_cloned.state::<DatabaseManager>();
96142

97143
if let Ok(Some(settings)) =
98-
settings::read(&db_manager.pool, "auto_start_launcher_ids").await
144+
settings::read(&db_manager.pool, AUTO_START_LAUNCHER_IDS_KEY).await
99145
{
100146
if let Ok(auto_start_launcher_ids) =
101147
serde_json::from_str::<Vec<i64>>(&settings.value)

src-tauri/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ pub async fn run() -> Result<()> {
104104

105105
tauri::Builder::default()
106106
.setup(move |app| {
107-
// 初始化窗口
108-
setup_tray(app.handle())?;
109-
110107
// 注册监听器,之后添加新的监听器时在这个方法内部添加
111108
register_listeners(app.handle());
112109

110+
// 初始化窗口
111+
setup_tray(app.handle())?;
112+
113113
// 当所有初始化都完成后发送应用程序启动完成事件
114114
EventDispatcher::<ApplicationStartupComplete>::send_event(
115115
app.handle(),
@@ -128,7 +128,9 @@ pub async fn run() -> Result<()> {
128128
// 优先注册单例插件
129129
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
130130
info!("{}, {argv:?}, {cwd}", app.package_info().name);
131-
let windows = app.get_webview_window("main").unwrap();
131+
let windows = app
132+
.get_webview_window(constants::MAIN_WINDOW_LABEL)
133+
.unwrap();
132134
if windows.is_visible().unwrap() {
133135
let _ = windows.unmaximize();
134136
}

src-tauri/tauri.conf.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://schema.tauri.app/config/2",
33
"productName": "一键启动",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"identifier": "one-click-launch",
66
"build": {
77
"beforeDevCommand": "yarn dev",
@@ -16,9 +16,7 @@
1616
"title": "一键启动",
1717
"width": 800,
1818
"height": 600,
19-
"minWidth": 800,
20-
"minHeight": 600,
21-
"devtools": true
19+
"visible": false
2220
}
2321
],
2422
"security": {

0 commit comments

Comments
 (0)