Added a number of customization options to the main menubar.

Reviewed by me.
This commit is contained in:
Francisco Ryan Tolmasky I
2008-11-06 00:12:47 -08:00
parent 6cff27c7bb
commit edd49f222e
5 changed files with 148 additions and 25 deletions
+80 -8
View File
@@ -25,7 +25,6 @@
@import <Foundation/CPNotificationCenter.j>
@import <Foundation/CPString.j>
@import "_CPImageAndTitleView.j"
@import "CPApplication.j"
@import "CPClipView.j"
@import "CPMenuItem.j"
@@ -46,6 +45,7 @@ var _CPMenuBarVisible = NO,
_CPMenuBarTitle = @"",
_CPMenuBarIconImage = nil,
_CPMenuBarIconImageAlphaValue = 1.0,
_CPMenuBarAttributes = nil,
_CPMenuBarSharedWindow = nil;
/*! @class CPMenu
@@ -93,6 +93,10 @@ var _CPMenuBarVisible = NO,
[_CPMenuBarSharedWindow setIconImage:_CPMenuBarIconImage];
[_CPMenuBarSharedWindow setIconImageAlphaValue:_CPMenuBarIconImageAlphaValue];
[_CPMenuBarSharedWindow setColor:[_CPMenuBarAttributes objectForKey:@"CPMenuBarBackgroundColor"]];
[_CPMenuBarSharedWindow setTextColor:[_CPMenuBarAttributes objectForKey:@"CPMenuBarTextColor"]];
[_CPMenuBarSharedWindow setTitleColor:[_CPMenuBarAttributes objectForKey:@"CPMenuBarTitleColor"]];
[_CPMenuBarSharedWindow orderFront:self];
}
else
@@ -126,6 +130,41 @@ var _CPMenuBarVisible = NO,
return _CPMenuBarImage;
}
+ (void)setMenuBarAttributes:(CPDictionary)attributes
{
if (_CPMenuBarAttributes == attributes)
return;
_CPMenuBarAttributes = [attributes copy];
var textColor = [attributes objectForKey:@"CPMenuBarTextColor"],
titleColor = [attributes objectForKey:@"CPMenuBarTitleColor"];
if (!textColor && titleColor)
[_CPMenuBarAttributes setObject:titleColor forKey:@"CPMenuBarTextColor"];
else if (textColor && !titleColor)
[_CPMenuBarAttributes setObject:textColor forKey:@"CPMenuBarTitleColor"];
else if (!textColor && !titleColor)
{
[_CPMenuBarAttributes setObject:[CPColor blackColor] forKey:@"CPMenuBarTextColor"];
[_CPMenuBarAttributes setObject:[CPColor blackColor] forKey:@"CPMenuBarTitleColor"];
}
if (_CPMenuBarSharedWindow)
{
[_CPMenuBarSharedWindow setColor:[_CPMenuBarAttributes objectForKey:@"CPMenuBarBackgroundColor"]];
[_CPMenuBarSharedWindow setTextColor:[_CPMenuBarAttributes objectForKey:@"CPMenuBarTextColor"]];
[_CPMenuBarSharedWindow setTitleColor:[_CPMenuBarAttributes objectForKey:@"CPMenuBarTitleColor"]];
}
}
+ (CPDictionary)menuBarAttributes
{
return _CPMenuBarAttributes;
}
+ (void)_setMenuBarIconImageAlphaValue:(float)anAlphaValue
{
_CPMenuBarIconImageAlphaValue = anAlphaValue;
@@ -1312,6 +1351,9 @@ var _CPMenuBarWindowBackgroundColor = nil,
CPImageView _iconImageView;
CPTextField _titleField;
CPColor _textColor;
CPColor _titleColor;
}
+ (void)initialize
@@ -1321,8 +1363,6 @@ var _CPMenuBarWindowBackgroundColor = nil,
var bundle = [CPBundle bundleForClass:self];
_CPMenuBarWindowBackgroundColor = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"_CPMenuBarWindow/_CPMenuBarWindowBackground.png"] size:CGSizeMake(1.0, 18.0)]];
_CPMenuBarWindowFont = [CPFont systemFontOfSize:11.0];
}
@@ -1340,7 +1380,6 @@ var _CPMenuBarWindowBackgroundColor = nil,
var contentView = [self contentView];
[contentView setBackgroundColor:_CPMenuBarWindowBackgroundColor];
[contentView setAutoresizesSubviews:NO];
[self setBecomesKeyOnlyIfNeeded:YES];
@@ -1350,11 +1389,9 @@ var _CPMenuBarWindowBackgroundColor = nil,
[contentView addSubview:_iconImageView];
_titleField = [[_CPImageAndTitleView alloc] initWithFrame:CGRectMakeZero()];
_titleField = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[_titleField setFont:[CPFont boldSystemFontOfSize:12.0]];
[_titleField setImagePosition:CPImageLeft];
[_titleField setAlignment:CPCenterTextAlignment];
[contentView addSubview:_titleField];
@@ -1376,7 +1413,7 @@ var _CPMenuBarWindowBackgroundColor = nil,
document.title = bundleName;
#endif
[_titleField setTitle:aTitle];
[_titleField setStringValue:aTitle];
[_titleField sizeToFit];
[self tile];
@@ -1395,6 +1432,39 @@ var _CPMenuBarWindowBackgroundColor = nil,
[_iconImageView setAlphaValue:anAlphaValue];
}
- (void)setColor:(CPColor)aColor
{
if (!aColor)
{
if (!_CPMenuBarWindowBackgroundColor)
_CPMenuBarWindowBackgroundColor = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[_CPMenuBarWindow class]] pathForResource:@"_CPMenuBarWindow/_CPMenuBarWindowBackground.png"] size:CGSizeMake(1.0, 18.0)]];
[[self contentView] setBackgroundColor:_CPMenuBarWindowBackgroundColor];
}
else
[[self contentView] setBackgroundColor:aColor];
}
- (void)setTextColor:(CPColor)aColor
{
if (_textColor == aColor)
return;
_textColor = aColor;
[_menuItemViews makeObjectsPerformSelector:@selector(setTextColor:) withObject:_textColor];
}
- (void)setTitleColor:(CPColor)aColor
{
if (_titleColor == aColor)
return;
_titleColor = aColor;
[_titleField setTextColor:aColor ? aColor : [CPColor blackColor]];
}
- (void)setMenu:(CPMenu)aMenu
{
if (_menu == aMenu)
@@ -1465,6 +1535,7 @@ var _CPMenuBarWindowBackgroundColor = nil,
[menuItemView setShowsStateColumn:NO];
[menuItemView setBelongsToMenuBar:YES];
[menuItemView setFont:_CPMenuBarWindowFont];
[menuItemView setTextColor:_textColor];
[menuItemView setHidden:[item isHidden]];
[menuItemView synchronizeWithMenuItem];
@@ -1497,6 +1568,7 @@ var _CPMenuBarWindowBackgroundColor = nil,
[menuItemView setShowsStateColumn:NO];
[menuItemView setBelongsToMenuBar:YES];
[menuItemView setFont:_CPMenuBarWindowFont];
[menuItemView setTextColor:_textColor];
[menuItemView setHidden:[menuItem isHidden]];
[menuItemView synchronizeWithMenuItem];
+67 -17
View File
@@ -843,6 +843,7 @@ var _CPMenuItemSelectionColor = nil,
CPMenuItem _menuItem;
CPFont _font;
CPColor _textColor;
CGSize _minSize;
BOOL _isDirty;
@@ -851,7 +852,7 @@ var _CPMenuItemSelectionColor = nil,
CPImageView _stateView;
_CPImageAndTitleView _imageAndTitleView;
CPImageView _submenuImageView;
CPView _submenuView;
}
+ (void)initialize
@@ -923,8 +924,8 @@ var _CPMenuItemSelectionColor = nil,
[_stateView removeFromSuperview];
_stateView = nil;
[_submenuImageView removeFromSuperview];
_submenuImageView = nil;
[_submenuView removeFromSuperview];
_submenuView = nil;
_minSize = [view frame].size;
@@ -985,11 +986,11 @@ var _CPMenuItemSelectionColor = nil,
if (!font)
font = _font;
[_imageAndTitleView setFont:font];
[_imageAndTitleView setImage:[_menuItem image]];
[_imageAndTitleView setTitle:[_menuItem title]];
[_imageAndTitleView setTextColor:[_menuItem isEnabled] ? [CPColor blackColor] : [CPColor darkGrayColor]];
[_imageAndTitleView setTextColor:[self textColor]];
[_imageAndTitleView setFrameOrigin:CGPointMake(x, 0.0)];
[_imageAndTitleView sizeToFit];
@@ -1001,24 +1002,24 @@ var _CPMenuItemSelectionColor = nil,
// Submenu Arrow
if ([_menuItem hasSubmenu])
{
if (!_submenuImageView)
if (!_submenuView)
{
_submenuImageView = [[CPImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 10.0)];
_submenuView = [[_CPMenuItemArrowView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 10.0)];
[self addSubview:_submenuImageView];
[self addSubview:_submenuView];
}
if (_belongsToMenuBar && !_CPMenuItemViewMenuBarArrowImage)
_CPMenuItemViewMenuBarArrowImage = [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[_CPMenuItemView class]] pathForResource:@"_CPMenuItemView/_CPMenuItemViewMenuBarArrow.png"] size:CGSizeMake(10.0, 10.0)];
[_submenuImageView setHidden:NO];
[_submenuImageView setImage:_belongsToMenuBar ? _CPMenuItemViewMenuBarArrowImage : nil];
[_submenuImageView setFrameOrigin:CGPointMake(x, (CGRectGetHeight(frame) - 10.0) / 2.0)];
[_submenuView setHidden:NO];
[_submenuView setColor:_belongsToMenuBar ? [self textColor] : nil];
[_submenuView setFrameOrigin:CGPointMake(x, (CGRectGetHeight(frame) - 10.0) / 2.0)];
x += 10.0;
}
else
[_submenuImageView setHidden:YES];
[_submenuView setHidden:YES];
_minSize = CGSizeMake(x + (_belongsToMenuBar ? 0.0 : RIGHT_MARGIN), CGRectGetHeight(frame));
@@ -1052,7 +1053,7 @@ var _CPMenuItemSelectionColor = nil,
else
{
[_imageAndTitleView setTextColor:shouldHighlight ? [CPColor whiteColor] : [CPColor blackColor]];
[_imageAndTitleView setTextColor:shouldHighlight ? [CPColor whiteColor] : [self textColor]];
if (shouldHighlight)
[self setBackgroundColor:_CPMenuItemSelectionColor];
@@ -1083,12 +1084,12 @@ var _CPMenuItemSelectionColor = nil,
_CPMenuItemViewMenuBarArrowActivatedImage = [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[_CPMenuItemView class]] pathForResource:@"_CPMenuItemView/_CPMenuItemViewMenuBarArrowActivated.png"] size:CGSizeMake(10.0, 10.0)];
[_imageAndTitleView setTextColor:[CPColor whiteColor]];
[_submenuImageView setImage:_CPMenuItemViewMenuBarArrowActivatedImage];
[_submenuView setColor:[CPColor whiteColor]];
}
else
{
[_imageAndTitleView setTextColor:[CPColor blackColor]];
[_submenuImageView setImage:_CPMenuItemViewMenuBarArrowImage];
[_imageAndTitleView setTextColor:[self textColor]];
[_submenuView setColor:[self textColor]];
}
}
@@ -1097,7 +1098,7 @@ var _CPMenuItemSelectionColor = nil,
if (![_menuItem hasSubmenu])
return NO;
return CGRectContainsPoint([_submenuImageView frame], [self convertPoint:[anEvent locationInWindow] fromView:nil]);
return CGRectContainsPoint([_submenuView frame], [self convertPoint:[anEvent locationInWindow] fromView:nil]);
}
- (BOOL)isHidden
@@ -1120,5 +1121,54 @@ var _CPMenuItemSelectionColor = nil,
[self setDirty];
}
- (void)setTextColor:(CPColor)aColor
{
if (_textColor == aColor)
return;
_textColor = aColor;
[_imageAndTitleView setTextColor:[self textColor]];
[_submenuView setColor:[self textColor]];
}
- (CPColor)textColor
{
return [_menuItem isEnabled] ? (_textColor ? _textColor : [CPColor blackColor]) : [CPColor darkGrayColor];
}
@end
@implementation _CPMenuItemArrowView : CPView
{
CPColor _color;
}
- (void)setColor:(CPColor)aColor
{
if (_color == aColor)
return;
_color = aColor;
[self setNeedsDisplay:YES];
}
- (void)drawRect:(CGRect)aRect
{
var context = [[CPGraphicsContext currentContext] graphicsPort];
CGContextBeginPath(context);
CGContextMoveToPoint(context, 1.0, 4.0);
CGContextAddLineToPoint(context, 9.0, 4.0);
CGContextAddLineToPoint(context, 5.0, 8.0);
CGContextAddLineToPoint(context, 1.0, 4.0);
CGContextClosePath(context);
CGContextSetFillColor(context, _color);
CGContextFillPath(context);
}
@end
Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

+1
View File
@@ -238,6 +238,7 @@
[_titleField setAlignment:_alignment];
[_titleField setTextColor:_textColor];
[_titleField setHidden:_imagePosition == CPImageOnly];
[_titleField setHitTests:NO];
[self addSubview:_titleField];
}