Themable CPAlert which automatically grows height-wise to accommodate all text. Adjustable icons, margins, spacing and fonts.

This commit is contained in:
Alexander Ljungberg committed 2010-08-19 23:03:35 -04:00
1 parent 62f81f2601
commit c06068c931
8 files changed
+212 -108

No files matched your search

+134 -100
View File
@@ -52,10 +52,6 @@ CPInformationalAlertStyle = 1;
CPCriticalAlertStyle = 2;
var CPAlertWarningImage,
CPAlertInformationImage,
CPAlertErrorImage;
/*!
@ingroup appkit
@@ -78,7 +74,7 @@ var CPAlertWarningImage,
representing the first button added to the alert which appears on the
right, 1 representing the next button to the left and so on)
*/
@implementation CPAlert : CPObject
@implementation CPAlert : CPView
{
CPPanel _alertPanel;
@@ -89,27 +85,34 @@ var CPAlertWarningImage,
CPAlertStyle _alertStyle;
CPString _windowTitle;
int _windowStyle;
int _buttonCount;
CPArray _buttons;
id _delegate;
}
+ (void)initialize
+ (CPString)themeClass
{
if (self != CPAlert)
return;
return @"alert";
}
var bundle = [CPBundle bundleForClass:[self class]];
CPAlertWarningImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-warning.png"]
size:CGSizeMake(32.0, 32.0)];
CPAlertInformationImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-information.png"]
size:CGSizeMake(32.0, 32.0)];
CPAlertErrorImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-error.png"]
size:CGSizeMake(32.0, 32.0)];
+ (id)themeAttributes
{
return [CPDictionary dictionaryWithObjects:[CGSizeMake(400.0, 110.0), CGInsetMake(15, 15, 15, 50), 6, 10,
CPJustifiedTextAlignment, [CPColor blackColor], [CPFont boldSystemFontOfSize:13.0], [CPNull null], CGSizeMakeZero(),
CPJustifiedTextAlignment, [CPColor blackColor], [CPFont systemFontOfSize:12.0], [CPNull null], CGSizeMakeZero(),
CGPointMake(15, 12),
[CPNull null],
[CPNull null],
[CPNull null]
]
forKeys:[@"size", @"content-inset", @"informative-offset", @"button-offset",
@"message-text-alignment", @"message-text-color", @"message-text-font", @"message-text-shadow-color", @"message-text-shadow-offset",
@"informative-text-alignment", @"informative-text-color", @"informative-text-font", @"informative-text-shadow-color", @"informative-text-shadow-offset",
@"image-offset",
@"information-image",
@"warning-image",
@"error-image"
]];
}
/*!
@@ -119,9 +122,13 @@ var CPAlertWarningImage,
{
if (self = [super init])
{
_buttonCount = 0;
_buttons = [CPArray array];
_alertStyle = CPWarningAlertStyle;
_alertPanel = nil;
_messageLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
_alertImageView = [[CPImageView alloc] initWithFrame:CGRectMakeZero()];
_informativeLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[self setWindowStyle:nil];
}
@@ -130,52 +137,38 @@ var CPAlertWarningImage,
}
/*!
Sets the window appearance.
Sets the window appearance. If CPHUDBackgroundWindowMask is set, the default HUD theme
will be activated.
@param styleMask - Either CPHUDBackgroundWindowMask or nil for standard.
*/
- (void)setWindowStyle:(int)styleMask
{
_windowStyle = styleMask;
_alertPanel = [[CPPanel alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 110.0) styleMask:styleMask ? styleMask | CPTitledWindowMask : CPTitledWindowMask];
[_alertPanel setFloatingPanel:YES];
[_alertPanel center];
[self setTheme:(_windowStyle === CPHUDBackgroundWindowMask) ? [CPTheme defaultHudTheme] : [CPTheme defaultTheme]];
var count = [_buttons count];
for (var i = 0; i < count; i++)
{
var button = _buttons[i];
[button setTheme:(_windowStyle === CPHUDBackgroundWindowMask) ? [CPTheme defaultHudTheme] : [CPTheme defaultTheme]];
// We'll need to recreate the panel to get the new window style.
_alertPanel = nil;
}
[[_alertPanel contentView] addSubview:button];
}
- (void)_createPanel
{
var frame = CGRectMakeZero();
frame.size = [self currentValueForThemeAttribute:@"size"];
_alertPanel = [[CPPanel alloc] initWithContentRect:frame styleMask:_windowStyle ? _windowStyle | CPTitledWindowMask : CPTitledWindowMask];
[self _layoutButtons];
var contentView = [_alertPanel contentView],
count = [_buttons count];
while (count--)
[contentView addSubview:_buttons[count]];
if (!_messageLabel)
{
_messageLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[_messageLabel setFont:[CPFont boldSystemFontOfSize:13.0]];
[_messageLabel setLineBreakMode:CPLineBreakByWordWrapping];
[_messageLabel setAlignment:CPJustifiedTextAlignment];
[_messageLabel setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
[contentView addSubview:_messageLabel];
[contentView addSubview:_alertImageView];
[contentView addSubview:_informativeLabel];
_alertImageView = [[CPImageView alloc] initWithFrame:CGRectMake(15.0, 12.0, 32.0, 32.0)];
_informativeLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[_informativeLabel setFont:[CPFont systemFontOfSize:12.0]];
[_informativeLabel setLineBreakMode:CPLineBreakByWordWrapping];
[_informativeLabel setAlignment:CPJustifiedTextAlignment];
[_informativeLabel setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
}
[_messageLabel setTextColor:(styleMask & CPHUDBackgroundWindowMask) ? [CPColor whiteColor] : [CPColor blackColor]];
[_informativeLabel setTextColor:(styleMask & CPHUDBackgroundWindowMask) ? [CPColor whiteColor] : [CPColor blackColor]];
[[_alertPanel contentView] addSubview:_messageLabel];
[[_alertPanel contentView] addSubview:_alertImageView];
[[_alertPanel contentView] addSubview:_informativeLabel];
[self _layoutMessage];
// For reference: does not actually work since this 'view' is not in the hierarchy.
// [self setNeedsLayout];
}
/*!
@@ -227,6 +220,9 @@ var CPAlertWarningImage,
- (void)setAlertStyle:(CPAlertStyle)style
{
_alertStyle = style;
// For reference: does not actually work since this 'view' is not in the hierarchy.
// [self setNeedsLayout];
}
/*!
@@ -244,7 +240,9 @@ var CPAlertWarningImage,
- (void)setMessageText:(CPString)messageText
{
[_messageLabel setStringValue:messageText];
[self _layoutMessage];
// For reference: does not actually work since this 'view' is not in the hierarchy.
// [self setNeedsLayout];
}
/*!
@@ -287,16 +285,14 @@ var CPAlertWarningImage,
- (void)addButtonWithTitle:(CPString)title
{
var bounds = [[_alertPanel contentView] bounds],
button = [[CPButton alloc] initWithFrame:CGRectMakeZero()];
button = [[CPButton alloc] initWithFrame:CGRectMakeZero()],
_buttonCount = [_buttons count];
[button setTitle:title];
[button setTarget:self];
[button setTag:_buttonCount];
[button setAction:@selector(_notifyDelegate:)];
[button setTheme:(_windowStyle === CPHUDBackgroundWindowMask) ? [CPTheme defaultHudTheme] : [CPTheme defaultTheme]];
[button setAutoresizingMask:CPViewMinXMargin | CPViewMinYMargin];
[[_alertPanel contentView] addSubview:button];
if (_buttonCount == 0)
@@ -306,43 +302,97 @@ var CPAlertWarningImage,
else
[button setKeyEquivalent:nil];
_buttonCount++;
[_buttons addObject:button];
[self _layoutButtons];
// For reference: does not actually work since this 'view' is not in the hierarchy.
// [self setNeedsLayout];
}
- (void)_layoutButtons
- (void)layoutSubviews
{
var bounds = [[_alertPanel contentView] bounds],
count = [_buttons count],
offsetX = CGRectGetWidth(bounds),
offsetY = CGRectGetHeight(bounds) - 34.0;
if (!_alertPanel)
[self _createPanel];
for (var i = 0; i < count; i++)
var inset = [self currentValueForThemeAttribute:@"content-inset"],
iconOffset = [self currentValueForThemeAttribute:@"image-offset"],
theTitle,
theImage;
switch (_alertStyle)
{
var button = _buttons[i];
case CPWarningAlertStyle: theImage = [self currentValueForThemeAttribute:@"warning-image"];
theTitle = @"Warning";
break;
case CPInformationalAlertStyle: theImage = [self currentValueForThemeAttribute:@"information-image"];
theTitle = @"Information";
break;
case CPCriticalAlertStyle: theImage = [self currentValueForThemeAttribute:@"error-image"];
theTitle = @"Error";
break;
}
[_alertImageView setImage:theImage];
var imageSize = theImage ? [theImage size] : CGSizeMakeZero();
[_alertImageView setFrame:CGRectMake(iconOffset.x, iconOffset.y, imageSize.width, imageSize.height)];
[_alertPanel setTitle:_windowTitle ? _windowTitle : theTitle];
[_alertPanel setFloatingPanel:YES];
[_alertPanel center];
[_messageLabel setTextColor:[self currentValueForThemeAttribute:@"message-text-color"]];
[_messageLabel setFont:[self currentValueForThemeAttribute:@"message-text-font"]];
[_messageLabel setTextShadowColor:[self currentValueForThemeAttribute:@"message-text-shadow-color"]];
[_messageLabel setTextShadowOffset:[self currentValueForThemeAttribute:@"message-text-shadow-offset"]];
[_messageLabel setAlignment:[self currentValueForThemeAttribute:@"message-text-alignment"]];
[_messageLabel setLineBreakMode:CPLineBreakByWordWrapping];
[_informativeLabel setTextColor:[self currentValueForThemeAttribute:@"informative-text-color"]];
[_informativeLabel setFont:[self currentValueForThemeAttribute:@"informative-text-font"]];
[_informativeLabel setTextShadowColor:[self currentValueForThemeAttribute:@"informative-text-shadow-color"]];
[_informativeLabel setTextShadowOffset:[self currentValueForThemeAttribute:@"informative-text-shadow-offset"]];
[_informativeLabel setLineBreakMode:CPLineBreakByWordWrapping];
// FIXME sizeWithFontCorrection shouldn't be needed.
var bounds = [[_alertPanel contentView] bounds],
offsetX = CGRectGetWidth(bounds) - inset.right,
informativeOffset = [self currentValueForThemeAttribute:@"informative-offset"],
buttonOffset = [self currentValueForThemeAttribute:@"button-offset"],
textWidth = offsetX - inset.left,
messageSize = [([_messageLabel stringValue] || " ") sizeWithFont:[_messageLabel font] inWidth:textWidth],
informativeSize = [([_informativeLabel stringValue] || " ") sizeWithFont:[_informativeLabel font] inWidth:textWidth],
sizeWithFontCorrection = 6.0;
[_messageLabel setFrame:CGRectMake(inset.left, inset.top, textWidth, messageSize.height + sizeWithFontCorrection)];
[_informativeLabel setFrame:CGRectMake(inset.left, CGRectGetMaxY([_messageLabel frame]) + informativeOffset, textWidth, informativeSize.height + sizeWithFontCorrection)];
var aRepresentativeButton = _buttons[0],
buttonY = CGRectGetMaxY([_informativeLabel frame]) + buttonOffset;
[aRepresentativeButton setTheme:[self theme]];
[aRepresentativeButton sizeToFit];
// Make the window just tall enough to fit everything. Bit of a hack really.
var extraY = buttonY + CGRectGetHeight([aRepresentativeButton bounds]) + inset.bottom - CGRectGetHeight(bounds),
frameSize = CGSizeMakeCopy([_alertPanel frame].size);
frameSize.height += extraY;
[_alertPanel setFrameSize:frameSize];
var count = [_buttons count];
while (count--)
{
var button = _buttons[count];
[button setTheme:[self theme]];
[button sizeToFit];
var buttonBounds = [button bounds],
width = MAX(80.0, CGRectGetWidth(buttonBounds)),
height = CGRectGetHeight(buttonBounds);
offsetX -= (width + 10);
[button setFrame:CGRectMake(offsetX, offsetY, width, height)];
offsetX -= width;
[button setFrame:CGRectMake(offsetX, buttonY, width, height)];
offsetX -= 10;
}
}
- (void)_layoutMessage
{
var bounds = [[_alertPanel contentView] bounds],
width = CGRectGetWidth(bounds) - 73.0,
size = [([_messageLabel stringValue] || " ") sizeWithFont:[_messageLabel currentValueForThemeAttribute:@"font"] inWidth:width],
contentInset = [_messageLabel currentValueForThemeAttribute:@"content-inset"],
height = size.height + contentInset.top + contentInset.bottom;
[_messageLabel setFrame:CGRectMake(57.0, 10.0, width, height)];
[_informativeLabel setFrame:CGRectMake(57.0, 10.0 + height + 6.0, width, CGRectGetHeight(bounds) - height - 50.0)];
}
/*!
@@ -352,23 +402,7 @@ var CPAlertWarningImage,
*/
- (void)runModal
{
var theTitle;
switch (_alertStyle)
{
case CPWarningAlertStyle: [_alertImageView setImage:CPAlertWarningImage];
theTitle = @"Warning";
break;
case CPInformationalAlertStyle: [_alertImageView setImage:CPAlertInformationImage];
theTitle = @"Information";
break;
case CPCriticalAlertStyle: [_alertImageView setImage:CPAlertErrorImage];
theTitle = @"Error";
break;
}
[_alertPanel setTitle:_windowTitle ? _windowTitle : theTitle];
[self layoutSubviews];
[CPApp runModalForWindow:_alertPanel];
}
File renamed without changes.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

File renamed without changes.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

File renamed without changes.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

+48 -1
View File
@@ -17,7 +17,8 @@ var themedButtonValues = nil,
themedHorizontalSliderValues = nil,
themedVerticalSliderValues = nil,
themedCircularSliderValues = nil,
themedButtonBarValues = nil;
themedButtonBarValues = nil,
themedAlertValues = nil;
/*
HOW TO ADD OR MODIFY THEMED ELEMENTS
@@ -1301,6 +1302,37 @@ var themedButtonValues = nil,
return view;
}
+ (CPAlert)themedAlert
{
var alert = [CPAlert new],
size = CGSizeMake(400.0, 110.0),
inset = CGInsetMake(10, 15, 15, 55),
messageFont = [CPFont boldSystemFontOfSize:13.0],
informativeFont = [CPFont systemFontOfSize:12.0],
informationIcon = PatternImage("dialog-information.png", 32.0, 32.0),
warningIcon = PatternImage("dialog-warning.png", 32.0, 32.0),
errorIcon = PatternImage("dialog-error.png", 32.0, 32.0);
themedAlertValues =
[
[@"size", size],
[@"content-inset", inset],
[@"message-text-alignment", CPJustifiedTextAlignment],
[@"message-text-color", [CPColor blackColor]],
[@"message-text-font", messageFont],
[@"informative-text-alignment", CPJustifiedTextAlignment],
[@"informative-text-color", [CPColor blackColor]],
[@"informative-text-font", informativeFont],
[@"information-image", informationIcon],
[@"warning-image", warningIcon],
[@"error-image", errorIcon]
];
[self registerThemeValues:themedAlertValues forView:alert];
return alert;
}
@end
@implementation AristoHUDThemeDescriptor : BKThemeDescriptor
@@ -1423,4 +1455,19 @@ var themedButtonValues = nil,
return slider;
}
+ (CPAlert)themedAlert
{
var alert = [CPAlert new],
hudSpecificValues =
[
[@"message-text-color", [CPColor whiteColor]],
[@"informative-text-color", [CPColor whiteColor]],
];
[self registerThemeValues:hudSpecificValues forView:alert inherit:themedAlertValues];
return alert;
}
@end
+9 -1
View File
@@ -348,10 +348,18 @@ function PatternColor()
}
else if (arguments.length == 3)
{
return [CPColor colorWithPatternImage:[_CPCibCustomResource imageResourceWithName:arguments[0] size:CGSizeMake(arguments[1], arguments[2])]];
return [CPColor colorWithPatternImage:PatternImage(arguments[0], arguments[1], arguments[2])];
}
else
{
return nil;
}
}
/*
Like the 3 argument PatternColor, but return an image instead of a color.
*/
function PatternImage(name, width, height)
{
return [_CPCibCustomResource imageResourceWithName:name size:CGSizeMake(width, height)];
}
+21 -6
View File
@@ -13,10 +13,22 @@
{
CPTextField label;
CPArray variations;
CPArray messages;
int messageIndex;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
messages = [
[@"Are you sure you want to theorise before you have data?",
@"Invariably, you end up twisting facts to suit theories, instead of theories to suit facts.",
"Theorise", "Cancel"],
[@"Snakes. Why did it have to be snakes?",
nil,
"Torch", "Cancel"]
];
messageIndex = 0;
variations = [
[nil, CPWarningAlertStyle],
[nil, CPInformationalAlertStyle],
@@ -45,7 +57,7 @@
- (void)alertDidEnd:(CPAlert)anAlert returnCode:(id)returnCode
{
if (returnCode === 0)
[label setStringValue:"You chose 'Theorise'."];
[label setStringValue:"You chose the default action."];
else
[label setStringValue:"You cancelled the dialog."];
@@ -57,16 +69,19 @@
if (![variations count])
return;
var variation = [variations objectAtIndex:0],
var variation = variations[0],
message = messages[messageIndex],
alert = [[CPAlert alloc] init];
messageIndex = 1 - messageIndex;
[variations removeObjectAtIndex:0];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Theorise"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Do you want to theorise before you have data?"];
[alert setInformativeText:@"Invariably, you end up twisting facts to suit theories, instead of theories to suit facts."];
[alert setMessageText:message[0]];
[alert setInformativeText:message[1]];
[alert addButtonWithTitle:message[2]];
[alert addButtonWithTitle:message[3]];
[alert setWindowStyle:variation[0]];
[alert setAlertStyle:variation[1]];