-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtray.rs
More file actions
85 lines (75 loc) · 2.23 KB
/
tray.rs
File metadata and controls
85 lines (75 loc) · 2.23 KB
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
use tauri::{
plugin::TauriPlugin, AppHandle, CustomMenuItem, Manager, SystemTray, SystemTrayEvent,
SystemTrayMenu, SystemTrayMenuItem,
};
use tauri_plugin_autostart::MacosLauncher;
use crate::window;
pub enum TrayMenu {
Quit,
Settings,
#[cfg(debug_assertions)]
DevTools,
}
pub fn build() -> SystemTray {
let tray_menu = SystemTrayMenu::new()
.add_item(CustomMenuItem::new(TrayMenu::Settings, "Settings...").accelerator("CommandOrControl+,"))
.add_native_item(SystemTrayMenuItem::Separator);
#[cfg(debug_assertions)]
let tray_menu = tray_menu
.add_item(
CustomMenuItem::new(TrayMenu::DevTools, "Open DevTools").accelerator("CommandOrControl+Shift+I"),
)
.add_native_item(SystemTrayMenuItem::Separator);
let tray_menu = tray_menu.add_item(CustomMenuItem::new(
TrayMenu::Quit,
"Quit Commit Completely",
));
SystemTray::new().with_menu(tray_menu)
}
pub fn handle(app: &AppHandle, event: SystemTrayEvent) {
match event {
SystemTrayEvent::LeftClick { .. } => {
let main_window = app.get_window(window::MAIN).unwrap();
if main_window.is_visible().unwrap() {
window::main_window::hide(&main_window).unwrap();
} else {
window::main_window::show(&main_window).unwrap();
}
},
SystemTrayEvent::MenuItemClick { id, .. } => match id.into() {
TrayMenu::Quit => std::process::exit(0),
TrayMenu::Settings => {
let settings_window = app.get_window(window::SETTINGS).unwrap();
settings_window.show().unwrap();
settings_window.set_focus().unwrap();
},
#[cfg(debug_assertions)]
TrayMenu::DevTools => app.get_window(window::MAIN).unwrap().open_devtools(),
},
_ => {},
};
}
pub fn autostart() -> TauriPlugin<tauri::Wry> {
tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, None)
}
impl From<TrayMenu> for String {
fn from(value: TrayMenu) -> Self {
match value {
TrayMenu::Quit => "quit".to_string(),
TrayMenu::Settings => "settings".to_string(),
#[cfg(debug_assertions)]
TrayMenu::DevTools => "devtools".to_string(),
}
}
}
impl From<String> for TrayMenu {
fn from(value: String) -> Self {
match value.as_str() {
"quit" => TrayMenu::Quit,
"settings" => TrayMenu::Settings,
#[cfg(debug_assertions)]
"devtools" => TrayMenu::DevTools,
_ => unreachable!(),
}
}
}