Skip to content

Commit

Permalink
[Downloads] Refactor downloads animation code
Browse files Browse the repository at this point in the history
This refactors the current download animation (small down-arrow that
moves towards the download shelf), in preparation for reusing the code
for a new animation for the new download bubble UI.

Bug: 1410789
Change-Id: I54df7222b3742ebd818460e1b1f062d0b994da61
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4210433
Reviewed-by: Xinghui Lu <xinghuilu@chromium.org>
Commit-Queue: Lily Chen <chlily@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1100563}
  • Loading branch information
chlily1 authored and Chromium LUCI CQ committed Feb 2, 2023
1 parent 8331794 commit 7cc21be
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 92 deletions.
1 change: 0 additions & 1 deletion chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ static_library("browser") {
"download/download_query.h",
"download/download_request_limiter.cc",
"download/download_request_limiter.h",
"download/download_started_animation.h",
"download/download_stats.cc",
"download/download_stats.h",
"download/download_status_updater.cc",
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/download/download_shelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_started_animation.h"
#include "chrome/browser/download/offline_item_model.h"
#include "chrome/browser/download/offline_item_model_manager.h"
#include "chrome/browser/download/offline_item_model_manager_factory.h"
Expand All @@ -23,6 +22,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/download/download_started_animation.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/download/public/common/download_item.h"
#include "components/offline_items_collection/core/offline_content_aggregator.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/download/download_started_animation.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/download/download_started_animation.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ static_library("ui") {
"content_settings/content_setting_image_model_states.h",
"download/download_item_mode.cc",
"download/download_item_mode.h",
"download/download_started_animation.h",
"exclusive_access/exclusive_access_bubble.cc",
"exclusive_access/exclusive_access_bubble.h",
"exclusive_access/exclusive_access_bubble_hide_callback.h",
Expand Down Expand Up @@ -4515,9 +4516,13 @@ static_library("ui") {
"views/download/download_item_view.h",
"views/download/download_shelf_context_menu_view.cc",
"views/download/download_shelf_context_menu_view.h",
"views/download/download_shelf_started_animation_views.cc",
"views/download/download_shelf_started_animation_views.h",
"views/download/download_shelf_view.cc",
"views/download/download_shelf_view.h",
"views/download/download_started_animation.cc",
"views/download/download_started_animation_views.cc",
"views/download/download_started_animation_views.h",
"views/dropdown_bar_host.cc",
"views/dropdown_bar_host.h",
"views/dropdown_bar_host_delegate.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_STARTED_ANIMATION_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_STARTED_ANIMATION_H_
#ifndef CHROME_BROWSER_UI_DOWNLOAD_DOWNLOAD_STARTED_ANIMATION_H_
#define CHROME_BROWSER_UI_DOWNLOAD_DOWNLOAD_STARTED_ANIMATION_H_

namespace content {
class WebContents;
Expand All @@ -17,7 +17,7 @@ class DownloadStartedAnimation {
static void Show(content::WebContents* web_contents);

private:
DownloadStartedAnimation() { }
DownloadStartedAnimation() {}
};

#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_STARTED_ANIMATION_H_
#endif // CHROME_BROWSER_UI_DOWNLOAD_DOWNLOAD_STARTED_ANIMATION_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/download/download_shelf_started_animation_views.h"

#include "base/i18n/rtl.h"
#include "base/time/time.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/views/download/download_started_animation_views.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/paint_vector_icon.h"

DownloadShelfStartedAnimationViews::DownloadShelfStartedAnimationViews(
content::WebContents* web_contents)
: DownloadStartedAnimationViews(
web_contents,
base::Milliseconds(600),
ui::ImageModel::FromVectorIcon(
kFileDownloadShelfIcon,
kColorDownloadStartedAnimationForeground,
72)) {}

int DownloadShelfStartedAnimationViews::GetX() const {
// Align the image with the bottom left of the web contents (so that it
// points to the newly created download).
return base::i18n::IsRTL()
? web_contents_bounds().right() - GetPreferredSize().width()
: web_contents_bounds().x();
}

int DownloadShelfStartedAnimationViews::GetY() const {
int height = GetPreferredSize().height();
return static_cast<int>(web_contents_bounds().bottom() - height -
height * (1 - GetCurrentValue()));
}

float DownloadShelfStartedAnimationViews::GetOpacity() const {
// Start at zero, peak halfway and end at zero.
return static_cast<float>(
std::min(1.0 - pow(GetCurrentValue() - 0.5, 2) * 4.0, 1.0));
}

BEGIN_METADATA(DownloadShelfStartedAnimationViews,
DownloadStartedAnimationViews)
END_METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_SHELF_STARTED_ANIMATION_VIEWS_H_
#define CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_SHELF_STARTED_ANIMATION_VIEWS_H_

#include "chrome/browser/ui/views/download/download_started_animation_views.h"
#include "ui/base/metadata/metadata_header_macros.h"

class DownloadShelfStartedAnimationViews
: public DownloadStartedAnimationViews {
public:
METADATA_HEADER(DownloadShelfStartedAnimationViews);
explicit DownloadShelfStartedAnimationViews(
content::WebContents* web_contents);
DownloadShelfStartedAnimationViews(
const DownloadShelfStartedAnimationViews&) = delete;
DownloadShelfStartedAnimationViews& operator=(
const DownloadShelfStartedAnimationViews&) = delete;
~DownloadShelfStartedAnimationViews() override = default;

private:
// DownloadStartedAnimationViews
int GetX() const override;
int GetY() const override;
float GetOpacity() const override;
};

#endif // CHROME_BROWSER_UI_VIEWS_DOWNLOAD_DOWNLOAD_SHELF_STARTED_ANIMATION_VIEWS_H_
17 changes: 17 additions & 0 deletions chrome/browser/ui/views/download/download_started_animation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/download/download_started_animation.h"

#include "chrome/browser/ui/views/download/download_shelf_started_animation_views.h"

namespace content {
class WebContents;
} // namespace content

// static
void DownloadStartedAnimation::Show(content::WebContents* web_contents) {
// The animation will delete itself when it's finished.
new DownloadShelfStartedAnimationViews(web_contents);
}
112 changes: 27 additions & 85 deletions chrome/browser/ui/views/download/download_started_animation_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,32 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/raw_ptr.h"
#include "chrome/browser/download/download_started_animation.h"
#include "chrome/browser/ui/views/download/download_started_animation_views.h"

#include "base/i18n/rtl.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/widget/widget.h"

// How long to spend moving downwards and fading out after waiting.
constexpr auto kMoveTime = base::Milliseconds(600);

// The animation framerate.
const int kFrameRateHz = 60;

namespace {

// DownloadStartAnimation creates an animation (which begins running
// immediately) that animates an image downward from the center of the frame
// provided on the constructor, while simultaneously fading it out. To use,
// simply call "new DownloadStartAnimation"; the class cleans itself up when it
// finishes animating.
class DownloadStartedAnimationViews : public gfx::LinearAnimation,
public views::ImageView {
public:
METADATA_HEADER(DownloadStartedAnimationViews);
explicit DownloadStartedAnimationViews(content::WebContents* web_contents);
DownloadStartedAnimationViews(const DownloadStartedAnimationViews&) = delete;
DownloadStartedAnimationViews& operator=(
const DownloadStartedAnimationViews&) = delete;

private:
// Move the animation to wherever it should currently be.
void Reposition();

// Shut down the animation cleanly.
void Close();

// Animation
void AnimateToState(double state) override;

// We use a TYPE_POPUP for the popup so that it may float above any windows in
// our UI.
raw_ptr<views::Widget> popup_;

// The content area at the start of the animation. We store this so that the
// download shelf's resizing of the content area doesn't cause the animation
// to move around. This means that once started, the animation won't move
// with the parent window, but it's so fast that this shouldn't cause too
// much heartbreak.
gfx::Rect web_contents_bounds_;
};

DownloadStartedAnimationViews::DownloadStartedAnimationViews(
content::WebContents* web_contents)
: gfx::LinearAnimation(kMoveTime, kFrameRateHz, nullptr), popup_(nullptr) {
auto download_image = ui::ImageModel::FromVectorIcon(
kFileDownloadShelfIcon, kColorDownloadStartedAnimationForeground, 72);

// If we're too small to show the download image, then don't bother -
// the shelf will be enough.
content::WebContents* web_contents,
base::TimeDelta duration,
const ui::ImageModel& image)
: gfx::LinearAnimation(duration,
gfx::LinearAnimation::kDefaultFrameRate,
/*delegate=*/nullptr) {
// If we're too small to show the download image, then don't bother.
web_contents_bounds_ = web_contents->GetContainerBounds();
if (web_contents_bounds_.height() < download_image.Size().height())
if (WebContentsTooSmall(image.Size())) {
return;
}

SetImage(download_image);
SetImage(image);

popup_ = new views::Widget;

Expand All @@ -87,24 +38,26 @@ DownloadStartedAnimationViews::DownloadStartedAnimationViews(
popup_->Init(std::move(params));
popup_->SetOpacity(0.f);
popup_->SetContentsView(this);
Reposition();
popup_->Show();

Start();
}

int DownloadStartedAnimationViews::GetWidth() const {
return GetPreferredSize().width();
}

int DownloadStartedAnimationViews::GetHeight() const {
return GetPreferredSize().height();
}

bool DownloadStartedAnimationViews::WebContentsTooSmall(
const gfx::Size& image_size) const {
return web_contents_bounds_.height() < image_size.height();
}

void DownloadStartedAnimationViews::Reposition() {
// Align the image with the bottom left of the web contents (so that it
// points to the newly created download).
gfx::Size size = GetPreferredSize();
int x = base::i18n::IsRTL() ?
web_contents_bounds_.right() - size.width() : web_contents_bounds_.x();
popup_->SetBounds(gfx::Rect(
x,
static_cast<int>(web_contents_bounds_.bottom() -
size.height() - size.height() * (1 - GetCurrentValue())),
size.width(),
size.height()));
popup_->SetBounds(gfx::Rect(GetX(), GetY(), GetWidth(), GetHeight()));
}

void DownloadStartedAnimationViews::Close() {
Expand All @@ -116,20 +69,9 @@ void DownloadStartedAnimationViews::AnimateToState(double state) {
Close();
} else {
Reposition();

// Start at zero, peak halfway and end at zero.
popup_->SetOpacity(static_cast<float>(
std::min(1.0 - pow(GetCurrentValue() - 0.5, 2) * 4.0, 1.0)));
popup_->SetOpacity(GetOpacity());
}
}

BEGIN_METADATA(DownloadStartedAnimationViews, views::ImageView)
END_METADATA

} // namespace

// static
void DownloadStartedAnimation::Show(content::WebContents* web_contents) {
// The animation will delete itself when it's finished.
new DownloadStartedAnimationViews(web_contents);
}

0 comments on commit 7cc21be

Please sign in to comment.