libui/redo/osxaltest/window.swift

84 lines
2.0 KiB
Swift
Raw Normal View History

2015-08-06 22:10:50 -05:00
// 1 august 2015
import Cocoa
// auto layout helpers
func isAmbiguous(view: NSView, indent: Int) {
var s = String(count: indent, repeatedValue: " " as Character)
println("\(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 Window : NSWindow, Control {
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() {
super.init(
2015-08-06 22:10:50 -05:00
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.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 SetControl(c: Control) {
2015-08-06 22:10:50 -05:00
self.c = c
var contentView = self.contentView as! NSView
contentView.addSubview(self.c?.View())
self.Relayout()
2015-08-06 22:10:50 -05:00
}
func SetMargined(m: Bool) {
2015-08-06 22:10:50 -05:00
self.margined = m
self.Relayout()
2015-08-06 22:10:50 -05:00
}
func Show() {
self.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
self.makeKeyAndOrderFront(self)
tIsAmbiguous(self.contentView as! NSView, 0)
2015-08-06 22:10:50 -05:00
}
func View() -> NSView {
fatalError("cannot call Window.View()")
2015-08-07 00:28:26 -05:00
}
func SetParent(p: Control) {
fatalError("cannot call Window.SetParent()")
2015-08-07 00:28:26 -05:00
}
func Relayout() {
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)
// TODO why can't I just say var views = [ "view": p.view ]?
// I think the parser is getting confused
var views = [
"view": self.c?.View(),
]
2015-08-06 22:10:50 -05:00
var margin = ""
if self.margined {
margin = "-"
}
var constraint = "H:|" + margin + "[view]" + margin + "|"
var constraints = mkconstraints(constraint, views)
2015-08-06 22:10:50 -05:00
contentView.addConstraints(constraints)
constraint = "V:|" + margin + "[view]" + margin + "|"
constraints = mkconstraints(constraint, views)
2015-08-06 22:10:50 -05:00
contentView.addConstraints(constraints)
}
}