summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-04 08:31:16 +0000
committerthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-04 08:31:16 +0000
commit2bc70b08e10e985b635273f395bac10b7ef54dc9 (patch)
tree591e82501104c4a6ff4a17a6c2b98fce596a2d06
parentf6395a362d7d325b80de13f314ac1ff94d2a68c1 (diff)
downloadchromium_src-2bc70b08e10e985b635273f395bac10b7ef54dc9.zip
chromium_src-2bc70b08e10e985b635273f395bac10b7ef54dc9.tar.gz
chromium_src-2bc70b08e10e985b635273f395bac10b7ef54dc9.tar.bz2
mac: attempt to fix a startup perf regression after r174653
BUG=168084 Review URL: https://chromiumcodereview.appspot.com/11647050 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175123 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/ui/cocoa/image_button_cell.h9
-rw-r--r--chrome/browser/ui/cocoa/image_button_cell.mm50
2 files changed, 43 insertions, 16 deletions
diff --git a/chrome/browser/ui/cocoa/image_button_cell.h b/chrome/browser/ui/cocoa/image_button_cell.h
index 8d64c1e..74feb94 100644
--- a/chrome/browser/ui/cocoa/image_button_cell.h
+++ b/chrome/browser/ui/cocoa/image_button_cell.h
@@ -36,7 +36,11 @@ enum ButtonState {
// state. Images are specified by image IDs.
@interface ImageButtonCell : NSButtonCell {
@private
- scoped_nsobject<NSImage> image_[image_button_cell::kButtonStateCount];
+ struct {
+ // At most one of these two fields will be non-null.
+ int imageId;
+ scoped_nsobject<NSImage> image;
+ } image_[image_button_cell::kButtonStateCount];
NSInteger overlayImageID_;
BOOL isMouseInside_;
}
@@ -45,7 +49,8 @@ enum ButtonState {
@property(assign, nonatomic) BOOL isMouseInside;
// Sets the image for the given button state using an image ID.
-// The image will be loaded from a resource pak.
+// The image will be lazy loaded from a resource pak -- important because
+// this is in the hot path for startup.
- (void)setImageID:(NSInteger)imageID
forButtonState:(image_button_cell::ButtonState)state;
diff --git a/chrome/browser/ui/cocoa/image_button_cell.mm b/chrome/browser/ui/cocoa/image_button_cell.mm
index 3223db8..5457908 100644
--- a/chrome/browser/ui/cocoa/image_button_cell.mm
+++ b/chrome/browser/ui/cocoa/image_button_cell.mm
@@ -58,20 +58,34 @@ const CGFloat kImageNoFocusAlpha = 0.65;
[self setShowsBorderOnlyWhileMouseInside:YES];
}
+- (NSImage*)imageForState:(image_button_cell::ButtonState)state
+ view:(NSView*)controlView{
+ if (image_[state].imageId)
+ return [self imageForID:image_[state].imageId controlView:controlView];
+ return image_[state].image;
+}
+
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
+ image_button_cell::ButtonState state = [self currentButtonState];
BOOL windowHasFocus = [[controlView window] isMainWindow] ||
[[controlView window] isKeyWindow];
CGFloat alpha = windowHasFocus ? 1.0 : kImageNoFocusAlpha;
- NSImage* image = image_[[self currentButtonState]];
+ NSImage* image = [self imageForState:state view:controlView];
if (!windowHasFocus) {
+ NSImage* defaultImage = [self
+ imageForState:image_button_cell::kDefaultStateBackground
+ view:controlView];
+ NSImage* hoverImage = [self
+ imageForState:image_button_cell::kHoverStateBackground
+ view:controlView];
if ([self currentButtonState] == image_button_cell::kDefaultState &&
- image_[image_button_cell::kDefaultStateBackground]) {
- image = image_[image_button_cell::kDefaultStateBackground];
+ defaultImage) {
+ image = defaultImage;
alpha = 1.0;
} else if ([self currentButtonState] == image_button_cell::kHoverState &&
- image_[image_button_cell::kHoverStateBackground]) {
- image = image_[image_button_cell::kHoverStateBackground];
+ hoverImage) {
+ image = hoverImage;
alpha = 1.0;
}
}
@@ -110,9 +124,12 @@ const CGFloat kImageNoFocusAlpha = 0.65;
- (void)setImageID:(NSInteger)imageID
forButtonState:(image_button_cell::ButtonState)state {
- ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
- NSImage* image = imageID ? rb.GetNativeImageNamed(imageID).ToNSImage() : nil;
- [self setImage:image forButtonState:state];
+ DCHECK_GE(state, 0);
+ DCHECK_LT(state, image_button_cell::kButtonStateCount);
+
+ image_[state].image.reset();
+ image_[state].imageId = imageID;
+ [[self controlView] setNeedsDisplay:YES];
}
// Sets the image for the given button state using an image.
@@ -120,7 +137,9 @@ const CGFloat kImageNoFocusAlpha = 0.65;
forButtonState:(image_button_cell::ButtonState)state {
DCHECK_GE(state, 0);
DCHECK_LT(state, image_button_cell::kButtonStateCount);
- image_[state].reset([image retain]);
+
+ image_[state].image.reset([image retain]);
+ image_[state].imageId = 0;
[[self controlView] setNeedsDisplay:YES];
}
@@ -132,14 +151,17 @@ const CGFloat kImageNoFocusAlpha = 0.65;
}
- (image_button_cell::ButtonState)currentButtonState {
- if (![self isEnabled] && image_[image_button_cell::kDisabledState])
+ bool (^has)(image_button_cell::ButtonState) =
+ ^(image_button_cell::ButtonState state) {
+ return image_[state].image || image_[state].imageId;
+ };
+ if (![self isEnabled] && has(image_button_cell::kDisabledState))
return image_button_cell::kDisabledState;
- else if ([self isHighlighted] && image_[image_button_cell::kPressedState])
+ if ([self isHighlighted] && has(image_button_cell::kPressedState))
return image_button_cell::kPressedState;
- else if ([self isMouseInside] && image_[image_button_cell::kHoverState])
+ if ([self isMouseInside] && has(image_button_cell::kHoverState))
return image_button_cell::kHoverState;
- else
- return image_button_cell::kDefaultState;
+ return image_button_cell::kDefaultState;
}
- (NSImage*)imageForID:(NSInteger)imageID