Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Fix inner_size() and inner_position() of minimized window #2176

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,15 @@ unsafe fn public_window_callback_inner<T: 'static>(
winuser::WM_SYSCOMMAND => {
if wparam == winuser::SC_RESTORE {
let mut w = userdata.window_state.lock();
w.saved_inner_rect = None;
kazzix14 marked this conversation as resolved.
Show resolved Hide resolved
w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, false));
}
if wparam == winuser::SC_MINIMIZE {
let mut w = userdata.window_state.lock();
w.saved_inner_rect = Some(
util::get_client_rect(&mut *window)
.expect("Unexpected GetClientRct failure: please report this error to https://github.com/rust-windowing/winit")
);
w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, true));
}
// Send `WindowEvent::Minimized` here if we decide to implement one
Expand Down
23 changes: 18 additions & 5 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use winapi::{
ctypes::c_int,
shared::{
minwindef::{HINSTANCE, LPARAM, UINT, WPARAM},
windef::{HWND, POINT, POINTS, RECT},
windef::{HWND, POINT, POINTS},
winerror::SUCCEEDED,
},
um::{
Expand Down Expand Up @@ -118,6 +118,10 @@ impl Window {

#[inline]
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
if let Some(rect) = self.window_state.lock().saved_inner_rect {
return Ok(PhysicalPosition::new(rect.left as i32, rect.top as i32));
}

let mut position: POINT = unsafe { mem::zeroed() };
if unsafe { winuser::ClientToScreen(self.window.0, &mut position) } == 0 {
panic!("Unexpected ClientToScreen failure: please report this error to https://github.com/rust-windowing/winit")
Expand Down Expand Up @@ -157,10 +161,12 @@ impl Window {

#[inline]
pub fn inner_size(&self) -> PhysicalSize<u32> {
let mut rect: RECT = unsafe { mem::zeroed() };
if unsafe { winuser::GetClientRect(self.window.0, &mut rect) } == 0 {
panic!("Unexpected GetClientRect failure: please report this error to https://github.com/rust-windowing/winit")
}
let rect = self.window_state.lock()
.saved_inner_rect
.unwrap_or_else( ||
util::get_client_rect(self.window.0).expect("Unexpected GetClientRct failure: please report this error to https://github.com/rust-windowing/winit")
kazzix14 marked this conversation as resolved.
Show resolved Hide resolved
);

PhysicalSize::new(
(rect.right - rect.left) as u32,
(rect.bottom - rect.top) as u32,
Expand Down Expand Up @@ -347,6 +353,13 @@ impl Window {

self.thread_executor.execute_in_thread(move || {
let _ = &window;

window_state.lock().saved_inner_rect = if minimized {
Some(util::get_client_rect(window.0).expect("Unexpected GetClientRect failure: please report this error to https://github.com/rust-windowing/winit"))
} else {
None
};

WindowState::set_window_flags(window_state.lock(), window.0, |f| {
f.set(WindowFlags::MINIMIZED, minimized)
});
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/windows/window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct WindowState {
pub taskbar_icon: Option<Icon>,

pub saved_window: Option<SavedWindow>,
pub saved_inner_rect: Option<RECT>,
pub scale_factor: f64,

pub modifiers_state: ModifiersState,
Expand Down Expand Up @@ -115,6 +116,7 @@ impl WindowState {
taskbar_icon,

saved_window: None,
saved_inner_rect: None,
scale_factor,

modifiers_state: ModifiersState::default(),
Expand Down