Fixed some more runtime errors. I have no idea how this code even worked anymore.

This commit is contained in:
Pietro Gagliardi 2015-08-07 16:24:15 -04:00
parent 9aeeeab812
commit ad895a7f7e
5 changed files with 24 additions and 45 deletions

View File

@ -7,7 +7,6 @@ import Cocoa
class tBoxContainer : NSView { class tBoxContainer : NSView {
override var alignmentRectInsets: NSEdgeInsets { override var alignmentRectInsets: NSEdgeInsets {
get { get {
print("in tBoxContainer.alignmentRectInsets")
return NSEdgeInsetsMake(50, 50, 50, 50) return NSEdgeInsetsMake(50, 50, 50, 50)
} }
} }
@ -61,7 +60,7 @@ class tBox : tControl {
// TODO make the other dimension not hug (as an experiment) // TODO make the other dimension not hug (as an experiment)
func tFillAutoLayout(inout p: tAutoLayoutParams) { func tFillAutoLayout(inout p: tAutoLayoutParams) {
var hasStretchy = false var hasStretchy = false
if self.children.count == 0 { if self.children.count != 0 {
hasStretchy = self.actualLayoutWork() hasStretchy = self.actualLayoutWork()
} }
p.view = self.v p.view = self.v
@ -117,9 +116,9 @@ class tBox : tControl {
} }
// first string the views together // first string the views together
var constraint = "H:" var constraint = "H:|"
if self.vertical { if self.vertical {
constraint = "V:" constraint = "V:|"
} }
var firstStretchy = true var firstStretchy = true
// swift can't tell that nStretchy isn't used until firstStretchy becomes false // swift can't tell that nStretchy isn't used until firstStretchy becomes false
@ -129,7 +128,7 @@ class tBox : tControl {
constraint += "-" constraint += "-"
} }
constraint += "[" + tAutoLayoutKey(i) constraint += "[" + tAutoLayoutKey(i)
// swift currently can't do self.children[i].stretchy // TODO swift currently can't do self.children[i].stretchy
var child = self.children[Int(i)] var child = self.children[Int(i)]
if child.stretchy { if child.stretchy {
if firstStretchy { if firstStretchy {
@ -144,11 +143,7 @@ class tBox : tControl {
constraint += "]" constraint += "]"
} }
constraint += "|" constraint += "|"
self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( self.v.addConstraints(mkconstraints(constraint, views))
constraint,
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views))
// TODO do not release constraint; it's autoreleased? // TODO do not release constraint; it's autoreleased?
// next make the views span the full other dimension // next make the views span the full other dimension
@ -159,11 +154,7 @@ class tBox : tControl {
constraint = "H:|[" constraint = "H:|["
} }
constraint += tAutoLayoutKey(i) + "]|" constraint += tAutoLayoutKey(i) + "]|"
self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( self.v.addConstraints(mkconstraints(constraint, views))
constraint,
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views))
// TODO do not release constraint; it's autoreleased? // TODO do not release constraint; it's autoreleased?
} }

View File

@ -22,3 +22,12 @@ protocol tControl : class {
func tAutoLayoutKey(n: UIntMax) -> String { func tAutoLayoutKey(n: UIntMax) -> String {
return NSString(format: "view%d", n) as String return NSString(format: "view%d", n) as String
} }
func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] {
println("making constraint \(constraint)")
return NSLayoutConstraint.constraintsWithVisualFormat(
constraint,
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views)
}

View File

@ -2,7 +2,7 @@
import Cocoa import Cocoa
var spaced = false var spaced = false
var firstvert = false var firstvert = true
func appLaunched() { func appLaunched() {
var hbox: tBox var hbox: tBox
@ -57,9 +57,9 @@ func appLaunched() {
hbox = tBox(vertical: !firstvert, spaced: spaced) hbox = tBox(vertical: !firstvert, spaced: spaced)
entry = tEntry() entry = tEntry()
hbox.tAddControl(entry, stretchy: false)
entry = tEntry()
hbox.tAddControl(entry, stretchy: true) hbox.tAddControl(entry, stretchy: true)
entry = tEntry()
hbox.tAddControl(entry, stretchy: false)
box.tAddControl(hbox, stretchy: false) box.tAddControl(hbox, stretchy: false)
hbox = tBox(vertical: !firstvert, spaced: spaced) hbox = tBox(vertical: !firstvert, spaced: spaced)

View File

@ -5,7 +5,6 @@ import Cocoa
class tSpinboxContainer : NSView { class tSpinboxContainer : NSView {
override var alignmentRectInsets: NSEdgeInsets { override var alignmentRectInsets: NSEdgeInsets {
get { get {
print("in tSpinboxContainer.alignmentRectInsets")
return NSEdgeInsetsMake(50, 50, 50, 50) return NSEdgeInsetsMake(50, 50, 50, 50)
} }
} }
@ -51,23 +50,11 @@ nspinbox++
"t": self.t, "t": self.t,
"s": self.s, "s": self.s,
] ]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat( var constraints = mkconstraints("H:|[t]-[s]|", views)
"H:|[t]-[s]|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views)
self.c.addConstraints(constraints) self.c.addConstraints(constraints)
constraints = NSLayoutConstraint.constraintsWithVisualFormat( constraints = mkconstraints("V:|[t]|", views)
"V:|[t]|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views)
self.c.addConstraints(constraints) self.c.addConstraints(constraints)
constraints = NSLayoutConstraint.constraintsWithVisualFormat( constraints = mkconstraints("V:|[s]|", views)
"V:|[s]|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views)
self.c.addConstraints(constraints) self.c.addConstraints(constraints)
self.parent = nil self.parent = nil

View File

@ -4,7 +4,7 @@ import Cocoa
// auto layout helpers // auto layout helpers
func tIsAmbiguous(view: NSView, indent: Int) { func tIsAmbiguous(view: NSView, indent: Int) {
var s = String(count: indent, repeatedValue: " " as Character) var s = String(count: indent, repeatedValue: " " as Character)
print("\(s) \(view.className) \(view.hasAmbiguousLayout)") println("\(s) \(view.className) \(view.hasAmbiguousLayout)")
if view.hasAmbiguousLayout { if view.hasAmbiguousLayout {
view.window?.visualizeConstraints(view.superview!.constraints) view.window?.visualizeConstraints(view.superview!.constraints)
} }
@ -85,11 +85,7 @@ class tWindow : tControl {
if p.attachRight { if p.attachRight {
constraint += margin + "|" constraint += margin + "|"
} }
var constraints = NSLayoutConstraint.constraintsWithVisualFormat( var constraints = mkconstraints(constraint, views)
constraint,
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views)
contentView.addConstraints(constraints) contentView.addConstraints(constraints)
constraint = "V:" constraint = "V:"
@ -100,11 +96,7 @@ class tWindow : tControl {
if p.attachBottom { if p.attachBottom {
constraint += margin + "|" constraint += margin + "|"
} }
constraints = NSLayoutConstraint.constraintsWithVisualFormat( constraints = mkconstraints(constraint, views)
constraint,
options: NSLayoutFormatOptions(0),
metrics: nil,
views: views)
contentView.addConstraints(constraints) contentView.addConstraints(constraints)
} }
} }