Bug 1896960 - Preserve the user's custom colors between multiple color pickers. r=emilio

I think there is no good way of combining support for datalists and
whatever the user picked. So I just treat the datalist colors as an
overwrite.

Differential Revision: https://phabricator.services.mozilla.com/D210891
This commit is contained in:
Tom Schuster 2024-05-20 11:01:21 +00:00
parent c26f9795d5
commit 8945a03562

View file

@ -6,6 +6,7 @@
#include "nsColorPicker.h"
#include <algorithm>
#include <shlwapi.h>
#include "mozilla/ArrayUtils.h"
@ -86,6 +87,15 @@ AsyncColorChooser::Run() {
MOZ_ASSERT(NS_IsMainThread(),
"Color pickers can only be opened from main thread currently");
// Static to preserve the custom colors between different ChooseColor calls.
static COLORREF sCustomColors[16];
static bool sInitialized = false;
if (!sInitialized) {
// Initialize to white instead of black.
std::fill(std::begin(sCustomColors), std::end(sCustomColors), 0x00FFFFFF);
sInitialized = true;
}
// Allow only one color picker to be opened at a time, to workaround bug
// 944737
if (!gColorChooser) {
@ -94,13 +104,11 @@ AsyncColorChooser::Run() {
ScopedRtlShimWindow shim(mParentWidget.get());
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;
}
// This will overwrite custom colors if default colors were defined.
for (size_t i = 0; i < std::min(mozilla::ArrayLength(sCustomColors),
mDefaultColors.Length());
i++) {
sCustomColors[i] = ColorStringToRGB(mDefaultColors[i]);
}
CHOOSECOLOR options;
@ -108,7 +116,7 @@ AsyncColorChooser::Run() {
options.hwndOwner = shim.get();
options.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
options.rgbResult = mInitialColor;
options.lpCustColors = customColors;
options.lpCustColors = sCustomColors;
options.lpfnHook = HookProc;
mColor = ChooseColor(&options) ? options.rgbResult : mInitialColor;