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..8fd2daad46bbf60d0499a16125a7cd370c3daa52 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/color_chooser_mac.mm |
| @@ -0,0 +1,151 @@ |
| +// 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/browser/renderer_host/render_view_host.h" |
| +#include "content/public/browser/render_view_host_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> { |
| + BOOL nonUserChange_; |
| + ColorChooserMac* chooser_; |
| +} |
| + |
| +- (id)initWithChooser:(ColorChooserMac*)chooser; |
| +- (void)dealloc; |
| + |
| +// Called from NSColorPanel |
|
Nico
2012/02/21 19:40:17
.
keishi
2012/02/24 14:38:54
Done.
|
| +- (void)didChooseColor:(NSColorPanel *)panel; |
|
Nico
2012/02/21 19:40:17
No space in front of *
keishi
2012/02/24 14:38:54
Done.
|
| + |
| +- (void)setColor:(NSColor *)color; |
|
Nico
2012/02/21 19:40:17
No space in front of *
keishi
2012/02/24 14:38:54
Done.
|
| + |
| +@end |
| + |
| +class ColorChooserMac : public ColorChooser, |
| + public content::RenderViewHostObserver { |
| +public: |
| + ColorChooserMac( |
| + int identifier, RenderViewHost* rvh, SkColor initial_color); |
| + virtual ~ColorChooserMac(); |
| + |
| + // Called from ColorPanelBridge |
|
Nico
2012/02/21 19:40:17
.
keishi
2012/02/24 14:38:54
Done.
|
| + void DidChooseColor(SkColor color); |
| + void DidClose(); |
| + |
| + virtual RenderViewHost* render_view_host() const OVERRIDE; |
| + virtual int identifier() const OVERRIDE { return identifier_; } |
| + |
| + virtual void End() OVERRIDE; |
| + virtual void SetSelectedColor(SkColor color) OVERRIDE; |
| + |
| +private: |
| + virtual void RenderViewHostDestroyed( |
| + RenderViewHost* render_view_host) OVERRIDE; |
| + |
| + int identifier_; |
| + scoped_nsobject<ColorPanelBridge> bridge_; |
| +}; |
| + |
| +ColorChooser* ColorChooser::Create( |
| + int identifier, RenderViewHost* rvh, SkColor initial_color) { |
| + return new ColorChooserMac(identifier, rvh, initial_color); |
| +} |
| + |
| +ColorChooserMac::ColorChooserMac( |
| + int identifier, RenderViewHost* rvh, SkColor initial_color) |
| + : content::RenderViewHostObserver(rvh), |
| + identifier_(identifier) { |
| + bridge_.reset([[ColorPanelBridge alloc] initWithChooser:this]); |
| + // setColor needs to be called after panel's target is set. |
|
Nico
2012/02/21 19:40:17
what does "panel" refer to?
keishi
2012/02/24 14:38:54
I meant the shared color panel. I added this comme
|
| + [bridge_ setColor:gfx::SkColorToDeviceNSColor(initial_color)]; |
| + [[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:NULL]; |
| +} |
| + |
| +ColorChooserMac::~ColorChooserMac() { |
| + End(); |
| +} |
| + |
| +void ColorChooserMac::DidChooseColor(SkColor color) { |
| + if (render_view_host()) |
| + render_view_host()->DidChooseColorInColorChooser(identifier_, color); |
| +} |
| + |
| +void ColorChooserMac::DidClose() { |
| + End(); |
| +} |
| + |
| +void ColorChooserMac::End() { |
| + bridge_.reset(); |
| + if (render_view_host()) |
| + render_view_host()->DidEndColorChooser(identifier_); |
| +} |
| + |
| +void ColorChooserMac::SetSelectedColor(SkColor color) { |
| + [bridge_ setColor:gfx::SkColorToDeviceNSColor(color)]; |
| +} |
| + |
| +RenderViewHost* ColorChooserMac::render_view_host() const { |
| + return content::RenderViewHostObserver::render_view_host(); |
| +} |
| + |
| +void ColorChooserMac::RenderViewHostDestroyed( |
| + RenderViewHost* render_view_host) { |
| + 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:)]; |
|
Nico
2012/02/21 19:40:17
What happens if two web sites open this chooser? I
keishi
2012/02/24 14:38:54
If a second site tries to open the color chooser,
Nico
2012/02/27 17:48:59
This is something that might be worth having a tes
|
| + } |
| + 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 { |
| + chooser_->DidClose(); |
| + nonUserChange_ = NO; |
| +} |
| + |
| +- (void)didChooseColor:(NSColorPanel *)panel { |
| + if (nonUserChange_) { |
| + nonUserChange_ = NO; |
|
Nico
2012/02/21 19:40:17
dedent 2
keishi
2012/02/24 14:38:54
Done.
|
| + return; |
| + } |
| + chooser_->DidChooseColor(gfx::NSColorToSkColor( |
| + [[panel color] colorUsingColorSpaceName:NSDeviceRGBColorSpace])); |
| + nonUserChange_ = NO; |
| +} |
| + |
| +- (void)setColor:(NSColor *)color { |
| + nonUserChange_ = YES; |
| + [[NSColorPanel sharedColorPanel] setColor:color]; |
| +} |
| + |
| +@end |