Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" | 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #import "chrome/browser/bookmarks/bookmark_model.h" | 9 #import "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
| 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu.h" | 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu.h" |
| 12 #import "chrome/browser/ui/cocoa/image_utils.h" | 12 #import "chrome/browser/ui/cocoa/image_utils.h" |
| 13 #include "content/browser/user_metrics.h" | 13 #include "content/browser/user_metrics.h" |
| 14 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 15 #include "ui/base/l10n/l10n_util_mac.h" | 15 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 #include "ui/gfx/mac/nsimage_cache.h" | 16 #include "ui/gfx/mac/nsimage_cache.h" |
| 17 | 17 |
| 18 | 18 |
| 19 @interface BookmarkButtonCell(Private) | 19 @interface BookmarkButtonCell(Private) |
| 20 - (void)configureBookmarkButtonCell; | 20 - (void)configureBookmarkButtonCell; |
| 21 - (void)applyTextColor; | |
| 21 @end | 22 @end |
| 22 | 23 |
| 23 | 24 |
| 24 @implementation BookmarkButtonCell | 25 @implementation BookmarkButtonCell |
| 25 | 26 |
| 26 @synthesize startingChildIndex = startingChildIndex_; | 27 @synthesize startingChildIndex = startingChildIndex_; |
| 27 @synthesize drawFolderArrow = drawFolderArrow_; | 28 @synthesize drawFolderArrow = drawFolderArrow_; |
| 28 | 29 |
| 29 + (id)buttonCellForNode:(const BookmarkNode*)node | 30 + (id)buttonCellForNode:(const BookmarkNode*)node |
| 30 contextMenu:(NSMenu*)contextMenu | 31 contextMenu:(NSMenu*)contextMenu |
| 31 cellText:(NSString*)cellText | 32 cellText:(NSString*)cellText |
| 32 cellImage:(NSImage*)cellImage { | 33 cellImage:(NSImage*)cellImage { |
| 33 id buttonCell = | 34 id buttonCell = |
| 34 [[[BookmarkButtonCell alloc] initForNode:node | 35 [[[BookmarkButtonCell alloc] initForNode:node |
| 35 contextMenu:contextMenu | 36 contextMenu:contextMenu |
| 36 cellText:cellText | 37 cellText:cellText |
| 37 cellImage:cellImage] | 38 cellImage:cellImage] |
| 38 autorelease]; | 39 autorelease]; |
| 39 return buttonCell; | 40 return buttonCell; |
| 40 } | 41 } |
| 41 | 42 |
| 42 - (id)initForNode:(const BookmarkNode*)node | 43 - (id)initForNode:(const BookmarkNode*)node |
| 43 contextMenu:(NSMenu*)contextMenu | 44 contextMenu:(NSMenu*)contextMenu |
| 44 cellText:(NSString*)cellText | 45 cellText:(NSString*)cellText |
| 45 cellImage:(NSImage*)cellImage { | 46 cellImage:(NSImage*)cellImage { |
| 46 if ((self = [super initTextCell:cellText])) { | 47 if ((self = [super initTextCell:cellText])) { |
| 47 [self configureBookmarkButtonCell]; | 48 [self configureBookmarkButtonCell]; |
|
John Grabowski
2011/07/12 02:15:38
Minor nit: we now have 3 method calls with blank l
| |
| 48 | 49 |
| 50 [self setTextColor:[NSColor redColor]]; | |
| 51 | |
| 49 [self setBookmarkNode:node]; | 52 [self setBookmarkNode:node]; |
| 50 | 53 |
| 51 if (node) { | 54 if (node) { |
| 52 NSString* title = base::SysUTF16ToNSString(node->GetTitle()); | 55 NSString* title = base::SysUTF16ToNSString(node->GetTitle()); |
| 53 [self setBookmarkCellText:title image:cellImage]; | 56 [self setBookmarkCellText:title image:cellImage]; |
| 54 [self setMenu:contextMenu]; | 57 [self setMenu:contextMenu]; |
| 55 } else { | 58 } else { |
| 56 [self setEmpty:YES]; | 59 [self setEmpty:YES]; |
| 57 [self setBookmarkCellText:l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU) | 60 [self setBookmarkCellText:l10n_util::GetNSString(IDS_MENU_EMPTY_SUBMENU) |
| 58 image:nil]; | 61 image:nil]; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 UserMetrics::RecordAction(UserMetricsAction("BookmarkBarFolder_CtxMenu")); | 156 UserMetrics::RecordAction(UserMetricsAction("BookmarkBarFolder_CtxMenu")); |
| 154 } else { | 157 } else { |
| 155 UserMetrics::RecordAction(UserMetricsAction("BookmarkBar_CtxMenu")); | 158 UserMetrics::RecordAction(UserMetricsAction("BookmarkBar_CtxMenu")); |
| 156 } | 159 } |
| 157 | 160 |
| 158 [menu setRepresentedObject:[NSNumber numberWithLongLong:node->id()]]; | 161 [menu setRepresentedObject:[NSNumber numberWithLongLong:node->id()]]; |
| 159 | 162 |
| 160 return menu; | 163 return menu; |
| 161 } | 164 } |
| 162 | 165 |
| 163 // Unfortunately, NSCell doesn't already have something like this. | 166 - (void)setTitle:(NSString*)title { |
| 164 // TODO(jrg): consider placing in GTM. | 167 if ([[self title] isEqualTo:title]) |
| 168 return; | |
| 169 [super setTitle:title]; | |
| 170 [self applyTextColor]; | |
| 171 } | |
| 172 | |
| 165 - (void)setTextColor:(NSColor*)color { | 173 - (void)setTextColor:(NSColor*)color { |
| 174 if ([textColor_ isEqualTo:color]) | |
| 175 return; | |
| 176 textColor_.reset([color copy]); | |
| 177 [self applyTextColor]; | |
| 178 } | |
| 166 | 179 |
| 167 // We can't properly set the cell's text color without a control. | 180 - (void)applyTextColor { |
|
John Grabowski
2011/07/12 02:15:38
Add comment in code explaining why this is here.
| |
| 168 // In theory we could just save the next for later and wait until | |
| 169 // the cell is moved to a control, but there is no obvious way to | |
| 170 // accomplish that (e.g. no "cellDidMoveToControl" notification.) | |
| 171 DCHECK([self controlView]); | |
| 172 | |
| 173 scoped_nsobject<NSMutableParagraphStyle> style([NSMutableParagraphStyle new]); | 181 scoped_nsobject<NSMutableParagraphStyle> style([NSMutableParagraphStyle new]); |
| 174 [style setAlignment:NSLeftTextAlignment]; | 182 [style setAlignment:NSLeftTextAlignment]; |
| 175 NSDictionary* dict = [NSDictionary | 183 NSDictionary* dict = [NSDictionary |
| 176 dictionaryWithObjectsAndKeys:color, | 184 dictionaryWithObjectsAndKeys:textColor_, |
| 177 NSForegroundColorAttributeName, | 185 NSForegroundColorAttributeName, |
| 178 [self font], NSFontAttributeName, | 186 [self font], NSFontAttributeName, |
| 179 style.get(), NSParagraphStyleAttributeName, | 187 style.get(), NSParagraphStyleAttributeName, |
| 180 nil]; | 188 nil]; |
| 181 scoped_nsobject<NSAttributedString> ats([[NSAttributedString alloc] | 189 scoped_nsobject<NSAttributedString> ats([[NSAttributedString alloc] |
| 182 initWithString:[self title] | 190 initWithString:[self title] |
| 183 attributes:dict]); | 191 attributes:dict]); |
| 184 NSButton* button = static_cast<NSButton*>([self controlView]); | 192 [self setAttributedTitle:ats.get()]; |
| 185 if (button) { | |
| 186 DCHECK([button isKindOfClass:[NSButton class]]); | |
| 187 [button setAttributedTitle:ats.get()]; | |
| 188 } | |
| 189 } | 193 } |
| 190 | 194 |
| 191 // To implement "hover open a bookmark button to open the folder" | 195 // To implement "hover open a bookmark button to open the folder" |
| 192 // which feels like menus, we override NSButtonCell's mouseEntered: | 196 // which feels like menus, we override NSButtonCell's mouseEntered: |
| 193 // and mouseExited:, then and pass them along to our owning control. | 197 // and mouseExited:, then and pass them along to our owning control. |
| 194 // Note: as verified in a debugger, mouseEntered: does NOT increase | 198 // Note: as verified in a debugger, mouseEntered: does NOT increase |
| 195 // the retainCount of the cell or its owning control. | 199 // the retainCount of the cell or its owning control. |
| 196 - (void)mouseEntered:(NSEvent*)event { | 200 - (void)mouseEntered:(NSEvent*)event { |
| 197 [super mouseEntered:event]; | 201 [super mouseEntered:event]; |
| 198 [[self controlView] mouseEntered:event]; | 202 [[self controlView] mouseEntered:event]; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 NSRect drawRect = NSOffsetRect(imageRect, dX, dY); | 246 NSRect drawRect = NSOffsetRect(imageRect, dX, dY); |
| 243 [arrowImage_ drawInRect:drawRect | 247 [arrowImage_ drawInRect:drawRect |
| 244 fromRect:imageRect | 248 fromRect:imageRect |
| 245 operation:NSCompositeSourceOver | 249 operation:NSCompositeSourceOver |
| 246 fraction:[self isEnabled] ? 1.0 : 0.5 | 250 fraction:[self isEnabled] ? 1.0 : 0.5 |
| 247 neverFlipped:YES]; | 251 neverFlipped:YES]; |
| 248 } | 252 } |
| 249 } | 253 } |
| 250 | 254 |
| 251 @end | 255 @end |
| OLD | NEW |