Fixed build errors. Now to fix runtime errors.
This commit is contained in:
parent
06f32ca759
commit
dbde124471
|
@ -5,21 +5,34 @@ import Cocoa
|
||||||
// TODO fine tune this
|
// TODO fine tune this
|
||||||
// TODO de-duplicate this from spinbox.m
|
// TODO de-duplicate this from spinbox.m
|
||||||
class tBoxContainer : NSView {
|
class tBoxContainer : NSView {
|
||||||
override func alignmentRectInsets() -> NSEdgeInsets {
|
override var alignmentRectInsets: NSEdgeInsets {
|
||||||
|
get {
|
||||||
|
debugPrint("in tBoxContainer.alignmentRectInsets")
|
||||||
return NSEdgeInsetsMake(50, 50, 50, 50)
|
return NSEdgeInsetsMake(50, 50, 50, 50)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct tBoxChild {
|
struct tBoxChild {
|
||||||
var c: tControl
|
var c: tControl
|
||||||
var stretchy: Bool
|
var stretchy: Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the swift bridge isn't perfect; it won't recognize these properly
|
||||||
|
// thanks to Eridius in freenode/#swift-lang
|
||||||
|
let myNSLayoutPriorityRequired: NSLayoutPriority = 1000
|
||||||
|
let myNSLayoutPriorityDefaultHigh: NSLayoutPriority = 750
|
||||||
|
let myNSLayoutPriorityDragThatCanResizeWindow: NSLayoutPriority = 510
|
||||||
|
let myNSLayoutPriorityWindowSizeStayPut: NSLayoutPriority = 500
|
||||||
|
let myNSLayoutPriorityDragThatCannotResizeWindow: NSLayoutPriority = 490
|
||||||
|
let myNSLayoutPriorityDefaultLow: NSLayoutPriority = 250
|
||||||
|
let myNSLayoutPriorityFittingSizeCompression: NSLayoutPriority = 50
|
||||||
|
|
||||||
class tBox : tControl {
|
class tBox : tControl {
|
||||||
private var v: NSView
|
private var v: NSView
|
||||||
private var children: [tBoxChild]
|
private var children: [tBoxChild]
|
||||||
private var vertical: Bool
|
private var vertical: Bool
|
||||||
private var parent: tControl
|
private var parent: tControl?
|
||||||
private var spaced: Bool
|
private var spaced: Bool
|
||||||
|
|
||||||
// TODO rename to padded
|
// TODO rename to padded
|
||||||
|
@ -46,15 +59,29 @@ class tBox : tControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO make the other dimension not hug (as an experiment)
|
// TODO make the other dimension not hug (as an experiment)
|
||||||
func tFillAutoLayout(p: tAutoLayoutParams) {
|
func tFillAutoLayout(inout p: tAutoLayoutParams) {
|
||||||
var orientation: NSLayoutConstraintOrientation
|
var hasStretchy = false
|
||||||
var i, n: UIntMax
|
|
||||||
var pp: tAutoLayoutParams
|
|
||||||
var nStretchy: UIntMax
|
|
||||||
|
|
||||||
if self.children.count == 0 {
|
if self.children.count == 0 {
|
||||||
goto selfOnly
|
hasStretchy = self.actualLayoutWork()
|
||||||
}
|
}
|
||||||
|
p.view = self.v
|
||||||
|
p.attachLeft = true
|
||||||
|
p.attachTop = true
|
||||||
|
// don't attach to the end if there weren't any stretchy controls
|
||||||
|
if self.vertical {
|
||||||
|
p.attachRight = true
|
||||||
|
p.attachBottom = hasStretchy
|
||||||
|
} else {
|
||||||
|
p.attachRight = hasStretchy
|
||||||
|
p.attachBottom = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func actualLayoutWork() -> Bool {
|
||||||
|
var orientation: NSLayoutConstraintOrientation
|
||||||
|
// TODO don't use UIntMax
|
||||||
|
var i, n: UIntMax
|
||||||
|
var nStretchy: UIntMax
|
||||||
|
|
||||||
self.v.removeConstraints(self.v.constraints)
|
self.v.removeConstraints(self.v.constraints)
|
||||||
|
|
||||||
|
@ -66,6 +93,7 @@ class tBox : tControl {
|
||||||
var views = [String: NSView]()
|
var views = [String: NSView]()
|
||||||
n = 0
|
n = 0
|
||||||
var predicates = [String]()
|
var predicates = [String]()
|
||||||
|
var pp = tAutoLayoutParams()
|
||||||
for child in self.children {
|
for child in self.children {
|
||||||
var priority: NSLayoutPriority
|
var priority: NSLayoutPriority
|
||||||
|
|
||||||
|
@ -73,17 +101,17 @@ class tBox : tControl {
|
||||||
pp.nonStretchyHeightPredicate = ""
|
pp.nonStretchyHeightPredicate = ""
|
||||||
// this also resets the hugging priority
|
// this also resets the hugging priority
|
||||||
// TODO do this when adding and removing controls instead
|
// TODO do this when adding and removing controls instead
|
||||||
child.c.tFillAutoLayout(pp)
|
child.c.tFillAutoLayout(&pp)
|
||||||
priority = NSLayoutPriorityDefaultHigh // forcibly hug; avoid stretching out
|
priority = myNSLayoutPriorityDefaultHigh // forcibly hug; avoid stretching out
|
||||||
if child.stretchy {
|
if child.stretchy {
|
||||||
priority = NSLayoutPriorityDefaultLow // do not forcibly hug; freely stretch out
|
priority = myNSLayoutPriorityDefaultLow // do not forcibly hug; freely stretch out
|
||||||
}
|
}
|
||||||
if self.vertical {
|
if self.vertical {
|
||||||
predicates.append(pp.nonStretchyHeightPredicate)
|
predicates.append(pp.nonStretchyHeightPredicate)
|
||||||
} else {
|
} else {
|
||||||
predicates.append(pp.nonStretchyWidthPredicate)
|
predicates.append(pp.nonStretchyWidthPredicate)
|
||||||
}
|
}
|
||||||
pp.view.setContentHuggingPriority(priority, forOrientation:orientation)
|
pp.view?.setContentHuggingPriority(priority, forOrientation:orientation)
|
||||||
views[tAutoLayoutKey(n)] = pp.view
|
views[tAutoLayoutKey(n)] = pp.view
|
||||||
n++
|
n++
|
||||||
}
|
}
|
||||||
|
@ -94,12 +122,16 @@ class tBox : tControl {
|
||||||
constraint = "V:"
|
constraint = "V:"
|
||||||
}
|
}
|
||||||
var firstStretchy = true
|
var firstStretchy = true
|
||||||
for i = 0; i < n; i++ {
|
// swift can't tell that nStretchy isn't used until firstStretchy becomes false
|
||||||
|
nStretchy = 0
|
||||||
|
for i in 0..<n {
|
||||||
if self.spaced && i != 0 {
|
if self.spaced && i != 0 {
|
||||||
constraint += "-"
|
constraint += "-"
|
||||||
}
|
}
|
||||||
constraint += "[" + tAutoLayoutKey(i)
|
constraint += "[" + tAutoLayoutKey(i)
|
||||||
if self.children[i].stretchy {
|
// swift currently can't do self.children[i].stretchy
|
||||||
|
var child = self.children[Int(i)]
|
||||||
|
if child.stretchy {
|
||||||
if firstStretchy {
|
if firstStretchy {
|
||||||
firstStretchy = false
|
firstStretchy = false
|
||||||
nStretchy = i
|
nStretchy = i
|
||||||
|
@ -107,53 +139,42 @@ class tBox : tControl {
|
||||||
constraint += "(==" + tAutoLayoutKey(nStretchy) + ")"
|
constraint += "(==" + tAutoLayoutKey(nStretchy) + ")"
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
constraint += predicates[i]
|
constraint += predicates[Int(i)]
|
||||||
}
|
}
|
||||||
constraint += "]"
|
constraint += "]"
|
||||||
}
|
}
|
||||||
constraint += "|"
|
constraint += "|"
|
||||||
self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
|
self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
|
||||||
visualFormat: constraint,
|
constraint,
|
||||||
options: 0,
|
options: NSLayoutFormatOptions(0),
|
||||||
metrics: nil,
|
metrics: nil,
|
||||||
views: views))
|
views: views))
|
||||||
// TODO do not release constraint; it's autoreleased?
|
// TODO do not release constraint; it's autoreleased?
|
||||||
|
|
||||||
// next make the views span the full other dimension
|
// next make the views span the full other dimension
|
||||||
// TODO make all of these the same width/height
|
// TODO make all of these the same width/height
|
||||||
for i = 0; i < n; i++ {
|
for i in 0..<n {
|
||||||
constraint = "V:|["
|
constraint = "V:|["
|
||||||
if self.vertical {
|
if self.vertical {
|
||||||
constraint = "H:|["
|
constraint = "H:|["
|
||||||
}
|
}
|
||||||
constraint += tAutoLayoutKey(i) + "]|"
|
constraint += tAutoLayoutKey(i) + "]|"
|
||||||
self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
|
self.v.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
|
||||||
visualFormat: constraint,
|
constraint,
|
||||||
options: 0,
|
options: NSLayoutFormatOptions(0),
|
||||||
metrics: nil,
|
metrics: nil,
|
||||||
views: views))
|
views: views))
|
||||||
// TODO do not release constraint; it's autoreleased?
|
// TODO do not release constraint; it's autoreleased?
|
||||||
}
|
}
|
||||||
|
|
||||||
// and now populate for self
|
// the caller needs to know if a control was stretchy
|
||||||
selfOnly:
|
// firstStretchy is false if there was one
|
||||||
p.view = self.v
|
return !firstStretchy
|
||||||
p.attachLeft = true
|
|
||||||
p.attachTop = true
|
|
||||||
// don't attach to the end if there weren't any stretchy controls
|
|
||||||
// firstStretchy is false if there was at least one stretchy control
|
|
||||||
if self.vertical {
|
|
||||||
p.attachRight = true
|
|
||||||
p.attachBottom = !firstStretchy
|
|
||||||
} else {
|
|
||||||
p.attachRight = !firstStretchy
|
|
||||||
p.attachBottom = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func tRelayout() {
|
func tRelayout() {
|
||||||
if self.parent != nil {
|
if self.parent != nil {
|
||||||
self.parent.tRelayout()
|
self.parent?.tRelayout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,16 @@ import Cocoa
|
||||||
|
|
||||||
class tButton : tControl {
|
class tButton : tControl {
|
||||||
private var b: NSButton
|
private var b: NSButton
|
||||||
private var parent: tControl
|
private var parent: tControl?
|
||||||
private var horzpri, vertpri: NSLayoutPriority
|
private var horzpri, vertpri: NSLayoutPriority
|
||||||
|
|
||||||
init(text: String) {
|
init(_ text: String) {
|
||||||
self.b = NSButton(frame: NSZeroRect)
|
self.b = NSButton(frame: NSZeroRect)
|
||||||
self.b.title = text
|
self.b.title = text
|
||||||
self.b.buttonType = NSMomentaryPushInButton
|
self.b.setButtonType(NSButtonType.MomentaryPushInButton)
|
||||||
self.b.bordered = true
|
self.b.bordered = true
|
||||||
self.b.bezelStyle = NSRoundedBezelStyle
|
self.b.bezelStyle = NSBezelStyle.RoundedBezelStyle
|
||||||
self.b.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSRegularControlSize))
|
self.b.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSControlSize.RegularControlSize))
|
||||||
self.b.translatesAutoresizingMaskIntoConstraints = false
|
self.b.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
self.parent = nil
|
self.parent = nil
|
||||||
|
@ -26,7 +26,7 @@ class tButton : tControl {
|
||||||
v.addSubview(self.b)
|
v.addSubview(self.b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tFillAutoLayout(p: tAutoLayoutParams) {
|
func tFillAutoLayout(inout p: tAutoLayoutParams) {
|
||||||
// reset the hugging priority
|
// reset the hugging priority
|
||||||
self.b.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
self.b.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
||||||
self.b.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
self.b.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
||||||
|
@ -40,7 +40,7 @@ class tButton : tControl {
|
||||||
|
|
||||||
func tRelayout() {
|
func tRelayout() {
|
||||||
if self.parent != nil {
|
if self.parent != nil {
|
||||||
self.parent.tRelayout()
|
self.parent?.tRelayout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,22 +3,22 @@ import Cocoa
|
||||||
|
|
||||||
// TODO stretchy across both dimensions
|
// TODO stretchy across both dimensions
|
||||||
// for a vertical box, the horizontal width should be variable
|
// for a vertical box, the horizontal width should be variable
|
||||||
class tAutoLayoutParams {
|
struct tAutoLayoutParams {
|
||||||
var view: NSView
|
var view: NSView? = nil
|
||||||
var attachLeft: Bool
|
var attachLeft: Bool = false
|
||||||
var attachTop: Bool
|
var attachTop: Bool = false
|
||||||
var attachRight: Bool
|
var attachRight: Bool = false
|
||||||
var attachBottom: Bool
|
var attachBottom: Bool = false
|
||||||
var nonStretchyWidthPredicate: String
|
var nonStretchyWidthPredicate: String = ""
|
||||||
var nonStretchyHeightPredicate: String
|
var nonStretchyHeightPredicate: String = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol tControl {
|
protocol tControl : class {
|
||||||
mutating func tSetParent(p: tControl, addToView: NSView)
|
func tSetParent(p: tControl, addToView: NSView)
|
||||||
mutating func tFillAutoLayout(p: tAutoLayoutParams)
|
func tFillAutoLayout(inout p: tAutoLayoutParams)
|
||||||
mutating func tRelayout()
|
func tRelayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
func tAutoLayoutKey(n: UIntMax) -> String {
|
func tAutoLayoutKey(n: UIntMax) -> String {
|
||||||
return NSString(format: "view%d", n)
|
return NSString(format: "view%d", n) as String
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,22 @@
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
class tEntry : tControl {
|
class tEntry : tControl {
|
||||||
private var b: NSButton
|
private var t: NSTextField
|
||||||
private var parent: tControl
|
private var parent: tControl?
|
||||||
private var horzpri, vertpri: NSLayoutPriority
|
private var horzpri, vertpri: NSLayoutPriority
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
var cell: NSTextFieldCell
|
||||||
|
|
||||||
self.t = NSTextField(frame: NSZeroRect)
|
self.t = NSTextField(frame: NSZeroRect)
|
||||||
self.t.selectable = true
|
self.t.selectable = true
|
||||||
self.t.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSRegularControlSize))
|
self.t.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSControlSize.RegularControlSize))
|
||||||
self.t.bordered = false
|
self.t.bordered = false
|
||||||
self.t.bezelStyle = NSTextFieldSquareBezel
|
self.t.bezelStyle = NSTextFieldBezelStyle.SquareBezel
|
||||||
self.t.bezeled = true
|
self.t.bezeled = true
|
||||||
self.t.cell.lineBreakMode = NSLineBreakByClipping
|
cell = self.t.cell() as! NSTextFieldCell
|
||||||
self.t.cell.scrollable = true
|
cell.lineBreakMode = NSLineBreakMode.ByClipping
|
||||||
|
cell.scrollable = true
|
||||||
self.t.translatesAutoresizingMaskIntoConstraints = false
|
self.t.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
self.parent = nil
|
self.parent = nil
|
||||||
|
@ -28,7 +31,7 @@ class tEntry : tControl {
|
||||||
v.addSubview(self.t)
|
v.addSubview(self.t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tFillAutoLayout(p: tAutoLayoutParams) {
|
func tFillAutoLayout(inout p: tAutoLayoutParams) {
|
||||||
// reset the hugging priority
|
// reset the hugging priority
|
||||||
self.t.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
self.t.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
||||||
self.t.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
self.t.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
||||||
|
@ -43,7 +46,7 @@ class tEntry : tControl {
|
||||||
|
|
||||||
func tRelayout() {
|
func tRelayout() {
|
||||||
if self.parent != nil {
|
if self.parent != nil {
|
||||||
self.parent.tRelayout()
|
self.parent?.tRelayout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,26 @@
|
||||||
// 31 july 2015
|
// 31 july 2015
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
class tEntry : tControl {
|
class tLabel : tControl {
|
||||||
private var b: NSButton
|
private var t: NSTextField
|
||||||
private var parent: tControl
|
private var parent: tControl?
|
||||||
private var horzpri, vertpri: NSLayoutPriority
|
private var horzpri, vertpri: NSLayoutPriority
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
var cell: NSTextFieldCell
|
||||||
|
|
||||||
self.t = NSTextField(frame: NSZeroRect)
|
self.t = NSTextField(frame: NSZeroRect)
|
||||||
self.t.stringValue = "Label"
|
self.t.stringValue = "Label"
|
||||||
self.t.setEditable = false
|
self.t.editable = false
|
||||||
self.t.selectable = false
|
self.t.selectable = false
|
||||||
self.t.drawsBackground = false
|
self.t.drawsBackground = false
|
||||||
self.t.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSRegularControlSize))
|
self.t.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSControlSize.RegularControlSize))
|
||||||
self.t.bordered = false
|
self.t.bordered = false
|
||||||
self.t.bezelStyle = NSTextFieldSquareBezel
|
self.t.bezelStyle = NSTextFieldBezelStyle.SquareBezel
|
||||||
self.t.bezeled = false
|
self.t.bezeled = false
|
||||||
self.t.cell.lineBreakMode = NSLineBreakByClipping
|
cell = self.t.cell() as! NSTextFieldCell
|
||||||
self.t.cell.scrollable = true
|
cell.lineBreakMode = NSLineBreakMode.ByClipping
|
||||||
|
cell.scrollable = true
|
||||||
self.t.translatesAutoresizingMaskIntoConstraints = false
|
self.t.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
self.parent = nil
|
self.parent = nil
|
||||||
|
@ -31,7 +34,7 @@ class tEntry : tControl {
|
||||||
v.addSubview(self.t)
|
v.addSubview(self.t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tFillAutoLayout(p: tAutoLayoutParams) {
|
func tFillAutoLayout(inout p: tAutoLayoutParams) {
|
||||||
// reset the hugging priority
|
// reset the hugging priority
|
||||||
self.t.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
self.t.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
||||||
self.t.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
self.t.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
||||||
|
@ -45,7 +48,7 @@ class tEntry : tControl {
|
||||||
|
|
||||||
func tRelayout() {
|
func tRelayout() {
|
||||||
if self.parent != nil {
|
if self.parent != nil {
|
||||||
self.parent.tRelayout()
|
self.parent?.tRelayout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ func appLaunched() {
|
||||||
mainwin.tShow()
|
mainwin.tShow()
|
||||||
}
|
}
|
||||||
|
|
||||||
class appDelegate : NSApplicationDelegate {
|
class appDelegate : NSObject, NSApplicationDelegate {
|
||||||
func applicationDidFinishLaunching(note: NSNotification) {
|
func applicationDidFinishLaunching(note: NSNotification) {
|
||||||
appLaunched()
|
appLaunched()
|
||||||
}
|
}
|
||||||
|
@ -84,8 +84,8 @@ func main() {
|
||||||
spaced = Process.arguments.count > 1
|
spaced = Process.arguments.count > 1
|
||||||
|
|
||||||
var app = NSApplication.sharedApplication()
|
var app = NSApplication.sharedApplication()
|
||||||
app.setActivationPolicy(NSApplicationActivationPolicyRegular)
|
app.setActivationPolicy(NSApplicationActivationPolicy.Regular)
|
||||||
app.setDelegate(appDelegate())
|
app.delegate = appDelegate()
|
||||||
app.run()
|
app.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,13 @@ import Cocoa
|
||||||
|
|
||||||
// leave a whole lot of space around the alignment rect, just to be safe
|
// leave a whole lot of space around the alignment rect, just to be safe
|
||||||
class tSpinboxContainer : NSView {
|
class tSpinboxContainer : NSView {
|
||||||
override func alignmentRectInsets() -> NSEdgeInsets {
|
override var alignmentRectInsets: NSEdgeInsets {
|
||||||
|
get {
|
||||||
|
debugPrint("in tSpinboxContainer.alignmentRectInsets")
|
||||||
return NSEdgeInsetsMake(50, 50, 50, 50)
|
return NSEdgeInsetsMake(50, 50, 50, 50)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var nspinbox = 0
|
var nspinbox = 0
|
||||||
|
|
||||||
|
@ -14,10 +17,12 @@ class tSpinbox : tControl {
|
||||||
private var c: tSpinboxContainer
|
private var c: tSpinboxContainer
|
||||||
private var t: NSTextField
|
private var t: NSTextField
|
||||||
private var s: NSStepper
|
private var s: NSStepper
|
||||||
private var parent: tControl
|
private var parent: tControl?
|
||||||
private var horzpri, vertpri: NSLayoutPriority
|
private var horzpri, vertpri: NSLayoutPriority
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
var cell: NSTextFieldCell
|
||||||
|
|
||||||
self.c = tSpinboxContainer(frame: NSZeroRect)
|
self.c = tSpinboxContainer(frame: NSZeroRect)
|
||||||
self.c.translatesAutoresizingMaskIntoConstraints = false
|
self.c.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
|
@ -25,16 +30,17 @@ class tSpinbox : tControl {
|
||||||
self.t.stringValue = "\(nspinbox)"
|
self.t.stringValue = "\(nspinbox)"
|
||||||
nspinbox++
|
nspinbox++
|
||||||
self.t.selectable = true
|
self.t.selectable = true
|
||||||
self.t.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSRegularControlSize))
|
self.t.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSControlSize.RegularControlSize))
|
||||||
self.t.bordered = false
|
self.t.bordered = false
|
||||||
self.t.bezelStyle = NSTextFieldSquareBezel
|
self.t.bezelStyle = NSTextFieldBezelStyle.SquareBezel
|
||||||
self.t.bezeled = true
|
self.t.bezeled = true
|
||||||
self.t.cell.lineBreakMode = NSLineBreakByClipping
|
cell = self.t.cell() as! NSTextFieldCell
|
||||||
self.t.cell.scrollable = true
|
cell.lineBreakMode = NSLineBreakMode.ByClipping
|
||||||
|
cell.scrollable = true
|
||||||
self.t.translatesAutoresizingMaskIntoConstraints = false
|
self.t.translatesAutoresizingMaskIntoConstraints = false
|
||||||
self.c.addSubview(self.t)
|
self.c.addSubview(self.t)
|
||||||
|
|
||||||
self.s = NSStepper(NSZeroFrame)
|
self.s = NSStepper(frame: NSZeroRect)
|
||||||
self.s.increment = 1
|
self.s.increment = 1
|
||||||
self.s.valueWraps = false
|
self.s.valueWraps = false
|
||||||
self.s.autorepeat = true
|
self.s.autorepeat = true
|
||||||
|
@ -46,20 +52,20 @@ nspinbox++
|
||||||
"s": self.s,
|
"s": self.s,
|
||||||
]
|
]
|
||||||
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||||
visualFormat: "H:|[t]-[s]|",
|
"H:|[t]-[s]|",
|
||||||
options: 0,
|
options: NSLayoutFormatOptions(0),
|
||||||
metrics: nil,
|
metrics: nil,
|
||||||
views: views)
|
views: views)
|
||||||
self.c.addConstraints(constraints)
|
self.c.addConstraints(constraints)
|
||||||
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||||
visualFormat: "V:|[t]|",
|
"V:|[t]|",
|
||||||
options: 0,
|
options: NSLayoutFormatOptions(0),
|
||||||
metrics: nil,
|
metrics: nil,
|
||||||
views: views)
|
views: views)
|
||||||
self.c.addConstraints(constraints)
|
self.c.addConstraints(constraints)
|
||||||
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||||
visualFormat: "V:|[s]|",
|
"V:|[s]|",
|
||||||
options: 0,
|
options: NSLayoutFormatOptions(0),
|
||||||
metrics: nil,
|
metrics: nil,
|
||||||
views: views)
|
views: views)
|
||||||
self.c.addConstraints(constraints)
|
self.c.addConstraints(constraints)
|
||||||
|
@ -75,7 +81,7 @@ nspinbox++
|
||||||
v.addSubview(self.c)
|
v.addSubview(self.c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tFillAutoLayout(p: tAutoLayoutParams) {
|
func tFillAutoLayout(inout p: tAutoLayoutParams) {
|
||||||
// reset the hugging priority
|
// reset the hugging priority
|
||||||
self.c.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
self.c.setContentHuggingPriority(self.horzpri, forOrientation:NSLayoutConstraintOrientation.Horizontal)
|
||||||
self.c.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
self.c.setContentHuggingPriority(self.vertpri, forOrientation:NSLayoutConstraintOrientation.Vertical)
|
||||||
|
@ -90,7 +96,7 @@ nspinbox++
|
||||||
|
|
||||||
func tRelayout() {
|
func tRelayout() {
|
||||||
if self.parent != nil {
|
if self.parent != nil {
|
||||||
self.parent.tRelayout()
|
self.parent?.tRelayout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,71 +3,73 @@ import Cocoa
|
||||||
|
|
||||||
// auto layout helpers
|
// auto layout helpers
|
||||||
func tIsAmbiguous(view: NSView, indent: Int) {
|
func tIsAmbiguous(view: NSView, indent: Int) {
|
||||||
var s = string(indent, ' ')
|
var s = String(count: indent, repeatedValue: " " as Character)
|
||||||
debugPrint("\(s) \(view.className) \(view.hasAmbiguousLayout)")
|
debugPrint("\(s) \(view.className) \(view.hasAmbiguousLayout)")
|
||||||
if view.hasAmbiguousLayout {
|
if view.hasAmbiguousLayout {
|
||||||
view.window.visualizeConstraints(view.superview.constraints)
|
view.window?.visualizeConstraints(view.superview!.constraints)
|
||||||
}
|
}
|
||||||
for subview in view.subviews {
|
for subview in view.subviews {
|
||||||
tIsAmbiguous(subview, indent + 1)
|
tIsAmbiguous(subview as! NSView, indent + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class tWindow : tControl {
|
class tWindow : tControl {
|
||||||
private var w: NSWindow
|
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(
|
self.w = NSWindow(
|
||||||
contentRect: NSMakeRect(0, 0, 320, 240),
|
contentRect: NSMakeRect(0, 0, 320, 240),
|
||||||
styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask),
|
styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask),
|
||||||
backing: NSBackingStoreBuffered,
|
backing: NSBackingStoreType.Buffered,
|
||||||
defer: true)
|
defer: true)
|
||||||
self.w.title = "Auto Layout Test"
|
self.w.title = "Auto Layout Test"
|
||||||
self.c = nil
|
self.c = nil
|
||||||
self.margined = false
|
self.margined = false
|
||||||
}
|
}
|
||||||
|
|
||||||
public func tSetControl(c: tControl) {
|
func tSetControl(c: tControl) {
|
||||||
self.c = c
|
self.c = c
|
||||||
self.c.tSetParent(self, addToView: self.w.contentView)
|
// TODO use self.c here
|
||||||
|
c.tSetParent(self, addToView: self.w.contentView as! NSView)
|
||||||
self.tRelayout()
|
self.tRelayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func tSetMargined(m: Bool) {
|
func tSetMargined(m: Bool) {
|
||||||
self.margined = m
|
self.margined = m
|
||||||
self.tRelayout()
|
self.tRelayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func tShow() {
|
func tShow() {
|
||||||
self.w.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
|
self.w.cascadeTopLeftFromPoint(NSMakePoint(20, 20))
|
||||||
self.w.makeKeyAndOrderFront(self)
|
self.w.makeKeyAndOrderFront(self)
|
||||||
tIsAmbiguous(self.w.contentView, 0)
|
tIsAmbiguous(self.w.contentView as! NSView, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tSetParent(p: tControl, addToView: NSView) {
|
func tSetParent(p: tControl, addToView: NSView) {
|
||||||
fatalError("cannot call tWindow.tSetParent()")
|
fatalError("cannot call tWindow.tSetParent()")
|
||||||
}
|
}
|
||||||
|
|
||||||
func tFillAutoLayout(p: tAutoLayoutParams) {
|
func tFillAutoLayout(inout p: tAutoLayoutParams) {
|
||||||
fatalError("cannot call tWindow.tFillAutoLayout()")
|
fatalError("cannot call tWindow.tFillAutoLayout()")
|
||||||
}
|
}
|
||||||
|
|
||||||
public func tRelayout() {
|
func tRelayout() {
|
||||||
if self.c == nil {
|
if self.c == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var contentView = self.w.contentView
|
var contentView = self.w.contentView as! NSView
|
||||||
contentView.removeConstraints(contentView.constraints)
|
contentView.removeConstraints(contentView.constraints)
|
||||||
|
|
||||||
var p = tAutoLayoutParams()
|
var p = tAutoLayoutParams()
|
||||||
self.c.tFillAutoLayout(p)
|
c?.tFillAutoLayout(&p)
|
||||||
|
|
||||||
var views = [
|
// TODO why can't I just say var views = [ "view": p.view ]?
|
||||||
"view": p.view,
|
// I think the parser is getting confused
|
||||||
]
|
var views = [String: NSView]()
|
||||||
|
views["view"] = p.view
|
||||||
var margin = ""
|
var margin = ""
|
||||||
if self.margined {
|
if self.margined {
|
||||||
margin = "-"
|
margin = "-"
|
||||||
|
@ -84,8 +86,8 @@ class tWindow : tControl {
|
||||||
constraint += margin + "|"
|
constraint += margin + "|"
|
||||||
}
|
}
|
||||||
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
var constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||||
visualFormat:constraint,
|
constraint,
|
||||||
options:0,
|
options: NSLayoutFormatOptions(0),
|
||||||
metrics: nil,
|
metrics: nil,
|
||||||
views: views)
|
views: views)
|
||||||
contentView.addConstraints(constraints)
|
contentView.addConstraints(constraints)
|
||||||
|
@ -99,8 +101,8 @@ class tWindow : tControl {
|
||||||
constraint += margin + "|"
|
constraint += margin + "|"
|
||||||
}
|
}
|
||||||
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
constraints = NSLayoutConstraint.constraintsWithVisualFormat(
|
||||||
visualFormat:constraint,
|
constraint,
|
||||||
options:0,
|
options: NSLayoutFormatOptions(0),
|
||||||
metrics: nil,
|
metrics: nil,
|
||||||
views: views)
|
views: views)
|
||||||
contentView.addConstraints(constraints)
|
contentView.addConstraints(constraints)
|
||||||
|
|
Loading…
Reference in New Issue