Skip to content

Commit

Permalink
fix(windows): respect min/max sizes when creating window, closes #498 (
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Jul 25, 2022
1 parent b86ada7 commit c1c6822
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/windows-initial-window-min-max-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

On Windows, respect min/max inner sizes when creating the window.
23 changes: 23 additions & 0 deletions src/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,29 @@ impl Size {
Size::Logical(size) => size.to_physical(scale_factor),
}
}

pub fn clamp<S: Into<Size>>(input: S, min: S, max: S, scale_factor: f64) -> Size {
let (input, min, max) = (
input.into().to_physical::<f64>(scale_factor),
min.into().to_physical::<f64>(scale_factor),
max.into().to_physical::<f64>(scale_factor),
);

let clamp = |input: f64, min: f64, max: f64| {
if input < min {
min
} else if input > max {
max
} else {
input
}
};

let width = clamp(input.width, min.width, max.width);
let height = clamp(input.height, min.height, max.height);

PhysicalSize::new(width, height).into()
}
}

impl<P: Pixel> From<PhysicalSize<P>> for Size {
Expand Down
31 changes: 20 additions & 11 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,22 +941,31 @@ unsafe fn init<T: 'static>(

win.set_skip_taskbar(pl_attribs.skip_taskbar);

let dimensions = attributes
.inner_size
.unwrap_or_else(|| PhysicalSize::new(800, 600).into());
win.set_inner_size(dimensions);
if attributes.maximized {
// Need to set MAXIMIZED after setting `inner_size` as
// `Window::set_inner_size` changes MAXIMIZED to false.
win.set_maximized(true);
}
win.set_visible(attributes.visible);

if attributes.fullscreen.is_some() {
win.set_fullscreen(attributes.fullscreen);
force_window_active(win.window.0);
} else {
let size = attributes
.inner_size
.unwrap_or_else(|| PhysicalSize::new(800, 600).into());
let max_size = attributes
.max_inner_size
.unwrap_or_else(|| PhysicalSize::new(f64::MAX, f64::MAX).into());
let min_size = attributes
.min_inner_size
.unwrap_or_else(|| PhysicalSize::new(0, 0).into());
let clamped_size = Size::clamp(size, min_size, max_size, win.scale_factor());
win.set_inner_size(clamped_size);

if attributes.maximized {
// Need to set MAXIMIZED after setting `inner_size` as
// `Window::set_inner_size` changes MAXIMIZED to false.
win.set_maximized(true);
}
}

win.set_visible(attributes.visible);

if let Some(position) = attributes.position {
win.set_outer_position(position);
}
Expand Down

0 comments on commit c1c6822

Please sign in to comment.