app menu only shows when user is connected

This commit is contained in:
2026-01-15 15:36:54 +08:00
parent 08b4bc4ca3
commit 7ae501aaf0
4 changed files with 48 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ use tokio::time::{sleep, Instant};
use tracing::{info, warn}; use tracing::{info, warn};
use crate::{ use crate::{
lock_w,
remotes::health::{HealthError, HealthRemote}, remotes::health::{HealthError, HealthRemote},
services::{ services::{
auth::{get_access_token, get_tokens}, auth::{get_access_token, get_tokens},
@@ -13,11 +14,16 @@ use crate::{
ws::init_ws_client, ws::init_ws_client,
}, },
state::init_app_data, state::init_app_data,
system_tray::init_system_tray, system_tray::{init_system_tray, update_system_tray},
FDOLL,
}; };
pub async fn start_fdoll() { pub async fn start_fdoll() {
init_system_tray(); let tray = init_system_tray();
{
let mut guard = lock_w!(FDOLL);
guard.tray = Some(tray);
}
if let Err(err) = init_startup_sequence().await { if let Err(err) = init_startup_sequence().await {
tracing::error!("startup sequence failed: {err}"); tracing::error!("startup sequence failed: {err}");
show_health_manager_with_error(Some(err.to_string())); show_health_manager_with_error(Some(err.to_string()));
@@ -66,11 +72,13 @@ pub async fn bootstrap() {
Some(_tokens) => { Some(_tokens) => {
info!("Tokens found in keyring - restoring user session"); info!("Tokens found in keyring - restoring user session");
construct_app().await; construct_app().await;
update_system_tray(true);
} }
None => { None => {
info!("No active session found - showing welcome first"); info!("No active session found - showing welcome first");
open_welcome_window(); open_welcome_window();
close_splash_window(); close_splash_window();
update_system_tray(false);
} }
} }
} }

View File

@@ -1,4 +1,5 @@
use crate::get_app_handle; use crate::get_app_handle;
use crate::{lock_r, state::FDOLL, system_tray::update_system_tray};
use tauri::{Emitter, Manager}; use tauri::{Emitter, Manager};
use tauri_plugin_dialog::{DialogExt, MessageDialogBuilder, MessageDialogKind}; use tauri_plugin_dialog::{DialogExt, MessageDialogBuilder, MessageDialogKind};
use tauri_plugin_positioner::WindowExt; use tauri_plugin_positioner::WindowExt;
@@ -29,6 +30,8 @@ pub fn show_health_manager_with_error(error_message: Option<String>) {
close_window_if_exists(crate::services::scene::SCENE_WINDOW_LABEL); close_window_if_exists(crate::services::scene::SCENE_WINDOW_LABEL);
close_window_if_exists(crate::services::app_menu::APP_MENU_WINDOW_LABEL); close_window_if_exists(crate::services::app_menu::APP_MENU_WINDOW_LABEL);
update_system_tray(false);
let existing_webview_window = app_handle.get_window(HEALTH_MANAGER_WINDOW_LABEL); let existing_webview_window = app_handle.get_window(HEALTH_MANAGER_WINDOW_LABEL);
if let Some(window) = existing_webview_window { if let Some(window) = existing_webview_window {
@@ -113,6 +116,10 @@ pub fn close_health_manager_window() {
error!("Failed to close health manager window: {}", e); error!("Failed to close health manager window: {}", e);
} else { } else {
info!("Health manager window closed"); info!("Health manager window closed");
let guard = lock_r!(FDOLL);
let is_logged_in = guard.app_data.user.is_some();
drop(guard);
update_system_tray(is_logged_in);
} }
} }
} }

View File

@@ -37,6 +37,7 @@ pub struct AppState {
pub auth_pass: Option<AuthPass>, pub auth_pass: Option<AuthPass>,
pub oauth_flow: OAuthFlowTracker, pub oauth_flow: OAuthFlowTracker,
pub tracing_guard: Option<tracing_appender::non_blocking::WorkerGuard>, pub tracing_guard: Option<tracing_appender::non_blocking::WorkerGuard>,
pub tray: Option<tauri::tray::TrayIcon>,
// exposed to the frontend // exposed to the frontend
pub app_data: AppData, pub app_data: AppData,

View File

@@ -1,12 +1,11 @@
use crate::{get_app_handle, lock_r, services::app_menu::open_app_menu_window, state::FDOLL};
use tauri::{ use tauri::{
menu::{Menu, MenuItem}, menu::{Menu, MenuItem},
tray::TrayIconBuilder, tray::TrayIconBuilder,
}; };
use tracing::error; use tracing::error;
use crate::{get_app_handle, services::app_menu::open_app_menu_window}; pub fn init_system_tray() -> tauri::tray::TrayIcon {
pub fn init_system_tray() {
let app = get_app_handle(); let app = get_app_handle();
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap(); let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap();
@@ -18,7 +17,7 @@ pub fn init_system_tray() {
Err(err) => todo!("Handle error: {}", err), Err(err) => todo!("Handle error: {}", err),
}; };
match TrayIconBuilder::new() TrayIconBuilder::new()
.menu(&menu) .menu(&menu)
.on_menu_event(|app, event| match event.id.as_ref() { .on_menu_event(|app, event| match event.id.as_ref() {
"quit" => { "quit" => {
@@ -33,8 +32,31 @@ pub fn init_system_tray() {
}) })
.icon(app.default_window_icon().unwrap().clone()) .icon(app.default_window_icon().unwrap().clone())
.build(app) .build(app)
{ .unwrap_or_else(|err| panic!("Failed to build tray: {}", err))
Ok(it) => it, }
Err(err) => todo!("Handle error: {}", err),
}; pub fn update_system_tray(is_logged_in: bool) {
let app = get_app_handle();
let guard = lock_r!(FDOLL);
if let Some(tray) = &guard.tray {
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap();
let menu = if is_logged_in {
let open_app_menu_i =
MenuItem::with_id(app, "open-app-menu", "Open App Menu", true, None::<&str>)
.unwrap();
Menu::with_items(app, &[&open_app_menu_i, &quit_i])
} else {
Menu::with_items(app, &[&quit_i])
};
let menu = match menu {
Ok(it) => it,
Err(err) => {
error!("Failed to create menu: {}", err);
return;
}
};
if let Err(err) = tray.set_menu(Some(menu)) {
error!("Failed to update tray menu: {}", err);
}
}
} }