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

feat: hide overlay when switching macOS Spaces

Louis Beaumont9649592ff5b9
diff --git a/screenpipe-app-tauri/src-tauri/Cargo.lock b/screenpipe-app-tauri/src-tauri/Cargo.lock
index 31f37a2e4..a3a833d28 100755
--- a/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -5635,6 +5635,7 @@ dependencies = [
"async-stream",
"axum",
"base64 0.22.1",
+ "block",
"chrono",
"cocoa 0.26.0",
"core-foundation 0.10.0",
diff --git a/screenpipe-app-tauri/src-tauri/Cargo.toml b/screenpipe-app-tauri/src-tauri/Cargo.toml
index 9c5b1622e..87ec4136b 100644
--- a/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -110,6 +110,7 @@ core-foundation = "=0.10.0"
libc = "0.2.152"
cocoa = "0.26.0"
objc = "0.2.7"
+block = "0.1"
base64 = "0.22.1"
# bunch of hack for permissions
core-graphics-helmer-fork = "0.24.0"
diff --git a/screenpipe-app-tauri/src-tauri/src/space_monitor.rs b/screenpipe-app-tauri/src-tauri/src/space_monitor.rs
new file mode 100644
index 000000000..8464f0109
--- /dev/null
+++ b/screenpipe-app-tauri/src-tauri/src/space_monitor.rs
@@ -0,0 +1,53 @@
+//! macOS Space change monitor
+//!
+//! Listens for NSWorkspaceActiveSpaceDidChangeNotification to detect when
+//! the user switches Spaces (virtual desktops) and hides the overlay.
+
+use crate::commands::hide_main_window;
+use tauri::AppHandle;
+use tracing::debug;
+
+/// Sets up a listener for macOS Space changes.
+/// When the active Space changes, hides the main overlay window.
+pub fn setup_space_listener(app: AppHandle) {
+ use cocoa::base::{id, nil};
+ use cocoa::foundation::NSString;
+ use objc::{class, msg_send, sel, sel_impl};
+ use std::sync::Once;
+
+ static INIT: Once = Once::new();
+
+ INIT.call_once(|| {
+ debug!("Setting up macOS Space change listener");
+
+ // Clone app handle for use in the block
+ let app_for_block = app.clone();
+
+ unsafe {
+ let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
+ let notification_center: id = msg_send![workspace, notificationCenter];
+
+ // NSWorkspaceActiveSpaceDidChangeNotification
+ let notification_name =
+ NSString::alloc(nil).init_str("NSWorkspaceActiveSpaceDidChangeNotification");
+
+ // Create the block that will be called when space changes
+ let block = block::ConcreteBlock::new(move |_notification: id| {
+ debug!("macOS Space changed, hiding overlay");
+ hide_main_window(&app_for_block);
+ });
+ let block = block.copy();
+
+ // Add observer for the notification
+ let _: id = msg_send![
+ notification_center,
+ addObserverForName: notification_name
+ object: workspace
+ queue: nil
+ usingBlock: &*block
+ ];
+
+ debug!("macOS Space change listener registered successfully");
+ }
+ });
+}