From afd50c1f0ee2223a9d8f01e4f8c9e7b5483e1aef Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sun, 11 Jun 2023 15:08:16 -0500 Subject: [PATCH] tray --- src-tauri/src/main.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index dacc2f2..062e8f2 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,11 +9,15 @@ extern crate serde; mod kanji; mod srs; +use std::process; use std::str::FromStr; use anyhow::{Context, Result}; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; -use tauri::SystemTray; +use tauri::{ + CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu, + SystemTrayMenuItem, WindowEvent, +}; use tokio::fs; use crate::kanji::KanjiDb; @@ -44,7 +48,11 @@ async fn main() -> Result<()> { sqlx::migrate!().run(&srs_pool).await?; // System tray - let tray = SystemTray::new(); + let quit = CustomMenuItem::new("quit".to_string(), "Quit"); + let tray_menu = SystemTrayMenu::new().add_item(quit); + let tray = SystemTray::new() + .with_tooltip("Houhou") + .with_menu(tray_menu); // Build tauri tauri::Builder::default() @@ -56,6 +64,22 @@ async fn main() -> Result<()> { kanji::get_kanji, kanji::get_single_kanji ]) + .on_window_event(|event| match event.event() { + WindowEvent::CloseRequested { api, .. } => { + event.window().hide().unwrap(); + api.prevent_close(); + } + _ => {} + }) + .on_system_tray_event(|app, event| match event { + SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { + "quit" => { + process::exit(0); + } + _ => {} + }, + _ => {} + }) .run(tauri::generate_context!()) .context("error while running tauri application")?;