OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/download/download_item_button.h" | 5 #import "chrome/browser/ui/cocoa/download/download_item_button.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
9 #import "chrome/browser/ui/cocoa/download/download_item_cell.h" | 9 #import "chrome/browser/ui/cocoa/download/download_item_cell.h" |
10 #import "chrome/browser/ui/cocoa/download/download_item_controller.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" | 11 #import "chrome/browser/ui/cocoa/download/download_shelf_context_menu_controller
.h" |
12 #import "chrome/browser/ui/cocoa/view_id_util.h" | 12 #import "chrome/browser/ui/cocoa/view_id_util.h" |
13 #import "ui/base/cocoa/nsview_additions.h" | 13 #import "ui/base/cocoa/nsview_additions.h" |
14 | 14 |
15 @implementation DownloadItemButton | 15 @implementation DownloadItemButton |
16 | 16 |
17 @synthesize download = downloadPath_; | 17 @synthesize download = downloadPath_; |
18 @synthesize controller = controller_; | 18 @synthesize controller = controller_; |
19 | 19 |
20 // Overridden from DraggableButton. | 20 // Overridden from DraggableButton. |
21 - (void)beginDrag:(NSEvent*)event { | 21 - (void)beginDrag:(NSEvent*)event { |
22 if (!downloadPath_.empty()) { | 22 if (!downloadPath_.empty()) { |
23 NSString* filename = base::SysUTF8ToNSString(downloadPath_.value()); | 23 NSString* filename = base::SysUTF8ToNSString(downloadPath_.value()); |
24 [self dragFile:filename fromRect:[self bounds] slideBack:YES event:event]; | 24 [self dragFile:filename fromRect:[self bounds] slideBack:YES event:event]; |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
| 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(); |
| 38 } |
| 39 |
28 // Override to show a context menu on mouse down if clicked over the context | 40 // Override to show a context menu on mouse down if clicked over the context |
29 // menu area. | 41 // menu area. |
30 - (void)mouseDown:(NSEvent*)event { | 42 - (void)mouseDown:(NSEvent*)event { |
31 DCHECK(controller_); | 43 DCHECK(controller_); |
32 // Override so that we can pop up a context menu on mouse down. | 44 // Override so that we can pop up a context menu on mouse down. |
33 NSCell* cell = [self cell]; | 45 NSCell* cell = [self cell]; |
34 DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]); | 46 DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]); |
35 if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) { | 47 if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) { |
36 [self.draggableButton mouseDownImpl:event]; | 48 [self.draggableButton mouseDownImpl:event]; |
37 } else { | 49 } else { |
38 base::scoped_nsobject<DownloadShelfContextMenuController> menuController( | |
39 [[DownloadShelfContextMenuController alloc] | |
40 initWithItemController:controller_ | |
41 withDelegate:self]); | |
42 | |
43 [cell setHighlighted:YES]; | 50 [cell setHighlighted:YES]; |
44 [NSMenu popUpContextMenu:[menuController menu] | 51 [self showContextMenu]; |
45 withEvent:[NSApp currentEvent] | |
46 forView:self]; | |
47 } | 52 } |
48 } | 53 } |
49 | 54 |
50 // Override to retain the controller, in case a closure is pumped that deletes | 55 // 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>. | 56 // the DownloadItemController while the menu is open <http://crbug.com/129826>. |
52 - (void)rightMouseDown:(NSEvent*)event { | 57 - (void)rightMouseDown:(NSEvent*)event { |
53 base::scoped_nsobject<DownloadItemController> ref([controller_ retain]); | 58 base::scoped_nsobject<DownloadItemController> ref([controller_ retain]); |
54 [super rightMouseDown:event]; | 59 [super rightMouseDown:event]; |
55 } | 60 } |
56 | 61 |
(...skipping 20 matching lines...) Expand all Loading... |
77 // ThemedWindowDrawing implementation. | 82 // ThemedWindowDrawing implementation. |
78 | 83 |
79 - (void)windowDidChangeTheme { | 84 - (void)windowDidChangeTheme { |
80 [self setNeedsDisplay:YES]; | 85 [self setNeedsDisplay:YES]; |
81 } | 86 } |
82 | 87 |
83 - (void)windowDidChangeActive { | 88 - (void)windowDidChangeActive { |
84 [self setNeedsDisplay:YES]; | 89 [self setNeedsDisplay:YES]; |
85 } | 90 } |
86 | 91 |
| 92 - (BOOL)showingContextMenu |
| 93 { |
| 94 return contextMenu_.get() != nil; |
| 95 } |
| 96 |
| 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 } |
| 102 } |
| 103 |
87 @end | 104 @end |
OLD | NEW |