| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "chrome/browser/ui/views/download/download_shelf_view.h" | 5 #include "chrome/browser/ui/views/download/download_shelf_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "ui/base/theme_provider.h" | 29 #include "ui/base/theme_provider.h" |
| 30 #include "ui/gfx/animation/slide_animation.h" | 30 #include "ui/gfx/animation/slide_animation.h" |
| 31 #include "ui/gfx/canvas.h" | 31 #include "ui/gfx/canvas.h" |
| 32 #include "ui/resources/grit/ui_resources.h" | 32 #include "ui/resources/grit/ui_resources.h" |
| 33 #include "ui/views/background.h" | 33 #include "ui/views/background.h" |
| 34 #include "ui/views/controls/button/image_button.h" | 34 #include "ui/views/controls/button/image_button.h" |
| 35 #include "ui/views/controls/image_view.h" | 35 #include "ui/views/controls/image_view.h" |
| 36 #include "ui/views/controls/link.h" | 36 #include "ui/views/controls/link.h" |
| 37 #include "ui/views/mouse_watcher_view_host.h" | 37 #include "ui/views/mouse_watcher_view_host.h" |
| 38 | 38 |
| 39 using content::DownloadItem; |
| 40 |
| 41 namespace { |
| 42 |
| 39 // Max number of download views we'll contain. Any time a view is added and | 43 // Max number of download views we'll contain. Any time a view is added and |
| 40 // we already have this many download views, one is removed. | 44 // we already have this many download views, one is removed. |
| 41 static const size_t kMaxDownloadViews = 15; | 45 const size_t kMaxDownloadViews = 15; |
| 42 | 46 |
| 43 // Padding from left edge and first download view. | 47 // Padding from left edge and first download view. |
| 44 static const int kLeftPadding = 2; | 48 const int kLeftPadding = 2; |
| 49 const int kStartPaddingMd = 4; |
| 45 | 50 |
| 46 // Padding from right edge and close button/show downloads link. | 51 // Padding from right edge and close button/show downloads link. |
| 47 static const int kRightPadding = 10; | 52 const int kRightPadding = 10; |
| 53 const int kEndPaddingMd = 6; |
| 48 | 54 |
| 49 // Padding between the show all link and close button. | 55 // Padding between the show all link and close button. |
| 50 static const int kCloseAndLinkPadding = 14; | 56 const int kCloseAndLinkPadding = 14; |
| 51 | 57 |
| 52 // Padding between the download views. | 58 // Padding between the download views. |
| 53 static const int kDownloadPadding = 10; | 59 const int kDownloadPadding = 10; |
| 54 | 60 |
| 55 // Padding between the top/bottom and the content. | 61 // Padding between the top/bottom and the content. |
| 56 static const int kTopBottomPadding = 2; | 62 const int kTopBottomPadding = 2; |
| 63 const int kTopBottomPaddingMd = 6; |
| 57 | 64 |
| 58 // Padding between the icon and 'show all downloads' link | 65 // Padding between the icon and 'show all downloads' link |
| 59 static const int kDownloadsTitlePadding = 4; | 66 const int kDownloadsTitlePadding = 4; |
| 60 | 67 |
| 61 // Border color. | 68 // Border color. |
| 62 static const SkColor kBorderColor = SkColorSetRGB(214, 214, 214); | 69 const SkColor kBorderColor = SkColorSetRGB(214, 214, 214); |
| 63 | 70 |
| 64 // New download item animation speed in milliseconds. | 71 // New download item animation speed in milliseconds. |
| 65 static const int kNewItemAnimationDurationMs = 800; | 72 const int kNewItemAnimationDurationMs = 800; |
| 66 | 73 |
| 67 // Shelf show/hide speed. | 74 // Shelf show/hide speed. |
| 68 static const int kShelfAnimationDurationMs = 120; | 75 const int kShelfAnimationDurationMs = 120; |
| 69 | 76 |
| 70 // Amount of time to delay if the mouse leaves the shelf by way of entering | 77 // Amount of time to delay if the mouse leaves the shelf by way of entering |
| 71 // another window. This is much larger than the normal delay as openning a | 78 // another window. This is much larger than the normal delay as openning a |
| 72 // download is most likely going to trigger a new window to appear over the | 79 // download is most likely going to trigger a new window to appear over the |
| 73 // button. Delay the time so that the user has a chance to quickly close the | 80 // button. Delay the time so that the user has a chance to quickly close the |
| 74 // other app and return to chrome with the download shelf still open. | 81 // other app and return to chrome with the download shelf still open. |
| 75 static const int kNotifyOnExitTimeMS = 5000; | 82 const int kNotifyOnExitTimeMS = 5000; |
| 76 | 83 |
| 77 using content::DownloadItem; | 84 int GetStartPadding() { |
| 85 return ui::MaterialDesignController::IsModeMaterial() ? kStartPaddingMd |
| 86 : kLeftPadding; |
| 87 } |
| 78 | 88 |
| 79 namespace { | 89 int GetEndPadding() { |
| 90 return ui::MaterialDesignController::IsModeMaterial() ? kEndPaddingMd |
| 91 : kRightPadding; |
| 92 } |
| 93 |
| 94 int GetBetweenItemPadding() { |
| 95 return ui::MaterialDesignController::IsModeMaterial() ? 0 : kDownloadPadding; |
| 96 } |
| 97 |
| 98 int GetTopBottomPadding() { |
| 99 return ui::MaterialDesignController::IsModeMaterial() ? kTopBottomPaddingMd |
| 100 : kTopBottomPadding; |
| 101 } |
| 80 | 102 |
| 81 // Sets size->width() to view's preferred width + size->width().s | 103 // Sets size->width() to view's preferred width + size->width().s |
| 82 // Sets size->height() to the max of the view's preferred height and | 104 // Sets size->height() to the max of the view's preferred height and |
| 83 // size->height(); | 105 // size->height(); |
| 84 void AdjustSize(views::View* view, gfx::Size* size) { | 106 void AdjustSize(views::View* view, gfx::Size* size) { |
| 85 gfx::Size view_preferred = view->GetPreferredSize(); | 107 gfx::Size view_preferred = view->GetPreferredSize(); |
| 86 size->Enlarge(view_preferred.width(), 0); | 108 size->Enlarge(view_preferred.width(), 0); |
| 87 size->set_height(std::max(view_preferred.height(), size->height())); | 109 size->set_height(std::max(view_preferred.height(), size->height())); |
| 88 } | 110 } |
| 89 | 111 |
| 90 int CenterPosition(int size, int target_size) { | 112 int CenterPosition(int size, int target_size) { |
| 91 return std::max((target_size - size) / 2, kTopBottomPadding); | 113 return std::max((target_size - size) / 2, GetTopBottomPadding()); |
| 92 } | 114 } |
| 93 | 115 |
| 94 } // namespace | 116 } // namespace |
| 95 | 117 |
| 96 DownloadShelfView::DownloadShelfView(Browser* browser, BrowserView* parent) | 118 DownloadShelfView::DownloadShelfView(Browser* browser, BrowserView* parent) |
| 97 : browser_(browser), | 119 : browser_(browser), |
| 98 arrow_image_(NULL), | 120 arrow_image_(NULL), |
| 99 show_all_view_(NULL), | 121 show_all_view_(NULL), |
| 100 close_button_(NULL), | 122 close_button_(NULL), |
| 101 parent_(parent), | 123 parent_(parent), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 void DownloadShelfView::OpenedDownload() { | 188 void DownloadShelfView::OpenedDownload() { |
| 167 if (CanAutoClose()) | 189 if (CanAutoClose()) |
| 168 mouse_watcher_.Start(); | 190 mouse_watcher_.Start(); |
| 169 } | 191 } |
| 170 | 192 |
| 171 content::PageNavigator* DownloadShelfView::GetNavigator() { | 193 content::PageNavigator* DownloadShelfView::GetNavigator() { |
| 172 return browser_; | 194 return browser_; |
| 173 } | 195 } |
| 174 | 196 |
| 175 gfx::Size DownloadShelfView::GetPreferredSize() const { | 197 gfx::Size DownloadShelfView::GetPreferredSize() const { |
| 176 gfx::Size prefsize(kRightPadding + kLeftPadding + kCloseAndLinkPadding, 0); | 198 gfx::Size prefsize(GetEndPadding() + GetStartPadding() + kCloseAndLinkPadding, |
| 199 0); |
| 177 AdjustSize(close_button_, &prefsize); | 200 AdjustSize(close_button_, &prefsize); |
| 178 AdjustSize(show_all_view_, &prefsize); | 201 AdjustSize(show_all_view_, &prefsize); |
| 179 // Add one download view to the preferred size. | 202 // Add one download view to the preferred size. |
| 180 if (!download_views_.empty()) { | 203 if (!download_views_.empty()) { |
| 181 AdjustSize(*download_views_.begin(), &prefsize); | 204 AdjustSize(*download_views_.begin(), &prefsize); |
| 182 prefsize.Enlarge(kDownloadPadding, 0); | 205 prefsize.Enlarge(GetBetweenItemPadding(), 0); |
| 183 } | 206 } |
| 184 prefsize.Enlarge(0, kTopBottomPadding + kTopBottomPadding); | 207 prefsize.Enlarge(0, 2 * GetTopBottomPadding()); |
| 185 if (shelf_animation_->is_animating()) { | 208 if (shelf_animation_->is_animating()) { |
| 186 prefsize.set_height(static_cast<int>( | 209 prefsize.set_height(static_cast<int>( |
| 187 static_cast<double>(prefsize.height()) * | 210 static_cast<double>(prefsize.height()) * |
| 188 shelf_animation_->GetCurrentValue())); | 211 shelf_animation_->GetCurrentValue())); |
| 189 } | 212 } |
| 190 return prefsize; | 213 return prefsize; |
| 191 } | 214 } |
| 192 | 215 |
| 193 void DownloadShelfView::AnimationProgressed(const gfx::Animation *animation) { | 216 void DownloadShelfView::AnimationProgressed(const gfx::Animation *animation) { |
| 194 if (animation == new_item_animation_.get()) { | 217 if (animation == new_item_animation_.get()) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 219 | 242 |
| 220 // If there is not enough room to show the first download item, show the | 243 // If there is not enough room to show the first download item, show the |
| 221 // "Show all downloads" link to the left to make it more visible that there is | 244 // "Show all downloads" link to the left to make it more visible that there is |
| 222 // something to see. | 245 // something to see. |
| 223 bool show_link_only = !CanFitFirstDownloadItem(); | 246 bool show_link_only = !CanFitFirstDownloadItem(); |
| 224 | 247 |
| 225 gfx::Size image_size = arrow_image_->GetPreferredSize(); | 248 gfx::Size image_size = arrow_image_->GetPreferredSize(); |
| 226 gfx::Size close_button_size = close_button_->GetPreferredSize(); | 249 gfx::Size close_button_size = close_button_->GetPreferredSize(); |
| 227 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); | 250 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); |
| 228 int max_download_x = | 251 int max_download_x = |
| 229 std::max<int>(0, width() - kRightPadding - close_button_size.width() - | 252 std::max<int>(0, width() - GetEndPadding() - close_button_size.width() - |
| 230 kCloseAndLinkPadding - show_all_size.width() - | 253 kCloseAndLinkPadding - show_all_size.width() - |
| 231 kDownloadsTitlePadding - image_size.width() - | 254 kDownloadsTitlePadding - image_size.width() - |
| 232 kDownloadPadding); | 255 GetBetweenItemPadding()); |
| 233 int next_x = show_link_only ? kLeftPadding : | 256 int next_x = show_link_only ? GetStartPadding() |
| 234 max_download_x + kDownloadPadding; | 257 : max_download_x + GetBetweenItemPadding(); |
| 235 // Align vertically with show_all_view_. | 258 // Align vertically with show_all_view_. |
| 236 arrow_image_->SetBounds(next_x, | 259 arrow_image_->SetBounds(next_x, |
| 237 CenterPosition(image_size.height(), height()), | 260 CenterPosition(image_size.height(), height()), |
| 238 image_size.width(), image_size.height()); | 261 image_size.width(), image_size.height()); |
| 239 next_x += image_size.width() + kDownloadsTitlePadding; | 262 next_x += image_size.width() + kDownloadsTitlePadding; |
| 240 show_all_view_->SetBounds(next_x, | 263 show_all_view_->SetBounds(next_x, |
| 241 CenterPosition(show_all_size.height(), height()), | 264 CenterPosition(show_all_size.height(), height()), |
| 242 show_all_size.width(), | 265 show_all_size.width(), |
| 243 show_all_size.height()); | 266 show_all_size.height()); |
| 244 next_x += show_all_size.width() + kCloseAndLinkPadding; | 267 next_x += show_all_size.width() + kCloseAndLinkPadding; |
| 245 // If the window is maximized, we want to expand the hitbox of the close | 268 // If the window is maximized, we want to expand the hitbox of the close |
| 246 // button to the right and bottom to make it easier to click. | 269 // button to the right and bottom to make it easier to click. |
| 247 bool is_maximized = browser_->window()->IsMaximized(); | 270 bool is_maximized = browser_->window()->IsMaximized(); |
| 248 int y = CenterPosition(close_button_size.height(), height()); | 271 int y = CenterPosition(close_button_size.height(), height()); |
| 249 close_button_->SetBounds(next_x, y, | 272 close_button_->SetBounds(next_x, y, |
| 250 is_maximized ? width() - next_x : close_button_size.width(), | 273 is_maximized ? width() - next_x : close_button_size.width(), |
| 251 is_maximized ? height() - y : close_button_size.height()); | 274 is_maximized ? height() - y : close_button_size.height()); |
| 252 if (show_link_only) { | 275 if (show_link_only) { |
| 253 // Let's hide all the items. | 276 // Let's hide all the items. |
| 254 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) | 277 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) |
| 255 (*ri)->SetVisible(false); | 278 (*ri)->SetVisible(false); |
| 256 return; | 279 return; |
| 257 } | 280 } |
| 258 | 281 |
| 259 next_x = kLeftPadding; | 282 next_x = GetStartPadding(); |
| 260 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) { | 283 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) { |
| 261 gfx::Size view_size = (*ri)->GetPreferredSize(); | 284 gfx::Size view_size = (*ri)->GetPreferredSize(); |
| 262 | 285 |
| 263 int x = next_x; | 286 int x = next_x; |
| 264 | 287 |
| 265 // Figure out width of item. | 288 // Figure out width of item. |
| 266 int item_width = view_size.width(); | 289 int item_width = view_size.width(); |
| 267 if (new_item_animation_->is_animating() && ri == download_views_.rbegin()) { | 290 if (new_item_animation_->is_animating() && ri == download_views_.rbegin()) { |
| 268 item_width = static_cast<int>(static_cast<double>(view_size.width()) * | 291 item_width = static_cast<int>(static_cast<double>(view_size.width()) * |
| 269 new_item_animation_->GetCurrentValue()); | 292 new_item_animation_->GetCurrentValue()); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 if (download_views_.empty()) | 345 if (download_views_.empty()) |
| 323 return true; | 346 return true; |
| 324 | 347 |
| 325 gfx::Size image_size = arrow_image_->GetPreferredSize(); | 348 gfx::Size image_size = arrow_image_->GetPreferredSize(); |
| 326 gfx::Size close_button_size = close_button_->GetPreferredSize(); | 349 gfx::Size close_button_size = close_button_->GetPreferredSize(); |
| 327 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); | 350 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); |
| 328 | 351 |
| 329 // Let's compute the width available for download items, which is the width | 352 // Let's compute the width available for download items, which is the width |
| 330 // of the shelf minus the "Show all downloads" link, arrow and close button | 353 // of the shelf minus the "Show all downloads" link, arrow and close button |
| 331 // and the padding. | 354 // and the padding. |
| 332 int available_width = width() - kRightPadding - close_button_size.width() - | 355 int available_width = width() - GetEndPadding() - close_button_size.width() - |
| 333 kCloseAndLinkPadding - show_all_size.width() - kDownloadsTitlePadding - | 356 kCloseAndLinkPadding - show_all_size.width() - |
| 334 image_size.width() - kDownloadPadding - kLeftPadding; | 357 kDownloadsTitlePadding - image_size.width() - |
| 358 GetBetweenItemPadding() - GetStartPadding(); |
| 335 if (available_width <= 0) | 359 if (available_width <= 0) |
| 336 return false; | 360 return false; |
| 337 | 361 |
| 338 gfx::Size item_size = (*download_views_.rbegin())->GetPreferredSize(); | 362 gfx::Size item_size = (*download_views_.rbegin())->GetPreferredSize(); |
| 339 return item_size.width() < available_width; | 363 return item_size.width() < available_width; |
| 340 } | 364 } |
| 341 | 365 |
| 342 void DownloadShelfView::UpdateColorsFromTheme() { | 366 void DownloadShelfView::UpdateColorsFromTheme() { |
| 343 if (show_all_view_ && close_button_ && GetThemeProvider()) { | 367 if (show_all_view_ && close_button_ && GetThemeProvider()) { |
| 344 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 368 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 return false; | 451 return false; |
| 428 } | 452 } |
| 429 return true; | 453 return true; |
| 430 } | 454 } |
| 431 | 455 |
| 432 content::DownloadItem* DownloadShelfView::GetDownloadItemForView(size_t i) { | 456 content::DownloadItem* DownloadShelfView::GetDownloadItemForView(size_t i) { |
| 433 if (ui::MaterialDesignController::IsModeMaterial()) | 457 if (ui::MaterialDesignController::IsModeMaterial()) |
| 434 return static_cast<DownloadItemViewMd*>(download_views_[i])->download(); | 458 return static_cast<DownloadItemViewMd*>(download_views_[i])->download(); |
| 435 return static_cast<DownloadItemView*>(download_views_[i])->download(); | 459 return static_cast<DownloadItemView*>(download_views_[i])->download(); |
| 436 } | 460 } |
| OLD | NEW |