Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Implement audible system bell (#47531)
Ian Chamberlain971775e3b266
diff --git a/Cargo.lock b/Cargo.lockindex f68704cb6ef..bfd80726843 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -7601,6 +7601,7 @@ dependencies = ["media","metal","objc",+ "objc2-app-kit","parking_lot","pathfinder_geometry","raw-window-handle",@@ -11211,6 +11212,16 @@ dependencies = ["objc2-encode",]+[[package]]+name = "objc2-app-kit"+version = "0.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e6f29f568bec459b0ddff777cec4fe3fd8666d82d5a40ebd0ff7e66134f89bcc"+dependencies = [+ "objc2",+ "objc2-foundation",+]+[[package]]name = "objc2-audio-toolbox"version = "0.3.2"diff --git a/Cargo.toml b/Cargo.tomlindex d1271e01666..3a393237ab9 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -604,6 +604,7 @@ nbformat = "1.2.0"nix = "0.29"num-format = "0.4.4"objc = "0.2"+objc2-app-kit = { version = "0.3", default-features = false, features = [ "NSGraphics" ] }objc2-foundation = { version = "=0.3.2", default-features = false, features = ["NSArray","NSAttributedString",@@ -821,6 +822,7 @@ features = ["Win32_System_Com","Win32_System_Com_StructuredStorage","Win32_System_Console",+ "Win32_System_Diagnostics_Debug","Win32_System_DataExchange","Win32_System_IO","Win32_System_LibraryLoader",diff --git a/crates/gpui/examples/input.rs b/crates/gpui/examples/input.rsindex d15d791cd00..370e27de7d5 100644--- a/crates/gpui/examples/input.rs+++ b/crates/gpui/examples/input.rs@@ -85,14 +85,24 @@ impl TextInput {fn backspace(&mut self, _: &Backspace, window: &mut Window, cx: &mut Context<Self>) {if self.selected_range.is_empty() {- self.select_to(self.previous_boundary(self.cursor_offset()), cx)+ let prev = self.previous_boundary(self.cursor_offset());+ if self.cursor_offset() == prev {+ window.play_system_bell();+ return;+ }+ self.select_to(prev, cx)}self.replace_text_in_range(None, "", window, cx)}fn delete(&mut self, _: &Delete, window: &mut Window, cx: &mut Context<Self>) {if self.selected_range.is_empty() {- self.select_to(self.next_boundary(self.cursor_offset()), cx)+ let next = self.next_boundary(self.cursor_offset());+ if self.cursor_offset() == next {+ window.play_system_bell();+ return;+ }+ self.select_to(next, cx)}self.replace_text_in_range(None, "", window, cx)}diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rsindex 806a34040a4..efca26a6b48 100644--- a/crates/gpui/src/platform.rs+++ b/crates/gpui/src/platform.rs@@ -689,6 +689,8 @@ pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle {fn update_ime_position(&self, _bounds: Bounds<Pixels>);+ fn play_system_bell(&self) {}+#[cfg(any(test, feature = "test-support"))]fn as_test(&mut self) -> Option<&mut TestWindow> {Nonediff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex 48c381e5275..7790480e321 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -5024,6 +5024,12 @@ impl Window {.set_tabbing_identifier(tabbing_identifier)}+ /// Request the OS to play an alert sound. On some platforms this is associated+ /// with the window, for others it's just a simple global function call.+ pub fn play_system_bell(&self) {+ self.platform_window.play_system_bell()+ }+/// Toggles the inspector mode on this window.#[cfg(any(feature = "inspector", debug_assertions))]pub fn toggle_inspector(&mut self, cx: &mut App) {diff --git a/crates/gpui_linux/src/linux/wayland/client.rs b/crates/gpui_linux/src/linux/wayland/client.rsindex b65a203dd34..10f4aab0db1 100644--- a/crates/gpui_linux/src/linux/wayland/client.rs+++ b/crates/gpui_linux/src/linux/wayland/client.rs@@ -58,6 +58,7 @@ use wayland_protocols::xdg::decoration::zv1::client::{zxdg_decoration_manager_v1, zxdg_toplevel_decoration_v1,};use wayland_protocols::xdg::shell::client::{xdg_surface, xdg_toplevel, xdg_wm_base};+use wayland_protocols::xdg::system_bell::v1::client::xdg_system_bell_v1;use wayland_protocols::{wp::cursor_shape::v1::client::{wp_cursor_shape_device_v1, wp_cursor_shape_manager_v1},xdg::dialog::v1::client::xdg_wm_dialog_v1::{self, XdgWmDialogV1},@@ -129,6 +130,7 @@ pub struct Globals {pub text_input_manager: Option<zwp_text_input_manager_v3::ZwpTextInputManagerV3>,pub gesture_manager: Option<zwp_pointer_gestures_v1::ZwpPointerGesturesV1>,pub dialog: Option<xdg_wm_dialog_v1::XdgWmDialogV1>,+ pub system_bell: Option<xdg_system_bell_v1::XdgSystemBellV1>,pub executor: ForegroundExecutor,}@@ -170,6 +172,7 @@ impl Globals {text_input_manager: globals.bind(&qh, 1..=1, ()).ok(),gesture_manager: globals.bind(&qh, 1..=3, ()).ok(),dialog: globals.bind(&qh, dialog_v..=dialog_v, ()).ok(),+ system_bell: globals.bind(&qh, 1..=1, ()).ok(),executor,qh,}@@ -1069,6 +1072,7 @@ impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for WaylandClientStat}delegate_noop!(WaylandClientStatePtr: ignore xdg_activation_v1::XdgActivationV1);+delegate_noop!(WaylandClientStatePtr: ignore xdg_system_bell_v1::XdgSystemBellV1);delegate_noop!(WaylandClientStatePtr: ignore wl_compositor::WlCompositor);delegate_noop!(WaylandClientStatePtr: ignore wp_cursor_shape_device_v1::WpCursorShapeDeviceV1);delegate_noop!(WaylandClientStatePtr: ignore wp_cursor_shape_manager_v1::WpCursorShapeManagerV1);diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/src/linux/wayland/window.rsindex c4ff55fc80c..1e3af66c598 100644--- a/crates/gpui_linux/src/linux/wayland/window.rs+++ b/crates/gpui_linux/src/linux/wayland/window.rs@@ -1479,6 +1479,18 @@ impl PlatformWindow for WaylandWindow {fn gpu_specs(&self) -> Option<GpuSpecs> {self.borrow().renderer.gpu_specs().into()}++ fn play_system_bell(&self) {+ let state = self.borrow();+ let surface = if state.surface_state.toplevel().is_some() {+ Some(&state.surface)+ } else {+ None+ };+ if let Some(bell) = state.globals.system_bell.as_ref() {+ bell.ring(surface);+ }+ }}fn update_window(mut state: RefMut<WaylandWindowState>) {diff --git a/crates/gpui_linux/src/linux/x11/window.rs b/crates/gpui_linux/src/linux/x11/window.rsindex 5e1287976cb..1974cc0bb28 100644--- a/crates/gpui_linux/src/linux/x11/window.rs+++ b/crates/gpui_linux/src/linux/x11/window.rs@@ -1846,4 +1846,9 @@ impl PlatformWindow for X11Window {fn gpu_specs(&self) -> Option<GpuSpecs> {self.0.state.borrow().renderer.gpu_specs().into()}++ fn play_system_bell(&self) {+ // Volume 0% means don't increase or decrease from system volume+ let _ = self.0.xcb.bell(0);+ }}diff --git a/crates/gpui_macos/Cargo.toml b/crates/gpui_macos/Cargo.tomlindex 06e5d0e7321..3626bbd05e8 100644--- a/crates/gpui_macos/Cargo.toml+++ b/crates/gpui_macos/Cargo.toml@@ -48,6 +48,7 @@ mach2.workspace = truemedia.workspace = truemetal.workspace = trueobjc.workspace = true+objc2-app-kit.workspace = trueparking_lot.workspace = truepathfinder_geometry = "0.5"raw-window-handle = "0.6"diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rsindex 398cf46eab0..ace36d69540 100644--- a/crates/gpui_macos/src/window.rs+++ b/crates/gpui_macos/src/window.rs@@ -49,6 +49,7 @@ use objc::{runtime::{BOOL, Class, NO, Object, Protocol, Sel, YES},sel, sel_impl,};+use objc2_app_kit::NSBeep;use parking_lot::Mutex;use raw_window_handle as rwh;use smallvec::SmallVec;@@ -1676,6 +1677,10 @@ impl PlatformWindow for MacWindow {}}+ fn play_system_bell(&self) {+ unsafe { NSBeep() }+ }+#[cfg(any(test, feature = "test-support"))]fn render_to_image(&self, scene: &gpui::Scene) -> Result<RgbaImage> {let mut this = self.0.lock();diff --git a/crates/gpui_windows/src/window.rs b/crates/gpui_windows/src/window.rsindex 3a55100dfb7..92255f93fd9 100644--- a/crates/gpui_windows/src/window.rs+++ b/crates/gpui_windows/src/window.rs@@ -20,7 +20,9 @@ use windows::{Foundation::*,Graphics::Dwm::*,Graphics::Gdi::*,- System::{Com::*, LibraryLoader::*, Ole::*, SystemServices::*},+ System::{+ Com::*, Diagnostics::Debug::MessageBeep, LibraryLoader::*, Ole::*, SystemServices::*,+ },UI::{Controls::*, HiDpi::*, Input::KeyboardAndMouse::*, Shell::*, WindowsAndMessaging::*},},core::*,@@ -950,6 +952,11 @@ impl PlatformWindow for WindowsWindow {self.0.update_ime_position(self.0.hwnd, caret_position);}++ fn play_system_bell(&self) {+ // MB_OK: The sound specified as the Windows Default Beep sound.+ let _ = unsafe { MessageBeep(MB_OK) };+ }}#[implement(IDropTarget)]