From 4857ddc01877d2ceebb63fea8b2dddd9f89dbc66 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 7 Aug 2015 18:17:29 -0400 Subject: [PATCH] Started restructuring this whole thing, Swift-izing it in the process. I'm going to just abandon the current tBox implementation; I have no idea how it worked at all. --- redo/osxaltest/{box.swift => box.swift_} | 0 .../osxaltest/{button.swift => button.swift_} | 0 redo/osxaltest/control.swift | 24 ++----- redo/osxaltest/{entry.swift => entry.swift_} | 0 redo/osxaltest/{label.swift => label.swift_} | 0 redo/osxaltest/main.swift | 65 +----------------- .../{spinbox.swift => spinbox.swift_} | 0 redo/osxaltest/window.swift | 67 +++++++------------ 8 files changed, 31 insertions(+), 125 deletions(-) rename redo/osxaltest/{box.swift => box.swift_} (100%) rename redo/osxaltest/{button.swift => button.swift_} (100%) rename redo/osxaltest/{entry.swift => entry.swift_} (100%) rename redo/osxaltest/{label.swift => label.swift_} (100%) rename redo/osxaltest/{spinbox.swift => spinbox.swift_} (100%) diff --git a/redo/osxaltest/box.swift b/redo/osxaltest/box.swift_ similarity index 100% rename from redo/osxaltest/box.swift rename to redo/osxaltest/box.swift_ diff --git a/redo/osxaltest/button.swift b/redo/osxaltest/button.swift_ similarity index 100% rename from redo/osxaltest/button.swift rename to redo/osxaltest/button.swift_ diff --git a/redo/osxaltest/control.swift b/redo/osxaltest/control.swift index accbff82..c0beb94a 100644 --- a/redo/osxaltest/control.swift +++ b/redo/osxaltest/control.swift @@ -1,26 +1,10 @@ // 31 july 2015 import Cocoa -// TODO stretchy across both dimensions -// for a vertical box, the horizontal width should be variable -struct tAutoLayoutParams { - var view: NSView? = nil - var attachLeft: Bool = false - var attachTop: Bool = false - var attachRight: Bool = false - var attachBottom: Bool = false - var nonStretchyWidthPredicate: String = "" - var nonStretchyHeightPredicate: String = "" -} - -protocol tControl : class { - func tSetParent(p: tControl, addToView: NSView) - func tFillAutoLayout(inout p: tAutoLayoutParams) - func tRelayout() -} - -func tAutoLayoutKey(n: Int) -> String { - return "view\(n)" +protocol Control : class { + func View() -> NSView + func SetParent(p: Control) + func Relayout() } func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] { diff --git a/redo/osxaltest/entry.swift b/redo/osxaltest/entry.swift_ similarity index 100% rename from redo/osxaltest/entry.swift rename to redo/osxaltest/entry.swift_ diff --git a/redo/osxaltest/label.swift b/redo/osxaltest/label.swift_ similarity index 100% rename from redo/osxaltest/label.swift rename to redo/osxaltest/label.swift_ diff --git a/redo/osxaltest/main.swift b/redo/osxaltest/main.swift index 4ed49e8a..9f10b262 100644 --- a/redo/osxaltest/main.swift +++ b/redo/osxaltest/main.swift @@ -5,69 +5,10 @@ var spaced = false var firstvert = true func appLaunched() { - var hbox: tBox - var spinbox: tSpinbox - var button: tButton - var entry: tEntry - var label: tLabel + var mainwin = Window() + mainwin.SetMargined(spaced) - var mainwin = tWindow() - mainwin.tSetMargined(spaced) - - var box = tBox(vertical: firstvert, spaced: spaced) - - spinbox = tSpinbox() - box.tAddControl(spinbox, stretchy: false) - - mainwin.tSetControl(box) - - hbox = tBox(vertical: !firstvert, spaced: spaced) - button = tButton("Button") - hbox.tAddControl(button, stretchy: true) - button = tButton("Button") - hbox.tAddControl(button, stretchy: true) - box.tAddControl(hbox, stretchy: false) - - hbox = tBox(vertical: !firstvert, spaced: spaced) - button = tButton("Button") - hbox.tAddControl(button, stretchy: true) - button = tButton("Button") - hbox.tAddControl(button, stretchy: true) - box.tAddControl(hbox, stretchy: false) - - // TODO in vertical mode the three non-stretchy buttons are smaller than they should be - hbox = tBox(vertical: !firstvert, spaced: spaced) - button = tButton("Button") - hbox.tAddControl(button, stretchy: true) - button = tButton("A") - hbox.tAddControl(button, stretchy: false) - button = tButton("BB") - hbox.tAddControl(button, stretchy: false) - button = tButton("CCC") - hbox.tAddControl(button, stretchy: false) - box.tAddControl(hbox, stretchy: false) - - // TODO this isn't stretchy in the proper order - hbox = tBox(vertical: !firstvert, spaced: spaced) - spinbox = tSpinbox() - hbox.tAddControl(spinbox, stretchy: false) - spinbox = tSpinbox() - hbox.tAddControl(spinbox, stretchy: true) - box.tAddControl(hbox, stretchy: false) - - hbox = tBox(vertical: !firstvert, spaced: spaced) - 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) - label = tLabel() - hbox.tAddControl(label, stretchy: false) - box.tAddControl(hbox, stretchy: false) - - mainwin.tShow() + mainwin.Show() } class appDelegate : NSObject, NSApplicationDelegate { diff --git a/redo/osxaltest/spinbox.swift b/redo/osxaltest/spinbox.swift_ similarity index 100% rename from redo/osxaltest/spinbox.swift rename to redo/osxaltest/spinbox.swift_ diff --git a/redo/osxaltest/window.swift b/redo/osxaltest/window.swift index 5d0869c1..b7e6e5fa 100644 --- a/redo/osxaltest/window.swift +++ b/redo/osxaltest/window.swift @@ -2,7 +2,7 @@ import Cocoa // auto layout helpers -func tIsAmbiguous(view: NSView, indent: Int) { +func isAmbiguous(view: NSView, indent: Int) { var s = String(count: indent, repeatedValue: " " as Character) println("\(s) \(view.className) \(view.hasAmbiguousLayout)") if view.hasAmbiguousLayout { @@ -13,49 +13,48 @@ func tIsAmbiguous(view: NSView, indent: Int) { } } -class tWindow : tControl { - private var w: NSWindow +class Window : NSWindow, Control { private var c: tControl? private var margined: Bool init() { - self.w = NSWindow( + super.init( contentRect: NSMakeRect(0, 0, 320, 240), styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask), backing: NSBackingStoreType.Buffered, defer: true) - self.w.title = "Auto Layout Test" + self.title = "Auto Layout Test" self.c = nil self.margined = false } - func tSetControl(c: tControl) { + func SetControl(c: Control) { self.c = c - // TODO use self.c here - c.tSetParent(self, addToView: self.w.contentView as! NSView) - self.tRelayout() + var contentView = self.contentView as! NSView + contentView.addSubview(self.c?.View()) + self.Relayout() } - func tSetMargined(m: Bool) { + func SetMargined(m: Bool) { self.margined = m - self.tRelayout() + self.Relayout() } - func tShow() { - self.w.cascadeTopLeftFromPoint(NSMakePoint(20, 20)) - self.w.makeKeyAndOrderFront(self) - tIsAmbiguous(self.w.contentView as! NSView, 0) + func Show() { + self.cascadeTopLeftFromPoint(NSMakePoint(20, 20)) + self.makeKeyAndOrderFront(self) + tIsAmbiguous(self.contentView as! NSView, 0) } - func tSetParent(p: tControl, addToView: NSView) { - fatalError("cannot call tWindow.tSetParent()") + func View() -> NSView { + fatalError("cannot call Window.View()") } - func tFillAutoLayout(inout p: tAutoLayoutParams) { - fatalError("cannot call tWindow.tFillAutoLayout()") + func SetParent(p: Control) { + fatalError("cannot call Window.SetParent()") } - func tRelayout() { + func Relayout() { if self.c == nil { return } @@ -63,39 +62,21 @@ class tWindow : tControl { var contentView = self.w.contentView as! NSView contentView.removeConstraints(contentView.constraints) - var p = tAutoLayoutParams() - c?.tFillAutoLayout(&p) - // TODO why can't I just say var views = [ "view": p.view ]? // I think the parser is getting confused - var views = [String: NSView]() - views["view"] = p.view + var views = [ + "view": self.c?.View(), + ] var margin = "" if self.margined { margin = "-" } - // TODO always append margins even if not attached? - // or if not attached, append ->=0- as well? - var constraint = "H:" - if p.attachLeft { - constraint += "|" + margin - } - constraint += "[view]" - if p.attachRight { - constraint += margin + "|" - } + var constraint = "H:|" + margin + "[view]" + margin + "|" var constraints = mkconstraints(constraint, views) contentView.addConstraints(constraints) - constraint = "V:" - if p.attachTop { - constraint += "|" + margin - } - constraint += "[view]" - if p.attachBottom { - constraint += margin + "|" - } + constraint = "V:|" + margin + "[view]" + margin + "|" constraints = mkconstraints(constraint, views) contentView.addConstraints(constraints) }