libui/redo/osxaltest/window.swift

111 lines
2.6 KiB
Swift
Raw Normal View History

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(count: indent, repeatedValue: " " as Character)
2015-08-07 14:07:53 -05:00
print("\(s) \(view.className) \(view.hasAmbiguousLayout)")
2015-08-06 22:10:50 -05:00
if view.hasAmbiguousLayout {
view.window?.visualizeConstraints(view.superview!.constraints)
2015-08-06 22:10:50 -05:00
}
for subview in view.subviews {
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
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),
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
}
func tSetControl(c: tControl) {
2015-08-06 22:10:50 -05:00
self.c = c
// TODO use self.c here
c.tSetParent(self, addToView: self.w.contentView as! NSView)
2015-08-06 22:10:50 -05:00
self.tRelayout()
}
func tSetMargined(m: Bool) {
2015-08-06 22:10:50 -05:00
self.margined = m
self.tRelayout()
}
func tShow() {
2015-08-06 22:10:50 -05:00
self.w.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
self.w.makeKeyAndOrderFront(self)
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()")
}
func tFillAutoLayout(inout p: tAutoLayoutParams) {
2015-08-07 00:28:26 -05:00
fatalError("cannot call tWindow.tFillAutoLayout()")
}
func tRelayout() {
2015-08-06 22:10:50 -05:00
if self.c == nil {
return
}
var contentView = self.w.contentView as! NSView
2015-08-06 22:10:50 -05:00
contentView.removeConstraints(contentView.constraints)
var p = tAutoLayoutParams()
c?.tFillAutoLayout(&p)
2015-08-06 22:10:50 -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 09:46:34 -05:00
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
constraint,
options: NSLayoutFormatOptions(0),
metrics: nil,
views: 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 09:46:34 -05:00
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
constraint,
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views)
2015-08-06 22:10:50 -05:00
contentView.addConstraints(constraints)
}
}