From 02991879bccbe22da715072861eca84639c535d1 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 8 Aug 2015 21:54:58 -0400 Subject: [PATCH] Implemented lack of stretchiness. I believe this is a complete Box implementation. A lot simpler than the other one! :) But will it hold up to the stress test... --- redo/osxaltest/box.swift | 32 ++++++++++++++++++++++++++++++++ redo/osxaltest/main.swift | 4 ++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/redo/osxaltest/box.swift b/redo/osxaltest/box.swift index d1e328ac..e98ef9e2 100644 --- a/redo/osxaltest/box.swift +++ b/redo/osxaltest/box.swift @@ -19,6 +19,10 @@ class Box : NSView, Control { private var primaryOrientation: NSLayoutConstraintOrientation private var secondaryOrientation: NSLayoutConstraintOrientation + // we implement a lack of stretchy controls by adding a stretchy view at the end of the view list when we assemble layouts + // this is that view + private var noStretchyView: NSView + init(vertical: Bool, padded: Bool) { self.controls = [] self.parent = nil @@ -36,6 +40,13 @@ class Box : NSView, Control { self.secondaryOrientation = NSLayoutConstraintOrientation.Horizontal } + self.noStretchyView = NSView(frame: NSZeroRect) + self.noStretchyView.translatesAutoresizingMaskIntoConstraints = false + // make the view stretchy in both directions + // you can tell this is correct by synthesizing an Add() in your head; see below + setHorzHuggingPri(self.noStretchyView, myNSLayoutPriorityDefaultLow) + setVertHuggingPri(self.noStretchyView, myNSLayoutPriorityDefaultLow) + super.init(frame: NSZeroRect) self.translatesAutoresizingMaskIntoConstraints = false } @@ -101,6 +112,19 @@ class Box : NSView, Control { n++ } + // if there are no stretchy controls, we must add the no-stretchy view + // if there are, we must remove it + if firstStretchy == -1 { + if self.noStretchyView.superview == nil { + self.addSubview(self.noStretchyView) + } + views["noStretchyView"] = self.noStretchyView + } else { + if self.noStretchyView.superview != nil { + self.noStretchyView.removeFromSuperview() + } + } + // next, assemble the views in the primary direction // they all go in a straight line constraint = "\(self.primaryDirPrefix)|" @@ -115,6 +139,9 @@ class Box : NSView, Control { } constraint += "]" } + if firstStretchy == -1 { // don't space between the last control and the no-stretchy view + constraint += "[noStretchyView]" + } constraint += "|" var constraints = mkconstraints(constraint, views) self.addConstraints(constraints) @@ -126,5 +153,10 @@ class Box : NSView, Control { var constraints = mkconstraints(constraint, views) self.addConstraints(constraints) } + if firstStretchy == -1 { // and again to the no-stretchy view + constraint = "\(self.secondaryDirPrefix)|[noStretchyView]|" + var constraints = mkconstraints(constraint, views) + self.addConstraints(constraints) + } } } diff --git a/redo/osxaltest/main.swift b/redo/osxaltest/main.swift index aff6f017..f3f352bd 100644 --- a/redo/osxaltest/main.swift +++ b/redo/osxaltest/main.swift @@ -16,8 +16,8 @@ func appLaunched() { var box = Box(vertical: firstvert, padded: spaced) mainwin.SetControl(box) - box.Add(Entry(), true) - box.Add(Button("Button"), true) + box.Add(Entry(), false) + box.Add(Button("Button"), false) mainwin.Show()