Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/browser_action_button.h" | 5 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #include "ui/gfx/canvas_skia_paint.h" | 28 #include "ui/gfx/canvas_skia_paint.h" |
| 29 #include "ui/gfx/geometry/rect.h" | 29 #include "ui/gfx/geometry/rect.h" |
| 30 #include "ui/gfx/image/image.h" | 30 #include "ui/gfx/image/image.h" |
| 31 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 31 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 32 | 32 |
| 33 NSString* const kBrowserActionButtonDraggingNotification = | 33 NSString* const kBrowserActionButtonDraggingNotification = |
| 34 @"BrowserActionButtonDraggingNotification"; | 34 @"BrowserActionButtonDraggingNotification"; |
| 35 NSString* const kBrowserActionButtonDragEndNotification = | 35 NSString* const kBrowserActionButtonDragEndNotification = |
| 36 @"BrowserActionButtonDragEndNotification"; | 36 @"BrowserActionButtonDragEndNotification"; |
| 37 | 37 |
| 38 static const CGFloat kBrowserActionBadgeOriginYOffset = 5; | |
| 39 static const CGFloat kAnimationDuration = 0.2; | 38 static const CGFloat kAnimationDuration = 0.2; |
| 40 static const CGFloat kMinimumDragDistance = 5; | 39 static const CGFloat kMinimumDragDistance = 5; |
| 41 | 40 |
| 42 @interface BrowserActionButton () | 41 @interface BrowserActionButton () |
| 43 - (void)endDrag; | 42 - (void)endDrag; |
| 44 - (void)updateHighlightedState; | 43 - (void)updateHighlightedState; |
| 45 - (MenuController*)contextMenuController; | 44 - (MenuController*)contextMenuController; |
| 46 - (void)menuDidClose:(NSNotification*)notification; | 45 - (void)menuDidClose:(NSNotification*)notification; |
| 47 @end | 46 @end |
| 48 | 47 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 [[owner_ cell] setHighlighted:YES]; | 163 [[owner_ cell] setHighlighted:YES]; |
| 165 [[owner_ menu] popUpMenuPositioningItem:nil | 164 [[owner_ menu] popUpMenuPositioningItem:nil |
| 166 atLocation:menuPoint | 165 atLocation:menuPoint |
| 167 inView:owner_]; | 166 inView:owner_]; |
| 168 [[owner_ cell] setHighlighted:NO]; | 167 [[owner_ cell] setHighlighted:NO]; |
| 169 contextMenuRunning_ = false; | 168 contextMenuRunning_ = false; |
| 170 // When the menu closed, the ViewController should have popped itself back in. | 169 // When the menu closed, the ViewController should have popped itself back in. |
| 171 DCHECK(![controller_ toolbarActionsBar]->popped_out_action()); | 170 DCHECK(![controller_ toolbarActionsBar]->popped_out_action()); |
| 172 } | 171 } |
| 173 | 172 |
| 174 @interface BrowserActionCell (Internals) | |
| 175 - (void)drawBadgeWithinFrame:(NSRect)frame | |
| 176 forWebContents:(content::WebContents*)webContents; | |
| 177 @end | |
| 178 | |
| 179 @implementation BrowserActionButton | 173 @implementation BrowserActionButton |
| 180 | 174 |
| 181 @synthesize isBeingDragged = isBeingDragged_; | 175 @synthesize isBeingDragged = isBeingDragged_; |
| 182 | 176 |
| 183 + (Class)cellClass { | 177 + (Class)cellClass { |
| 184 return [BrowserActionCell class]; | 178 return [BrowserActionCell class]; |
| 185 } | 179 } |
| 186 | 180 |
| 187 - (id)initWithFrame:(NSRect)frame | 181 - (id)initWithFrame:(NSRect)frame |
| 188 viewController:(ToolbarActionViewController*)viewController | 182 viewController:(ToolbarActionViewController*)viewController |
| 189 controller:(BrowserActionsController*)controller { | 183 controller:(BrowserActionsController*)controller { |
| 190 if ((self = [super initWithFrame:frame])) { | 184 if ((self = [super initWithFrame:frame])) { |
| 191 BrowserActionCell* cell = [[[BrowserActionCell alloc] init] autorelease]; | 185 BrowserActionCell* cell = [[[BrowserActionCell alloc] init] autorelease]; |
| 192 // [NSButton setCell:] warns to NOT use setCell: other than in the | 186 // [NSButton setCell:] warns to NOT use setCell: other than in the |
| 193 // initializer of a control. However, we are using a basic | 187 // initializer of a control. However, we are using a basic |
| 194 // NSButton whose initializer does not take an NSCell as an | 188 // NSButton whose initializer does not take an NSCell as an |
| 195 // object. To honor the assumed semantics, we do nothing with | 189 // object. To honor the assumed semantics, we do nothing with |
| 196 // NSButton between alloc/init and setCell:. | 190 // NSButton between alloc/init and setCell:. |
| 197 [self setCell:cell]; | 191 [self setCell:cell]; |
| 198 | 192 |
| 199 browserActionsController_ = controller; | 193 browserActionsController_ = controller; |
| 200 viewController_ = viewController; | 194 viewController_ = viewController; |
| 201 viewControllerDelegate_.reset( | 195 viewControllerDelegate_.reset( |
| 202 new ToolbarActionViewDelegateBridge(self, controller, viewController)); | 196 new ToolbarActionViewDelegateBridge(self, controller, viewController)); |
| 203 | 197 |
| 204 [cell setBrowserActionsController:controller]; | 198 [cell setBrowserActionsController:controller]; |
| 205 [cell setViewController:viewController_]; | |
| 206 [cell | 199 [cell |
| 207 accessibilitySetOverrideValue:base::SysUTF16ToNSString( | 200 accessibilitySetOverrideValue:base::SysUTF16ToNSString( |
| 208 viewController_->GetAccessibleName([controller currentWebContents])) | 201 viewController_->GetAccessibleName([controller currentWebContents])) |
| 209 forAttribute:NSAccessibilityDescriptionAttribute]; | 202 forAttribute:NSAccessibilityDescriptionAttribute]; |
| 210 [cell setImageID:IDR_BROWSER_ACTION | 203 [cell setImageID:IDR_BROWSER_ACTION |
| 211 forButtonState:image_button_cell::kDefaultState]; | 204 forButtonState:image_button_cell::kDefaultState]; |
| 212 [cell setImageID:IDR_BROWSER_ACTION_H | 205 [cell setImageID:IDR_BROWSER_ACTION_H |
| 213 forButtonState:image_button_cell::kHoverState]; | 206 forButtonState:image_button_cell::kHoverState]; |
| 214 [cell setImageID:IDR_BROWSER_ACTION_P | 207 [cell setImageID:IDR_BROWSER_ACTION_P |
| 215 forButtonState:image_button_cell::kPressedState]; | 208 forButtonState:image_button_cell::kPressedState]; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 [[self cell] setImageID:IDR_BROWSER_ACTION_R | 393 [[self cell] setImageID:IDR_BROWSER_ACTION_R |
| 401 forButtonState:image_button_cell::kDefaultState]; | 394 forButtonState:image_button_cell::kDefaultState]; |
| 402 } else { | 395 } else { |
| 403 [[self cell] setImageID:IDR_BROWSER_ACTION | 396 [[self cell] setImageID:IDR_BROWSER_ACTION |
| 404 forButtonState:image_button_cell::kDefaultState]; | 397 forButtonState:image_button_cell::kDefaultState]; |
| 405 } | 398 } |
| 406 | 399 |
| 407 base::string16 tooltip = viewController_->GetTooltip(webContents); | 400 base::string16 tooltip = viewController_->GetTooltip(webContents); |
| 408 [self setToolTip:(tooltip.empty() ? nil : base::SysUTF16ToNSString(tooltip))]; | 401 [self setToolTip:(tooltip.empty() ? nil : base::SysUTF16ToNSString(tooltip))]; |
| 409 | 402 |
| 410 gfx::Image image = viewController_->GetIcon(webContents); | 403 NSRect frame = [self frame]; |
| 404 gfx::Image image = | |
| 405 viewController_->GetIcon(webContents, | |
| 406 gfx::Size(NSWidth(frame), NSHeight(frame))); | |
|
Avi (use Gerrit)
2015/07/02 00:33:23
CGSizeFromNSSize(frame.size) should do it.
Devlin
2015/07/06 19:16:07
Dang, I was looking for that method, but couldn't
| |
| 411 | 407 |
| 412 if (!image.IsEmpty()) | 408 if (!image.IsEmpty()) |
| 413 [self setImage:image.ToNSImage()]; | 409 [self setImage:image.ToNSImage()]; |
| 414 | 410 |
| 415 [self setEnabled:viewController_->IsEnabled(webContents)]; | 411 [self setEnabled:viewController_->IsEnabled(webContents)]; |
| 416 | 412 |
| 417 [self setNeedsDisplay:YES]; | 413 [self setNeedsDisplay:YES]; |
| 418 } | 414 } |
| 419 | 415 |
| 420 - (void)onRemoved { | 416 - (void)onRemoved { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 NSMakeRect(std::floor((NSWidth(bounds) - imageSize.width) / 2.0), | 456 NSMakeRect(std::floor((NSWidth(bounds) - imageSize.width) / 2.0), |
| 461 std::floor((NSHeight(bounds) - imageSize.height) / 2.0), | 457 std::floor((NSHeight(bounds) - imageSize.height) / 2.0), |
| 462 imageSize.width, imageSize.height); | 458 imageSize.width, imageSize.height); |
| 463 [actionImage drawInRect:imageRect | 459 [actionImage drawInRect:imageRect |
| 464 fromRect:NSZeroRect | 460 fromRect:NSZeroRect |
| 465 operation:NSCompositeSourceOver | 461 operation:NSCompositeSourceOver |
| 466 fraction:1.0 | 462 fraction:1.0 |
| 467 respectFlipped:YES | 463 respectFlipped:YES |
| 468 hints:nil]; | 464 hints:nil]; |
| 469 | 465 |
| 470 bounds.origin.y += kBrowserActionBadgeOriginYOffset; | |
| 471 [[self cell] drawBadgeWithinFrame:bounds | |
| 472 forWebContents: | |
| 473 [browserActionsController_ currentWebContents]]; | |
| 474 | |
| 475 [image unlockFocus]; | 466 [image unlockFocus]; |
| 476 return image; | 467 return image; |
| 477 } | 468 } |
| 478 | 469 |
| 479 - (NSMenu*)menu { | 470 - (NSMenu*)menu { |
| 480 // Hack: Since Cocoa doesn't support menus-running-in-menus (see also comment | 471 // Hack: Since Cocoa doesn't support menus-running-in-menus (see also comment |
| 481 // in -rightMouseDown:), it doesn't launch the menu for an overflowed action | 472 // in -rightMouseDown:), it doesn't launch the menu for an overflowed action |
| 482 // on a Control-click. Even more unfortunate, it doesn't even pass us the | 473 // on a Control-click. Even more unfortunate, it doesn't even pass us the |
| 483 // mouseDown event for control clicks. However, it does call -menuForEvent:, | 474 // mouseDown event for control clicks. However, it does call -menuForEvent:, |
| 484 // which in turn calls -menu:, so we can tap in here and show the menu | 475 // which in turn calls -menu:, so we can tap in here and show the menu |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 521 | 512 |
| 522 - (void)setTestContextMenu:(NSMenu*)testContextMenu { | 513 - (void)setTestContextMenu:(NSMenu*)testContextMenu { |
| 523 testContextMenu_ = testContextMenu; | 514 testContextMenu_ = testContextMenu; |
| 524 } | 515 } |
| 525 | 516 |
| 526 @end | 517 @end |
| 527 | 518 |
| 528 @implementation BrowserActionCell | 519 @implementation BrowserActionCell |
| 529 | 520 |
| 530 @synthesize browserActionsController = browserActionsController_; | 521 @synthesize browserActionsController = browserActionsController_; |
| 531 @synthesize viewController = viewController_; | |
| 532 | |
| 533 - (void)drawBadgeWithinFrame:(NSRect)frame | |
| 534 forWebContents:(content::WebContents*)webContents { | |
| 535 gfx::CanvasSkiaPaint canvas(frame, false); | |
| 536 canvas.set_composite_alpha(true); | |
| 537 gfx::Rect boundingRect(NSRectToCGRect(frame)); | |
| 538 viewController_->PaintExtra(&canvas, boundingRect, webContents); | |
| 539 } | |
| 540 | 522 |
| 541 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 523 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 542 gfx::ScopedNSGraphicsContextSaveGState scopedGState; | 524 gfx::ScopedNSGraphicsContextSaveGState scopedGState; |
| 543 [super drawWithFrame:cellFrame inView:controlView]; | 525 [super drawWithFrame:cellFrame inView:controlView]; |
| 544 DCHECK(viewController_); | 526 |
| 545 content::WebContents* webContents = | |
| 546 [browserActionsController_ currentWebContents]; | |
| 547 const NSSize imageSize = self.image.size; | 527 const NSSize imageSize = self.image.size; |
| 548 const NSRect imageRect = | 528 const NSRect imageRect = |
| 549 NSMakeRect(std::floor((NSWidth(cellFrame) - imageSize.width) / 2.0), | 529 NSMakeRect(std::floor((NSWidth(cellFrame) - imageSize.width) / 2.0), |
| 550 std::floor((NSHeight(cellFrame) - imageSize.height) / 2.0), | 530 std::floor((NSHeight(cellFrame) - imageSize.height) / 2.0), |
| 551 imageSize.width, imageSize.height); | 531 imageSize.width, imageSize.height); |
| 552 [self.image drawInRect:imageRect | 532 [self.image drawInRect:imageRect |
| 553 fromRect:NSZeroRect | 533 fromRect:NSZeroRect |
| 554 operation:NSCompositeSourceOver | 534 operation:NSCompositeSourceOver |
| 555 fraction:1.0 | 535 fraction:1.0 |
| 556 respectFlipped:YES | 536 respectFlipped:YES |
| 557 hints:nil]; | 537 hints:nil]; |
| 558 | |
| 559 cellFrame.origin.y += kBrowserActionBadgeOriginYOffset; | |
| 560 [self drawBadgeWithinFrame:cellFrame | |
| 561 forWebContents:webContents]; | |
| 562 } | 538 } |
| 563 | 539 |
| 564 - (ui::ThemeProvider*)themeProviderForWindow:(NSWindow*)window { | 540 - (ui::ThemeProvider*)themeProviderForWindow:(NSWindow*)window { |
| 565 ui::ThemeProvider* themeProvider = [window themeProvider]; | 541 ui::ThemeProvider* themeProvider = [window themeProvider]; |
| 566 if (!themeProvider) | 542 if (!themeProvider) |
| 567 themeProvider = | 543 themeProvider = |
| 568 [[browserActionsController_ browser]->window()->GetNativeWindow() | 544 [[browserActionsController_ browser]->window()->GetNativeWindow() |
| 569 themeProvider]; | 545 themeProvider]; |
| 570 return themeProvider; | 546 return themeProvider; |
| 571 } | 547 } |
| 572 | 548 |
| 573 @end | 549 @end |
| OLD | NEW |