diff --git a/Cargo.toml b/Cargo.toml index 16221eb6..02eb93ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ objc = "0.2" [target.'cfg(target_os = "macos")'.dependencies] objc = "0.2" cgl = "0.1" -cocoa = "0.5.1" +cocoa = "0.5.0" core-foundation = "0" core-graphics = "0.4" diff --git a/src/platform/macos/mod.rs b/src/platform/macos/mod.rs index 68d2a35a..8687197e 100644 --- a/src/platform/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -13,8 +13,8 @@ use objc::declare::ClassDecl; use cocoa::base::{id, nil}; use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize, - NSString, NSUInteger, NSArray}; -use cocoa::appkit::{self, NSApplication, NSEvent, NSView, NSWindow, NSLayoutConstraint, NSLayoutDimension}; + NSString, NSUInteger}; +use cocoa::appkit::{self, NSApplication, NSEvent, NSView, NSWindow}; use core_graphics::display::{CGAssociateMouseAndMouseCursorPosition, CGMainDisplayID, CGDisplayPixelsHigh, CGWarpMouseCursorPosition}; @@ -297,22 +297,12 @@ impl Window { window.makeKeyWindow(); } - let mut constraints = vec![]; if let Some((width, height)) = win_attribs.min_dimensions { - constraints.extend_from_slice(&[ - view.widthAnchor().constraintGreaterThanOrEqualToConstant(width.into()), - view.heightAnchor().constraintGreaterThanOrEqualToConstant(height.into()), - ]); + nswindow_set_min_dimensions(window.0, width.into(), height.into()); } + if let Some((width, height)) = win_attribs.max_dimensions { - constraints.extend_from_slice(&[ - view.widthAnchor().constraintLessThanOrEqualToConstant(width.into()), - view.heightAnchor().constraintLessThanOrEqualToConstant(height.into()), - ]); - } - if !constraints.is_empty() { - let constraints_nsarray = NSArray::arrayWithObjects(nil, &constraints); - NSLayoutConstraint::activateConstraints(nil, constraints_nsarray); + nswindow_set_max_dimensions(window.0, width.into(), height.into()); } } @@ -641,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 {