diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b745bc1..36046736 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- On Windows and macOS, add `Window::title` to query the current window title. - On Windows, fix focusing menubar when pressing `Alt`. - On MacOS, made `accepts_first_mouse` configurable. - Migrated `WindowBuilderExtUnix::with_resize_increments` to `WindowBuilder`. diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 5f402cf9..350c39d0 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -856,6 +856,10 @@ impl Window { pub fn theme(&self) -> Option { None } + + pub fn title(&self) -> String { + String::new() + } } #[derive(Default, Clone, Debug)] diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index 2333d60f..ba2e3a3b 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -347,6 +347,11 @@ impl Inner { warn!("`Window::theme` is ignored on iOS"); None } + + pub fn title(&self) -> String { + warn!("`Window::title` is ignored on iOS"); + String::new() + } } pub struct Window { diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 4e5f7610..3ffba899 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -589,6 +589,11 @@ impl Window { pub fn theme(&self) -> Option { x11_or_wayland!(match self; Window(window) => window.theme()) } + + #[inline] + pub fn title(&self) -> String { + x11_or_wayland!(match self; Window(window) => window.title()) + } } /// Hooks for X11 errors. diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index 18b84afa..e30d0873 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -624,6 +624,11 @@ impl Window { pub fn theme(&self) -> Option { None } + + #[inline] + pub fn title(&self) -> String { + String::new() + } } impl Drop for Window { diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 2c600a81..3135febc 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1551,4 +1551,9 @@ impl UnownedWindow { pub fn theme(&self) -> Option { None } + + #[inline] + pub fn title(&self) -> String { + String::new() + } } diff --git a/src/platform_impl/macos/appkit/window.rs b/src/platform_impl/macos/appkit/window.rs index 8e504abd..2c9b5d99 100644 --- a/src/platform_impl/macos/appkit/window.rs +++ b/src/platform_impl/macos/appkit/window.rs @@ -143,6 +143,10 @@ extern_methods!( #[sel(setTitle:)] pub fn setTitle(&self, title: &NSString); + pub fn title_(&self) -> Id { + unsafe { msg_send_id![self, title] } + } + #[sel(setReleasedWhenClosed:)] pub fn setReleasedWhenClosed(&self, val: bool); diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 8723c2cb..368be89a 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -1115,6 +1115,11 @@ impl WinitWindow { let state = self.shared_state.lock().unwrap(); state.current_theme } + + #[inline] + pub fn title(&self) -> String { + self.title_().to_string() + } } impl WindowExtMacOS for WinitWindow { diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index 3dc7a5dc..14290a8a 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -378,6 +378,11 @@ impl Window { pub fn theme(&self) -> Option { None } + + #[inline] + pub fn title(&self) -> String { + String::new() + } } impl Drop for Window { diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 70af126a..1361176a 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -40,13 +40,13 @@ use windows_sys::Win32::{ }, WindowsAndMessaging::{ CreateWindowExW, FlashWindowEx, GetClientRect, GetCursorPos, GetForegroundWindow, - GetSystemMetrics, GetWindowPlacement, IsWindowVisible, LoadCursorW, PeekMessageW, - PostMessageW, RegisterClassExW, SetCursor, SetCursorPos, SetForegroundWindow, - SetWindowPlacement, SetWindowPos, SetWindowTextW, CS_HREDRAW, CS_VREDRAW, - CW_USEDEFAULT, FLASHWINFO, FLASHW_ALL, FLASHW_STOP, FLASHW_TIMERNOFG, FLASHW_TRAY, - GWLP_HINSTANCE, HTCAPTION, MAPVK_VK_TO_VSC, NID_READY, PM_NOREMOVE, SM_DIGITIZER, - SWP_ASYNCWINDOWPOS, SWP_NOACTIVATE, SWP_NOSIZE, SWP_NOZORDER, WM_NCLBUTTONDOWN, - WNDCLASSEXW, + GetSystemMetrics, GetWindowPlacement, GetWindowTextLengthW, GetWindowTextW, + IsWindowVisible, LoadCursorW, PeekMessageW, PostMessageW, RegisterClassExW, SetCursor, + SetCursorPos, SetForegroundWindow, SetWindowPlacement, SetWindowPos, SetWindowTextW, + CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, FLASHWINFO, FLASHW_ALL, FLASHW_STOP, + FLASHW_TIMERNOFG, FLASHW_TRAY, GWLP_HINSTANCE, HTCAPTION, MAPVK_VK_TO_VSC, NID_READY, + PM_NOREMOVE, SM_DIGITIZER, SWP_ASYNCWINDOWPOS, SWP_NOACTIVATE, SWP_NOSIZE, + SWP_NOZORDER, WM_NCLBUTTONDOWN, WNDCLASSEXW, }, }, }; @@ -698,6 +698,14 @@ impl Window { Some(self.window_state_lock().current_theme) } + #[inline] + pub fn title(&self) -> String { + let len = unsafe { GetWindowTextLengthW(self.window.0) } + 1; + let mut buf = vec![0; len as usize]; + unsafe { GetWindowTextW(self.window.0, buf.as_mut_ptr(), len) }; + util::decode_wide(&buf).to_string_lossy().to_string() + } + #[inline] pub fn set_skip_taskbar(&self, skip: bool) { self.window_state_lock().skip_taskbar = skip; diff --git a/src/window.rs b/src/window.rs index 65e652d8..c11be5da 100644 --- a/src/window.rs +++ b/src/window.rs @@ -952,6 +952,16 @@ impl Window { pub fn theme(&self) -> Option { self.window.theme() } + + /// Gets the current title of the window. + /// + /// ## Platform-specific + /// + /// - **iOS / Android / x11 / Wayland / Web:** Unsupported. Always returns an empty string. + #[inline] + pub fn title(&self) -> String { + self.window.title() + } } /// Cursor functions.