Add Window::title getter on Windows and macOS

This commit is contained in:
Amr Bashir 2022-11-03 10:11:37 -07:00 committed by GitHub
parent a7a7cc64cd
commit 08f9e374e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 64 additions and 7 deletions

View file

@ -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`.

View file

@ -856,6 +856,10 @@ impl Window {
pub fn theme(&self) -> Option<Theme> {
None
}
pub fn title(&self) -> String {
String::new()
}
}
#[derive(Default, Clone, Debug)]

View file

@ -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 {

View file

@ -589,6 +589,11 @@ impl Window {
pub fn theme(&self) -> Option<Theme> {
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.

View file

@ -624,6 +624,11 @@ impl Window {
pub fn theme(&self) -> Option<Theme> {
None
}
#[inline]
pub fn title(&self) -> String {
String::new()
}
}
impl Drop for Window {

View file

@ -1551,4 +1551,9 @@ impl UnownedWindow {
pub fn theme(&self) -> Option<Theme> {
None
}
#[inline]
pub fn title(&self) -> String {
String::new()
}
}

View file

@ -143,6 +143,10 @@ extern_methods!(
#[sel(setTitle:)]
pub fn setTitle(&self, title: &NSString);
pub fn title_(&self) -> Id<NSString, Shared> {
unsafe { msg_send_id![self, title] }
}
#[sel(setReleasedWhenClosed:)]
pub fn setReleasedWhenClosed(&self, val: bool);

View file

@ -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 {

View file

@ -378,6 +378,11 @@ impl Window {
pub fn theme(&self) -> Option<Theme> {
None
}
#[inline]
pub fn title(&self) -> String {
String::new()
}
}
impl Drop for Window {

View file

@ -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;

View file

@ -952,6 +952,16 @@ impl Window {
pub fn theme(&self) -> Option<Theme> {
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.