forked from mirrors/gecko-dev
Bug 960984 - Implement the list attribute for <input type=color>. r=emilio,geckoview-reviewers,m_kato
UI support on Windows and Linux. macOS and Android are not supported. Differential Revision: https://phabricator.services.mozilla.com/D163796
This commit is contained in:
parent
d3e3ee5bb1
commit
a93e3a2add
21 changed files with 176 additions and 45 deletions
|
|
@ -56,6 +56,8 @@
|
||||||
#include "nsMappedAttributes.h"
|
#include "nsMappedAttributes.h"
|
||||||
#include "nsIFormControl.h"
|
#include "nsIFormControl.h"
|
||||||
#include "mozilla/dom/Document.h"
|
#include "mozilla/dom/Document.h"
|
||||||
|
#include "mozilla/dom/HTMLDataListElement.h"
|
||||||
|
#include "mozilla/dom/HTMLOptionElement.h"
|
||||||
#include "nsIFormControlFrame.h"
|
#include "nsIFormControlFrame.h"
|
||||||
#include "nsITextControlFrame.h"
|
#include "nsITextControlFrame.h"
|
||||||
#include "nsIFrame.h"
|
#include "nsIFrame.h"
|
||||||
|
|
@ -687,6 +689,33 @@ static bool IsPopupBlocked(Document* aDoc) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nsTArray<nsString> HTMLInputElement::GetColorsFromList() {
|
||||||
|
RefPtr<HTMLDataListElement> dataList = GetList();
|
||||||
|
if (!dataList) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
nsTArray<nsString> colors;
|
||||||
|
|
||||||
|
RefPtr<nsContentList> options = dataList->Options();
|
||||||
|
uint32_t length = options->Length(true);
|
||||||
|
for (uint32_t i = 0; i < length; ++i) {
|
||||||
|
auto* option = HTMLOptionElement::FromNodeOrNull(options->Item(i, false));
|
||||||
|
if (!option) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
nsString value;
|
||||||
|
option->GetValue(value);
|
||||||
|
if (IsValidSimpleColor(value)) {
|
||||||
|
ToLowerCase(value);
|
||||||
|
colors.AppendElement(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return colors;
|
||||||
|
}
|
||||||
|
|
||||||
nsresult HTMLInputElement::InitColorPicker() {
|
nsresult HTMLInputElement::InitColorPicker() {
|
||||||
MOZ_ASSERT(IsMutable());
|
MOZ_ASSERT(IsMutable());
|
||||||
|
|
||||||
|
|
@ -719,7 +748,8 @@ nsresult HTMLInputElement::InitColorPicker() {
|
||||||
|
|
||||||
nsAutoString initialValue;
|
nsAutoString initialValue;
|
||||||
GetNonFileValueInternal(initialValue);
|
GetNonFileValueInternal(initialValue);
|
||||||
nsresult rv = colorPicker->Init(win, title, initialValue);
|
nsTArray<nsString> colors = GetColorsFromList();
|
||||||
|
nsresult rv = colorPicker->Init(win, title, initialValue, colors);
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
nsCOMPtr<nsIColorPickerShownCallback> callback =
|
nsCOMPtr<nsIColorPickerShownCallback> callback =
|
||||||
|
|
@ -4764,6 +4794,22 @@ void HTMLInputElement::SanitizeValue(nsAString& aValue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Maybe<nscolor> HTMLInputElement::ParseSimpleColor(const nsAString& aColor) {
|
||||||
|
// Input color string should be 7 length (i.e. a string representing a valid
|
||||||
|
// simple color)
|
||||||
|
if (aColor.Length() != 7 || aColor.First() != '#') {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const nsAString& withoutHash = StringTail(aColor, 6);
|
||||||
|
nscolor color;
|
||||||
|
if (!NS_HexToRGBA(withoutHash, nsHexColorType::NoAlpha, &color)) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some(color);
|
||||||
|
}
|
||||||
|
|
||||||
bool HTMLInputElement::IsValidSimpleColor(const nsAString& aValue) const {
|
bool HTMLInputElement::IsValidSimpleColor(const nsAString& aValue) const {
|
||||||
if (aValue.Length() != 7 || aValue.First() != '#') {
|
if (aValue.Length() != 7 || aValue.First() != '#') {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "mozilla/Attributes.h"
|
#include "mozilla/Attributes.h"
|
||||||
#include "mozilla/Decimal.h"
|
#include "mozilla/Decimal.h"
|
||||||
|
#include "mozilla/Maybe.h"
|
||||||
#include "mozilla/TextControlElement.h"
|
#include "mozilla/TextControlElement.h"
|
||||||
#include "mozilla/TextControlState.h"
|
#include "mozilla/TextControlState.h"
|
||||||
#include "mozilla/UniquePtr.h"
|
#include "mozilla/UniquePtr.h"
|
||||||
|
|
@ -858,6 +859,9 @@ class HTMLInputElement final : public TextControlElement,
|
||||||
*/
|
*/
|
||||||
bool IsValueEmpty() const;
|
bool IsValueEmpty() const;
|
||||||
|
|
||||||
|
// Parse a simple (hex) color.
|
||||||
|
static mozilla::Maybe<nscolor> ParseSimpleColor(const nsAString& aColor);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual ~HTMLInputElement();
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual ~HTMLInputElement();
|
||||||
|
|
||||||
|
|
@ -1384,6 +1388,11 @@ class HTMLInputElement final : public TextControlElement,
|
||||||
*/
|
*/
|
||||||
nsresult MaybeInitPickers(EventChainPostVisitor& aVisitor);
|
nsresult MaybeInitPickers(EventChainPostVisitor& aVisitor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all valid colors in the <datalist> for the input with type=color.
|
||||||
|
*/
|
||||||
|
nsTArray<nsString> GetColorsFromList();
|
||||||
|
|
||||||
enum FilePickerType { FILE_PICKER_FILE, FILE_PICKER_DIRECTORY };
|
enum FilePickerType { FILE_PICKER_FILE, FILE_PICKER_DIRECTORY };
|
||||||
nsresult InitFilePicker(FilePickerType aType);
|
nsresult InitFilePicker(FilePickerType aType);
|
||||||
nsresult InitColorPicker();
|
nsresult InitColorPicker();
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ support-files = file_double_submit.html
|
||||||
[test_formnovalidate_attribute.html]
|
[test_formnovalidate_attribute.html]
|
||||||
[test_input_attributes_reflection.html]
|
[test_input_attributes_reflection.html]
|
||||||
[test_input_color_input_change_events.html]
|
[test_input_color_input_change_events.html]
|
||||||
|
[test_input_color_picker_datalist.html]
|
||||||
[test_input_color_picker_initial.html]
|
[test_input_color_picker_initial.html]
|
||||||
[test_input_color_picker_popup.html]
|
[test_input_color_picker_popup.html]
|
||||||
[test_input_color_picker_update.html]
|
[test_input_color_picker_update.html]
|
||||||
|
|
|
||||||
42
dom/html/test/forms/test_input_color_picker_datalist.html
Normal file
42
dom/html/test/forms/test_input_color_picker_datalist.html
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<script src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||||
|
<script src="/tests/SimpleTest/EventUtils.js"></script>
|
||||||
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
|
||||||
|
<script>
|
||||||
|
SimpleTest.waitForExplicitFinish();
|
||||||
|
|
||||||
|
function runTest() {
|
||||||
|
let MockColorPicker = SpecialPowers.MockColorPicker;
|
||||||
|
|
||||||
|
MockColorPicker.init(window);
|
||||||
|
|
||||||
|
MockColorPicker.showCallback = (picker) => {
|
||||||
|
is(picker.defaultColors.length, 2);
|
||||||
|
is(picker.defaultColors[0], "#112233");
|
||||||
|
is(picker.defaultColors[1], "#00ffaa");
|
||||||
|
|
||||||
|
MockColorPicker.cleanup();
|
||||||
|
SimpleTest.finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
let input = document.querySelector("input");
|
||||||
|
synthesizeMouseAtCenter(input, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleTest.waitForFocus(runTest);
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<input type="color" list="color-list">
|
||||||
|
<datalist id="color-list">
|
||||||
|
<option value="#112233"></option>
|
||||||
|
<option value="black"></option> <!-- invalid -->
|
||||||
|
<option value="#000000" disabled></option>
|
||||||
|
<option value="#00FFAA"></option>
|
||||||
|
<option></option>
|
||||||
|
</datalist>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -2272,8 +2272,8 @@ bool BrowserChild::DeallocPDocAccessibleChild(
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
PColorPickerChild* BrowserChild::AllocPColorPickerChild(const nsAString&,
|
PColorPickerChild* BrowserChild::AllocPColorPickerChild(
|
||||||
const nsAString&) {
|
const nsAString&, const nsAString&, const nsTArray<nsString>&) {
|
||||||
MOZ_CRASH("unused");
|
MOZ_CRASH("unused");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -436,8 +436,9 @@ class BrowserChild final : public nsMessageManagerScriptExecutor,
|
||||||
bool DeallocPDocAccessibleChild(PDocAccessibleChild*);
|
bool DeallocPDocAccessibleChild(PDocAccessibleChild*);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
PColorPickerChild* AllocPColorPickerChild(const nsAString& aTitle,
|
PColorPickerChild* AllocPColorPickerChild(
|
||||||
const nsAString& aInitialColor);
|
const nsAString& aTitle, const nsAString& aInitialColor,
|
||||||
|
const nsTArray<nsString>& aDefaultColors);
|
||||||
|
|
||||||
bool DeallocPColorPickerChild(PColorPickerChild* aActor);
|
bool DeallocPColorPickerChild(PColorPickerChild* aActor);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3402,8 +3402,9 @@ BrowserParent::GetAuthPrompt(uint32_t aPromptReason, const nsIID& iid,
|
||||||
}
|
}
|
||||||
|
|
||||||
PColorPickerParent* BrowserParent::AllocPColorPickerParent(
|
PColorPickerParent* BrowserParent::AllocPColorPickerParent(
|
||||||
const nsString& aTitle, const nsString& aInitialColor) {
|
const nsString& aTitle, const nsString& aInitialColor,
|
||||||
return new ColorPickerParent(aTitle, aInitialColor);
|
const nsTArray<nsString>& aDefaultColors) {
|
||||||
|
return new ColorPickerParent(aTitle, aInitialColor, aDefaultColors);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BrowserParent::DeallocPColorPickerParent(PColorPickerParent* actor) {
|
bool BrowserParent::DeallocPColorPickerParent(PColorPickerParent* actor) {
|
||||||
|
|
|
||||||
|
|
@ -423,8 +423,9 @@ class BrowserParent final : public PBrowserParent,
|
||||||
const ScrollAxis& aHorizontal, const ScrollFlags& aScrollFlags,
|
const ScrollAxis& aHorizontal, const ScrollFlags& aScrollFlags,
|
||||||
const int32_t& aAppUnitsPerDevPixel);
|
const int32_t& aAppUnitsPerDevPixel);
|
||||||
|
|
||||||
PColorPickerParent* AllocPColorPickerParent(const nsString& aTitle,
|
PColorPickerParent* AllocPColorPickerParent(
|
||||||
const nsString& aInitialColor);
|
const nsString& aTitle, const nsString& aInitialColor,
|
||||||
|
const nsTArray<nsString>& aDefaultColors);
|
||||||
|
|
||||||
bool DeallocPColorPickerParent(PColorPickerParent* aColorPicker);
|
bool DeallocPColorPickerParent(PColorPickerParent* aColorPicker);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,8 @@ bool ColorPickerParent::CreateColorPicker() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NS_SUCCEEDED(mPicker->Init(window, mTitle, mInitialColor));
|
return NS_SUCCEEDED(
|
||||||
|
mPicker->Init(window, mTitle, mInitialColor, mDefaultColors));
|
||||||
}
|
}
|
||||||
|
|
||||||
mozilla::ipc::IPCResult ColorPickerParent::RecvOpen() {
|
mozilla::ipc::IPCResult ColorPickerParent::RecvOpen() {
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,11 @@ namespace mozilla::dom {
|
||||||
|
|
||||||
class ColorPickerParent : public PColorPickerParent {
|
class ColorPickerParent : public PColorPickerParent {
|
||||||
public:
|
public:
|
||||||
ColorPickerParent(const nsString& aTitle, const nsString& aInitialColor)
|
ColorPickerParent(const nsString& aTitle, const nsString& aInitialColor,
|
||||||
: mTitle(aTitle), mInitialColor(aInitialColor) {}
|
const nsTArray<nsString>& aDefaultColors)
|
||||||
|
: mTitle(aTitle),
|
||||||
|
mInitialColor(aInitialColor),
|
||||||
|
mDefaultColors(aDefaultColors.Clone()) {}
|
||||||
|
|
||||||
virtual mozilla::ipc::IPCResult RecvOpen() override;
|
virtual mozilla::ipc::IPCResult RecvOpen() override;
|
||||||
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
|
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
|
||||||
|
|
@ -45,6 +48,7 @@ class ColorPickerParent : public PColorPickerParent {
|
||||||
|
|
||||||
nsString mTitle;
|
nsString mTitle;
|
||||||
nsString mInitialColor;
|
nsString mInitialColor;
|
||||||
|
nsTArray<nsString> mDefaultColors;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mozilla::dom
|
} // namespace mozilla::dom
|
||||||
|
|
|
||||||
|
|
@ -436,7 +436,7 @@ parent:
|
||||||
* Create an asynchronous color picker on the parent side,
|
* Create an asynchronous color picker on the parent side,
|
||||||
* but don't open it yet.
|
* but don't open it yet.
|
||||||
*/
|
*/
|
||||||
async PColorPicker(nsString title, nsString initialColor);
|
async PColorPicker(nsString title, nsString initialColor, nsString[] defaultColors);
|
||||||
|
|
||||||
async PFilePicker(nsString aTitle, int16_t aMode);
|
async PFilePicker(nsString aTitle, int16_t aMode);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,8 @@ XPCOMUtils.defineLazyModuleGetters(lazy, {
|
||||||
const { debug, warn } = GeckoViewUtils.initLogging("ColorPickerDelegate");
|
const { debug, warn } = GeckoViewUtils.initLogging("ColorPickerDelegate");
|
||||||
|
|
||||||
class ColorPickerDelegate {
|
class ColorPickerDelegate {
|
||||||
init(aParent, aTitle, aInitialColor) {
|
// TODO(bug 1805397): Implement default colors
|
||||||
|
init(aParent, aTitle, aInitialColor, aDefaultColors) {
|
||||||
this._prompt = new lazy.GeckoViewPrompter(aParent);
|
this._prompt = new lazy.GeckoViewPrompter(aParent);
|
||||||
this._msg = {
|
this._msg = {
|
||||||
type: "color",
|
type: "color",
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,10 @@ function MockColorPickerInstance(window) {
|
||||||
}
|
}
|
||||||
MockColorPickerInstance.prototype = {
|
MockColorPickerInstance.prototype = {
|
||||||
QueryInterface: ChromeUtils.generateQI(["nsIColorPicker"]),
|
QueryInterface: ChromeUtils.generateQI(["nsIColorPicker"]),
|
||||||
init(aParent, aTitle, aInitialColor) {
|
init(aParent, aTitle, aInitialColor, aDefaultColors) {
|
||||||
this.parent = aParent;
|
this.parent = aParent;
|
||||||
this.initialColor = aInitialColor;
|
this.initialColor = aInitialColor;
|
||||||
|
this.defaultColors = aDefaultColors;
|
||||||
},
|
},
|
||||||
initialColor: "",
|
initialColor: "",
|
||||||
parent: null,
|
parent: null,
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,7 @@ class mozIDOMWindowProxy;
|
||||||
class nsColorPicker final : public nsIColorPicker {
|
class nsColorPicker final : public nsIColorPicker {
|
||||||
public:
|
public:
|
||||||
NS_DECL_ISUPPORTS
|
NS_DECL_ISUPPORTS
|
||||||
|
NS_DECL_NSICOLORPICKER
|
||||||
NS_IMETHOD Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
|
|
||||||
const nsAString& aInitialColor) override;
|
|
||||||
NS_IMETHOD Open(nsIColorPickerShownCallback* aCallback) override;
|
|
||||||
|
|
||||||
// For NSColorPanelWrapper.
|
// For NSColorPanelWrapper.
|
||||||
void Update(NSColor* aColor);
|
void Update(NSColor* aColor);
|
||||||
|
|
|
||||||
|
|
@ -93,9 +93,10 @@ nsColorPicker::~nsColorPicker() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(bug 1805397): Implement default colors
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsColorPicker::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
|
nsColorPicker::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
|
||||||
const nsAString& aInitialColor) {
|
const nsAString& aInitialColor, const nsTArray<nsString>& aDefaultColors) {
|
||||||
MOZ_ASSERT(NS_IsMainThread(), "Color pickers can only be opened from main thread currently");
|
MOZ_ASSERT(NS_IsMainThread(), "Color pickers can only be opened from main thread currently");
|
||||||
mTitle = aTitle;
|
mTitle = aTitle;
|
||||||
mColor = aInitialColor;
|
mColor = aInitialColor;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
|
#include "mozilla/Maybe.h"
|
||||||
|
#include "mozilla/dom/HTMLInputElement.h"
|
||||||
#include "nsColor.h"
|
#include "nsColor.h"
|
||||||
#include "nsColorPicker.h"
|
#include "nsColorPicker.h"
|
||||||
#include "nsGtkUtils.h"
|
#include "nsGtkUtils.h"
|
||||||
|
|
@ -12,6 +14,8 @@
|
||||||
#include "WidgetUtils.h"
|
#include "WidgetUtils.h"
|
||||||
#include "nsPIDOMWindow.h"
|
#include "nsPIDOMWindow.h"
|
||||||
|
|
||||||
|
using mozilla::dom::HTMLInputElement;
|
||||||
|
|
||||||
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
|
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
|
||||||
|
|
||||||
#if defined(ACTIVATE_GTK3_COLOR_PICKER)
|
#if defined(ACTIVATE_GTK3_COLOR_PICKER)
|
||||||
|
|
@ -59,28 +63,24 @@ GtkColorSelection* nsColorPicker::WidgetGetColorSelection(GtkWidget* widget) {
|
||||||
|
|
||||||
NS_IMETHODIMP nsColorPicker::Init(mozIDOMWindowProxy* aParent,
|
NS_IMETHODIMP nsColorPicker::Init(mozIDOMWindowProxy* aParent,
|
||||||
const nsAString& title,
|
const nsAString& title,
|
||||||
const nsAString& initialColor) {
|
const nsAString& initialColor,
|
||||||
|
const nsTArray<nsString>& aDefaultColors) {
|
||||||
auto* parent = nsPIDOMWindowOuter::From(aParent);
|
auto* parent = nsPIDOMWindowOuter::From(aParent);
|
||||||
mParentWidget = mozilla::widget::WidgetUtils::DOMWindowToWidget(parent);
|
mParentWidget = mozilla::widget::WidgetUtils::DOMWindowToWidget(parent);
|
||||||
mTitle = title;
|
mTitle = title;
|
||||||
mInitialColor = initialColor;
|
mInitialColor = initialColor;
|
||||||
|
mDefaultColors.Assign(aDefaultColors);
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP nsColorPicker::Open(
|
NS_IMETHODIMP nsColorPicker::Open(
|
||||||
nsIColorPickerShownCallback* aColorPickerShownCallback) {
|
nsIColorPickerShownCallback* aColorPickerShownCallback) {
|
||||||
// Input color string should be 7 length (i.e. a string representing a valid
|
auto maybeColor = HTMLInputElement::ParseSimpleColor(mInitialColor);
|
||||||
// simple color)
|
if (maybeColor.isNothing()) {
|
||||||
if (mInitialColor.Length() != 7) {
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
const nsAString& withoutHash = StringTail(mInitialColor, 6);
|
|
||||||
nscolor color;
|
|
||||||
if (!NS_HexToRGBA(withoutHash, nsHexColorType::NoAlpha, &color)) {
|
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
nscolor color = maybeColor.value();
|
||||||
|
|
||||||
if (mCallback) {
|
if (mCallback) {
|
||||||
// It means Open has already been called: this is not allowed
|
// It means Open has already been called: this is not allowed
|
||||||
|
|
@ -106,6 +106,16 @@ NS_IMETHODIMP nsColorPicker::Open(
|
||||||
}
|
}
|
||||||
|
|
||||||
gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(color_chooser), FALSE);
|
gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(color_chooser), FALSE);
|
||||||
|
|
||||||
|
// Setting the default colors will put them into "Custom" colors list.
|
||||||
|
for (const nsString& defaultColor : mDefaultColors) {
|
||||||
|
if (auto color = HTMLInputElement::ParseSimpleColor(defaultColor)) {
|
||||||
|
GdkRGBA color_rgba = convertToRgbaColor(*color);
|
||||||
|
gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(color_chooser), &color_rgba);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The initial color needs to be set last.
|
||||||
GdkRGBA color_rgba = convertToRgbaColor(color);
|
GdkRGBA color_rgba = convertToRgbaColor(color);
|
||||||
gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(color_chooser), &color_rgba);
|
gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(color_chooser), &color_rgba);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,7 @@ class nsColorPicker final : public nsIColorPicker {
|
||||||
nsString mTitle;
|
nsString mTitle;
|
||||||
nsString mColor;
|
nsString mColor;
|
||||||
nsString mInitialColor;
|
nsString mInitialColor;
|
||||||
|
nsTArray<nsString> mDefaultColors;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // nsColorPicker_h__
|
#endif // nsColorPicker_h__
|
||||||
|
|
|
||||||
|
|
@ -14,14 +14,15 @@ NS_IMPL_ISUPPORTS(nsColorPickerProxy, nsIColorPicker)
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsColorPickerProxy::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
|
nsColorPickerProxy::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
|
||||||
const nsAString& aInitialColor) {
|
const nsAString& aInitialColor,
|
||||||
|
const nsTArray<nsString>& aDefaultColors) {
|
||||||
BrowserChild* browserChild = BrowserChild::GetFrom(aParent);
|
BrowserChild* browserChild = BrowserChild::GetFrom(aParent);
|
||||||
if (!browserChild) {
|
if (!browserChild) {
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
browserChild->SendPColorPickerConstructor(this, nsString(aTitle),
|
browserChild->SendPColorPickerConstructor(this, aTitle, aInitialColor,
|
||||||
nsString(aInitialColor));
|
aDefaultColors);
|
||||||
NS_ADDREF_THIS();
|
NS_ADDREF_THIS();
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ interface nsIColorPicker : nsISupports
|
||||||
* parameter has to follow the format specified on top
|
* parameter has to follow the format specified on top
|
||||||
* of this file.
|
* of this file.
|
||||||
*/
|
*/
|
||||||
void init(in mozIDOMWindowProxy parent, in AString title, in AString initialColor);
|
void init(in mozIDOMWindowProxy parent, in AString title, in AString initialColor, in Array<AString> defaultColors);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the color dialog asynchrounously.
|
* Opens the color dialog asynchrounously.
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <shlwapi.h>
|
#include <shlwapi.h>
|
||||||
|
|
||||||
|
#include "mozilla/ArrayUtils.h"
|
||||||
#include "mozilla/AutoRestore.h"
|
#include "mozilla/AutoRestore.h"
|
||||||
#include "nsIWidget.h"
|
#include "nsIWidget.h"
|
||||||
#include "nsString.h"
|
#include "nsString.h"
|
||||||
|
|
@ -86,18 +87,18 @@ static void BGRIntToRGBString(DWORD color, nsAString& aResult) {
|
||||||
static AsyncColorChooser* gColorChooser;
|
static AsyncColorChooser* gColorChooser;
|
||||||
|
|
||||||
AsyncColorChooser::AsyncColorChooser(COLORREF aInitialColor,
|
AsyncColorChooser::AsyncColorChooser(COLORREF aInitialColor,
|
||||||
|
const nsTArray<nsString>& aDefaultColors,
|
||||||
nsIWidget* aParentWidget,
|
nsIWidget* aParentWidget,
|
||||||
nsIColorPickerShownCallback* aCallback)
|
nsIColorPickerShownCallback* aCallback)
|
||||||
: mozilla::Runnable("AsyncColorChooser"),
|
: mozilla::Runnable("AsyncColorChooser"),
|
||||||
mInitialColor(aInitialColor),
|
mInitialColor(aInitialColor),
|
||||||
|
mDefaultColors(aDefaultColors.Clone()),
|
||||||
mColor(aInitialColor),
|
mColor(aInitialColor),
|
||||||
mParentWidget(aParentWidget),
|
mParentWidget(aParentWidget),
|
||||||
mCallback(aCallback) {}
|
mCallback(aCallback) {}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
AsyncColorChooser::Run() {
|
AsyncColorChooser::Run() {
|
||||||
static COLORREF sCustomColors[16] = {0};
|
|
||||||
|
|
||||||
MOZ_ASSERT(NS_IsMainThread(),
|
MOZ_ASSERT(NS_IsMainThread(),
|
||||||
"Color pickers can only be opened from main thread currently");
|
"Color pickers can only be opened from main thread currently");
|
||||||
|
|
||||||
|
|
@ -112,12 +113,21 @@ AsyncColorChooser::Run() {
|
||||||
? mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW)
|
? mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW)
|
||||||
: nullptr));
|
: nullptr));
|
||||||
|
|
||||||
|
COLORREF customColors[16];
|
||||||
|
for (size_t i = 0; i < mozilla::ArrayLength(customColors); i++) {
|
||||||
|
if (i < mDefaultColors.Length()) {
|
||||||
|
customColors[i] = ColorStringToRGB(mDefaultColors[i]);
|
||||||
|
} else {
|
||||||
|
customColors[i] = 0x00FFFFFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CHOOSECOLOR options;
|
CHOOSECOLOR options;
|
||||||
options.lStructSize = sizeof(options);
|
options.lStructSize = sizeof(options);
|
||||||
options.hwndOwner = adtw.get();
|
options.hwndOwner = adtw.get();
|
||||||
options.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
|
options.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
|
||||||
options.rgbResult = mInitialColor;
|
options.rgbResult = mInitialColor;
|
||||||
options.lpCustColors = sCustomColors;
|
options.lpCustColors = customColors;
|
||||||
options.lpfnHook = HookProc;
|
options.lpfnHook = HookProc;
|
||||||
|
|
||||||
mColor = ChooseColor(&options) ? options.rgbResult : mInitialColor;
|
mColor = ChooseColor(&options) ? options.rgbResult : mInitialColor;
|
||||||
|
|
@ -191,19 +201,21 @@ NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsColorPicker::Init(mozIDOMWindowProxy* parent, const nsAString& title,
|
nsColorPicker::Init(mozIDOMWindowProxy* parent, const nsAString& title,
|
||||||
const nsAString& aInitialColor) {
|
const nsAString& aInitialColor,
|
||||||
|
const nsTArray<nsString>& aDefaultColors) {
|
||||||
MOZ_ASSERT(parent,
|
MOZ_ASSERT(parent,
|
||||||
"Null parent passed to colorpicker, no color picker for you!");
|
"Null parent passed to colorpicker, no color picker for you!");
|
||||||
mParentWidget =
|
mParentWidget =
|
||||||
WidgetUtils::DOMWindowToWidget(nsPIDOMWindowOuter::From(parent));
|
WidgetUtils::DOMWindowToWidget(nsPIDOMWindowOuter::From(parent));
|
||||||
mInitialColor = ColorStringToRGB(aInitialColor);
|
mInitialColor = ColorStringToRGB(aInitialColor);
|
||||||
|
mDefaultColors.Assign(aDefaultColors);
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) {
|
nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) {
|
||||||
NS_ENSURE_ARG(aCallback);
|
NS_ENSURE_ARG(aCallback);
|
||||||
nsCOMPtr<nsIRunnable> event =
|
nsCOMPtr<nsIRunnable> event = new AsyncColorChooser(
|
||||||
new AsyncColorChooser(mInitialColor, mParentWidget, aCallback);
|
mInitialColor, mDefaultColors, mParentWidget, aCallback);
|
||||||
return NS_DispatchToMainThread(event);
|
return NS_DispatchToMainThread(event);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,9 @@ class nsIWidget;
|
||||||
|
|
||||||
class AsyncColorChooser : public mozilla::Runnable {
|
class AsyncColorChooser : public mozilla::Runnable {
|
||||||
public:
|
public:
|
||||||
AsyncColorChooser(COLORREF aInitialColor, nsIWidget* aParentWidget,
|
AsyncColorChooser(COLORREF aInitialColor,
|
||||||
|
const nsTArray<nsString>& aDefaultColors,
|
||||||
|
nsIWidget* aParentWidget,
|
||||||
nsIColorPickerShownCallback* aCallback);
|
nsIColorPickerShownCallback* aCallback);
|
||||||
NS_IMETHOD Run() override;
|
NS_IMETHOD Run() override;
|
||||||
|
|
||||||
|
|
@ -29,6 +31,7 @@ class AsyncColorChooser : public mozilla::Runnable {
|
||||||
LPARAM aLParam);
|
LPARAM aLParam);
|
||||||
|
|
||||||
COLORREF mInitialColor;
|
COLORREF mInitialColor;
|
||||||
|
nsTArray<nsString> mDefaultColors;
|
||||||
COLORREF mColor;
|
COLORREF mColor;
|
||||||
nsCOMPtr<nsIWidget> mParentWidget;
|
nsCOMPtr<nsIWidget> mParentWidget;
|
||||||
nsCOMPtr<nsIColorPickerShownCallback> mCallback;
|
nsCOMPtr<nsIColorPickerShownCallback> mCallback;
|
||||||
|
|
@ -41,13 +44,11 @@ class nsColorPicker : public nsIColorPicker {
|
||||||
nsColorPicker();
|
nsColorPicker();
|
||||||
|
|
||||||
NS_DECL_ISUPPORTS
|
NS_DECL_ISUPPORTS
|
||||||
|
NS_DECL_NSICOLORPICKER
|
||||||
NS_IMETHOD Init(mozIDOMWindowProxy* parent, const nsAString& title,
|
|
||||||
const nsAString& aInitialColor) override;
|
|
||||||
NS_IMETHOD Open(nsIColorPickerShownCallback* aCallback) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
COLORREF mInitialColor;
|
COLORREF mInitialColor;
|
||||||
|
nsTArray<nsString> mDefaultColors;
|
||||||
nsCOMPtr<nsIWidget> mParentWidget;
|
nsCOMPtr<nsIWidget> mParentWidget;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue