AkurAI Build
Menu

Public repository / master

AkurAI-Drive / desktop/src-tauri/src/tray.rs

Latest change 2d36cd6c72fc6a63a7edb87bb54a4020e50f2f8a - desktop: Tauri v2 tray + Google-Drive-style file browser app by olibuijr

//! KDE/StatusNotifier tray icon: a menu (Open, Sync now, Open folder, a disabled
//! status line, Quit), a left-click window toggle, and helpers to show/hide the
//! window and reflect sync status onto the tray.

use std::sync::Arc;

use tauri::menu::{Menu, MenuItem, PredefinedMenuItem};
use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
use tauri::{include_image, AppHandle, Manager, Wry};

use crate::sync::SyncManager;

/// Holds the handles we need to mutate after the tray is built.
struct TrayState {
    status_item: MenuItem<Wry>,
}

/// Show + focus the main window (creating nothing — it already exists, hidden).
pub fn show_window(app: &AppHandle) {
    match app.get_webview_window("main") {
        Some(w) => {
            if let Err(e) = w.show() {
                tracing::error!(error = %e, "window show failed");
            }
            let _ = w.unminimize();
            if let Err(e) = w.set_focus() {
                tracing::warn!(error = %e, "window set_focus failed");
            }
            tracing::info!("window shown + focused");
        }
        None => tracing::error!("main window not found"),
    }
}

/// Toggle the window's visibility (used by left-click on the tray).
pub fn toggle_window(app: &AppHandle) {
    if let Some(w) = app.get_webview_window("main") {
        let visible = w.is_visible().unwrap_or(false);
        if visible {
            if let Err(e) = w.hide() {
                tracing::error!(error = %e, "window hide failed");
            } else {
                tracing::info!("window hidden (tray toggle)");
            }
        } else {
            show_window(app);
        }
    } else {
        tracing::error!("main window not found for toggle");
    }
}

/// Build the tray icon, its menu, and wire up events.
pub fn build_tray(app: &AppHandle) -> tauri::Result<()> {
    let open_i = MenuItem::with_id(app, "open", "Open AkurAI Drive", true, None::<&str>)?;
    let sync_i = MenuItem::with_id(app, "sync", "Sync now", true, None::<&str>)?;
    let folder_i = MenuItem::with_id(app, "folder", "Open folder", true, None::<&str>)?;
    // Disabled informational line.
    let status_i = MenuItem::with_id(app, "status", "Status: idle", false, None::<&str>)?;
    let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;

    let menu = Menu::with_items(
        app,
        &[
            &open_i,
            &sync_i,
            &folder_i,
            &PredefinedMenuItem::separator(app)?,
            &status_i,
            &PredefinedMenuItem::separator(app)?,
            &quit_i,
        ],
    )?;

    // Keep the status item so the sync loop can relabel it.
    app.manage(TrayState {
        status_item: status_i.clone(),
    });

    TrayIconBuilder::with_id("main")
        .icon(include_image!("icons/tray.png"))
        .tooltip("AkurAI Drive — idle")
        .menu(&menu)
        .show_menu_on_left_click(false)
        .on_menu_event(|app, event| {
            let id = event.id.as_ref();
            tracing::info!(item = %id, "tray menu event");
            match id {
                "open" => show_window(app),
                "sync" => {
                    if let Some(m) = app.try_state::<Arc<SyncManager>>() {
                        m.trigger_sync();
                    } else {
                        tracing::error!("sync manager state missing on tray sync");
                    }
                }
                "folder" => {
                    let app2 = app.clone();
                    tauri::async_runtime::spawn(async move {
                        if let Err(e) = crate::commands::open_folder(app2).await {
                            tracing::error!(error = %e, "tray 'open folder' failed");
                        }
                    });
                }
                "quit" => {
                    tracing::info!("quit selected from tray");
                    app.exit(0);
                }
                other => tracing::warn!(item = %other, "unhandled tray menu item"),
            }
        })
        .on_tray_icon_event(|tray, event| {
            if let TrayIconEvent::Click {
                button: MouseButton::Left,
                button_state: MouseButtonState::Up,
                ..
            } = event
            {
                toggle_window(tray.app_handle());
            }
        })
        .build(app)?;

    tracing::info!("tray icon built");
    Ok(())
}

/// Reflect a sync state (`idle` / `syncing` / `error` / `login`) onto the tray
/// status item and tooltip.
pub fn update_status(app: &AppHandle, state: &str) {
    let (label, tip) = match state {
        "syncing" => ("Status: syncing…", "AkurAI Drive — syncing…"),
        "error" => ("Status: error", "AkurAI Drive — error"),
        "login" => ("Status: signing in…", "AkurAI Drive — signing in…"),
        _ => ("Status: idle", "AkurAI Drive — idle"),
    };

    if let Some(ts) = app.try_state::<TrayState>() {
        if let Err(e) = ts.status_item.set_text(label) {
            tracing::warn!(error = %e, "tray status set_text failed");
        }
    }
    if let Some(tray) = app.tray_by_id("main") {
        if let Err(e) = tray.set_tooltip(Some(tip)) {
            tracing::warn!(error = %e, "tray set_tooltip failed");
        }
    }
}