Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(tray): recreate tray icon to get rightmost position after policy reset

Louis Beaumont23f2a5908a1f
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/tray.rs b/apps/screenpipe-app-tauri/src-tauri/src/tray.rs
index 98e5f331a..02546fb23 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/tray.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/tray.rs
@@ -8,10 +8,10 @@ use anyhow::Result;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::Mutex;
-use tauri::tray::TrayIcon;
+use tauri::tray::{TrayIcon, TrayIconBuilder};
use tauri::Emitter;
use tauri::{
- menu::{MenuBuilder, MenuItemBuilder, PredefinedMenuItem},
+ menu::{MenuBuilder, MenuItem, MenuItemBuilder, PredefinedMenuItem},
AppHandle, Manager, WebviewUrl, WebviewWindowBuilder, Wry,
};
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
@@ -19,6 +19,10 @@ use tauri_plugin_opener::OpenerExt;
use tracing::{debug, error, info};
+/// Global storage for the update menu item so we can recreate the tray
+/// without needing to pass the update_item through every call chain.
+static UPDATE_MENU_ITEM: Lazy<Mutex<Option<MenuItem<Wry>>>> = Lazy::new(|| Mutex::new(None));
+
// Track last known state to avoid unnecessary updates
static LAST_MENU_STATE: Lazy<Mutex<MenuState>> = Lazy::new(|| Mutex::new(MenuState::default()));
@@ -30,6 +34,11 @@ struct MenuState {
}
pub fn setup_tray(app: &AppHandle, update_item: &tauri::menu::MenuItem<Wry>) -> Result<()> {
+ // Store update_item globally so recreate_tray can use it
+ if let Ok(mut guard) = UPDATE_MENU_ITEM.lock() {
+ *guard = Some(update_item.clone());
+ }
+
if let Some(main_tray) = app.tray_by_id("screenpipe_main") {
// Initial menu setup with empty state
let menu = create_dynamic_menu(app, &MenuState::default(), update_item)?;
@@ -44,6 +53,62 @@ pub fn setup_tray(app: &AppHandle, update_item: &tauri::menu::MenuItem<Wry>) ->
Ok(())
}
+/// Destroy and recreate the tray icon to get a fresh rightmost position.
+/// On MacBook Pro models with a notch, the tray icon can get pushed behind
+/// the notch when there are many status bar items. Recreating it assigns
+/// the rightmost (most visible) position.
+pub fn recreate_tray(app: &AppHandle) {
+ let update_item = match UPDATE_MENU_ITEM.lock() {
+ Ok(guard) => guard.clone(),
+ Err(_) => {
+ error!("failed to lock UPDATE_MENU_ITEM for tray recreation");
+ return;
+ }
+ };
+
+ let update_item = match update_item {
+ Some(item) => item,
+ None => {
+ debug!("update_item not yet stored, skipping tray recreation");
+ return;
+ }
+ };
+
+ // Remove the old tray icon
+ let _old = app.remove_tray_by_id("screenpipe_main");
+
+ // Create a new tray icon — macOS assigns it the rightmost position
+ let icon = match app.path().resolve("assets/screenpipe-logo-tray-white.png", tauri::path::BaseDirectory::Resource) {
+ Ok(path) => tauri::image::Image::from_path(path).ok(),
+ Err(_) => tauri::image::Image::from_path("assets/screenpipe-logo-tray-white.png").ok(),
+ };
+
+ let mut builder = TrayIconBuilder::<Wry>::with_id("screenpipe_main")
+ .icon_as_template(true)
+ .show_menu_on_left_click(true);
+
+ if let Some(icon) = icon {
+ builder = builder.icon(icon);
+ } else {
+ error!("failed to load tray icon for recreation");
+ }
+
+ match builder.build(app) {
+ Ok(new_tray) => {
+ // Setup menu
+ if let Ok(menu) = create_dynamic_menu(app, &MenuState::default(), &update_item) {
+ let _ = new_tray.set_menu(Some(menu));
+ }
+ // Setup click handlers
+ let _ = setup_tray_click_handlers(&new_tray);
+ info!("tray icon recreated at rightmost position");
+ }
+ Err(e) => {
+ error!("failed to recreate tray icon: {}", e);
+ }
+ }
+}
+
fn create_dynamic_menu(
app: &AppHandle,
_state: &MenuState,
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/window_api.rs b/apps/screenpipe-app-tauri/src-tauri/src/window_api.rs
index 8b87ad95b..52951c236 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/window_api.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/window_api.rs
@@ -12,25 +12,14 @@ use tauri_nspanel::WebviewWindowExt;
use crate::{store::OnboardingStore, ServerState};
-/// Reset activation policy to Regular and refresh the tray icon.
+/// Reset activation policy to Regular and recreate the tray icon.
/// On MacBook Pro models with a notch, toggling between Accessory and Regular
-/// can cause macOS to re-layout status bar items, pushing the tray icon behind
-/// the notch. Re-setting the icon forces macOS to re-render and reposition it.
+/// can push the tray icon behind the notch. Destroying and recreating the
+/// NSStatusItem gives it the rightmost (most visible) position.
#[cfg(target_os = "macos")]
pub fn reset_to_regular_and_refresh_tray(app: &AppHandle) {
let _ = app.set_activation_policy(tauri::ActivationPolicy::Regular);
-
- if let Some(tray) = app.tray_by_id("screenpipe_main") {
- // Re-set the icon to force macOS to re-render the NSStatusItem
- // This works around a macOS bug where the tray icon disappears
- // or gets stuck behind the notch after activation policy changes
- if let Ok(icon) = tauri::image::Image::from_path("assets/screenpipe-logo-tray-white.png") {
- let _ = tray.set_visible(false);
- let _ = tray.set_visible(true);
- let _ = tray.set_icon(Some(icon));
- let _ = tray.set_icon_as_template(true);
- }
- }
+ crate::tray::recreate_tray(app);
}
#[derive(Deserialize, Debug)]