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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/download/download_item_button.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "chrome/browser/ui/cocoa/download/download_item_cell.h"
10 #import "chrome/browser/ui/cocoa/download/download_item_controller.h"
11 #import "chrome/browser/ui/cocoa/download/download_shelf_context_menu_controller.h"
13 @implementation DownloadItemButton
15 @synthesize download = downloadPath_;
16 @synthesize controller = controller_;
18 // Overridden from DraggableButton.
19 - (void)beginDrag:(NSEvent*)event {
20 if (!downloadPath_.empty()) {
21 NSString* filename = base::SysUTF8ToNSString(downloadPath_.value());
22 [self dragFile:filename fromRect:[self bounds] slideBack:YES event:event];
26 // Override to show a context menu on mouse down if clicked over the context
28 - (void)mouseDown:(NSEvent*)event {
30 // Override so that we can pop up a context menu on mouse down.
31 NSCell* cell = [self cell];
32 DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]);
33 if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) {
34 [self.draggableButton mouseDownImpl:event];
36 base::scoped_nsobject<DownloadShelfContextMenuController> menuController(
37 [[DownloadShelfContextMenuController alloc]
38 initWithItemController:controller_
41 [cell setHighlighted:YES];
42 [NSMenu popUpContextMenu:[menuController menu]
43 withEvent:[NSApp currentEvent]
48 // Override to retain the controller, in case a closure is pumped that deletes
49 // the DownloadItemController while the menu is open <http://crbug.com/129826>.
50 - (void)rightMouseDown:(NSEvent*)event {
51 base::scoped_nsobject<DownloadItemController> ref([controller_ retain]);
52 [super rightMouseDown:event];
55 - (void)menuDidClose:(NSMenu*)menu {
56 [[self cell] setHighlighted:NO];
59 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event {