libui/redo/osxaltest/spinbox.swift

94 lines
2.6 KiB
Swift
Raw Normal View History

2015-08-07 19:25:18 -05:00
// 31 july 2015
import Cocoa
var nspinbox = 0
class Spinbox : NSView, Control {
private var t: NSTextField
private var s: NSStepper
private var parent: Control?
init() {
var cell: NSTextFieldCell
self.t = NSTextField(frame: NSZeroRect)
self.t.stringValue = "\(nspinbox)"
nspinbox++
self.t.selectable = true
self.t.font = NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSControlSize.RegularControlSize))
self.t.bordered = false
self.t.bezelStyle = NSTextFieldBezelStyle.SquareBezel
self.t.bezeled = true
cell = self.t.cell() as! NSTextFieldCell
cell.lineBreakMode = NSLineBreakMode.ByClipping
cell.scrollable = true
self.t.translatesAutoresizingMaskIntoConstraints = false
// make the textbox grow horizontally and vertically
setHorzHuggingPri(self.t, myNSLayoutPriorityDefaultLow)
setVertHuggingPri(self.t, myNSLayoutPriorityDefaultLow)
2015-08-07 19:25:18 -05:00
self.s = NSStepper(frame: NSZeroRect)
self.s.increment = 1
self.s.valueWraps = false
self.s.autorepeat = true
self.s.translatesAutoresizingMaskIntoConstraints = false
// make the spinner grow vertically but not horizontally
// TODO Required instead?
setHorzHuggingPri(self.s, myNSLayoutPriorityDefaultHigh)
setVertHuggingPri(self.s, myNSLayoutPriorityDefaultLow)
2015-08-07 19:25:18 -05:00
self.parent = nil
super.init(frame: NSZeroRect)
self.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.t)
self.addSubview(self.s)
var views = [
"t": self.t,
"s": self.s,
]
var constraints = mkconstraints("H:|[t]-[s]|", nil, views)
2015-08-07 19:25:18 -05:00
self.addConstraints(constraints)
constraints = mkconstraints("V:|[t]|", nil, views)
2015-08-07 19:25:18 -05:00
self.addConstraints(constraints)
constraints = mkconstraints("V:|[s]|", nil, views)
2015-08-07 19:25:18 -05:00
self.addConstraints(constraints)
}
required init?(coder: NSCoder) {
fatalError("can't use this constructor, sorry")
}
// TODO leave only the required amount of space around the alignment rect
// TODO even with this the stepper sometimes gets cut off at the bottom *anyway*
2015-08-07 19:25:18 -05:00
override var alignmentRectInsets: NSEdgeInsets {
get {
return NSEdgeInsetsMake(50, 50, 50, 50)
}
}
func View() -> NSView {
return self
}
func SetParent(p: Control) {
self.parent = p
}
2015-08-08 22:33:56 -05:00
// TODO justify this
// TODO no really, is this height the right way to go?
2015-08-09 00:51:13 -05:00
// TODO in particular, why is making the NSTextField an Entry insufficient?
// TODO restrict width to the text field only?
2015-08-08 22:33:56 -05:00
override var intrinsicContentSize: NSSize {
get {
var s = super.intrinsicContentSize
s.width = 96
s.height = max(self.t.intrinsicContentSize.height, self.s.intrinsicContentSize.height)
2015-08-08 22:33:56 -05:00
return s
}
}
}