1 // Copyright 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"
12 #import "chrome/browser/ui/cocoa/view_id_util.h"
13 #import "ui/base/cocoa/nsview_additions.h"
15 @implementation DownloadItemButton
17 @synthesize download = downloadPath_;
18 @synthesize controller = controller_;
20 // Overridden from DraggableButton.
21 - (void)beginDrag:(NSEvent*)event {
22 if (!downloadPath_.empty()) {
23 NSString* filename = base::SysUTF8ToNSString(downloadPath_.value());
24 [self dragFile:filename fromRect:[self bounds] slideBack:YES event:event];
28 - (void)showContextMenu {
29 base::scoped_nsobject<DownloadShelfContextMenuController> menuController(
30 [[DownloadShelfContextMenuController alloc]
31 initWithItemController:controller_
33 contextMenu_.reset([[menuController menu] retain]);
34 [NSMenu popUpContextMenu:contextMenu_.get()
35 withEvent:[NSApp currentEvent]
40 // Override to show a context menu on mouse down if clicked over the context
42 - (void)mouseDown:(NSEvent*)event {
44 // Override so that we can pop up a context menu on mouse down.
45 NSCell* cell = [self cell];
46 DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]);
47 if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) {
48 [self.draggableButton mouseDownImpl:event];
50 [cell setHighlighted:YES];
51 [self showContextMenu];
55 // Override to retain the controller, in case a closure is pumped that deletes
56 // the DownloadItemController while the menu is open <http://crbug.com/129826>.
57 - (void)rightMouseDown:(NSEvent*)event {
58 base::scoped_nsobject<DownloadItemController> ref([controller_ retain]);
59 [super rightMouseDown:event];
62 - (void)menuDidClose:(NSMenu*)menu {
63 [[self cell] setHighlighted:NO];
66 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event {
71 // Make this control opaque so that sub-pixel anti-aliasing works when
72 // CoreAnimation is enabled.
76 - (void)drawRect:(NSRect)rect {
77 NSView* downloadShelfView = [self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF];
78 [self cr_drawUsingAncestor:downloadShelfView inRect:rect];
79 [super drawRect:rect];
82 // ThemedWindowDrawing implementation.
84 - (void)windowDidChangeTheme {
85 [self setNeedsDisplay:YES];
88 - (void)windowDidChangeActive {
89 [self setNeedsDisplay:YES];
92 - (BOOL)showingContextMenu
94 return contextMenu_.get() != nil;
97 - (void)viewWillMoveToWindow:(NSWindow *)newWindow {
98 // If the DownloadItemButton's context menu is still visible, dismiss it.
100 [contextMenu_.get() cancelTrackingWithoutAnimation];