From ff36db07f131d632a434ff5bec2b10b87a229520 Mon Sep 17 00:00:00 2001 From: cyrgani Date: Sat, 12 Apr 2025 22:37:13 +0200 Subject: [PATCH] allow setting the icon and window title at runtime --- src/lib.rs | 19 +++++++++++++++++++ src/native.rs | 2 ++ src/native/linux_x11.rs | 6 ++++++ src/native/windows.rs | 6 ++++++ 4 files changed, 33 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7f396c82..d250c1f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -259,6 +259,25 @@ pub mod window { .unwrap(); } + /// Set the application's window title. + /// TODO: This is not implemented for all platforms yet. + pub fn set_window_title(title: &str) { + let d = native_display().lock().unwrap(); + d.native_requests + .send(native::Request::SetWindowTitle(title.to_string())) + .unwrap(); + } + + // Since icons are large structs, wrap them in a Box to avoid excessive stack consumption. + /// Set the application's icon. + /// TODO: This is not implemented for all platforms yet. + pub fn set_window_icon(icon: Box) { + let d = native_display().lock().unwrap(); + d.native_requests + .send(native::Request::SetWindowIcon(icon)) + .unwrap(); + } + /// Get the position of the window. /// TODO: implement for other platforms #[cfg(any(target_os = "windows", target_os = "linux"))] diff --git a/src/native.rs b/src/native.rs index 88640a55..fa92ee81 100644 --- a/src/native.rs +++ b/src/native.rs @@ -71,6 +71,8 @@ pub(crate) enum Request { SetWindowPosition { new_x: u32, new_y: u32 }, SetFullscreen(bool), ShowKeyboard(bool), + SetWindowTitle(String), + SetWindowIcon(Box), } pub trait Clipboard: Send + Sync { diff --git a/src/native/linux_x11.rs b/src/native/linux_x11.rs index 31c9605b..5046d32c 100644 --- a/src/native/linux_x11.rs +++ b/src/native/linux_x11.rs @@ -420,6 +420,12 @@ impl X11Display { ShowKeyboard(..) => { eprintln!("Not implemented for X11") } + SetWindowTitle(title) => { + self.libx11.update_window_title(self.display, self.window, &title); + } + SetWindowIcon(icon) => { + self.libx11.update_window_icon(self.display, self.window, &icon); + } } } } diff --git a/src/native/windows.rs b/src/native/windows.rs index de328aad..24d42729 100644 --- a/src/native/windows.rs +++ b/src/native/windows.rs @@ -869,6 +869,12 @@ impl WindowsDisplay { ShowKeyboard(_show) => { eprintln!("Not implemented for windows") } + SetWindowTitle(_title) => { + eprintln!("SetWindowTitle is not yet implemented on windows"); + } + SetWindowIcon(icon) => unsafe { + set_icon(self.wnd, &icon); + }, } } }