From cacc4f1ed6b31e408ac926247199aa3d807bd56c Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 7 Aug 2015 22:40:09 -0400 Subject: [PATCH] More Box work. --- redo/osxaltest/box.swift | 43 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/redo/osxaltest/box.swift b/redo/osxaltest/box.swift index 556baac9..19945802 100644 --- a/redo/osxaltest/box.swift +++ b/redo/osxaltest/box.swift @@ -9,17 +9,33 @@ struct BoxControl { class Box : NSView, Control { private var controls: [BoxControl] private var parent: Control? + private var vertical: Bool private var padded: Bool - init(_ padded: Bool) { + private var primaryDirPrefix: String + private var secondaryDirPrefix: String + + init(vertical: Bool, padded: Bool) { self.controls = [] self.parent = nil + self.vertical = vertical self.padded = padded + self.primaryDirPrefix = "H:" + self.secondaryDirPrefix = "V:" + if self.vertical { + self.primaryDirPrefix = "V:" + self.secondaryDirPrefix = "H:" + } + super.init(frame: NSZeroRect) self.translatesAutoresizingMaskIntoConstraints = false } + required init?(coder: NSCoder) { + fatalError("can't use this constructor, sorry") + } + func Add(control: Control, stretchy: Stretchy) { var c BoxControl @@ -27,7 +43,7 @@ class Box : NSView, Control { c.stretchy = stretchy self.addSubview(c.c.View()) self.controls.append(c) - // TODO relayout + self.relayout() } func View() -> NSView { @@ -37,4 +53,27 @@ class Box : NSView, Control { func SetParent(p: Control) { self.parent = p } + + // TODO stretchiness + // - we will need a trailing view if there are no stretchy controls + private func relayout() { + if self.children.count == 0 { + 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() + } + + // next, assemble the views in the primary direction + // they all go in a straight line + + // next: assemble the views in the secondary direction + // each of them will span the secondary direction + } }