forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_chooser_mac.mm
More file actions
161 lines (128 loc) · 4.43 KB
/
Copy pathcolor_chooser_mac.mm
File metadata and controls
161 lines (128 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "base/logging.h"
#import "base/mac/scoped_nsobject.h"
#include "content/nw/src/browser/browser_dialogs.h"
#include "content/public/browser/color_chooser.h"
#include "content/public/browser/web_contents.h"
#include "skia/ext/skia_utils_mac.h"
class ColorChooserMac;
// A Listener class to act as a event target for NSColorPanel and send
// the results to the C++ class, ColorChooserMac.
@interface ColorPanelCocoa : NSObject<NSWindowDelegate> {
@private
// We don't call DidChooseColor if the change wasn't caused by the user
// interacting with the panel.
BOOL nonUserChange_;
ColorChooserMac* chooser_; // weak, owns this
}
- (id)initWithChooser:(ColorChooserMac*)chooser;
// Called from NSColorPanel.
- (void)didChooseColor:(NSColorPanel*)panel;
// Sets color to the NSColorPanel as a non user change.
- (void)setColor:(NSColor*)color;
@end
class ColorChooserMac : public content::ColorChooser {
public:
static ColorChooserMac* Open(content::WebContents* web_contents,
SkColor initial_color);
ColorChooserMac(content::WebContents* tab, SkColor initial_color);
virtual ~ColorChooserMac();
// Called from ColorPanelCocoa.
void DidChooseColorInColorPanel(SkColor color);
void DidCloseColorPabel();
virtual void End() OVERRIDE;
virtual void SetSelectedColor(SkColor color) OVERRIDE;
private:
static ColorChooserMac* current_color_chooser_;
// The web contents invoking the color chooser. No ownership because it will
// outlive this class.
content::WebContents* web_contents_;
base::scoped_nsobject<ColorPanelCocoa> panel_;
};
ColorChooserMac* ColorChooserMac::current_color_chooser_ = NULL;
// static
ColorChooserMac* ColorChooserMac::Open(content::WebContents* web_contents,
SkColor initial_color) {
if (current_color_chooser_)
current_color_chooser_->End();
CHECK(!current_color_chooser_);
current_color_chooser_ =
new ColorChooserMac(web_contents, initial_color);
return current_color_chooser_;
}
ColorChooserMac::ColorChooserMac(content::WebContents* web_contents,
SkColor initial_color)
: web_contents_(web_contents) {
panel_.reset([[ColorPanelCocoa alloc] initWithChooser:this]);
[panel_ setColor:gfx::SkColorToDeviceNSColor(initial_color)];
[[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:nil];
}
ColorChooserMac::~ColorChooserMac() {
// Always call End() before destroying.
CHECK(!panel_);
}
void ColorChooserMac::DidChooseColorInColorPanel(SkColor color) {
if (web_contents_)
web_contents_->DidChooseColorInColorChooser(color);
}
void ColorChooserMac::DidCloseColorPabel() {
End();
}
void ColorChooserMac::End() {
panel_.reset();
CHECK(current_color_chooser_ == this);
current_color_chooser_ = NULL;
if (web_contents_)
web_contents_->DidEndColorChooser();
}
void ColorChooserMac::SetSelectedColor(SkColor color) {
[panel_ setColor:gfx::SkColorToDeviceNSColor(color)];
}
@implementation ColorPanelCocoa
- (id)initWithChooser:(ColorChooserMac*)chooser {
if ((self = [super init])) {
chooser_ = chooser;
NSColorPanel* panel = [NSColorPanel sharedColorPanel];
[panel setShowsAlpha:NO];
[panel setDelegate:self];
[panel setTarget:self];
[panel setAction:@selector(didChooseColor:)];
}
return self;
}
- (void)dealloc {
NSColorPanel* panel = [NSColorPanel sharedColorPanel];
if ([panel delegate] == self) {
[panel setDelegate:nil];
[panel setTarget:nil];
[panel setAction:nil];
}
[super dealloc];
}
- (void)windowWillClose:(NSNotification*)notification {
nonUserChange_ = NO;
chooser_->DidCloseColorPabel();
}
- (void)didChooseColor:(NSColorPanel*)panel {
if (nonUserChange_) {
nonUserChange_ = NO;
return;
}
chooser_->DidChooseColorInColorPanel(gfx::NSDeviceColorToSkColor(
[[panel color] colorUsingColorSpaceName:NSDeviceRGBColorSpace]));
nonUserChange_ = NO;
}
- (void)setColor:(NSColor*)color {
nonUserChange_ = YES;
[[NSColorPanel sharedColorPanel] setColor:color];
}
namespace nw {
content::ColorChooser* ShowColorChooser(content::WebContents* web_contents,
SkColor initial_color) {
return ColorChooserMac::Open(web_contents, initial_color);
}
} // namepace chrome
@end