Add a progress bar corner radius setter
Add a constructor parameter for the corner radius on the progress bar so
we can set a custom corner radius in a follow up CL.
Change-Id: I9309a66dbc4ea215bb3949e5f6dbe08e56087892
Bug: b/286931547
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4974822
Commit-Queue: Andrew Pantera <andp@chromium.org>
Reviewed-by: Cait Phillips <caitkp@chromium.org>
Reviewed-by: Allen Bauer <kylixrd@chromium.org>
Reviewed-by: Tommy Steimel <steimel@chromium.org>
Reviewed-by: Luke Halliwell <halliwell@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1225935}
diff --git a/ash/glanceables/common/glanceables_progress_bar_view.cc b/ash/glanceables/common/glanceables_progress_bar_view.cc
index f5d0386..a123187 100644
--- a/ash/glanceables/common/glanceables_progress_bar_view.cc
+++ b/ash/glanceables/common/glanceables_progress_bar_view.cc
@@ -31,8 +31,9 @@
SetPreferredSize(kProgressBarContainerPreferredSize);
SetProperty(views::kMarginsKey, kProgressBarContainerMargins);
- progress_bar_ = AddChildView(std::make_unique<views::ProgressBar>(
- kProgressBarThickness, /*allow_round_corner=*/false));
+ progress_bar_ = AddChildView(std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(kProgressBarThickness);
+ progress_bar_->SetPreferredCornerRadii(absl::nullopt);
progress_bar_->SetID(base::to_underlying(GlanceablesViewId::kProgressBar));
progress_bar_->SetBackgroundColorId(cros_tokens::kCrosSysSystemOnBase);
progress_bar_->SetForegroundColorId(cros_tokens::kCrosSysPrimary);
diff --git a/ash/system/focus_mode/focus_mode_countdown_view.cc b/ash/system/focus_mode/focus_mode_countdown_view.cc
index ee453a1..339fbbf 100644
--- a/ash/system/focus_mode/focus_mode_countdown_view.cc
+++ b/ash/system/focus_mode/focus_mode_countdown_view.cc
@@ -84,8 +84,8 @@
// TODO(b/286931547): Timer Progress Bar.
progress_bar_ =
- timer_container->AddChildView(std::make_unique<views::ProgressBar>(
- /*preferred_height=*/kBarHeight, /*allow_round_corner*/ true));
+ timer_container->AddChildView(std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(kBarHeight);
progress_bar_->SetBackgroundColorId(cros_tokens::kCrosSysSystemOnBase);
progress_bar_->SetForegroundColorId(cros_tokens::kCrosSysPrimary);
progress_bar_->SetBorder(views::CreateEmptyBorder(
diff --git a/ash/system/focus_mode/focus_mode_countdown_view.h b/ash/system/focus_mode/focus_mode_countdown_view.h
index ea9bd21..dd07f03 100644
--- a/ash/system/focus_mode/focus_mode_countdown_view.h
+++ b/ash/system/focus_mode/focus_mode_countdown_view.h
@@ -40,6 +40,7 @@
private:
friend class FocusModeCountdownViewTest;
+
// The main timer label, displays the amount of time left in the focus
// session.
raw_ptr<views::Label, ExperimentalAsh> time_remaining_label_ = nullptr;
diff --git a/ash/system/phonehub/phone_hub_interstitial_view.cc b/ash/system/phonehub/phone_hub_interstitial_view.cc
index 8ac5fd8..859994d0 100644
--- a/ash/system/phonehub/phone_hub_interstitial_view.cc
+++ b/ash/system/phonehub/phone_hub_interstitial_view.cc
@@ -56,7 +56,8 @@
progress_bar_container->SetMainAxisAlignment(
views::BoxLayout::MainAxisAlignment::kCenter);
progress_bar_ = progress_bar_container->AddChildView(
- std::make_unique<views::ProgressBar>(2));
+ std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(2);
progress_bar_->SetForegroundColor(color_provider->GetContentLayerColor(
AshColorProvider::ContentLayerType::kIconColorProminent));
progress_bar_->SetValue(-1.0);
diff --git a/ash/system/tray/tray_detailed_view.cc b/ash/system/tray/tray_detailed_view.cc
index 185a90e..59cd3c2 100644
--- a/ash/system/tray/tray_detailed_view.cc
+++ b/ash/system/tray/tray_detailed_view.cc
@@ -235,9 +235,9 @@
void TrayDetailedView::ShowProgress(double value, bool visible) {
DCHECK(tri_view_);
if (!progress_bar_) {
- progress_bar_ = AddChildViewAt(
- std::make_unique<views::ProgressBar>(kTitleRowProgressBarHeight),
- kTitleRowProgressBarIndex + 1);
+ progress_bar_ = AddChildViewAt(std::make_unique<views::ProgressBar>(),
+ kTitleRowProgressBarIndex + 1);
+ progress_bar_->SetPreferredHeight(kTitleRowProgressBarHeight);
progress_bar_->GetViewAccessibility().OverrideName(
progress_bar_accessible_name_.value_or(l10n_util::GetStringUTF16(
IDS_ASH_STATUS_TRAY_PROGRESS_BAR_ACCESSIBLE_NAME)));
diff --git a/chrome/browser/ash/arc/nearby_share/ui/progress_bar_dialog_view.cc b/chrome/browser/ash/arc/nearby_share/ui/progress_bar_dialog_view.cc
index 4fa6482..ab91ae2 100644
--- a/chrome/browser/ash/arc/nearby_share/ui/progress_bar_dialog_view.cc
+++ b/chrome/browser/ash/arc/nearby_share/ui/progress_bar_dialog_view.cc
@@ -60,8 +60,8 @@
message_label_->SetVerticalAlignment(gfx::ALIGN_TOP);
AddChildView(message_label_.get());
- progress_bar_ = AddChildView(std::make_unique<views::ProgressBar>(
- /*preferred_height=*/kProgressBarHeight));
+ progress_bar_ = AddChildView(std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(kProgressBarHeight);
progress_bar_->SetValue(0.01); // set small initial value.
progress_bar_->SetPreferredSize(
gfx::Size(kProgressBarWidth, kProgressBarHeight));
diff --git a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc
index e1a83fd..c80fec2 100644
--- a/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc
+++ b/chrome/browser/ui/views/bruschetta/bruschetta_installer_view.cc
@@ -163,7 +163,8 @@
learn_more_url_));
secondary_message_container_view->AddChildView(link_label_.get());
- progress_bar_ = new views::ProgressBar(kProgressBarHeight);
+ progress_bar_ = new views::ProgressBar();
+ progress_bar_->SetPreferredHeight(kProgressBarHeight);
progress_bar_->SetProperty(
views::kMarginsKey,
gfx::Insets::TLBR(kProgressBarTopMargin - kProgressBarHeight, 0, 0, 0));
diff --git a/chrome/browser/ui/views/download/bubble/download_bubble_row_view.cc b/chrome/browser/ui/views/download/bubble/download_bubble_row_view.cc
index 6ea5f198..ac3ec04 100644
--- a/chrome/browser/ui/views/download/bubble/download_bubble_row_view.cc
+++ b/chrome/browser/ui/views/download/bubble/download_bubble_row_view.cc
@@ -510,9 +510,9 @@
gfx::Size(kNumColumns, 1));
progress_bar_holder_->SetProperty(views::kTableHorizAlignKey,
views::LayoutAlignment::kStretch);
- progress_bar_ =
- progress_bar_holder_->AddChildView(std::make_unique<views::ProgressBar>(
- /*preferred_height=*/kProgressBarHeight));
+ progress_bar_ = progress_bar_holder_->AddChildView(
+ std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(kProgressBarHeight);
progress_bar_->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL),
diff --git a/chrome/browser/ui/views/download/bubble/download_bubble_security_view.cc b/chrome/browser/ui/views/download/bubble/download_bubble_security_view.cc
index a261a29..4be503cc 100644
--- a/chrome/browser/ui/views/download/bubble/download_bubble_security_view.cc
+++ b/chrome/browser/ui/views/download/bubble/download_bubble_security_view.cc
@@ -573,8 +573,8 @@
progress_bar_holder->SetProperty(views::kTableHorizAlignKey,
views::LayoutAlignment::kStretch);
progress_bar_ =
- progress_bar_holder->AddChildView(std::make_unique<views::ProgressBar>(
- /*preferred_height=*/kProgressBarHeight));
+ progress_bar_holder->AddChildView(std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(kProgressBarHeight);
progress_bar_->SetProperty(
views::kMarginsKey,
gfx::Insets().set_top(ChromeLayoutProvider::Get()->GetDistanceMetric(
diff --git a/chrome/browser/ui/views/location_bar/cookie_controls/cookie_controls_bubble_view_controller.cc b/chrome/browser/ui/views/location_bar/cookie_controls/cookie_controls_bubble_view_controller.cc
index 579edfe..2976d5a 100644
--- a/chrome/browser/ui/views/location_bar/cookie_controls/cookie_controls_bubble_view_controller.cc
+++ b/chrome/browser/ui/views/location_bar/cookie_controls/cookie_controls_bubble_view_controller.cc
@@ -376,8 +376,9 @@
reloading_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
- auto progress_bar = std::make_unique<views::ProgressBar>(
- kProgressBarHeight, /*allow_round_corner=*/false);
+ auto progress_bar = std::make_unique<views::ProgressBar>();
+ progress_bar->SetPreferredHeight(kProgressBarHeight);
+ progress_bar->SetPreferredCornerRadii(absl::nullopt);
progress_bar->SetValue(-1);
auto reloading_content = std::make_unique<views::View>();
diff --git a/chrome/browser/ui/views/payments/payment_handler_web_flow_view_controller.cc b/chrome/browser/ui/views/payments/payment_handler_web_flow_view_controller.cc
index 202e4a2..88995b3 100644
--- a/chrome/browser/ui/views/payments/payment_handler_web_flow_view_controller.cc
+++ b/chrome/browser/ui/views/payments/payment_handler_web_flow_view_controller.cc
@@ -264,7 +264,8 @@
// Add the progress bar to the separator container. The progress bar
// colors will be set in PopulateSheetHeaderView.
progress_bar_ = header_content_separator_container()->AddChildView(
- std::make_unique<views::ProgressBar>(/*preferred_height=*/2));
+ std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(2);
if (!spec()->IsPaymentHandlerMinimalHeaderUXEnabled()) {
// Prior to minimal UX, the separator container used a Separator view,
// which uses the Chrome theme color which may not match the header color.
diff --git a/chrome/browser/ui/views/payments/secure_payment_confirmation_views_util.cc b/chrome/browser/ui/views/payments/secure_payment_confirmation_views_util.cc
index fd82e34..6e118338 100644
--- a/chrome/browser/ui/views/payments/secure_payment_confirmation_views_util.cc
+++ b/chrome/browser/ui/views/payments/secure_payment_confirmation_views_util.cc
@@ -85,8 +85,9 @@
std::unique_ptr<views::ProgressBar>
CreateSecurePaymentConfirmationProgressBarView() {
- auto progress_bar = std::make_unique<views::ProgressBar>(
- kProgressBarHeight, /*allow_round_corner=*/false);
+ auto progress_bar = std::make_unique<views::ProgressBar>();
+ progress_bar->SetPreferredHeight(kProgressBarHeight);
+ progress_bar->SetPreferredCornerRadii(absl::nullopt);
progress_bar->SetValue(-1); // infinite animation.
progress_bar->SetBackgroundColor(SK_ColorTRANSPARENT);
progress_bar->SetPreferredSize(
diff --git a/chrome/browser/ui/views/plugin_vm/plugin_vm_installer_view.cc b/chrome/browser/ui/views/plugin_vm/plugin_vm_installer_view.cc
index d36ef8a3..1c3a05aa 100644
--- a/chrome/browser/ui/views/plugin_vm/plugin_vm_installer_view.cc
+++ b/chrome/browser/ui/views/plugin_vm/plugin_vm_installer_view.cc
@@ -175,7 +175,8 @@
learn_more_link_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
message_container_view->AddChildView(learn_more_link_.get());
- progress_bar_ = new views::ProgressBar(kProgressBarHeight);
+ progress_bar_ = new views::ProgressBar();
+ progress_bar_->SetPreferredHeight(kProgressBarHeight);
progress_bar_->SetProperty(
views::kMarginsKey,
gfx::Insets::TLBR(kProgressBarTopMargin - kProgressBarHeight, 0, 0, 0));
diff --git a/chrome/browser/ui/views/webauthn/authenticator_request_sheet_view.cc b/chrome/browser/ui/views/webauthn/authenticator_request_sheet_view.cc
index 6189473..b8368f72 100644
--- a/chrome/browser/ui/views/webauthn/authenticator_request_sheet_view.cc
+++ b/chrome/browser/ui/views/webauthn/authenticator_request_sheet_view.cc
@@ -129,9 +129,10 @@
if (model()->IsActivityIndicatorVisible()) {
constexpr int kActivityIndicatorHeight = 4;
- auto activity_indicator = std::make_unique<views::ProgressBar>(
- kActivityIndicatorHeight, false /* allow_round_corner */);
- activity_indicator->SetValue(-1 /* inifinite animation */);
+ auto activity_indicator = std::make_unique<views::ProgressBar>();
+ activity_indicator->SetPreferredHeight(kActivityIndicatorHeight);
+ activity_indicator->SetPreferredCornerRadii(absl::nullopt);
+ activity_indicator->SetValue(-1 /* infinite animation */);
activity_indicator->SetBackgroundColor(SK_ColorTRANSPARENT);
activity_indicator->SetPreferredSize(
gfx::Size(dialog_width, kActivityIndicatorHeight));
diff --git a/chrome/browser/ui/views/webid/account_selection_bubble_view.cc b/chrome/browser/ui/views/webid/account_selection_bubble_view.cc
index 309af5d..680311d 100644
--- a/chrome/browser/ui/views/webid/account_selection_bubble_view.cc
+++ b/chrome/browser/ui/views/webid/account_selection_bubble_view.cc
@@ -600,7 +600,8 @@
RemoveNonHeaderChildViews();
views::ProgressBar* const progress_bar =
- AddChildView(std::make_unique<views::ProgressBar>(kProgressBarHeight));
+ AddChildView(std::make_unique<views::ProgressBar>());
+ progress_bar->SetPreferredHeight(kProgressBarHeight);
// Use an infinite animation: SetValue(-1).
progress_bar->SetValue(-1);
progress_bar->SetBackgroundColor(SK_ColorLTGRAY);
diff --git a/chromecast/graphics/cast_views_test.cc b/chromecast/graphics/cast_views_test.cc
index 439c1677..127da22 100644
--- a/chromecast/graphics/cast_views_test.cc
+++ b/chromecast/graphics/cast_views_test.cc
@@ -18,7 +18,8 @@
std::make_unique<CastWindowManagerAura>(true /* enable input */);
gfx::Rect bounds = window_manager->GetRootWindow()->bounds();
- views::ProgressBar* progress_bar = new views::ProgressBar(bounds.height());
+ views::ProgressBar* progress_bar = new views::ProgressBar();
+ progress_bar->SetPreferredHeight(bounds.height());
progress_bar->SetValue(0.5);
// Create the window. We close the window by deleting it, so we take
diff --git a/components/media_message_center/media_controls_progress_view.cc b/components/media_message_center/media_controls_progress_view.cc
index 8b8134b..e6e4df3 100644
--- a/components/media_message_center/media_controls_progress_view.cc
+++ b/components/media_message_center/media_controls_progress_view.cc
@@ -44,9 +44,10 @@
is_modern_notification_ ? kModernProgressViewInsets : kProgressViewInsets,
kProgressBarAndTimeSpacing));
- progress_bar_ = AddChildView(std::make_unique<views::ProgressBar>(
- is_modern_notification_ ? kModernProgressBarHeight : kProgressBarHeight,
- false));
+ progress_bar_ = AddChildView(std::make_unique<views::ProgressBar>());
+ progress_bar_->SetPreferredHeight(
+ is_modern_notification_ ? kModernProgressBarHeight : kProgressBarHeight);
+ progress_bar_->SetPreferredCornerRadii(absl::nullopt);
// Font list for text views.
gfx::Font default_font;
diff --git a/ui/message_center/views/notification_view_base.cc b/ui/message_center/views/notification_view_base.cc
index 78ad026..cf00434 100644
--- a/ui/message_center/views/notification_view_base.cc
+++ b/ui/message_center/views/notification_view_base.cc
@@ -525,9 +525,9 @@
DCHECK(left_content_);
if (!progress_bar_view_) {
- auto progress_bar_view =
- std::make_unique<views::ProgressBar>(kProgressBarHeight,
- /* allow_round_corner */ false);
+ auto progress_bar_view = std::make_unique<views::ProgressBar>();
+ progress_bar_view->SetPreferredHeight(kProgressBarHeight);
+ progress_bar_view->SetPreferredCornerRadii(absl::nullopt);
progress_bar_view->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(kProgressBarTopPadding, 0, 0, 0)));
progress_bar_view_ = AddViewToLeftContent(std::move(progress_bar_view));
diff --git a/ui/views/bubble/bubble_frame_view.cc b/ui/views/bubble/bubble_frame_view.cc
index fdcde45..962b132 100644
--- a/ui/views/bubble/bubble_frame_view.cc
+++ b/ui/views/bubble/bubble_frame_view.cc
@@ -130,8 +130,9 @@
close->SetVisible(false);
close_ = AddChildView(std::move(close));
- auto progress_indicator = std::make_unique<ProgressBar>(
- kProgressIndicatorHeight, /*allow_round_corner=*/false);
+ auto progress_indicator = std::make_unique<ProgressBar>();
+ progress_indicator->SetPreferredHeight(kProgressIndicatorHeight);
+ progress_indicator->SetPreferredCornerRadii(absl::nullopt);
progress_indicator->SetBackgroundColor(SK_ColorTRANSPARENT);
progress_indicator->SetVisible(false);
progress_indicator->GetViewAccessibility().OverrideIsIgnored(true);
diff --git a/ui/views/controls/progress_bar.cc b/ui/views/controls/progress_bar.cc
index c90a740a..6e89b3b 100644
--- a/ui/views/controls/progress_bar.cc
+++ b/ui/views/controls/progress_bar.cc
@@ -12,7 +12,7 @@
#include "base/check_op.h"
#include "base/i18n/number_formatting.h"
#include "cc/paint/paint_flags.h"
-#include "third_party/skia/include/core/SkPath.h"
+#include "third_party/skia/include/core/SkRRect.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
@@ -29,27 +29,27 @@
namespace {
-// In DP, the amount to round the corners of the progress bar (both bg and
-// fg, aka slice).
-constexpr int kCornerRadius = 3;
-constexpr int kSmallCornerRadius = 1;
-
-// Adds a rectangle to the path. The corners will be rounded with regular corner
-// radius if the progress bar height is larger than the regular corner radius.
-// Otherwise the corners will be rounded with the small corner radius if there
-// is room for it.
-void AddPossiblyRoundRectToPath(const gfx::Rect& rectangle,
- bool allow_round_corner,
- SkPath* path) {
- if (!allow_round_corner || rectangle.height() < kSmallCornerRadius) {
+// Adds a rectangle to the path.
+void AddPossiblyRoundRectToPath(
+ const gfx::Rect& rectangle,
+ const gfx::RoundedCornersF& preferred_corner_radii,
+ SkPath* path) {
+ if (preferred_corner_radii.IsEmpty() || rectangle.height() == 0) {
path->addRect(gfx::RectToSkRect(rectangle));
- } else if (rectangle.height() < kCornerRadius) {
- path->addRoundRect(gfx::RectToSkRect(rectangle), kSmallCornerRadius,
- kSmallCornerRadius);
- } else {
- path->addRoundRect(gfx::RectToSkRect(rectangle), kCornerRadius,
- kCornerRadius);
+ return;
}
+ SkVector radii[4] = {{preferred_corner_radii.upper_left(),
+ preferred_corner_radii.upper_left()},
+ {preferred_corner_radii.upper_right(),
+ preferred_corner_radii.upper_right()},
+ {preferred_corner_radii.lower_right(),
+ preferred_corner_radii.lower_right()},
+ {preferred_corner_radii.lower_left(),
+ preferred_corner_radii.lower_left()}};
+
+ SkRRect rr;
+ rr.setRectRadii(gfx::RectToSkRect(rectangle), radii);
+ path->addRRect(rr);
}
int RoundToPercent(double fractional_value) {
@@ -58,9 +58,7 @@
} // namespace
-ProgressBar::ProgressBar(int preferred_height, bool allow_round_corner)
- : preferred_height_(preferred_height),
- allow_round_corner_(allow_round_corner) {
+ProgressBar::ProgressBar() {
SetFlipCanvasOnPaintForRTLUI(true);
SetAccessibilityProperties(ax::mojom::Role::kProgressIndicator);
}
@@ -101,8 +99,8 @@
// Draw background.
SkPath background_path;
- AddPossiblyRoundRectToPath(content_bounds, allow_round_corner_,
- &background_path);
+ gfx::RoundedCornersF rounded_corners = GetPreferredCornerRadii();
+ AddPossiblyRoundRectToPath(content_bounds, rounded_corners, &background_path);
cc::PaintFlags background_flags;
background_flags.setStyle(cc::PaintFlags::kFill_Style);
background_flags.setAntiAlias(true);
@@ -119,7 +117,7 @@
gfx::Rect slice_bounds = content_bounds;
slice_bounds.set_width(slice_width);
- AddPossiblyRoundRectToPath(slice_bounds, allow_round_corner_, &slice_path);
+ AddPossiblyRoundRectToPath(slice_bounds, rounded_corners, &slice_path);
cc::PaintFlags slice_flags;
slice_flags.setStyle(cc::PaintFlags::kFill_Style);
@@ -227,6 +225,41 @@
OnPropertyChanged(&background_color_id_, kPropertyEffectsPaint);
}
+int ProgressBar::GetPreferredHeight() const {
+ return preferred_height_;
+}
+
+void ProgressBar::SetPreferredHeight(const int preferred_height) {
+ if (preferred_height_ == preferred_height) {
+ return;
+ }
+ preferred_height_ = preferred_height;
+ OnPropertyChanged(&preferred_height_, kPropertyEffectsPreferredSizeChanged);
+}
+
+gfx::RoundedCornersF ProgressBar::GetPreferredCornerRadii() const {
+ if (!preferred_corner_radii_) {
+ return gfx::RoundedCornersF(0);
+ }
+ const float max_radius = GetContentsBounds().height();
+
+ // No corner should have a radius greater than the height of the bar.
+ return gfx::RoundedCornersF(
+ std::min(max_radius, preferred_corner_radii_->upper_left()),
+ std::min(max_radius, preferred_corner_radii_->upper_right()),
+ std::min(max_radius, preferred_corner_radii_->lower_right()),
+ std::min(max_radius, preferred_corner_radii_->lower_left()));
+}
+
+void ProgressBar::SetPreferredCornerRadii(
+ const absl::optional<gfx::RoundedCornersF> preferred_corner_radii) {
+ if (preferred_corner_radii_ == preferred_corner_radii) {
+ return;
+ }
+ preferred_corner_radii_ = preferred_corner_radii;
+ OnPropertyChanged(&preferred_corner_radii_, kPropertyEffectsPaint);
+}
+
void ProgressBar::AnimationProgressed(const gfx::Animation* animation) {
DCHECK_EQ(animation, indeterminate_bar_animation_.get());
DCHECK(IsIndeterminate());
@@ -250,8 +283,8 @@
// Draw background.
SkPath background_path;
- AddPossiblyRoundRectToPath(content_bounds, allow_round_corner_,
- &background_path);
+ gfx::RoundedCornersF rounded_corners = GetPreferredCornerRadii();
+ AddPossiblyRoundRectToPath(content_bounds, rounded_corners, &background_path);
cc::PaintFlags background_flags;
background_flags.setStyle(cc::PaintFlags::kFill_Style);
background_flags.setAntiAlias(true);
@@ -295,10 +328,10 @@
gfx::Rect slice_bounds = content_bounds;
slice_bounds.set_x(content_bounds.x() + bar1_start_x);
slice_bounds.set_width(bar1_end_x - bar1_start_x);
- AddPossiblyRoundRectToPath(slice_bounds, allow_round_corner_, &slice_path);
+ AddPossiblyRoundRectToPath(slice_bounds, rounded_corners, &slice_path);
slice_bounds.set_x(content_bounds.x() + bar2_start_x);
slice_bounds.set_width(bar2_end_x - bar2_start_x);
- AddPossiblyRoundRectToPath(slice_bounds, allow_round_corner_, &slice_path);
+ AddPossiblyRoundRectToPath(slice_bounds, rounded_corners, &slice_path);
cc::PaintFlags slice_flags;
slice_flags.setStyle(cc::PaintFlags::kFill_Style);
@@ -317,6 +350,9 @@
}
BEGIN_METADATA(ProgressBar, View)
+ADD_PROPERTY_METADATA(int, PreferredHeight)
+ADD_PROPERTY_METADATA(absl::optional<gfx::RoundedCornersF>,
+ PreferredCornerRadii)
ADD_PROPERTY_METADATA(SkColor, ForegroundColor, ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(SkColor, BackgroundColor, ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(absl::optional<ui::ColorId>, ForegroundColorId);
diff --git a/ui/views/controls/progress_bar.h b/ui/views/controls/progress_bar.h
index 1b5fddd..74d2c71 100644
--- a/ui/views/controls/progress_bar.h
+++ b/ui/views/controls/progress_bar.h
@@ -10,6 +10,7 @@
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/color/color_id.h"
#include "ui/gfx/animation/animation_delegate.h"
+#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/view.h"
namespace gfx {
@@ -23,10 +24,7 @@
public:
METADATA_HEADER(ProgressBar);
- // The preferred height parameter makes it easier to use a ProgressBar with
- // layout managers that size to preferred size.
- explicit ProgressBar(int preferred_height = 5,
- bool allow_round_corner = true);
+ ProgressBar();
ProgressBar(const ProgressBar&) = delete;
ProgressBar& operator=(const ProgressBar&) = delete;
@@ -60,6 +58,19 @@
absl::optional<ui::ColorId> GetBackgroundColorId() const;
void SetBackgroundColorId(absl::optional<ui::ColorId> color_id);
+ int GetPreferredHeight() const;
+ void SetPreferredHeight(const int preferred_height);
+
+ // Calculates the rounded corners of the view based on
+ // `preferred_corner_radii_`. If `preferred_corner_radii_` was not provided,
+ // empty corners will be returned . If any corner radius in
+ // `preferred_corner_radii_` is greater than the height of the bar, its value
+ // will be capped to the height of the bar.
+ gfx::RoundedCornersF GetPreferredCornerRadii() const;
+
+ void SetPreferredCornerRadii(
+ const absl::optional<gfx::RoundedCornersF> preferred_corner_radii);
+
protected:
int preferred_height() const { return preferred_height_; }
@@ -81,10 +92,15 @@
// Is the progress bar paused.
bool is_paused_ = false;
- // In DP, the preferred height of this progress bar.
- const int preferred_height_;
+ // In DP, the preferred height of this progress bar. This makes it easier to
+ // use a ProgressBar with layout managers that size to preferred size.
+ int preferred_height_ = 5;
- const bool allow_round_corner_;
+ // The radii to round the progress bar corners with. A value of
+ // `absl::nullopt` will produce a bar with no rounded corners, otherwise a
+ // default value of 3 on all corners will be used.
+ absl::optional<gfx::RoundedCornersF> preferred_corner_radii_ =
+ gfx::RoundedCornersF(3);
absl::optional<SkColor> foreground_color_;
absl::optional<ui::ColorId> foreground_color_id_;
diff --git a/ui/views/controls/progress_bar_unittest.cc b/ui/views/controls/progress_bar_unittest.cc
index 52d9ade..058edf8 100644
--- a/ui/views/controls/progress_bar_unittest.cc
+++ b/ui/views/controls/progress_bar_unittest.cc
@@ -15,9 +15,10 @@
#include "ui/events/test/event_generator.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"
+#include "ui/views/layout/flex_layout.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/test/views_test_base.h"
-#include "ui/views/widget/widget_utils.h"
+#include "ui/views/test/views_test_utils.h"
namespace views {
@@ -28,20 +29,27 @@
ViewsTestBase::SetUp();
widget_ = CreateTestWidget();
- widget_->SetContentsView(std::make_unique<ProgressBar>());
+ container_view_ = widget_->SetContentsView(std::make_unique<View>());
+ auto* layout =
+ container_view_->SetLayoutManager(std::make_unique<FlexLayout>());
+ layout->SetOrientation(views::LayoutOrientation::kVertical);
+ bar_ = container_view_->AddChildView(std::make_unique<ProgressBar>());
+ views::test::RunScheduledLayout(container_view_);
widget_->Show();
}
void TearDown() override {
+ container_view_ = nullptr;
+ bar_ = nullptr;
widget_.reset();
ViewsTestBase::TearDown();
}
- ProgressBar* bar() {
- return static_cast<ProgressBar*>(widget_->GetContentsView());
- }
+ ProgressBar* bar() { return bar_.get(); }
std::unique_ptr<Widget> widget_;
+ raw_ptr<views::View> container_view_;
+ raw_ptr<ProgressBar> bar_;
};
TEST_F(ProgressBarTest, AccessibleNodeData) {
@@ -120,4 +128,40 @@
EXPECT_EQ(absl::nullopt, bar()->GetBackgroundColorId());
}
+// Test that if no `preferred_corner_radii` are provided the default radius is
+// 3, and a value of `absl::nullopt` will not round the corners.
+TEST_F(ProgressBarTest, RoundCornerDefault) {
+ // The default bar should have a rounded corner radius of 3.
+ EXPECT_EQ(gfx::RoundedCornersF(3), bar()->GetPreferredCornerRadii());
+
+ // Setting `absl::nullopt` for the corner radius should make the bar have no
+ // rounded corners.
+ bar()->SetPreferredHeight(12);
+ bar()->SetPreferredCornerRadii(absl::nullopt);
+ views::test::RunScheduledLayout(container_view_);
+ EXPECT_EQ(gfx::RoundedCornersF(0), bar()->GetPreferredCornerRadii());
+ EXPECT_TRUE(bar()->GetPreferredCornerRadii().IsEmpty());
+}
+
+// Test that a value set for `preferred_corner_radii` is saved and can be
+// retrieved from `GetPreferredCornerRadii()`.
+TEST_F(ProgressBarTest, RoundCornerRetrieval) {
+ // Setting custom corners should result in them being saved.
+ bar()->SetPreferredHeight(12);
+ bar()->SetPreferredCornerRadii(gfx::RoundedCornersF(6));
+ views::test::RunScheduledLayout(container_view_);
+ EXPECT_EQ(gfx::RoundedCornersF(6), bar()->GetPreferredCornerRadii());
+}
+
+// Test that `GetPreferredCornerRadii()` will return no corner with a radius
+// greater than the height of the bar.
+TEST_F(ProgressBarTest, RoundCornerMax) {
+ // The max corner radius for a bar with a height of 12 should be 12.
+ bar()->SetPreferredHeight(12);
+ bar()->SetPreferredCornerRadii(gfx::RoundedCornersF(13, 14, 15, 16));
+ views::test::RunScheduledLayout(container_view_);
+ EXPECT_EQ(gfx::RoundedCornersF(12, 12, 12, 12),
+ bar()->GetPreferredCornerRadii());
+}
+
} // namespace views
diff --git a/ui/views/examples/progress_bar_example.cc b/ui/views/examples/progress_bar_example.cc
index 58ee858..726168a 100644
--- a/ui/views/examples/progress_bar_example.cc
+++ b/ui/views/examples/progress_bar_example.cc
@@ -61,7 +61,9 @@
container->AddChildView(
std::make_unique<Label>(GetStringUTF16(IDS_PROGRESS_LOADER_SHORT_LABEL)));
- container->AddChildView(std::make_unique<ProgressBar>(2))->SetValue(-1);
+ auto* short_bar = container->AddChildView(std::make_unique<ProgressBar>());
+ short_bar->SetValue(-1);
+ short_bar->SetPreferredHeight(2);
}
void ProgressBarExample::ButtonPressed(double step) {