Skip to content

Commit

Permalink
On Windows, keep window maximized when setting size bounds (rust-wind…
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger authored and kchibisov committed Aug 14, 2023
1 parent 416fc45 commit d9e7a64
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
31 changes: 31 additions & 0 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::single_match)]

use simple_logger::SimpleLogger;
use winit::dpi::PhysicalSize;
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::EventLoop;
use winit::keyboard::Key;
Expand All @@ -18,6 +19,8 @@ fn main() -> Result<(), impl std::error::Error> {

let mut decorations = true;
let mut minimized = false;
let mut with_min_size = false;
let mut with_max_size = false;

let window = WindowBuilder::new()
.with_title("Hello world!")
Expand Down Expand Up @@ -46,6 +49,8 @@ fn main() -> Result<(), impl std::error::Error> {
println!("- D\tToggle window decorations");
println!("- X\tMaximize window");
println!("- Z\tMinimize window");
println!("- I\tToggle mIn size limit");
println!("- A\tToggle mAx size limit");

event_loop.run(move |event, elwt, control_flow| {
control_flow.set_wait();
Expand Down Expand Up @@ -120,6 +125,32 @@ fn main() -> Result<(), impl std::error::Error> {
minimized = !minimized;
window.set_minimized(minimized);
}
"i" => {
with_min_size = !with_min_size;
let min_size = if with_min_size {
Some(PhysicalSize::new(100, 100))
} else {
None
};
window.set_min_inner_size(min_size);
eprintln!(
"Min: {with_min_size}: {min_size:?} => {:?}",
window.inner_size()
);
}
"a" => {
with_max_size = !with_max_size;
let max_size = if with_max_size {
Some(PhysicalSize::new(200, 200))
} else {
None
};
window.set_max_inner_size(max_size);
eprintln!(
"Max: {with_max_size}: {max_size:?} => {:?}",
window.inner_size()
);
}
_ => (),
},
_ => (),
Expand Down
21 changes: 12 additions & 9 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,20 @@ impl Window {
let scale_factor = self.scale_factor();
let physical_size = size.to_physical::<u32>(scale_factor);

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

let window_flags = self.window_state_lock().window_flags;
window_flags.set_size(self.hwnd(), physical_size);

if physical_size != self.inner_size() {
let window_state = Arc::clone(&self.window_state);
let window = self.window.clone();
self.thread_executor.execute_in_thread(move || {
let _ = &window;
WindowState::set_window_flags(window_state.lock().unwrap(), window.0, |f| {
f.set(WindowFlags::MAXIMIZED, false)
});
});
}

None
}

Expand Down

0 comments on commit d9e7a64

Please sign in to comment.