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 @@
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.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/controls/button/label_button.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
......@@ -130,15 +131,19 @@ class ToastOverlayButton : public views::LabelButton {
std::max((kToastHeight - GetPreferredSize().height()) / 2, 0);
SetBorder(views::CreateEmptyBorder(
gfx::Insets(vertical_spacing, kToastHorizontalSpacing)));
views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
kToastCornerRounding);
}
~ToastOverlayButton() override = default;
protected:
// views::LabelButton:
std::unique_ptr<views::InkDropMask> CreateInkDropMask() const override {
return std::make_unique<views::RoundRectInkDropMask>(size(), gfx::Insets(),
kToastCornerRounding);
std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight()
const override {
return std::make_unique<views::InkDropHighlight>(
gfx::SizeF(GetLocalBounds().size()), GetInkDropBaseColor());
}
private:
......
......@@ -133,9 +133,10 @@ class OverviewCloseButton : public views::ImageButton {
std::unique_ptr<views::InkDropHighlight> CreateInkDropHighlight()
const override {
return std::make_unique<views::InkDropHighlight>(
gfx::SizeF(GetLocalBounds().size()),
kCloseButtonInkDropRippleHighlightColor);
auto highlight = std::make_unique<views::InkDropHighlight>(
gfx::SizeF(size()), kCloseButtonInkDropRippleHighlightColor);
highlight->set_visible_opacity(1.f);
return highlight;
}
};
......
......@@ -26,7 +26,7 @@ namespace views {
namespace {
// The opacity of the highlight when it is not visible.
const float kHiddenOpacity = 0.0f;
constexpr float kHiddenOpacity = 0.0f;
} // namespace
......@@ -70,7 +70,6 @@ InkDropHighlight::InkDropHighlight(const gfx::SizeF& size,
center_point,
std::unique_ptr<BasePaintedLayerDelegate>(
new RoundedRectangleLayerDelegate(color, size, corner_radius))) {
visible_opacity_ = 0.128f;
layer_->SetOpacity(visible_opacity_);
}
......@@ -80,14 +79,12 @@ InkDropHighlight::InkDropHighlight(const gfx::Size& size,
SkColor color)
: InkDropHighlight(gfx::SizeF(size), corner_radius, center_point, color) {}
InkDropHighlight::InkDropHighlight(const gfx::SizeF& size, SkColor color)
: visible_opacity_(1.f),
last_animation_initiated_was_fade_in_(false),
observer_(nullptr) {
InkDropHighlight::InkDropHighlight(const gfx::SizeF& size, SkColor base_color)
: last_animation_initiated_was_fade_in_(false), observer_(nullptr) {
size_ = explode_size_ = size;
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_->SetVisible(false);
layer_->SetMasksToBounds(false);
......
......@@ -57,8 +57,10 @@ class VIEWS_EXPORT InkDropHighlight {
SkColor color);
// 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.
InkDropHighlight(const gfx::SizeF& size, SkColor color);
// be determined by the mask or clip applied to the parent layer. Note that
// 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();
......@@ -128,7 +130,7 @@ class VIEWS_EXPORT InkDropHighlight {
gfx::PointF center_point_;
// 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
// otherwise.
......
......@@ -11,6 +11,12 @@
namespace views {
HighlightPathGenerator::HighlightPathGenerator()
: HighlightPathGenerator(gfx::Insets()) {}
HighlightPathGenerator::HighlightPathGenerator(const gfx::Insets& insets)
: insets_(insets) {}
HighlightPathGenerator::~HighlightPathGenerator() = default;
// static
......@@ -41,10 +47,17 @@ SkPath HighlightPathGenerator::GetHighlightPath(const View* view) {
}
base::Optional<HighlightPathGenerator::RoundRect>
HighlightPathGenerator::GetRoundRect(const View* view) {
HighlightPathGenerator::GetRoundRect(const gfx::RectF& rect) {
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) {
return SkPath().addRect(gfx::RectToSkRect(view->GetLocalBounds()));
}
......@@ -55,8 +68,8 @@ void InstallRectHighlightPathGenerator(View* view) {
}
base::Optional<HighlightPathGenerator::RoundRect>
CircleHighlightPathGenerator::GetRoundRect(const View* view) {
gfx::RectF bounds(view->GetLocalBounds());
CircleHighlightPathGenerator::GetRoundRect(const gfx::RectF& rect) {
gfx::RectF bounds = rect;
const float corner_radius = std::min(bounds.width(), bounds.height()) / 2.f;
bounds.ClampToCenteredSize(
gfx::SizeF(corner_radius * 2.f, corner_radius * 2.f));
......@@ -90,8 +103,8 @@ FixedSizeCircleHighlightPathGenerator::FixedSizeCircleHighlightPathGenerator(
: radius_(radius) {}
base::Optional<HighlightPathGenerator::RoundRect>
FixedSizeCircleHighlightPathGenerator::GetRoundRect(const View* view) {
gfx::RectF bounds(view->GetLocalBounds());
FixedSizeCircleHighlightPathGenerator::GetRoundRect(const gfx::RectF& rect) {
gfx::RectF bounds = rect;
bounds.ClampToCenteredSize(gfx::SizeF(radius_ * 2, radius_ * 2));
HighlightPathGenerator::RoundRect round_rect;
round_rect.bounds = bounds;
......@@ -104,4 +117,25 @@ void InstallFixedSizeCircleHighlightPathGenerator(View* view, int 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
......@@ -9,6 +9,7 @@
#include "base/optional.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/views/views_export.h"
......@@ -26,7 +27,10 @@ class VIEWS_EXPORT HighlightPathGenerator {
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();
HighlightPathGenerator(const HighlightPathGenerator&) = delete;
......@@ -44,7 +48,11 @@ class VIEWS_EXPORT HighlightPathGenerator {
// highlight.
// TODO(sammiequon): Once |GetHighlightPath()| is deprecated, make this a pure
// 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.
......@@ -73,8 +81,7 @@ class VIEWS_EXPORT CircleHighlightPathGenerator
delete;
// HighlightPathGenerator:
base::Optional<HighlightPathGenerator::RoundRect> GetRoundRect(
const View* view) override;
base::Optional<RoundRect> GetRoundRect(const gfx::RectF& rect) override;
};
void VIEWS_EXPORT InstallCircleHighlightPathGenerator(View* view);
......@@ -94,6 +101,10 @@ class VIEWS_EXPORT PillHighlightPathGenerator : public HighlightPathGenerator {
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.
class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator
: public HighlightPathGenerator {
......@@ -106,8 +117,7 @@ class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator
const FixedSizeCircleHighlightPathGenerator&) = delete;
// HighlightPathGenerator:
base::Optional<HighlightPathGenerator::RoundRect> GetRoundRect(
const View* view) override;
base::Optional<RoundRect> GetRoundRect(const gfx::RectF& rect) override;
private:
const int radius_;
......@@ -116,6 +126,29 @@ class VIEWS_EXPORT FixedSizeCircleHighlightPathGenerator
void VIEWS_EXPORT InstallFixedSizeCircleHighlightPathGenerator(View* view,
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
#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