| 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/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
| 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_context_menu_cocoa_controlle
r.h" | 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_context_menu_cocoa_controlle
r.h" |
| 11 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 12 #import "components/bookmarks/browser/bookmark_model.h" | 12 #import "components/bookmarks/browser/bookmark_model.h" |
| 13 #include "content/public/browser/user_metrics.h" | 13 #include "content/public/browser/user_metrics.h" |
| 14 #include "ui/base/l10n/l10n_util_mac.h" | 14 #include "ui/base/l10n/l10n_util_mac.h" |
| 15 #include "ui/base/material_design/material_design_controller.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/resources/grit/ui_resources.h" | 17 #include "ui/resources/grit/ui_resources.h" |
| 17 | 18 |
| 18 using base::UserMetricsAction; | 19 using base::UserMetricsAction; |
| 19 using bookmarks::BookmarkNode; | 20 using bookmarks::BookmarkNode; |
| 20 | 21 |
| 22 namespace { |
| 23 |
| 21 const int kHierarchyButtonXMargin = 4; | 24 const int kHierarchyButtonXMargin = 4; |
| 25 const int kIconTextSpacer = 4; |
| 26 const int kTextRightPadding = 1; |
| 27 const int kIconLeftPadding = 3; |
| 28 |
| 29 }; // namespace |
| 22 | 30 |
| 23 @interface BookmarkButtonCell(Private) | 31 @interface BookmarkButtonCell(Private) |
| 24 - (void)configureBookmarkButtonCell; | 32 - (void)configureBookmarkButtonCell; |
| 25 - (void)applyTextColor; | 33 - (void)applyTextColor; |
| 34 // Returns the dictionary of attributes to associate with the button title. |
| 35 - (NSDictionary*)titleTextAttributes; |
| 26 @end | 36 @end |
| 27 | 37 |
| 28 | 38 |
| 29 @implementation BookmarkButtonCell | 39 @implementation BookmarkButtonCell |
| 30 | 40 |
| 31 @synthesize startingChildIndex = startingChildIndex_; | 41 @synthesize startingChildIndex = startingChildIndex_; |
| 32 @synthesize drawFolderArrow = drawFolderArrow_; | 42 @synthesize drawFolderArrow = drawFolderArrow_; |
| 33 | 43 |
| 34 + (id)buttonCellForNode:(const BookmarkNode*)node | 44 + (id)buttonCellForNode:(const BookmarkNode*)node |
| 35 text:(NSString*)text | 45 text:(NSString*)text |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 [self configureBookmarkButtonCell]; | 124 [self configureBookmarkButtonCell]; |
| 115 } | 125 } |
| 116 | 126 |
| 117 - (BOOL)isFolderButtonCell { | 127 - (BOOL)isFolderButtonCell { |
| 118 return NO; | 128 return NO; |
| 119 } | 129 } |
| 120 | 130 |
| 121 // Perform all normal init routines specific to the BookmarkButtonCell. | 131 // Perform all normal init routines specific to the BookmarkButtonCell. |
| 122 - (void)configureBookmarkButtonCell { | 132 - (void)configureBookmarkButtonCell { |
| 123 [self setButtonType:NSMomentaryPushInButton]; | 133 [self setButtonType:NSMomentaryPushInButton]; |
| 124 [self setBezelStyle:NSShadowlessSquareBezelStyle]; | |
| 125 [self setShowsBorderOnlyWhileMouseInside:YES]; | 134 [self setShowsBorderOnlyWhileMouseInside:YES]; |
| 126 [self setControlSize:NSSmallControlSize]; | 135 [self setControlSize:NSSmallControlSize]; |
| 127 [self setAlignment:NSLeftTextAlignment]; | 136 [self setAlignment:NSLeftTextAlignment]; |
| 128 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | 137 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 138 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
| 139 [self setBezelStyle:NSShadowlessSquareBezelStyle]; |
| 140 } else { |
| 141 [self setFont:[NSFont systemFontOfSize:12]]; |
| 142 [self setBordered:NO]; |
| 143 [self setBezeled:NO]; |
| 144 } |
| 129 [self setWraps:NO]; | 145 [self setWraps:NO]; |
| 130 // NSLineBreakByTruncatingMiddle seems more common on OSX but let's | 146 // NSLineBreakByTruncatingMiddle seems more common on OSX but let's |
| 131 // try to match Windows for a bit to see what happens. | 147 // try to match Windows for a bit to see what happens. |
| 132 [self setLineBreakMode:NSLineBreakByTruncatingTail]; | 148 [self setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 133 | 149 |
| 134 // The overflow button chevron bitmap is not 16 units high, so it'd be scaled | 150 // The overflow button chevron bitmap is not 16 units high, so it'd be scaled |
| 135 // at paint time without this. | 151 // at paint time without this. |
| 136 [self setImageScaling:NSImageScaleNone]; | 152 [self setImageScaling:NSImageScaleNone]; |
| 137 | 153 |
| 138 // Theming doesn't work for bookmark buttons yet (cell text is chucked). | 154 // Theming doesn't work for bookmark buttons yet (cell text is chucked). |
| 139 [super setShouldTheme:NO]; | 155 [super setShouldTheme:NO]; |
| 140 } | 156 } |
| 141 | 157 |
| 142 - (BOOL)empty { | 158 - (BOOL)empty { |
| 143 return empty_; | 159 return empty_; |
| 144 } | 160 } |
| 145 | 161 |
| 146 - (void)setEmpty:(BOOL)empty { | 162 - (void)setEmpty:(BOOL)empty { |
| 147 empty_ = empty; | 163 empty_ = empty; |
| 148 [self setShowsBorderOnlyWhileMouseInside:!empty]; | 164 [self setShowsBorderOnlyWhileMouseInside:!empty]; |
| 149 } | 165 } |
| 150 | 166 |
| 151 - (NSSize)cellSizeForBounds:(NSRect)aRect { | 167 - (NSSize)cellSizeForBounds:(NSRect)aRect { |
| 168 // There's no bezel or border in Material Design so return cellSize. |
| 169 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 170 NSSize size = [self cellSize]; |
| 171 size.width = std::min(aRect.size.width, size.width); |
| 172 size.height = std::min(aRect.size.height, size.height); |
| 173 return size; |
| 174 } |
| 152 NSSize size = [super cellSizeForBounds:aRect]; | 175 NSSize size = [super cellSizeForBounds:aRect]; |
| 153 // Cocoa seems to slightly underestimate how much space we need, so we | 176 // Cocoa seems to slightly underestimate how much space we need, so we |
| 154 // compensate here to avoid a clipped rendering. | 177 // compensate here to avoid a clipped rendering. |
| 155 size.width += 2; | 178 size.width += 2; |
| 156 size.height += 4; | 179 size.height += 4; |
| 157 return size; | 180 return size; |
| 158 } | 181 } |
| 159 | 182 |
| 160 - (void)setBookmarkCellText:(NSString*)title | 183 - (void)setBookmarkCellText:(NSString*)title |
| 161 image:(NSImage*)image { | 184 image:(NSImage*)image { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 278 |
| 256 - (void)setDrawFolderArrow:(BOOL)draw { | 279 - (void)setDrawFolderArrow:(BOOL)draw { |
| 257 drawFolderArrow_ = draw; | 280 drawFolderArrow_ = draw; |
| 258 if (draw && !arrowImage_) { | 281 if (draw && !arrowImage_) { |
| 259 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 282 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 260 arrowImage_.reset( | 283 arrowImage_.reset( |
| 261 [rb.GetNativeImageNamed(IDR_MENU_HIERARCHY_ARROW).ToNSImage() retain]); | 284 [rb.GetNativeImageNamed(IDR_MENU_HIERARCHY_ARROW).ToNSImage() retain]); |
| 262 } | 285 } |
| 263 } | 286 } |
| 264 | 287 |
| 288 - (NSDictionary*)titleTextAttributes { |
| 289 base::scoped_nsobject<NSMutableParagraphStyle> style( |
| 290 [NSMutableParagraphStyle new]); |
| 291 [style setAlignment:NSTextAlignmentNatural]; |
| 292 [style setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 293 NSColor* textColor = nil; |
| 294 if (![self isEnabled]) { |
| 295 textColor = [textColor colorWithAlphaComponent:0.5]; |
| 296 } else { |
| 297 textColor = textColor_ == nil ? [NSColor blackColor] : textColor_.get(); |
| 298 } |
| 299 NSFont* theFont = [self font]; |
| 300 DCHECK(theFont); |
| 301 return @{ |
| 302 NSFontAttributeName : theFont, |
| 303 NSForegroundColorAttributeName : textColor, |
| 304 NSParagraphStyleAttributeName : style.get(), |
| 305 NSKernAttributeName : [NSNumber numberWithFloat:0.2] |
| 306 }; |
| 307 } |
| 308 |
| 265 // Add extra size for the arrow so it doesn't overlap the text. | 309 // Add extra size for the arrow so it doesn't overlap the text. |
| 266 // Does not sanity check to be sure this is actually a folder node. | 310 // Does not sanity check to be sure this is actually a folder node. |
| 267 - (NSSize)cellSize { | 311 - (NSSize)cellSize { |
| 268 NSSize cellSize = [super cellSize]; | 312 NSSize cellSize = NSZeroSize; |
| 313 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 314 cellSize = [super cellSize]; |
| 315 } else { |
| 316 // Return the space needed to display the image and text, with a little |
| 317 // distance between them. |
| 318 cellSize = [[self image] size]; |
| 319 cellSize.width += kIconLeftPadding; |
| 320 NSString* title = [self title]; |
| 321 if ([title length] > 0) { |
| 322 CGFloat textWidth = |
| 323 [title sizeWithAttributes:[self titleTextAttributes]].width; |
| 324 cellSize.width += |
| 325 kIconTextSpacer + std::ceil(textWidth) + kTextRightPadding; |
| 326 } |
| 327 } |
| 328 |
| 269 if (drawFolderArrow_) { | 329 if (drawFolderArrow_) { |
| 270 cellSize.width += [arrowImage_ size].width + 2 * kHierarchyButtonXMargin; | 330 cellSize.width += [arrowImage_ size].width + 2 * kHierarchyButtonXMargin; |
| 271 } | 331 } |
| 272 return cellSize; | 332 return cellSize; |
| 273 } | 333 } |
| 274 | 334 |
| 335 - (NSRect)imageRectForBounds:(NSRect)theRect { |
| 336 NSRect imageRect = [super imageRectForBounds:theRect]; |
| 337 // In Material Design, add a little space between the image and the button's |
| 338 // left edge. |
| 339 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 340 imageRect.origin.x += kIconLeftPadding; |
| 341 } |
| 342 return imageRect; |
| 343 } |
| 344 |
| 345 - (CGFloat)textStartXOffset { |
| 346 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 347 return [super textStartXOffset]; |
| 348 } |
| 349 return kIconLeftPadding + [[self image] size].width + kIconTextSpacer; |
| 350 } |
| 351 |
| 275 // Override cell drawing to add a submenu arrow like a real menu. | 352 // Override cell drawing to add a submenu arrow like a real menu. |
| 276 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 353 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 277 // First draw "everything else". | 354 // First draw "everything else". |
| 278 [super drawInteriorWithFrame:cellFrame inView:controlView]; | 355 [super drawInteriorWithFrame:cellFrame inView:controlView]; |
| 279 | 356 |
| 280 // If asked to do so, and if a folder, draw the arrow. | 357 // If asked to do so, and if a folder, draw the arrow. |
| 281 if (!drawFolderArrow_) | 358 if (!drawFolderArrow_) |
| 282 return; | 359 return; |
| 283 BookmarkButton* button = static_cast<BookmarkButton*>([self controlView]); | 360 BookmarkButton* button = static_cast<BookmarkButton*>([self controlView]); |
| 284 DCHECK([button respondsToSelector:@selector(isFolder)]); | 361 DCHECK([button respondsToSelector:@selector(isFolder)]); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 298 respectFlipped:YES | 375 respectFlipped:YES |
| 299 hints:nil]; | 376 hints:nil]; |
| 300 } | 377 } |
| 301 } | 378 } |
| 302 | 379 |
| 303 - (int)verticalTextOffset { | 380 - (int)verticalTextOffset { |
| 304 return 0; | 381 return 0; |
| 305 } | 382 } |
| 306 | 383 |
| 307 @end | 384 @end |
| OLD | NEW |