Skip to content

Commit 6b7ceed

Browse files
authored
Windows: respect min/max sizes when creating the window (#2393)
1 parent c53a574 commit 6b7ceed

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ And please only add new entries to the top of this list, right below the `# Unre
88

99
# Unreleased
1010

11+
- On Windows, respect min/max inner sizes when creating the window.
12+
1113
# 0.27.1 (2022-07-30)
1214

1315
- The minimum supported Rust version was lowered to `1.57.0` and now explicitly tested.

src/dpi.rs

+23
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,29 @@ impl Size {
511511
Size::Logical(size) => size.to_physical(scale_factor),
512512
}
513513
}
514+
515+
pub fn clamp<S: Into<Size>>(input: S, min: S, max: S, scale_factor: f64) -> Size {
516+
let (input, min, max) = (
517+
input.into().to_physical::<f64>(scale_factor),
518+
min.into().to_physical::<f64>(scale_factor),
519+
max.into().to_physical::<f64>(scale_factor),
520+
);
521+
522+
let clamp = |input: f64, min: f64, max: f64| {
523+
if input < min {
524+
min
525+
} else if input > max {
526+
max
527+
} else {
528+
input
529+
}
530+
};
531+
532+
let width = clamp(input.width, min.width, max.width);
533+
let height = clamp(input.height, min.height, max.height);
534+
535+
PhysicalSize::new(width, height).into()
536+
}
514537
}
515538

516539
impl<P: Pixel> From<PhysicalSize<P>> for Size {

src/platform_impl/windows/window.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -879,10 +879,17 @@ impl<'a, T: 'static> InitData<'a, T> {
879879
win.set_fullscreen(attributes.fullscreen);
880880
force_window_active(win.window.0);
881881
} else {
882-
let dimensions = attributes
882+
let size = attributes
883883
.inner_size
884884
.unwrap_or_else(|| PhysicalSize::new(800, 600).into());
885-
win.set_inner_size(dimensions);
885+
let max_size = attributes
886+
.max_inner_size
887+
.unwrap_or_else(|| PhysicalSize::new(f64::MAX, f64::MAX).into());
888+
let min_size = attributes
889+
.min_inner_size
890+
.unwrap_or_else(|| PhysicalSize::new(0, 0).into());
891+
let clamped_size = Size::clamp(size, min_size, max_size, win.scale_factor());
892+
win.set_inner_size(clamped_size);
886893

887894
if attributes.maximized {
888895
// Need to set MAXIMIZED after setting `inner_size` as

0 commit comments

Comments
 (0)