diff --git a/redo/osxaltest/box.swift b/redo/osxaltest/box.swift index 5a6fd590..f39b511b 100644 --- a/redo/osxaltest/box.swift +++ b/redo/osxaltest/box.swift @@ -16,6 +16,8 @@ class Box : NSView, Control { private var primaryDirPrefix: String private var secondaryDirPrefix: String + private var primaryOrientation: NSLayoutConstraintOrientation + private var secondaryOrientation: NSLayoutConstraintOrientation init(vertical: Bool, padded: Bool) { self.controls = [] @@ -25,9 +27,13 @@ class Box : NSView, Control { self.primaryDirPrefix = "H:" self.secondaryDirPrefix = "V:" + self.primaryOrientation = NSLayoutConstraintOrientation.Horizontal + self.secondaryOrientation = NSLayoutConstraintOrientation.Vertical if self.vertical { self.primaryDirPrefix = "V:" self.secondaryDirPrefix = "H:" + self.primaryOrientation = NSLayoutConstraintOrientation.Vertical + self.secondaryOrientation = NSLayoutConstraintOrientation.Horizontal } super.init(frame: NSZeroRect) @@ -44,12 +50,13 @@ class Box : NSView, Control { var view = control.View() c = BoxControl( c: control, - stretchy: stretchy + stretchy: stretchy, horzHuggingPri: horzHuggingPri(view), vertHuggingPri: vertHuggingPri(view)) self.addSubview(view) self.controls.append(c) - // TODO set secondary hugging priority to low + // make sure controls don't hug their secondary direction so they fill the width of the view + setHuggingPri(view, myNSLayoutPriorityDefaultLow, self.secondaryOrientation) self.relayout() } diff --git a/redo/osxaltest/control.swift b/redo/osxaltest/control.swift index a92fe300..e06499fc 100644 --- a/redo/osxaltest/control.swift +++ b/redo/osxaltest/control.swift @@ -5,33 +5,3 @@ protocol Control : class { func View() -> NSView func SetParent(p: Control) } - -// TODO move to layout.swift - -func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] { - return NSLayoutConstraint.constraintsWithVisualFormat( - constraint, - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views) -} - -func horzHuggingPri(view: NSView) -> NSLayoutPriority { - return view.contentHuggingPriorityForOrientation(NSLayoutConstraintOrientation.Horizontal) -} - -func vertHuggingPri(view: NSView) -> NSLayoutPriority { - return view.contentHuggingPriorityForOrientation(NSLayoutConstraintOrientation.Vertical) -} - -func setHuggingPri(view: NSView, priority: NSLayoutPriority, orientation: NSLayoutConstraintOrientation) { - view.setContentHuggingPriority(priority, forOrientation: orientation) -} - -func setHorzHuggingPri(view: NSView, priority: NSLayoutPriority) { - setHuggingPri(view, priority, NSLayoutConstraintOrientation.Horizontal) -} - -func setVertHuggingPri(view: NSView, priority: NSLayoutPriority) { - setHuggingPri(view, priority, NSLayoutConstraintOrientation.Vertical) -} diff --git a/redo/osxaltest/layout.swift b/redo/osxaltest/layout.swift new file mode 100644 index 00000000..0f9818a5 --- /dev/null +++ b/redo/osxaltest/layout.swift @@ -0,0 +1,40 @@ +// 8 august 2015 +import Cocoa + +func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] { + return NSLayoutConstraint.constraintsWithVisualFormat( + constraint, + options: NSLayoutFormatOptions(0), + metrics: nil, + views: views) +} + +// the swift bridge isn't perfect; it won't recognize these properly +// thanks to Eridius in freenode/#swift-lang +let myNSLayoutPriorityRequired: NSLayoutPriority = 1000 +let myNSLayoutPriorityDefaultHigh: NSLayoutPriority = 750 +let myNSLayoutPriorityDragThatCanResizeWindow: NSLayoutPriority = 510 +let myNSLayoutPriorityWindowSizeStayPut: NSLayoutPriority = 500 +let myNSLayoutPriorityDragThatCannotResizeWindow: NSLayoutPriority = 490 +let myNSLayoutPriorityDefaultLow: NSLayoutPriority = 250 +let myNSLayoutPriorityFittingSizeCompression: NSLayoutPriority = 50 + +func horzHuggingPri(view: NSView) -> NSLayoutPriority { + return view.contentHuggingPriorityForOrientation(NSLayoutConstraintOrientation.Horizontal) +} + +func vertHuggingPri(view: NSView) -> NSLayoutPriority { + return view.contentHuggingPriorityForOrientation(NSLayoutConstraintOrientation.Vertical) +} + +func setHuggingPri(view: NSView, priority: NSLayoutPriority, orientation: NSLayoutConstraintOrientation) { + view.setContentHuggingPriority(priority, forOrientation: orientation) +} + +func setHorzHuggingPri(view: NSView, priority: NSLayoutPriority) { + setHuggingPri(view, priority, NSLayoutConstraintOrientation.Horizontal) +} + +func setVertHuggingPri(view: NSView, priority: NSLayoutPriority) { + setHuggingPri(view, priority, NSLayoutConstraintOrientation.Vertical) +}