[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / download / download_item_button.mm
blob67726e87a44dffc3bd5f9f2a8d09aed5c59d859a
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"
12 #import "chrome/browser/ui/cocoa/nsview_additions.h"
13 #import "chrome/browser/ui/cocoa/view_id_util.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];
25   }
28 // Override to show a context menu on mouse down if clicked over the context
29 // menu area.
30 - (void)mouseDown:(NSEvent*)event {
31   DCHECK(controller_);
32   // Override so that we can pop up a context menu on mouse down.
33   NSCell* cell = [self cell];
34   DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]);
35   if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) {
36     [self.draggableButton mouseDownImpl:event];
37   } else {
38     base::scoped_nsobject<DownloadShelfContextMenuController> menuController(
39         [[DownloadShelfContextMenuController alloc]
40             initWithItemController:controller_
41                       withDelegate:self]);
43     [cell setHighlighted:YES];
44     [NSMenu popUpContextMenu:[menuController menu]
45                    withEvent:[NSApp currentEvent]
46                      forView:self];
47   }
50 // Override to retain the controller, in case a closure is pumped that deletes
51 // the DownloadItemController while the menu is open <http://crbug.com/129826>.
52 - (void)rightMouseDown:(NSEvent*)event {
53   base::scoped_nsobject<DownloadItemController> ref([controller_ retain]);
54   [super rightMouseDown:event];
57 - (void)menuDidClose:(NSMenu*)menu {
58   [[self cell] setHighlighted:NO];
61 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event {
62   return YES;
65 - (BOOL)isOpaque {
66   // Make this control opaque so that sub-pixel anti-aliasing works when
67   // CoreAnimation is enabled.
68   return YES;
71 - (void)drawRect:(NSRect)rect {
72   NSView* downloadShelfView = [self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF];
73   [self cr_drawUsingAncestor:downloadShelfView inRect:rect];
74   [super drawRect:rect];
77 @end