1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #import <Cocoa/Cocoa.h>
8 #include "nsColorPicker.h"
9 #include "nsCocoaUtils.h"
10 #include "nsThreadUtils.h"
12 using namespace mozilla;
14 static unsigned int HexStrToInt(NSString* str) {
15 unsigned int result = 0;
17 for (unsigned int i = 0; i < [str length]; ++i) {
18 char c = [str characterAtIndex:i];
20 if (c >= '0' && c <= '9') {
22 } else if (c >= 'A' && c <= 'F') {
23 result += 10 + (c - 'A');
25 result += 10 + (c - 'a');
32 @interface NSColorPanelWrapper : NSObject <NSWindowDelegate> {
33 NSColorPanel* mColorPanel;
34 nsColorPicker* mColorPicker;
36 - (id)initWithPicker:(nsColorPicker*)aPicker;
37 - (void)open:(NSColor*)aInitialColor title:(NSString*)aTitle;
38 - (void)colorChanged:(NSColorPanel*)aPanel;
39 - (void)windowWillClose:(NSNotification*)aNotification;
43 @implementation NSColorPanelWrapper
44 - (id)initWithPicker:(nsColorPicker*)aPicker {
45 mColorPicker = aPicker;
46 mColorPanel = [NSColorPanel sharedColorPanel];
52 - (void)open:(NSColor*)aInitialColor title:(NSString*)aTitle {
53 [mColorPanel setTarget:self];
54 [mColorPanel setAction:@selector(colorChanged:)];
55 [mColorPanel setDelegate:self];
56 [mColorPanel setTitle:aTitle];
57 [mColorPanel setColor:aInitialColor];
58 mColorPanel.showsAlpha = NO;
59 [mColorPanel setFrameOrigin:[NSEvent mouseLocation]];
60 [mColorPanel makeKeyAndOrderFront:nil];
63 - (void)colorChanged:(NSColorPanel*)aPanel {
67 mColorPicker->Update([mColorPanel color]);
70 - (void)windowWillClose:(NSNotification*)aNotification {
78 [mColorPanel setTarget:nil];
79 [mColorPanel setAction:nil];
80 [mColorPanel setDelegate:nil];
83 mColorPicker = nullptr;
87 NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
89 nsColorPicker::~nsColorPicker() {
90 if (mColorPanelWrapper) {
91 [mColorPanelWrapper close];
92 [mColorPanelWrapper release];
93 mColorPanelWrapper = nullptr;
97 // TODO(bug 1805397): Implement default colors
98 nsresult nsColorPicker::InitNative(const nsTArray<nsString>& aDefaultColors) {
99 MOZ_ASSERT(NS_IsMainThread(),
100 "Color pickers can only be opened from main thread currently");
101 mColorPanelWrapper = [[NSColorPanelWrapper alloc] initWithPicker:this];
105 /* static */ NSColor* nsColorPicker::GetNSColorFromHexString(
106 const nsAString& aColor) {
107 NSString* str = nsCocoaUtils::ToNSString(aColor);
109 double red = HexStrToInt([str substringWithRange:NSMakeRange(1, 2)]) / 255.0;
111 HexStrToInt([str substringWithRange:NSMakeRange(3, 2)]) / 255.0;
112 double blue = HexStrToInt([str substringWithRange:NSMakeRange(5, 2)]) / 255.0;
114 return [NSColor colorWithDeviceRed:red green:green blue:blue alpha:1.0];
117 /* static */ void nsColorPicker::GetHexStringFromNSColor(NSColor* aColor,
118 nsAString& aResult) {
119 CGFloat redFloat, greenFloat, blueFloat;
121 NSColor* color = aColor;
123 [color getRed:&redFloat green:&greenFloat blue:&blueFloat alpha:nil];
124 } @catch (NSException* e) {
125 color = [color colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
126 [color getRed:&redFloat green:&greenFloat blue:&blueFloat alpha:nil];
129 nsCocoaUtils::GetStringForNSString(
130 [NSString stringWithFormat:@"#%02x%02x%02x", (int)(redFloat * 255),
131 (int)(greenFloat * 255),
132 (int)(blueFloat * 255)],
136 nsresult nsColorPicker::OpenNative() {
137 [mColorPanelWrapper open:GetNSColorFromHexString(mInitialColor)
138 title:nsCocoaUtils::ToNSString(mTitle)];
145 void nsColorPicker::Update(NSColor* aColor) {
146 GetHexStringFromNSColor(aColor, mInitialColor);
147 mCallback->Update(mInitialColor);
150 void nsColorPicker::Done() {
151 [mColorPanelWrapper close];
152 [mColorPanelWrapper release];
153 mColorPanelWrapper = nullptr;
154 mCallback->Done(u""_ns);