Bug 975468 - Allow only one NSColorPanelWrapper to be opened at a time, and retarget the existing one if user clicks on another input type color. r=areinald

This commit is contained in:
Arnaud Bienner 2014-03-13 16:04:57 +01:00
parent 93347f7268
commit 803ccbf6d6
2 changed files with 35 additions and 8 deletions

View File

@ -27,15 +27,21 @@ public:
// For NSColorPanelWrapper.
void Update(NSColor* aColor);
// Call this method if you are done with this input, but the color picker needs to
// stay open as it will be associated to another input
void DoneWithRetarget();
// Same as DoneWithRetarget + clean the static instance of sColorPanelWrapper,
// as it is not needed anymore for now
void Done();
private:
static NSColor* GetNSColorFromHexString(const nsAString& aColor);
static void GetHexStringFromNSColor(NSColor* aColor, nsAString& aResult);
static NSColorPanelWrapper* sColorPanelWrapper;
nsString mTitle;
nsString mColor;
NSColorPanelWrapper* mColorPanel;
nsCOMPtr<nsIColorPickerShownCallback> mCallback;
};

View File

@ -37,6 +37,7 @@ HexStrToInt(NSString* str)
}
- (id)initWithPicker:(nsColorPicker*)aPicker;
- (void)open:(NSColor*)aInitialColor title:(NSString*)aTitle;
- (void)retarget:(nsColorPicker*)aPicker;
- (void)colorChanged:(NSColorPanel*)aPanel;
@end
@ -70,6 +71,12 @@ HexStrToInt(NSString* str)
mColorPicker->Done();
}
- (void)retarget:(nsColorPicker*)aPicker
{
mColorPicker->DoneWithRetarget();
mColorPicker = aPicker;
}
- (void)dealloc
{
if ([mColorPanel delegate] == self) {
@ -87,15 +94,24 @@ HexStrToInt(NSString* str)
NS_IMPL_ISUPPORTS1(nsColorPicker, nsIColorPicker)
NSColorPanelWrapper* nsColorPicker::sColorPanelWrapper = nullptr;
NS_IMETHODIMP
nsColorPicker::Init(nsIDOMWindow* aParent, const nsAString& aTitle,
const nsAString& aInitialColor)
{
MOZ_ASSERT(NS_IsMainThread(),
"Color pickers can only be opened from main thread currently");
mTitle = aTitle;
mColor = aInitialColor;
mColorPanel = [[NSColorPanelWrapper alloc] initWithPicker:this];
if (sColorPanelWrapper) {
// Update current wrapper to target the new input instead
[sColorPanelWrapper retarget:this];
} else {
// Create a brand new color panel wrapper
sColorPanelWrapper = [[NSColorPanelWrapper alloc] initWithPicker:this];
}
return NS_OK;
}
@ -130,7 +146,7 @@ nsColorPicker::Open(nsIColorPickerShownCallback* aCallback)
MOZ_ASSERT(aCallback);
mCallback = aCallback;
[mColorPanel open:GetNSColorFromHexString(mColor)
[sColorPanelWrapper open:GetNSColorFromHexString(mColor)
title:nsCocoaUtils::ToNSString(mTitle)];
NS_ADDREF_THIS();
@ -146,12 +162,17 @@ nsColorPicker::Update(NSColor* aColor)
}
void
nsColorPicker::Done()
nsColorPicker::DoneWithRetarget()
{
mCallback->Done(EmptyString());
mCallback = nullptr;
[mColorPanel release];
NS_RELEASE_THIS();
}
void
nsColorPicker::Done()
{
[sColorPanelWrapper release];
sColorPanelWrapper = nullptr;
DoneWithRetarget();
}