forked from rustdesk/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtray.rs
215 lines (202 loc) · 6.92 KB
/
tray.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::client::translate;
#[cfg(windows)]
use crate::ipc::Data;
#[cfg(windows)]
use hbb_common::tokio;
use hbb_common::{allow_err, log};
use std::sync::{Arc, Mutex};
#[cfg(windows)]
use std::time::Duration;
pub fn start_tray() {
allow_err!(make_tray());
}
pub fn make_tray() -> hbb_common::ResultType<()> {
// https://github.com/tauri-apps/tray-icon/blob/dev/examples/tao.rs
use hbb_common::anyhow::Context;
use tao::event_loop::{ControlFlow, EventLoopBuilder};
use tray_icon::{
menu::{Menu, MenuEvent, MenuItem},
TrayEvent, TrayIconBuilder,
};
let icon;
#[cfg(target_os = "macos")]
{
const DARK: &[u8] = include_bytes!("../res/mac-tray-dark-x2.png");
const LIGHT: &[u8] = include_bytes!("../res/mac-tray-light-x2.png");
let output = std::process::Command::new("sw_vers")
.args(&["-productVersion"])
.output()
.map(|x| x.stdout)
.unwrap_or_default();
let version: f64 = String::from_utf8_lossy(output.as_slice())
.trim()
.parse()
.unwrap_or_default();
icon = if version >= 14. {
// workaround for Sonoma, always light menubar
DARK
} else {
let mode = dark_light::detect();
match mode {
dark_light::Mode::Dark => LIGHT,
_ => DARK,
}
};
}
#[cfg(not(target_os = "macos"))]
{
icon = include_bytes!("../res/tray-icon.ico");
}
let (icon_rgba, icon_width, icon_height) = {
let image = image::load_from_memory(icon)
.context("Failed to open icon path")?
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
let icon = tray_icon::icon::Icon::from_rgba(icon_rgba, icon_width, icon_height)
.context("Failed to open icon")?;
let event_loop = EventLoopBuilder::new().build();
let tray_menu = Menu::new();
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 tooltip = |count: usize| {
if count == 0 {
format!(
"{} {}",
crate::get_app_name(),
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(windows)]
let (ipc_sender, ipc_receiver) = std::sync::mpsc::channel::<Data>();
let mut docker_hiden = false;
let open_func = move || {
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();
#[cfg(target_os = "windows")]
{
use std::os::windows::process::CommandExt;
use std::process::Command;
Command::new("cmd")
.arg("/c")
.arg("start rustdesk://")
.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW)
.spawn()
.ok();
}
#[cfg(target_os = "linux")]
if !std::process::Command::new("xdg-open")
.arg("rustdesk://")
.spawn()
.is_ok()
{
crate::run_me::<&str>(vec![]).ok();
}
};
#[cfg(windows)]
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")]
crate::platform::macos::hide_dock();
docker_hiden = true;
}
*control_flow = ControlFlow::WaitUntil(
std::time::Instant::now() + std::time::Duration::from_millis(100),
);
if let Ok(event) = menu_channel.try_recv() {
if event.id == quit_i.id() {
/* failed in windows, seems no permission to check system process
if !crate::check_process("--server", false) {
*control_flow = ControlFlow::Exit;
return;
}
*/
if !crate::platform::uninstall_service(false) {
*control_flow = ControlFlow::Exit;
}
} else if event.id == open_i.id() {
open_func();
}
}
if let Ok(_event) = tray_channel.try_recv() {
#[cfg(target_os = "windows")]
if _event.event == tray_icon::ClickEvent::Left {
open_func();
}
}
#[cfg(windows)]
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(windows)]
#[tokio::main(flavor = "current_thread")]
async fn start_query_session_count(sender: std::sync::mpsc::Sender<Data>) {
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;
}
}