AppletKit.js
ObjC.import('Cocoa')
function Url(str) {
return $.NSURL.URLWithString(str)
}
function Req(url) {
return $.NSURLRequest.requestWithURL(url)
}
function Button(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 90, 26)
let b = $.NSButton.alloc.initWithFrame(r)
b.bezelStyle = $.NSRoundedBezelStyle
fun(b)
return b
}
function CheckBoxButton(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 26, 26)
let b = $.NSButton.alloc.initWithFrame(r)
b.buttonType = $.NSSwitchButton
fun(b)
return b
}
function RadioButton(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 26, 26)
let b = $.NSButton.alloc.initWithFrame(r)
b.buttonType = $.NSRadioButton
fun(b)
return b
}
function PopUpButton(fun = ()=>{}) {
let p = $.NSPopUpButton.alloc.initWithFrame($.NSMakeRect(0, 0, 90, 26))
fun(p)
return b
}
function TextField(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 200, 22)
let t = $.NSTextField.alloc.initWithFrame(r)
fun(t)
return t
}
function Label(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 200, 22)
let t = $.NSTextField.alloc.initWithFrame(r)
t.drawsBackground = false
t.bordered = false
t.editable = false
t.selectable = true
fun(t)
return t
}
function WrappingTextField(fun = ()=>{}) {
if (!$.AppletKitControlTextEditingDelegate) ObjC.registerSubclass({
name: 'AppletKitControlTextEditingDelegate',
protocols: ['NSControlTextEditingDelegate'],
methods: {
'control:textView:doCommandBySelector:': function(control, textView, commandSelector) {
let result = false
if (commandSelector == 'insertNewline:') {
textView.insertNewlineIgnoringFieldEditor(this)
return true
}
if (commandSelector == 'insertTab:') {
textView.insertTabIgnoringFieldEditor(this)
return true
}
return result
},
},
})
let r = $.NSMakeRect(0, 0, 26, 26)
let t = $.NSTextField.alloc.initWithFrame(r)
t.delegate = $.AppletKitControlTextEditingDelegate.alloc.init
t.cell.wraps = true
t.cell.lineBreakMode = $.NSLineBreakByWordWrapping
t.cell.usesSingleLineMode = false
fun(t)
return t
}
function TextDatePicker(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 26, 26)
let p = $.NSDatePicker.alloc.initWithFrame(r)
p.drawsBackground = true
p.datePickerElements = $.NSYearMonthDayDatePickerElementFlag
p.datePickerStyle = $.NSTextFieldAndStepperDatePickerStyle
p.dateValue = $.NSDate.date
fun(p)
return p
}
function GraphicalDatePicker(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 26, 26)
let p = $.NSDatePicker.alloc.initWithFrame(r)
p.drawsBackground = true
p.datePickerElements = $.NSYearMonthDayDatePickerElementFlag
p.datePickerStyle = $.NSClockAndCalendarDatePickerStyle
p.dateValue = $.NSDate.date
fun(p)
return p
}
function Line(fun = ()=>{}) {
let r = $.NSMakeRect(0, 0, 0, 0)
let b = $.NSBox.alloc.initWithFrame(r)
b.boxType = $.NSBoxSeparator
fun(b)
return b
}
function View(fun = ()=>{}) {
let v = $.NSView.alloc.initWithFrame($.NSMakeRect(0, 0, 0, 0))
fun(v)
return v
}
function WebView(fun = ()=>{}) {
ObjC.import('WebKit')
let r = $.NSMakeRect(0, 0, 0, 0)
let c = $.WKWebViewConfiguration.alloc.init
let w = $.WKWebView.alloc.initWithFrameConfiguration(r, c)
fun(w)
return w
}
function ShoeboxWindow(name, methods) {
let _methods = {}
for (let m in methods) {
_methods[m+':'] = {
types: ['void', ['id']],
implementation: methods[m],
}
}
_methods.init = function () {
let _this = ObjC.super(this).initWithContentRectStyleMaskBackingDefer(
$.NSMakeRect(0, 0, 0, 0),
$.NSTitledWindowMask | $.NSClosableWindowMask | $.NSMiniaturizableWindowMask | $.NSResizableWindowMask,
$.NSBackingStoreBuffered,
false
)
if (_this != undefined) {
this.releasedWhenClosed = false
if (_this.setup) _this.setup($())
}
return _this
}
ObjC.registerSubclass({
name: name,
superclass: 'NSWindow',
methods: _methods,
})
}
function UtilityWindow(name, methods) {
registAppletKitWindowDelegate()
let _methods = {}
for (let m in methods) {
_methods[m+':'] = {
types: ['void', ['id']],
implementation: methods[m],
}
}
_methods.init = function () {
let _this = ObjC.super(this).initWithContentRectStyleMaskBackingDefer(
$.NSMakeRect(0, 0, 100, 100),
$.NSTitledWindowMask | $.NSClosableWindowMask | $.NSMiniaturizableWindowMask,
$.NSBackingStoreBuffered,
false
)
if (_this != undefined) {
_this.delegate = $.AppletKitWindowDelegate.alloc.init
if (_this.setup) _this.setup($())
}
return _this
}
ObjC.registerSubclass({
name: name,
superclass: 'NSWindow',
methods: _methods,
})
}
function registAppletKitWindowDelegate() {
if (!$.AppletKitWindowDelegate) ObjC.registerSubclass({
name:'AppletKitWindowDelegate',
protocols: ['NSWindowDelegate'],
methods: {
'windowWillClose:'(notification) {
$.NSApplication.sharedApplication.terminate($())
}
}
})
}