Skip to content

Commit

Permalink
feat: impl singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
lich0821 committed Feb 13, 2024
1 parent 0baed56 commit 5afc992
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde_json = "1"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
winapi = { version = "0.3", features = ["winuser", "synchapi"] }
tauri = { version = "1.5", features = [ "dialog-all", "system-tray", "shell-open"] }

[features]
Expand Down
62 changes: 28 additions & 34 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

use chrono::Local;
use log::{info, Level, LevelFilter, Log, Metadata, Record};
use std::ptr;
use std::sync::{Arc, Mutex};
use tauri::{
command, AppHandle, CustomMenuItem, Manager, SystemTray, SystemTrayMenu, SystemTrayMenuItem,
WindowEvent,
use tauri::{command, AppHandle, CustomMenuItem, Manager, SystemTray, SystemTrayMenu, WindowEvent};
use winapi::{
shared::winerror::ERROR_ALREADY_EXISTS,
um::{
errhandlingapi::GetLastError,
synchapi::CreateMutexA,
winuser::{FindWindowA, SetForegroundWindow, ShowWindow, SW_RESTORE},
},
};

mod endpoints;
Expand Down Expand Up @@ -88,31 +94,12 @@ fn handle_system_tray_event(app_handle: &tauri::AppHandle, event: tauri::SystemT
"quit" => {
app_handle.emit_all("request-exit", ()).unwrap();
}
"hide" => {
if let Some(window) = app_handle.get_window("main") {
window.hide().unwrap();
let tray_handle = app_handle.tray_handle();
tray_handle.get_item("hide").set_enabled(false).unwrap();
tray_handle.get_item("show").set_enabled(true).unwrap();
}
}
"show" => {
if let Some(window) = app_handle.get_window("main") {
window.show().unwrap();
let tray_handle = app_handle.tray_handle();
tray_handle.get_item("show").set_enabled(false).unwrap();
tray_handle.get_item("hide").set_enabled(true).unwrap();
}
}
_ => {}
},
tauri::SystemTrayEvent::LeftClick { .. } => {
if let Some(window) = app_handle.get_window("main") {
window.show().unwrap();
window.set_focus().unwrap();
let tray_handle = app_handle.tray_handle();
tray_handle.get_item("show").set_enabled(false).unwrap();
tray_handle.get_item("hide").set_enabled(true).unwrap();
}
}
_ => {}
Expand Down Expand Up @@ -153,16 +140,26 @@ fn init_log(handle: AppHandle) {
}

fn main() {
let show = CustomMenuItem::new("show".to_string(), "显示").disabled();
let hide = CustomMenuItem::new("hide".to_string(), "隐藏");
let quit = CustomMenuItem::new("quit".to_string(), "退出");

let tray_menu = SystemTrayMenu::new()
.add_item(show)
.add_item(hide)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(quit);
let mutex_name = b"Global\\wcfrust_app_mutex\0";
unsafe {
let handle = CreateMutexA(ptr::null_mut(), 0, mutex_name.as_ptr() as *const i8);
if handle.is_null() {
eprintln!("Failed to create mutex.");
return;
}
if GetLastError() == ERROR_ALREADY_EXISTS {
let window_name = "WcfRust\0".as_ptr() as *const i8;
let hwnd = FindWindowA(ptr::null(), window_name);
if !hwnd.is_null() {
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
}
return;
}
}

let quit = CustomMenuItem::new("quit".to_string(), "退出");
let tray_menu = SystemTrayMenu::new().add_item(quit);
let tray = SystemTray::new().with_menu(tray_menu);

let app = tauri::Builder::default()
Expand All @@ -176,9 +173,6 @@ fn main() {
api.prevent_close();
if let Some(window) = event.window().get_window("main") {
window.hide().unwrap();
let tray_handle = event.window().app_handle().tray_handle();
tray_handle.get_item("show").set_enabled(true).unwrap();
tray_handle.get_item("hide").set_enabled(false).unwrap();
}
}
_ => {}
Expand Down

0 comments on commit 5afc992

Please sign in to comment.