Add Window::is_minimized

Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
Co-authored-by: Markus Siglreithmaier <m.siglreith@gmail.com>
This commit is contained in:
Amr Bashir 2023-01-19 23:39:04 +02:00 committed by GitHub
parent de782504ab
commit 809162fbd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 83 additions and 11 deletions

View file

@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre
# Unreleased # Unreleased
- Add `Window::is_minimized`.
- On X11, fix errors handled during `register_xlib_error_hook` invocation bleeding into winit. - On X11, fix errors handled during `register_xlib_error_hook` invocation bleeding into winit.
- Add `Window::has_focus`. - Add `Window::has_focus`.
- On Windows, fix `Window::set_minimized(false)` not working for windows minimized by `Win + D` hotkey. - On Windows, fix `Window::set_minimized(false)` not working for windows minimized by `Win + D` hotkey.

View file

@ -974,6 +974,10 @@ impl Window {
pub fn set_minimized(&self, _minimized: bool) {} pub fn set_minimized(&self, _minimized: bool) {}
pub fn is_minimized(&self) -> Option<bool> {
None
}
pub fn set_maximized(&self, _maximized: bool) {} pub fn set_maximized(&self, _maximized: bool) {}
pub fn is_maximized(&self) -> bool { pub fn is_maximized(&self) -> bool {

View file

@ -216,6 +216,11 @@ impl Inner {
warn!("`Window::set_minimized` is ignored on iOS") warn!("`Window::set_minimized` is ignored on iOS")
} }
pub fn is_minimized(&self) -> Option<bool> {
warn!("`Window::is_minimized` is ignored on iOS");
None
}
pub fn set_maximized(&self, _maximized: bool) { pub fn set_maximized(&self, _maximized: bool) {
warn!("`Window::set_maximized` is ignored on iOS") warn!("`Window::set_maximized` is ignored on iOS")
} }

View file

@ -464,6 +464,11 @@ impl Window {
x11_or_wayland!(match self; Window(w) => w.set_minimized(minimized)) x11_or_wayland!(match self; Window(w) => w.set_minimized(minimized))
} }
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
x11_or_wayland!(match self; Window(w) => w.is_minimized())
}
#[inline] #[inline]
pub(crate) fn fullscreen(&self) -> Option<Fullscreen> { pub(crate) fn fullscreen(&self) -> Option<Fullscreen> {
x11_or_wayland!(match self; Window(w) => w.fullscreen()) x11_or_wayland!(match self; Window(w) => w.fullscreen())

View file

@ -483,6 +483,11 @@ impl Window {
self.decorated.load(Ordering::Relaxed) self.decorated.load(Ordering::Relaxed)
} }
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
None
}
#[inline] #[inline]
pub fn set_minimized(&self, minimized: bool) { pub fn set_minimized(&self, minimized: bool) {
// You can't unminimize the window on Wayland. // You can't unminimize the window on Wayland.

View file

@ -802,6 +802,20 @@ impl UnownedWindow {
self.xconn.primary_monitor() self.xconn.primary_monitor()
} }
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
let state_atom = unsafe { self.xconn.get_atom_unchecked(b"_NET_WM_STATE\0") };
let state = self
.xconn
.get_property(self.xwindow, state_atom, ffi::XA_ATOM);
let hidden_atom = unsafe { self.xconn.get_atom_unchecked(b"_NET_WM_STATE_HIDDEN\0") };
Some(match state {
Ok(atoms) => atoms.iter().any(|atom: &ffi::Atom| *atom == hidden_atom),
_ => false,
})
}
fn set_minimized_inner(&self, minimized: bool) -> util::Flusher<'_> { fn set_minimized_inner(&self, minimized: bool) -> util::Flusher<'_> {
unsafe { unsafe {
if minimized { if minimized {

View file

@ -864,6 +864,11 @@ impl WinitWindow {
} }
} }
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
Some(self.isMiniaturized())
}
#[inline] #[inline]
pub fn set_maximized(&self, maximized: bool) { pub fn set_maximized(&self, maximized: bool) {
let is_zoomed = self.is_zoomed(); let is_zoomed = self.is_zoomed();

View file

@ -276,6 +276,11 @@ impl Window {
#[inline] #[inline]
pub fn set_minimized(&self, _minimized: bool) {} pub fn set_minimized(&self, _minimized: bool) {}
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
None
}
#[inline] #[inline]
pub fn set_maximized(&self, _maximized: bool) {} pub fn set_maximized(&self, _maximized: bool) {}

View file

@ -287,6 +287,12 @@ impl Window {
// Intentionally a no-op, as canvases cannot be 'minimized' // Intentionally a no-op, as canvases cannot be 'minimized'
} }
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
// Canvas cannot be 'minimized'
Some(false)
}
#[inline] #[inline]
pub fn set_maximized(&self, _maximized: bool) { pub fn set_maximized(&self, _maximized: bool) {
// Intentionally a no-op, as canvases cannot be 'maximized' // Intentionally a no-op, as canvases cannot be 'maximized'

View file

@ -24,9 +24,10 @@ use windows_sys::{
Input::KeyboardAndMouse::GetActiveWindow, Input::KeyboardAndMouse::GetActiveWindow,
WindowsAndMessaging::{ WindowsAndMessaging::{
ClipCursor, GetClientRect, GetClipCursor, GetSystemMetrics, GetWindowRect, ClipCursor, GetClientRect, GetClipCursor, GetSystemMetrics, GetWindowRect,
ShowCursor, IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, IDC_IBEAM, IsIconic, ShowCursor, IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP,
IDC_NO, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_WAIT, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE,
SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN, IDC_WAIT, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN,
SM_YVIRTUALSCREEN,
}, },
}, },
}, },
@ -135,6 +136,10 @@ pub fn is_focused(window: HWND) -> bool {
window == unsafe { GetActiveWindow() } window == unsafe { GetActiveWindow() }
} }
pub fn is_minimized(window: HWND) -> bool {
unsafe { IsIconic(window) != false.into() }
}
pub fn get_instance_handle() -> HINSTANCE { pub fn get_instance_handle() -> HINSTANCE {
// Gets the instance handle by taking the address of the // Gets the instance handle by taking the address of the
// pseudo-variable created by the microsoft linker: // pseudo-variable created by the microsoft linker:

View file

@ -40,7 +40,7 @@ use windows_sys::Win32::{
}, },
WindowsAndMessaging::{ WindowsAndMessaging::{
CreateWindowExW, FlashWindowEx, GetClientRect, GetCursorPos, GetForegroundWindow, CreateWindowExW, FlashWindowEx, GetClientRect, GetCursorPos, GetForegroundWindow,
GetSystemMetrics, GetWindowPlacement, GetWindowTextLengthW, GetWindowTextW, IsIconic, GetSystemMetrics, GetWindowPlacement, GetWindowTextLengthW, GetWindowTextW,
IsWindowVisible, LoadCursorW, PeekMessageW, PostMessageW, RegisterClassExW, SetCursor, IsWindowVisible, LoadCursorW, PeekMessageW, PostMessageW, RegisterClassExW, SetCursor,
SetCursorPos, SetForegroundWindow, SetWindowDisplayAffinity, SetWindowPlacement, SetCursorPos, SetForegroundWindow, SetWindowDisplayAffinity, SetWindowPlacement,
SetWindowPos, SetWindowTextW, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, FLASHWINFO, SetWindowPos, SetWindowTextW, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, FLASHWINFO,
@ -462,7 +462,7 @@ impl Window {
let window = self.window.clone(); let window = self.window.clone();
let window_state = Arc::clone(&self.window_state); let window_state = Arc::clone(&self.window_state);
let is_minimized = self.is_minimized(); let is_minimized = util::is_minimized(self.hwnd());
self.thread_executor.execute_in_thread(move || { self.thread_executor.execute_in_thread(move || {
let _ = &window; let _ = &window;
@ -475,6 +475,11 @@ impl Window {
}); });
} }
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
Some(util::is_minimized(self.hwnd()))
}
#[inline] #[inline]
pub fn set_maximized(&self, maximized: bool) { pub fn set_maximized(&self, maximized: bool) {
let window = self.window.clone(); let window = self.window.clone();
@ -494,11 +499,6 @@ impl Window {
window_state.window_flags.contains(WindowFlags::MAXIMIZED) window_state.window_flags.contains(WindowFlags::MAXIMIZED)
} }
#[inline]
pub fn is_minimized(&self) -> bool {
unsafe { IsIconic(self.hwnd()) == 1 }
}
#[inline] #[inline]
pub fn fullscreen(&self) -> Option<Fullscreen> { pub fn fullscreen(&self) -> Option<Fullscreen> {
let window_state = self.window_state_lock(); let window_state = self.window_state_lock();
@ -810,7 +810,7 @@ impl Window {
let window_flags = self.window_state_lock().window_flags(); let window_flags = self.window_state_lock().window_flags();
let is_visible = window_flags.contains(WindowFlags::VISIBLE); let is_visible = window_flags.contains(WindowFlags::VISIBLE);
let is_minimized = window_flags.contains(WindowFlags::MINIMIZED); let is_minimized = util::is_minimized(self.hwnd());
let is_foreground = window.0 == unsafe { GetForegroundWindow() }; let is_foreground = window.0 == unsafe { GetForegroundWindow() };
if is_visible && !is_minimized && !is_foreground { if is_visible && !is_minimized && !is_foreground {

View file

@ -842,6 +842,23 @@ impl Window {
self.window.set_minimized(minimized); self.window.set_minimized(minimized);
} }
/// Gets the window's current minimized state.
///
/// `None` will be returned, if the minimized state couldn't be determined.
///
/// ## Note
///
/// - You shouldn't stop rendering for minimized windows, however you could lower the fps.
///
/// ## Platform-specific
///
/// - **Wayland**: always `None`.
/// - **iOS / Android / Web / Orbital:** Unsupported.
#[inline]
pub fn is_minimized(&self) -> Option<bool> {
self.window.is_minimized()
}
/// Sets the window to maximized or back. /// Sets the window to maximized or back.
/// ///
/// ## Platform-specific /// ## Platform-specific