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
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] {

View File

@ -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 {

View File

@ -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)
}