feat: tray, window-state, sqlite cache, decrypt worker
System tray (desktop) - tauri tray-icon feature + tray with menu (Öffnen/Ausblenden/Beenden) - Left-click toggles main window; right-click shows menu - JS emits tray-unread-update event, Rust mirrors into tooltip + macOS dock badge via set_badge_label - ConversationsContext wires totalUnread → tray Window state persistence - tauri-plugin-window-state (desktop-only target guard) - Auto-restore size/position/maximized between restarts Local SQLite message cache - tauri-plugin-sql hydration of conversation view on mount - persistMessages after each refresh, deleteCachedMessage on realtime DELETE, pruneCache keeps latest 1000 per conversation - Stores plaintext only (same trust boundary as stronghold device key; cache never leaves the device, E2EE w.r.t. server unchanged) Web Worker for decryption - workers/decrypt.worker.ts runs crypto_secretbox_open_easy + utf-8 decode off the main thread with its own libsodium instance - lib/decryptWorker.ts is a request/response wrapper with inline fallback when Worker spawn fails - shared decryptMessages accepts aeadBatchDelegate so key lookup stays on the main thread while the AEAD loop offloads Build fix - Enable tauri tray-icon feature - Import Listener + Manager traits, clone tray handle for the event listener, conditional icon attach
This commit is contained in:
@@ -1,3 +1,50 @@
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
use tauri::{
|
||||
menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem},
|
||||
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
|
||||
AppHandle, Listener, Manager,
|
||||
};
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
use serde::Deserialize;
|
||||
|
||||
// Payload for the `tray-unread-update` event the JS layer emits whenever the
|
||||
// aggregate unread-count changes. 0 hides the badge / resets the tooltip;
|
||||
// non-zero sets a count indicator.
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
#[derive(Deserialize)]
|
||||
struct TrayUnreadPayload {
|
||||
count: u32,
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
fn show_main_window(app: &AppHandle) {
|
||||
if let Some(win) = app.get_webview_window("main") {
|
||||
let _ = win.show();
|
||||
let _ = win.unminimize();
|
||||
let _ = win.set_focus();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
fn hide_main_window(app: &AppHandle) {
|
||||
if let Some(win) = app.get_webview_window("main") {
|
||||
let _ = win.hide();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
fn handle_menu_event(app: &AppHandle, event: MenuEvent) {
|
||||
match event.id.as_ref() {
|
||||
"tray-show" => show_main_window(app),
|
||||
"tray-hide" => hide_main_window(app),
|
||||
"tray-quit" => {
|
||||
app.exit(0);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let mut builder = tauri::Builder::default()
|
||||
@@ -13,12 +60,91 @@ pub fn run() {
|
||||
.build(),
|
||||
);
|
||||
|
||||
// Global shortcut + updater plugins are desktop-only (no mobile support).
|
||||
// Global shortcut + updater + window-state plugins are desktop-only
|
||||
// (no mobile support — mobile windows are OS-managed).
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
{
|
||||
builder = builder
|
||||
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
|
||||
.plugin(tauri_plugin_updater::Builder::new().build());
|
||||
.plugin(tauri_plugin_updater::Builder::new().build())
|
||||
.plugin(tauri_plugin_window_state::Builder::new().build());
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
{
|
||||
builder = builder.setup(|app| {
|
||||
// Tray icon with a minimal menu. Left-click toggles window
|
||||
// visibility; right-click shows the menu. Badge / tooltip updates
|
||||
// come from the JS side via `tray-unread-update` events.
|
||||
let show = MenuItem::with_id(app, "tray-show", "Öffnen", true, None::<&str>)?;
|
||||
let hide = MenuItem::with_id(app, "tray-hide", "Ausblenden", true, None::<&str>)?;
|
||||
let sep = PredefinedMenuItem::separator(app)?;
|
||||
let quit = MenuItem::with_id(app, "tray-quit", "Beenden", true, None::<&str>)?;
|
||||
let menu = Menu::with_items(app, &[&show, &hide, &sep, &quit])?;
|
||||
|
||||
let mut tray_builder = TrayIconBuilder::with_id("chatapp-tray")
|
||||
.menu(&menu)
|
||||
.show_menu_on_left_click(false)
|
||||
.tooltip("ChatApp")
|
||||
.on_menu_event(|app, event| handle_menu_event(app, event))
|
||||
.on_tray_icon_event(|tray, event| {
|
||||
if let TrayIconEvent::Click {
|
||||
button: MouseButton::Left,
|
||||
button_state: MouseButtonState::Up,
|
||||
..
|
||||
} = event
|
||||
{
|
||||
let app = tray.app_handle();
|
||||
if let Some(win) = app.get_webview_window("main") {
|
||||
if win.is_visible().unwrap_or(false) {
|
||||
let _ = win.hide();
|
||||
} else {
|
||||
let _ = win.show();
|
||||
let _ = win.set_focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// `default_window_icon` returns Option<&Image>; only attach if
|
||||
// we actually have one bundled (should always be true via the
|
||||
// tauri.conf.json icon list, but guard to stay typesafe).
|
||||
if let Some(icon) = app.default_window_icon() {
|
||||
tray_builder = tray_builder.icon(icon.clone());
|
||||
}
|
||||
|
||||
let tray = tray_builder.build(app)?;
|
||||
|
||||
// Listen for JS-side unread updates and mirror them into the tray
|
||||
// tooltip + macOS dock badge. `tray` is cheap to clone (internal
|
||||
// Arc) so we can move it into the listener closure directly.
|
||||
let tray_handle = tray.clone();
|
||||
let badge_window = app.get_webview_window("main");
|
||||
app.listen("tray-unread-update", move |event| {
|
||||
let Ok(payload) = serde_json::from_str::<TrayUnreadPayload>(event.payload())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let tooltip = if payload.count == 0 {
|
||||
"ChatApp".to_string()
|
||||
} else {
|
||||
format!("ChatApp · {} neu", payload.count)
|
||||
};
|
||||
let _ = tray_handle.set_tooltip(Some(tooltip));
|
||||
// macOS dock badge. `set_badge_label` is macOS-only but the
|
||||
// call is a no-op on other platforms so we don't need a cfg.
|
||||
if let Some(win) = badge_window.as_ref() {
|
||||
let badge = if payload.count == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(payload.count.to_string())
|
||||
};
|
||||
let _ = win.set_badge_label(badge);
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
builder
|
||||
|
||||
Reference in New Issue
Block a user