Refactor OFBHeaderView

This commit is contained in:
Allan Odgaard
2013-01-11 13:37:27 +01:00
parent 73e18d30db
commit 5224d0b89c

View File

@@ -1,10 +1,4 @@
#import "OFBHeaderView.h"
#import <OakAppKit/NSMenuItem Additions.h>
#import <OakAppKit/OakFileIconImage.h>
@interface OFBHeaderView ()
@property (nonatomic, retain) NSMutableArray* buttonConstraints;
@end
static NSButton* ImageButton (NSString* imageName)
{
@@ -14,7 +8,6 @@ static NSButton* ImageButton (NSString* imageName)
[res setButtonType:NSMomentaryChangeButton];
[res setBezelStyle:NSSmallSquareBezelStyle];
[res setBordered:NO];
[res setTranslatesAutoresizingMaskIntoConstraints:NO];
NSImage* image = [[NSImage imageNamed:imageName] copy];
[image setSize:NSMakeSize(13, 13)];
@@ -29,19 +22,6 @@ static NSPopUpButton* PopUpButton ()
NSPopUpButton* res = [NSPopUpButton new];
[[res cell] setBackgroundStyle:NSBackgroundStyleRaised];
[res setBordered:NO];
[res setTranslatesAutoresizingMaskIntoConstraints:NO];
NSMenu* menu = [NSMenu new];
[menu setAutoenablesItems:NO];
NSString* path = NSHomeDirectory();
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:[[NSFileManager defaultManager] displayNameAtPath:path] action:@selector(nop:) keyEquivalent:@""];
[menuItem setIconForFile:path];
[menu addItem:menuItem];
[res setMenu:menu];
return res;
}
@@ -56,32 +36,25 @@ static NSPopUpButton* PopUpButton ()
self.goForwardButton = ImageButton(NSImageNameGoRightTemplate);
self.goForwardButton.toolTip = @"Go Forward";
for(NSView* view in @[ self.folderPopUpButton, self.goBackButton, self.goForwardButton ])
NSDictionary* views = @{
@"folder" : self.folderPopUpButton,
@"back" : self.goBackButton,
@"forward" : self.goForwardButton,
};
for(NSView* view in [views allValues])
{
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:view];
}
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(5)-[folder(>=75)]-[back(==21)][forward(==back)]-(5)-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[folder(==26)]" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(1)-[back(==forward,==23)]-(1)-|" options:0 metrics:nil views:views]];
}
return self;
}
- (void)updateConstraints
{
if(self.buttonConstraints)
[self removeConstraints:self.buttonConstraints];
[super updateConstraints];
NSDictionary* views = @{
@"folder" : self.folderPopUpButton,
@"back" : self.goBackButton,
@"forward" : self.goForwardButton,
};
self.buttonConstraints = [NSMutableArray new];
[self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(5)-[folder]-[back(==21)][forward(==back)]-(5)-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
[self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[folder(==26)]" options:0 metrics:nil views:views]];
[self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(1)-[back(==forward,==25)]-(1)-|" options:0 metrics:nil views:views]];
[self addConstraints:self.buttonConstraints];
}
- (BOOL)isOpaque
{
return YES;
@@ -89,12 +62,6 @@ static NSPopUpButton* PopUpButton ()
- (void)drawRect:(NSRect)aRect
{
NSRect bounds = self.bounds;
NSGradient* gradient = [[NSGradient alloc] initWithStartingColor:[NSColor darkGrayColor] endingColor:[NSColor lightGrayColor]];
[gradient drawInRect:bounds angle:90];
[[NSColor blackColor] set];
NSRectFill(NSMakeRect(NSMinX(bounds), 0, NSWidth(bounds), 1));
[[[NSGradient alloc] initWithStartingColor:[NSColor grayColor] endingColor:[NSColor lightGrayColor]] drawInRect:self.bounds angle:90];
}
@end