Skip to content

Commit

Permalink
feat: Display server connection status in the systray menu
Browse files Browse the repository at this point in the history
  • Loading branch information
doraemonkeys committed Jan 15, 2025
1 parent 7baf859 commit 1a92dba
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 18 deletions.
15 changes: 15 additions & 0 deletions windSend-rs/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub enum LanguageKey {
CopySuccessfully,
EffectiveAfterRestart,
DirCreatedSuccessfully,
RelayConnected,
RelayServerNotConnected,
}

impl LanguageKey {
Expand Down Expand Up @@ -146,6 +148,14 @@ lazy_static! {
(
LanguageKey::DirCreatedSuccessfully,
String::from("Directory created successfully")
),
(
LanguageKey::RelayConnected,
String::from("Server Connected")
),
(
LanguageKey::RelayServerNotConnected,
String::from("Server Disconnected")
)
]
.into_iter()
Expand Down Expand Up @@ -202,6 +212,11 @@ lazy_static! {
(
LanguageKey::DirCreatedSuccessfully,
String::from("文件夹创建成功")
),
(LanguageKey::RelayConnected, String::from("中转已连接")),
(
LanguageKey::RelayServerNotConnected,
String::from("中转未连接")
)
]
.into_iter()
Expand Down
22 changes: 11 additions & 11 deletions windSend-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ mod config;
mod file;
mod language;
mod route;
mod status;
mod utils;

use std::{collections::HashSet, sync::Mutex};

// #[cfg(not(all(target_os = "linux", target_env = "musl")))]
#[cfg(not(feature = "disable-systray-support"))]
mod icon_bytes;
#[cfg(not(feature = "disable-systray-support"))]
mod systray;
#[cfg(not(feature = "disable-systray-support"))]
mod web;
#[cfg(not(feature = "disable-systray-support"))]
pub static TX_RESET_FILES: OnceLock<crossbeam_channel::Sender<()>> = OnceLock::new();
#[cfg(not(feature = "disable-systray-support"))]
pub static TX_CLOSE_QUICK_PAIR: OnceLock<crossbeam_channel::Sender<()>> = OnceLock::new();
// pub static TX_CLOSE_ALLOW_TO_BE_SEARCHED: OnceLock<crossbeam_channel::Sender<()>> = OnceLock::new();

use std::{collections::HashSet, sync::Mutex};
pub static SELECTED_FILES: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();

#[allow(dead_code)]
static PROGRAM_NAME: &str = "WindSend-S-Rust";
Expand All @@ -41,7 +36,9 @@ fn init() {
.build()
.unwrap();
RUNTIME.set(r).unwrap();
SELECTED_FILES.set(Mutex::new(HashSet::new())).unwrap();
status::SELECTED_FILES
.set(Mutex::new(HashSet::new()))
.unwrap();
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
default_panic(panic_info);
Expand Down Expand Up @@ -80,11 +77,14 @@ fn main() {

let (tx1, rx1) = crossbeam_channel::bounded(1);
let (tx2, rx2) = crossbeam_channel::bounded(1);
TX_RESET_FILES.set(tx1).unwrap();
TX_CLOSE_QUICK_PAIR.set(tx2).unwrap();
let (tx3, rx3) = crossbeam_channel::bounded(1);
status::TX_RESET_FILES.set(tx1).unwrap();
status::TX_CLOSE_QUICK_PAIR.set(tx2).unwrap();
status::TX_UPDATE_RELAY_SERVER_CONNECTED.set(tx3).unwrap();
let rm = systray::MenuReceiver {
rx_reset_files_item: rx1,
rx_close_quick_pair: rx2,
rx_update_relay_server_connected: rx3,
};

let return_code = systray::show_systray(rm);
Expand Down
7 changes: 4 additions & 3 deletions windSend-rs/src/route/copy.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::language::{LanguageKey, LANGUAGE_MANAGER};
use crate::route::resp::{resp_common_error_msg, send_head, send_msg_with_body};
use crate::route::{RouteDataType, RouteRecvHead, RouteRespHead, RouteTransferInfo};
use crate::status;
use std::path::PathBuf;
use tokio::net::TcpStream;
use tokio_rustls::server::TlsStream;
use tracing::{debug, error, info, warn};

pub async fn copy_handler(conn: &mut TlsStream<TcpStream>) {
// 用户选择的文件
let selected_files = crate::SELECTED_FILES.get();
let selected_files = status::SELECTED_FILES.get();
let files = match selected_files {
Some(selected) => {
let selected = selected.lock().unwrap();
Expand All @@ -23,9 +24,9 @@ pub async fn copy_handler(conn: &mut TlsStream<TcpStream>) {
let r = send_files(conn, files).await;
if r.is_ok() {
#[cfg(not(feature = "disable-systray-support"))]
crate::TX_RESET_FILES.get().unwrap().try_send(()).unwrap();
status::TX_RESET_FILES.get().unwrap().try_send(()).unwrap();
}
*crate::SELECTED_FILES.get().unwrap().lock().unwrap() = std::collections::HashSet::new();
*status::SELECTED_FILES.get().unwrap().lock().unwrap() = std::collections::HashSet::new();
return;
}

Expand Down
8 changes: 6 additions & 2 deletions windSend-rs/src/route/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ pub async fn common_auth(conn: &mut TlsStream<TcpStream>) -> Result<RouteRecvHea
time_str,
remote_access_host
);
return Ok(head);
Ok(head)
// let t = chrono::NaiveDateTime::parse_from_str(time_str, TIME_FORMAT)
// .map_err(|e| error!("parse time failed, err: {}", e))?;
// let now = chrono::Utc::now();
Expand Down Expand Up @@ -407,7 +407,11 @@ async fn match_handler(conn: &mut TlsStream<TcpStream>) -> Result<(), ()> {
match r {
Ok(_) => {
#[cfg(not(feature = "disable-systray-support"))]
if let Err(e) = crate::TX_CLOSE_QUICK_PAIR.get().unwrap().try_send(()) {
if let Err(e) = crate::status::TX_CLOSE_QUICK_PAIR
.get()
.unwrap()
.try_send(())
{
error!("send close allow to be search failed, err: {}", e);
}
*crate::config::ALLOW_TO_BE_SEARCHED.lock().unwrap() = false;
Expand Down
15 changes: 15 additions & 0 deletions windSend-rs/src/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::sync::OnceLock;
use std::{collections::HashSet, sync::Mutex};

#[cfg(not(feature = "disable-systray-support"))]
pub static TX_RESET_FILES: OnceLock<crossbeam_channel::Sender<()>> = OnceLock::new();
#[cfg(not(feature = "disable-systray-support"))]
pub static TX_CLOSE_QUICK_PAIR: OnceLock<crossbeam_channel::Sender<()>> = OnceLock::new();
// pub static TX_CLOSE_ALLOW_TO_BE_SEARCHED: OnceLock<crossbeam_channel::Sender<()>> = OnceLock::new();

pub static SELECTED_FILES: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();

pub static RELAY_SERVER_CONNECTED: Mutex<bool> = Mutex::new(false);
#[cfg(not(feature = "disable-systray-support"))]
pub static TX_UPDATE_RELAY_SERVER_CONNECTED: OnceLock<crossbeam_channel::Sender<()>> =
OnceLock::new();
60 changes: 58 additions & 2 deletions windSend-rs/src/systray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use tray_icon::TrayIconBuilder;

use crate::config;
use crate::language::{Language, LanguageKey, LANGUAGE_MANAGER};
use crate::status::SELECTED_FILES;
use crate::utils;
use crate::web;
use crate::PROGRAM_NAME;
use crate::SELECTED_FILES;

// use global_hotkey::hotkey::Modifiers as hotkey_Modifiers;
// use global_hotkey::{
Expand All @@ -33,6 +33,7 @@ pub struct MenuReceiver {
pub rx_reset_files_item: crossbeam_channel::Receiver<()>,
// pub rx_close_allow_to_be_searched: crossbeam_channel::Receiver<()>,
pub rx_close_quick_pair: crossbeam_channel::Receiver<()>,
pub rx_update_relay_server_connected: crossbeam_channel::Receiver<()>,
}

#[cfg(not(all(target_os = "linux", target_env = "musl")))]
Expand Down Expand Up @@ -133,6 +134,24 @@ fn loop_systray(mr: MenuReceiver) -> ReturnCode {
*config::ALLOW_TO_BE_SEARCHED.lock().unwrap(),
None,
);
let relay_server_connected = *crate::status::RELAY_SERVER_CONNECTED.lock().unwrap();
let relay_server_connected_str = if relay_server_connected {
LANGUAGE_MANAGER
.read()
.unwrap()
.translate(LanguageKey::RelayConnected)
} else {
LANGUAGE_MANAGER
.read()
.unwrap()
.translate(LanguageKey::RelayServerNotConnected)
};
let relay_server_connected_i = CheckMenuItem::new(
relay_server_connected_str,
false,
relay_server_connected,
None,
);
let copy_from_web_i = MenuItem::new(
LANGUAGE_MANAGER
.read()
Expand Down Expand Up @@ -214,11 +233,13 @@ fn loop_systray(mr: MenuReceiver) -> ReturnCode {
true,
None,
);

let items: &[&dyn IsMenuItem] = &[
&add_files_i,
&clear_files_i,
// &copy_from_web_i,
// &paste_to_web_i,
&relay_server_connected_i,
&PredefinedMenuItem::separator(),
&save_path_i,
&sub_menu_hide,
Expand Down Expand Up @@ -261,6 +282,21 @@ fn loop_systray(mr: MenuReceiver) -> ReturnCode {
.translate(LanguageKey::ClearFiles)
.clone()
}),
(&relay_server_connected_i, &|| {
if *crate::status::RELAY_SERVER_CONNECTED.lock().unwrap() {
LANGUAGE_MANAGER
.read()
.unwrap()
.translate(LanguageKey::RelayConnected)
.clone()
} else {
LANGUAGE_MANAGER
.read()
.unwrap()
.translate(LanguageKey::RelayServerNotConnected)
.to_owned()
}
}),
(&sub_menu_hide, &|| {
LANGUAGE_MANAGER
.read()
Expand Down Expand Up @@ -367,7 +403,11 @@ fn loop_systray(mr: MenuReceiver) -> ReturnCode {
allow_to_be_search_i.set_checked(false);
should_poll = false;
}
// 不能一直阻塞在这里,否则右键点击托盘图标会没有反应
if mr.rx_update_relay_server_connected.try_recv().is_ok() {
handle_menu_event_update_relay_server_connected(&relay_server_connected_i);
}
// cannot remain blocked here indefinitely,
// otherwise right-clicking on the tray icon will not respond.
if let Ok(event) = menu_channel.try_recv() {
match event.id {
id if id == sub_hide_once_i.id() => {
Expand Down Expand Up @@ -508,6 +548,22 @@ async fn handle_menu_event_add_files(add_item: &MenuItem, clear_item: &MenuItem)
));
}

fn handle_menu_event_update_relay_server_connected(relay_server_connected_i: &CheckMenuItem) {
let relay_server_connected = *crate::status::RELAY_SERVER_CONNECTED.lock().unwrap();
relay_server_connected_i.set_checked(relay_server_connected);
relay_server_connected_i.set_text(if relay_server_connected {
LANGUAGE_MANAGER
.read()
.unwrap()
.translate(LanguageKey::RelayConnected)
} else {
LANGUAGE_MANAGER
.read()
.unwrap()
.translate(LanguageKey::RelayServerNotConnected)
});
}

async fn handle_menu_event_save_path() {
let pick_task = rfd::AsyncFileDialog::new().pick_folder();
let path = match pick_task.await {
Expand Down

0 comments on commit 1a92dba

Please sign in to comment.