From ad895a7f7eaf5c4b345e99a2a68697d0140e8a00 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 7 Aug 2015 16:24:15 -0400 Subject: [PATCH] Fixed some more runtime errors. I have no idea how this code even worked anymore. --- redo/osxaltest/box.swift | 21 ++++++--------------- redo/osxaltest/control.swift | 9 +++++++++ redo/osxaltest/main.swift | 6 +++--- redo/osxaltest/spinbox.swift | 19 +++---------------- redo/osxaltest/window.swift | 14 +++----------- 5 files changed, 24 insertions(+), 45 deletions(-) diff --git a/redo/osxaltest/box.swift b/redo/osxaltest/box.swift index 90c75201..0d1ae063 100644 --- a/redo/osxaltest/box.swift +++ b/redo/osxaltest/box.swift @@ -7,7 +7,6 @@ import Cocoa class tBoxContainer : NSView { override var alignmentRectInsets: NSEdgeInsets { get { -print("in tBoxContainer.alignmentRectInsets") return NSEdgeInsetsMake(50, 50, 50, 50) } } @@ -61,7 +60,7 @@ class tBox : tControl { // TODO make the other dimension not hug (as an experiment) func tFillAutoLayout(inout p: tAutoLayoutParams) { var hasStretchy = false - if self.children.count == 0 { + if self.children.count != 0 { hasStretchy = self.actualLayoutWork() } p.view = self.v @@ -117,9 +116,9 @@ class tBox : tControl { } // first string the views together - var constraint = "H:" + var constraint = "H:|" if self.vertical { - constraint = "V:" + constraint = "V:|" } var firstStretchy = true // swift can't tell that nStretchy isn't used until firstStretchy becomes false @@ -129,7 +128,7 @@ class tBox : tControl { constraint += "-" } 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)] if child.stretchy { if firstStretchy { @@ -144,11 +143,7 @@ class tBox : tControl { constraint += "]" } constraint += "|" - self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( - constraint, - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views)) + self.v.addConstraints(mkconstraints(constraint, views)) // TODO do not release constraint; it's autoreleased? // next make the views span the full other dimension @@ -159,11 +154,7 @@ class tBox : tControl { constraint = "H:|[" } constraint += tAutoLayoutKey(i) + "]|" - self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( - constraint, - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views)) + self.v.addConstraints(mkconstraints(constraint, views)) // TODO do not release constraint; it's autoreleased? } diff --git a/redo/osxaltest/control.swift b/redo/osxaltest/control.swift index 25d8e739..bb9bd370 100644 --- a/redo/osxaltest/control.swift +++ b/redo/osxaltest/control.swift @@ -22,3 +22,12 @@ protocol tControl : class { func tAutoLayoutKey(n: UIntMax) -> 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) +} diff --git a/redo/osxaltest/main.swift b/redo/osxaltest/main.swift index a47e24a9..4ed49e8a 100644 --- a/redo/osxaltest/main.swift +++ b/redo/osxaltest/main.swift @@ -2,7 +2,7 @@ import Cocoa var spaced = false -var firstvert = false +var firstvert = true func appLaunched() { var hbox: tBox @@ -57,9 +57,9 @@ func appLaunched() { hbox = tBox(vertical: !firstvert, spaced: spaced) entry = tEntry() - hbox.tAddControl(entry, stretchy: false) - entry = tEntry() hbox.tAddControl(entry, stretchy: true) + entry = tEntry() + hbox.tAddControl(entry, stretchy: false) box.tAddControl(hbox, stretchy: false) hbox = tBox(vertical: !firstvert, spaced: spaced) diff --git a/redo/osxaltest/spinbox.swift b/redo/osxaltest/spinbox.swift index 095304b8..a832ecdd 100644 --- a/redo/osxaltest/spinbox.swift +++ b/redo/osxaltest/spinbox.swift @@ -5,7 +5,6 @@ import Cocoa class tSpinboxContainer : NSView { override var alignmentRectInsets: NSEdgeInsets { get { -print("in tSpinboxContainer.alignmentRectInsets") return NSEdgeInsetsMake(50, 50, 50, 50) } } @@ -51,23 +50,11 @@ nspinbox++ "t": self.t, "s": self.s, ] - var constraints = NSLayoutConstraint.constraintsWithVisualFormat( - "H:|[t]-[s]|", - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views) + var constraints = mkconstraints("H:|[t]-[s]|", views) self.c.addConstraints(constraints) - constraints = NSLayoutConstraint.constraintsWithVisualFormat( - "V:|[t]|", - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views) + constraints = mkconstraints("V:|[t]|", views) self.c.addConstraints(constraints) - constraints = NSLayoutConstraint.constraintsWithVisualFormat( - "V:|[s]|", - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views) + constraints = mkconstraints("V:|[s]|", views) self.c.addConstraints(constraints) self.parent = nil diff --git a/redo/osxaltest/window.swift b/redo/osxaltest/window.swift index 60e8cebb..5d0869c1 100644 --- a/redo/osxaltest/window.swift +++ b/redo/osxaltest/window.swift @@ -4,7 +4,7 @@ import Cocoa // auto layout helpers func tIsAmbiguous(view: NSView, indent: Int) { var s = String(count: indent, repeatedValue: " " as Character) - print("\(s) \(view.className) \(view.hasAmbiguousLayout)") + println("\(s) \(view.className) \(view.hasAmbiguousLayout)") if view.hasAmbiguousLayout { view.window?.visualizeConstraints(view.superview!.constraints) } @@ -85,11 +85,7 @@ class tWindow : tControl { if p.attachRight { constraint += margin + "|" } - var constraints = NSLayoutConstraint.constraintsWithVisualFormat( - constraint, - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views) + var constraints = mkconstraints(constraint, views) contentView.addConstraints(constraints) constraint = "V:" @@ -100,11 +96,7 @@ class tWindow : tControl { if p.attachBottom { constraint += margin + "|" } - constraints = NSLayoutConstraint.constraintsWithVisualFormat( - constraint, - options: NSLayoutFormatOptions(0), - metrics: nil, - views: views) + constraints = mkconstraints(constraint, views) contentView.addConstraints(constraints) } }