Skip to content

Commit

Permalink
feat: support system tray (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge authored Nov 30, 2024
1 parent 57bc599 commit fdfc260
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
tauri-build = { version = "2.0.0", features = [] }

[dependencies]
tauri = { version = "2.0.0", features = ["macos-private-api"] }
tauri = { version = "2.0.0", features = ["macos-private-api", "tray-icon"] }
tauri-plugin-shell = "2.0.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
25 changes: 25 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub fn run() {

enable_autostart(app);
enable_shortcut(app);
enable_tray(app);

Ok(())
})
Expand Down Expand Up @@ -218,3 +219,27 @@ fn remove_shortcut<R: Runtime>(app: &tauri::AppHandle<R>) -> Result<(), String>

Ok(())
}

fn enable_tray(app: &mut tauri::App) {
use tauri::{
menu::{Menu, MenuItem},
tray::TrayIconBuilder,
};

let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap();
let menu = Menu::with_items(app, &[&quit_i]).unwrap();
let _tray = TrayIconBuilder::new()
.icon(app.default_window_icon().unwrap().clone())
.menu(&menu)
.on_menu_event(|app, event| match event.id.as_ref() {
"quit" => {
println!("quit menu item was clicked");
app.exit(0);
}
_ => {
println!("menu item {:?} not handled", event.id);
}
})
.build(app)
.unwrap();
}

0 comments on commit fdfc260

Please # to comment.