OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/views/download_shelf_view.h" | 5 #include "chrome/browser/views/download_shelf_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 // Border color. | 50 // Border color. |
51 static const SkColor kBorderColor = SkColorSetRGB(214, 214, 214); | 51 static const SkColor kBorderColor = SkColorSetRGB(214, 214, 214); |
52 | 52 |
53 // New download item animation speed in milliseconds. | 53 // New download item animation speed in milliseconds. |
54 static const int kNewItemAnimationDurationMs = 800; | 54 static const int kNewItemAnimationDurationMs = 800; |
55 | 55 |
56 // Shelf show/hide speed. | 56 // Shelf show/hide speed. |
57 static const int kShelfAnimationDurationMs = 120; | 57 static const int kShelfAnimationDurationMs = 120; |
58 | 58 |
| 59 // Amount of time to delay if the mouse leaves the shelf by way of entering |
| 60 // another window. This is much larger than the normal delay as openning a |
| 61 // download is most likely going to trigger a new window to appear over the |
| 62 // button. Delay the time so that the user has a chance to quickly close the |
| 63 // other app and return to chrome with the download shelf still open. |
| 64 static const int kNotifyOnExitTimeMS = 5000; |
| 65 |
59 namespace { | 66 namespace { |
60 | 67 |
61 // Sets size->width() to view's preferred width + size->width().s | 68 // Sets size->width() to view's preferred width + size->width().s |
62 // Sets size->height() to the max of the view's preferred height and | 69 // Sets size->height() to the max of the view's preferred height and |
63 // size->height(); | 70 // size->height(); |
64 void AdjustSize(views::View* view, gfx::Size* size) { | 71 void AdjustSize(views::View* view, gfx::Size* size) { |
65 gfx::Size view_preferred = view->GetPreferredSize(); | 72 gfx::Size view_preferred = view->GetPreferredSize(); |
66 size->Enlarge(view_preferred.width(), 0); | 73 size->Enlarge(view_preferred.width(), 0); |
67 size->set_height(std::max(view_preferred.height(), size->height())); | 74 size->set_height(std::max(view_preferred.height(), size->height())); |
68 } | 75 } |
69 | 76 |
70 int CenterPosition(int size, int target_size) { | 77 int CenterPosition(int size, int target_size) { |
71 return std::max((target_size - size) / 2, kTopBottomPadding); | 78 return std::max((target_size - size) / 2, kTopBottomPadding); |
72 } | 79 } |
73 | 80 |
74 } // namespace | 81 } // namespace |
75 | 82 |
76 DownloadShelfView::DownloadShelfView(Browser* browser, BrowserView* parent) | 83 DownloadShelfView::DownloadShelfView(Browser* browser, BrowserView* parent) |
77 : browser_(browser), | 84 : browser_(browser), |
78 parent_(parent) { | 85 parent_(parent), |
| 86 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 87 mouse_watcher_(this, this, gfx::Insets())) { |
| 88 mouse_watcher_.set_notify_on_exit_time_ms(kNotifyOnExitTimeMS); |
79 SetID(VIEW_ID_DOWNLOAD_SHELF); | 89 SetID(VIEW_ID_DOWNLOAD_SHELF); |
80 parent->AddChildView(this); | 90 parent->AddChildView(this); |
81 Init(); | 91 Init(); |
82 } | 92 } |
83 | 93 |
84 DownloadShelfView::~DownloadShelfView() { | 94 DownloadShelfView::~DownloadShelfView() { |
85 parent_->RemoveChildView(this); | 95 parent_->RemoveChildView(this); |
86 } | 96 } |
87 | 97 |
88 void DownloadShelfView::Init() { | 98 void DownloadShelfView::Init() { |
(...skipping 19 matching lines...) Expand all Loading... |
108 | 118 |
109 new_item_animation_.reset(new SlideAnimation(this)); | 119 new_item_animation_.reset(new SlideAnimation(this)); |
110 new_item_animation_->SetSlideDuration(kNewItemAnimationDurationMs); | 120 new_item_animation_->SetSlideDuration(kNewItemAnimationDurationMs); |
111 | 121 |
112 shelf_animation_.reset(new SlideAnimation(this)); | 122 shelf_animation_.reset(new SlideAnimation(this)); |
113 shelf_animation_->SetSlideDuration(kShelfAnimationDurationMs); | 123 shelf_animation_->SetSlideDuration(kShelfAnimationDurationMs); |
114 Show(); | 124 Show(); |
115 } | 125 } |
116 | 126 |
117 void DownloadShelfView::AddDownloadView(DownloadItemView* view) { | 127 void DownloadShelfView::AddDownloadView(DownloadItemView* view) { |
| 128 mouse_watcher_.Stop(); |
| 129 |
118 Show(); | 130 Show(); |
119 | 131 |
120 DCHECK(view); | 132 DCHECK(view); |
121 download_views_.push_back(view); | 133 download_views_.push_back(view); |
122 AddChildView(view); | 134 AddChildView(view); |
123 if (download_views_.size() > kMaxDownloadViews) | 135 if (download_views_.size() > kMaxDownloadViews) |
124 RemoveDownloadView(*download_views_.begin()); | 136 RemoveDownloadView(*download_views_.begin()); |
125 | 137 |
126 new_item_animation_->Reset(); | 138 new_item_animation_->Reset(); |
127 new_item_animation_->Show(); | 139 new_item_animation_->Show(); |
128 } | 140 } |
129 | 141 |
130 void DownloadShelfView::AddDownload(BaseDownloadItemModel* download_model) { | 142 void DownloadShelfView::AddDownload(BaseDownloadItemModel* download_model) { |
131 DownloadItemView* view = new DownloadItemView( | 143 DownloadItemView* view = new DownloadItemView( |
132 download_model->download(), this, download_model); | 144 download_model->download(), this, download_model); |
133 AddDownloadView(view); | 145 AddDownloadView(view); |
134 } | 146 } |
135 | 147 |
| 148 void DownloadShelfView::MouseMovedOutOfView() { |
| 149 Close(); |
| 150 } |
| 151 |
136 void DownloadShelfView::RemoveDownloadView(View* view) { | 152 void DownloadShelfView::RemoveDownloadView(View* view) { |
137 DCHECK(view); | 153 DCHECK(view); |
138 std::vector<DownloadItemView*>::iterator i = | 154 std::vector<DownloadItemView*>::iterator i = |
139 find(download_views_.begin(), download_views_.end(), view); | 155 find(download_views_.begin(), download_views_.end(), view); |
140 DCHECK(i != download_views_.end()); | 156 DCHECK(i != download_views_.end()); |
141 download_views_.erase(i); | 157 download_views_.erase(i); |
142 RemoveChildView(view); | 158 RemoveChildView(view); |
143 delete view; | 159 delete view; |
144 if (download_views_.empty()) | 160 if (download_views_.empty()) |
145 Close(); | 161 Close(); |
| 162 else if (CanAutoClose()) |
| 163 mouse_watcher_.Start(); |
146 Layout(); | 164 Layout(); |
147 SchedulePaint(); | 165 SchedulePaint(); |
148 } | 166 } |
149 | 167 |
150 void DownloadShelfView::Paint(gfx::Canvas* canvas) { | 168 void DownloadShelfView::Paint(gfx::Canvas* canvas) { |
151 PaintBackground(canvas); | 169 PaintBackground(canvas); |
152 PaintBorder(canvas); | 170 PaintBorder(canvas); |
153 } | 171 } |
154 | 172 |
155 void DownloadShelfView::PaintBorder(gfx::Canvas* canvas) { | 173 void DownloadShelfView::PaintBorder(gfx::Canvas* canvas) { |
156 canvas->FillRectInt(kBorderColor, 0, 0, width(), 1); | 174 canvas->FillRectInt(kBorderColor, 0, 0, width(), 1); |
157 } | 175 } |
158 | 176 |
| 177 void DownloadShelfView::OpenedDownload(DownloadItemView* view) { |
| 178 if (CanAutoClose()) |
| 179 mouse_watcher_.Start(); |
| 180 } |
| 181 |
159 gfx::Size DownloadShelfView::GetPreferredSize() { | 182 gfx::Size DownloadShelfView::GetPreferredSize() { |
160 gfx::Size prefsize(kRightPadding + kLeftPadding + kCloseAndLinkPadding, 0); | 183 gfx::Size prefsize(kRightPadding + kLeftPadding + kCloseAndLinkPadding, 0); |
161 AdjustSize(close_button_, &prefsize); | 184 AdjustSize(close_button_, &prefsize); |
162 AdjustSize(show_all_view_, &prefsize); | 185 AdjustSize(show_all_view_, &prefsize); |
163 // Add one download view to the preferred size. | 186 // Add one download view to the preferred size. |
164 if (download_views_.size() > 0) { | 187 if (download_views_.size() > 0) { |
165 AdjustSize(*download_views_.begin(), &prefsize); | 188 AdjustSize(*download_views_.begin(), &prefsize); |
166 prefsize.Enlarge(kDownloadPadding, 0); | 189 prefsize.Enlarge(kDownloadPadding, 0); |
167 } | 190 } |
168 prefsize.Enlarge(0, kTopBottomPadding + kTopBottomPadding); | 191 prefsize.Enlarge(0, kTopBottomPadding + kTopBottomPadding); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 352 |
330 void DownloadShelfView::Close() { | 353 void DownloadShelfView::Close() { |
331 parent_->SetDownloadShelfVisible(false); | 354 parent_->SetDownloadShelfVisible(false); |
332 shelf_animation_->Hide(); | 355 shelf_animation_->Hide(); |
333 } | 356 } |
334 | 357 |
335 void DownloadShelfView::Closed() { | 358 void DownloadShelfView::Closed() { |
336 // When the close animation is complete, remove all completed downloads. | 359 // When the close animation is complete, remove all completed downloads. |
337 size_t i = 0; | 360 size_t i = 0; |
338 while (i < download_views_.size()) { | 361 while (i < download_views_.size()) { |
339 DownloadItem* download = download_views_[i]->get_download(); | 362 DownloadItem* download = download_views_[i]->download(); |
340 bool is_transfer_done = download->state() == DownloadItem::COMPLETE || | 363 bool is_transfer_done = download->state() == DownloadItem::COMPLETE || |
341 download->state() == DownloadItem::CANCELLED; | 364 download->state() == DownloadItem::CANCELLED; |
342 if (is_transfer_done && | 365 if (is_transfer_done && |
343 download->safety_state() != DownloadItem::DANGEROUS) { | 366 download->safety_state() != DownloadItem::DANGEROUS) { |
344 RemoveDownloadView(download_views_[i]); | 367 RemoveDownloadView(download_views_[i]); |
345 } else { | 368 } else { |
| 369 // Treat the item as opened when we close. This way if we get shown again |
| 370 // the user need not open this item for the shelf to auto-close. |
| 371 download->set_opened(true); |
346 ++i; | 372 ++i; |
347 } | 373 } |
348 } | 374 } |
349 } | 375 } |
| 376 |
| 377 bool DownloadShelfView::CanAutoClose() { |
| 378 for (size_t i = 0; i < download_views_.size(); ++i) { |
| 379 if (!download_views_[i]->download()->opened()) |
| 380 return false; |
| 381 } |
| 382 return true; |
| 383 } |
OLD | NEW |