Skip to content

Instantly share code, notes, and snippets.

@alloy
Created March 30, 2011 09:10
Show Gist options
  • Save alloy/894104 to your computer and use it in GitHub Desktop.
Save alloy/894104 to your computer and use it in GitHub Desktop.
A MacRuby example of a custom view in the status bar, with selection highlight.
class StatusBarView < NSView
attr_reader :progressIndicator, :statusBarItem
def initWithStatusBarItem(item)
bar = item.statusBar
if initWithFrame([[0,0], [bar.thickness, bar.thickness]])
@statusBarItem = item
@highlight = false
origin, size = frame.origin, frame.size
@progressIndicator = NSProgressIndicator.alloc.initWithFrame([[origin.x + 2, origin.x + 2], [size.width - 4, size.height - 4]])
@progressIndicator.style = NSProgressIndicatorSpinningStyle
addSubview(@progressIndicator)
@menu = NSMenu.alloc.init
@menu.delegate = self
@menu.addItemWithTitle('Quit App', action: 'terminate:', keyEquivalent: '')
@statusBarItem.view = self
self
end
end
def menuWillOpen(_)
@highlight = true
self.needsDisplay = true
end
def menuDidClose(_)
@highlight = false
self.needsDisplay = true
end
def mouseDown(event)
@statusBarItem.popUpStatusItemMenu(@menu)
end
def drawRect(rect)
@statusBarItem.drawStatusBarBackgroundInRect(bounds, withHighlight: @highlight) if @statusBarItem
end
def startAnimation
@progressIndicator.startAnimation(self)
end
def stopAnimation
@progressIndicator.stopAnimation(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment