refactor: move color_chooser out of chromium_src (#15091)

* chore: move color_chooser out of chromium_src

* fix: remove deleted source files

* fix: add build flag, patch and build instructions for ColorChooser
This commit is contained in:
Heilig Benedek 2018-10-12 01:53:46 +02:00 committed by Samuel Attard
parent 95696c9456
commit 5ef5d60f77
13 changed files with 127 additions and 423 deletions

View file

@ -1,69 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/color_chooser_aura.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "content/public/browser/web_contents.h"
#include "ui/views/color_chooser/color_chooser_view.h"
#include "ui/views/widget/widget.h"
ColorChooserAura::ColorChooserAura(content::WebContents* web_contents,
SkColor initial_color)
: web_contents_(web_contents) {
view_ = new views::ColorChooserView(this, initial_color);
widget_ = views::Widget::CreateWindowWithParent(
view_, web_contents->GetTopLevelNativeWindow());
widget_->Show();
}
void ColorChooserAura::OnColorChosen(SkColor color) {
if (web_contents_)
web_contents_->DidChooseColorInColorChooser(color);
}
void ColorChooserAura::OnColorChooserDialogClosed() {
view_ = NULL;
widget_ = NULL;
DidEndColorChooser();
}
void ColorChooserAura::End() {
if (widget_) {
view_->set_listener(NULL);
widget_->Close();
view_ = NULL;
widget_ = NULL;
// DidEndColorChooser will invoke Browser::DidEndColorChooser, which deletes
// this. Take care of the call order.
DidEndColorChooser();
}
}
void ColorChooserAura::DidEndColorChooser() {
if (web_contents_)
web_contents_->DidEndColorChooser();
}
void ColorChooserAura::SetSelectedColor(SkColor color) {
if (view_)
view_->OnColorChanged(color);
}
// static
ColorChooserAura* ColorChooserAura::Open(content::WebContents* web_contents,
SkColor initial_color) {
return new ColorChooserAura(web_contents, initial_color);
}
#if !defined(OS_WIN)
namespace chrome {
content::ColorChooser* ShowColorChooser(content::WebContents* web_contents,
SkColor initial_color) {
return ColorChooserAura::Open(web_contents, initial_color);
}
} // namespace chrome
#endif // OS_WIN

View file

@ -1,57 +0,0 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_VIEWS_COLOR_CHOOSER_AURA_H_
#define CHROME_BROWSER_UI_VIEWS_COLOR_CHOOSER_AURA_H_
#include "base/macros.h"
#include "content/public/browser/color_chooser.h"
#include "ui/views/color_chooser/color_chooser_listener.h"
namespace content {
class WebContents;
}
namespace views {
class ColorChooserView;
class Widget;
} // namespace views
// TODO(mukai): rename this as -Ash and move to c/b/ui/ash after Linux-aura
// switches to its native color chooser.
class ColorChooserAura : public content::ColorChooser,
public views::ColorChooserListener {
public:
static ColorChooserAura* Open(content::WebContents* web_contents,
SkColor initial_color);
private:
ColorChooserAura(content::WebContents* web_contents, SkColor initial_color);
// content::ColorChooser overrides:
void End() override;
void SetSelectedColor(SkColor color) override;
// views::ColorChooserListener overrides:
void OnColorChosen(SkColor color) override;
void OnColorChooserDialogClosed() override;
void DidEndColorChooser();
// The actual view of the color chooser. No ownership because its parent
// view will take care of its lifetime.
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
// outlive this class.
content::WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(ColorChooserAura);
};
#endif // CHROME_BROWSER_UI_VIEWS_COLOR_CHOOSER_AURA_H_

View file

@ -1,83 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/color_chooser_dialog.h"
#include <commdlg.h>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "content/public/browser/browser_thread.h"
#include "skia/ext/skia_utils_win.h"
#include "ui/views/color_chooser/color_chooser_listener.h"
#include "ui/views/win/hwnd_util.h"
using content::BrowserThread;
// static
COLORREF ColorChooserDialog::g_custom_colors[16];
ColorChooserDialog::ExecuteOpenParams::ExecuteOpenParams(SkColor color,
RunState run_state,
HWND owner)
: color(color), run_state(run_state), owner(owner) {}
ColorChooserDialog::ColorChooserDialog(views::ColorChooserListener* listener,
SkColor initial_color,
gfx::NativeWindow owning_window)
: listener_(listener) {
DCHECK(listener_);
CopyCustomColors(g_custom_colors, custom_colors_);
HWND owning_hwnd = views::HWNDForNativeWindow(owning_window);
ExecuteOpenParams execute_params(initial_color, BeginRun(owning_hwnd),
owning_hwnd);
execute_params.run_state.dialog_thread->task_runner()->PostTask(
FROM_HERE,
base::Bind(&ColorChooserDialog::ExecuteOpen, this, execute_params));
}
ColorChooserDialog::~ColorChooserDialog() {}
bool ColorChooserDialog::IsRunning(gfx::NativeWindow owning_window) const {
return listener_ &&
IsRunningDialogForOwner(views::HWNDForNativeWindow(owning_window));
}
void ColorChooserDialog::ListenerDestroyed() {
// Our associated listener has gone away, so we shouldn't call back to it if
// our worker thread returns after the listener is dead.
listener_ = NULL;
}
void ColorChooserDialog::ExecuteOpen(const ExecuteOpenParams& params) {
CHOOSECOLOR cc;
cc.lStructSize = sizeof(CHOOSECOLOR);
cc.hwndOwner = params.owner;
cc.rgbResult = skia::SkColorToCOLORREF(params.color);
cc.lpCustColors = custom_colors_;
cc.Flags = CC_ANYCOLOR | CC_FULLOPEN | CC_RGBINIT;
bool success = !!ChooseColor(&cc);
DisableOwner(cc.hwndOwner);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&ColorChooserDialog::DidCloseDialog, this, success,
skia::COLORREFToSkColor(cc.rgbResult), params.run_state));
}
void ColorChooserDialog::DidCloseDialog(bool chose_color,
SkColor color,
RunState run_state) {
EndRun(run_state);
CopyCustomColors(custom_colors_, g_custom_colors);
if (listener_) {
if (chose_color)
listener_->OnColorChosen(color);
listener_->OnColorChooserDialogClosed();
}
}
void ColorChooserDialog::CopyCustomColors(COLORREF* src, COLORREF* dst) {
memcpy(dst, src, sizeof(COLORREF) * arraysize(g_custom_colors));
}

