diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 30f7e07..e3a77c3 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,58 +4,12 @@ mod plugins; mod tray; +use plugins::system_info; use plugins::window::{self, show_window, MAIN_WINDOW_LABEL}; -use sysinfo::{Networks, RefreshKind, System}; use tauri::Manager; -use tauri::{async_runtime, command, generate_context, generate_handler, Builder, WindowEvent}; +use tauri::{async_runtime, generate_context, Builder, WindowEvent}; use tauri_plugin_autostart::MacosLauncher; -#[derive(Debug, serde::Serialize)] -#[serde(rename_all = "camelCase")] -struct SystemInfo { - memory_usage: f32, - cpu_usage: f32, - network_speed_up: u64, - network_speed_down: u64, -} - -#[command] -fn get_sys_info() -> SystemInfo { - let mut sys = System::new_with_specifics(RefreshKind::everything().without_processes()); - sys.refresh_all(); - - // Raw - let total_memory = sys.total_memory(); - let used_memory = sys.used_memory(); - let memory_usage = used_memory as f32 / total_memory as f32 * 100.0; - - // cpu 占用 - std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL); - sys.refresh_cpu(); - let cpu_usage = - sys.cpus().iter().map(|cpu| cpu.cpu_usage()).sum::() / sys.cpus().len() as f32; - - let mut networks = Networks::new_with_refreshed_list(); - networks.refresh_list(); - let network_speed_up = networks - .iter() - .map(|(_, data)| data.transmitted()) - .sum::() - / networks.len() as u64; - let network_speed_down = networks - .iter() - .map(|(_, data)| data.received()) - .sum::() - / networks.len() as u64; - - SystemInfo { - memory_usage, - cpu_usage, - network_speed_up, - network_speed_down, - } -} - fn main() { Builder::default() .setup(|app| { @@ -81,10 +35,10 @@ fn main() { )) // 自定义的窗口管理插件 .plugin(window::init()) + .plugin(system_info::init()) // 系统托盘:https://tauri.app/v1/guides/features/system-tray .system_tray(tray::menu()) .on_system_tray_event(tray::handler) - .invoke_handler(generate_handler![get_sys_info]) // 让 app 保持在后台运行:https://tauri.app/v1/guides/features/system-tray/#preventing-the-app-from-closing .on_window_event(|event| match event.event() { WindowEvent::CloseRequested { api, .. } => { diff --git a/src-tauri/src/plugins/mod.rs b/src-tauri/src/plugins/mod.rs index 61b63f1..159f60c 100644 --- a/src-tauri/src/plugins/mod.rs +++ b/src-tauri/src/plugins/mod.rs @@ -1 +1,3 @@ pub mod window; + +pub mod system_info; diff --git a/src-tauri/src/plugins/system_info.rs b/src-tauri/src/plugins/system_info.rs new file mode 100644 index 0000000..d985eab --- /dev/null +++ b/src-tauri/src/plugins/system_info.rs @@ -0,0 +1,88 @@ +use std::sync::Mutex; +use sysinfo::{Networks, RefreshKind, System}; + +use tauri::{ + command, generate_handler, + plugin::{Builder, TauriPlugin}, + Manager, Wry, +}; + +#[derive(Debug, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct SystemInfo { + memory_usage: f32, + cpu_usage: f32, + network_speed_up: u64, + network_speed_down: u64, +} + +pub struct SysInfoState { + pub sysinfo: Mutex, +} +/// A Mute Wrapper for sysinfo crate's System struct +pub struct SysInfo { + pub sys: System, +} + +impl Default for SysInfoState { + fn default() -> SysInfoState { + SysInfoState { + sysinfo: Mutex::new(SysInfo { + sys: System::new_with_specifics(RefreshKind::everything().without_processes()), + }), + } + } +} + +#[command] +pub fn get_sys_info(state: tauri::State<'_, SysInfoState>) -> SystemInfo { + // 锁定 sysinfo 并将其绑定到一个变量上 + let mut sysinfo_guard = state.sysinfo.lock().unwrap(); + + sysinfo_guard.sys.refresh_memory(); + let total_memory = sysinfo_guard.sys.total_memory(); + let used_memory = sysinfo_guard.sys.used_memory(); + let memory_usage = used_memory as f32 / total_memory as f32 * 100.0; + + // // cpu 占用 + sysinfo_guard.sys.refresh_cpu(); + + let cpu_usage = sysinfo_guard + .sys + .cpus() + .iter() + .map(|cpu| cpu.cpu_usage()) + .sum::() + / sysinfo_guard.sys.cpus().len() as f32; + + let mut networks = Networks::new_with_refreshed_list(); + networks.refresh_list(); + let network_speed_up = networks + .iter() + .map(|(_, data)| data.transmitted()) + .sum::() + / networks.len() as u64; + let network_speed_down = networks + .iter() + .map(|(_, data)| data.received()) + .sum::() + / networks.len() as u64; + + SystemInfo { + memory_usage, + cpu_usage, + network_speed_up, + network_speed_down, + } +} + +pub fn init() -> TauriPlugin { + Builder::new("system_info") + .setup(move |app| { + app.manage(SysInfoState::default()); + + Ok(()) + }) + .invoke_handler(generate_handler![get_sys_info,]) + .build() +} diff --git a/src-tauri/src/plugins/window.rs b/src-tauri/src/plugins/window.rs index f9e83e2..ece5226 100644 --- a/src-tauri/src/plugins/window.rs +++ b/src-tauri/src/plugins/window.rs @@ -30,14 +30,16 @@ pub async fn create_window(app_handle: AppHandle, label: String, mut options: Wi #[command] pub async fn show_window(window: Window) { window.show().unwrap(); - window.unminimize().unwrap(); window.set_focus().unwrap(); + + window.emit("window-shown", true).unwrap(); } // 隐藏窗口 #[command] pub async fn hide_window(window: Window) { window.hide().unwrap(); + window.emit("window-hidden", true).unwrap(); } pub fn init() -> TauriPlugin { diff --git a/src/main.ts b/src/main.ts index 3e1cbb8..65d5a17 100644 --- a/src/main.ts +++ b/src/main.ts @@ -36,9 +36,12 @@ function setElement(id: string, num: number) { } } +let timer: number async function getSystemInfo() { + clearTimeout(timer) + const { cpuUsage, memoryUsage, networkSpeedDown, networkSpeedUp } = - (await invoke("get_sys_info")) as SystemInfo + (await invoke("plugin:system_info|get_sys_info")) as SystemInfo setElement("#cpu-usage", cpuUsage) setElement("#memory-usage", memoryUsage) @@ -47,52 +50,43 @@ async function getSystemInfo() { document.querySelector("#network-usage")!.textContent = `↓ ${formatBytes( networkSpeedDown, )} / ↑ ${formatBytes(networkSpeedUp)}` -} -let timer: number -function loopGetInfo() { - getSystemInfo() - timer = setInterval(getSystemInfo, 1000) + setTimeout(() => { + getSystemInfo() + }, 1200) } function hiddenWindow() { - console.log(222) - invoke("plugin:window|hide_window") } -window.onload = async () => { - console.log("load") +async function dragWindow() { + await appWindow.startDragging() +} +window.onload = async () => { listen("tray-click", () => { invoke("plugin:window|show_window") - clearInterval(timer) - loopGetInfo() + getSystemInfo() }) listen("screen-center", async () => { invoke("plugin:window|show_window") await appWindow.center() - clearInterval(timer) - loopGetInfo() + getSystemInfo() }) - listen("hidden", () => { - clearInterval(timer) + listen("window-hidden", () => { + clearTimeout(timer) }) await appWindow.setPosition(new LogicalPosition(1730, 810)) - window.addEventListener("mousedown", async () => { - await appWindow.startDragging() - }) - - clearInterval(timer) - loopGetInfo() + getSystemInfo() // 禁用键盘事件 document.addEventListener("keydown", (e) => e.preventDefault()) - // 禁用滚轮和鼠标右键 + // // 禁用滚轮和鼠标右键 document.addEventListener("wheel", (e) => e.preventDefault()) document.addEventListener("contextmenu", (e) => e.preventDefault()) // 禁用选择文本 @@ -100,4 +94,9 @@ window.onload = async () => { // 监听鼠标左键双击 document.addEventListener("dblclick", hiddenWindow) + + // biome-ignore lint/style/noNonNullAssertion: + document.querySelector(".container")!.addEventListener("mousedown", () => { + dragWindow() + }) }