1
0
mirror of https://github.com/spacebarchat/client.git synced 2024-11-25 03:32:54 +01:00

tauri: tray icon

This commit is contained in:
Puyodead1 2023-12-20 09:49:25 -05:00
parent bc7cfec5c5
commit 3b00be1029
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
3 changed files with 54 additions and 0 deletions

View File

@ -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"

View File

@ -1,5 +1,6 @@
#[cfg(desktop)]
use tauri::Manager;
mod tray;
#[tauri::command]
async fn close_splashscreen(window: tauri::Window) {
@ -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");

36
src-tauri/src/tray.rs Normal file
View File

@ -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(())
}