Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Implement window attention (#58779)

Lukas Wirth905e955a7027
diff --git a/crates/agent_ui/src/agent_panel.rs b/crates/agent_ui/src/agent_panel.rs
index 8ff5e046a46..bf6f61295b3 100644
--- a/crates/agent_ui/src/agent_panel.rs
+++ b/crates/agent_ui/src/agent_panel.rs
@@ -2660,11 +2660,13 @@ impl AgentPanel {
let settings = AgentSettings::get_global(cx);
match settings.notify_when_agent_waiting {
NotifyWhenAgentWaiting::PrimaryScreen => {
+ window.request_attention();
if let Some(primary) = cx.primary_display() {
self.pop_up_terminal_notification(terminal_id, &title, primary, window, cx);
}
}
NotifyWhenAgentWaiting::AllScreens => {
+ window.request_attention();
for screen in cx.displays() {
self.pop_up_terminal_notification(terminal_id, &title, screen, window, cx);
}
diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rs
index 7b6dd48ca65..baae5267649 100644
--- a/crates/agent_ui/src/conversation_view.rs
+++ b/crates/agent_ui/src/conversation_view.rs
@@ -2860,6 +2860,7 @@ impl ConversationView {
match settings.notify_when_agent_waiting {
NotifyWhenAgentWaiting::PrimaryScreen => {
+ window.request_attention();
if let Some(primary) = cx.primary_display() {
self.pop_up(
icon,
@@ -2875,6 +2876,7 @@ impl ConversationView {
}
}
NotifyWhenAgentWaiting::AllScreens => {
+ window.request_attention();
let caption = caption.into();
for screen in cx.displays() {
self.pop_up(
diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs
index 224fb5e2040..3d23df0e944 100644
--- a/crates/gpui/src/platform.rs
+++ b/crates/gpui/src/platform.rs
@@ -736,6 +736,8 @@ pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
answers: &[PromptButton],
) -> Option<oneshot::Receiver<usize>>;
fn activate(&self);
+ /// Requests that the operating system draw attention to this window.
+ fn request_attention(&self) {}
fn is_active(&self) -> bool;
fn is_hovered(&self) -> bool;
fn background_appearance(&self) -> WindowBackgroundAppearance;
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index 5246964274a..249d703e98d 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -5192,6 +5192,11 @@ impl Window {
self.platform_window.activate();
}
+ /// Requests that the operating system draw attention to this window.
+ pub fn request_attention(&self) {
+ self.platform_window.request_attention();
+ }
+
/// Minimize the current window at the platform level.
pub fn minimize_window(&self) {
self.platform_window.minimize();
diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/src/linux/wayland/window.rs
index 675602082bb..6d2753e131d 100644
--- a/crates/gpui_linux/src/linux/wayland/window.rs
+++ b/crates/gpui_linux/src/linux/wayland/window.rs
@@ -1539,6 +1539,8 @@ impl PlatformWindow for WaylandWindow {
}
}
+ fn request_attention(&self) {}
+
fn is_active(&self) -> bool {
self.borrow().active
}
diff --git a/crates/gpui_linux/src/linux/x11/window.rs b/crates/gpui_linux/src/linux/x11/window.rs
index a459ed03b5d..4fde66f06ca 100644
--- a/crates/gpui_linux/src/linux/x11/window.rs
+++ b/crates/gpui_linux/src/linux/x11/window.rs
@@ -18,7 +18,7 @@ use x11rb::{
connection::Connection,
cookie::{Cookie, VoidCookie},
errors::ConnectionError,
- properties::WmSizeHints,
+ properties::{WmHints, WmSizeHints},
protocol::{
sync,
xinput::{self, ConnectionExt as _},
@@ -1492,6 +1492,33 @@ impl PlatformWindow for X11Window {
xcb_flush(&self.0.xcb);
}
+ fn request_attention(&self) {
+ if self.is_active() {
+ return;
+ }
+
+ let mut hints = WmHints::new();
+ match WmHints::get(&*self.0.xcb, self.0.x_window) {
+ Ok(cookie) => match cookie.reply() {
+ Ok(Some(existing_hints)) => hints = existing_hints,
+ Ok(None) => {}
+ Err(error) => {
+ log::debug!("failed to read X11 WM_HINTS before setting urgency: {error}")
+ }
+ },
+ Err(error) => {
+ log::debug!("failed to request X11 WM_HINTS before setting urgency: {error}")
+ }
+ }
+ hints.urgent = true;
+ check_reply(
+ || "X11 ChangeProperty for WM_HINTS urgency failed.",
+ hints.set(&*self.0.xcb, self.0.x_window),
+ )
+ .log_err();
+ xcb_flush(&self.0.xcb);
+ }
+
fn is_active(&self) -> bool {
self.0.state.borrow().active
}
diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs
index 4be73b8ce39..011ece9d919 100644
--- a/crates/gpui_macos/src/window.rs
+++ b/crates/gpui_macos/src/window.rs
@@ -10,10 +10,11 @@ use block::ConcreteBlock;
use cocoa::{
appkit::{
NSAppKitVersionNumber, NSAppKitVersionNumber12_0, NSApplication, NSBackingStoreBuffered,
- NSColor, NSEvent, NSEventModifierFlags, NSFilenamesPboardType, NSPasteboard, NSScreen,
- NSView, NSViewHeightSizable, NSViewWidthSizable, NSVisualEffectMaterial,
- NSVisualEffectState, NSVisualEffectView, NSWindow, NSWindowCollectionBehavior,
- NSWindowOcclusionState, NSWindowOrderingMode, NSWindowStyleMask, NSWindowTitleVisibility,
+ NSColor, NSEvent, NSEventModifierFlags, NSFilenamesPboardType, NSPasteboard,
+ NSRequestUserAttentionType, NSScreen, NSView, NSViewHeightSizable, NSViewWidthSizable,
+ NSVisualEffectMaterial, NSVisualEffectState, NSVisualEffectView, NSWindow,
+ NSWindowCollectionBehavior, NSWindowOcclusionState, NSWindowOrderingMode,
+ NSWindowStyleMask, NSWindowTitleVisibility,
},
base::{id, nil},
foundation::{
@@ -1471,6 +1472,22 @@ impl PlatformWindow for MacWindow {
.detach();
}
+ fn request_attention(&self) {
+ if self.is_active() {
+ return;
+ }
+
+ let executor = self.0.lock().foreground_executor.clone();
+ executor
+ .spawn(async move {
+ unsafe {
+ let app = NSApplication::sharedApplication(nil);
+ app.requestUserAttention_(NSRequestUserAttentionType::NSInformationalRequest);
+ }
+ })
+ .detach();
+ }
+
fn is_active(&self) -> bool {
unsafe { self.0.lock().native_window.isKeyWindow() == YES }
}
diff --git a/crates/gpui_windows/src/window.rs b/crates/gpui_windows/src/window.rs
index 5a4129b493c..49a935bafe4 100644
--- a/crates/gpui_windows/src/window.rs
+++ b/crates/gpui_windows/src/window.rs
@@ -820,6 +820,27 @@ impl PlatformWindow for WindowsWindow {
.detach();
}
+ fn request_attention(&self) {
+ if self.is_active() {
+ return;
+ }
+
+ let hwnd = self.0.hwnd;
+ self.0
+ .executor
+ .spawn(async move {
+ let info = FLASHWINFO {
+ cbSize: std::mem::size_of::<FLASHWINFO>() as u32,
+ hwnd,
+ dwFlags: FLASHW_ALL | FLASHW_TIMERNOFG,
+ uCount: 0,
+ dwTimeout: 0,
+ };
+ unsafe { FlashWindowEx(&info).ok().log_err() };
+ })
+ .detach();
+ }
+
fn is_active(&self) -> bool {
self.0.hwnd == unsafe { GetActiveWindow() }
}