More Box work. Secondary dimension stretchiness works fine.

This commit is contained in:
Pietro Gagliardi 2015-08-08 19:32:24 -04:00
parent b2f5254fe2
commit c76584c3d2
3 changed files with 49 additions and 32 deletions

View File

@ -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()
}

View File

@ -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)
}

View File

@ -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)
}