Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(tray): keep the menu's shape so an open menu is not rebuilt and closed (T2)

purple68cbf667310f
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 80e28b67..7dd5c004 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -1401,7 +1401,7 @@ pub fn run() {
#[cfg(desktop)]
app.manage(tray_menu::TrayMenuState::<tauri::Wry>::default());
#[cfg(desktop)]
- let (menu, menu_infos) = tray_menu::build_menu(app, &tray_menu::TrayStatus::default())?;
+ let (menu, menu_items) = tray_menu::build_menu(app, &tray_menu::TrayStatus::default())?;
// GH #204: on Linux the tray needs libayatana-appindicator, and
// building it without that library aborts the whole app. Without
@@ -1433,13 +1433,14 @@ pub fn run() {
.menu(&menu)
.on_menu_event(|app, event| {
let id = event.id().as_ref();
- let action = tray_menu::TrayAction::from_id(id);
match id {
- tray_menu::OPEN_ID | "tray-plan" | "tray-models" => {
+ tray_menu::OPEN_ID | tray_menu::ACTION_ID => {
onboarding_window::bring_to_front(app);
// The page opens Plan & Credits or Models.
- if let Some(action) = action {
- let _ = app.emit(tray_menu::ACTION_EVENT, action.event_name());
+ if id == tray_menu::ACTION_ID {
+ if let Some(action) = tray_menu::current_action(app) {
+ let _ = app.emit(tray_menu::ACTION_EVENT, action.event_name());
+ }
}
}
tray_menu::QUIT_ID => {
@@ -1470,7 +1471,7 @@ pub fn run() {
}
})
.build(app)?;
- tray_menu::remember(app.handle(), tray_menu::TrayStatus::default(), menu_infos);
+ tray_menu::remember(app.handle(), tray_menu::TrayStatus::default(), menu_items);
}
// ─── Close → hide to tray instead of quit ───
diff --git a/src-tauri/src/tray_menu.rs b/src-tauri/src/tray_menu.rs
index 27390514..abb9be5c 100644
--- a/src-tauri/src/tray_menu.rs
+++ b/src-tauri/src/tray_menu.rs
@@ -12,11 +12,16 @@
//
// The grey lines are disabled items. The frontend knows the account, the
// engine and the model, so it sends the lines (`tray_update`); this side
-// only checks and lays them out. The action is one of two fixed ids with
+// only checks and lays them out. The action is one of two fixed actions with
// labels from here, so a page can change what the menu says but never what
// a click does. A click on the icon emits `lu-tray-opened`, and the frontend
// answers with fresh lines; texts change in place, so a menu that is
// already open on a Mac updates while it is open.
+//
+// Only a different number of grey lines builds a new menu, and a new menu
+// closes one that is open (seen in the Mac test build, 31.10.). So the action
+// is one item whose label changes, and the page keeps the line count steady
+// (three lines in every state once the memory figure is in).
use serde::Deserialize;
@@ -28,6 +33,9 @@ pub(crate) const OPENED_EVENT: &str = "lu-tray-opened";
pub(crate) const ACTION_EVENT: &str = "lu-tray-action";
pub(crate) const OPEN_ID: &str = "open";
+/// The one action item: "Plan & Credits…" or "Models…", whichever the
+/// current status names.
+pub(crate) const ACTION_ID: &str = "tray-action";
pub(crate) const QUIT_ID: &str = "quit";
const INFO_ID_PREFIX: &str = "info-";
@@ -52,12 +60,6 @@ pub(crate) enum TrayAction {
}
impl TrayAction {
- pub(crate) fn id(self) -> &'static str {
- match self {
- TrayAction::Plan => "tray-plan",
- TrayAction::Models => "tray-models",
- }
- }
pub(crate) fn label(self) -> &'static str {
match self {
TrayAction::Plan => "Plan & Credits…",
@@ -71,9 +73,6 @@ impl TrayAction {
TrayAction::Models => "models",
}
}
- pub(crate) fn from_id(id: &str) -> Option<TrayAction> {
- [TrayAction::Plan, TrayAction::Models].into_iter().find(|a| a.id() == id)
- }
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
@@ -144,10 +143,11 @@ pub(crate) fn layout(status: &TrayStatus) -> Vec<Row> {
rows
}
-/// Two statuses with the same shape differ only in texts and dots, which
-/// change in place. Anything else needs a new menu.
+/// Two statuses with the same shape differ only in texts, dots and which
+/// action the action item names, all of which change in place. Anything else
+/// needs a new menu.
pub(crate) fn same_shape(a: &TrayStatus, b: &TrayStatus) -> bool {
- a.lines.len() == b.lines.len() && a.action == b.action
+ a.lines.len() == b.lines.len() && a.action.is_some() == b.action.is_some()
}
#[cfg(desktop)]
@@ -200,7 +200,13 @@ mod desktop {
struct Live<R: Runtime> {
status: TrayStatus,
+ items: MenuItems<R>,
+ }
+
+ /// The items whose text changes in place.
+ pub(crate) struct MenuItems<R: Runtime> {
infos: Vec<IconMenuItem<R>>,
+ action: Option<MenuItem<R>>,
}
impl<R: Runtime> Default for TrayMenuState<R> {
@@ -213,13 +219,15 @@ mod desktop {
dot.map(|d| Image::new_owned(dot_rgba(d, DOT_SIZE), DOT_SIZE, DOT_SIZE))
}
- /// Build the menu for `status`. Returns the menu and the info items.
+ /// Build the menu for `status`. Returns the menu and the items that
+ /// change in place.
pub(crate) fn build_menu<R: Runtime, M: Manager<R>>(
app: &M,
status: &TrayStatus,
- ) -> tauri::Result<(Menu<R>, Vec<IconMenuItem<R>>)> {
+ ) -> tauri::Result<(Menu<R>, MenuItems<R>)> {
let menu = Menu::new(app)?;
let mut infos = Vec::new();
+ let mut action = None;
for (i, row) in layout(status).into_iter().enumerate() {
match row {
Row::Info(line) => {
@@ -235,7 +243,11 @@ mod desktop {
infos.push(item);
}
Row::Separator => menu.append(&PredefinedMenuItem::separator(app)?)?,
- Row::Action(a) => menu.append(&MenuItem::with_id(app, a.id(), a.label(), true, None::<&str>)?)?,
+ Row::Action(a) => {
+ let item = MenuItem::with_id(app, ACTION_ID, a.label(), true, None::<&str>)?;
+ menu.append(&item)?;
+ action = Some(item);
+ }
Row::Open => menu.append(&MenuItem::with_id(app, OPEN_ID, "Open LU", true, None::<&str>)?)?,
Row::Quit => {
// ⌘Q next to Quit, as in every Mac menu. On Windows and
@@ -248,7 +260,7 @@ mod desktop {
}
}
}
- Ok((menu, infos))
+ Ok((menu, MenuItems { infos, action }))
}
/// Show `status` in the tray menu. Same shape: texts and dots change in
@@ -260,7 +272,7 @@ mod desktop {
let status = sanitize(status);
let mut live = state.inner.lock().unwrap_or_else(|e| e.into_inner());
if let Some(cur) = live.as_mut().filter(|cur| same_shape(&cur.status, &status)) {
- for (item, (old, new)) in cur.infos.iter().zip(cur.status.lines.iter().zip(&status.lines)) {
+ for (item, (old, new)) in cur.items.infos.iter().zip(cur.status.lines.iter().zip(&status.lines)) {
if old.text != new.text {
item.set_text(&new.text)?;
}
@@ -268,27 +280,39 @@ mod desktop {
item.set_icon(dot_image(new.dot))?;
}
}
+ if let (Some(item), Some(new)) = (&cur.items.action, status.action) {
+ if cur.status.action != Some(new) {
+ item.set_text(new.label())?;
+ }
+ }
cur.status = status;
return Ok(());
}
- let (menu, infos) = build_menu(app, &status)?;
+ let (menu, items) = build_menu(app, &status)?;
tray.set_menu(Some(menu))?;
- *live = Some(Live { status, infos });
+ *live = Some(Live { status, items });
Ok(())
}
+ /// The action the action item names right now.
+ pub(crate) fn current_action<R: Runtime>(app: &AppHandle<R>) -> Option<TrayAction> {
+ let state = app.try_state::<TrayMenuState<R>>()?;
+ let live = state.inner.lock().unwrap_or_else(|e| e.into_inner());
+ live.as_ref()?.status.action
+ }
+
/// Record the menu the tray was built with, so the first update can
/// change it in place.
- pub(crate) fn remember<R: Runtime>(app: &AppHandle<R>, status: TrayStatus, infos: Vec<IconMenuItem<R>>) {
+ pub(crate) fn remember<R: Runtime>(app: &AppHandle<R>, status: TrayStatus, items: MenuItems<R>) {
if let Some(state) = app.try_state::<TrayMenuState<R>>() {
- *state.inner.lock().unwrap_or_else(|e| e.into_inner()) = Some(Live { status, infos });
+ *state.inner.lock().unwrap_or_else(|e| e.into_inner()) = Some(Live { status, items });
}
}
- /// Memory in use for the menu's third line, measured where it can be:
- /// an NVIDIA card through nvidia-smi ("GPU memory"), a Mac's unified
- /// memory through sysinfo ("Memory": the model lives there). Elsewhere
- /// nothing measures the card, and the line is left out.
+ /// Memory in use for the menu's third line: an NVIDIA card through
+ /// nvidia-smi ("GPU memory"), otherwise the computer's memory through
+ /// sysinfo ("Memory"; on a Mac that is where the model lives, the
+ /// unified memory). `None` only when neither can be read.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct TrayMemory {
@@ -307,17 +331,10 @@ mod desktop {
if let Some((free, total)) = crate::commands::gpu::nvidia_memory_mib() {
return Some(memory_line("GPU memory", (total - free) as f64 / 1024.0, total as f64 / 1024.0));
}
- #[cfg(target_os = "macos")]
- {
- let mut sys = sysinfo::System::new();
- sys.refresh_memory();
- let total = sys.total_memory() as f64 / GIB;
- if total > 0.0 {
- return Some(memory_line("Memory", sys.used_memory() as f64 / GIB, total));
- }
- }
- let _ = GIB;
- None
+ let mut sys = sysinfo::System::new();
+ sys.refresh_memory();
+ let total = sys.total_memory() as f64 / GIB;
+ (total > 0.0).then(|| memory_line("Memory", sys.used_memory() as f64 / GIB, total))
}
/// The frontend's lines for the tray menu.
@@ -402,21 +419,23 @@ mod tests {
assert!(serde_json::from_str::<TrayStatus>(r#"{"action":"delete-everything"}"#).is_err());
let s: TrayStatus = serde_json::from_str(r#"{"lines":[{"text":"x","dot":"green"}],"action":"models"}"#).unwrap();
assert_eq!(s.action, Some(TrayAction::Models));
- assert_eq!(TrayAction::from_id("tray-models"), Some(TrayAction::Models));
- assert_eq!(TrayAction::from_id("open"), None);
assert_eq!(TrayAction::Plan.label(), "Plan & Credits…");
+ assert_eq!(TrayAction::Models.event_name(), "models");
}
#[test]
- fn texts_and_dots_change_in_place_but_a_new_line_count_rebuilds() {
+ fn texts_dots_and_the_action_change_in_place_but_a_new_line_count_rebuilds() {
+ // A rebuild closes a menu that is open, so Local ↔ Cloud with three
+ // lines each must not be one.
let a = TrayStatus { lines: vec![line("x"), line("y")], action: Some(TrayAction::Models) };
let mut b = a.clone();
b.lines[1] = TrayLine { text: "z".into(), dot: Some(TrayDot::Amber) };
assert!(same_shape(&a, &b));
+ let c = TrayStatus { action: Some(TrayAction::Plan), ..a.clone() };
+ assert!(same_shape(&a, &c));
b.lines.push(line("w"));
assert!(!same_shape(&a, &b));
- let c = TrayStatus { action: Some(TrayAction::Plan), ..a.clone() };
- assert!(!same_shape(&a, &c));
+ assert!(!same_shape(&a, &TrayStatus { action: None, ..a.clone() }));
}
#[test]
diff --git a/src/hooks/__tests__/tray-sync.test.ts b/src/hooks/__tests__/tray-sync.test.ts
index 01cb890e..6e9fd6d6 100644
--- a/src/hooks/__tests__/tray-sync.test.ts
+++ b/src/hooks/__tests__/tray-sync.test.ts
@@ -87,7 +87,7 @@ describe('tray sync (T2)', () => {
it('a cloud model kept from the Cloud mode is not shown as the local model', () => {
useModelStore.setState({ activeModel: 'lu-cloud::moonshotai/Kimi-K2.6' })
- expect(currentTrayStatus().lines.map((l) => l.text)).toEqual(['Local', 'No model selected'])
+ expect(currentTrayStatus().lines.map((l) => l.text).slice(0, 2)).toEqual(['Local', 'No model selected'])
})
it('cloud mode: "Signed in", plan, credits incl. bought ones, never the e-mail', () => {
diff --git a/src/lib/__tests__/tray-menu.test.ts b/src/lib/__tests__/tray-menu.test.ts
index 0227f07e..417d7641 100644
--- a/src/lib/__tests__/tray-menu.test.ts
+++ b/src/lib/__tests__/tray-menu.test.ts
@@ -27,16 +27,32 @@ describe('tray menu lines (T2)', () => {
expect(cloudTrayStatus({ ...signedIn, licenseActive: false }).lines[1]).toEqual({ text: 'No active plan', dot: 'grey' })
})
- it('cloud: signed out and still asking say so in one line', () => {
- expect(cloudTrayStatus({ ...signedIn, signedIn: false }).lines).toEqual([{ text: 'LU Cloud · Signed out' }])
- expect(cloudTrayStatus({ ...signedIn, probing: true }).lines).toEqual([{ text: 'LU Cloud · Connecting…' }])
+ it('cloud: signed out and still asking keep the three lines', () => {
+ expect(cloudTrayStatus({ ...signedIn, signedIn: false }).lines.map((l) => l.text)).toEqual([
+ 'LU Cloud · Signed out',
+ 'No plan',
+ 'Sign in under Plan & Credits',
+ ])
+ expect(cloudTrayStatus({ ...signedIn, probing: true }).lines.map((l) => l.text)).toEqual(['LU Cloud · Connecting…', 'Plan …', 'Credits …'])
})
- it('cloud: unknown credits leave the line out, never a 0', () => {
- expect(cloudTrayStatus({ ...signedIn, credits: null }).lines).toHaveLength(2)
+ it('cloud: unknown credits say so, never a 0', () => {
+ expect(cloudTrayStatus({ ...signedIn, credits: null }).lines[2].text).toBe('Credits …')
expect(cloudTrayStatus({ ...signedIn, credits: -3.5 }).lines[2].text).toBe('0 credits left')
})
+ it('every state has three lines, so the open menu is never rebuilt (and closed)', () => {
+ const states = [
+ cloudTrayStatus(signedIn),
+ cloudTrayStatus({ ...signedIn, signedIn: false }),
+ cloudTrayStatus({ ...signedIn, probing: true }),
+ cloudTrayStatus({ ...signedIn, pastDue: true, credits: null }),
+ localTrayStatus({ engine: null, model: null, state: 'unknown', memory: null }),
+ localTrayStatus({ engine: 'LU Engine', model: 'm', state: 'ready', memory: { label: 'Memory', usedGb: 1, totalGb: 8 } }),
+ ]
+ for (const s of states) expect(s.lines).toHaveLength(3)
+ })
+
it('a tier without a known name still reads like one', () => {
expect(planName('hosted-max')).toBe('Hosted Max')
expect(planName('self-host')).toBe('Self Host')
@@ -65,6 +81,7 @@ describe('tray menu lines (T2)', () => {
expect(localTrayStatus({ engine: null, model: null, state: 'unknown', memory: null }).lines).toEqual([
{ text: 'Local' },
{ text: 'No model selected', dot: 'grey' },
+ { text: 'Memory …' },
])
expect(localTrayStatus({ engine: 'Ollama', model: 'm', state: 'engine-off', memory: null }).lines[1]).toEqual({ text: 'm · not running', dot: 'amber' })
expect(localTrayStatus({ engine: 'LM Studio', model: 'm', state: 'unknown', memory: null }).lines[1]).toEqual({ text: 'm' })
diff --git a/src/lib/tray-menu.ts b/src/lib/tray-menu.ts
index d3a19f08..9bc9c5fa 100644
--- a/src/lib/tray-menu.ts
+++ b/src/lib/tray-menu.ts
@@ -10,6 +10,10 @@
// Plan & Credits… Models…
//
// No e-mail address in the menu (David, 31.10.), only "Signed in".
+//
+// Always three lines: a different count makes the Rust side build a new
+// menu, and a new menu closes one that is open (Mac test build, 31.10.).
+// So a state with nothing to say on a line says so instead of leaving it out.
import { PLAN_NAMES } from './past-due'
@@ -39,8 +43,15 @@ export function planName(tier: string): string {
}
export function cloudTrayStatus(c: TrayCloudInput): TrayStatus {
- if (c.probing) return { lines: [{ text: 'LU Cloud · Connecting…' }], action: 'plan' }
- if (!c.signedIn) return { lines: [{ text: 'LU Cloud · Signed out' }], action: 'plan' }
+ if (c.probing) {
+ return { lines: [{ text: 'LU Cloud · Connecting…' }, { text: 'Plan …', dot: 'grey' }, { text: 'Credits …' }], action: 'plan' }
+ }
+ if (!c.signedIn) {
+ return {
+ lines: [{ text: 'LU Cloud · Signed out' }, { text: 'No plan', dot: 'grey' }, { text: 'Sign in under Plan & Credits' }],
+ action: 'plan',
+ }
+ }
const lines: TrayLine[] = [{ text: 'LU Cloud · Signed in' }]
if (c.pastDue) {
lines.push({ text: `${c.tier ? `${planName(c.tier)} plan` : 'Plan'} · payment failed`, dot: 'amber' })
@@ -49,9 +60,9 @@ export function cloudTrayStatus(c: TrayCloudInput): TrayStatus {
} else {
lines.push({ text: 'No active plan', dot: 'grey' })
}
- if (c.credits !== null) {
- lines.push({ text: `${Math.max(0, Math.floor(c.credits)).toLocaleString('en-US')} credits left` })
- }
+ lines.push({
+ text: c.credits === null ? 'Credits …' : `${Math.max(0, Math.floor(c.credits)).toLocaleString('en-US')} credits left`,
+ })
return { lines, action: 'plan' }
}
@@ -84,8 +95,11 @@ export function localTrayStatus(l: TrayLocalInput): TrayStatus {
const s = STATE_WORDS[l.state]
lines.push(s ? { text: `${l.model} · ${s.word}`, dot: s.dot } : { text: l.model })
}
- if (l.memory && l.memory.totalGb > 0) {
- lines.push({ text: `${l.memory.label} ${l.memory.usedGb.toFixed(1)} of ${Math.round(l.memory.totalGb)} GB` })
- }
+ lines.push({
+ text:
+ l.memory && l.memory.totalGb > 0
+ ? `${l.memory.label} ${l.memory.usedGb.toFixed(1)} of ${Math.round(l.memory.totalGb)} GB`
+ : 'Memory …',
+ })
return { lines, action: 'models' }
}