diff --git a/apps/desktop-tauri/src-tauri/src/tray_bridge.rs b/apps/desktop-tauri/src-tauri/src/tray_bridge.rs index c2f18b8fe0..694c4e9bbd 100644 --- a/apps/desktop-tauri/src-tauri/src/tray_bridge.rs +++ b/apps/desktop-tauri/src-tauri/src/tray_bridge.rs @@ -671,16 +671,13 @@ fn selected_tray_percents( snapshot: &crate::commands::ProviderUsageSnapshot, settings: &Settings, ) -> (f64, Option) { - let primary = crate::usage_metric::selected_usage_window(snapshot, settings).used_percent; - - let secondary = snapshot - .secondary - .as_ref() - .map(|w| display_metric_percent(w.used_percent, settings.show_as_used)); - + let (selected, companion) = + crate::usage_metric::selected_usage_icon_windows(snapshot, settings); ( - display_metric_percent(primary, settings.show_as_used), - secondary, + display_metric_percent(selected.used_percent, settings.show_as_used), + companion + .as_ref() + .map(|window| display_metric_percent(window.used_percent, settings.show_as_used)), ) } @@ -1386,6 +1383,43 @@ mod tests { assert_eq!(primary, 72.0); } + #[test] + fn single_meaningful_secondary_quota_uses_full_single_meter() { + let settings = Settings::default(); + let mut snapshot = fake_snapshot_with("claude", "Claude", 0.0, Some(42.0), None, None); + snapshot.primary.is_informational = true; + + let (primary, secondary) = selected_tray_percents(&snapshot, &settings); + + assert_eq!(primary, 42.0); + assert_eq!(secondary, None); + } + + #[test] + fn selected_secondary_quota_is_not_duplicated_when_tertiary_is_meaningful() { + let settings = Settings::default(); + let mut snapshot = + fake_snapshot_with("claude", "Claude", 0.0, Some(42.0), Some(30.0), None); + snapshot.primary.is_informational = true; + + let (primary, secondary) = selected_tray_percents(&snapshot, &settings); + + assert_eq!(primary, 42.0); + assert_eq!(secondary, Some(30.0)); + } + + #[test] + fn two_meaningful_quotas_keep_two_meter_layout() { + let mut settings = Settings::default(); + settings.set_provider_metric(ProviderId::Cursor, MetricPreference::Session); + let snapshot = fake_snapshot_with("cursor", "Cursor", 15.0, Some(40.0), None, None); + + let (primary, secondary) = selected_tray_percents(&snapshot, &settings); + + assert_eq!(primary, 15.0); + assert_eq!(secondary, Some(40.0)); + } + #[test] fn informational_primary_skips_session_and_automatic_phantom_zero() { let mut settings = Settings::default(); diff --git a/apps/desktop-tauri/src-tauri/src/usage_metric.rs b/apps/desktop-tauri/src-tauri/src/usage_metric.rs index be5c83bfa0..c464f75c57 100644 --- a/apps/desktop-tauri/src-tauri/src/usage_metric.rs +++ b/apps/desktop-tauri/src-tauri/src/usage_metric.rs @@ -21,6 +21,42 @@ pub(crate) fn selected_usage_window( .unwrap_or_else(|| snapshot.primary.clone()) } +/// Select the primary tray metric and, when there are multiple meaningful core +/// quotas, one distinct companion lane. Keeping this policy beside canonical +/// metric selection prevents tray rendering from duplicating the selected lane. +pub(crate) fn selected_usage_icon_windows( + snapshot: &ProviderUsageSnapshot, + settings: &Settings, +) -> (RateWindowSnapshot, Option) { + let selected = selected_usage_window(snapshot, settings); + let meaningful_count = std::iter::once(&snapshot.primary) + .chain(snapshot.secondary.iter()) + .chain(snapshot.tertiary.iter()) + .filter(|window| !window.is_informational) + .count(); + if meaningful_count <= 1 { + return (selected, None); + } + + let companion = snapshot + .secondary + .iter() + .chain(std::iter::once(&snapshot.primary)) + .chain(snapshot.tertiary.iter()) + .filter(|window| !window.is_informational) + .find(|window| !same_window(window, &selected)) + .cloned(); + (selected, companion) +} + +fn same_window(left: &RateWindowSnapshot, right: &RateWindowSnapshot) -> bool { + left.used_percent.to_bits() == right.used_percent.to_bits() + && left.window_minutes == right.window_minutes + && left.resets_at == right.resets_at + && left.reset_description == right.reset_description + && left.is_informational == right.is_informational +} + fn preferred_window( snapshot: &ProviderUsageSnapshot, provider: Option,