From 6d8e1705b8aad5f4eb0159040037b05dd4207122 Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Tue, 19 May 2009 12:02:10 -0700 Subject: [PATCH] Cleaned up button's sizeToFit. Reviewed by me. --- AppKit/CPButton.j | 78 +++++++++++++------------ AppKit/CPCheckBox.j | 11 ++-- AppKit/CPControl.j | 25 +++++++- AppKit/CPRadio.j | 10 ++++ AppKit/Themes/Aristo/ThemeDescriptors.j | 16 +++-- 5 files changed, 90 insertions(+), 50 deletions(-) diff --git a/AppKit/CPButton.j b/AppKit/CPButton.j index ca3dac841..c29324923 100644 --- a/AppKit/CPButton.j +++ b/AppKit/CPButton.j @@ -122,25 +122,22 @@ CPButtonStateMixed = CPThemeState("mixed"); CPControlSize _controlSize; } -+ (id)standardButtonWithTitle:(CPString)aTitle ++ (id)buttonWithTitle:(CPString)aTitle { - var button = [[CPButton alloc] init]; + return [self buttonWithTitle:aTitle theme:[CPTheme defaultTheme]]; +} ++ (id)buttonWithTitle:(CPString)aTitle theme:(CPTheme)aTheme +{ + var button = [[self alloc] init]; + + [button setTheme:aTheme]; [button setTitle:aTitle]; + [button sizeToFit]; return button; } -+ (id)standardCheckBoxWithTitle:(CPString)aTitle -{ - return [CPCheckBox standardButtonWithTitle:aTitle]; -} - -- (id)standardRadioWithTitle:(CPString)aTitle -{ - return [CPRadio standardButtonWithTitle:aTitle]; -} - + (CPString)themeClass { return @"button"; @@ -148,8 +145,8 @@ CPButtonStateMixed = CPThemeState("mixed"); + (id)themeAttributes { - return [CPDictionary dictionaryWithObjects:[_CGInsetMakeZero(), _CGInsetMakeZero(), nil, 24.0] - forKeys:[@"bezel-inset", @"content-inset", @"bezel-color", @"default-height"]]; + return [CPDictionary dictionaryWithObjects:[_CGInsetMakeZero(), _CGInsetMakeZero(), nil] + forKeys:[@"bezel-inset", @"content-inset", @"bezel-color"]]; } - (id)initWithFrame:(CGRect)aFrame @@ -472,12 +469,12 @@ CPButtonStateMixed = CPThemeState("mixed"); if (_CGInsetIsEmpty(bezelInset)) return bounds; - + bounds.origin.x += bezelInset.left; bounds.origin.y += bezelInset.top; bounds.size.width -= bezelInset.left + bezelInset.right; bounds.size.height -= bezelInset.top + bezelInset.bottom; - + return bounds; } @@ -486,11 +483,21 @@ CPButtonStateMixed = CPThemeState("mixed"); */ - (void)sizeToFit { - var size = [([self title] || " ") sizeWithFont:[self font]], + var size = [([self title] || " ") sizeWithFont:[self currentValueForThemeAttribute:@"font"]],//[self font]], contentInset = [self currentValueForThemeAttribute:@"content-inset"], - defaultHeight = [self currentValueForThemeAttribute:@"default-height"]; + minSize = [self currentValueForThemeAttribute:@"min-size"], + maxSize = [self currentValueForThemeAttribute:@"max-size"]; - [self setFrameSize:CGSizeMake(size.width + contentInset.left + contentInset.right, defaultHeight)]; + size.width = MAX(size.width + contentInset.left + contentInset.right, minSize.width); + size.height = MAX(size.height + contentInset.top + contentInset.bottom, minSize.height); + + if (maxSize.width >= 0.0) + size.width = MIN(size.width, maxSize.width); + + if (maxSize.height >= 0.0) + size.height = MIN(size.height, maxSize.height); + + [self setFrameSize:size]; } - (CGRect)rectForEphemeralSubviewNamed:(CPString)aName @@ -558,18 +565,6 @@ CPButtonStateMixed = CPThemeState("mixed"); [self unsetThemeState:CPThemeStateDefault]; } -@end - -@implementation CPButton (NS) - -- (void)setBezelStyle:(unsigned)aBezelStyle -{ -} - -- (unsigned)bezelStyle -{ -} - - (void)setBordered:(BOOL)shouldBeBordered { if (shouldBeBordered) @@ -585,13 +580,24 @@ CPButtonStateMixed = CPThemeState("mixed"); @end +@implementation CPButton (NS) + +- (void)setBezelStyle:(unsigned)aBezelStyle +{ +} + +- (unsigned)bezelStyle +{ +} + +@end + var CPButtonImageKey = @"CPButtonImageKey", CPButtonAlternateImageKey = @"CPButtonAlternateImageKey", CPButtonTitleKey = @"CPButtonTitleKey", CPButtonAlternateTitleKey = @"CPButtonAlternateTitleKey", - CPButtonIsBorderedKey = @"CPButtonIsBorderedKey", - CPButtonBezelStyleKey = @"CPButtonBezelStyleKey"; + CPButtonIsBorderedKey = @"CPButtonIsBorderedKey"; @implementation CPButton (CPCoding) @@ -627,14 +633,12 @@ var CPButtonImageKey = @"CPButtonImageKey", - (void)encodeWithCoder:(CPCoder)aCoder { [super encodeWithCoder:aCoder]; - + [aCoder encodeObject:_image forKey:CPButtonImageKey]; [aCoder encodeObject:_alternateImage forKey:CPButtonAlternateImageKey]; - + [aCoder encodeObject:_title forKey:CPButtonTitleKey]; [aCoder encodeObject:_alternateTitle forKey:CPButtonAlternateTitleKey]; - - [aCoder encodeInt:_bezelStyle forKey:CPButtonBezelStyleKey]; } @end diff --git a/AppKit/CPCheckBox.j b/AppKit/CPCheckBox.j index 487c51813..2c2852883 100644 --- a/AppKit/CPCheckBox.j +++ b/AppKit/CPCheckBox.j @@ -27,13 +27,14 @@ { } -+ (CPButton)standardButtonWithTitle:(CPString)aTitle ++ (id)checkBoxWithTitle:(CPString)aTitle theme:(CPTheme)aTheme { - var button = [[CPCheckBox alloc] init]; + return [self buttonWithTitle:aTitle theme:aTheme]; +} - [button setTitle:aTitle]; - - return button; ++ (id)checkBoxWithTitle:(CPString)aTitle +{ + return [self buttonWithTitle:aTitle]; } + (CPString)themeClass diff --git a/AppKit/CPControl.j b/AppKit/CPControl.j index 9722d059a..6fe63b8a8 100644 --- a/AppKit/CPControl.j +++ b/AppKit/CPControl.j @@ -109,9 +109,28 @@ var CPControlBlackColor = [CPColor blackColor]; + (CPDictionary)themeAttributes { - return [CPDictionary dictionaryWithObjects:[ - CPLeftTextAlignment, CPTopVerticalTextAlignment, CPLineBreakByClipping, [CPColor blackColor], [CPFont systemFontOfSize:12.0], nil, _CGSizeMakeZero(), CPImageLeft, CPScaleToFit] - forKeys:[@"alignment", @"vertical-alignment", @"line-break-mode", @"text-color", @"font", @"text-shadow-color", @"text-shadow-offset", @"image-position", @"image-scaling"]]; + return [CPDictionary dictionaryWithObjects:[CPLeftTextAlignment, + CPTopVerticalTextAlignment, + CPLineBreakByClipping, + [CPColor blackColor], + [CPFont systemFontOfSize:12.0], + nil, + _CGSizeMakeZero(), + CPImageLeft, + CPScaleToFit, + _CGSizeMakeZero(), + _CGSizeMake(-1.0, -1.0)] + forKeys:[@"alignment", + @"vertical-alignment", + @"line-break-mode", + @"text-color", + @"font", + @"text-shadow-color", + @"text-shadow-offset", + @"image-position", + @"image-scaling", + @"min-size", + @"max-size"]]; } - (id)initWithFrame:(CGRect)aFrame diff --git a/AppKit/CPRadio.j b/AppKit/CPRadio.j index f6079c74d..3fef28d02 100644 --- a/AppKit/CPRadio.j +++ b/AppKit/CPRadio.j @@ -67,6 +67,16 @@ CPRadioGroup _radioGroup; } ++ (id)radioWithTitle:(CPString)aTitle theme:(CPTheme)aTheme +{ + return [self buttonWithTitle:aTitle theme:aTheme]; +} + ++ (id)radioWithTitle:(CPString)aTitle +{ + return [self buttonWithTitle:aTitle]; +} + + (CPButton)standardButtonWithTitle:(CPString)aTitle { var button = [[CPRadio alloc] init]; diff --git a/AppKit/Themes/Aristo/ThemeDescriptors.j b/AppKit/Themes/Aristo/ThemeDescriptors.j index 40c63114b..887cea8ca 100644 --- a/AppKit/Themes/Aristo/ThemeDescriptors.j +++ b/AppKit/Themes/Aristo/ThemeDescriptors.j @@ -1,9 +1,9 @@ /* * AppController.j - * A + * Aristo * - * Created by __Me__ on __Date__. - * Copyright 2008 __MyCompanyName__. All rights reserved. + * Created by Francisco Tolmasky. + * Copyright 2009, 280 North, Inc. All rights reserved. */ @import @@ -250,7 +250,8 @@ [button setValue:defaultHighlightedBezelColor forThemeAttribute:@"bezel-color" inState:CPThemeStateBordered|CPThemeStateHighlighted|CPThemeStateDefault]; [button setValue:[CPColor colorWithCalibratedRed:13.0/255.0 green:51.0/255.0 blue:70.0/255.0 alpha:1.0] forThemeAttribute:@"text-color" inState:CPThemeStateDefault]; - [button setValue:24.0 forThemeAttribute:@"default-height"]; + [button setValue:CGSizeMake(0.0, 24.0) forThemeAttribute:@"min-size"]; + [button setValue:CGSizeMake(-1.0, 24.0) forThemeAttribute:@"max-size"]; return button; } @@ -278,6 +279,8 @@ [button setValue:bezelColor forThemeAttribute:@"bezel-color" inState:CPThemeStateBordered]; [button setValue:bezelColorSelected forThemeAttribute:@"bezel-color" inState:CPThemeStateBordered | CPThemeStateSelected]; + [button setValue:CGSizeMake(0.0, 17.0) forThemeAttribute:@"min-size"]; + return button; } @@ -304,6 +307,8 @@ [button setValue:bezelColor forThemeAttribute:@"bezel-color" inState:CPThemeStateBordered]; [button setValue:bezelColorSelected forThemeAttribute:@"bezel-color" inState:CPThemeStateBordered | CPThemeStateSelected]; + [button setValue:CGSizeMake(0.0, 16.0) forThemeAttribute:@"min-size"]; + return button; } @@ -563,7 +568,8 @@ [button setValue:highlightedBezelColor forThemeAttribute:@"bezel-color" inState:CPThemeStateBordered|CPThemeStateHighlighted]; [button setValue:CGInsetMake(2.0, 5.0, 4.0, 5.0) forThemeAttribute:@"content-inset" inState:CPThemeStateBordered]; - [button setValue:20.0 forThemeAttribute:@"default-height"]; + [button setValue:CGSizeMake(0.0, 20.0) forThemeAttribute:@"min-size"]; + [button setValue:CGSizeMake(-1.0, 20.0) forThemeAttribute:@"max-size"]; return button; }