Bug 1543813: Minor refactor of macOS color picker. r=mstange

Differential Revision: https://phabricator.services.mozilla.com/D71043
This commit is contained in:
Stephen A Pohl 2020-05-19 17:27:42 +00:00
Родитель 785ca4a0fc
Коммит bb9d5ca840
4 изменённых файлов: 11 добавлений и 32 удалений

Просмотреть файл

@ -116,7 +116,8 @@ static NSFontManager* sFontManager;
static void GetStringForNSString(const NSString* aSrc, nsAString& aDest) {
aDest.SetLength([aSrc length]);
[aSrc getCharacters:reinterpret_cast<unichar*>(aDest.BeginWriting())];
[aSrc getCharacters:reinterpret_cast<unichar*>(aDest.BeginWriting())
range:NSMakeRange(0, [aSrc length])];
}
static NSString* GetNSStringForString(const nsAString& aSrc) {

Просмотреть файл

@ -571,7 +571,8 @@ void nsCocoaUtils::GetStringForNSString(const NSString* aSrc, nsAString& aDist)
}
aDist.SetLength([aSrc length]);
[aSrc getCharacters:reinterpret_cast<unichar*>(aDist.BeginWriting())];
[aSrc getCharacters:reinterpret_cast<unichar*>(aDist.BeginWriting())
range:NSMakeRange(0, [aSrc length])];
NS_OBJC_END_TRY_ABORT_BLOCK;
}

Просмотреть файл

@ -26,11 +26,6 @@ class nsColorPicker final : public nsIColorPicker {
// 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:
@ -39,7 +34,7 @@ class nsColorPicker final : public nsIColorPicker {
static NSColor* GetNSColorFromHexString(const nsAString& aColor);
static void GetHexStringFromNSColor(NSColor* aColor, nsAString& aResult);
static NSColorPanelWrapper* sColorPanelWrapper;
NSColorPanelWrapper* mColorPanelWrapper;
nsString mTitle;
nsString mColor;

Просмотреть файл

@ -35,8 +35,8 @@ static unsigned int HexStrToInt(NSString* str) {
}
- (id)initWithPicker:(nsColorPicker*)aPicker;
- (void)open:(NSColor*)aInitialColor title:(NSString*)aTitle;
- (void)retarget:(nsColorPicker*)aPicker;
- (void)colorChanged:(NSColorPanel*)aPanel;
- (void)windowWillClose:(NSNotification*)aNotification;
@end
@implementation NSColorPanelWrapper
@ -65,11 +65,6 @@ static unsigned int HexStrToInt(NSString* str) {
mColorPicker->Done();
}
- (void)retarget:(nsColorPicker*)aPicker {
mColorPicker->DoneWithRetarget();
mColorPicker = aPicker;
}
- (void)dealloc {
[mColorPanel setTarget:nil];
[mColorPanel setAction:nil];
@ -84,8 +79,6 @@ static unsigned int HexStrToInt(NSString* str) {
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
NSColorPanelWrapper* nsColorPicker::sColorPanelWrapper = nullptr;
nsColorPicker::~nsColorPicker() {}
NS_IMETHODIMP
@ -94,14 +87,7 @@ nsColorPicker::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
MOZ_ASSERT(NS_IsMainThread(), "Color pickers can only be opened from main thread currently");
mTitle = aTitle;
mColor = aInitialColor;
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];
}
mColorPanelWrapper = [[NSColorPanelWrapper alloc] initWithPicker:this];
return NS_OK;
}
@ -137,7 +123,7 @@ nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) {
MOZ_ASSERT(aCallback);
mCallback = aCallback;
[sColorPanelWrapper open:GetNSColorFromHexString(mColor) title:nsCocoaUtils::ToNSString(mTitle)];
[mColorPanelWrapper open:GetNSColorFromHexString(mColor) title:nsCocoaUtils::ToNSString(mTitle)];
NS_ADDREF_THIS();
@ -149,14 +135,10 @@ void nsColorPicker::Update(NSColor* aColor) {
mCallback->Update(mColor);
}
void nsColorPicker::DoneWithRetarget() {
void nsColorPicker::Done() {
[mColorPanelWrapper release];
mColorPanelWrapper = nullptr;
mCallback->Done(EmptyString());
mCallback = nullptr;
NS_RELEASE_THIS();
}
void nsColorPicker::Done() {
[sColorPanelWrapper release];
sColorPanelWrapper = nullptr;
DoneWithRetarget();
}