Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions apps/desktop-tauri/src-tauri/src/tray_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,13 @@ fn selected_tray_percents(
snapshot: &crate::commands::ProviderUsageSnapshot,
settings: &Settings,
) -> (f64, Option<f64>) {
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)),
)
}

Expand Down Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions apps/desktop-tauri/src-tauri/src/usage_metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RateWindowSnapshot>) {
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<ProviderId>,
Expand Down
Loading