From 372cfd048d56b1a383f9b955874a7e17cb7f5b42 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 11 Aug 2015 20:21:30 -0400 Subject: [PATCH] Experimented with manually specifying control sizes for nonstretchy controls in Box. This resolves ambiguities! --- redo/osxaltest/box.swift | 16 +++++++++++++++- redo/osxaltest/layout.swift | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/redo/osxaltest/box.swift b/redo/osxaltest/box.swift index 2a7d358c..806c82bb 100644 --- a/redo/osxaltest/box.swift +++ b/redo/osxaltest/box.swift @@ -90,6 +90,8 @@ class Box : NSView, Control { self.parent = p } + // TODO do we still need to set hugging? + // TODO try unsetting spinbox intrinsics and seeing what happens private func relayout() { var constraint: String @@ -103,8 +105,12 @@ class Box : NSView, Control { var views = [String: NSView]() var n = 0 var firstStretchy = -1 + var metrics = [String: CGFloat]() for c in self.controls { views["view\(n)"] = c.c.View() + var s = fittingAlignmentSize(c.c.View()) + metrics["view\(n)width"] = s.width + metrics["view\(n)height"] = s.height if firstStretchy == -1 && c.stretchy { firstStretchy = n } @@ -136,13 +142,21 @@ class Box : NSView, Control { if self.controls[i].stretchy && i != firstStretchy { constraint += "(==view\(firstStretchy))" } + // if the control is not stretchy, restrict it to the fitting size + if !self.controls[i].stretchy { + if self.vertical { + constraint += "(==view\(i)height)" + } else { + constraint += "(==view\(i)width)" + } + } constraint += "]" } if firstStretchy == -1 { // don't space between the last control and the no-stretchy view constraint += "[noStretchyView]" } constraint += "|" - var constraints = mkconstraints(constraint, nil, views) + var constraints = mkconstraints(constraint, metrics, views) self.addConstraints(constraints) // next: assemble the views in the secondary direction diff --git a/redo/osxaltest/layout.swift b/redo/osxaltest/layout.swift index 8b830182..bbcd7b5a 100644 --- a/redo/osxaltest/layout.swift +++ b/redo/osxaltest/layout.swift @@ -1,7 +1,7 @@ // 8 august 2015 import Cocoa -func mkconstraints(constraint: String, metrics: [String: Double]?, views: [String: NSView]) -> [AnyObject] { +func mkconstraints(constraint: String, metrics: [String: CGFloat]?, views: [String: NSView]) -> [AnyObject] { return NSLayoutConstraint.constraintsWithVisualFormat( constraint, options: NSLayoutFormatOptions(0),