diff --git a/src/ipc.rs b/src/ipc.rs index 1cbd994bb..db41b8649 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -234,6 +234,8 @@ pub enum Data { #[cfg(windows)] SyncWinCpuUsage(Option), FileTransferLog(String), + #[cfg(any(windows, target_os = "macos"))] + ControlledSessionCount(usize), } #[tokio::main(flavor = "current_thread")] @@ -482,6 +484,16 @@ async fn handle(data: Data, stream: &mut Connection) { #[cfg(all(feature = "flutter", feature = "plugin_framework"))] #[cfg(not(any(target_os = "android", target_os = "ios")))] Data::Plugin(plugin) => crate::plugin::ipc::handle_plugin(plugin, stream).await, + #[cfg(any(windows, target_os = "macos"))] + Data::ControlledSessionCount(_) => { + allow_err!( + stream + .send(&Data::ControlledSessionCount( + crate::Connection::alive_conns().len() + )) + .await + ); + } _ => {} } } diff --git a/src/lang/ar.rs b/src/lang/ar.rs index 4a1f8486c..33782738f 100644 --- a/src/lang/ar.rs +++ b/src/lang/ar.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/ca.rs b/src/lang/ca.rs index 55e01fefa..65c4ea9ce 100644 --- a/src/lang/ca.rs +++ b/src/lang/ca.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/cn.rs b/src/lang/cn.rs index 5fdfa460e..006edd270 100644 --- a/src/lang/cn.rs +++ b/src/lang/cn.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", "安装成功!"), ("Installation failed!", "安装失败!"), ("Reverse mouse wheel", "鼠标滚轮反向"), + ("{} sessions", "{}个会话"), ].iter().cloned().collect(); } diff --git a/src/lang/cs.rs b/src/lang/cs.rs index 37473e0b6..45e224d81 100644 --- a/src/lang/cs.rs +++ b/src/lang/cs.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/da.rs b/src/lang/da.rs index 9a9ecdfe1..889f82634 100644 --- a/src/lang/da.rs +++ b/src/lang/da.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/de.rs b/src/lang/de.rs index c307281ee..9185f3288 100644 --- a/src/lang/de.rs +++ b/src/lang/de.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", "Installation erfolgreich!"), ("Installation failed!", "Installation fehlgeschlagen!"), ("Reverse mouse wheel", "Mausrad rückwärts drehen"), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/el.rs b/src/lang/el.rs index d2ae3eb5c..3440486f7 100644 --- a/src/lang/el.rs +++ b/src/lang/el.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/eo.rs b/src/lang/eo.rs index 90144c694..5a8c4da1f 100644 --- a/src/lang/eo.rs +++ b/src/lang/eo.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/es.rs b/src/lang/es.rs index 4589844c2..484d6fe5b 100644 --- a/src/lang/es.rs +++ b/src/lang/es.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", "Instalación exitosa"), ("Installation failed!", "La instalación ha fallado"), ("Reverse mouse wheel", "Invertir rueda del ratón"), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/fa.rs b/src/lang/fa.rs index ab1d7ab12..86dbbe013 100644 --- a/src/lang/fa.rs +++ b/src/lang/fa.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/fr.rs b/src/lang/fr.rs index 3ce8bca02..9dc310940 100644 --- a/src/lang/fr.rs +++ b/src/lang/fr.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/hu.rs b/src/lang/hu.rs index 50ca96792..4a24572cd 100644 --- a/src/lang/hu.rs +++ b/src/lang/hu.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/id.rs b/src/lang/id.rs index e85811772..d7c8aed82 100644 --- a/src/lang/id.rs +++ b/src/lang/id.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", "Instalasi berhasil!"), ("Installation failed!", "Instalasi gagal!"), ("Reverse mouse wheel", "Balikkan arah scroll mouse!"), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/it.rs b/src/lang/it.rs index 9b708ca39..dc1d09020 100644 --- a/src/lang/it.rs +++ b/src/lang/it.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", "Installazione completata"), ("Installation failed!", "Installazione fallita"), ("Reverse mouse wheel", "Rotella mouse inversa"), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/ja.rs b/src/lang/ja.rs index 015ee7af3..1e2829bd4 100644 --- a/src/lang/ja.rs +++ b/src/lang/ja.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/ko.rs b/src/lang/ko.rs index 2897ed5f8..a3c251e86 100644 --- a/src/lang/ko.rs +++ b/src/lang/ko.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/kz.rs b/src/lang/kz.rs index 5a0c14871..0b956d66c 100644 --- a/src/lang/kz.rs +++ b/src/lang/kz.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/lt.rs b/src/lang/lt.rs index 100e22955..c64554239 100644 --- a/src/lang/lt.rs +++ b/src/lang/lt.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/nl.rs b/src/lang/nl.rs index 7503831bf..d4038e2ed 100644 --- a/src/lang/nl.rs +++ b/src/lang/nl.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/pl.rs b/src/lang/pl.rs index cea9b69bd..5a727cfa1 100644 --- a/src/lang/pl.rs +++ b/src/lang/pl.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", "Instalacja zakończona!"), ("Installation failed!", "Instalacja nie powiodła się"), ("Reverse mouse wheel", "Odwróć rolkę myszki"), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/pt_PT.rs b/src/lang/pt_PT.rs index 91a85a3b1..7b31cc813 100644 --- a/src/lang/pt_PT.rs +++ b/src/lang/pt_PT.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/ptbr.rs b/src/lang/ptbr.rs index 7ca81495d..c5134e9dd 100644 --- a/src/lang/ptbr.rs +++ b/src/lang/ptbr.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/ro.rs b/src/lang/ro.rs index bcc13a0df..23fc8bf82 100644 --- a/src/lang/ro.rs +++ b/src/lang/ro.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/ru.rs b/src/lang/ru.rs index eb9f8161b..d231d3ef0 100644 --- a/src/lang/ru.rs +++ b/src/lang/ru.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", "Установка выполнена успешно!"), ("Installation failed!", "Установка не выполнена!"), ("Reverse mouse wheel", "Реверсировать колесо мыши"), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/sk.rs b/src/lang/sk.rs index 003b2cabc..16b33ee44 100644 --- a/src/lang/sk.rs +++ b/src/lang/sk.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/sl.rs b/src/lang/sl.rs index 80b9aed1d..7cb0c677d 100755 --- a/src/lang/sl.rs +++ b/src/lang/sl.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/sq.rs b/src/lang/sq.rs index fd2db0c83..028096c6d 100644 --- a/src/lang/sq.rs +++ b/src/lang/sq.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/sr.rs b/src/lang/sr.rs index 9092d453d..55ae4022c 100644 --- a/src/lang/sr.rs +++ b/src/lang/sr.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/sv.rs b/src/lang/sv.rs index ed8c3697a..ca4adea6b 100644 --- a/src/lang/sv.rs +++ b/src/lang/sv.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/template.rs b/src/lang/template.rs index 6b4d176d2..48a710e5d 100644 --- a/src/lang/template.rs +++ b/src/lang/template.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/th.rs b/src/lang/th.rs index a5e193a00..abe318b18 100644 --- a/src/lang/th.rs +++ b/src/lang/th.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/tr.rs b/src/lang/tr.rs index 0fe1369df..b38c3e497 100644 --- a/src/lang/tr.rs +++ b/src/lang/tr.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/tw.rs b/src/lang/tw.rs index 5e533e9bc..22cd296c9 100644 --- a/src/lang/tw.rs +++ b/src/lang/tw.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/ua.rs b/src/lang/ua.rs index 7cc885f56..dba9b625b 100644 --- a/src/lang/ua.rs +++ b/src/lang/ua.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/lang/vn.rs b/src/lang/vn.rs index e65fab66b..6a6b8366b 100644 --- a/src/lang/vn.rs +++ b/src/lang/vn.rs @@ -544,5 +544,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("Installation Successful!", ""), ("Installation failed!", ""), ("Reverse mouse wheel", ""), + ("{} sessions", ""), ].iter().cloned().collect(); } diff --git a/src/tray.rs b/src/tray.rs index 2f4aae93a..8dac19bab 100644 --- a/src/tray.rs +++ b/src/tray.rs @@ -1,5 +1,11 @@ +use crate::{client::translate, ipc::Data}; +use hbb_common::{allow_err, log, tokio}; +use std::{ + sync::{Arc, Mutex}, + time::Duration, +}; + pub fn start_tray() { - use hbb_common::{allow_err, log}; allow_err!(make_tray()); } @@ -40,31 +46,44 @@ pub fn make_tray() -> hbb_common::ResultType<()> { let event_loop = EventLoopBuilder::new().build(); let tray_menu = Menu::new(); - let quit_i = MenuItem::new(crate::client::translate("Exit".to_owned()), true, None); - let open_i = MenuItem::new(crate::client::translate("Open".to_owned()), true, None); + let quit_i = MenuItem::new(translate("Exit".to_owned()), true, None); + let open_i = MenuItem::new(translate("Open".to_owned()), true, None); tray_menu.append_items(&[&open_i, &quit_i]); - - let _tray_icon = Some( - TrayIconBuilder::new() - .with_menu(Box::new(tray_menu)) - .with_tooltip(format!( + let tooltip = |count: usize| { + if count == 0 { + format!( "{} {}", crate::get_app_name(), - crate::lang::translate("Service is running".to_owned()) - )) + translate("Service is running".to_owned()), + ) + } else { + format!( + "{} - {}\n{}", + crate::get_app_name(), + translate("Ready".to_owned()), + translate("{".to_string() + &format!("{count}") + "} sessions"), + ) + } + }; + let tray_icon = Some( + TrayIconBuilder::new() + .with_menu(Box::new(tray_menu)) + .with_tooltip(tooltip(0)) .with_icon(icon) .build()?, ); + let tray_icon = Arc::new(Mutex::new(tray_icon)); let menu_channel = MenuEvent::receiver(); let tray_channel = TrayEvent::receiver(); + #[cfg(not(target_os = "linux"))] + let (ipc_sender, ipc_receiver) = std::sync::mpsc::channel::(); let mut docker_hiden = false; let open_func = move || { - if cfg!(not(feature = "flutter")) - { - crate::run_me::<&str>(vec![]).ok(); - return; + if cfg!(not(feature = "flutter")) { + crate::run_me::<&str>(vec![]).ok(); + return; } #[cfg(target_os = "macos")] crate::platform::macos::handle_application_should_open_untitled_file(); @@ -89,6 +108,11 @@ pub fn make_tray() -> hbb_common::ResultType<()> { } }; + // ubuntu 22.04 can't see tooltip + #[cfg(not(target_os = "linux"))] + std::thread::spawn(move || { + start_query_session_count(ipc_sender.clone()); + }); event_loop.run(move |_event, _, control_flow| { if !docker_hiden { #[cfg(target_os = "macos")] @@ -121,5 +145,55 @@ pub fn make_tray() -> hbb_common::ResultType<()> { open_func(); } } + + #[cfg(not(target_os = "linux"))] + if let Ok(data) = ipc_receiver.try_recv() { + match data { + Data::ControlledSessionCount(count) => { + tray_icon + .lock() + .unwrap() + .as_mut() + .map(|t| t.set_tooltip(Some(tooltip(count)))); + } + _ => {} + } + } }); } + +#[cfg(not(target_os = "linux"))] +#[tokio::main(flavor = "current_thread")] +async fn start_query_session_count(sender: std::sync::mpsc::Sender) { + let mut last_count = 0; + loop { + if let Ok(mut c) = crate::ipc::connect(1000, "").await { + let mut timer = tokio::time::interval(Duration::from_secs(1)); + loop { + tokio::select! { + res = c.next() => { + match res { + Err(err) => { + log::error!("ipc connection closed: {}", err); + break; + } + + Ok(Some(Data::ControlledSessionCount(count))) => { + if count != last_count { + last_count = count; + sender.send(Data::ControlledSessionCount(count)).ok(); + } + } + _ => {} + } + } + + _ = timer.tick() => { + c.send(&Data::ControlledSessionCount(0)).await.ok(); + } + } + } + } + hbb_common::sleep(1.).await; + } +}