Skip to content

Commit ac49f3b

Browse files
committed
feat(backend): add update info in systtray item
1 parent a21a5e3 commit ac49f3b

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src-tauri/src/setup.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
use anyhow::Result;
22
use std::error::Error;
3-
use tauri::{App, Manager};
3+
use tauri::{App, AppHandle, Manager};
44

55
use crate::commands::{
66
fs::settings::{read_settings, write_settings, Settings},
77
timer::update_info_interval,
88
};
99

1010
pub fn tauri_setup(app: &mut App) -> Result<(), Box<(dyn Error + 'static)>> {
11-
//! NOTE: If not declared here, ownership errors will occur within the async block!
12-
let app = app.app_handle();
11+
setup_inner(&app.app_handle());
12+
Ok(())
13+
}
1314

15+
pub fn setup_inner(app: &AppHandle) {
16+
let app = app.clone();
1417
tauri::async_runtime::spawn(async move {
1518
let settings = read_settings().unwrap_or_else(|err| {
1619
error!("Fallback to default settings. Reason: {err}");
@@ -21,5 +24,4 @@ pub fn tauri_setup(app: &mut App) -> Result<(), Box<(dyn Error + 'static)>> {
2124
info!("duration minutes: {duration_mins}");
2225
update_info_interval(app, duration_mins).await;
2326
});
24-
Ok(())
2527
}

src-tauri/src/system_tray.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
use tauri::Manager;
21
use tauri::{AppHandle, CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu};
2+
use tauri::{Manager, SystemTrayMenuItem};
3+
4+
use crate::setup::setup_inner;
35

46
pub fn create_system_tray() -> SystemTray {
57
let toggle_window = CustomMenuItem::new("toggle_window".to_string(), "Show");
8+
let update_info = CustomMenuItem::new("update_info".to_string(), "Update info");
69
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
7-
let tray_menu = SystemTrayMenu::new().add_item(toggle_window).add_item(quit);
10+
let tray_menu = SystemTrayMenu::new()
11+
.add_item(update_info)
12+
.add_native_item(SystemTrayMenuItem::Separator)
13+
.add_item(toggle_window)
14+
.add_item(quit);
815
SystemTray::new().with_menu(tray_menu)
916
}
1017

@@ -32,16 +39,19 @@ pub fn tray_event(app: &AppHandle, event: SystemTrayEvent) {
3239
info!("Double click");
3340
}
3441
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
42+
"reload_info" => setup_inner(app),
3543
"toggle_window" => {
3644
let item_handle = app.tray_handle().get_item(&id);
3745
let window = app.get_window("main").unwrap();
38-
39-
if window.is_visible().unwrap() {
40-
window.hide().unwrap();
41-
item_handle.set_title("Show").unwrap();
42-
} else {
43-
window.show().unwrap();
44-
item_handle.set_title("Hide").unwrap();
46+
match window.is_visible().unwrap() {
47+
true => {
48+
window.hide().expect("Couldn't hide window");
49+
item_handle.set_title("Show").expect("Couldn't set title");
50+
}
51+
false => {
52+
window.show().expect("Couldn't show window");
53+
item_handle.set_title("Hide").expect("Couldn't set title");
54+
}
4555
}
4656
}
4757
"quit" => {

0 commit comments

Comments
 (0)