2015-08-06 22:10:50 -05:00
|
|
|
// 1 august 2015
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
// auto layout helpers
|
2015-08-07 17:17:29 -05:00
|
|
|
func isAmbiguous(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 18:29:01 -05:00
|
|
|
isAmbiguous(subview as! NSView, indent + 1)
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
class Window : NSWindow, Control {
|
2015-08-07 18:29:01 -05:00
|
|
|
private var c: Control?
|
2015-08-07 01:06:18 -05:00
|
|
|
private var margined: Bool
|
2015-08-06 22:10:50 -05:00
|
|
|
|
|
|
|
init() {
|
2015-08-07 18:29:01 -05:00
|
|
|
self.c = nil
|
|
|
|
self.margined = false
|
|
|
|
// we have to initialize our own instance variables first, unfortunately (thanks erica in irc.freenode.net/#swift-lang)
|
2015-08-07 17:17:29 -05:00
|
|
|
super.init(
|
2015-08-06 22:10:50 -05:00
|
|
|
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)
|
2015-08-07 17:17:29 -05:00
|
|
|
self.title = "Auto Layout Test"
|
2015-08-07 18:29:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("can't use this constructor, sorry")
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
func SetControl(c: Control) {
|
2015-08-06 22:10:50 -05:00
|
|
|
self.c = c
|
2015-08-07 17:17:29 -05:00
|
|
|
var contentView = self.contentView as! NSView
|
2015-08-07 18:29:01 -05:00
|
|
|
contentView.addSubview(self.c!.View())
|
2015-08-07 17:17:29 -05:00
|
|
|
self.Relayout()
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
func SetMargined(m: Bool) {
|
2015-08-06 22:10:50 -05:00
|
|
|
self.margined = m
|
2015-08-07 17:17:29 -05:00
|
|
|
self.Relayout()
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
func Show() {
|
|
|
|
self.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
|
|
|
|
self.makeKeyAndOrderFront(self)
|
2015-08-07 18:29:01 -05:00
|
|
|
isAmbiguous(self.contentView as! NSView, 0)
|
2015-08-06 22:10:50 -05:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
func View() -> NSView {
|
|
|
|
fatalError("cannot call Window.View()")
|
2015-08-07 00:28:26 -05:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
func SetParent(p: Control) {
|
|
|
|
fatalError("cannot call Window.SetParent()")
|
2015-08-07 00:28:26 -05:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
func Relayout() {
|
2015-08-06 22:10:50 -05:00
|
|
|
if self.c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-07 18:29:01 -05:00
|
|
|
var contentView = self.contentView as! NSView
|
2015-08-06 22:10:50 -05:00
|
|
|
contentView.removeConstraints(contentView.constraints)
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
var views = [
|
2015-08-07 18:29:01 -05:00
|
|
|
"view": self.c!.View(),
|
2015-08-07 17:17:29 -05:00
|
|
|
]
|
2015-08-06 22:10:50 -05:00
|
|
|
var margin = ""
|
|
|
|
if self.margined {
|
|
|
|
margin = "-"
|
|
|
|
}
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
var constraint = "H:|" + margin + "[view]" + 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)
|
|
|
|
|
2015-08-07 17:17:29 -05:00
|
|
|
constraint = "V:|" + margin + "[view]" + margin + "|"
|
2015-08-07 15:24:15 -05:00
|
|
|
constraints = mkconstraints(constraint, views)
|
2015-08-06 22:10:50 -05:00
|
|
|
contentView.addConstraints(constraints)
|
|
|
|
}
|
|
|
|
}
|