Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / download / download_item_button.mm
blobb2b6ae9cb369f6abcff4e13df8719469c5628ad3
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];
25   }
28 - (void)showContextMenu {
29   base::scoped_nsobject<DownloadShelfContextMenuController> menuController(
30       [[DownloadShelfContextMenuController alloc]
31           initWithItemController:controller_
32                     withDelegate:self]);
33   contextMenu_.reset([[menuController menu] retain]);
34   [NSMenu popUpContextMenu:contextMenu_.get()
35                  withEvent:[NSApp currentEvent]
36                    forView:self];
37   contextMenu_.reset();
40 // Override to show a context menu on mouse down if clicked over the context
41 // menu area.
42 - (void)mouseDown:(NSEvent*)event {
43   DCHECK(controller_);
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];
49   } else {
50     [cell setHighlighted:YES];
51     [self showContextMenu];
52   }
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 {
67   return YES;
70 - (BOOL)isOpaque {
71   // Make this control opaque so that sub-pixel anti-aliasing works when
72   // CoreAnimation is enabled.
73   return YES;
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.
99   if (!newWindow) {
100     [contextMenu_.get() cancelTrackingWithoutAnimation];
101   }
104 @end