Merge pull request #89 from frewsxcv/constraints

Implement min/max window dimension constraints for MacOS.
This commit is contained in:
tomaka 2016-11-23 19:32:35 +01:00 committed by GitHub
commit 79bed75d28
2 changed files with 80 additions and 4 deletions

23
examples/min_max_size.rs Normal file
View file

@ -0,0 +1,23 @@
#[cfg(target_os = "android")]
#[macro_use]
extern crate android_glue;
extern crate winit;
#[cfg(target_os = "android")]
android_start!(main);
fn main() {
let window = winit::WindowBuilder::new()
.with_min_dimensions(400, 200)
.with_max_dimensions(800, 400)
.build()
.unwrap();
for event in window.wait_events() {
match event {
winit::Event::Closed => break,
_ => ()
}
}
}

View file

@ -269,10 +269,6 @@ impl Window {
pl_attribs: &PlatformSpecificWindowBuilderAttributes)
-> Result<Window, CreationError>
{
// not implemented
assert!(win_attribs.min_dimensions.is_none());
assert!(win_attribs.max_dimensions.is_none());
// let app = match Window::create_app() {
let app = match Window::create_app(pl_attribs.activation_policy) {
Some(app) => app,
@ -300,6 +296,14 @@ impl Window {
} else {
window.makeKeyWindow();
}
if let Some((width, height)) = win_attribs.min_dimensions {
nswindow_set_min_dimensions(window.0, width.into(), height.into());
}
if let Some((width, height)) = win_attribs.max_dimensions {
nswindow_set_max_dimensions(window.0, width.into(), height.into());
}
}
let ds = DelegateState {
@ -627,6 +631,55 @@ impl Window {
}
}
unsafe fn nswindow_set_min_dimensions<V: NSWindow + Copy>(
window: V, min_width: f64, min_height: f64)
{
window.setMinSize_(NSSize {
width: min_width,
height: min_height,
});
// If necessary, resize the window to match constraint
let mut current_rect = NSWindow::frame(window);
if current_rect.size.width < min_width {
current_rect.size.width = min_width;
window.setFrame_display_(current_rect, 0)
}
if current_rect.size.height < min_height {
// The origin point of a rectangle is at its bottom left in Cocoa. To
// ensure the window's top-left point remains the same:
current_rect.origin.y +=
current_rect.size.height - min_height;
current_rect.size.height = min_height;
window.setFrame_display_(current_rect, 0)
}
}
unsafe fn nswindow_set_max_dimensions<V: NSWindow + Copy>(
window: V, max_width: f64, max_height: f64)
{
window.setMaxSize_(NSSize {
width: max_width,
height: max_height,
});
// If necessary, resize the window to match constraint
let mut current_rect = NSWindow::frame(window);
if current_rect.size.width > max_width {
current_rect.size.width = max_width;
window.setFrame_display_(current_rect, 0)
}
if current_rect.size.height > max_height {
// The origin point of a rectangle is at its bottom left in
// Cocoa. To ensure the window's top-left point remains the
// same:
current_rect.origin.y +=
current_rect.size.height - max_height;
current_rect.size.height = max_height;
window.setFrame_display_(current_rect, 0)
}
}
struct IdRef(id);
impl IdRef {