1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/browser_theme_provider.h"
#include "chrome/browser/ui/browser.h"
#import "chrome/browser/ui/cocoa/animatable_view.h"
#include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#include "chrome/browser/ui/cocoa/download/download_item_controller.h"
#include "chrome/browser/ui/cocoa/download/download_shelf_mac.h"
#import "chrome/browser/ui/cocoa/download/download_shelf_view.h"
#import "chrome/browser/ui/cocoa/hyperlink_button_cell.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h"
// Download shelf autoclose behavior:
//
// The download shelf autocloses if all of this is true:
// 1) An item on the shelf has just been opened.
// 2) All remaining items on the shelf have been opened in the past.
// 3) The mouse leaves the shelf and remains off the shelf for 5 seconds.
//
// If the mouse re-enters the shelf within the 5 second grace period, the
// autoclose is canceled. An autoclose can only be scheduled in response to a
// shelf item being opened or removed. If an item is opened and then the
// resulting autoclose is canceled, subsequent mouse exited events will NOT
// trigger an autoclose.
//
// If the shelf is manually closed while a download is still in progress, that
// download is marked as "opened" for these purposes. If the shelf is later
// reopened, these previously-in-progress download will not block autoclose,
// even if that download was never actually clicked on and opened.
namespace {
// Max number of download views we'll contain. Any time a view is added and
// we already have this many download views, one is removed.
const size_t kMaxDownloadItemCount = 16;
// Horizontal padding between two download items.
const int kDownloadItemPadding = 0;
// Duration for the open-new-leftmost-item animation, in seconds.
const NSTimeInterval kDownloadItemOpenDuration = 0.8;
// Duration for download shelf closing animation, in seconds.
const NSTimeInterval kDownloadShelfCloseDuration = 0.12;
// Amount of time between when the mouse is moved off the shelf and the shelf is
// autoclosed, in seconds.
const NSTimeInterval kAutoCloseDelaySeconds = 5;
} // namespace
@interface DownloadShelfController(Private)
- (void)showDownloadShelf:(BOOL)enable;
- (void)layoutItems:(BOOL)skipFirst;
- (void)closed;
- (BOOL)canAutoClose;
- (void)updateTheme;
- (void)themeDidChangeNotification:(NSNotification*)notification;
- (void)viewFrameDidChange:(NSNotification*)notification;
- (void)installTrackingArea;
- (void)cancelAutoCloseAndRemoveTrackingArea;
@end
@implementation DownloadShelfController
- (id)initWithBrowser:(Browser*)browser
resizeDelegate:(id<ViewResizer>)resizeDelegate {
if ((self = [super initWithNibName:@"DownloadShelf"
bundle:mac_util::MainAppBundle()])) {
resizeDelegate_ = resizeDelegate;
maxShelfHeight_ = NSHeight([[self view] bounds]);
currentShelfHeight_ = maxShelfHeight_;
// Reset the download shelf's frame height to zero. It will be properly
// positioned and sized the first time we try to set its height. (Just
// setting the rect to NSZeroRect does not work: it confuses Cocoa's view
// layout logic. If the shelf's width is too small, cocoa makes the download
// item container view wider than the browser window).
NSRect frame = [[self view] frame];
frame.size.height = 0;
[[self view] setFrame:frame];
downloadItemControllers_.reset([[NSMutableArray alloc] init]);
bridge_.reset(new DownloadShelfMac(browser, self));
}
return self;
}
- (void)awakeFromNib {
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(themeDidChangeNotification:)
name:kBrowserThemeDidChangeNotification
object:nil];
[[self animatableView] setResizeDelegate:resizeDelegate_];
[[self view] setPostsFrameChangedNotifications:YES];
[defaultCenter addObserver:self
selector:@selector(viewFrameDidChange:)
name:NSViewFrameDidChangeNotification
object:[self view]];
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
NSImage* favicon = rb.GetNativeImageNamed(IDR_DOWNLOADS_FAVICON);
DCHECK(favicon);
[image_ setImage:favicon];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self cancelAutoCloseAndRemoveTrackingArea];
// The controllers will unregister themselves as observers when they are
// deallocated. No need to do that here.
[super dealloc];
}
// Called after the current theme has changed.
- (void)themeDidChangeNotification:(NSNotification*)notification {
[self updateTheme];
}
// Called after the frame's rect has changed; usually when the height is
// animated.
- (void)viewFrameDidChange:(NSNotification*)notification {
// Anchor subviews at the top of |view|, so that it looks like the shelf
// is sliding out.
CGFloat newShelfHeight = NSHeight([[self view] frame]);
if (newShelfHeight == currentShelfHeight_)
return;
for (NSView* view in [[self view] subviews]) {
NSRect frame = [view frame];
frame.origin.y -= currentShelfHeight_ - newShelfHeight;
[view setFrame:frame];
}
currentShelfHeight_ = newShelfHeight;
}
// Adapt appearance to the current theme. Called after theme changes and before
// this is shown for the first time.
- (void)updateTheme {
NSColor* color = nil;
if (bridge_.get() && bridge_->browser() && bridge_->browser()->profile()) {
ThemeProvider* provider = bridge_->browser()->profile()->GetThemeProvider();
color =
provider->GetNSColor(BrowserThemeProvider::COLOR_BOOKMARK_TEXT, false);
}
if (!color)
color = [HyperlinkButtonCell defaultTextColor];
[showAllDownloadsCell_ setTextColor:color];
}
- (AnimatableView*)animatableView {
return static_cast<AnimatableView*>([self view]);
}
- (void)showDownloadsTab:(id)sender {
bridge_->browser()->ShowDownloadsTab();
}
- (void)remove:(DownloadItemController*)download {
// Look for the download in our controller array and remove it. This will
// explicity release it so that it removes itself as an Observer of the
// DownloadItem. We don't want to wait for autorelease since the DownloadItem
// we are observing will likely be gone by then.
[[NSNotificationCenter defaultCenter] removeObserver:download];
// TODO(dmaclach): Remove -- http://crbug.com/25845
[[download view] removeFromSuperview];
[downloadItemControllers_ removeObject:download];
[self layoutItems];
// Check to see if we have any downloads remaining and if not, hide the shelf.
if (![downloadItemControllers_ count])
[self showDownloadShelf:NO];
}
- (void)downloadWasOpened:(DownloadItemController*)item_controller {
// This should only be called on the main thead.
DCHECK([NSThread isMainThread]);
if ([self canAutoClose])
[self installTrackingArea];
}
// We need to explicitly release our download controllers here since they need
// to remove themselves as observers before the remaining shutdown happens.
- (void)exiting {
[[self animatableView] stopAnimation];
[self cancelAutoCloseAndRemoveTrackingArea];
downloadItemControllers_.reset();
}
// Show or hide the bar based on the value of |enable|. Handles animating the
// resize of the content view.
- (void)showDownloadShelf:(BOOL)enable {
if ([self isVisible] == enable)
return;
if ([[self view] window])
[self updateTheme];
// Animate the shelf out, but not in.
// TODO(rohitrao): We do not animate on the way in because Cocoa is already
// doing a lot of work to set up the download arrow animation. I've chosen to
// do no animation over janky animation. Find a way to make animating in
// smoother.
AnimatableView* view = [self animatableView];
if (enable)
[view setHeight:maxShelfHeight_];
else
[view animateToNewHeight:0 duration:kDownloadShelfCloseDuration];
barIsVisible_ = enable;
}
- (DownloadShelf*)bridge {
return bridge_.get();
}
- (BOOL)isVisible {
return barIsVisible_;
}
- (void)show:(id)sender {
[self showDownloadShelf:YES];
}
- (void)hide:(id)sender {
[self cancelAutoCloseAndRemoveTrackingArea];
// If |sender| isn't nil, then we're being closed from the UI by the user and
// we need to tell our shelf implementation to close. Otherwise, we're being
// closed programmatically by our shelf implementation.
if (sender)
bridge_->Close();
else
[self showDownloadShelf:NO];
}
- (void)animationDidEnd:(NSAnimation*)animation {
if (![self isVisible])
[self closed];
}
- (float)height {
return maxShelfHeight_;
}
// If |skipFirst| is true, the frame of the leftmost item is not set.
- (void)layoutItems:(BOOL)skipFirst {
CGFloat currentX = 0;
for (DownloadItemController* itemController
in downloadItemControllers_.get()) {
NSRect frame = [[itemController view] frame];
frame.origin.x = currentX;
frame.size.width = [itemController preferredSize].width;
if (!skipFirst)
[[[itemController view] animator] setFrame:frame];
currentX += frame.size.width + kDownloadItemPadding;
skipFirst = NO;
}
}
- (void)layoutItems {
[self layoutItems:NO];
}
- (void)addDownloadItem:(BaseDownloadItemModel*)model {
DCHECK([NSThread isMainThread]);
[self cancelAutoCloseAndRemoveTrackingArea];
// Insert new item at the left.
scoped_nsobject<DownloadItemController> controller(
[[DownloadItemController alloc] initWithModel:model shelf:self]);
// Adding at index 0 in NSMutableArrays is O(1).
[downloadItemControllers_ insertObject:controller.get() atIndex:0];
[itemContainerView_ addSubview:[controller view]];
// The controller is in charge of removing itself as an observer in its
// dealloc.
[[NSNotificationCenter defaultCenter]
addObserver:controller
selector:@selector(updateVisibility:)
name:NSViewFrameDidChangeNotification
object:[controller view]];
[[NSNotificationCenter defaultCenter]
addObserver:controller
selector:@selector(updateVisibility:)
name:NSViewFrameDidChangeNotification
object:itemContainerView_];
// Start at width 0...
NSSize size = [controller preferredSize];
NSRect frame = NSMakeRect(0, 0, 0, size.height);
[[controller view] setFrame:frame];
// ...then animate in
frame.size.width = size.width;
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext]
gtm_setDuration:kDownloadItemOpenDuration
eventMask:NSLeftMouseUpMask];
[[[controller view] animator] setFrame:frame];
[NSAnimationContext endGrouping];
// Keep only a limited number of items in the shelf.
if ([downloadItemControllers_ count] > kMaxDownloadItemCount) {
DCHECK(kMaxDownloadItemCount > 0);
// Since no user will ever see the item being removed (needs a horizontal
// screen resolution greater than 3200 at 16 items at 200 pixels each),
// there's no point in animating the removal.
[self remove:[downloadItemControllers_ lastObject]];
}
// Finally, move the remaining items to the right. Skip the first item when
// laying out the items, so that the longer animation duration we set up above
// is not overwritten.
[self layoutItems:YES];
}
- (void)closed {
NSUInteger i = 0;
while (i < [downloadItemControllers_ count]) {
DownloadItemController* itemController =
[downloadItemControllers_ objectAtIndex:i];
DownloadItem* download = [itemController download];
bool isTransferDone =
download->state() == DownloadItem::COMPLETE ||
download->state() == DownloadItem::CANCELLED;
if (isTransferDone &&
download->safety_state() != DownloadItem::DANGEROUS) {
[self remove:itemController];
} else {
// Treat the item as opened when we close. This way if we get shown again
// the user need not open this item for the shelf to auto-close.
download->set_opened(true);
++i;
}
}
}
- (void)mouseEntered:(NSEvent*)event {
// If the mouse re-enters the download shelf, cancel the auto-close. Further
// mouse exits should not trigger autoclose, so also remove the tracking area.
[self cancelAutoCloseAndRemoveTrackingArea];
}
- (void)mouseExited:(NSEvent*)event {
// Cancel any previous hide requests, just to be safe.
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(hide:)
object:self];
// Schedule an autoclose after a delay. If the mouse is moved back into the
// view, or if an item is added to the shelf, the timer will be canceled.
[self performSelector:@selector(hide:)
withObject:self
afterDelay:kAutoCloseDelaySeconds];
}
- (BOOL)canAutoClose {
for (NSUInteger i = 0; i < [downloadItemControllers_ count]; ++i) {
DownloadItemController* itemController =
[downloadItemControllers_ objectAtIndex:i];
if (![itemController download]->opened())
return NO;
}
return YES;
}
- (void)installTrackingArea {
// Install the tracking area to listen for mouseExited messages and trigger
// the shelf autoclose.
if (trackingArea_.get())
return;
trackingArea_.reset([[NSTrackingArea alloc]
initWithRect:[[self view] bounds]
options:NSTrackingMouseEnteredAndExited |
NSTrackingActiveAlways
owner:self
userInfo:nil]);
[[self view] addTrackingArea:trackingArea_];
}
- (void)cancelAutoCloseAndRemoveTrackingArea {
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(hide:)
object:self];
if (trackingArea_.get()) {
[[self view] removeTrackingArea:trackingArea_];
trackingArea_.reset(nil);
}
}
@end
|