Update min/max window constraints to support MacOS 10.10.

This commit is contained in:
Corey Farwell 2016-11-23 10:50:12 -05:00
parent f8666a05fb
commit 57d1e162ee
2 changed files with 55 additions and 16 deletions

View file

@ -26,7 +26,7 @@ objc = "0.2"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2" objc = "0.2"
cgl = "0.1" cgl = "0.1"
cocoa = "0.5.1" cocoa = "0.5.0"
core-foundation = "0" core-foundation = "0"
core-graphics = "0.4" core-graphics = "0.4"

View file

@ -13,8 +13,8 @@ use objc::declare::ClassDecl;
use cocoa::base::{id, nil}; use cocoa::base::{id, nil};
use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize, use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize,
NSString, NSUInteger, NSArray}; NSString, NSUInteger};
use cocoa::appkit::{self, NSApplication, NSEvent, NSView, NSWindow, NSLayoutConstraint, NSLayoutDimension}; use cocoa::appkit::{self, NSApplication, NSEvent, NSView, NSWindow};
use core_graphics::display::{CGAssociateMouseAndMouseCursorPosition, CGMainDisplayID, CGDisplayPixelsHigh, CGWarpMouseCursorPosition}; use core_graphics::display::{CGAssociateMouseAndMouseCursorPosition, CGMainDisplayID, CGDisplayPixelsHigh, CGWarpMouseCursorPosition};
@ -297,22 +297,12 @@ impl Window {
window.makeKeyWindow(); window.makeKeyWindow();
} }
let mut constraints = vec![];
if let Some((width, height)) = win_attribs.min_dimensions { if let Some((width, height)) = win_attribs.min_dimensions {
constraints.extend_from_slice(&[ nswindow_set_min_dimensions(window.0, width.into(), height.into());
view.widthAnchor().constraintGreaterThanOrEqualToConstant(width.into()),
view.heightAnchor().constraintGreaterThanOrEqualToConstant(height.into()),
]);
} }
if let Some((width, height)) = win_attribs.max_dimensions { if let Some((width, height)) = win_attribs.max_dimensions {
constraints.extend_from_slice(&[ nswindow_set_max_dimensions(window.0, width.into(), height.into());
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);
} }
} }
@ -641,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); struct IdRef(id);
impl IdRef { impl IdRef {