diff --git a/examples/min_max_size.rs b/examples/min_max_size.rs new file mode 100644 index 00000000..7518bf0d --- /dev/null +++ b/examples/min_max_size.rs @@ -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, + _ => () + } + } +} diff --git a/src/platform/macos/mod.rs b/src/platform/macos/mod.rs index 2ed0e895..8687197e 100644 --- a/src/platform/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -269,10 +269,6 @@ impl Window { pl_attribs: &PlatformSpecificWindowBuilderAttributes) -> Result { - // 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( + 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( + 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 {