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
216
217
218
219
220
|
use std::{
process::exit,
time::{Duration, Instant},
};
use crate::{
path,
usecase::{view::Event, DesktopServiceState},
BUFFER,
};
use async_std::task::sleep;
use crossbeam_channel::{Receiver, Sender};
use tauri::{
CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent, SystemTrayMenu,
SystemTrayMenuItem, WindowEvent,
};
pub async fn run(sender: Sender<Event>, receiver: Receiver<Event>) {
let setup_sender = sender.clone();
let menu_sender = sender.clone();
let tray_sender = sender.clone();
let menu = Menu::new()
.add_submenu(Submenu::new(
"Service",
Menu::new()
.add_item(CustomMenuItem::new("restart", "Restart"))
.add_native_item(MenuItem::Separator)
.add_item(CustomMenuItem::new("start", "Start"))
.add_item(CustomMenuItem::new("stop", "Stop")),
))
.add_submenu(Submenu::new(
"Logs",
Menu::new()
.add_item(CustomMenuItem::new("hbbs.out", "hbbs.out"))
.add_item(CustomMenuItem::new("hbbs.err", "hbbs.err"))
.add_native_item(MenuItem::Separator)
.add_item(CustomMenuItem::new("hbbr.out", "hbbr.out"))
.add_item(CustomMenuItem::new("hbbr.err", "hbbr.err")),
))
.add_submenu(Submenu::new(
"Configuration",
Menu::new().add_item(CustomMenuItem::new(".env", ".env")),
));
let tray = SystemTray::new().with_menu(
SystemTrayMenu::new()
.add_item(CustomMenuItem::new("restart", "Restart"))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("start", "Start"))
.add_item(CustomMenuItem::new("stop", "Stop"))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("exit", "Exit GUI")),
);
let mut app = tauri::Builder::default()
.on_window_event(|event| match event.event() {
// WindowEvent::Resized(size) => {
// if size.width == 0 && size.height == 0 {
// event.window().hide().unwrap();
// }
// }
WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
event.window().hide().unwrap();
}
_ => {}
})
.menu(menu)
.on_menu_event(move |event| {
// println!(
// "send {}: {}",
// std::time::SystemTime::now()
// .duration_since(std::time::UNIX_EPOCH)
// .unwrap_or_default()
// .as_millis(),
// event.menu_item_id()
// );
menu_sender
.send(Event::ViewAction(event.menu_item_id().to_owned()))
.unwrap_or_default()
})
.system_tray(tray)
.on_system_tray_event(move |app, event| match event {
SystemTrayEvent::LeftClick { .. } => {
let main = app.get_window("main").unwrap();
if main.is_visible().unwrap() {
main.hide().unwrap();
} else {
main.show().unwrap();
main.unminimize().unwrap();
main.set_focus().unwrap();
}
}
SystemTrayEvent::MenuItemClick { id, .. } => {
tray_sender.send(Event::ViewAction(id)).unwrap_or_default();
}
_ => {}
})
.setup(move |app| {
setup_sender.send(Event::ViewInit).unwrap_or_default();
app.listen_global("__action__", move |msg| {
match msg.payload().unwrap_or_default() {
r#""__init__""# => setup_sender.send(Event::BroswerInit).unwrap_or_default(),
r#""restart""# => setup_sender
.send(Event::BrowserAction("restart".to_owned()))
.unwrap_or_default(),
_ => (),
}
});
Ok(())
})
.invoke_handler(tauri::generate_handler![root])
.build(tauri::generate_context!())
.expect("error while running tauri application");
let mut now = Instant::now();
let mut blink = false;
let mut span = 0;
let mut title = "".to_owned();
let product = "RustDesk Server";
let buffer = BUFFER.get().unwrap().to_owned();
loop {
for _ in 1..buffer {
match receiver.recv_timeout(Duration::from_nanos(1)) {
Ok(event) => {
let main = app.get_window("main").unwrap();
let menu = main.menu_handle();
let tray = app.tray_handle();
match event {
Event::BrowserUpdate((action, data)) => match action.as_str() {
"file" => {
let list = ["hbbs.out", "hbbs.err", "hbbr.out", "hbbr.err", ".env"];
let id = data.as_str();
if list.contains(&id) {
for file in list {
menu.get_item(file)
.set_selected(file == id)
.unwrap_or_default();
}
// println!(
// "emit {}: {}",
// std::time::SystemTime::now()
// .duration_since(std::time::UNIX_EPOCH)
// .unwrap_or_default()
// .as_millis(),
// data
// );
app.emit_all("__update__", (action, data))
.unwrap_or_default();
}
}
_ => (),
},
Event::ViewRenderAppExit => exit(0),
Event::ViewRenderServiceState(state) => {
let enabled = |id, enabled| {
menu.get_item(id).set_enabled(enabled).unwrap_or_default();
tray.get_item(id).set_enabled(enabled).unwrap_or_default();
};
title = format!("{} {:?}", product, state);
main.set_title(title.as_str()).unwrap_or_default();
match state {
DesktopServiceState::Started => {
enabled("start", false);
enabled("stop", true);
enabled("restart", true);
blink = false;
}
DesktopServiceState::Stopped => {
enabled("start", true);
enabled("stop", false);
enabled("restart", false);
blink = true;
}
_ => {
enabled("start", false);
enabled("stop", false);
enabled("restart", false);
blink = true;
}
}
}
_ => (),
}
}
Err(_) => break,
}
}
let elapsed = now.elapsed().as_micros();
if elapsed > 16666 {
now = Instant::now();
// println!("{}ms", elapsed as f64 * 0.001);
let iteration = app.run_iteration();
if iteration.window_count == 0 {
break;
}
if blink {
if span > 1000000 {
span = 0;
app.get_window("main")
.unwrap()
.set_title(title.as_str())
.unwrap_or_default();
} else {
span += elapsed;
if span > 500000 {
app.get_window("main")
.unwrap()
.set_title(product)
.unwrap_or_default();
}
}
}
} else {
sleep(Duration::from_micros(999)).await;
}
}
}
#[tauri::command]
fn root() -> String {
path().to_str().unwrap_or_default().to_owned()
}
|