Skip to content

Commit 5186336

Browse files
author
luoqiz
committed
fix: 系统托盘
1 parent 0eaefcf commit 5186336

File tree

5 files changed

+73
-71
lines changed

5 files changed

+73
-71
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": "wcf-client-rust",
33
"private": true,
4-
"version": "2024.4.28",
4+
"version": "0.0.2",
55
"type": "module",
66
"scripts": {
77
"tauri-dev": "pnpm tauri dev",

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 = "wcf-client-rust"
3-
version = "0.0.1"
3+
version = "0.0.2"
44
description = "A HTTP (Rust) client for WeChatFerry"
55
authors = ["luoqiz"]
66
edition = "2021"

src-tauri/src/main.rs

Lines changed: 67 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
33

4-
use chrono::Local;
5-
use log::{info, Level, LevelFilter, Log, Metadata, Record};
64
use std::ptr;
75
use std::sync::{Arc, Mutex};
8-
use tauri::{command, AppHandle, Manager, Window, WindowEvent};
6+
7+
use chrono::Local;
8+
use log::{info, Level, LevelFilter, Log, Metadata, Record};
9+
use tauri::{App, AppHandle, command, Manager, Window, WindowEvent};
910
use tauri::{menu::{MenuBuilder, MenuItemBuilder}, tray::{ClickType, TrayIconBuilder}};
11+
use tauri::image::Image;
12+
use tauri::tray::TrayIcon;
1013
use winapi::{
1114
shared::winerror::ERROR_ALREADY_EXISTS,
1215
um::{
@@ -16,10 +19,11 @@ use winapi::{
1619
},
1720
};
1821

22+
use http_server::HttpServer;
23+
1924
mod endpoints;
2025
mod http_server;
2126
mod wcferry;
22-
use http_server::HttpServer;
2327

2428
struct FrontendLogger {
2529
app_handle: tauri::AppHandle,
@@ -108,32 +112,32 @@ fn handle_system_tray_event(window: &Window, event: &WindowEvent) {
108112
}
109113

110114
// 初始化窗口位置(暂时不用自定义,让窗口居中显示)
111-
fn init_window(window: tauri::WebviewWindow) {
112-
window.hide().unwrap();
113-
if let Ok(Some(monitor)) = window.primary_monitor() {
114-
let monitor_size = monitor.size();
115-
if let Ok(window_size) = window.outer_size() {
116-
let x = (monitor_size.width as i32 - window_size.width as i32) / 2;
117-
let y = (monitor_size.height as i32 - window_size.height as i32) / 2;
118-
window
119-
.set_position(tauri::Position::Logical(tauri::LogicalPosition {
120-
x: x.into(),
121-
y: y.into(),
122-
}))
123-
.unwrap();
124-
} else {
125-
let x = (monitor_size.width as i32 - 640) / 2;
126-
let y = (monitor_size.height as i32 - 320) / 2;
127-
window
128-
.set_position(tauri::Position::Logical(tauri::LogicalPosition {
129-
x: x.into(),
130-
y: y.into(),
131-
}))
132-
.unwrap();
133-
}
134-
}
135-
window.show().unwrap();
136-
}
115+
// fn init_window(window: tauri::WebviewWindow) {
116+
// window.hide().unwrap();
117+
// if let Ok(Some(monitor)) = window.primary_monitor() {
118+
// let monitor_size = monitor.size();
119+
// if let Ok(window_size) = window.outer_size() {
120+
// let x = (monitor_size.width as i32 - window_size.width as i32) / 2;
121+
// let y = (monitor_size.height as i32 - window_size.height as i32) / 2;
122+
// window
123+
// .set_position(tauri::Position::Logical(tauri::LogicalPosition {
124+
// x: x.into(),
125+
// y: y.into(),
126+
// }))
127+
// .unwrap();
128+
// } else {
129+
// let x = (monitor_size.width as i32 - 640) / 2;
130+
// let y = (monitor_size.height as i32 - 320) / 2;
131+
// window
132+
// .set_position(tauri::Position::Logical(tauri::LogicalPosition {
133+
// x: x.into(),
134+
// y: y.into(),
135+
// }))
136+
// .unwrap();
137+
// }
138+
// }
139+
// window.show().unwrap();
140+
// }
137141

138142
// 初始化日志功能
139143
fn init_log(handle: AppHandle) {
@@ -142,8 +146,37 @@ fn init_log(handle: AppHandle) {
142146
.expect("Failed to initialize logger");
143147
}
144148

149+
// 初始化菜单
150+
fn init_menu(app: &mut App) {
151+
let toggle = MenuItemBuilder::with_id("quit", "退出").build(app).unwrap();
152+
let menu = MenuBuilder::new(app).items(&[&toggle]).build().unwrap();
153+
let _ = TrayIconBuilder::new()
154+
.menu(&menu)
155+
.icon(Image::from_path("icons/icon.png").unwrap())
156+
.on_menu_event(move |app, event| match event.id().as_ref() {
157+
"quit" => {
158+
app.exit(0);
159+
}
160+
_ => (),
161+
})
162+
.on_tray_icon_event(|tray, event| {
163+
if event.click_type == ClickType::Double {
164+
let app = tray.app_handle();
165+
if let Some(webview_window) = app.get_webview_window("main") {
166+
if webview_window.is_visible().unwrap() {
167+
let _ = webview_window.hide();
168+
} else {
169+
let _ = webview_window.show();
170+
let _ = webview_window.set_focus();
171+
}
172+
}
173+
}
174+
})
175+
.build(app);
176+
}
177+
145178
fn main() {
146-
179+
147180
// let mutex_name = b"Global\\wcfrust_app_mutex\0";
148181
// unsafe {
149182
// let handle = CreateMutexA(ptr::null_mut(), 0, mutex_name.as_ptr() as *const i8);
@@ -162,44 +195,15 @@ fn main() {
162195
// }
163196
// }
164197

165-
// let quit = MenuItemBuilder::new("quit".to_string()).id("quit").build(app);
166-
// let tray_menu = app::tray_menu::new().add_item(quit);
167-
// let tray = tray::TrayIconBuilder::new().with_menu(tray_menu);
168-
169-
let app1 = tauri::Builder::default()
198+
let app1 = tauri::Builder::default()
170199
.plugin(tauri_plugin_shell::init())
171200
.setup(|app| {
172201
// init_window(app.get_webview_window("main").unwrap());
173202
init_log(app.app_handle().clone());
174-
175-
#[cfg(all(desktop, not(test)))]{
176-
let toggle = MenuItemBuilder::with_id("quit", "退出").build(app)?;
177-
let menu = MenuBuilder::new(app).items(&[&toggle]).build()?;
178-
let tray = TrayIconBuilder::new()
179-
.menu(&menu)
180-
.on_menu_event(move |app, event| match event.id().as_ref() {
181-
"quit" => {
182-
println!("toggle clicked");
183-
app.exit(0);
184-
}
185-
_ => (),
186-
})
187-
.on_tray_icon_event(|tray, event| {
188-
if event.click_type == ClickType::Double {
189-
let app = tray.app_handle();
190-
if let Some(webview_window) = app.get_webview_window("main") {
191-
let _ = webview_window.show();
192-
let _ = webview_window.set_focus();
193-
}
194-
}
195-
})
196-
.build(app)?;
197-
}
203+
init_menu(app);
198204
Ok(())
199205
})
200206
.on_window_event(handle_system_tray_event)
201-
// .system_tray(tray)
202-
// .on_system_tray_event(handle_system_tray_event)
203207
.manage(Arc::new(Mutex::new(AppState {
204208
http_server: HttpServer::new(),
205209
})))
@@ -208,3 +212,4 @@ fn main() {
208212
app1.run(tauri::generate_context!())
209213
.expect("error while running tauri application");
210214
}
215+

src-tauri/tauri.conf.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"productName": "wcf-client-rust",
3-
"version": "2024.4.28",
3+
"version": "0.0.2",
44
"identifier": "com.iamteer.wcf",
55
"build": {
66
"beforeDevCommand": "pnpm dev",
@@ -23,10 +23,6 @@
2323
],
2424
"security": {
2525
"csp": null
26-
},
27-
"trayIcon": {
28-
"iconPath": "icons/icon.png",
29-
"iconAsTemplate": true
3026
}
3127
},
3228
"bundle": {
@@ -40,7 +36,8 @@
4036
"icons/icon.ico"
4137
],
4238
"resources":[
43-
"./src/wcferry/lib"
39+
"./src/wcferry/lib",
40+
"./icons"
4441
]
4542
}
4643
}

0 commit comments

Comments
 (0)