Skip to content
Browse files

flexible items in UMEToolbar

  • Loading branch information...
1 parent ac42856 commit 2abf65c5c2f6d15ced4210419c8789b16f33cac3 @itod committed
View
3 include/UMEKit/UMEBarButtonItem.h
@@ -56,6 +56,9 @@ typedef enum {
CGFloat width;
UMEBarStyle barStyle;
+
+ BOOL space;
+ BOOL flexible;
}
- (id)initWithBarButtonSystemItem:(UMEBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
View
2 include/UMEKit/UMEToolbar.h
@@ -16,6 +16,8 @@
NSColor *tintColor;
BOOL translucent;
BOOL layoutDone;
+ NSMutableArray *flexibleItems;
+ //NSUInteger numNonSpaceItems;
}
@property (nonatomic, assign) UMEBarStyle barStyle; // default is UMEBarStyleDefault (blue)
View
1 src/UMEActivityBarButtonItemButtonCell.m
@@ -129,6 +129,7 @@ - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSPoint inner;
NSPoint outer;
[NSBezierPath setDefaultLineCapStyle:NSRoundLineCapStyle];
+ strokeWidth = strokeWidth < 0.1 ? 0.1 :strokeWidth;
[NSBezierPath setDefaultLineWidth:strokeWidth];
if ([self isSpinning]) {
a = (270+(step* 30))*DEG2RAD;
View
23 src/UMEBarButtonItem.m
@@ -25,6 +25,8 @@ - (void)sizeToFit;
- (void)layout;
@property (nonatomic, retain) NSButton *button;
@property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault
+@property (nonatomic, getter=isSpace) BOOL space;
+@property (nonatomic, getter=isFlexible) BOOL flexible;
@end
@implementation UMEBarButtonItem
@@ -55,9 +57,12 @@ - (id)initWithBarButtonSystemItem:(UMEBarButtonSystemItem)systemItem target:(id)
aTitle = NSLocalizedString(@"Add", @"");
break;
case UMEBarButtonSystemItemFlexibleSpace:
+ self.space = YES;
+ self.flexible = YES;
imgPath = [b pathForImageResource:@""];
break;
case UMEBarButtonSystemItemFixedSpace:
+ self.space = YES;
imgPath = [b pathForImageResource:@""];
break;
case UMEBarButtonSystemItemCompose:
@@ -134,11 +139,15 @@ - (id)initWithBarButtonSystemItem:(UMEBarButtonSystemItem)systemItem target:(id)
break;
}
- self = [self initWithTitle:aTitle style:aStyle target:t action:sel];
- if ([imgPath length]) {
- self.image = [[[NSImage alloc] initWithContentsOfFile:imgPath] autorelease];
+ if (self.isSpace) {
+ self = [self initWithCustomView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];
+ } else {
+ self = [self initWithTitle:aTitle style:aStyle target:t action:sel];
+ if ([imgPath length]) {
+ self.image = [[[NSImage alloc] initWithContentsOfFile:imgPath] autorelease];
+ }
+ [button setImagePosition:imgPos];
}
- [button setImagePosition:imgPos];
return self;
}
@@ -199,7 +208,9 @@ - (void)sizeToFit {
[button sizeToFit];
//[button setFrameOrigin:NSZeroPoint];
[customView setFrameSize:[button frame].size];
- } else {
+ } else {
+ if (self.isFlexible) return;
+
if ([customView respondsToSelector:@selector(sizeToFit)]) {
[customView performSelector:@selector(sizeToFit)];
} else {
@@ -282,4 +293,6 @@ - (void)setCustomView:(NSView *)v {
@synthesize style;
@synthesize width;
@synthesize barStyle;
+@synthesize space;
+@synthesize flexible;
@end
View
5 src/UMEBarButtonItemButtonCell.m
@@ -32,6 +32,7 @@
@interface UMEBarButtonItem ()
@property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault
+@property (nonatomic, getter=isSpace) BOOL space;
@end
@interface UMEBarButtonItemButtonCell ()
@@ -83,7 +84,7 @@ - (void)dealloc {
- (BOOL)isOpaque {
- return YES;
+ return NO;
}
@@ -271,6 +272,8 @@ - (void)drawInteriorWithFrame:(NSRect)r inView:(NSView *)cv {
UMEBarButtonItemButton *button = (UMEBarButtonItemButton *)cv;
UMEBarButtonItem *item = button.item;
+ if (item.isSpace) return;
+
// if below the min width, just clear and return (dont draw borked background image)
CGFloat minWidth = MIN_WIDTH;
if (NSImageOnly == [self imagePosition]) {
View
56 src/UMEToolbar.m
@@ -15,10 +15,12 @@
@interface UMEBarButtonItem ()
- (void)sizeToFit;
@property (nonatomic) UMEBarStyle barStyle; // default is UMEBarStyleDefault
+@property (nonatomic, getter=isFlexible) BOOL flexible;
@end
@interface UMEToolbar ()
- (void)layoutItems;
+@property (nonatomic, retain) NSMutableArray *flexibleItems;
@end
@implementation UMEToolbar
@@ -34,10 +36,16 @@ - (id)initWithFrame:(NSRect)frame {
- (void)dealloc {
self.items = nil;
self.tintColor = nil;
+ self.flexibleItems = nil;
[super dealloc];
}
+- (void)awakeFromNib {
+
+}
+
+
#pragma mark -
#pragma mark NSView
@@ -82,7 +90,11 @@ - (void)drawRect:(NSRect)dirtyRect {
}
NSRect bounds = [self bounds];
- NSDrawThreePartImage(bounds, bgImg, bgImg, bgImg, NO, NSCompositeSourceOver, 1, YES);
+ NSDrawThreePartImage(bounds, nil, bgImg, nil, NO, NSCompositeSourceOver, 1, YES);
+
+ [[NSColor darkGrayColor] setStroke];
+ CGFloat y = bounds.size.height - 1;
+ [NSBezierPath strokeLineFromPoint:NSMakePoint(0, y) toPoint:NSMakePoint(bounds.size.width, y)];
}
@@ -96,11 +108,24 @@ - (void)setItems:(NSArray *)a animated:(BOOL)animated {
- (void)setItems:(NSArray *)a {
if (a != items) {
+ for (UMEBarButtonItem *item in items) {
+ [item.customView removeFromSuperview];
+ }
+
[items autorelease];
items = [a retain];
+ self.flexibleItems = [NSMutableArray array];
+ //numNonSpaceItems = 0;
+
for (UMEBarButtonItem *item in items) {
item.barStyle = barStyle;
+// if (!item.isSpace) {
+// numNonSpaceItems++;
+// }
+ if (item.isFlexible) {
+ [flexibleItems addObject:item];
+ }
}
[self layoutItems];
@@ -112,16 +137,40 @@ - (void)setItems:(NSArray *)a {
#pragma mark Private
- (void)layoutItems {
+ NSRect bounds = [self bounds];
+
CGFloat x = ITEM_X;
CGFloat y = 0;
CGFloat w = 0;
- CGFloat h = 0;
+ CGFloat h = bounds.size.height - 2.0; // room for bottom line
+
+ CGFloat availWidth = bounds.size.width;
+ CGFloat nonFlexibleItemsTotalWidth = ITEM_X * 2; // left and right margin
+
+ // calc total non-flexible width, and figure how many items can be visible in avail width
+ for (UMEBarButtonItem *item in items) {
+ CGFloat currWidth = NSWidth([item.customView frame]) + ITEM_MARGIN;
+ if (!item.isFlexible) {
+ nonFlexibleItemsTotalWidth += currWidth;
+ }
+ }
+
+ NSUInteger flexibleItemCount = [flexibleItems count];
+ BOOL itemsTruncated = (nonFlexibleItemsTotalWidth > availWidth);
+
+ if (flexibleItemCount > 0) {
+ CGFloat flexibleItemWidth = itemsTruncated ? 0 : (availWidth - nonFlexibleItemsTotalWidth) / flexibleItemCount;
+ for (UMEBarButtonItem *flexibleItem in flexibleItems) {
+ NSRect frame = [flexibleItem.customView frame];
+ frame.size.width = flexibleItemWidth;
+ [flexibleItem.customView setFrame:frame];
+ }
+ }
for (UMEBarButtonItem *item in items) {
//[item sizeToFit];
[self addSubview:item.customView];
w = [item width];
- h = NSHeight([item.customView frame]);
[item.customView setFrame:NSMakeRect(x, y, w, h)];
x += w + ITEM_MARGIN;
}
@@ -133,4 +182,5 @@ - (void)layoutItems {
@synthesize items;
@synthesize tintColor;
@synthesize translucent;
+@synthesize flexibleItems;
@end

0 comments on commit 2abf65c

Please sign in to comment.
Something went wrong with that request. Please try again.