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

fix(windows): revert maximized state handling to winit impl, closes #193 #299

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 .changes/windows-fix-maximized-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Fix using `WindowBuilder::with_visible` and `WindowBuilder::with_maximized` not behaving correctly.
19 changes: 17 additions & 2 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,18 @@ unsafe fn public_window_callback_inner<T: 'static>(
event: Resized(physical_size),
};

{
let mut w = subclass_input.window_state.lock();
// See WindowFlags::MARKER_RETAIN_STATE_ON_SIZE docs for info on why this `if` check exists.
if !w
.window_flags()
.contains(WindowFlags::MARKER_RETAIN_STATE_ON_SIZE)
{
let maximized = wparam.0 == win32wm::SIZE_MAXIMIZED as _;
w.set_window_flags_in_place(|f| f.set(WindowFlags::MAXIMIZED, maximized));
}
}

subclass_input.send_event(event);
result = ProcResult::Value(LRESULT(0));
}
Expand Down Expand Up @@ -1698,7 +1710,8 @@ unsafe fn public_window_callback_inner<T: 'static>(
return;
}

window_state.fullscreen.is_none() && !util::is_maximized(window)
window_state.fullscreen.is_none()
&& !window_state.window_flags().contains(WindowFlags::MAXIMIZED)
};

let style = GetWindowLongW(window, GWL_STYLE) as WINDOW_STYLE;
Expand Down Expand Up @@ -1771,7 +1784,9 @@ unsafe fn public_window_callback_inner<T: 'static>(
.contains(WindowFlags::MARKER_IN_SIZE_MOVE);
// Unset maximized if we're changing the window's size.
if new_physical_inner_size != old_physical_inner_size {
util::set_maximized(window, false);
WindowState::set_window_flags(window_state, window, |f| {
f.set(WindowFlags::MAXIMIZED, false)
});
}
}

Expand Down
14 changes: 0 additions & 14 deletions src/platform_impl/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,6 @@ pub fn is_maximized(window: HWND) -> bool {
placement.showCmd == SW_MAXIMIZE
}

pub fn set_maximized(window: HWND, maximized: bool) {
unsafe {
if IsWindowVisible(window).as_bool() {
ShowWindow(
window,
match maximized {
true => SW_MAXIMIZE,
false => SW_RESTORE,
},
);
}
}
}

pub fn get_hicon_from_buffer(buffer: &[u8], width: i32, height: i32) -> Option<HICON> {
unsafe {
match LookupIconIdFromDirectoryEx(buffer.as_ptr() as _, true, width, height, LR_DEFAULTCOLOR)
Expand Down
26 changes: 20 additions & 6 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,12 @@ impl Window {
pub fn set_outer_position(&self, position: Position) {
let (x, y): (i32, i32) = position.to_physical::<i32>(self.scale_factor()).into();

let window_state = Arc::clone(&self.window_state);
let window = self.window.clone();
self.thread_executor.execute_in_thread(move || {
util::set_maximized(window.0, false);
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
f.set(WindowFlags::MAXIMIZED, false)
});
});

unsafe {
Expand Down Expand Up @@ -246,9 +249,12 @@ impl Window {
let scale_factor = self.scale_factor();
let (width, height) = size.to_physical::<u32>(scale_factor).into();

let window_state = Arc::clone(&self.window_state);
let window = self.window.clone();
self.thread_executor.execute_in_thread(move || {
util::set_maximized(window.0, false);
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
f.set(WindowFlags::MAXIMIZED, false)
});
});

util::set_inner_size_physical(self.window.0, width, height);
Expand Down Expand Up @@ -406,12 +412,20 @@ impl Window {

#[inline]
pub fn set_maximized(&self, maximized: bool) {
util::set_maximized(self.window.0, maximized);
let window = self.window.clone();
let window_state = Arc::clone(&self.window_state);

self.thread_executor.execute_in_thread(move || {
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
f.set(WindowFlags::MAXIMIZED, maximized)
});
});
}

#[inline]
pub fn is_maximized(&self) -> bool {
util::is_maximized(self.window.0)
let window_state = self.window_state.lock();
window_state.window_flags.contains(WindowFlags::MAXIMIZED)
}

#[inline]
Expand Down Expand Up @@ -996,8 +1010,8 @@ unsafe extern "system" fn window_proc(
win32wm::WM_NCCALCSIZE => {
let userdata = util::GetWindowLongPtrW(window, GWL_USERDATA);
if userdata != 0 {
let window_flags = WindowFlags::from_bits_unchecked(userdata as _);
if !window_flags.contains(WindowFlags::DECORATIONS) {
let win_flags = WindowFlags::from_bits_unchecked(userdata as _);
if !win_flags.contains(WindowFlags::DECORATIONS) {
// adjust the maximized borderless window so it doesn't cover the taskbar
if util::is_maximized(window) {
let monitor = monitor::current_monitor(window);
Expand Down
17 changes: 17 additions & 0 deletions src/platform_impl/windows/window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ bitflags! {
const MINIMIZED = 1 << 12;

const EXCLUSIVE_FULLSCREEN_OR_MASK = WindowFlags::ALWAYS_ON_TOP.bits;
const INVISIBLE_AND_MASK = !WindowFlags::MAXIMIZED.bits;
}
}

Expand Down Expand Up @@ -184,6 +185,10 @@ impl WindowFlags {
self |= WindowFlags::EXCLUSIVE_FULLSCREEN_OR_MASK;
}

if !self.contains(WindowFlags::VISIBLE) {
self &= WindowFlags::INVISIBLE_AND_MASK;
}

self
}

Expand Down Expand Up @@ -284,6 +289,18 @@ impl WindowFlags {
}
}

if diff.contains(WindowFlags::MAXIMIZED) || new.contains(WindowFlags::MAXIMIZED) {
unsafe {
ShowWindow(
window,
match new.contains(WindowFlags::MAXIMIZED) {
true => SW_MAXIMIZE,
false => SW_RESTORE,
},
);
}
}

if diff != WindowFlags::empty() {
let (style, style_ex) = new.to_window_styles();

Expand Down