2015-08-06 22:10:50 -05:00
|
|
|
// 1 august 2015
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
// auto layout helpers
|
|
|
|
func tIsAmbiguous(view: NSView, indent: Int) {
|
2015-08-07 13:45:01 -05:00
|
|
|
var s = String(count: indent, repeatedValue: " " as Character)
|
2015-08-07 15:24:15 -05:00
|
|
|
println("\(s) \(view.className) \(view.hasAmbiguousLayout)")
|
2015-08-06 22:10:50 -05:00
|
|
|
if view.hasAmbiguousLayout {
|
2015-08-07 13:45:01 -05:00
|
|
|
view.window?.visualizeConstraints(view.superview!.constraints)
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
for subview in view.subviews {
|
2015-08-07 13:45:01 -05:00
|
|
|
tIsAmbiguous(subview as! NSView, indent + 1)
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class tWindow : tControl {
|
2015-08-07 01:06:18 -05:00
|
|
|
private var w: NSWindow
|
2015-08-07 13:45:01 -05:00
|
|
|
private var c: tControl?
|
2015-08-07 01:06:18 -05:00
|
|
|
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),
|
2015-08-07 13:45:01 -05:00
|
|
|
backing: NSBackingStoreType.Buffered,
|
2015-08-06 22:10:50 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-08-07 13:45:01 -05:00
|
|
|
func tSetControl(c: tControl) {
|
2015-08-06 22:10:50 -05:00
|
|
|
self.c = c
|
2015-08-07 13:45:01 -05:00
|
|
|
// TODO use self.c here
|
|
|
|
c.tSetParent(self, addToView: self.w.contentView as! NSView)
|
2015-08-06 22:10:50 -05:00
|
|
|
self.tRelayout()
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:45:01 -05:00
|
|
|
func tSetMargined(m: Bool) {
|
2015-08-06 22:10:50 -05:00
|
|
|
self.margined = m
|
|
|
|
self.tRelayout()
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:45:01 -05:00
|
|
|
func tShow() {
|
2015-08-06 22:10:50 -05:00
|
|
|
self.w.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
|
|
|
|
self.w.makeKeyAndOrderFront(self)
|
2015-08-07 13:45:01 -05:00
|
|
|
tIsAmbiguous(self.w.contentView as! NSView, 0)
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
|
2015-08-07 00:28:26 -05:00
|
|
|
func tSetParent(p: tControl, addToView: NSView) {
|
|
|
|
fatalError("cannot call tWindow.tSetParent()")
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:45:01 -05:00
|
|
|
func tFillAutoLayout(inout p: tAutoLayoutParams) {
|
2015-08-07 00:28:26 -05:00
|
|
|
fatalError("cannot call tWindow.tFillAutoLayout()")
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:45:01 -05:00
|
|
|
func tRelayout() {
|
2015-08-06 22:10:50 -05:00
|
|
|
if self.c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-07 13:45:01 -05:00
|
|
|
var contentView = self.w.contentView as! NSView
|
2015-08-06 22:10:50 -05:00
|
|
|
contentView.removeConstraints(contentView.constraints)
|
|
|
|
|
|
|
|
var p = tAutoLayoutParams()
|
2015-08-07 13:45:01 -05:00
|
|
|
c?.tFillAutoLayout(&p)
|
2015-08-06 22:10:50 -05:00
|
|
|
|
2015-08-07 13:45:01 -05:00
|
|
|
// TODO why can't I just say var views = [ "view": p.view ]?
|
|
|
|
// I think the parser is getting confused
|
|
|
|
var views = [String: NSView]()
|
|
|
|
views["view"] = p.view
|
2015-08-06 22:10:50 -05:00
|
|
|
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 15:24:15 -05:00
|
|
|
var constraints = mkconstraints(constraint, views)
|
2015-08-06 22:10:50 -05:00
|
|
|
contentView.addConstraints(constraints)
|
|
|
|
|
|
|
|
constraint = "V:"
|
|
|
|
if p.attachTop {
|
|
|
|
constraint += "|" + margin
|
|
|
|
}
|
|
|
|
constraint += "[view]"
|
|
|
|
if p.attachBottom {
|
|
|
|
constraint += margin + "|"
|
|
|
|
}
|
2015-08-07 15:24:15 -05:00
|
|
|
constraints = mkconstraints(constraint, views)
|
2015-08-06 22:10:50 -05:00
|
|
|
contentView.addConstraints(constraints)
|
|
|
|
}
|
|
|
|
}
|