Skip to content

Instantly share code, notes, and snippets.

@dbrajkovic
Created March 22, 2011 16:24
Show Gist options
  • Save dbrajkovic/881505 to your computer and use it in GitHub Desktop.
Save dbrajkovic/881505 to your computer and use it in GitHub Desktop.
/*!
@class STWindow.j
@abstract Custom CPWindow class for apps
@detail STWindow is the default window class for apps in StockTwits Desktop. This window class is made to be subclassed.
To enable the menu items, window controllers of the subclass must implement -newWindow: and -showPreferences:
*/
@import <AppKit/AppKit.j>
@import <Foundation/Foundation.j>
@implementation STWindow : CPWindow
{
@outlet CPButton _actionButton; /// A reference to the action menu button in the top bar
@outlet CPMenu theMenu; /// A reference to the menu that displays when the action menu button is clicked
}
#pragma mark -
#pragma mark Initialization
/*!
@param aRect a rectangle that determines the size of the window
@param aMask the style mask to apply to the window
@abstract Designated initializer
@discussion This method sets up a default window with a custom view
@returns self
*/
- (id)initWithContentRect:(CGRect)aRect styleMask:(int)aMask
{
if (self = [super initWithContentRect:aRect styleMask:aMask])
{
var bundle = [CPBundle mainBundle],
buttonClose = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"STActionTemplate.png"] size:CPSizeMake(15.0, 15.0)],
buttonClosePressed = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"STActionTemplate-Pressed.png"] size:CPSizeMake(15.0, 15.0)];
_actionButton = [[STActionButton alloc] initWithFrame:CPRectMake(CGRectGetWidth([[self contentView] bounds]) - 24.0, 6.75, 15.0, 15.0)];
[_actionButton setImageScaling:CPScaleProportionally];
[_actionButton setBordered:NO];
[_actionButton setImage:buttonClose];
[_actionButton setValue:buttonClose forThemeAttribute:@"image"];
[_actionButton setValue:buttonClosePressed forThemeAttribute:@"image" inState:CPThemeStateHighlighted];
[_actionButton setAutoresizingMask:CPViewMinXMargin | CPViewMaxYMargin];
[[[self contentView] superview] addSubview:_actionButton];
[self setMovableByWindowBackground:YES];
}
return self;
}
@end
/*!
@class STActionButton
@abstract A subclass of CPButton used as the action menu button
*/
@implementation STActionButton : CPButton
{
}
#pragma mark -
#pragma mark Initialization
/*!
Designated initalizer
@param aFrame: a CGRect with dimensions for the frame
@returns self
*/
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame]) {
}
return self;
}
/*!
Override the defualt behavior on mouse down to create theMenu
@param theEvent:a CPEvent that gets passed in by the framework
@returns none
*/
- (void)mouseDown:(CPEvent)theEvent
{
theMenu = [[CPMenu alloc] initWithTitle:@"STWindowMenu"];
var menuItem1 = [theMenu insertItemWithTitle:@"Preferences…" action:@selector(showPreferences:) keyEquivalent:@"p" atIndex:0];
var menuItem2 = [theMenu insertItemWithTitle:@"New Window" action:@selector(newWindow:) keyEquivalent:@"n" atIndex:1];
[menuItem1 setTarget:[[[self superview] superview] windowController]];
[menuItem2 setTarget:[[[self superview] superview] windowController]];
[CPMenu popUpContextMenu:theMenu withEvent:theEvent forView:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment