Wrote some preparation for stretchiness.

This commit is contained in:
Pietro Gagliardi 2015-08-08 11:48:35 -04:00
parent 240dc25d74
commit b2f5254fe2
2 changed files with 31 additions and 3 deletions

View File

@ -4,6 +4,8 @@ import Cocoa
struct BoxControl { struct BoxControl {
var c: Control var c: Control
var stretchy: Bool var stretchy: Bool
var horzHuggingPri: NSLayoutPriority
var vertHuggingPri: NSLayoutPriority
} }
class Box : NSView, Control { class Box : NSView, Control {
@ -39,11 +41,15 @@ class Box : NSView, Control {
func Add(control: Control, _ stretchy: Bool) { func Add(control: Control, _ stretchy: Bool) {
var c: BoxControl var c: BoxControl
var view = control.View()
c = BoxControl( c = BoxControl(
c: control, c: control,
stretchy: stretchy) stretchy: stretchy
self.addSubview(c.c.View()) horzHuggingPri: horzHuggingPri(view),
vertHuggingPri: vertHuggingPri(view))
self.addSubview(view)
self.controls.append(c) self.controls.append(c)
// TODO set secondary hugging priority to low
self.relayout() self.relayout()
} }

View File

@ -6,6 +6,8 @@ protocol Control : class {
func SetParent(p: Control) func SetParent(p: Control)
} }
// TODO move to layout.swift
func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] { func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] {
return NSLayoutConstraint.constraintsWithVisualFormat( return NSLayoutConstraint.constraintsWithVisualFormat(
constraint, constraint,
@ -13,3 +15,23 @@ func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] {
metrics: nil, metrics: nil,
views: views) 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)
}