From 5614750ead79618cbf8bd81dda83df5855d86471 Mon Sep 17 00:00:00 2001 From: nongyehong <2439646234@qq.com> Date: Sat, 23 Mar 2024 19:52:20 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20perf(system):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=89=98=E7=9B=98=E5=9B=BE=E6=A0=87=E9=97=AA?= =?UTF-8?q?=E7=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/Cargo.lock | 18 ++- src-tauri/Cargo.toml | 1 + src-tauri/src/common/plugins.rs | 55 +++++++ src-tauri/src/common/tray.rs | 3 + src-tauri/src/main.rs | 13 +- src-tauri/tray/msg-sub.png | Bin 0 -> 731 bytes src-tauri/tray/msg.png | Bin 0 -> 601 bytes src/components/rightBox/Details.vue | 2 +- src/components/rightBox/chatBox/ChatMain.vue | 65 +++----- .../rightBox/chatBox/ChatSidebar.vue | 55 +++---- src/components/rightBox/chatBox/config.ts | 36 +++++ src/components/windows/ActionBar.vue | 4 +- src/enums/index.ts | 8 + src/hooks/useMessage.ts | 139 ++++++++++++++++++ src/layout/center/index.vue | 29 +++- src/styles/scss/chat-sidebar.scss | 21 +++ src/typings/components.d.ts | 2 - src/views/Tray.vue | 10 ++ src/views/home-window/message/Alone.vue | 1 + src/views/home-window/message/config.ts | 132 ----------------- src/views/home-window/message/index.vue | 3 +- 21 files changed, 373 insertions(+), 224 deletions(-) create mode 100644 src-tauri/tray/msg-sub.png create mode 100644 src-tauri/tray/msg.png create mode 100644 src/components/rightBox/chatBox/config.ts create mode 100644 src/hooks/useMessage.ts create mode 100644 src/styles/scss/chat-sidebar.scss delete mode 100644 src/views/home-window/message/config.ts diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index e2165395..314affed 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1262,6 +1262,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tokio", "window-shadows", "window-vibrancy", ] @@ -3088,14 +3089,27 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.35.0" +version = "1.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841d45b238a16291a4e1584e61820b8ae57d696cc5015c459c229ccc6990cc1c" +checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" dependencies = [ "backtrace", "bytes", "num_cpus", "pin-project-lite", + "tokio-macros", + "windows-sys 0.48.0", +] + +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.41", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 8244e9b9..d6a48282 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,6 +25,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" window-shadows = "0.2.2" # windows 阴影和圆角 window-vibrancy = "0.4.3" # windows 磨砂背景 tauri-v1.x版本最好使用0.4.3版本 +tokio = { version = "1.36.0", features = ["macros", "windows-sys", "rt-multi-thread", "time"] } [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/src-tauri/src/common/plugins.rs b/src-tauri/src/common/plugins.rs index 958f2619..62dff2c8 100644 --- a/src-tauri/src/common/plugins.rs +++ b/src-tauri/src/common/plugins.rs @@ -1,3 +1,4 @@ +use std::sync::Mutex; use tauri::{AppHandle, Manager, Runtime}; use window_shadows::set_shadow; @@ -31,4 +32,58 @@ pub fn set_tray_icon(app: AppHandle) { #[tauri::command] pub fn exit(app: AppHandle) { app.exit(0) +} + +pub struct TrayState { + // 用来缓存上一次线程的句柄 + pub id: Mutex>> +} + +/// 设置托盘图标闪烁 +// TODO 现在只能实现两张图片交换实现闪烁效果 (nyh -> 2024-03-23 17:57:45) +#[tauri::command] +pub fn tray_blink( + app: AppHandle, + state: tauri::State, + is_run: bool, // 是否运行 + ms: Option, // 可选, 时间间隔,单位毫秒 + icon_path_1: Option, // 可选,图标1 + icon_path_2: Option, // 可选,图标2 +) { + // 如果存在就取消上一次的线程 + match state.id.lock().as_deref_mut().map(|x| x.as_mut()) { + Ok(Some(v)) => v.abort(), + _ => (), + } + + // 是否停止运行,如果停止,则取消上一次线程后的返回 + if !is_run { + return; + } + + // 缓存线程 id 到 tauri state + *state.id.lock().unwrap() = Some(tokio::spawn(async move { + let path1 = &icon_path_1.unwrap(); + let path2 = &icon_path_2.unwrap(); + let mut count = 0; + + loop { + // 如果 ms 不存在 则默认 500ms + tokio::time::sleep(std::time::Duration::from_millis(ms.unwrap_or_else(|| 500))).await; + count += 1; + + // 两张图片交替显示, 所以对2取模运算 + let path = if count % 2 == 0 { + path2 + } else { + path1 + }; + + // tauri api 进行图标设置 + app + .tray_handle() + .set_icon(tauri::Icon::File(std::path::PathBuf::from(path))) + .unwrap(); + } + })); } \ No newline at end of file diff --git a/src-tauri/src/common/tray.rs b/src-tauri/src/common/tray.rs index f4c75693..e1746a26 100644 --- a/src-tauri/src/common/tray.rs +++ b/src-tauri/src/common/tray.rs @@ -29,6 +29,9 @@ fn red_icon(app: &AppHandle) { pub fn handler(app: &AppHandle, event: SystemTrayEvent) { match event { SystemTrayEvent::LeftClick { .. } => { + // 当我点击闪烁的图标的时候就停止闪烁 + let window = app.get_window("tray").unwrap(); + window.emit("stop", false).unwrap(); open_home(app); red_icon(app); }, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a34d8abd..82f058af 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,25 +1,32 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use std::sync::Mutex; use tauri::{SystemTray}; use crate::common::window::set_window_attribute; -use crate::common::plugins::{ exit, reset_set_window, set_tray_icon }; +use crate::common::plugins::{ exit, reset_set_window, set_tray_icon, tray_blink, TrayState }; mod common; -fn main() { +#[tokio::main] +async fn main() { let context = tauri::generate_context!(); let system_tray = SystemTray::new(); + // 初始化状态 + let state = TrayState { + id: Mutex::new(None) + }; tauri::Builder::default() .setup(|app| { set_window_attribute(app); Ok(()) }) + .manage(state) // 将状态注入到应用 .menu(tauri::Menu::new())// 使用空菜单来替换默认的操作系统菜单 .system_tray(system_tray)// 将 `tauri.conf.json` 上配置的图标添加到系统托盘 .on_system_tray_event(common::tray::handler) // 注册系统托盘事件处理程序 - .invoke_handler(tauri::generate_handler![reset_set_window, set_tray_icon, exit]) // 使用定义的插件 + .invoke_handler(tauri::generate_handler![reset_set_window, set_tray_icon, exit, tray_blink]) // 使用定义的插件 .on_window_event(|event| match event.event() { tauri::WindowEvent::CloseRequested { api, .. } => { event.window().hide().unwrap(); diff --git a/src-tauri/tray/msg-sub.png b/src-tauri/tray/msg-sub.png new file mode 100644 index 0000000000000000000000000000000000000000..f04a4fc697a0691832e72657aa045f8b8a770f11 GIT binary patch literal 731 zcmV<10wn#3P)Px%l}SWFR7gwhR=;Z%K@gtVyH_Z@-96qv@S31jk+cemtw$RpxNO1a0j zQ4~E##M`xV`&pK)4F-d+{J#RoU_yvhJ}ZP6u|bw)hl_xNAUG(%o*?3VM0^VX&j8>D z5nVs>br%5kl~OAeKo`syvu7POVM#>2G)+??`YxqBi-^Xai{p5UL5b)lr?+Gn$MMEd zWZD?B+Xk#)w}ObDn3+=Qo*QIQ(+`OFR7$z&0zP!>(S(_`8IQ;3dc7XkXh$h^s|I+D zh+YH01EtjC9MGn-NM&BTwoe?#-vHnYB3_hIe&q8+B+@i}Q*>?qKL z1B;^Q1|q&JzIV9s0N^SBED_NQt@WL>Uavd+8J7_-fbx*Xm~lB^OD)6d(t3sB3>T?!>2 zOA5X(yrNDD%D#NxU*`GDGgT?IT^VimgAF~;W2P)Ga-D*RRcByg330r&Rt33 zyhp`CC^gS4O_N8*hi2#+c2KGT+ibP;=gwK8%qq=X)m9r%tl-!QtAa8`j!NJl8tu_W zfEL^id9E=LX>PK%Dxnpi+gL>qm4;~2*{uNCp(~J&bA4I^Hes>I<~O|*zbx4SKT`kz N002ovPDHLkV1jHSQpf-R literal 0 HcmV?d00001 diff --git a/src-tauri/tray/msg.png b/src-tauri/tray/msg.png new file mode 100644 index 0000000000000000000000000000000000000000..f5761dadbae6dae84f10b4713f464c91858a592f GIT binary patch literal 601 zcmV-f0;c_mP)Px%6G=otR7gwhmc5SCKoG}g)*o?4TDrUeDNZL*!3&@iCqYSvYzOIbF916608Wb7 zZmtL&5lwgk@&u%%Kne{7Rl0LxX9uk>w!BFkelq+DuL>0F zRe(_t@TJyJe&yI(q3!t+Q1`nXPV06ZAQH|)+Z$=F)dK)W@_{%GEjKRb$$+t^%}E94 z&0|GZFBHag;_OG*b!XWC(vlvNVH!QMg+hE1SpGcz=Jc9sWBEk%^2!`ePMcvS4RMf& zcq$kSmDyhfZl8nyz&f3A(B|JL=`l)ESdyeiv(i@grYM9Z(zoeN`L;MhPxHXp{gWC$ zQ3kEC>=06`Oyost5{=y}ZP|}vKM|l+7aS`Mrc3cb00000NkvXXu0mjfbTAKC literal 0 HcmV?d00001 diff --git a/src/components/rightBox/Details.vue b/src/components/rightBox/Details.vue index 66397ea4..63630772 100644 --- a/src/components/rightBox/Details.vue +++ b/src/components/rightBox/Details.vue @@ -73,7 +73,7 @@ 群成员:({{ options.length }}人) - +