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 showingContextMenu_ = YES; | |
34 [NSMenu popUpContextMenu:[menuController menu] | |
35 withEvent:[NSApp currentEvent] | |
36 forView:self]; | |
37 } | |
38 | |
28 // Override to show a context menu on mouse down if clicked over the context | 39 // Override to show a context menu on mouse down if clicked over the context |
29 // menu area. | 40 // menu area. |
30 - (void)mouseDown:(NSEvent*)event { | 41 - (void)mouseDown:(NSEvent*)event { |
31 DCHECK(controller_); | 42 DCHECK(controller_); |
32 // Override so that we can pop up a context menu on mouse down. | 43 // Override so that we can pop up a context menu on mouse down. |
33 NSCell* cell = [self cell]; | 44 NSCell* cell = [self cell]; |
34 DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]); | 45 DCHECK([cell respondsToSelector:@selector(isMouseOverButtonPart)]); |
35 if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) { | 46 if ([reinterpret_cast<DownloadItemCell*>(cell) isMouseOverButtonPart]) { |
36 [self.draggableButton mouseDownImpl:event]; | 47 [self.draggableButton mouseDownImpl:event]; |
37 } else { | 48 } else { |
38 base::scoped_nsobject<DownloadShelfContextMenuController> menuController( | |
39 [[DownloadShelfContextMenuController alloc] | |
40 initWithItemController:controller_ | |
41 withDelegate:self]); | |
42 | |
43 [cell setHighlighted:YES]; | 49 [cell setHighlighted:YES]; |
44 [NSMenu popUpContextMenu:[menuController menu] | 50 [self showContextMenu]; |
45 withEvent:[NSApp currentEvent] | |
46 forView:self]; | |
47 } | 51 } |
48 } | 52 } |
49 | 53 |
50 // Override to retain the controller, in case a closure is pumped that deletes | 54 // 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>. | 55 // the DownloadItemController while the menu is open <http://crbug.com/129826>. |
52 - (void)rightMouseDown:(NSEvent*)event { | 56 - (void)rightMouseDown:(NSEvent*)event { |
53 base::scoped_nsobject<DownloadItemController> ref([controller_ retain]); | 57 base::scoped_nsobject<DownloadItemController> ref([controller_ retain]); |
54 [super rightMouseDown:event]; | 58 [super rightMouseDown:event]; |
55 } | 59 } |
56 | 60 |
57 - (void)menuDidClose:(NSMenu*)menu { | 61 - (void)menuDidClose:(NSMenu*)menu { |
62 showingContextMenu_ = NO; | |
58 [[self cell] setHighlighted:NO]; | 63 [[self cell] setHighlighted:NO]; |
59 } | 64 } |
60 | 65 |
61 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event { | 66 - (BOOL)shouldDelayWindowOrderingForEvent:(NSEvent*)event { |
62 return YES; | 67 return YES; |
63 } | 68 } |
64 | 69 |
65 - (BOOL)isOpaque { | 70 - (BOOL)isOpaque { |
66 // Make this control opaque so that sub-pixel anti-aliasing works when | 71 // Make this control opaque so that sub-pixel anti-aliasing works when |
67 // CoreAnimation is enabled. | 72 // CoreAnimation is enabled. |
68 return YES; | 73 return YES; |
69 } | 74 } |
70 | 75 |
71 - (void)drawRect:(NSRect)rect { | 76 - (void)drawRect:(NSRect)rect { |
72 NSView* downloadShelfView = [self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF]; | 77 NSView* downloadShelfView = [self ancestorWithViewID:VIEW_ID_DOWNLOAD_SHELF]; |
73 [self cr_drawUsingAncestor:downloadShelfView inRect:rect]; | 78 [self cr_drawUsingAncestor:downloadShelfView inRect:rect]; |
74 [super drawRect:rect]; | 79 [super drawRect:rect]; |
75 } | 80 } |
76 | 81 |
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 showingContextMenu_; | |
95 } | |
96 | |
97 - (void)viewWillMoveToWindow:(NSWindow *)newWindow { | |
asanka
2015/05/08 17:12:47
Shall we trigger the menu close logic on the downl
shrike
2015/05/08 17:52:04
By "described above" do you mean your previous com
asanka
2015/05/12 01:27:33
Sorry, I was referring to https://codereview.chrom
| |
98 // If the DownloadItemButton's context menu is still visible, synthesize an | |
99 // Escape key event to hide it (there is no API to explicitly dismiss a | |
100 // pop up menu). | |
asanka
2015/05/08 17:12:47
What about NSMenu cancelTracking: ?
shrike
2015/05/08 17:52:04
Indeed, but the problem is that popUpContextMenu:
asanka
2015/05/12 01:27:33
Can we keep a reference to the DownloadShelfContex
| |
101 if (showingContextMenu_ && !newWindow) { | |
102 unichar escapeString[] = { 27, 0 }; | |
103 NSString* escapeCharacterString = | |
104 [NSString stringWithCharacters:escapeString length:1]; | |
105 NSEvent* escapeKeyEvent = | |
106 [NSEvent keyEventWithType:NSKeyDown | |
107 location:NSZeroPoint | |
108 modifierFlags:0 | |
109 timestamp:[[NSDate date] timeIntervalSinceNow] | |
110 windowNumber:[[self window] windowNumber] | |
111 context:NULL | |
112 characters:escapeCharacterString | |
113 charactersIgnoringModifiers:escapeCharacterString | |
114 isARepeat:NO | |
115 keyCode:53]; | |
116 [[NSApplication sharedApplication] postEvent:escapeKeyEvent atStart:YES]; | |
117 } | |
118 } | |
119 | |
87 @end | 120 @end |
OLD | NEW |