Commit 2c208002 authored by Sammie Quon's avatar Sammie Quon Committed by Commit Bot

views: Add HighlightPathGenerator::RoundRectHighlightPathGenerator.

Part of an effort to describe highlight paths as a round rect struct.
The first user will be the cros toast button.

Test: manual
Bug: 1042303
Change-Id: I0d3a784f2421ea2feb8e3f5d4fc2e50bbcb20e55
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2038084Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Commit-Queue: Sammie Quon <sammiequon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#739207}
parent e66c244f
...@@ -24,9 +24,10 @@ ...@@ -24,9 +24,10 @@
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/insets.h" #include "ui/gfx/geometry/insets.h"
#include "ui/views/animation/ink_drop_mask.h" #include "ui/views/animation/ink_drop_highlight.h"
#include "ui/views/border.h" #include "ui/views/border.h"
#include "ui/views/controls/button/label_button.h" #include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h" #include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h" #include "ui/views/layout/box_layout.h"
#include "ui/views/view.h" #include "ui/views/view.h"
...@@ -130,15 +131,19 @@ class ToastOverlayButton : public views::LabelButton { ...@@ -130,15 +131,19 @@ class ToastOverlayButton : public views::LabelButton {
std::max((kToastHeight - GetPreferredSize().height()) / 2, 0); std::max((kToastHeight - GetPreferredSize().height()) / 2, 0);
SetBorder(views::CreateEmptyBorder( SetBorder(views::CreateEmptyBorder(
gfx::Insets(vertical_spacing, kToastHorizontalSpacing))); gfx::Insets(vertical_spacing, kToastHorizontalSpacing)));
views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
kToastCornerRounding);
} }
~ToastOverlayButton() override = default; ~ToastOverlayButton() override = default;
protected: protected:
// views::LabelButton: // views::LabelButton:
std::unique_ptr<views::InkDropMask> CreateInkDropMask() const override { std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight()
return std::make_unique<views::RoundRectInkDropMask>(size(), gfx::Insets(), const override {
kToastCornerRounding); return std::make_unique<views::InkDropHighlight>(
gfx::SizeF(GetLocalBounds().size()), GetInkDropBaseColor());
} }
private: private:
......
...@@ -133,9 +133,10 @@ class OverviewCloseButton : public views::ImageButton { ...@@ -133,9 +133,10 @@ class OverviewCloseButton : public views::ImageButton {
std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight() std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight()
const override { const override {
return std::make_unique<views::InkDropHighlight>( auto highlight = std::make_unique<views::InkDropHighlight>(
gfx::SizeF(GetLocalBounds().size()), gfx::SizeF(size()), kCloseButtonInkDropRippleHighlightColor);
kCloseButtonInkDropRippleHighlightColor); highlight->set_visible_opacity(1.f);
return highlight;
} }
}; };
......
...@@ -26,7 +26,7 @@ namespace views { ...@@ -26,7 +26,7 @@ namespace views {
namespace { namespace {
// The opacity of the highlight when it is not visible. // The opacity of the highlight when it is not visible.
const float kHiddenOpacity = 0.0f; constexpr float kHiddenOpacity = 0.0f;
} // namespace } // namespace
...@@ -70,7 +70,6 @@ InkDropHighlight::InkDropHighlight(const gfx::SizeF& size, ...@@ -70,7 +70,6 @@ InkDropHighlight::InkDropHighlight(const gfx::SizeF& size,
center_point, center_point,
std::unique_ptr<BasePaintedLayerDelegate>( std::unique_ptr<BasePaintedLayerDelegate>(
new RoundedRectangleLayerDelegate(color, size, corner_radius))) { new RoundedRectangleLayerDelegate(color, size, corner_radius))) {
visible_opacity_ = 0.128f;
layer_->SetOpacity(visible_opacity_); layer_->SetOpacity(visible_opacity_);
} }
...@@ -80,14 +79,12 @@ InkDropHighlight::InkDropHighlight(const gfx::Size& size, ...@@ -80,14 +79,12 @@ InkDropHighlight::InkDropHighlight(const gfx::Size& size,
SkColor color) SkColor color)
: InkDropHighlight(gfx::SizeF(size), corner_radius, center_point, color) {} : InkDropHighlight(gfx::SizeF(size), corner_radius, center_point, color) {}
InkDropHighlight::InkDropHighlight(const gfx::SizeF& size, SkColor color) InkDropHighlight::InkDropHighlight(const gfx::SizeF& size, SkColor base_color)
: visible_opacity_(1.f), : last_animation_initiated_was_fade_in_(false), observer_(nullptr) {
last_animation_initiated_was_fade_in_(false),
observer_(nullptr) {
size_ = explode_size_ = size; size_ = explode_size_ = size;
layer_ = std::make_unique<ui::Layer>(ui::LAYER_SOLID_COLOR); layer_ = std::make_unique<ui::Layer>(ui::LAYER_SOLID_COLOR);
layer_->SetColor(color); layer_->SetColor(base_color);
layer_->SetBounds(gfx::Rect(gfx::ToRoundedSize(size))); layer_->SetBounds(gfx::Rect(gfx::ToRoundedSize(size)));
layer_->SetVisible(false); layer_->SetVisible(false);
layer_->SetMasksToBounds(false); layer_->SetMasksToBounds(false);
......
...@@ -57,8 +57,10 @@ class VIEWS_EXPORT InkDropHighlight { ...@@ -57,8 +57,10 @@ class VIEWS_EXPORT InkDropHighlight {
SkColor color); SkColor color);
// Creates a highlight that is drawn with a solid color layer. It's shape will // Creates a highlight that is drawn with a solid color layer. It's shape will
// be determined by the mask or clip applied to the parent layer. // be determined by the mask or clip applied to the parent layer. Note that
InkDropHighlight(const gfx::SizeF& size, SkColor color); // this still uses the default highlight opacity. Users who supply a
// |base_color| with alpha will also want to call set_visible_opacity(1.f);.
InkDropHighlight(const gfx::SizeF& size, SkColor base_color);
virtual ~InkDropHighlight(); virtual ~InkDropHighlight();
...@@ -128,7 +130,7 @@ class VIEWS_EXPORT InkDropHighlight { ...@@ -128,7 +130,7 @@ class VIEWS_EXPORT InkDropHighlight {
gfx::PointF center_point_; gfx::PointF center_point_;
// The opacity for the fully visible state of the highlight. // The opacity for the fully visible state of the highlight.
float visible_opacity_; float visible_opacity_ = 0.128f;
// True if the last animation to be initiated was a FADE_IN, and false // True if the last animation to be initiated was a FADE_IN, and false
// otherwise. // otherwise.
......
...@@ -11,6 +11,12 @@ ...@@ -11,6 +11,12 @@
namespace views { namespace views {
HighlightPathGenerator::HighlightPathGenerator()
: HighlightPathGenerator(gfx::Insets()) {}
HighlightPathGenerator::HighlightPathGenerator(const gfx::Insets& insets)
: insets_(insets) {}
HighlightPathGenerator::~HighlightPathGenerator() = default; HighlightPathGenerator::~HighlightPathGenerator() = default;
// static // static
...@@ -41,10 +47,17 @@ SkPath HighlightPathGenerator::GetHighlightPath(const View* view) { ...@@ -41,10 +47,17 @@ SkPath HighlightPathGenerator::GetHighlightPath(const View* view) {
} }
base::Optional<HighlightPathGenerator::RoundRect> base::Optional<HighlightPathGenerator::RoundRect>
HighlightPathGenerator::GetRoundRect(const View* view) { HighlightPathGenerator::GetRoundRect(const gfx::RectF& rect) {
return base::nullopt; return base::nullopt;
} }
base::Optional<HighlightPathGenerator::RoundRect>
HighlightPathGenerator::GetRoundRect(const View* view) {
gfx::Rect bounds(view->GetLocalBounds());
bounds.Inset(insets_);
return GetRoundRect(gfx::RectF(bounds));
}
SkPath RectHighlightPathGenerator::GetHighlightPath(const View* view) { SkPath RectHighlightPathGenerator::GetHighlightPath(const View* view) {
return SkPath().addRect(gfx::RectToSkRect(view->GetLocalBounds())); return SkPath().addRect(gfx::RectToSkRect(view->GetLocalBounds()));
} }
...@@ -55,8 +68,8 @@ void InstallRectHighlightPathGenerator(View* view) { ...@@ -55,8 +68,8 @@ void InstallRectHighlightPathGenerator(View* view) {
} }
base::Optional<HighlightPathGenerator::RoundRect> base::Optional<HighlightPathGenerator::RoundRect>
CircleHighlightPathGenerator::GetRoundRect(const View* view) { CircleHighlightPathGenerator::GetRoundRect(const gfx::RectF& rect) {
gfx::RectF bounds(view->GetLocalBounds()); gfx::RectF bounds = rect;
const float corner_radius = std::min(bounds.width(), bounds.height()) / 2.f; const float corner_radius = std::min(bounds.width(), bounds.height()) / 2.f;
bounds.ClampToCenteredSize( bounds.ClampToCenteredSize(
gfx::SizeF(corner_radius * 2.f, corner_radius * 2.f)); gfx::SizeF(corner_radius * 2.f, corner_radius * 2.f));
...@@ -90,8 +103,8 @@ FixedSizeCircleHighlightPathGenerator::FixedSizeCircleHighlightPathGenerator( ...@@ -90,8 +103,8 @@ FixedSizeCircleHighlightPathGenerator::FixedSizeCircleHighlightPathGenerator(
: radius_(radius) {} : radius_(radius) {}
base::Optional<HighlightPathGenerator::RoundRect> base::Optional<HighlightPathGenerator::RoundRect>
FixedSizeCircleHighlightPathGenerator::GetRoundRect(const View* view) { FixedSizeCircleHighlightPathGenerator::GetRoundRect(const gfx::RectF& rect) {
gfx::RectF bounds(view->GetLocalBounds()); gfx::RectF bounds = rect;
bounds.ClampToCenteredSize(gfx::SizeF(radius_ * 2, radius_ * 2)); bounds.ClampToCenteredSize(gfx::SizeF(radius_ * 2, radius_ * 2));
HighlightPathGenerator::RoundRect round_rect; HighlightPathGenerator::RoundRect round_rect;
round_rect.bounds = bounds; round_rect.bounds = bounds;
...@@ -104,4 +117,25 @@ void InstallFixedSizeCircleHighlightPathGenerator(View* view, int radius) { ...@@ -104,4 +117,25 @@ void InstallFixedSizeCircleHighlightPathGenerator(View* view, int radius) {
view, std::make_unique<FixedSizeCircleHighlightPathGenerator>(radius)); view, std::make_unique<FixedSizeCircleHighlightPathGenerator>(radius));
} }
RoundRectHighlightPathGenerator::RoundRectHighlightPathGenerator(
const gfx::Insets& insets,
int corner_radius)
: HighlightPathGenerator(insets), corner_radius_(corner_radius) {}
base::Optional<HighlightPathGenerator::RoundRect>
RoundRectHighlightPathGenerator::GetRoundRect(const gfx::RectF& rect) {
HighlightPathGenerator::RoundRect round_rect;
round_rect.bounds = rect;
round_rect.corner_radius = corner_radius_;
return base::make_optional(round_rect);
}
void InstallRoundRectHighlightPathGenerator(View* view,
const gfx::Insets& insets,
int corner_radius) {
HighlightPathGenerator::Install(
view,
std::make_unique<RoundRectHighlightPathGenerator>(insets, corner_radius));
}
} // namespace views } // namespace views
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/optional.h" #include "base/optional.h"
#include "third_party/skia/include/core/SkPath.h" #include "third_party/skia/include/core/SkPath.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect_f.h" #include "ui/gfx/geometry/rect_f.h"
#include "ui/views/views_export.h" #include "ui/views/views_export.h"
...@@ -26,7 +27,10 @@ class VIEWS_EXPORT HighlightPathGenerator { ...@@ -26,7 +27,10 @@ class VIEWS_EXPORT HighlightPathGenerator {
float corner_radius; float corner_radius;
}; };
HighlightPathGenerator() = default; // TODO(sammiequon): Remove this constructor in favor of the one that takes
// |insets|.
HighlightPathGenerator();
explicit HighlightPathGenerator(const gfx::Insets& insets);
virtual ~HighlightPathGenerator(); virtual ~HighlightPathGenerator();
HighlightPathGenerator(const HighlightPathGenerator&) = delete; HighlightPathGenerator(const HighlightPathGenerator&) = delete;
...@@ -44,7 +48,11 @@ class VIEWS_EXPORT HighlightPathGenerator { ...@@ -44,7 +48,11 @@ class VIEWS_EXPORT HighlightPathGenerator {
// highlight. // highlight.
// TODO(sammiequon): Once |GetHighlightPath()| is deprecated, make this a pure // TODO(sammiequon): Once |GetHighlightPath()| is deprecated, make this a pure
// virtual function and make the return not optional. // virtual function and make the return not optional.
virtual base::Optional<RoundRect> GetRoundRect(const View* view); virtual base::Optional<RoundRect> GetRoundRect(const gfx::RectF& rect);
base::Optional<RoundRect> GetRoundRect(const View* view);
private:
const gfx::Insets insets_;
}; };
// Sets a rectangular highlight path. // Sets a rectangular highlight path.
...@@ -73,8 +81,7 @@ class VIEWS_EXPORT CircleHighlightPathGenerator ...@@ -73,8 +81,7 @@ class VIEWS_EXPORT CircleHighlightPathGenerator
delete; delete;
// HighlightPathGenerator: // HighlightPathGenerator:
base::Optional<HighlightPathGenerator::RoundRect> GetRoundRect( base::Optional<RoundRect> GetRoundRect(const gfx::RectF& rect) override;
const View* view) override;
}; };
void VIEWS_EXPORT InstallCircleHighlightPathGenerator(View* view); void VIEWS_EXPORT InstallCircleHighlightPathGenerator(View* view);
...@@ -94,6 +101,10 @@ class VIEWS_EXPORT PillHighlightPathGenerator : public HighlightPathGenerator { ...@@ -94,6 +101,10 @@ class VIEWS_EXPORT PillHighlightPathGenerator : public HighlightPathGenerator {
void VIEWS_EXPORT InstallPillHighlightPathGenerator(View* view); void VIEWS_EXPORT InstallPillHighlightPathGenerator(View* view);
// TODO(sammiequon): Investigate if we can make |radius| optional for
// FixedSizeCircleHighlightPathGenerator and RoundRectHighlightPathGenerator,
// and combine them with CircleHighlightPathGenerator and
// PillHighlightPathGenerator respectively.
// Sets a centered fixed-size circular highlight path. // Sets a centered fixed-size circular highlight path.
class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator
: public HighlightPathGenerator { : public HighlightPathGenerator {
...@@ -106,8 +117,7 @@ class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator ...@@ -106,8 +117,7 @@ class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator
const FixedSizeCircleHighlightPathGenerator&) = delete; const FixedSizeCircleHighlightPathGenerator&) = delete;
// HighlightPathGenerator: // HighlightPathGenerator:
base::Optional<HighlightPathGenerator::RoundRect> GetRoundRect( base::Optional<RoundRect> GetRoundRect(const gfx::RectF& rect) override;
const View* view) override;
private: private:
const int radius_; const int radius_;
...@@ -116,6 +126,29 @@ class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator ...@@ -116,6 +126,29 @@ class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator
void VIEWS_EXPORT InstallFixedSizeCircleHighlightPathGenerator(View* view, void VIEWS_EXPORT InstallFixedSizeCircleHighlightPathGenerator(View* view,
int radius); int radius);
// Sets a rounded rectangle highlight path with optional insets.
class VIEWS_EXPORT RoundRectHighlightPathGenerator
: public HighlightPathGenerator {
public:
RoundRectHighlightPathGenerator(const gfx::Insets& insets, int corner_radius);
RoundRectHighlightPathGenerator(const RoundRectHighlightPathGenerator&) =
delete;
RoundRectHighlightPathGenerator& operator=(
const RoundRectHighlightPathGenerator&) = delete;
// HighlightPathGenerator:
base::Optional<RoundRect> GetRoundRect(const gfx::RectF& rect) override;
private:
const int corner_radius_;
};
void VIEWS_EXPORT
InstallRoundRectHighlightPathGenerator(View* view,
const gfx::Insets& insets,
int corner_radius);
} // namespace views } // namespace views
#endif // UI_VIEWS_CONTROLS_HIGHLIGHT_PATH_GENERATOR_H_ #endif // UI_VIEWS_CONTROLS_HIGHLIGHT_PATH_GENERATOR_H_
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