From 14c73f7122369481a765b0fe8347d0cbb334c7bf Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 2 May 2020 11:53:45 -0700 Subject: [PATCH] window: linux: set window icon Teach the window layer about window icons and implement the plumbing for this on X11. For Wayland there is no direct way to specify the icon; instead the application ID is used to locate an appropriate .desktop filename. We set the app id from the classname but that didn't match the installed name for our desktop file which is namespaced under my domain, so change the window class to match that and enable the window icon on Wayland. refs: https://github.com/wez/wezterm/issues/172#issuecomment-619938047 --- src/frontend/gui/termwindow.rs | 12 +++++++++++- window/src/bitmaps/mod.rs | 1 + window/src/lib.rs | 14 +++++++++++++- window/src/os/x11/window.rs | 34 +++++++++++++++++++++++++++++++++- window/src/os/x_and_wayland.rs | 8 ++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) 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),