Created
February 19, 2014 14:14
-
-
Save fjolnir/9092947 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "AppKit" | |
@NSString { | |
- trimmed `self stringByTrimmingCharactersInSet: NSCharacterSet whitespaceAndNewlineCharacterSet` | |
} | |
@NSBlock { | |
- applicationDidFinishLaunching: app `self call` | |
} | |
@NSApplication { | |
+ run: readyBlock `self sharedApplication run:readyBlock` | |
- run: readyBlock { | |
@readyBlock = readyBlock | |
self setDelegate: readyBlock; | |
run | |
} | |
- activateAndRun { | |
self setActivationPolicy: NSApplicationActivationPolicyRegular; | |
activateIgnoringOtherApps: yes; | |
run | |
} | |
} | |
@NSWindow { | |
+ resizableWindow `self new setStyleMask: 0b1111; self` | |
} | |
@NSMenu { | |
+ withItems: items { | |
NSMenu new addItems: (items map: { info | | |
if (info isKindOfClass:NSString) and (info hasPrefix: #---) then | |
NSMenuItem separatorItem | |
else { | |
item = NSMenuItem new setTitle: info[#title]; | |
setKeyEquivalent: info[#key] || ""; | |
self | |
item setSubmenu: (NSMenu withItems: info[#items]) if info[#items] | |
if info[#action] { | |
item setAction: #call; | |
setTarget: info[#action] copy | |
} | |
^item | |
} | |
}); self | |
} | |
+ withItem: item `self withItems:[item]` | |
- addItems: items { | |
items each: `item | self addItem:item` | |
} | |
} | |
@NSView { | |
+ withSubviews: subviews [constraints: constraints] { | |
ret = self new | |
subviews each: { pair | | |
pair right setTranslatesAutoresizingMaskIntoConstraints: no | |
ret addSubview: pair right | |
} | |
(constraints componentsSeparatedByString: ";") each: { constraint | | |
constraint = constraint trimmed | |
opts = 0 | |
if constraint hasPrefix: #L { | |
opts = NSLayoutFormatAlignAllLeading | |
constraint = constraint substringFromIndex:1 | |
} else if constraint hasPrefix: #T { | |
opts = NSLayoutFormatAlignAllTrailing | |
constraint = constraint substringFromIndex:1 | |
} | |
if constraint hasPrefix: #B { | |
opts = opts bitAnd: NSLayoutFormatAlignAllBaseline | |
constraint = constraint substringFromIndex:1 | |
} | |
c = NSLayoutConstraint constraintsWithVisualFormat: constraint | |
options: opts | |
metrics: nil | |
views: subviews dictionaryRepresentation | |
c each: `c| ret addConstraint: c` | |
} | |
^ret | |
} | |
} | |
@NSButton { | |
+ pushButton `self new setBezelStyle: NSRoundedBezelStyle; self` | |
} | |
@NSTextField { | |
+ labelWithText: text | |
{ | |
style = NSParagraphStyle defaultParagraphStyle mutableCopy | |
style setAlignment: NSCenterTextAlignment | |
str = NSAttributedString alloc initWithString: text attributes: { | |
NSParagraphStyleAttributeName => style, | |
NSFontTraitsAttribute => { | |
NSFontWeightTrait => 1 | |
} dictionaryRepresentation | |
} dictionaryRepresentation | |
self new setDrawsBackground: no; | |
setBordered: no; | |
setEditable: no; | |
setSelectable: no; | |
setStringValue: str; | |
self | |
} | |
} | |
appName = NSProcessInfo processInfo processName | |
app = NSApplication sharedApplication | |
app setMainMenu: (NSMenu withItem: { | |
#title => appName, #items => [ | |
{ | |
#title => "About «appName»", | |
#action => `app orderFrontStandardAboutPanel:nil` | |
}, | |
#---, | |
{ | |
#title => "Quit «appName»", | |
#key => #q, | |
#action => `app terminate:nil` | |
} | |
] | |
}) | |
contentView = NSView withSubviews: { | |
#button => NSButton pushButton setTitle: "Click me"; self, | |
#label => NSTextField labelWithText: "Click below" | |
} constraints: " | |
LV:|-10-[label]-10-[button]-(15)-|; | |
H:[button(100)]; | |
H:|-20-[label(==button)]-20-| | |
" | |
NSWindow resizableWindow setTitle: "Test"; | |
setContentView: contentView; | |
makeKeyAndOrderFront: nil | |
app activateAndRun |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment