Skip to content

Commit

Permalink
tauri: tray icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Dec 20, 2023
1 parent bc7cfec commit 3b00be1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tauri-build = { version = "2.0.0-alpha", features = [] }
# tauri = { version = "2.0.0-alpha", features = [] }
tauri = { git = "https://github.com/tauri-apps/tauri.git", branch = "dev", features = [
"devtools",
"tray-icon",
] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
17 changes: 17 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(desktop)]
use tauri::Manager;
mod tray;

#[tauri::command]
async fn close_splashscreen(window: tauri::Window) {
Expand All @@ -21,6 +22,22 @@ pub fn run() {
std::env::set_var("RUST_LOG", "debug");

tauri::Builder::default()
.setup(move |app| {
#[cfg(desktop)]
{
let handle = app.handle();
tray::create_tray(handle)?;
}

Ok(())
})
.on_window_event(|event| match event.event() {
tauri::WindowEvent::CloseRequested { api, .. } => {
event.window().hide().unwrap();
api.prevent_close();
}
_ => {}
})
.invoke_handler(tauri::generate_handler![close_splashscreen,])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
36 changes: 36 additions & 0 deletions src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use tauri::{
menu::{Menu, MenuItem},
tray::{ClickType, TrayIconBuilder},
Manager, Runtime,
};

pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
let branding = MenuItem::with_id(app, "name", "Spacebar", false, None);
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None);
let menu1 = Menu::with_items(app, &[&branding, &quit_i])?;

let _ = TrayIconBuilder::with_id("main")
.tooltip("Spacebar")
.icon(app.default_window_icon().unwrap().clone())
.menu(&menu1)
.menu_on_left_click(false)
.on_menu_event(move |app, event| match event.id.as_ref() {
"quit" => {
app.exit(0);
}

_ => {}
})
.on_tray_icon_event(|tray, event| {
if event.click_type == ClickType::Left {
let app = tray.app_handle();
if let Some(window) = app.get_window("main") {
let _ = window.show();
let _ = window.set_focus();
}
}
})
.build(app);

Ok(())
}

0 comments on commit 3b00be1

Please sign in to comment.