change tray menu text when toggling visibility

This commit is contained in:
Joseph Montanaro 2024-01-26 21:03:45 -08:00
parent 70e23c7e20
commit 69f6a39396
4 changed files with 48 additions and 15 deletions

View File

@ -3,8 +3,10 @@
* ~~Switch to "process" provider for AWS credentials (much less hacky)~~ * ~~Switch to "process" provider for AWS credentials (much less hacky)~~
* ~~Frontend needs to react when request is cancelled from backend~~ * ~~Frontend needs to react when request is cancelled from backend~~
* Session timeout (plain duration, or activity-based?) * Session timeout (plain duration, or activity-based?)
* Indicate on approval screen when additional requests are pending
* ~~Fix rehide behavior when new request comes in while old one is still being resolved~~ * ~~Fix rehide behavior when new request comes in while old one is still being resolved~~
* Additional hotkey configuration (approve/deny at the very least) * Additional hotkey configuration (approve/deny at the very least)
* ~~Switch tray menu item to Hide when window is visible~~
* Logging * Logging
* Icon * Icon
* Auto-updates * Auto-updates

View File

@ -31,8 +31,8 @@ pub static APP: OnceCell<AppHandle> = OnceCell::new();
pub fn run() -> tauri::Result<()> { pub fn run() -> tauri::Result<()> {
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| { .plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
app.get_window("main") show_main_window(app)
.map(|w| w.show().error_popup("Failed to show main window")); .error_popup("Failed to show main window");
})) }))
.system_tray(tray::create()) .system_tray(tray::create())
.on_system_tray_event(tray::handle_event) .on_system_tray_event(tray::handle_event)
@ -49,9 +49,9 @@ pub fn run() -> tauri::Result<()> {
.setup(|app| rt::block_on(setup(app))) .setup(|app| rt::block_on(setup(app)))
.build(tauri::generate_context!())? .build(tauri::generate_context!())?
.run(|app, run_event| match run_event { .run(|app, run_event| match run_event {
tauri::RunEvent::WindowEvent { label, event, .. } => match event { tauri::RunEvent::WindowEvent { event, .. } => match event {
tauri::WindowEvent::CloseRequested { api, .. } => { tauri::WindowEvent::CloseRequested { api, .. } => {
let _ = app.get_window(&label).map(|w| w.hide()); let _ = hide_main_window(app);
api.prevent_close(); api.prevent_close();
} }
_ => () _ => ()
@ -114,12 +114,41 @@ async fn setup(app: &mut App) -> Result<(), Box<dyn Error>> {
// if session is empty, this is probably the first launch, so don't autohide // if session is empty, this is probably the first launch, so don't autohide
if !conf.start_minimized || is_first_launch { if !conf.start_minimized || is_first_launch {
app.get_window("main") show_main_window(&app.handle())?;
.ok_or(HandlerError::NoMainWindow)?
.show()?;
} }
let state = AppState::new(conf, session, pool, setup_errors, desktop_is_gnome); let state = AppState::new(conf, session, pool, setup_errors, desktop_is_gnome);
app.manage(state); app.manage(state);
Ok(()) Ok(())
} }
pub fn show_main_window(app: &AppHandle) -> Result<(), WindowError> {
let w = app.get_window("main").ok_or(WindowError::NoMainWindow)?;
w.show()?;
app.tray_handle()
.get_item("show_hide")
.set_title("Hide")?;
Ok(())
}
pub fn hide_main_window(app: &AppHandle) -> Result<(), WindowError> {
let w = app.get_window("main").ok_or(WindowError::NoMainWindow)?;
w.hide()?;
app.tray_handle()
.get_item("show_hide")
.set_title("Show")?;
Ok(())
}
pub fn toggle_main_window(app: &AppHandle) -> Result<(), WindowError> {
let w = app.get_window("main").ok_or(WindowError::NoMainWindow)?;
if w.is_visible()? {
hide_main_window(app)
}
else {
show_main_window(app)
}
}

View File

@ -11,6 +11,7 @@ use tauri::{
async_runtime as rt, async_runtime as rt,
}; };
use crate::app;
use crate::credentials::{ use crate::credentials::{
Session, Session,
BaseCredentials, BaseCredentials,
@ -42,19 +43,19 @@ impl Visibility {
// `original` represents the visibility of the window before any leases were acquired // `original` represents the visibility of the window before any leases were acquired
// None means we don't know, Some(false) means it was previously hidden, // None means we don't know, Some(false) means it was previously hidden,
// Some(true) means it was previously visible // Some(true) means it was previously visible
let is_visible = window.is_visible()?;
if self.original.is_none() { if self.original.is_none() {
let is_visible = window.is_visible()?;
self.original = Some(is_visible); self.original = Some(is_visible);
} }
let state = app.state::<AppState>(); let state = app.state::<AppState>();
if matches!(self.original, Some(true)) && state.desktop_is_gnome { if is_visible && state.desktop_is_gnome {
// Gnome has a really annoying "focus-stealing prevention" behavior means we // Gnome has a really annoying "focus-stealing prevention" behavior means we
// can't just pop up when the window is already visible, so to work around it // can't just pop up when the window is already visible, so to work around it
// we hide and then immediately unhide the window // we hide and then immediately unhide the window
window.hide()?; window.hide()?;
} }
window.show()?; app::show_main_window(&app)?;
window.set_focus()?; window.set_focus()?;
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
@ -72,7 +73,7 @@ impl Visibility {
visibility.leases -= 1; visibility.leases -= 1;
if visibility.leases == 0 { if visibility.leases == 0 {
if let Some(false) = visibility.original { if let Some(false) = visibility.original {
window.hide().error_print(); app::hide_main_window(&handle).error_print();
} }
visibility.original = None; visibility.original = None;
} }

View File

@ -1,15 +1,16 @@
use tauri::{ use tauri::{
AppHandle, AppHandle,
Manager,
SystemTray, SystemTray,
SystemTrayEvent, SystemTrayEvent,
SystemTrayMenu, SystemTrayMenu,
CustomMenuItem, CustomMenuItem,
}; };
use crate::app;
pub fn create() -> SystemTray { pub fn create() -> SystemTray {
let show = CustomMenuItem::new("show".to_string(), "Show"); let show = CustomMenuItem::new("show_hide".to_string(), "Show");
let quit = CustomMenuItem::new("exit".to_string(), "Exit"); let quit = CustomMenuItem::new("exit".to_string(), "Exit");
let menu = SystemTrayMenu::new() let menu = SystemTrayMenu::new()
@ -25,8 +26,8 @@ pub fn handle_event(app: &AppHandle, event: SystemTrayEvent) {
SystemTrayEvent::MenuItemClick{ id, .. } => { SystemTrayEvent::MenuItemClick{ id, .. } => {
match id.as_str() { match id.as_str() {
"exit" => app.exit(0), "exit" => app.exit(0),
"show" => { "show_hide" => {
let _ = app.get_window("main").map(|w| w.show()); let _ = app::toggle_main_window(app);
} }
_ => (), _ => (),
} }