Chromium Code Reviews| 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 11 matching lines...) Expand all Loading... | |
| 22 #include "content/public/browser/download_item.h" | 22 #include "content/public/browser/download_item.h" |
| 23 #include "content/public/browser/download_manager.h" | 23 #include "content/public/browser/download_manager.h" |
| 24 #include "content/public/browser/page_navigator.h" | 24 #include "content/public/browser/page_navigator.h" |
| 25 #include "grit/theme_resources.h" | 25 #include "grit/theme_resources.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 27 #include "ui/base/resource/material_design/material_design_controller.h" | 27 #include "ui/base/resource/material_design/material_design_controller.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
| 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/gfx/color_palette.h" | |
| 33 #include "ui/gfx/paint_vector_icon.h" | |
| 34 #include "ui/gfx/vector_icons_public.h" | |
| 32 #include "ui/resources/grit/ui_resources.h" | 35 #include "ui/resources/grit/ui_resources.h" |
| 33 #include "ui/views/background.h" | 36 #include "ui/views/background.h" |
| 37 #include "ui/views/border.h" | |
| 34 #include "ui/views/controls/button/image_button.h" | 38 #include "ui/views/controls/button/image_button.h" |
| 35 #include "ui/views/controls/image_view.h" | 39 #include "ui/views/controls/image_view.h" |
| 36 #include "ui/views/controls/link.h" | 40 #include "ui/views/controls/link.h" |
| 37 #include "ui/views/mouse_watcher_view_host.h" | 41 #include "ui/views/mouse_watcher_view_host.h" |
| 38 | 42 |
| 39 using content::DownloadItem; | 43 using content::DownloadItem; |
| 40 | 44 |
| 41 namespace { | 45 namespace { |
| 42 | 46 |
| 43 // Max number of download views we'll contain. Any time a view is added and | 47 // Max number of download views we'll contain. Any time a view is added and |
| 44 // we already have this many download views, one is removed. | 48 // we already have this many download views, one is removed. |
| 45 const size_t kMaxDownloadViews = 15; | 49 const size_t kMaxDownloadViews = 15; |
| 46 | 50 |
| 47 // Padding from left edge and first download view. | 51 // Padding from left edge and first download view. |
| 48 const int kLeftPadding = 2; | 52 const int kLeftPadding = 2; |
| 49 const int kStartPaddingMd = 4; | 53 const int kStartPaddingMd = 4; |
| 50 | 54 |
| 51 // Padding from right edge and close button/show downloads link. | 55 // Padding from right edge and close button/show downloads link. |
| 52 const int kRightPadding = 10; | 56 const int kRightPadding = 10; |
| 53 const int kEndPaddingMd = 6; | 57 const int kEndPaddingMd = 6; |
| 54 | 58 |
| 55 // Padding between the show all link and close button. | 59 // Padding between the show all link and close button. |
| 56 const int kCloseAndLinkPadding = 14; | 60 const int kCloseAndLinkPadding = 14; |
| 61 const int kCloseAndLinkPaddingMd = 6; | |
| 57 | 62 |
| 58 // Padding between the download views. | 63 // Padding between the download views. |
| 59 const int kDownloadPadding = 10; | 64 const int kDownloadPadding = 10; |
| 60 | 65 |
| 61 // Padding between the top/bottom and the content. | 66 // Padding between the top/bottom and the content. |
| 62 const int kTopBottomPadding = 2; | 67 const int kTopBottomPadding = 2; |
| 63 const int kTopBottomPaddingMd = 6; | |
| 64 | 68 |
| 65 // Padding between the icon and 'show all downloads' link | 69 // Padding between the icon and 'show all downloads' link |
| 66 const int kDownloadsTitlePadding = 4; | 70 const int kDownloadsTitlePadding = 4; |
| 67 | 71 |
| 68 // Border color. | 72 // Border color. |
| 69 const SkColor kBorderColor = SkColorSetRGB(214, 214, 214); | 73 const SkColor kBorderColor = SkColorSetRGB(214, 214, 214); |
| 70 | 74 |
| 71 // New download item animation speed in milliseconds. | 75 // New download item animation speed in milliseconds. |
| 72 const int kNewItemAnimationDurationMs = 800; | 76 const int kNewItemAnimationDurationMs = 800; |
| 73 | 77 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 84 int GetStartPadding() { | 88 int GetStartPadding() { |
| 85 return ui::MaterialDesignController::IsModeMaterial() ? kStartPaddingMd | 89 return ui::MaterialDesignController::IsModeMaterial() ? kStartPaddingMd |
| 86 : kLeftPadding; | 90 : kLeftPadding; |
| 87 } | 91 } |
| 88 | 92 |
| 89 int GetEndPadding() { | 93 int GetEndPadding() { |
| 90 return ui::MaterialDesignController::IsModeMaterial() ? kEndPaddingMd | 94 return ui::MaterialDesignController::IsModeMaterial() ? kEndPaddingMd |
| 91 : kRightPadding; | 95 : kRightPadding; |
| 92 } | 96 } |
| 93 | 97 |
| 98 int GetCloseAndLinkPadding() { | |
| 99 return ui::MaterialDesignController::IsModeMaterial() ? kCloseAndLinkPaddingMd | |
| 100 : kCloseAndLinkPadding; | |
| 101 } | |
| 102 | |
| 94 int GetBetweenItemPadding() { | 103 int GetBetweenItemPadding() { |
| 95 return ui::MaterialDesignController::IsModeMaterial() ? 0 : kDownloadPadding; | 104 return ui::MaterialDesignController::IsModeMaterial() ? 0 : kDownloadPadding; |
| 96 } | 105 } |
| 97 | 106 |
| 98 int GetTopBottomPadding() { | 107 int GetTopBottomPadding() { |
| 99 return ui::MaterialDesignController::IsModeMaterial() ? kTopBottomPaddingMd | 108 return ui::MaterialDesignController::IsModeMaterial() ? 0 : kTopBottomPadding; |
| 100 : kTopBottomPadding; | |
| 101 } | 109 } |
| 102 | 110 |
| 103 // Sets size->width() to view's preferred width + size->width().s | 111 // Sets size->width() to view's preferred width + size->width().s |
| 104 // Sets size->height() to the max of the view's preferred height and | 112 // Sets size->height() to the max of the view's preferred height and |
| 105 // size->height(); | 113 // size->height(); |
| 106 void AdjustSize(views::View* view, gfx::Size* size) { | 114 void AdjustSize(views::View* view, gfx::Size* size) { |
| 107 gfx::Size view_preferred = view->GetPreferredSize(); | 115 gfx::Size view_preferred = view->GetPreferredSize(); |
| 108 size->Enlarge(view_preferred.width(), 0); | 116 size->Enlarge(view_preferred.width(), 0); |
| 109 size->set_height(std::max(view_preferred.height(), size->height())); | 117 size->set_height(std::max(view_preferred.height(), size->height())); |
| 110 } | 118 } |
| 111 | 119 |
| 112 int CenterPosition(int size, int target_size) { | 120 int CenterPosition(int size, int target_size) { |
| 113 return std::max((target_size - size) / 2, GetTopBottomPadding()); | 121 return std::max((target_size - size) / 2, GetTopBottomPadding()); |
| 114 } | 122 } |
| 115 | 123 |
| 116 } // namespace | 124 } // namespace |
| 117 | 125 |
| 118 DownloadShelfView::DownloadShelfView(Browser* browser, BrowserView* parent) | 126 DownloadShelfView::DownloadShelfView(Browser* browser, BrowserView* parent) |
| 119 : browser_(browser), | 127 : browser_(browser), |
| 120 arrow_image_(NULL), | 128 new_item_animation_(this), |
| 121 show_all_view_(NULL), | 129 shelf_animation_(this), |
| 122 close_button_(NULL), | 130 arrow_image_(nullptr), |
| 131 show_all_view_(nullptr), | |
| 132 close_button_(nullptr), | |
| 123 parent_(parent), | 133 parent_(parent), |
| 124 mouse_watcher_(new views::MouseWatcherViewHost(this, gfx::Insets()), | 134 mouse_watcher_(new views::MouseWatcherViewHost(this, gfx::Insets()), |
| 125 this) { | 135 this) { |
| 126 mouse_watcher_.set_notify_on_exit_time( | 136 mouse_watcher_.set_notify_on_exit_time( |
| 127 base::TimeDelta::FromMilliseconds(kNotifyOnExitTimeMS)); | 137 base::TimeDelta::FromMilliseconds(kNotifyOnExitTimeMS)); |
| 128 set_id(VIEW_ID_DOWNLOAD_SHELF); | 138 set_id(VIEW_ID_DOWNLOAD_SHELF); |
| 129 parent->AddChildView(this); | 139 parent->AddChildView(this); |
| 140 | |
| 141 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 142 | |
| 143 arrow_image_ = new views::ImageView(); | |
|
Evan Stade
2015/09/25 00:48:08
I moved this code because of pkasting's comment he
| |
| 144 AddChildView(arrow_image_); | |
| 145 close_button_ = new views::ImageButton(this); | |
| 146 close_button_->SetAccessibleName( | |
| 147 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); | |
| 148 if (!ui::MaterialDesignController::IsModeMaterial()) { | |
| 149 arrow_image_->SetImage(rb.GetImageSkiaNamed(IDR_DOWNLOADS_FAVICON)); | |
| 150 | |
| 151 views::Link* show_all_view = | |
| 152 new views::Link(l10n_util::GetStringUTF16(IDS_SHOW_ALL_DOWNLOADS)); | |
| 153 show_all_view->set_listener(this); | |
| 154 show_all_view_ = show_all_view; | |
| 155 | |
| 156 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
| 157 rb.GetImageSkiaNamed(IDR_CLOSE_1)); | |
| 158 close_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
| 159 rb.GetImageSkiaNamed(IDR_CLOSE_1_H)); | |
| 160 close_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
| 161 rb.GetImageSkiaNamed(IDR_CLOSE_1_P)); | |
| 162 } else { | |
| 163 views::LabelButton* show_all_view = new views::LabelButton( | |
| 164 this, l10n_util::GetStringUTF16(IDS_SHOW_ALL_DOWNLOADS_MD)); | |
| 165 show_all_view->SetFocusable(true); | |
| 166 show_all_view->SetStyle(views::Button::STYLE_BUTTON); | |
| 167 show_all_view_ = show_all_view; | |
| 168 | |
| 169 // TODO(estade): share this button init code with the find in page bar; | |
| 170 // also fix theming. | |
| 171 close_button_->SetBorder(views::Border::CreateEmptyBorder(4, 4, 4, 4)); | |
| 172 close_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | |
| 173 views::ImageButton::ALIGN_MIDDLE); | |
| 174 gfx::ImageSkia image = gfx::CreateVectorIcon(gfx::VectorIconId::BAR_CLOSE, | |
| 175 16, gfx::kChromeIconGrey); | |
| 176 close_button_->SetImage(views::CustomButton::STATE_NORMAL, &image); | |
| 177 } | |
| 178 AddChildView(show_all_view_); | |
| 179 AddChildView(close_button_); | |
| 180 | |
| 181 new_item_animation_.SetSlideDuration(kNewItemAnimationDurationMs); | |
| 182 shelf_animation_.SetSlideDuration(kShelfAnimationDurationMs); | |
| 130 } | 183 } |
| 131 | 184 |
| 132 DownloadShelfView::~DownloadShelfView() { | 185 DownloadShelfView::~DownloadShelfView() { |
| 133 parent_->RemoveChildView(this); | 186 parent_->RemoveChildView(this); |
| 134 } | 187 } |
| 135 | 188 |
| 136 void DownloadShelfView::AddDownloadView(views::View* view) { | 189 void DownloadShelfView::AddDownloadView(views::View* view) { |
| 137 mouse_watcher_.Stop(); | 190 mouse_watcher_.Stop(); |
| 138 | 191 |
| 139 DCHECK(view); | 192 DCHECK(view); |
| 140 download_views_.push_back(view); | 193 download_views_.push_back(view); |
| 141 | 194 |
| 142 // Insert the new view as the first child, so the logical child order matches | 195 // Insert the new view as the first child, so the logical child order matches |
| 143 // the visual order. This ensures that tabbing through downloads happens in | 196 // the visual order. This ensures that tabbing through downloads happens in |
| 144 // the order users would expect. | 197 // the order users would expect. |
| 145 AddChildViewAt(view, 0); | 198 AddChildViewAt(view, 0); |
| 146 if (download_views_.size() > kMaxDownloadViews) | 199 if (download_views_.size() > kMaxDownloadViews) |
| 147 RemoveDownloadView(*download_views_.begin()); | 200 RemoveDownloadView(*download_views_.begin()); |
| 148 | 201 |
| 149 new_item_animation_->Reset(); | 202 new_item_animation_.Reset(); |
| 150 new_item_animation_->Show(); | 203 new_item_animation_.Show(); |
| 151 } | 204 } |
| 152 | 205 |
| 153 void DownloadShelfView::DoAddDownload(DownloadItem* download) { | 206 void DownloadShelfView::DoAddDownload(DownloadItem* download) { |
| 154 if (ui::MaterialDesignController::IsModeMaterial()) | 207 if (ui::MaterialDesignController::IsModeMaterial()) |
| 155 AddDownloadView(new DownloadItemViewMd(download, this)); | 208 AddDownloadView(new DownloadItemViewMd(download, this)); |
| 156 else | 209 else |
| 157 AddDownloadView(new DownloadItemView(download, this)); | 210 AddDownloadView(new DownloadItemView(download, this)); |
| 158 } | 211 } |
| 159 | 212 |
| 160 void DownloadShelfView::MouseMovedOutOfHost() { | 213 void DownloadShelfView::MouseMovedOutOfHost() { |
| 161 Close(AUTOMATIC); | 214 Close(AUTOMATIC); |
| 162 } | 215 } |
| 163 | 216 |
| 164 void DownloadShelfView::RemoveDownloadView(View* view) { | 217 void DownloadShelfView::RemoveDownloadView(View* view) { |
| 165 DCHECK(view); | 218 DCHECK(view); |
| 166 auto i = find(download_views_.begin(), download_views_.end(), view); | 219 auto i = find(download_views_.begin(), download_views_.end(), view); |
| 167 DCHECK(i != download_views_.end()); | 220 DCHECK(i != download_views_.end()); |
| 168 download_views_.erase(i); | 221 download_views_.erase(i); |
| 169 RemoveChildView(view); | 222 RemoveChildView(view); |
| 170 delete view; | 223 delete view; |
| 171 if (download_views_.empty()) | 224 if (download_views_.empty()) |
| 172 Close(AUTOMATIC); | 225 Close(AUTOMATIC); |
| 173 else if (CanAutoClose()) | 226 else if (CanAutoClose()) |
| 174 mouse_watcher_.Start(); | 227 mouse_watcher_.Start(); |
| 175 Layout(); | 228 Layout(); |
| 176 SchedulePaint(); | 229 SchedulePaint(); |
| 177 } | 230 } |
| 178 | 231 |
| 179 views::View* DownloadShelfView::GetDefaultFocusableChild() { | 232 views::View* DownloadShelfView::GetDefaultFocusableChild() { |
| 180 return download_views_.empty() ? | 233 return download_views_.empty() ? show_all_view_ : download_views_.back(); |
| 181 static_cast<View*>(show_all_view_) : download_views_.back(); | |
| 182 } | 234 } |
| 183 | 235 |
| 184 void DownloadShelfView::OnPaintBorder(gfx::Canvas* canvas) { | 236 void DownloadShelfView::OnPaintBorder(gfx::Canvas* canvas) { |
| 185 canvas->FillRect(gfx::Rect(0, 0, width(), 1), kBorderColor); | 237 canvas->FillRect(gfx::Rect(0, 0, width(), 1), kBorderColor); |
| 186 } | 238 } |
| 187 | 239 |
| 188 void DownloadShelfView::OpenedDownload() { | 240 void DownloadShelfView::OpenedDownload() { |
| 189 if (CanAutoClose()) | 241 if (CanAutoClose()) |
| 190 mouse_watcher_.Start(); | 242 mouse_watcher_.Start(); |
| 191 } | 243 } |
| 192 | 244 |
| 193 content::PageNavigator* DownloadShelfView::GetNavigator() { | 245 content::PageNavigator* DownloadShelfView::GetNavigator() { |
| 194 return browser_; | 246 return browser_; |
| 195 } | 247 } |
| 196 | 248 |
| 197 gfx::Size DownloadShelfView::GetPreferredSize() const { | 249 gfx::Size DownloadShelfView::GetPreferredSize() const { |
| 198 gfx::Size prefsize(GetEndPadding() + GetStartPadding() + kCloseAndLinkPadding, | 250 gfx::Size prefsize( |
| 199 0); | 251 GetEndPadding() + GetStartPadding() + GetCloseAndLinkPadding(), 0); |
| 200 AdjustSize(close_button_, &prefsize); | 252 AdjustSize(close_button_, &prefsize); |
| 201 AdjustSize(show_all_view_, &prefsize); | 253 AdjustSize(show_all_view_, &prefsize); |
| 202 // Add one download view to the preferred size. | 254 // Add one download view to the preferred size. |
| 203 if (!download_views_.empty()) { | 255 if (!download_views_.empty()) { |
| 204 AdjustSize(*download_views_.begin(), &prefsize); | 256 AdjustSize(*download_views_.begin(), &prefsize); |
| 205 prefsize.Enlarge(GetBetweenItemPadding(), 0); | 257 prefsize.Enlarge(GetBetweenItemPadding(), 0); |
| 206 } | 258 } |
| 207 prefsize.Enlarge(0, 2 * GetTopBottomPadding()); | 259 prefsize.Enlarge(0, 2 * GetTopBottomPadding()); |
| 208 if (shelf_animation_->is_animating()) { | 260 if (shelf_animation_.is_animating()) { |
| 209 prefsize.set_height(static_cast<int>( | 261 prefsize.set_height( |
| 210 static_cast<double>(prefsize.height()) * | 262 static_cast<int>(static_cast<double>(prefsize.height()) * |
| 211 shelf_animation_->GetCurrentValue())); | 263 shelf_animation_.GetCurrentValue())); |
| 212 } | 264 } |
| 213 return prefsize; | 265 return prefsize; |
| 214 } | 266 } |
| 215 | 267 |
| 216 void DownloadShelfView::AnimationProgressed(const gfx::Animation *animation) { | 268 void DownloadShelfView::AnimationProgressed(const gfx::Animation* animation) { |
| 217 if (animation == new_item_animation_.get()) { | 269 if (animation == &new_item_animation_) { |
| 218 Layout(); | 270 Layout(); |
| 219 SchedulePaint(); | 271 SchedulePaint(); |
| 220 } else if (animation == shelf_animation_.get()) { | 272 } else if (animation == &shelf_animation_) { |
| 221 // Force a re-layout of the parent, which will call back into | 273 // Force a re-layout of the parent, which will call back into |
| 222 // GetPreferredSize, where we will do our animation. In the case where the | 274 // GetPreferredSize, where we will do our animation. In the case where the |
| 223 // animation is hiding, we do a full resize - the fast resizing would | 275 // animation is hiding, we do a full resize - the fast resizing would |
| 224 // otherwise leave blank white areas where the shelf was and where the | 276 // otherwise leave blank white areas where the shelf was and where the |
| 225 // user's eye is. Thankfully bottom-resizing is a lot faster than | 277 // user's eye is. Thankfully bottom-resizing is a lot faster than |
| 226 // top-resizing. | 278 // top-resizing. |
| 227 parent_->ToolbarSizeChanged(shelf_animation_->IsShowing()); | 279 parent_->ToolbarSizeChanged(shelf_animation_.IsShowing()); |
| 228 } | 280 } |
| 229 } | 281 } |
| 230 | 282 |
| 231 void DownloadShelfView::AnimationEnded(const gfx::Animation *animation) { | 283 void DownloadShelfView::AnimationEnded(const gfx::Animation *animation) { |
| 232 if (animation == shelf_animation_.get()) { | 284 if (animation == &shelf_animation_) { |
| 233 parent_->SetDownloadShelfVisible(shelf_animation_->IsShowing()); | 285 parent_->SetDownloadShelfVisible(shelf_animation_.IsShowing()); |
| 234 if (!shelf_animation_->IsShowing()) | 286 if (!shelf_animation_.IsShowing()) |
| 235 Closed(); | 287 Closed(); |
| 236 } | 288 } |
| 237 } | 289 } |
| 238 | 290 |
| 239 void DownloadShelfView::Layout() { | 291 void DownloadShelfView::Layout() { |
| 240 // Let our base class layout our child views | 292 // Let our base class layout our child views |
| 241 views::View::Layout(); | 293 views::View::Layout(); |
| 242 | 294 |
| 243 // If there is not enough room to show the first download item, show the | 295 // If there is not enough room to show the first download item, show the |
| 244 // "Show all downloads" link to the left to make it more visible that there is | 296 // "Show all downloads" link to the left to make it more visible that there is |
| 245 // something to see. | 297 // something to see. |
| 246 bool show_link_only = !CanFitFirstDownloadItem(); | 298 bool show_link_only = !CanFitFirstDownloadItem(); |
| 247 | 299 |
| 248 gfx::Size image_size = arrow_image_->GetPreferredSize(); | 300 gfx::Size image_size = arrow_image_->GetPreferredSize(); |
| 249 gfx::Size close_button_size = close_button_->GetPreferredSize(); | 301 gfx::Size close_button_size = close_button_->GetPreferredSize(); |
| 250 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); | 302 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); |
| 251 int max_download_x = | 303 int max_download_x = |
| 252 std::max<int>(0, width() - GetEndPadding() - close_button_size.width() - | 304 std::max<int>(0, width() - GetEndPadding() - close_button_size.width() - |
| 253 kCloseAndLinkPadding - show_all_size.width() - | 305 GetCloseAndLinkPadding() - show_all_size.width() - |
| 254 kDownloadsTitlePadding - image_size.width() - | 306 kDownloadsTitlePadding - image_size.width() - |
| 255 GetBetweenItemPadding()); | 307 GetBetweenItemPadding()); |
| 256 int next_x = show_link_only ? GetStartPadding() | 308 int next_x = show_link_only ? GetStartPadding() |
| 257 : max_download_x + GetBetweenItemPadding(); | 309 : max_download_x + GetBetweenItemPadding(); |
| 258 // Align vertically with show_all_view_. | 310 // Align vertically with show_all_view_. |
| 259 arrow_image_->SetBounds(next_x, | 311 arrow_image_->SetBounds(next_x, |
| 260 CenterPosition(image_size.height(), height()), | 312 CenterPosition(image_size.height(), height()), |
| 261 image_size.width(), image_size.height()); | 313 image_size.width(), image_size.height()); |
| 262 next_x += image_size.width() + kDownloadsTitlePadding; | 314 next_x += image_size.width() + kDownloadsTitlePadding; |
| 263 show_all_view_->SetBounds(next_x, | 315 show_all_view_->SetBounds(next_x, |
| 264 CenterPosition(show_all_size.height(), height()), | 316 CenterPosition(show_all_size.height(), height()), |
| 265 show_all_size.width(), | 317 show_all_size.width(), |
| 266 show_all_size.height()); | 318 show_all_size.height()); |
| 267 next_x += show_all_size.width() + kCloseAndLinkPadding; | 319 next_x += show_all_size.width() + GetCloseAndLinkPadding(); |
| 268 // If the window is maximized, we want to expand the hitbox of the close | 320 // If the window is maximized, we want to expand the hitbox of the close |
| 269 // button to the right and bottom to make it easier to click. | 321 // button to the right and bottom to make it easier to click. |
| 270 bool is_maximized = browser_->window()->IsMaximized(); | 322 bool is_maximized = browser_->window()->IsMaximized(); |
| 271 int y = CenterPosition(close_button_size.height(), height()); | 323 int y = CenterPosition(close_button_size.height(), height()); |
| 272 close_button_->SetBounds(next_x, y, | 324 close_button_->SetBounds(next_x, y, |
| 273 is_maximized ? width() - next_x : close_button_size.width(), | 325 is_maximized ? width() - next_x : close_button_size.width(), |
| 274 is_maximized ? height() - y : close_button_size.height()); | 326 is_maximized ? height() - y : close_button_size.height()); |
| 275 if (show_link_only) { | 327 if (show_link_only) { |
| 276 // Let's hide all the items. | 328 // Let's hide all the items. |
| 277 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) | 329 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) |
| 278 (*ri)->SetVisible(false); | 330 (*ri)->SetVisible(false); |
| 279 return; | 331 return; |
| 280 } | 332 } |
| 281 | 333 |
| 282 next_x = GetStartPadding(); | 334 next_x = GetStartPadding(); |
| 283 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) { | 335 for (auto ri = download_views_.rbegin(); ri != download_views_.rend(); ++ri) { |
| 284 gfx::Size view_size = (*ri)->GetPreferredSize(); | 336 gfx::Size view_size = (*ri)->GetPreferredSize(); |
| 285 | 337 |
| 286 int x = next_x; | 338 int x = next_x; |
| 287 | 339 |
| 288 // Figure out width of item. | 340 // Figure out width of item. |
| 289 int item_width = view_size.width(); | 341 int item_width = view_size.width(); |
| 290 if (new_item_animation_->is_animating() && ri == download_views_.rbegin()) { | 342 if (new_item_animation_.is_animating() && ri == download_views_.rbegin()) { |
| 291 item_width = static_cast<int>(static_cast<double>(view_size.width()) * | 343 item_width = static_cast<int>(static_cast<double>(view_size.width()) * |
| 292 new_item_animation_->GetCurrentValue()); | 344 new_item_animation_.GetCurrentValue()); |
| 293 } | 345 } |
| 294 | 346 |
| 295 next_x += item_width; | 347 next_x += item_width; |
| 296 | 348 |
| 297 // Make sure our item can be contained within the shelf. | 349 // Make sure our item can be contained within the shelf. |
| 298 if (next_x < max_download_x) { | 350 if (next_x < max_download_x) { |
| 299 (*ri)->SetVisible(true); | 351 (*ri)->SetVisible(true); |
| 300 (*ri)->SetBounds(x, CenterPosition(view_size.height(), height()), | 352 (*ri)->SetBounds(x, CenterPosition(view_size.height(), height()), |
| 301 item_width, view_size.height()); | 353 item_width, view_size.height()); |
| 302 } else { | 354 } else { |
| 303 (*ri)->SetVisible(false); | 355 (*ri)->SetVisible(false); |
| 304 } | 356 } |
| 305 } | 357 } |
| 306 } | 358 } |
| 307 | 359 |
| 308 void DownloadShelfView::ViewHierarchyChanged( | 360 void DownloadShelfView::ViewHierarchyChanged( |
| 309 const ViewHierarchyChangedDetails& details) { | 361 const ViewHierarchyChangedDetails& details) { |
| 310 View::ViewHierarchyChanged(details); | 362 View::ViewHierarchyChanged(details); |
| 311 | 363 if (details.is_add) |
| 312 if (details.is_add && (details.child == this)) { | |
| 313 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 314 arrow_image_ = new views::ImageView(); | |
| 315 arrow_image_->SetImage(rb.GetImageSkiaNamed(IDR_DOWNLOADS_FAVICON)); | |
| 316 AddChildView(arrow_image_); | |
| 317 | |
| 318 show_all_view_ = new views::Link( | |
| 319 l10n_util::GetStringUTF16(IDS_SHOW_ALL_DOWNLOADS)); | |
| 320 show_all_view_->set_listener(this); | |
| 321 AddChildView(show_all_view_); | |
| 322 | |
| 323 close_button_ = new views::ImageButton(this); | |
| 324 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
| 325 rb.GetImageSkiaNamed(IDR_CLOSE_1)); | |
| 326 close_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
| 327 rb.GetImageSkiaNamed(IDR_CLOSE_1_H)); | |
| 328 close_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
| 329 rb.GetImageSkiaNamed(IDR_CLOSE_1_P)); | |
| 330 close_button_->SetAccessibleName( | |
| 331 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); | |
| 332 AddChildView(close_button_); | |
| 333 | |
| 334 UpdateColorsFromTheme(); | 364 UpdateColorsFromTheme(); |
| 335 | |
| 336 new_item_animation_.reset(new gfx::SlideAnimation(this)); | |
| 337 new_item_animation_->SetSlideDuration(kNewItemAnimationDurationMs); | |
| 338 | |
| 339 shelf_animation_.reset(new gfx::SlideAnimation(this)); | |
| 340 shelf_animation_->SetSlideDuration(kShelfAnimationDurationMs); | |
| 341 } | |
| 342 } | 365 } |
| 343 | 366 |
| 344 bool DownloadShelfView::CanFitFirstDownloadItem() { | 367 bool DownloadShelfView::CanFitFirstDownloadItem() { |
| 345 if (download_views_.empty()) | 368 if (download_views_.empty()) |
| 346 return true; | 369 return true; |
| 347 | 370 |
| 348 gfx::Size image_size = arrow_image_->GetPreferredSize(); | 371 gfx::Size image_size = arrow_image_->GetPreferredSize(); |
| 349 gfx::Size close_button_size = close_button_->GetPreferredSize(); | 372 gfx::Size close_button_size = close_button_->GetPreferredSize(); |
| 350 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); | 373 gfx::Size show_all_size = show_all_view_->GetPreferredSize(); |
| 351 | 374 |
| 352 // Let's compute the width available for download items, which is the width | 375 // Let's compute the width available for download items, which is the width |
| 353 // of the shelf minus the "Show all downloads" link, arrow and close button | 376 // of the shelf minus the "Show all downloads" link, arrow and close button |
| 354 // and the padding. | 377 // and the padding. |
| 355 int available_width = width() - GetEndPadding() - close_button_size.width() - | 378 int available_width = width() - GetEndPadding() - close_button_size.width() - |
| 356 kCloseAndLinkPadding - show_all_size.width() - | 379 GetCloseAndLinkPadding() - show_all_size.width() - |
| 357 kDownloadsTitlePadding - image_size.width() - | 380 kDownloadsTitlePadding - image_size.width() - |
| 358 GetBetweenItemPadding() - GetStartPadding(); | 381 GetBetweenItemPadding() - GetStartPadding(); |
| 359 if (available_width <= 0) | 382 if (available_width <= 0) |
| 360 return false; | 383 return false; |
| 361 | 384 |
| 362 gfx::Size item_size = (*download_views_.rbegin())->GetPreferredSize(); | 385 gfx::Size item_size = (*download_views_.rbegin())->GetPreferredSize(); |
| 363 return item_size.width() < available_width; | 386 return item_size.width() < available_width; |
| 364 } | 387 } |
| 365 | 388 |
| 366 void DownloadShelfView::UpdateColorsFromTheme() { | 389 void DownloadShelfView::UpdateColorsFromTheme() { |
| 367 if (show_all_view_ && close_button_ && GetThemeProvider()) { | 390 if (show_all_view_ && close_button_ && GetThemeProvider()) { |
| 391 set_background(views::Background::CreateSolidBackground( | |
| 392 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR))); | |
| 393 | |
| 394 if (!ui::MaterialDesignController::IsModeMaterial()) { | |
| 395 views::Link* show_all_view = static_cast<views::Link*>(show_all_view_); | |
| 396 show_all_view->SetBackgroundColor(background()->get_color()); | |
| 397 show_all_view->SetEnabledColor( | |
| 398 GetThemeProvider()->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT)); | |
| 399 } | |
| 400 | |
| 368 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 401 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 369 set_background(views::Background::CreateSolidBackground( | 402 set_background(views::Background::CreateSolidBackground( |
| 370 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR))); | 403 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR))); |
| 371 show_all_view_->SetBackgroundColor(background()->get_color()); | |
| 372 show_all_view_->SetEnabledColor( | |
| 373 GetThemeProvider()->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT)); | |
| 374 close_button_->SetBackground( | 404 close_button_->SetBackground( |
| 375 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT), | 405 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TAB_TEXT), |
| 376 rb.GetImageSkiaNamed(IDR_CLOSE_1), | 406 rb.GetImageSkiaNamed(IDR_CLOSE_1), |
| 377 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); | 407 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); |
| 378 } | 408 } |
| 379 } | 409 } |
| 380 | 410 |
| 381 void DownloadShelfView::OnThemeChanged() { | 411 void DownloadShelfView::OnThemeChanged() { |
| 382 UpdateColorsFromTheme(); | 412 UpdateColorsFromTheme(); |
| 383 } | 413 } |
| 384 | 414 |
| 385 void DownloadShelfView::LinkClicked(views::Link* source, int event_flags) { | 415 void DownloadShelfView::LinkClicked(views::Link* source, int event_flags) { |
| 386 chrome::ShowDownloads(browser_); | 416 chrome::ShowDownloads(browser_); |
| 387 } | 417 } |
| 388 | 418 |
| 389 void DownloadShelfView::ButtonPressed( | 419 void DownloadShelfView::ButtonPressed( |
| 390 views::Button* button, const ui::Event& event) { | 420 views::Button* button, const ui::Event& event) { |
| 391 Close(USER_ACTION); | 421 if (button == close_button_) |
| 422 Close(USER_ACTION); | |
| 423 else if (button == show_all_view_) | |
| 424 chrome::ShowDownloads(browser_); | |
| 425 else | |
| 426 NOTREACHED(); | |
| 392 } | 427 } |
| 393 | 428 |
| 394 bool DownloadShelfView::IsShowing() const { | 429 bool DownloadShelfView::IsShowing() const { |
| 395 return visible() && shelf_animation_->IsShowing(); | 430 return visible() && shelf_animation_.IsShowing(); |
| 396 } | 431 } |
| 397 | 432 |
| 398 bool DownloadShelfView::IsClosing() const { | 433 bool DownloadShelfView::IsClosing() const { |
| 399 return shelf_animation_->IsClosing(); | 434 return shelf_animation_.IsClosing(); |
| 400 } | 435 } |
| 401 | 436 |
| 402 void DownloadShelfView::DoShow() { | 437 void DownloadShelfView::DoShow() { |
| 403 SetVisible(true); | 438 SetVisible(true); |
| 404 shelf_animation_->Show(); | 439 shelf_animation_.Show(); |
| 405 } | 440 } |
| 406 | 441 |
| 407 void DownloadShelfView::DoClose(CloseReason reason) { | 442 void DownloadShelfView::DoClose(CloseReason reason) { |
| 408 int num_in_progress = 0; | 443 int num_in_progress = 0; |
| 409 for (size_t i = 0; i < download_views_.size(); ++i) { | 444 for (size_t i = 0; i < download_views_.size(); ++i) { |
| 410 if (GetDownloadItemForView(i)->GetState() == DownloadItem::IN_PROGRESS) | 445 if (GetDownloadItemForView(i)->GetState() == DownloadItem::IN_PROGRESS) |
| 411 ++num_in_progress; | 446 ++num_in_progress; |
| 412 } | 447 } |
| 413 RecordDownloadShelfClose( | 448 RecordDownloadShelfClose( |
| 414 download_views_.size(), num_in_progress, reason == AUTOMATIC); | 449 download_views_.size(), num_in_progress, reason == AUTOMATIC); |
| 415 parent_->SetDownloadShelfVisible(false); | 450 parent_->SetDownloadShelfVisible(false); |
| 416 shelf_animation_->Hide(); | 451 shelf_animation_.Hide(); |
| 417 } | 452 } |
| 418 | 453 |
| 419 Browser* DownloadShelfView::browser() const { | 454 Browser* DownloadShelfView::browser() const { |
| 420 return browser_; | 455 return browser_; |
| 421 } | 456 } |
| 422 | 457 |
| 423 void DownloadShelfView::Closed() { | 458 void DownloadShelfView::Closed() { |
| 424 // Don't remove completed downloads if the shelf is just being auto-hidden | 459 // Don't remove completed downloads if the shelf is just being auto-hidden |
| 425 // rather than explicitly closed by the user. | 460 // rather than explicitly closed by the user. |
| 426 if (is_hidden()) | 461 if (is_hidden()) |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 451 return false; | 486 return false; |
| 452 } | 487 } |
| 453 return true; | 488 return true; |
| 454 } | 489 } |
| 455 | 490 |
| 456 content::DownloadItem* DownloadShelfView::GetDownloadItemForView(size_t i) { | 491 content::DownloadItem* DownloadShelfView::GetDownloadItemForView(size_t i) { |
| 457 if (ui::MaterialDesignController::IsModeMaterial()) | 492 if (ui::MaterialDesignController::IsModeMaterial()) |
| 458 return static_cast<DownloadItemViewMd*>(download_views_[i])->download(); | 493 return static_cast<DownloadItemViewMd*>(download_views_[i])->download(); |
| 459 return static_cast<DownloadItemView*>(download_views_[i])->download(); | 494 return static_cast<DownloadItemView*>(download_views_[i])->download(); |
| 460 } | 495 } |
| OLD | NEW |