Skip to content

Instantly share code, notes, and snippets.

@skahack
Created April 7, 2011 20:55
Show Gist options
  • Save skahack/908695 to your computer and use it in GitHub Desktop.
Save skahack/908695 to your computer and use it in GitHub Desktop.
Statusbar application sample
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>statusbar</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
CC = macrubyc
SO = statusbar.rb
DIR = statusbar.app
BIN = statusbar
statusbar: $(SO)
$(CC) $(SO) -o $(BIN)
mkdir -p $(DIR)
mv statusbar $(DIR)/
cp Info.plist $(DIR)/
rm *.o
#!/usr/bin/env macruby
framework 'Cocoa'
class StatusbarIcon < NSView
def initialize(frame, controller)
@highlight = false
@controller = controller
initWithFrame(frame)
end
def drawRect(rect)
if @highlight
NSColor.selectedMenuItemColor.set
NSRectFill(rect)
end
text = "S"
textColor = NSColor.controlTextColor
if @highlight
textColor = NSColor.selectedMenuItemTextColor
end
msgFont = NSFont.menuBarFontOfSize 15.0
paraStyle = NSMutableParagraphStyle.alloc.init;
paraStyle.setParagraphStyle(NSParagraphStyle.defaultParagraphStyle)
paraStyle.setAlignment(NSCenterTextAlignment)
paraStyle.setLineBreakMode(NSLineBreakByTruncatingTail)
msgAttrs = {
NSFontAttributeName => msgFont,
NSForegroundColorAttributeName => textColor,
NSParagraphStyleAttributeName => paraStyle,
}
msgSize = text.sizeWithAttributes(msgAttrs)
msgRect = [
(frame.size.width - msgSize.width) / 2.0,
(frame.size.height - msgSize.height) / 2.0,
msgSize.width,
msgSize.height
]
text.drawInRect(msgRect, withAttributes:msgAttrs)
end
def mouseDown(event)
@controller.toggleMainWindow(self.window.frame)
end
def show
@highlight = true
setNeedsDisplay true
end
def hide
@highlight = false
setNeedsDisplay true
end
end
class MainWindow < NSWindow
def init(frame, controller)
@controller = controller
width = 300.0
height = 300.0
rect = [
frame.origin.x,
frame.origin.y - height,
width,
height
]
window = self.initWithContentRect rect,
styleMask:NSBorderlessWindowMask,
backing:NSBackingStoreBuffered,
defer:false
window.level = NSStatusWindowLevel
window.hasShadow = true
window.delegate = self
button = NSButton.alloc.initWithFrame([80, 150, 120, 90])
button.bezelStyle = 4
button.title = 'Quit'
button.target = self
button.action = 'quit:'
window.contentView.addSubview(button)
window
end
def show
makeKeyAndOrderFront self
NSApplication.sharedApplication.activateIgnoringOtherApps true
end
def hide
orderOut self
end
def canBecomeKeyWindow
true
end
def resignKeyWindow
@controller.toggleMainWindow
end
def quit(sender)
NSApp.terminate nil
end
end
class App
attr_accessor :main_window, :statusbar_icon
def initialize
bar = NSStatusBar.systemStatusBar
item = bar.statusItemWithLength(NSVariableStatusItemLength)
@statusbar_icon = StatusbarIcon.new([0, 0, 30.0, bar.thickness], self)
item.view = @statusbar_icon
item.highlightMode = true
@barItem = item
end
def toggleMainWindow(frame = nil)
if @main_window == nil
@main_window = MainWindow.alloc.init(frame, self)
@main_window.show
@statusbar_icon.show
else
@main_window.hide
@statusbar_icon.hide
@main_window = nil
end
end
end
app = NSApplication.sharedApplication
NSApp.setActivationPolicy NSApplicationActivationPolicyAccessory
w = App.new
app.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment