fix(tray): cache last presence to skip redundant syncs
Directory/Status fire often; only push the tray when TrayPresence actually changed. Initialize the cache from the same snapshot used at Tray::new, and clear it on locale change so titles retranslate.
@@ -109,6 +109,8 @@ pub struct NotificationCenter {
109
109
hover_hold: bool,
110
110
unread_mentions: HashSet<String>,
111
111
tray: Option<Tray>,
112
/// Last presence pushed to the tray; skip `sync_tray` when unchanged.
113
last_tray_presence: Option<TrayPresence>,
112
114
app_icon: AppIcon,
113
115
shown_badge: Option<Badge>,
114
116
shown_mention_dot: bool,
@@ -133,17 +135,22 @@ impl NotificationCenter {
133
135
let mut settings = settings::load(&app.read(cx).store);
134
136
settings.account = app.read(cx).account_chat_settings().notifications;
135
137
let app_icon = active_appearance::current(cx).app_icon;
138
let initial_presence = tray_presence_from(&app.read(cx), false);
136
139
let tray = if app.read(cx).mode.demo {
137
140
None
138
141
} else {
139
Tray::new(tray_presence_from(&app.read(cx), false), app_icon.png())
142
Tray::new(initial_presence.clone(), app_icon.png())
140
143
};
144
let last_tray_presence = tray.as_ref().map(|_| initial_presence);
141
145
platform::set_window_icon(window, app_icon.png());
142
146
let appearance_observation = cx.observe_global::<ActiveAppearance>(|this, cx| {
143
147
this.apply_app_icon(active_appearance::current(cx).app_icon, cx)
144
148
});
145
let locale_observation = cx.observe_global::<ActiveLocale>(|this, _| {
149
let locale_observation = cx.observe_global::<ActiveLocale>(|this, cx| {
150
// Locale changes the presence title; force a tray sync.
151
this.last_tray_presence = None;
146
152
this.relocalize_tray();
153
this.sync_tray(cx);
147
154
});
148
155
let subscription = cx.subscribe(&app, |this, _, event: &AppEvent, cx| {
149
156
this.on_app_event(event, cx);
@@ -186,6 +193,7 @@ impl NotificationCenter {
186
193
hover_hold: false,
187
194
unread_mentions: HashSet::new(),
188
195
tray,
196
last_tray_presence,
189
197
app_icon,
190
198
shown_badge: None,
191
199
shown_mention_dot: false,
@@ -1273,12 +1281,15 @@ impl NotificationCenter {
1273
1281
}
1274
1282
1275
1283
fn sync_tray(&mut self, cx: &App) {
1276
if let Some(tray) = &self.tray {
1277
tray.sync(tray_presence_from(
1278
self.app.read(cx),
1279
!self.unread_mentions.is_empty(),
1280
));
1284
let Some(tray) = &self.tray else {
1285
return;
1286
};
1287
let presence = tray_presence_from(self.app.read(cx), !self.unread_mentions.is_empty());
1288
if self.last_tray_presence.as_ref() == Some(&presence) {
1289
return;
1281
1290
}
1291
tray.sync(presence.clone());
1292
self.last_tray_presence = Some(presence);
1282
1293
}
1283
1294
1284
1295
fn refresh_badge(&mut self, cx: &mut Context<Self>) {
@@ -109,6 +109,8 @@ pub struct NotificationCenter {109
hover_hold: bool,109
hover_hold: bool,110
unread_mentions: HashSet<String>,110
unread_mentions: HashSet<String>,111
tray: Option<Tray>,111
tray: Option<Tray>,112
/// Last presence pushed to the tray; skip `sync_tray` when unchanged.113
last_tray_presence: Option<TrayPresence>,112
app_icon: AppIcon,114
app_icon: AppIcon,113
shown_badge: Option<Badge>,115
shown_badge: Option<Badge>,114
shown_mention_dot: bool,116
shown_mention_dot: bool,@@ -133,17 +135,22 @@ impl NotificationCenter {133
let mut settings = settings::load(&app.read(cx).store);135
let mut settings = settings::load(&app.read(cx).store);134
settings.account = app.read(cx).account_chat_settings().notifications;136
settings.account = app.read(cx).account_chat_settings().notifications;135
let app_icon = active_appearance::current(cx).app_icon;137
let app_icon = active_appearance::current(cx).app_icon;138
let initial_presence = tray_presence_from(&app.read(cx), false);136
let tray = if app.read(cx).mode.demo {139
let tray = if app.read(cx).mode.demo {137
None140
None138
} else {141
} else {139
Tray::new(tray_presence_from(&app.read(cx), false), app_icon.png())142
Tray::new(initial_presence.clone(), app_icon.png())140
};143
};144
let last_tray_presence = tray.as_ref().map(|_| initial_presence);141
platform::set_window_icon(window, app_icon.png());145
platform::set_window_icon(window, app_icon.png());142
let appearance_observation = cx.observe_global::<ActiveAppearance>(|this, cx| {146
let appearance_observation = cx.observe_global::<ActiveAppearance>(|this, cx| {143
this.apply_app_icon(active_appearance::current(cx).app_icon, cx)147
this.apply_app_icon(active_appearance::current(cx).app_icon, cx)144
});148
});145
let locale_observation = cx.observe_global::<ActiveLocale>(|this, _| {149
let locale_observation = cx.observe_global::<ActiveLocale>(|this, cx| {150
// Locale changes the presence title; force a tray sync.151
this.last_tray_presence = None;146
this.relocalize_tray();152
this.relocalize_tray();153
this.sync_tray(cx);147
});154
});148
let subscription = cx.subscribe(&app, |this, _, event: &AppEvent, cx| {155
let subscription = cx.subscribe(&app, |this, _, event: &AppEvent, cx| {149
this.on_app_event(event, cx);156
this.on_app_event(event, cx);@@ -186,6 +193,7 @@ impl NotificationCenter {186
hover_hold: false,193
hover_hold: false,187
unread_mentions: HashSet::new(),194
unread_mentions: HashSet::new(),188
tray,195
tray,196
last_tray_presence,189
app_icon,197
app_icon,190
shown_badge: None,198
shown_badge: None,191
shown_mention_dot: false,199
shown_mention_dot: false,@@ -1273,12 +1281,15 @@ impl NotificationCenter {1273
}1281
}1274
1282
1275
fn sync_tray(&mut self, cx: &App) {1283
fn sync_tray(&mut self, cx: &App) {1276
if let Some(tray) = &self.tray {1284
let Some(tray) = &self.tray else {1277
tray.sync(tray_presence_from(1285
return;1278
self.app.read(cx),1286
};1279
!self.unread_mentions.is_empty(),1287
let presence = tray_presence_from(self.app.read(cx), !self.unread_mentions.is_empty());1280
));1288
if self.last_tray_presence.as_ref() == Some(&presence) {1289
return;1281
}1290
}1291
tray.sync(presence.clone());1292
self.last_tray_presence = Some(presence);1282
}1293
}1283
1294
1284
fn refresh_badge(&mut self, cx: &mut Context<Self>) {1295
fn refresh_badge(&mut self, cx: &mut Context<Self>) {