Chromium Code Reviews| Index: chrome/browser/ui/cocoa/color_chooser_mac.mm |
| diff --git a/chrome/browser/ui/cocoa/color_chooser_mac.mm b/chrome/browser/ui/cocoa/color_chooser_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..827c15a269d334ecdab7d08338a361e9b3103ff7 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/color_chooser_mac.mm |
| @@ -0,0 +1,144 @@ |
| +// 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. |
| + |
| +#include "chrome/browser/ui/color_chooser.h" |
| + |
| +#import "base/mac/cocoa_protocols.h" |
| +#import "base/memory/scoped_nsobject.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| +#include "skia/ext/skia_utils_mac.h" |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +class ColorChooserMac; |
| + |
| +// A Listener class to act as a event target for NSColorPanel and send |
| +// the results to the C++ class, ColorChooserMac. |
| +@interface ColorPanelBridge : NSObject<NSWindowDelegate> { |
|
Robert Sesek
2012/02/28 16:22:09
@private
Robert Sesek
2012/02/28 16:22:09
Naming: bridges are usually C++ -> ObjC. I'd call
keishi
2012/02/29 13:05:22
Done.
keishi
2012/02/29 13:05:22
Done.
|
| + BOOL nonUserChange_; |
| + ColorChooserMac* chooser_; |
|
Robert Sesek
2012/02/28 16:22:09
// weak, owns this
keishi
2012/02/29 13:05:22
Done.
|
| +} |
| + |
| +- (id)initWithChooser:(ColorChooserMac*)chooser; |
| +- (void)dealloc; |
|
Robert Sesek
2012/02/28 16:22:09
No need to declare this
keishi
2012/02/29 13:05:22
Done.
|
| + |
| +// Called from NSColorPanel. |
| +- (void)didChooseColor:(NSColorPanel*)panel; |
| + |
| +- (void)setColor:(NSColor*)color; |
| + |
| +@end |
| + |
| +class ColorChooserMac : public ColorChooser, |
| + public content::WebContentsObserver { |
| +public: |
|
Robert Sesek
2012/02/28 16:22:09
nit: indent 1
keishi
2012/02/29 13:05:22
Done.
|
| + ColorChooserMac( |
| + int identifier, content::WebContents* tab, SkColor initial_color); |
| + virtual ~ColorChooserMac(); |
| + |
| + // Called from ColorPanelBridge. |
| + void DidChooseColor(SkColor color); |
| + void DidClose(); |
| + |
| + virtual int GetIdentifier() const OVERRIDE { return identifier_; } |
| + |
| + virtual void End() OVERRIDE; |
| + virtual void SetSelectedColor(SkColor color) OVERRIDE; |
| + |
| +private: |
|
Robert Sesek
2012/02/28 16:22:09
nit: indent 1
keishi
2012/02/29 13:05:22
Done.
|
| + virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE; |
| + |
| + int identifier_; |
| + scoped_nsobject<ColorPanelBridge> bridge_; |
| +}; |
| + |
| +ColorChooser* ColorChooser::Create( |
| + int identifier, content::WebContents* tab, SkColor initial_color) { |
| + return new ColorChooserMac(identifier, tab, initial_color); |
| +} |
| + |
| +ColorChooserMac::ColorChooserMac( |
| + int identifier, content::WebContents* tab, SkColor initial_color) |
| + : content::WebContentsObserver(tab), |
| + identifier_(identifier) { |
| + bridge_.reset([[ColorPanelBridge alloc] initWithChooser:this]); |
| + [bridge_ setColor:gfx::SkColorToDeviceNSColor(initial_color)]; |
| + [[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:NULL]; |
|
Robert Sesek
2012/02/28 16:22:09
Use nil instead of NULL
keishi
2012/02/29 13:05:22
Done.
|
| +} |
| + |
| +ColorChooserMac::~ColorChooserMac() { |
| + // Always call End() before destroying. |
| + DCHECK(!bridge_); |
| +} |
| + |
| +void ColorChooserMac::DidChooseColor(SkColor color) { |
| + if (web_contents()) |
| + web_contents()->DidChooseColorInColorChooser(identifier_, color); |
| +} |
| + |
| +void ColorChooserMac::DidClose() { |
| + End(); |
| +} |
| + |
| +void ColorChooserMac::End() { |
| + bridge_.reset(); |
| + if (web_contents()) |
| + web_contents()->DidEndColorChooser(identifier_); |
| +} |
| + |
| +void ColorChooserMac::SetSelectedColor(SkColor color) { |
| + [bridge_ setColor:gfx::SkColorToDeviceNSColor(color)]; |
| +} |
| + |
| +void ColorChooserMac::WebContentsDestroyed(content::WebContents* tab) { |
| + End(); |
| +} |
| + |
| +@implementation ColorPanelBridge |
| + |
| +- (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 { |
|
Robert Sesek
2012/02/28 16:22:09
nit: no space before *
keishi
2012/02/29 13:05:22
Done.
|
| + chooser_->DidClose(); |
| + nonUserChange_ = NO; |
| +} |
| + |
| +- (void)didChooseColor:(NSColorPanel *)panel { |
|
Robert Sesek
2012/02/28 16:22:09
here too
keishi
2012/02/29 13:05:22
Done.
|
| + if (nonUserChange_) { |
| + nonUserChange_ = NO; |
| + return; |
| + } |
| + chooser_->DidChooseColor(gfx::NSDeviceColorToSkColor( |
| + [[panel color] colorUsingColorSpaceName:NSDeviceRGBColorSpace])); |
| + nonUserChange_ = NO; |
| +} |
| + |
| +- (void)setColor:(NSColor *)color { |
|
Robert Sesek
2012/02/28 16:22:09
and here
keishi
2012/02/29 13:05:22
Done.
|
| + nonUserChange_ = YES; |
| + [[NSColorPanel sharedColorPanel] setColor:color]; |
| +} |
| + |
| +@end |