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.

This commit is contained in:
Pietro Gagliardi 2015-08-07 18:17:29 -04:00
parent 10914816ff
commit 4857ddc018
8 changed files with 31 additions and 125 deletions

View File

@ -1,26 +1,10 @@
// 31 july 2015 // 31 july 2015
import Cocoa import Cocoa
// TODO stretchy across both dimensions protocol Control : class {
// for a vertical box, the horizontal width should be variable func View() -> NSView
struct tAutoLayoutParams { func SetParent(p: Control)
var view: NSView? = nil func Relayout()
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)"
} }
func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] { func mkconstraints(constraint: String, views: [String: NSView]) -> [AnyObject] {

View File

@ -5,69 +5,10 @@ var spaced = false
var firstvert = true var firstvert = true
func appLaunched() { func appLaunched() {
var hbox: tBox var mainwin = Window()
var spinbox: tSpinbox mainwin.SetMargined(spaced)
var button: tButton
var entry: tEntry
var label: tLabel
var mainwin = tWindow() mainwin.Show()
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()
} }
class appDelegate : NSObject, NSApplicationDelegate { class appDelegate : NSObject, NSApplicationDelegate {

View File

@ -2,7 +2,7 @@
import Cocoa import Cocoa
// auto layout helpers // auto layout helpers
func tIsAmbiguous(view: NSView, indent: Int) { func isAmbiguous(view: NSView, indent: Int) {
var s = String(count: indent, repeatedValue: " " as Character) var s = String(count: indent, repeatedValue: " " as Character)
println("\(s) \(view.className) \(view.hasAmbiguousLayout)") println("\(s) \(view.className) \(view.hasAmbiguousLayout)")
if view.hasAmbiguousLayout { if view.hasAmbiguousLayout {
@ -13,49 +13,48 @@ func tIsAmbiguous(view: NSView, indent: Int) {
} }
} }
class tWindow : tControl { class Window : NSWindow, Control {
private var w: NSWindow
private var c: tControl? private var c: tControl?
private var margined: Bool private var margined: Bool
init() { init() {
self.w = NSWindow( super.init(
contentRect: NSMakeRect(0, 0, 320, 240), contentRect: NSMakeRect(0, 0, 320, 240),
styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask), styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask),
backing: NSBackingStoreType.Buffered, backing: NSBackingStoreType.Buffered,
defer: true) defer: true)
self.w.title = "Auto Layout Test" self.title = "Auto Layout Test"
self.c = nil self.c = nil
self.margined = false self.margined = false
} }
func tSetControl(c: tControl) { func SetControl(c: Control) {
self.c = c self.c = c
// TODO use self.c here var contentView = self.contentView as! NSView
c.tSetParent(self, addToView: self.w.contentView as! NSView) contentView.addSubview(self.c?.View())
self.tRelayout() self.Relayout()
} }
func tSetMargined(m: Bool) { func SetMargined(m: Bool) {
self.margined = m self.margined = m
self.tRelayout() self.Relayout()
} }
func tShow() { func Show() {
self.w.cascadeTopLeftFromPoint(NSMakePoint(20, 20)) self.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
self.w.makeKeyAndOrderFront(self) self.makeKeyAndOrderFront(self)
tIsAmbiguous(self.w.contentView as! NSView, 0) tIsAmbiguous(self.contentView as! NSView, 0)
} }
func tSetParent(p: tControl, addToView: NSView) { func View() -> NSView {
fatalError("cannot call tWindow.tSetParent()") fatalError("cannot call Window.View()")
} }
func tFillAutoLayout(inout p: tAutoLayoutParams) { func SetParent(p: Control) {
fatalError("cannot call tWindow.tFillAutoLayout()") fatalError("cannot call Window.SetParent()")
} }
func tRelayout() { func Relayout() {
if self.c == nil { if self.c == nil {
return return
} }
@ -63,39 +62,21 @@ class tWindow : tControl {
var contentView = self.w.contentView as! NSView var contentView = self.w.contentView as! NSView
contentView.removeConstraints(contentView.constraints) contentView.removeConstraints(contentView.constraints)
var p = tAutoLayoutParams()
c?.tFillAutoLayout(&p)
// TODO why can't I just say var views = [ "view": p.view ]? // TODO why can't I just say var views = [ "view": p.view ]?
// I think the parser is getting confused // I think the parser is getting confused
var views = [String: NSView]() var views = [
views["view"] = p.view "view": self.c?.View(),
]
var margin = "" var margin = ""
if self.margined { if self.margined {
margin = "-" margin = "-"
} }
// TODO always append margins even if not attached? var constraint = "H:|" + margin + "[view]" + margin + "|"
// or if not attached, append ->=0- as well?
var constraint = "H:"
if p.attachLeft {
constraint += "|" + margin
}
constraint += "[view]"
if p.attachRight {
constraint += margin + "|"
}
var constraints = mkconstraints(constraint, views) var constraints = mkconstraints(constraint, views)
contentView.addConstraints(constraints) contentView.addConstraints(constraints)
constraint = "V:" constraint = "V:|" + margin + "[view]" + margin + "|"
if p.attachTop {
constraint += "|" + margin
}
constraint += "[view]"
if p.attachBottom {
constraint += margin + "|"
}
constraints = mkconstraints(constraint, views) constraints = mkconstraints(constraint, views)
contentView.addConstraints(constraints) contentView.addConstraints(constraints)
} }