| 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 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_constants.h" | 9 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_constants.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_context_menu_cocoa_controlle
r.h" | 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_context_menu_cocoa_controlle
r.h" |
| 12 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
| 13 #import "components/bookmarks/browser/bookmark_model.h" | 13 #import "components/bookmarks/browser/bookmark_model.h" |
| 14 #include "content/public/browser/user_metrics.h" | 14 #include "content/public/browser/user_metrics.h" |
| 15 #import "ui/base/cocoa/nsview_additions.h" | 15 #import "ui/base/cocoa/nsview_additions.h" |
| 16 #include "ui/base/l10n/l10n_util_mac.h" | 16 #include "ui/base/l10n/l10n_util_mac.h" |
| 17 #include "ui/base/material_design/material_design_controller.h" | 17 #include "ui/base/material_design/material_design_controller.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/resources/grit/ui_resources.h" | 19 #include "ui/resources/grit/ui_resources.h" |
| 20 | 20 |
| 21 using base::UserMetricsAction; | 21 using base::UserMetricsAction; |
| 22 using bookmarks::BookmarkNode; | 22 using bookmarks::BookmarkNode; |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kHierarchyButtonXMargin = 4; | 26 // Padding on the right side of the arrow icon. |
| 27 const int kHierarchyButtonRightPadding = 4; |
| 28 |
| 29 // Padding on the left side of the arrow icon. |
| 30 int HierarchyButtonLeftPadding() { |
| 31 return ui::MaterialDesignController::IsModeMaterial() ? 11 : 2; |
| 32 } |
| 33 |
| 27 const int kIconTextSpacer = 4; | 34 const int kIconTextSpacer = 4; |
| 28 const int kTextRightPadding = 3; | 35 const int kTextRightPadding = 1; |
| 29 const int kIconLeftPadding = 3; | 36 const int kIconLeftPadding = 1; |
| 30 | 37 |
| 31 const int kDefaultFontSize = 12; | 38 const int kDefaultFontSize = 12; |
| 32 | 39 |
| 33 }; // namespace | 40 }; // namespace |
| 34 | 41 |
| 35 @interface BookmarkButtonCell(Private) | 42 @interface BookmarkButtonCell(Private) |
| 36 - (void)configureBookmarkButtonCell; | 43 - (void)configureBookmarkButtonCell; |
| 37 - (void)applyTextColor; | 44 - (void)applyTextColor; |
| 38 // Returns the title the button cell displays. Note that a button cell can | 45 // Returns the title the button cell displays. Note that a button cell can |
| 39 // have a title string assigned but it won't be visible if its image position | 46 // have a title string assigned but it won't be visible if its image position |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 // Return the space needed to display the image and title, with a little | 330 // Return the space needed to display the image and title, with a little |
| 324 // distance between them. | 331 // distance between them. |
| 325 cellSize = NSMakeSize(kIconLeftPadding + [[self image] size].width, | 332 cellSize = NSMakeSize(kIconLeftPadding + [[self image] size].width, |
| 326 bookmarks::kBookmarkButtonHeight); | 333 bookmarks::kBookmarkButtonHeight); |
| 327 NSString* title = [self visibleTitle]; | 334 NSString* title = [self visibleTitle]; |
| 328 if ([title length] > 0) { | 335 if ([title length] > 0) { |
| 329 CGFloat textWidth = | 336 CGFloat textWidth = |
| 330 [title sizeWithAttributes:[self titleTextAttributes]].width; | 337 [title sizeWithAttributes:[self titleTextAttributes]].width; |
| 331 cellSize.width += | 338 cellSize.width += |
| 332 kIconTextSpacer + std::ceil(textWidth) + kTextRightPadding; | 339 kIconTextSpacer + std::ceil(textWidth) + kTextRightPadding; |
| 340 } else { |
| 341 // Make buttons without visible titles 20pts wide (18 plus padding). |
| 342 cellSize.width = 18; |
| 333 } | 343 } |
| 334 } | 344 } |
| 335 | 345 |
| 336 if (drawFolderArrow_) { | 346 if (drawFolderArrow_) { |
| 337 cellSize.width += [arrowImage_ size].width + 2 * kHierarchyButtonXMargin; | 347 cellSize.width += [arrowImage_ size].width + |
| 348 HierarchyButtonLeftPadding() + |
| 349 kHierarchyButtonRightPadding; |
| 338 } | 350 } |
| 339 return cellSize; | 351 return cellSize; |
| 340 } | 352 } |
| 341 | 353 |
| 342 - (NSRect)imageRectForBounds:(NSRect)theRect { | 354 - (NSRect)imageRectForBounds:(NSRect)theRect { |
| 343 NSRect imageRect = [super imageRectForBounds:theRect]; | 355 NSRect imageRect = [super imageRectForBounds:theRect]; |
| 344 // In Material Design, add a little space between the image and the button's | 356 // In Material Design, add a little space between the image and the button's |
| 345 // left edge, but only if there's a visible title. | 357 // left edge, but only if there's a visible title. |
| 346 if (ui::MaterialDesignController::IsModeMaterial()) { | 358 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 347 imageRect.origin.y -= 1; | 359 imageRect.origin.y -= 1; |
| 348 if ([[self visibleTitle] length]) { | 360 if ([[self visibleTitle] length] > 0) { |
| 349 imageRect.origin.x += kIconLeftPadding; | 361 imageRect.origin.x += kIconLeftPadding; |
| 350 } | 362 } |
| 351 } | 363 } |
| 352 return imageRect; | 364 return imageRect; |
| 353 } | 365 } |
| 354 | 366 |
| 355 - (CGFloat)textStartXOffset { | 367 - (CGFloat)textStartXOffset { |
| 356 if (!ui::MaterialDesignController::IsModeMaterial()) { | 368 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 357 return [super textStartXOffset]; | 369 return [super textStartXOffset]; |
| 358 } | 370 } |
| 359 return kIconLeftPadding + [[self image] size].width + kIconTextSpacer; | 371 return kIconLeftPadding + [[self image] size].width + kIconTextSpacer; |
| 360 } | 372 } |
| 361 | 373 |
| 362 // Override cell drawing to add a submenu arrow like a real menu. | 374 // Override cell drawing to add a submenu arrow like a real menu. |
| 363 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 375 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 364 // First draw "everything else". | 376 // First draw "everything else". |
| 365 [super drawInteriorWithFrame:cellFrame inView:controlView]; | 377 [super drawInteriorWithFrame:cellFrame inView:controlView]; |
| 366 | 378 |
| 367 // If asked to do so, and if a folder, draw the arrow. | 379 // If asked to do so, and if a folder, draw the arrow. |
| 368 if (!drawFolderArrow_) | 380 if (!drawFolderArrow_) |
| 369 return; | 381 return; |
| 370 BookmarkButton* button = static_cast<BookmarkButton*>([self controlView]); | 382 BookmarkButton* button = static_cast<BookmarkButton*>([self controlView]); |
| 371 DCHECK([button respondsToSelector:@selector(isFolder)]); | 383 DCHECK([button respondsToSelector:@selector(isFolder)]); |
| 372 if ([button isFolder]) { | 384 if ([button isFolder]) { |
| 373 NSRect imageRect = NSZeroRect; | 385 NSRect imageRect = NSZeroRect; |
| 374 imageRect.size = [arrowImage_ size]; | 386 imageRect.size = [arrowImage_ size]; |
| 375 const CGFloat kArrowOffset = 1.0; // Required for proper centering. | 387 const CGFloat kArrowOffset = 1.0; // Required for proper centering. |
| 376 CGFloat dX = | 388 CGFloat dX = |
| 377 NSWidth(cellFrame) - NSWidth(imageRect) - kHierarchyButtonXMargin; | 389 NSWidth(cellFrame) - NSWidth(imageRect) - kHierarchyButtonRightPadding; |
| 378 CGFloat dY = (NSHeight(cellFrame) / 2.0) - (NSHeight(imageRect) / 2.0) + | 390 CGFloat dY = (NSHeight(cellFrame) / 2.0) - (NSHeight(imageRect) / 2.0) + |
| 379 kArrowOffset; | 391 kArrowOffset; |
| 380 NSRect drawRect = NSOffsetRect(imageRect, dX, dY); | 392 NSRect drawRect = NSOffsetRect(imageRect, dX, dY); |
| 381 [arrowImage_ drawInRect:drawRect | 393 [arrowImage_ drawInRect:drawRect |
| 382 fromRect:imageRect | 394 fromRect:imageRect |
| 383 operation:NSCompositeSourceOver | 395 operation:NSCompositeSourceOver |
| 384 fraction:[self isEnabled] ? 1.0 : 0.5 | 396 fraction:[self isEnabled] ? 1.0 : 0.5 |
| 385 respectFlipped:YES | 397 respectFlipped:YES |
| 386 hints:nil]; | 398 hints:nil]; |
| 387 } | 399 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 400 const CGFloat kLineWidth = [controlView cr_lineWidth]; | 412 const CGFloat kLineWidth = [controlView cr_lineWidth]; |
| 401 if ([self tag] == kMaterialStandardButtonTypeWithLimitedClickFeedback && | 413 if ([self tag] == kMaterialStandardButtonTypeWithLimitedClickFeedback && |
| 402 ![self isFolderButtonCell] && kLineWidth < 1) { | 414 ![self isFolderButtonCell] && kLineWidth < 1) { |
| 403 return -kLineWidth; | 415 return -kLineWidth; |
| 404 } | 416 } |
| 405 return 0.0; | 417 return 0.0; |
| 406 } | 418 } |
| 407 | 419 |
| 408 | 420 |
| 409 @end | 421 @end |
| OLD | NEW |