libui/redo/osxaltest/window.swift

87 lines
2.1 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 {
isAmbiguous(subview as! NSView, indent + 1)
2015-08-06 22:10:50 -05:00
}
}
class Window : NSWindow, Control {
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() {
self.c = nil
self.margined = false
// we have to initialize our own instance variables first, unfortunately (thanks erica in irc.freenode.net/#swift-lang)
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"
}
required init?(coder: NSCoder) {
fatalError("can't use this constructor, sorry")
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)
isAmbiguous(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
}
private func relayout() {
2015-08-06 22:10:50 -05:00
if self.c == nil {
return
}
var contentView = self.contentView as! NSView
2015-08-06 22:10:50 -05:00
contentView.removeConstraints(contentView.constraints)
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, nil, views)
2015-08-06 22:10:50 -05:00
contentView.addConstraints(constraints)
constraint = "V:|" + margin + "[view]" + margin + "|"
constraints = mkconstraints(constraint, nil, views)
2015-08-06 22:10:50 -05:00
contentView.addConstraints(constraints)
}
}