Commit 63212d34 authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Chromium LUCI CQ

views: make ColorChooserView neither a WidgetDelegate nor a View.

This CL enacts the "class splitting refactor"
(//docs/ui/views/class_splitting.md) on ColorChooserView, breaking it
into:

* A controller class called ColorChooser, whose lifetime is fully
  controlled by the client, and which is safe to call at any point in
  the lifecycle of the matching Widget/Views/etc
* Two helper objects (instances of WidgetDelegate and View respectively)
  whose lifetimes are fully controlled by Views

This CL serves as a prototype for how the class splitting refactor looks
when applied to production code.

Instead, have it use only the public API on WidgetDelegate.

Bug: 1075649
Change-Id: I84655a9c204a9f5f88767ac792ce1abc846feda0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2622995
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatarAllen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846358}
parent 9e150f6e
...@@ -9,33 +9,30 @@ ...@@ -9,33 +9,30 @@
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "ui/views/color_chooser/color_chooser_view.h" #include "ui/views/color_chooser/color_chooser_view.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
ColorChooserAura::ColorChooserAura(content::WebContents* web_contents, ColorChooserAura::ColorChooserAura(content::WebContents* web_contents,
SkColor initial_color) SkColor initial_color)
: web_contents_(web_contents) { : web_contents_(web_contents) {
view_ = new views::ColorChooserView(this, initial_color); chooser_ = std::make_unique<views::ColorChooser>(this, initial_color);
widget_ = views::Widget::CreateWindowWithParent( chooser_widget_ = base::WrapUnique(views::Widget::CreateWindowWithParent(
view_, web_contents->GetTopLevelNativeWindow()); chooser_->MakeWidgetDelegate(), web_contents->GetTopLevelNativeWindow()));
widget_->Show(); chooser_widget_->Show();
} }
ColorChooserAura::~ColorChooserAura() = default;
void ColorChooserAura::OnColorChosen(SkColor color) { void ColorChooserAura::OnColorChosen(SkColor color) {
if (web_contents_) if (web_contents_)
web_contents_->DidChooseColorInColorChooser(color); web_contents_->DidChooseColorInColorChooser(color);
} }
void ColorChooserAura::OnColorChooserDialogClosed() { void ColorChooserAura::OnColorChooserDialogClosed() {
view_ = nullptr;
widget_ = nullptr;
DidEndColorChooser(); DidEndColorChooser();
} }
void ColorChooserAura::End() { void ColorChooserAura::End() {
if (widget_) { if (chooser_widget_) {
view_->set_listener(nullptr);
widget_->Close();
view_ = nullptr;
widget_ = nullptr;
// DidEndColorChooser will invoke Browser::DidEndColorChooser, which deletes // DidEndColorChooser will invoke Browser::DidEndColorChooser, which deletes
// this. Take care of the call order. // this. Take care of the call order.
DidEndColorChooser(); DidEndColorChooser();
...@@ -48,8 +45,7 @@ void ColorChooserAura::DidEndColorChooser() { ...@@ -48,8 +45,7 @@ void ColorChooserAura::DidEndColorChooser() {
} }
void ColorChooserAura::SetSelectedColor(SkColor color) { void ColorChooserAura::SetSelectedColor(SkColor color) {
if (view_) chooser_->OnColorChanged(color);
view_->OnColorChanged(color);
} }
// static // static
......
...@@ -9,14 +9,14 @@ ...@@ -9,14 +9,14 @@
#include "base/macros.h" #include "base/macros.h"
#include "content/public/browser/color_chooser.h" #include "content/public/browser/color_chooser.h"
#include "ui/views/color_chooser/color_chooser_listener.h" #include "ui/views/color_chooser/color_chooser_listener.h"
#include "ui/views/widget/unique_widget_ptr.h"
namespace content { namespace content {
class WebContents; class WebContents;
} }
namespace views { namespace views {
class ColorChooserView; class ColorChooser;
class Widget;
} }
// TODO(mukai): rename this as -Ash and move to c/b/ui/ash after Linux-aura // TODO(mukai): rename this as -Ash and move to c/b/ui/ash after Linux-aura
...@@ -29,6 +29,7 @@ class ColorChooserAura : public content::ColorChooser, ...@@ -29,6 +29,7 @@ class ColorChooserAura : public content::ColorChooser,
private: private:
ColorChooserAura(content::WebContents* web_contents, SkColor initial_color); ColorChooserAura(content::WebContents* web_contents, SkColor initial_color);
~ColorChooserAura() override;
// content::ColorChooser overrides: // content::ColorChooser overrides:
void End() override; void End() override;
...@@ -40,13 +41,8 @@ class ColorChooserAura : public content::ColorChooser, ...@@ -40,13 +41,8 @@ class ColorChooserAura : public content::ColorChooser,
void DidEndColorChooser(); void DidEndColorChooser();
// The actual view of the color chooser. No ownership because its parent std::unique_ptr<views::ColorChooser> chooser_;
// view will take care of its lifetime. views::UniqueWidgetPtr chooser_widget_;
views::ColorChooserView* view_;
// The widget for the color chooser. No ownership because it's released
// automatically when closed.
views::Widget* widget_;
// The web contents invoking the color chooser. No ownership because it will // The web contents invoking the color chooser. No ownership because it will
// outlive this class. // outlive this class.
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "ui/views/color_chooser/color_chooser_view.h" #include "ui/views/color_chooser/color_chooser_view.h"
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/widget_utils.h" #include "ui/views/widget/widget_utils.h"
namespace { namespace {
...@@ -42,11 +43,18 @@ class ColorChooserTest : public views::ViewsTestBase { ...@@ -42,11 +43,18 @@ class ColorChooserTest : public views::ViewsTestBase {
void SetUp() override { void SetUp() override {
ViewsTestBase::SetUp(); ViewsTestBase::SetUp();
chooser_ = chooser_ = std::make_unique<views::ColorChooser>(&listener_, SK_ColorGREEN);
std::make_unique<views::ColorChooserView>(&listener_, SK_ColorGREEN);
chooser_->SetBounds(0, 0, 400, 300); // Icky: we can't use our own WidgetDelegate for CreateTestWidget, but we
// want to follow the production code path here regardless, so we create our
// own delegate, pull the contents view out of it, and stick it into the
// test widget. In production Views would handle that step itself.
auto delegate = chooser_->MakeWidgetDelegate();
auto* view = delegate->TransferOwnershipOfContentsView();
view->SetBounds(0, 0, 400, 300);
widget_ = CreateTestWidget(views::Widget::InitParams::TYPE_WINDOW); widget_ = CreateTestWidget(views::Widget::InitParams::TYPE_WINDOW);
widget_->GetContentsView()->AddChildView(chooser()); widget_->GetContentsView()->AddChildView(std::move(view));
generator_ = std::make_unique<ui::test::EventGenerator>( generator_ = std::make_unique<ui::test::EventGenerator>(
views::GetRootWindow(widget_.get()), widget_->GetNativeWindow()); views::GetRootWindow(widget_.get()), widget_->GetNativeWindow());
generator_->set_assume_window_at_origin(false); generator_->set_assume_window_at_origin(false);
...@@ -58,7 +66,7 @@ class ColorChooserTest : public views::ViewsTestBase { ...@@ -58,7 +66,7 @@ class ColorChooserTest : public views::ViewsTestBase {
ViewsTestBase::TearDown(); ViewsTestBase::TearDown();
} }
views::ColorChooserView* chooser() { return chooser_.get(); } views::ColorChooser* chooser() { return chooser_.get(); }
ui::test::EventGenerator* generator() { return generator_.get(); } ui::test::EventGenerator* generator() { return generator_.get(); }
void ExpectExactHSV(float h, float s, float v) const { void ExpectExactHSV(float h, float s, float v) const {
...@@ -115,7 +123,7 @@ class ColorChooserTest : public views::ViewsTestBase { ...@@ -115,7 +123,7 @@ class ColorChooserTest : public views::ViewsTestBase {
private: private:
TestChooserListener listener_; TestChooserListener listener_;
std::unique_ptr<views::ColorChooserView> chooser_; std::unique_ptr<views::ColorChooser> chooser_;
std::unique_ptr<views::Widget> widget_; std::unique_ptr<views::Widget> widget_;
std::unique_ptr<ui::test::EventGenerator> generator_; std::unique_ptr<ui::test::EventGenerator> generator_;
}; };
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "ui/views/layout/box_layout.h" #include "ui/views/layout/box_layout.h"
#include "ui/views/layout/grid_layout.h" #include "ui/views/layout/grid_layout.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
namespace { namespace {
...@@ -124,13 +125,14 @@ void DrawGradientRect(const gfx::Rect& rect, ...@@ -124,13 +125,14 @@ void DrawGradientRect(const gfx::Rect& rect,
namespace views { namespace views {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// ColorChooserView::HueView // HueView
// //
// The class to choose the hue of the color. It draws a vertical bar and // The class to choose the hue of the color. It draws a vertical bar and
// the indicator for the currently selected hue. // the indicator for the currently selected hue.
class ColorChooserView::HueView : public LocatedEventHandlerView { class HueView : public LocatedEventHandlerView {
public: public:
explicit HueView(ColorChooserView* chooser_view); using HueChangedCallback = base::RepeatingCallback<void(SkScalar)>;
explicit HueView(const HueChangedCallback& changed_callback);
void OnHueChanged(SkScalar hue); void OnHueChanged(SkScalar hue);
...@@ -142,16 +144,16 @@ class ColorChooserView::HueView : public LocatedEventHandlerView { ...@@ -142,16 +144,16 @@ class ColorChooserView::HueView : public LocatedEventHandlerView {
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
void OnPaint(gfx::Canvas* canvas) override; void OnPaint(gfx::Canvas* canvas) override;
ColorChooserView* chooser_view_; HueChangedCallback changed_callback_;
int level_; int level_;
DISALLOW_COPY_AND_ASSIGN(HueView); DISALLOW_COPY_AND_ASSIGN(HueView);
}; };
ColorChooserView::HueView::HueView(ColorChooserView* chooser_view) HueView::HueView(const HueChangedCallback& changed_callback)
: chooser_view_(chooser_view), level_(0) {} : changed_callback_(changed_callback), level_(0) {}
void ColorChooserView::HueView::OnHueChanged(SkScalar hue) { void HueView::OnHueChanged(SkScalar hue) {
SkScalar height = SkIntToScalar(kSaturationValueSize - 1); SkScalar height = SkIntToScalar(kSaturationValueSize - 1);
SkScalar hue_max = SkIntToScalar(360); SkScalar hue_max = SkIntToScalar(360);
int level = (hue_max - hue) * height / hue_max; int level = (hue_max - hue) * height / hue_max;
...@@ -162,23 +164,22 @@ void ColorChooserView::HueView::OnHueChanged(SkScalar hue) { ...@@ -162,23 +164,22 @@ void ColorChooserView::HueView::OnHueChanged(SkScalar hue) {
} }
} }
void ColorChooserView::HueView::ProcessEventAtLocation( void HueView::ProcessEventAtLocation(const gfx::Point& point) {
const gfx::Point& point) {
level_ = level_ =
std::max(kBorderWidth, std::min(height() - 1 - kBorderWidth, point.y())); std::max(kBorderWidth, std::min(height() - 1 - kBorderWidth, point.y()));
int base_height = kSaturationValueSize - 1; int base_height = kSaturationValueSize - 1;
chooser_view_->OnHueChosen(360.f * (base_height - (level_ - kBorderWidth)) / changed_callback_.Run(360.f * (base_height - (level_ - kBorderWidth)) /
base_height); base_height);
SchedulePaint(); SchedulePaint();
} }
gfx::Size ColorChooserView::HueView::CalculatePreferredSize() const { gfx::Size HueView::CalculatePreferredSize() const {
// We put indicators on the both sides of the hue bar. // We put indicators on the both sides of the hue bar.
return gfx::Size(kHueBarWidth + kHueIndicatorSize * 2 + kBorderWidth * 2, return gfx::Size(kHueBarWidth + kHueIndicatorSize * 2 + kBorderWidth * 2,
kSaturationValueSize + kBorderWidth * 2); kSaturationValueSize + kBorderWidth * 2);
} }
void ColorChooserView::HueView::OnPaint(gfx::Canvas* canvas) { void HueView::OnPaint(gfx::Canvas* canvas) {
SkScalar hsv[3]; SkScalar hsv[3];
// In the hue bar, saturation and value for the color should be always 100%. // In the hue bar, saturation and value for the color should be always 100%.
hsv[1] = SK_Scalar1; hsv[1] = SK_Scalar1;
...@@ -223,14 +224,17 @@ void ColorChooserView::HueView::OnPaint(gfx::Canvas* canvas) { ...@@ -223,14 +224,17 @@ void ColorChooserView::HueView::OnPaint(gfx::Canvas* canvas) {
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// ColorChooserView::SaturationValueView // SaturationValueView
// //
// The class to choose the saturation and the value of the color. It draws // The class to choose the saturation and the value of the color. It draws
// a square area and the indicator for the currently selected saturation and // a square area and the indicator for the currently selected saturation and
// value. // value.
class ColorChooserView::SaturationValueView : public LocatedEventHandlerView { class SaturationValueView : public LocatedEventHandlerView {
public: public:
explicit SaturationValueView(ColorChooserView* chooser_view); using SaturationValueChangedCallback =
base::RepeatingCallback<void(SkScalar, SkScalar)>;
explicit SaturationValueView(
const SaturationValueChangedCallback& changed_callback);
void OnHueChanged(SkScalar hue); void OnHueChanged(SkScalar hue);
void OnSaturationValueChanged(SkScalar saturation, SkScalar value); void OnSaturationValueChanged(SkScalar saturation, SkScalar value);
...@@ -243,29 +247,28 @@ class ColorChooserView::SaturationValueView : public LocatedEventHandlerView { ...@@ -243,29 +247,28 @@ class ColorChooserView::SaturationValueView : public LocatedEventHandlerView {
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
void OnPaint(gfx::Canvas* canvas) override; void OnPaint(gfx::Canvas* canvas) override;
ColorChooserView* chooser_view_; SaturationValueChangedCallback changed_callback_;
SkScalar hue_; SkScalar hue_;
gfx::Point marker_position_; gfx::Point marker_position_;
DISALLOW_COPY_AND_ASSIGN(SaturationValueView); DISALLOW_COPY_AND_ASSIGN(SaturationValueView);
}; };
ColorChooserView::SaturationValueView::SaturationValueView( SaturationValueView::SaturationValueView(
ColorChooserView* chooser_view) const SaturationValueChangedCallback& changed_callback)
: chooser_view_(chooser_view), hue_(0) { : changed_callback_(changed_callback), hue_(0) {
SetBorder(CreateSolidBorder(kBorderWidth, SK_ColorGRAY)); SetBorder(CreateSolidBorder(kBorderWidth, SK_ColorGRAY));
} }
void ColorChooserView::SaturationValueView::OnHueChanged(SkScalar hue) { void SaturationValueView::OnHueChanged(SkScalar hue) {
if (hue_ != hue) { if (hue_ != hue) {
hue_ = hue; hue_ = hue;
SchedulePaint(); SchedulePaint();
} }
} }
void ColorChooserView::SaturationValueView::OnSaturationValueChanged( void SaturationValueView::OnSaturationValueChanged(SkScalar saturation,
SkScalar saturation, SkScalar value) {
SkScalar value) {
SkScalar scalar_size = SkIntToScalar(kSaturationValueSize - 1); SkScalar scalar_size = SkIntToScalar(kSaturationValueSize - 1);
int x = SkScalarFloorToInt(saturation * scalar_size) + kBorderWidth; int x = SkScalarFloorToInt(saturation * scalar_size) + kBorderWidth;
int y = SkScalarFloorToInt((SK_Scalar1 - value) * scalar_size) + kBorderWidth; int y = SkScalarFloorToInt((SK_Scalar1 - value) * scalar_size) + kBorderWidth;
...@@ -277,24 +280,22 @@ void ColorChooserView::SaturationValueView::OnSaturationValueChanged( ...@@ -277,24 +280,22 @@ void ColorChooserView::SaturationValueView::OnSaturationValueChanged(
SchedulePaint(); SchedulePaint();
} }
void ColorChooserView::SaturationValueView::ProcessEventAtLocation( void SaturationValueView::ProcessEventAtLocation(const gfx::Point& point) {
const gfx::Point& point) {
SkScalar scalar_size = SkIntToScalar(kSaturationValueSize - 1); SkScalar scalar_size = SkIntToScalar(kSaturationValueSize - 1);
SkScalar saturation = (point.x() - kBorderWidth) / scalar_size; SkScalar saturation = (point.x() - kBorderWidth) / scalar_size;
SkScalar value = SK_Scalar1 - (point.y() - kBorderWidth) / scalar_size; SkScalar value = SK_Scalar1 - (point.y() - kBorderWidth) / scalar_size;
saturation = base::ClampToRange(saturation, 0.0f, SK_Scalar1); saturation = base::ClampToRange(saturation, 0.0f, SK_Scalar1);
value = base::ClampToRange(value, 0.0f, SK_Scalar1); value = base::ClampToRange(value, 0.0f, SK_Scalar1);
OnSaturationValueChanged(saturation, value); OnSaturationValueChanged(saturation, value);
chooser_view_->OnSaturationValueChosen(saturation, value); changed_callback_.Run(saturation, value);
} }
gfx::Size ColorChooserView::SaturationValueView::CalculatePreferredSize() gfx::Size SaturationValueView::CalculatePreferredSize() const {
const {
return gfx::Size(kSaturationValueSize + kBorderWidth * 2, return gfx::Size(kSaturationValueSize + kBorderWidth * 2,
kSaturationValueSize + kBorderWidth * 2); kSaturationValueSize + kBorderWidth * 2);
} }
void ColorChooserView::SaturationValueView::OnPaint(gfx::Canvas* canvas) { void SaturationValueView::OnPaint(gfx::Canvas* canvas) {
gfx::Rect color_bounds = bounds(); gfx::Rect color_bounds = bounds();
color_bounds.Inset(GetInsets()); color_bounds.Inset(GetInsets());
...@@ -329,10 +330,10 @@ void ColorChooserView::SaturationValueView::OnPaint(gfx::Canvas* canvas) { ...@@ -329,10 +330,10 @@ void ColorChooserView::SaturationValueView::OnPaint(gfx::Canvas* canvas) {
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// ColorChooserView::SelectedColorPatchView // SelectedColorPatchView
// //
// A view to simply show the selected color in a rectangle. // A view to simply show the selected color in a rectangle.
class ColorChooserView::SelectedColorPatchView : public views::View { class SelectedColorPatchView : public views::View {
public: public:
SelectedColorPatchView(); SelectedColorPatchView();
...@@ -342,12 +343,12 @@ class ColorChooserView::SelectedColorPatchView : public views::View { ...@@ -342,12 +343,12 @@ class ColorChooserView::SelectedColorPatchView : public views::View {
DISALLOW_COPY_AND_ASSIGN(SelectedColorPatchView); DISALLOW_COPY_AND_ASSIGN(SelectedColorPatchView);
}; };
ColorChooserView::SelectedColorPatchView::SelectedColorPatchView() { SelectedColorPatchView::SelectedColorPatchView() {
SetVisible(true); SetVisible(true);
SetBorder(CreateSolidBorder(kBorderWidth, SK_ColorGRAY)); SetBorder(CreateSolidBorder(kBorderWidth, SK_ColorGRAY));
} }
void ColorChooserView::SelectedColorPatchView::SetColor(SkColor color) { void SelectedColorPatchView::SetColor(SkColor color) {
if (!background()) if (!background())
SetBackground(CreateSolidBackground(color)); SetBackground(CreateSolidBackground(color));
else else
...@@ -355,29 +356,23 @@ void ColorChooserView::SelectedColorPatchView::SetColor(SkColor color) { ...@@ -355,29 +356,23 @@ void ColorChooserView::SelectedColorPatchView::SetColor(SkColor color) {
SchedulePaint(); SchedulePaint();
} }
//////////////////////////////////////////////////////////////////////////////// std::unique_ptr<View> ColorChooser::BuildView() {
// ColorChooserView auto view = std::make_unique<View>();
// tracker_.SetView(view.get());
view->SetBackground(CreateSolidBackground(SK_ColorLTGRAY));
ColorChooserView::ColorChooserView(ColorChooserListener* listener, view->SetLayoutManager(
SkColor initial_color)
: listener_(listener) {
DCHECK(listener_);
SetModalType(ui::MODAL_TYPE_WINDOW);
SetBackground(CreateSolidBackground(SK_ColorLTGRAY));
SetLayoutManager(
std::make_unique<BoxLayout>(BoxLayout::Orientation::kVertical, std::make_unique<BoxLayout>(BoxLayout::Orientation::kVertical,
gfx::Insets(kMarginWidth), kMarginWidth)); gfx::Insets(kMarginWidth), kMarginWidth));
auto container = std::make_unique<View>(); auto container = std::make_unique<View>();
container->SetLayoutManager(std::make_unique<BoxLayout>( container->SetLayoutManager(std::make_unique<BoxLayout>(
BoxLayout::Orientation::kHorizontal, gfx::Insets(), kMarginWidth)); BoxLayout::Orientation::kHorizontal, gfx::Insets(), kMarginWidth));
saturation_value_ = saturation_value_ = container->AddChildView(
container->AddChildView(std::make_unique<SaturationValueView>(this)); std::make_unique<SaturationValueView>(base::BindRepeating(
hue_ = container->AddChildView(std::make_unique<HueView>(this)); &ColorChooser::OnSaturationValueChosen, this->AsWeakPtr())));
AddChildView(std::move(container)); hue_ = container->AddChildView(std::make_unique<HueView>(
base::BindRepeating(&ColorChooser::OnHueChosen, this->AsWeakPtr())));
view->AddChildView(std::move(container));
auto container2 = std::make_unique<View>(); auto container2 = std::make_unique<View>();
GridLayout* layout = GridLayout* layout =
...@@ -397,95 +392,128 @@ ColorChooserView::ColorChooserView(ColorChooserListener* listener, ...@@ -397,95 +392,128 @@ ColorChooserView::ColorChooserView(ColorChooserListener* listener,
textfield_ = layout->AddView(std::move(textfield)); textfield_ = layout->AddView(std::move(textfield));
selected_color_patch_ = selected_color_patch_ =
layout->AddView(std::make_unique<SelectedColorPatchView>()); layout->AddView(std::make_unique<SelectedColorPatchView>());
AddChildView(std::move(container2)); view->AddChildView(std::move(container2));
OnColorChanged(initial_color_);
OnColorChanged(initial_color); return view;
} }
ColorChooserView::~ColorChooserView() = default; bool ColorChooser::IsViewAttached() const {
return tracker_.view();
}
void ColorChooserView::OnColorChanged(SkColor color) { void ColorChooser::OnColorChanged(SkColor color) {
SkColorToHSV(color, hsv_); SetColor(color);
hue_->OnHueChanged(hsv_[0]); if (IsViewAttached()) {
saturation_value_->OnHueChanged(hsv_[0]); hue_->OnHueChanged(hue());
saturation_value_->OnSaturationValueChanged(hsv_[1], hsv_[2]); saturation_value_->OnHueChanged(hue());
selected_color_patch_->SetColor(color); saturation_value_->OnSaturationValueChanged(saturation(), value());
textfield_->SetText(GetColorText(color)); selected_color_patch_->SetColor(color);
textfield_->SetText(GetColorText(color));
}
} }
void ColorChooserView::OnHueChosen(SkScalar hue) { void ColorChooser::OnHueChosen(SkScalar hue) {
hsv_[0] = hue; SetHue(hue);
SkColor color = SkHSVToColor(255, hsv_); SkColor color = GetColor();
if (listener_)
listener_->OnColorChosen(color);
saturation_value_->OnHueChanged(hue); saturation_value_->OnHueChanged(hue);
selected_color_patch_->SetColor(color); selected_color_patch_->SetColor(color);
textfield_->SetText(GetColorText(color)); textfield_->SetText(GetColorText(color));
} }
void ColorChooserView::OnSaturationValueChosen(SkScalar saturation, void ColorChooser::OnSaturationValueChosen(SkScalar saturation,
SkScalar value) { SkScalar value) {
hsv_[1] = saturation; SetSaturationValue(saturation, value);
hsv_[2] = value; SkColor color = GetColor();
SkColor color = SkHSVToColor(255, hsv_);
if (listener_)
listener_->OnColorChosen(color);
selected_color_patch_->SetColor(color); selected_color_patch_->SetColor(color);
textfield_->SetText(GetColorText(color)); textfield_->SetText(GetColorText(color));
} }
View* ColorChooserView::hue_view_for_testing() { View* ColorChooser::hue_view_for_testing() {
return hue_; return hue_;
} }
View* ColorChooserView::saturation_value_view_for_testing() { View* ColorChooser::saturation_value_view_for_testing() {
return saturation_value_; return saturation_value_;
} }
Textfield* ColorChooserView::textfield_for_testing() { Textfield* ColorChooser::textfield_for_testing() {
return textfield_; return textfield_;
} }
View* ColorChooserView::selected_color_patch_for_testing() { View* ColorChooser::selected_color_patch_for_testing() {
return selected_color_patch_; return selected_color_patch_;
} }
void ColorChooserView::ContentsChanged(Textfield* sender, void ColorChooser::ContentsChanged(Textfield* sender,
const base::string16& new_contents) { const base::string16& new_contents) {
DCHECK(IsViewAttached());
SkColor color = SK_ColorBLACK; SkColor color = SK_ColorBLACK;
if (GetColorFromText(new_contents, &color)) { if (GetColorFromText(new_contents, &color)) {
SkColorToHSV(color, hsv_); SetColor(color);
if (listener_) hue_->OnHueChanged(hue());
listener_->OnColorChosen(color); saturation_value_->OnHueChanged(hue());
hue_->OnHueChanged(hsv_[0]); saturation_value_->OnSaturationValueChanged(saturation(), value());
saturation_value_->OnHueChanged(hsv_[0]);
saturation_value_->OnSaturationValueChanged(hsv_[1], hsv_[2]);
selected_color_patch_->SetColor(color); selected_color_patch_->SetColor(color);
} }
} }
bool ColorChooserView::HandleKeyEvent(Textfield* sender, bool ColorChooser::HandleKeyEvent(Textfield* sender,
const ui::KeyEvent& key_event) { const ui::KeyEvent& key_event) {
DCHECK(IsViewAttached());
if (key_event.type() != ui::ET_KEY_PRESSED || if (key_event.type() != ui::ET_KEY_PRESSED ||
(key_event.key_code() != ui::VKEY_RETURN && (key_event.key_code() != ui::VKEY_RETURN &&
key_event.key_code() != ui::VKEY_ESCAPE)) key_event.key_code() != ui::VKEY_ESCAPE))
return false; return false;
GetWidget()->Close(); tracker_.view()->GetWidget()->Close();
return true; return true;
} }
bool ColorChooserView::CanMinimize() const { std::unique_ptr<WidgetDelegate> ColorChooser::MakeWidgetDelegate() {
return false; DCHECK(!IsViewAttached());
auto delegate = std::make_unique<WidgetDelegate>();
delegate->SetCanMinimize(false);
delegate->SetContentsView(BuildView());
delegate->SetInitiallyFocusedView(textfield_);
delegate->SetModalType(ui::MODAL_TYPE_WINDOW);
delegate->RegisterWindowClosingCallback(
base::BindOnce(&ColorChooser::OnViewClosing, base::Unretained(this)));
return delegate;
} }
View* ColorChooserView::GetInitiallyFocusedView() { ColorChooser::ColorChooser(ColorChooserListener* listener, SkColor initial)
return textfield_; : listener_(listener), initial_color_(initial) {}
ColorChooser::~ColorChooser() = default;
void ColorChooser::SetColor(SkColor color) {
SkColorToHSV(color, hsv_);
listener_->OnColorChosen(GetColor());
}
void ColorChooser::SetHue(SkScalar hue) {
hsv_[0] = hue;
listener_->OnColorChosen(GetColor());
}
void ColorChooser::SetSaturationValue(SkScalar saturation, SkScalar value) {
hsv_[1] = saturation;
hsv_[2] = value;
listener_->OnColorChosen(GetColor());
}
SkColor ColorChooser::GetColor() const {
return SkHSVToColor(255, hsv_);
} }
void ColorChooserView::WindowClosing() { void ColorChooser::OnViewClosing() {
if (listener_) listener_->OnColorChooserDialogClosed();
listener_->OnColorChooserDialogClosed();
} }
} // namespace views } // namespace views
...@@ -5,86 +5,102 @@ ...@@ -5,86 +5,102 @@
#ifndef UI_VIEWS_COLOR_CHOOSER_COLOR_CHOOSER_VIEW_H_ #ifndef UI_VIEWS_COLOR_CHOOSER_COLOR_CHOOSER_VIEW_H_
#define UI_VIEWS_COLOR_CHOOSER_COLOR_CHOOSER_VIEW_H_ #define UI_VIEWS_COLOR_CHOOSER_COLOR_CHOOSER_VIEW_H_
#include <memory>
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkScalar.h" #include "third_party/skia/include/core/SkScalar.h"
#include "ui/views/controls/textfield/textfield_controller.h" #include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/view.h"
#include "ui/views/view_tracker.h"
#include "ui/views/views_export.h" #include "ui/views/views_export.h"
#include "ui/views/widget/widget_delegate.h"
namespace views { namespace views {
class ColorChooserListener; class ColorChooserListener;
class Textfield; class Textfield;
class WidgetDelegate;
class HueView;
class SaturationValueView;
class SelectedColorPatchView;
// ColorChooserView provides the UI to choose a color by mouse and/or keyboard. // ColorChooser provides the UI to choose a color by mouse and/or keyboard.
// It is typically used for <input type="color">. Currently the user can // It is typically used for <input type="color">. Currently the user can
// choose a color by dragging over the bar for hue and the area for saturation // choose a color by dragging over the bar for hue and the area for saturation
// and value. // and value.
class VIEWS_EXPORT ColorChooserView : public WidgetDelegateView, //
public TextfieldController { // All public methods on ColorChooser are safe to call before, during, or after
// the existence of the corresponding Widget/Views/etc.
class VIEWS_EXPORT ColorChooser : public TextfieldController,
public base::SupportsWeakPtr<ColorChooser> {
public: public:
ColorChooserView(ColorChooserListener* listener, SkColor initial_color); ColorChooser(ColorChooserListener* listener, SkColor initial_color);
~ColorChooserView() override; ~ColorChooser() override;
// Called when its color value is changed in the web contents.
void OnColorChanged(SkColor color);
// Called when the user chooses a hue from the UI. // Construct the WidgetDelegate that should be used to show the actual dialog
void OnHueChosen(SkScalar hue); // for this ColorChooser. It is only safe to call this once per ColorChooser
// instance.
std::unique_ptr<WidgetDelegate> MakeWidgetDelegate();
// Called when the user chooses saturation/value from the UI. SkColor GetColor() const;
void OnSaturationValueChosen(SkScalar saturation, SkScalar value); SkScalar hue() const { return hsv_[0]; }
SkScalar saturation() const { return hsv_[1]; }
SkScalar value() const { return hsv_[2]; }
float hue() const { return hsv_[0]; } bool IsViewAttached() const;
float saturation() const { return hsv_[1]; }
float value() const { return hsv_[2]; }
void set_listener(ColorChooserListener* listener) { listener_ = listener; }
View* hue_view_for_testing(); // Called when its color value is changed in the web contents.
View* saturation_value_view_for_testing(); void OnColorChanged(SkColor color);
Textfield* textfield_for_testing();
View* selected_color_patch_for_testing();
// TextfieldController overrides: // TextfieldController overrides, public for testing:
void ContentsChanged(Textfield* sender, void ContentsChanged(Textfield* sender,
const base::string16& new_contents) override; const base::string16& new_contents) override;
bool HandleKeyEvent(Textfield* sender, bool HandleKeyEvent(Textfield* sender,
const ui::KeyEvent& key_event) override; const ui::KeyEvent& key_event) override;
View* hue_view_for_testing();
View* saturation_value_view_for_testing();
Textfield* textfield_for_testing();
View* selected_color_patch_for_testing();
private: private:
class HueView; std::unique_ptr<View> BuildView();
class SaturationValueView;
class SelectedColorPatchView;
// WidgetDelegate overrides: void SetColor(SkColor color);
bool CanMinimize() const override; void SetHue(SkScalar hue);
View* GetInitiallyFocusedView() override; void SetSaturationValue(SkScalar saturation, SkScalar value);
void WindowClosing() override;
void OnViewClosing();
// Called when the user chooses a hue from the UI.
void OnHueChosen(SkScalar hue);
// Called when the user chooses saturation/value from the UI.
void OnSaturationValueChosen(SkScalar saturation, SkScalar value);
// The current color in HSV coordinate. // The current color in HSV coordinate.
SkScalar hsv_[3]; SkScalar hsv_[3];
// The pointer to the current color chooser for callbacks. It doesn't take
// ownership on |listener_| so the user of this class should take care of
// its lifetime. See chrome/browser/ui/browser.cc for example.
ColorChooserListener* listener_; ColorChooserListener* listener_;
ViewTracker tracker_;
// Child views. These are owned as part of the normal views hierarchy. // Child views. These are owned as part of the normal views hierarchy.
// The view of hue chooser. // The view of hue chooser.
HueView* hue_; HueView* hue_ = nullptr;
// The view of saturation/value choosing area. // The view of saturation/value choosing area.
SaturationValueView* saturation_value_; SaturationValueView* saturation_value_ = nullptr;
// The textfield to write the color explicitly.
Textfield* textfield_;
// The rectangle to denote the selected color. // The rectangle to denote the selected color.
SelectedColorPatchView* selected_color_patch_; SelectedColorPatchView* selected_color_patch_ = nullptr;
// The textfield to write the color explicitly.
Textfield* textfield_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(ColorChooserView); SkColor initial_color_;
}; };
} // namespace views } // namespace views
......
...@@ -21,6 +21,7 @@ class VIEWS_EXPORT ViewTracker : public ViewObserver { ...@@ -21,6 +21,7 @@ class VIEWS_EXPORT ViewTracker : public ViewObserver {
void SetView(View* view); void SetView(View* view);
View* view() { return view_; } View* view() { return view_; }
const View* view() const { return view_; }
// ViewObserver: // ViewObserver:
void OnViewIsDeleting(View* observed_view) override; void OnViewIsDeleting(View* observed_view) override;
......
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