libui/redo/osxaltest/box.swift

112 lines
2.8 KiB
Swift
Raw Normal View History

2015-08-07 21:01:19 -05:00
// 7 august 2015
import Cocoa
struct BoxControl {
var c: Control
var stretchy: Bool
var horzHuggingPri: NSLayoutPriority
var vertHuggingPri: NSLayoutPriority
2015-08-07 21:01:19 -05:00
}
class Box : NSView, Control {
private var controls: [BoxControl]
private var parent: Control?
2015-08-07 21:40:09 -05:00
private var vertical: Bool
2015-08-07 21:01:19 -05:00
private var padded: Bool
2015-08-07 21:40:09 -05:00
private var primaryDirPrefix: String
private var secondaryDirPrefix: String
private var primaryOrientation: NSLayoutConstraintOrientation
private var secondaryOrientation: NSLayoutConstraintOrientation
2015-08-07 21:40:09 -05:00
init(vertical: Bool, padded: Bool) {
2015-08-07 21:01:19 -05:00
self.controls = []
self.parent = nil
2015-08-07 21:40:09 -05:00
self.vertical = vertical
2015-08-07 21:01:19 -05:00
self.padded = padded
2015-08-07 21:40:09 -05:00
self.primaryDirPrefix = "H:"
self.secondaryDirPrefix = "V:"
self.primaryOrientation = NSLayoutConstraintOrientation.Horizontal
self.secondaryOrientation = NSLayoutConstraintOrientation.Vertical
2015-08-07 21:40:09 -05:00
if self.vertical {
self.primaryDirPrefix = "V:"
self.secondaryDirPrefix = "H:"
self.primaryOrientation = NSLayoutConstraintOrientation.Vertical
self.secondaryOrientation = NSLayoutConstraintOrientation.Horizontal
2015-08-07 21:40:09 -05:00
}
2015-08-07 21:01:19 -05:00
super.init(frame: NSZeroRect)
self.translatesAutoresizingMaskIntoConstraints = false
}
2015-08-07 21:40:09 -05:00
required init?(coder: NSCoder) {
fatalError("can't use this constructor, sorry")
}
2015-08-08 10:12:55 -05:00
func Add(control: Control, _ stretchy: Bool) {
var c: BoxControl
2015-08-07 21:01:19 -05:00
var view = control.View()
2015-08-08 10:12:55 -05:00
c = BoxControl(
c: control,
stretchy: stretchy,
horzHuggingPri: horzHuggingPri(view),
vertHuggingPri: vertHuggingPri(view))
self.addSubview(view)
2015-08-07 21:01:19 -05:00
self.controls.append(c)
// make sure controls don't hug their secondary direction so they fill the width of the view
setHuggingPri(view, myNSLayoutPriorityDefaultLow, self.secondaryOrientation)
2015-08-07 21:40:09 -05:00
self.relayout()
2015-08-07 21:01:19 -05:00
}
func View() -> NSView {
return self
}
func SetParent(p: Control) {
self.parent = p
}
2015-08-07 21:40:09 -05:00
// TODO stretchiness
// - we will need a trailing view if there are no stretchy controls
private func relayout() {
2015-08-08 10:12:55 -05:00
var constraint: String
if self.controls.count == 0 {
2015-08-07 21:40:09 -05:00
return
}
self.removeConstraints(self.constraints)
// first collect the views
var views = [String: NSView]()
var n = 0
for c in self.controls {
views["view\(n)"] = c.c.View()
2015-08-08 10:12:55 -05:00
n++
2015-08-07 21:40:09 -05:00
}
// next, assemble the views in the primary direction
// they all go in a straight line
2015-08-08 10:12:55 -05:00
constraint = "\(self.primaryDirPrefix)|"
for i in 0..<n {
if self.padded && i != 0 {
constraint += "-"
}
constraint += "[view\(i)]"
}
constraint += "|"
var constraints = mkconstraints(constraint, views)
self.addConstraints(constraints)
2015-08-07 21:40:09 -05:00
// next: assemble the views in the secondary direction
// each of them will span the secondary direction
2015-08-08 10:12:55 -05:00
for i in 0..<n {
constraint = "\(self.secondaryDirPrefix)|[view\(i)]|"
var constraints = mkconstraints(constraint, views)
self.addConstraints(constraints)
}
2015-08-07 21:40:09 -05:00
}
2015-08-07 21:01:19 -05:00
}