2015-08-06 22:10:50 -05:00
|
|
|
// 1 august 2015
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
// auto layout helpers
|
|
|
|
func tIsAmbiguous(view: NSView, indent: Int) {
|
|
|
|
var s = string(indent, ' ')
|
2015-08-07 00:28:26 -05:00
|
|
|
debugPrint("\(s) \(view.className) \(view.hasAmbiguousLayout)")
|
2015-08-06 22:10:50 -05:00
|
|
|
if view.hasAmbiguousLayout {
|
|
|
|
view.window.visualizeConstraints(view.superview.constraints)
|
|
|
|
}
|
|
|
|
for subview in view.subviews {
|
|
|
|
tIsAmbiguous(subview, indent + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class tWindow : tControl {
|
2015-08-07 01:06:18 -05:00
|
|
|
private var w: NSWindow
|
|
|
|
private var c: tControl
|
|
|
|
private var margined: Bool
|
2015-08-06 22:10:50 -05:00
|
|
|
|
|
|
|
init() {
|
|
|
|
self.w = NSWindow(
|
|
|
|
contentRect: NSMakeRect(0, 0, 320, 240),
|
|
|
|
styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask),
|
|
|
|
backing: NSBackingStoreBuffered,
|
|
|
|
defer: true)
|
|
|
|
self.w.title = "Auto Layout Test"
|
2015-08-07 00:28:26 -05:00
|
|
|
self.c = nil
|
|
|
|
self.margined = false
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public func tSetControl(c: tControl) {
|
|
|
|
self.c = c
|
2015-08-07 00:11:55 -05:00
|
|
|
self.c.tSetParent(self, addToView: self.w.contentView)
|
2015-08-06 22:10:50 -05:00
|
|
|
self.tRelayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func tSetMargined(m: Bool) {
|
|
|
|
self.margined = m
|
|
|
|
self.tRelayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func tShow() {
|
|
|
|
self.w.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
|
|
|
|
self.w.makeKeyAndOrderFront(self)
|
|
|
|
tIsAmbiguous(self.w.contentView, 0)
|
|
|
|
}
|
|
|
|
|
2015-08-07 00:28:26 -05:00
|
|
|
func tSetParent(p: tControl, addToView: NSView) {
|
|
|
|
fatalError("cannot call tWindow.tSetParent()")
|
|
|
|
}
|
|
|
|
|
|
|
|
func tFillAutoLayout(p: tAutoLayoutParams) {
|
|
|
|
fatalError("cannot call tWindow.tFillAutoLayout()")
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:10:50 -05:00
|
|
|
public func tRelayout() {
|
|
|
|
if self.c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var contentView = self.w.contentView
|
|
|
|
contentView.removeConstraints(contentView.constraints)
|
|
|
|
|
|
|
|
var p = tAutoLayoutParams()
|
|
|
|
self.c.tFillAutoLayout(p)
|
|
|
|
|
|
|
|
var views = [
|
|
|
|
"view": p.view,
|
|
|
|
]
|
|
|
|
var margin = ""
|
|
|
|
if self.margined {
|
|
|
|
margin = "-"
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO always append margins even if not attached?
|
|
|
|
// or if not attached, append ->=0- as well?
|
|
|
|
var constraint = "H:"
|
|
|
|
if p.attachLeft {
|
|
|
|
constraint += "|" + margin
|
|
|
|
}
|
|
|
|
constraint += "[view]"
|
|
|
|
if p.attachRight {
|
|
|
|
constraint += margin + "|"
|
|
|
|
}
|
2015-08-07 09:46:34 -05:00
|
|
|
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
2015-08-06 22:10:50 -05:00
|
|
|
visualFormat:constraint,
|
|
|
|
options:0,
|
|
|
|
metrics:nil,
|
|
|
|
views:views)
|
|
|
|
contentView.addConstraints(constraints)
|
|
|
|
|
|
|
|
constraint = "V:"
|
|
|
|
if p.attachTop {
|
|
|
|
constraint += "|" + margin
|
|
|
|
}
|
|
|
|
constraint += "[view]"
|
|
|
|
if p.attachBottom {
|
|
|
|
constraint += margin + "|"
|
|
|
|
}
|
2015-08-07 09:46:34 -05:00
|
|
|
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
2015-08-06 22:10:50 -05:00
|
|
|
visualFormat:constraint,
|
|
|
|
options:0,
|
|
|
|
metrics:nil,
|
|
|
|
views:views)
|
|
|
|
contentView.addConstraints(constraints)
|
|
|
|
}
|
|
|
|
}
|