View file

@ -1,75 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_VIEWS_COLOR_CHOOSER_DIALOG_H_
#define CHROME_BROWSER_UI_VIEWS_COLOR_CHOOSER_DIALOG_H_
#include "base/memory/ref_counted.h"
#include "chrome/browser/ui/views/color_chooser_dialog.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/shell_dialogs/base_shell_dialog.h"
#include "ui/shell_dialogs/base_shell_dialog_win.h"
namespace views {
class ColorChooserListener;
}
class ColorChooserDialog
: public base::RefCountedThreadSafe<ColorChooserDialog>,
public ui::BaseShellDialog,
public ui::BaseShellDialogImpl {
public:
ColorChooserDialog(views::ColorChooserListener* listener,
SkColor initial_color,
gfx::NativeWindow owning_window);
// BaseShellDialog:
bool IsRunning(gfx::NativeWindow owning_window) const override;
void ListenerDestroyed() override;
private:
~ColorChooserDialog() override;
friend class base::RefCountedThreadSafe<ColorChooserDialog>;
struct ExecuteOpenParams {
ExecuteOpenParams(SkColor color, RunState run_state, HWND owner);
SkColor color;
RunState run_state;
HWND owner;
};
// Called on the dialog thread to show the actual color chooser. This is
// shown modal to |params.owner|. Once it's closed, calls back to
// DidCloseDialog() on the UI thread.
void ExecuteOpen(const ExecuteOpenParams& params);
// Called on the UI thread when a color chooser is closed. |chose_color| is
// true if the user actually chose a color, in which case |color| is the
// chosen color. Calls back to the |listener_| (if applicable) to notify it
// of the results, and copies the modified array of |custom_colors_| back to
// |g_custom_colors| so future dialogs will see the changes.
void DidCloseDialog(bool chose_color, SkColor color, RunState run_state);
// Copies the array of colors in |src| to |dst|.
void CopyCustomColors(COLORREF*, COLORREF*);
// The user's custom colors. Kept process-wide so that they can be persisted
// from one dialog invocation to the next.
static COLORREF g_custom_colors[16];
// A copy of the custom colors for the current dialog to display and modify.
// This allows us to safely access the colors even if multiple windows are
// simultaneously showing color choosers (which would cause thread safety
// problems if we gave them direct handles to |g_custom_colors|).
COLORREF custom_colors_[16];
// The listener to notify when the user closes the dialog. This may be set to
// NULL before the color chooser is closed, signalling that the listener no
// longer cares about the outcome.
views::ColorChooserListener* listener_;
DISALLOW_COPY_AND_ASSIGN(ColorChooserDialog);
};
#endif // CHROME_BROWSER_UI_VIEWS_COLOR_CHOOSER_DIALOG_H_

