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
This commit is contained in:
1 parent
8511bda6cf
commit
14c73f7122
5 files changed
+66
-3
No files matched your search
@@ -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()
|
||||
|
||||
@@ -423,6 +423,7 @@ pub trait BitmapImage {
|
||||
|
||||
/// A bitmap in big endian bgra32 color format, with storage
|
||||
/// in a Vec<u8>.
|
||||
#[derive(Clone)]
|
||||
pub struct Image {
|
||||
data: Vec<u8>,
|
||||
width: usize,
|
||||
|
||||
+13
-1
@@ -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) {}
|
||||
}
|
||||
@@ -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<R, F: Send + 'static + FnMut(&mut dyn Any, &dyn WindowOps) -> anyhow::Result<R>>(
|
||||
&self,
|
||||
mut func: F,
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user