diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index 01aa287d5..30b65fc20 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -550,7 +550,7 @@ impl TermWindow { let clipboard_contents = Arc::new(Mutex::new(None)); let window = Window::new_window( - "wezterm", + "org.wezfurlong.wezterm", "wezterm", dimensions.pixel_width, dimensions.pixel_height, @@ -581,6 +581,16 @@ impl TermWindow { }), )?; + let icon_image = + image::load_from_memory(include_bytes!("../../../assets/icon/terminal.png"))?; + let image = icon_image.to_bgra(); + let (width, height) = image.dimensions(); + window.set_icon(window::Image::from_raw( + width as usize, + height as usize, + image.into_raw(), + )); + let cloned_window = window.clone(); Connection::get() diff --git a/window/src/bitmaps/mod.rs b/window/src/bitmaps/mod.rs index e8848822b..7073c764b 100644 --- a/window/src/bitmaps/mod.rs +++ b/window/src/bitmaps/mod.rs @@ -423,6 +423,7 @@ pub trait BitmapImage { /// A bitmap in big endian bgra32 color format, with storage /// in a Vec. +#[derive(Clone)] pub struct Image { data: Vec, width: usize, diff --git a/window/src/lib.rs b/window/src/lib.rs index c3dd0d7a2..2f81d126a 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -18,7 +18,7 @@ mod egl; #[cfg(feature = "opengl")] pub use glium; -pub use bitmaps::BitmapImage; +pub use bitmaps::{BitmapImage, Image}; pub use color::Color; pub use connection::*; pub use input::*; @@ -228,6 +228,13 @@ pub trait WindowOps { /// Set some text in the clipboard fn set_clipboard(&self, text: String) -> Future<()>; + + /// Set the icon for the window. + /// Depending on the system this may be shown in its titlebar + /// and/or in the task manager/task switcher + fn set_icon(&self, _image: Image) -> Future<()> { + Future::ok(()) + } } pub trait WindowOpsMut { @@ -262,4 +269,9 @@ pub trait WindowOpsMut { /// The coordinates are of the top left pixel of the /// client area. fn set_window_position(&self, _coords: ScreenPoint) {} + + /// Set the icon for the window. + /// Depending on the system this may be shown in its titlebar + /// and/or in the task manager/task switcher + fn set_icon(&mut self, _image: &dyn BitmapImage) {} } diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 0b393cda7..83674cc89 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -644,7 +644,7 @@ impl XWindow { /// Create a new window on the specified screen with the specified /// dimensions pub fn new_window( - _class_name: &str, + class_name: &str, name: &str, width: usize, height: usize, @@ -721,6 +721,11 @@ impl XWindow { })) }; + xcb_util::icccm::set_wm_class(&*conn, window_id, class_name, class_name); + xcb_util::ewmh::set_wm_pid(conn.ewmh_conn(), window_id, unsafe { + libc::getpid() as u32 + }); + xcb::change_property( &*conn, xcb::PROP_MODE_REPLACE as u8, @@ -819,6 +824,26 @@ impl WindowOpsMut for XWindowInner { fn set_title(&mut self, title: &str) { xcb_util::icccm::set_wm_name(self.conn.conn(), self.window_id, title); } + + fn set_icon(&mut self, image: &dyn BitmapImage) { + let (width, height) = image.image_dimensions(); + + // https://specifications.freedesktop.org/wm-spec/wm-spec-1.3.html#idm44927025355360 + // says that this is an array of 32bit ARGB data. + // The first two elements are width, height, with the remainder + // being the the row data, left-to-right, top-to-bottom. + let mut icon_data = Vec::with_capacity((2 + (width * height)) * 4); + icon_data.push(width as u32); + icon_data.push(height as u32); + icon_data.extend_from_slice(image.pixels()); + + xcb_util::ewmh::set_wm_icon( + self.conn.ewmh_conn(), + xcb::PROP_MODE_REPLACE as u8, + self.window_id, + &icon_data, + ); + } } impl WindowOps for XWindow { @@ -879,6 +904,13 @@ impl WindowOps for XWindow { }) } + fn set_icon(&self, image: Image) -> Future<()> { + XConnection::with_window_inner(self.0, move |inner| { + inner.set_icon(&image); + Ok(()) + }) + } + fn apply anyhow::Result>( &self, mut func: F, diff --git a/window/src/os/x_and_wayland.rs b/window/src/os/x_and_wayland.rs index ca751659b..45452d289 100644 --- a/window/src/os/x_and_wayland.rs +++ b/window/src/os/x_and_wayland.rs @@ -179,6 +179,14 @@ impl WindowOps for Window { } } + fn set_icon(&self, image: crate::bitmaps::Image) -> Future<()> { + match self { + Self::X11(x) => x.set_icon(image), + #[cfg(feature = "wayland")] + Self::Wayland(w) => w.set_icon(image), + } + } + fn set_inner_size(&self, width: usize, height: usize) -> Future<()> { match self { Self::X11(x) => x.set_inner_size(width, height),