View file

@ -1,106 +0,0 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <windows.h>
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/views/color_chooser_aura.h"
#include "chrome/browser/ui/views/color_chooser_dialog.h"
#include "content/public/browser/color_chooser.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "ui/aura/window.h"
#include "ui/views/color_chooser/color_chooser_listener.h"
class ColorChooserWin : public content::ColorChooser,
public views::ColorChooserListener {
public:
static ColorChooserWin* Open(content::WebContents* web_contents,
SkColor initial_color);
ColorChooserWin(content::WebContents* web_contents, SkColor initial_color);
~ColorChooserWin() override;
// content::ColorChooser overrides:
void End() override;
void SetSelectedColor(SkColor color) override {}
// views::ColorChooserListener overrides:
void OnColorChosen(SkColor color) override;
void OnColorChooserDialogClosed() override;
private:
static ColorChooserWin* current_color_chooser_;
// The web contents invoking the color chooser. No ownership. because it will
// outlive this class.
content::WebContents* web_contents_;
// The color chooser dialog which maintains the native color chooser UI.
scoped_refptr<ColorChooserDialog> color_chooser_dialog_;
};
ColorChooserWin* ColorChooserWin::current_color_chooser_ = NULL;
ColorChooserWin* ColorChooserWin::Open(content::WebContents* web_contents,
SkColor initial_color) {
if (current_color_chooser_)
return NULL;
current_color_chooser_ = new ColorChooserWin(web_contents, initial_color);
return current_color_chooser_;
}
ColorChooserWin::ColorChooserWin(content::WebContents* web_contents,
SkColor initial_color)
: web_contents_(web_contents) {
gfx::NativeWindow owning_window = web_contents->GetRenderViewHost()
->GetWidget()
->GetView()
->GetNativeView()
->GetToplevelWindow();
color_chooser_dialog_ =
new ColorChooserDialog(this, initial_color, owning_window);
}
ColorChooserWin::~ColorChooserWin() {
// Always call End() before destroying.
DCHECK(!color_chooser_dialog_);
}
void ColorChooserWin::End() {
// The ColorChooserDialog's listener is going away. Ideally we'd
// programmatically close the dialog at this point. Since that's impossible,
// we instead tell the dialog its listener is going away, so that the dialog
// doesn't try to communicate with a destroyed listener later. (We also tell
// the renderer the dialog is closed, since from the renderer's perspective
// it effectively is.)
OnColorChooserDialogClosed();
}
void ColorChooserWin::OnColorChosen(SkColor color) {
if (web_contents_)
web_contents_->DidChooseColorInColorChooser(color);
}
void ColorChooserWin::OnColorChooserDialogClosed() {
if (color_chooser_dialog_.get()) {
color_chooser_dialog_->ListenerDestroyed();
color_chooser_dialog_ = NULL;
}
DCHECK(current_color_chooser_ == this);
current_color_chooser_ = NULL;
if (web_contents_)
web_contents_->DidEndColorChooser();
}
namespace chrome {
content::ColorChooser* ShowColorChooser(content::WebContents* web_contents,
SkColor initial_color) {
return ColorChooserWin::Open(web_contents, initial_color);
}
} // namespace chrome