Commit b3da2697 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Move PaintDownloadXXX() from DownloadShelf to DownloadItemView.

This is the only user of these functions, and they're not
"shelf"-related in any particular way.

(No code changes to the functions themselves.)

Bug: none
Change-Id: Idfd321b09fd9926f27523710cca0a3f209e35835
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2251053
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779936}
parent 260c078c
...@@ -9,12 +9,8 @@ ...@@ -9,12 +9,8 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/location.h" #include "base/location.h"
#include "base/numerics/math_constants.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "cc/paint/paint_flags.h"
#include "chrome/browser/download/download_core_service.h" #include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h" #include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_item_model.h" #include "chrome/browser/download/download_item_model.h"
...@@ -27,21 +23,15 @@ ...@@ -27,21 +23,15 @@
#include "chrome/browser/platform_util.h" #include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h" #include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/download/public/common/download_item.h" #include "components/download/public/common/download_item.h"
#include "components/offline_items_collection/core/offline_content_aggregator.h" #include "components/offline_items_collection/core/offline_content_aggregator.h"
#include "components/offline_items_collection/core/offline_item.h" #include "components/offline_items_collection/core/offline_item.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager.h" #include "content/public/browser/download_manager.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/animation/animation.h" #include "ui/gfx/animation/animation.h"
#include "ui/gfx/canvas.h"
using download::DownloadItem; using download::DownloadItem;
...@@ -50,20 +40,6 @@ namespace { ...@@ -50,20 +40,6 @@ namespace {
// Delay before we show a transient download. // Delay before we show a transient download.
const int64_t kDownloadShowDelayInSeconds = 2; const int64_t kDownloadShowDelayInSeconds = 2;
// Get the opacity based on |animation_progress|, with values in [0.0, 1.0].
// Range of return value is [0, 255].
int GetOpacity(double animation_progress) {
DCHECK(animation_progress >= 0 && animation_progress <= 1);
// How many times to cycle the complete animation. This should be an odd
// number so that the animation ends faded out.
static const int kCompleteAnimationCycles = 5;
double temp =
((animation_progress * kCompleteAnimationCycles) + 0.5) * base::kPiDouble;
temp = sin(temp) / 2 + 0.5;
return static_cast<int>(255.0 * temp);
}
void OnGetDownloadDoneForOfflineItem( void OnGetDownloadDoneForOfflineItem(
Profile* profile, Profile* profile,
base::OnceCallback<void(DownloadUIModelPtr)> callback, base::OnceCallback<void(DownloadUIModelPtr)> callback,
...@@ -115,74 +91,6 @@ DownloadShelf::DownloadShelf(Browser* browser, Profile* profile) ...@@ -115,74 +91,6 @@ DownloadShelf::DownloadShelf(Browser* browser, Profile* profile)
DownloadShelf::~DownloadShelf() {} DownloadShelf::~DownloadShelf() {}
// Download progress painting --------------------------------------------------
// static
void DownloadShelf::PaintDownloadProgress(
gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
const base::TimeDelta& progress_time,
int percent_done) {
// Draw background (light blue circle).
cc::PaintFlags bg_flags;
bg_flags.setStyle(cc::PaintFlags::kFill_Style);
SkColor indicator_color =
theme_provider.GetColor(ThemeProperties::COLOR_TAB_THROBBER_SPINNING);
bg_flags.setColor(SkColorSetA(indicator_color, 0x33));
bg_flags.setAntiAlias(true);
const SkScalar kCenterPoint = kProgressIndicatorSize / 2.f;
SkPath bg;
bg.addCircle(kCenterPoint, kCenterPoint, kCenterPoint);
canvas->DrawPath(bg, bg_flags);
// Calculate progress.
SkScalar sweep_angle = 0.f;
// Start at 12 o'clock.
SkScalar start_pos = SkIntToScalar(270);
if (percent_done < 0) {
// For unknown size downloads, draw a 50 degree sweep that moves at
// 0.08 degrees per millisecond.
sweep_angle = 50.f;
start_pos += static_cast<SkScalar>(progress_time.InMilliseconds() * 0.08);
} else if (percent_done > 0) {
sweep_angle = static_cast<SkScalar>(360 * percent_done / 100.0);
}
// Draw progress.
SkPath progress;
progress.addArc(
SkRect::MakeLTRB(0, 0, kProgressIndicatorSize, kProgressIndicatorSize),
start_pos, sweep_angle);
cc::PaintFlags progress_flags;
progress_flags.setColor(indicator_color);
progress_flags.setStyle(cc::PaintFlags::kStroke_Style);
progress_flags.setStrokeWidth(1.7f);
progress_flags.setAntiAlias(true);
canvas->DrawPath(progress, progress_flags);
}
// static
void DownloadShelf::PaintDownloadComplete(
gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
double animation_progress) {
// Start at full opacity, then loop back and forth five times before ending
// at zero opacity.
canvas->SaveLayerAlpha(GetOpacity(animation_progress));
PaintDownloadProgress(canvas, theme_provider, base::TimeDelta(), 100);
canvas->Restore();
}
// static
void DownloadShelf::PaintDownloadInterrupted(
gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
double animation_progress) {
// Start at zero opacity, then loop back and forth five times before ending
// at full opacity.
PaintDownloadComplete(canvas, theme_provider, 1.0 - animation_progress);
}
void DownloadShelf::AddDownload(DownloadUIModelPtr model) { void DownloadShelf::AddDownload(DownloadUIModelPtr model) {
DCHECK(model); DCHECK(model);
if (model->ShouldRemoveFromShelfWhenComplete()) { if (model->ShouldRemoveFromShelfWhenComplete()) {
......
...@@ -14,14 +14,6 @@ ...@@ -14,14 +14,6 @@
class Browser; class Browser;
namespace gfx {
class Canvas;
}
namespace ui {
class ThemeProvider;
}
using offline_items_collection::ContentId; using offline_items_collection::ContentId;
using offline_items_collection::OfflineItem; using offline_items_collection::OfflineItem;
using DownloadUIModelPtr = DownloadUIModel::DownloadUIModelPtr; using DownloadUIModelPtr = DownloadUIModel::DownloadUIModelPtr;
...@@ -30,29 +22,9 @@ using DownloadUIModelPtr = DownloadUIModel::DownloadUIModelPtr; ...@@ -30,29 +22,9 @@ using DownloadUIModelPtr = DownloadUIModel::DownloadUIModelPtr;
// implementations. // implementations.
class DownloadShelf { class DownloadShelf {
public: public:
// Size of the space used for the progress indicator.
static constexpr int kProgressIndicatorSize = 25;
DownloadShelf(Browser* browser, Profile* profile); DownloadShelf(Browser* browser, Profile* profile);
virtual ~DownloadShelf(); virtual ~DownloadShelf();
// Paint the common download animation progress foreground and background,
// clipping the foreground to 'percent' full. If percent is -1, then we don't
// know the total size, so we just draw a rotating segment until we're done.
// |progress_time| is only used for these unknown size downloads.
static void PaintDownloadProgress(gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
const base::TimeDelta& progress_time,
int percent);
static void PaintDownloadComplete(gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
double animation_progress);
static void PaintDownloadInterrupted(gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
double animation_progress);
// A new download has started. Add it to our shelf and show the download // A new download has started. Add it to our shelf and show the download
// started animation. // started animation.
// //
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stddef.h> #include <stddef.h>
#include <algorithm> #include <algorithm>
#include <cmath>
#include <vector> #include <vector>
#include "base/bind.h" #include "base/bind.h"
...@@ -18,12 +19,14 @@ ...@@ -18,12 +19,14 @@
#include "base/i18n/rtl.h" #include "base/i18n/rtl.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/numerics/math_constants.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "cc/paint/paint_flags.h"
#include "chrome/app/vector_icons/vector_icons.h" #include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h" #include "chrome/browser/download/chrome_download_manager_delegate.h"
...@@ -59,6 +62,9 @@ ...@@ -59,6 +62,9 @@
#include "components/vector_icons/vector_icons.h" #include "components/vector_icons/vector_icons.h"
#include "content/public/browser/download_item_utils.h" #include "content/public/browser/download_item_utils.h"
#include "third_party/icu/source/common/unicode/uchar.h" #include "third_party/icu/source/common/unicode/uchar.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkScalar.h"
#include "ui/accessibility/ax_node_data.h" #include "ui/accessibility/ax_node_data.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/time_format.h" #include "ui/base/l10n/time_format.h"
...@@ -99,6 +105,9 @@ using MixedContentStatus = download::DownloadItem::MixedContentStatus; ...@@ -99,6 +105,9 @@ using MixedContentStatus = download::DownloadItem::MixedContentStatus;
namespace { namespace {
// Size of the space used for the progress indicator.
constexpr int kProgressIndicatorSize = 25;
// The vertical distance between the item's visual upper bound (as delineated // The vertical distance between the item's visual upper bound (as delineated
// by the separator on the right) and the edge of the shelf. // by the separator on the right) and the edge of the shelf.
constexpr int kTopBottomPadding = 6; constexpr int kTopBottomPadding = 6;
...@@ -200,6 +209,84 @@ class TransparentButton : public views::Button { ...@@ -200,6 +209,84 @@ class TransparentButton : public views::Button {
} }
}; };
// Get the opacity based on |animation_progress|, with values in [0.0, 1.0].
// Range of return value is [0, 255].
int GetOpacity(double animation_progress) {
DCHECK(animation_progress >= 0 && animation_progress <= 1);
// How many times to cycle the complete animation. This should be an odd
// number so that the animation ends faded out.
static const int kCompleteAnimationCycles = 5;
double temp =
((animation_progress * kCompleteAnimationCycles) + 0.5) * base::kPiDouble;
temp = sin(temp) / 2 + 0.5;
return static_cast<int>(255.0 * temp);
}
// Paint the common download animation progress foreground and background,
// clipping the foreground to 'percent' full. If percent is -1, then we don't
// know the total size, so we just draw a rotating segment until we're done.
// |progress_time| is only used for these unknown size downloads.
void PaintDownloadProgress(gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
const base::TimeDelta& progress_time,
int percent_done) {
// Draw background (light blue circle).
cc::PaintFlags bg_flags;
bg_flags.setStyle(cc::PaintFlags::kFill_Style);
SkColor indicator_color =
theme_provider.GetColor(ThemeProperties::COLOR_TAB_THROBBER_SPINNING);
bg_flags.setColor(SkColorSetA(indicator_color, 0x33));
bg_flags.setAntiAlias(true);
const SkScalar kCenterPoint = kProgressIndicatorSize / 2.f;
SkPath bg;
bg.addCircle(kCenterPoint, kCenterPoint, kCenterPoint);
canvas->DrawPath(bg, bg_flags);
// Calculate progress.
SkScalar sweep_angle = 0.f;
// Start at 12 o'clock.
SkScalar start_pos = SkIntToScalar(270);
if (percent_done < 0) {
// For unknown size downloads, draw a 50 degree sweep that moves at
// 0.08 degrees per millisecond.
sweep_angle = 50.f;
start_pos += static_cast<SkScalar>(progress_time.InMilliseconds() * 0.08);
} else if (percent_done > 0) {
sweep_angle = static_cast<SkScalar>(360 * percent_done / 100.0);
}
// Draw progress.
SkPath progress;
progress.addArc(
SkRect::MakeLTRB(0, 0, kProgressIndicatorSize, kProgressIndicatorSize),
start_pos, sweep_angle);
cc::PaintFlags progress_flags;
progress_flags.setColor(indicator_color);
progress_flags.setStyle(cc::PaintFlags::kStroke_Style);
progress_flags.setStrokeWidth(1.7f);
progress_flags.setAntiAlias(true);
canvas->DrawPath(progress, progress_flags);
}
void PaintDownloadComplete(gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
double animation_progress) {
// Start at full opacity, then loop back and forth five times before ending
// at zero opacity.
canvas->SaveLayerAlpha(GetOpacity(animation_progress));
PaintDownloadProgress(canvas, theme_provider, base::TimeDelta(), 100);
canvas->Restore();
}
void PaintDownloadInterrupted(gfx::Canvas* canvas,
const ui::ThemeProvider& theme_provider,
double animation_progress) {
// Start at zero opacity, then loop back and forth five times before ending
// at full opacity.
PaintDownloadComplete(canvas, theme_provider, 1.0 - animation_progress);
}
} // namespace } // namespace
DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr download, DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr download,
...@@ -519,8 +606,7 @@ void DownloadItemView::Layout() { ...@@ -519,8 +606,7 @@ void DownloadItemView::Layout() {
} }
} else { } else {
int mirrored_x = GetMirroredXWithWidthInView( int mirrored_x = GetMirroredXWithWidthInView(
kStartPadding + DownloadShelf::kProgressIndicatorSize + kStartPadding + kProgressIndicatorSize + kProgressTextPadding,
kProgressTextPadding,
kTextWidth); kTextWidth);
int file_name_y = GetYForFilenameText(); int file_name_y = GetYForFilenameText();
file_name_label_->SetBoundsRect( file_name_label_->SetBoundsRect(
...@@ -587,8 +673,8 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const { ...@@ -587,8 +673,8 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const {
status_width = status_width =
std::max(status_width, status_label_->GetPreferredSize().width()); std::max(status_width, status_label_->GetPreferredSize().width());
} }
width = kStartPadding + DownloadShelf::kProgressIndicatorSize + width = kStartPadding + kProgressIndicatorSize + kProgressTextPadding +
kProgressTextPadding + status_width + kEndPadding; status_width + kEndPadding;
} }
if (mode_ != DANGEROUS_MODE) if (mode_ != DANGEROUS_MODE)
...@@ -792,11 +878,10 @@ void DownloadItemView::DrawIcon(gfx::Canvas* canvas) { ...@@ -792,11 +878,10 @@ void DownloadItemView::DrawIcon(gfx::Canvas* canvas) {
// Paint download progress. // Paint download progress.
DownloadItem::DownloadState state = model_->GetState(); DownloadItem::DownloadState state = model_->GetState();
canvas->Save(); canvas->Save();
int progress_x = int progress_x = base::i18n::IsRTL()
base::i18n::IsRTL() ? width() - kStartPadding - kProgressIndicatorSize
? width() - kStartPadding - DownloadShelf::kProgressIndicatorSize : kStartPadding;
: kStartPadding; int progress_y = (height() - kProgressIndicatorSize) / 2;
int progress_y = (height() - DownloadShelf::kProgressIndicatorSize) / 2;
canvas->Translate(gfx::Vector2d(progress_x, progress_y)); canvas->Translate(gfx::Vector2d(progress_x, progress_y));
const gfx::ImageSkia* current_icon = nullptr; const gfx::ImageSkia* current_icon = nullptr;
...@@ -811,16 +896,16 @@ void DownloadItemView::DrawIcon(gfx::Canvas* canvas) { ...@@ -811,16 +896,16 @@ void DownloadItemView::DrawIcon(gfx::Canvas* canvas) {
base::TimeDelta progress_time = previous_progress_elapsed_; base::TimeDelta progress_time = previous_progress_elapsed_;
if (!model_->IsPaused()) if (!model_->IsPaused())
progress_time += base::TimeTicks::Now() - progress_start_time_; progress_time += base::TimeTicks::Now() - progress_start_time_;
DownloadShelf::PaintDownloadProgress( PaintDownloadProgress(canvas, *GetThemeProvider(), progress_time,
canvas, *GetThemeProvider(), progress_time, model_->PercentComplete()); model_->PercentComplete());
} else if (complete_animation_.get() && complete_animation_->is_animating()) { } else if (complete_animation_.get() && complete_animation_->is_animating()) {
if (state == DownloadItem::INTERRUPTED) { if (state == DownloadItem::INTERRUPTED) {
DownloadShelf::PaintDownloadInterrupted( PaintDownloadInterrupted(canvas, *GetThemeProvider(),
canvas, *GetThemeProvider(), complete_animation_->GetCurrentValue()); complete_animation_->GetCurrentValue());
} else { } else {
DCHECK_EQ(DownloadItem::COMPLETE, state); DCHECK_EQ(DownloadItem::COMPLETE, state);
DownloadShelf::PaintDownloadComplete( PaintDownloadComplete(canvas, *GetThemeProvider(),
canvas, *GetThemeProvider(), complete_animation_->GetCurrentValue()); complete_animation_->GetCurrentValue());
} }
} else if (use_new_warnings) { } else if (use_new_warnings) {
current_icon = &icon_; current_icon = &icon_;
...@@ -832,7 +917,7 @@ void DownloadItemView::DrawIcon(gfx::Canvas* canvas) { ...@@ -832,7 +917,7 @@ void DownloadItemView::DrawIcon(gfx::Canvas* canvas) {
// Draw the icon image. // Draw the icon image.
int kFiletypeIconOffset = int kFiletypeIconOffset =
(DownloadShelf::kProgressIndicatorSize - current_icon->height()) / 2; (kProgressIndicatorSize - current_icon->height()) / 2;
int icon_x = progress_x + kFiletypeIconOffset; int icon_x = progress_x + kFiletypeIconOffset;
int icon_y = progress_y + kFiletypeIconOffset; int icon_y = progress_y + kFiletypeIconOffset;
cc::PaintFlags flags; cc::PaintFlags flags;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment