Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7973)

Unified Diff: chrome/browser/ui/cocoa/color_chooser_mac.mm

Issue 9203001: Implement input type=color UI (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fixed issues/removed color_select_helper/added color_chooser_id Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..bc11466ef1b6911844be5f8c292e1a0c45ddd58f
--- /dev/null
+++ b/chrome/browser/ui/cocoa/color_chooser_mac.mm
@@ -0,0 +1,135 @@
+// 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 "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;
+
+// Called from NSColorPanel
+- (void)didChooseColor:(NSColorPanel *)panel;
+
+- (void)setColor:(NSColor *)color;
+
+@end
+
+class ColorChooserMac : public ColorChooser {
+public:
+ ColorChooserMac(unsigned identifier, RenderViewHost* rvh);
+ virtual ~ColorChooserMac();
+
+ // Called from ColorPanelBridge
+ void DidChooseColor(SkColor color);
+ void DidClose();
+
+ virtual RenderViewHost* render_view_host() { return render_view_host_; }
+ virtual unsigned identifier() { return identifier_; }
+
+protected:
+ virtual void Open(SkColor initial_color);
+ virtual void End();
+ virtual void SetSelectedColor(SkColor color);
+
+private:
+ unsigned identifier_;
+ RenderViewHost* render_view_host_;
+ scoped_nsobject<ColorPanelBridge> bridge_;
+};
+
+ColorChooser* ColorChooser::Create(unsigned identifier, RenderViewHost* rvh) {
+ return new ColorChooserMac(identifier, rvh);
+}
+
+ColorChooserMac::ColorChooserMac(unsigned identifier, RenderViewHost* rvh)
+ : identifier_(identifier),
+ render_view_host_(rvh) {
+ bridge_.reset([[ColorPanelBridge alloc] initWithChooser:this]);
+}
+
+ColorChooserMac::~ColorChooserMac()
+{
+ printf("> ColorChooserMac::~ColorChooserMac %p\n", this);
+ NSColorPanel* panel = [NSColorPanel sharedColorPanel];
+ [panel setDelegate:nil];
+ [panel setTarget:nil];
+ [panel setAction:nil];
+}
+
+void ColorChooserMac::DidChooseColor(SkColor color) {
+ printf("ColorChooserMac::DidChooseColor\n");
+ render_view_host_->DidChooseColorInColorChooser(identifier_, color);
+}
+
+void ColorChooserMac::Open(SkColor initial_color) {
+ printf("> ColorChooserMac::Open %p\n", this);
+ NSColorPanel* panel = [NSColorPanel sharedColorPanel];
+ [panel setShowsAlpha:NO];
+ [panel setDelegate:bridge_.get()];
+ [panel setTarget:bridge_.get()];
+ [panel setAction:@selector(didChooseColor:)];
+ // setColor needs to be called after panel's target is set.
+ [bridge_ setColor:gfx::SkColorToDeviceNSColor(initial_color)];
+ [panel makeKeyAndOrderFront:NULL];
+}
+
+void ColorChooserMac::DidClose() {
+ printf("> ColorChooserMac::DidClose %p\n", this);
+ End();
+}
+
+
+void ColorChooserMac::End() {
+ printf("> ColorChooserMac::End %p %d\n", this, !bridge_.get());
+ render_view_host_->DidEndColorChooser(identifier_);
+}
+
+void ColorChooserMac::SetSelectedColor(SkColor color) {
+ [bridge_ setColor:gfx::SkColorToDeviceNSColor(color)];
+}
+
+@implementation ColorPanelBridge
+
+- (id)initWithChooser:(ColorChooserMac*)chooser
+{
+ if (self = [super init])
+ chooser_ = chooser;
+ return self;
+}
+
+- (void)windowWillClose:(NSNotification *)notification {
+ chooser_->DidClose();
+ nonUserChange_ = NO;
+}
+
+- (void)didChooseColor:(NSColorPanel *)panel {
+ if (nonUserChange_) {
+ nonUserChange_ = NO;
+ return;
+ }
+ chooser_->DidChooseColor(gfx::NSColorToSkColor(
+ [[panel color] colorUsingColorSpaceName:NSDeviceRGBColorSpace]));
+ nonUserChange_ = NO;
+}
+
+- (void)setColor:(NSColor *)color {
+ nonUserChange_ = YES;
+ [[NSColorPanel sharedColorPanel] setColor:color];
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698