1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/browser_dialogs.h" 6 #include "content/public/browser/color_chooser.h" 7 #include "content/public/browser/web_contents.h" 8 #include "content/public/browser/web_contents_view.h" 9 #include "ui/views/color_chooser/color_chooser_listener.h" 10 #include "ui/views/color_chooser/color_chooser_view.h" 11 #include "ui/views/widget/widget.h" 12 13 14 namespace { 15 16 class ColorChooserAura : public content::ColorChooser, 17 public views::ColorChooserListener { 18 public: 19 static ColorChooserAura* Open(content::WebContents* web_contents, 20 SkColor initial_color); 21 22 ColorChooserAura(content::WebContents* web_contents, SkColor initial_color); 23 24 private: 25 static ColorChooserAura* current_color_chooser_; 26 27 // content::ColorChooser overrides: 28 virtual void End() OVERRIDE; 29 virtual void SetSelectedColor(SkColor color) OVERRIDE; 30 31 // views::ColorChooserListener overrides: 32 virtual void OnColorChosen(SkColor color) OVERRIDE; 33 virtual void OnColorChooserDialogClosed() OVERRIDE; 34 35 void DidEndColorChooser(); 36 37 // The actual view of the color chooser. No ownership because its parent 38 // view will take care of its lifetime. 39 views::ColorChooserView* view_; 40 41 // The widget for the color chooser. No ownership because it's released 42 // automatically when closed. 43 views::Widget* widget_; 44 45 // The web contents invoking the color chooser. No ownership because it will 46 // outlive this class. 47 content::WebContents* web_contents_; 48 49 DISALLOW_COPY_AND_ASSIGN(ColorChooserAura); 50 }; 51 52 ColorChooserAura* ColorChooserAura::current_color_chooser_ = NULL; 53 54 ColorChooserAura::ColorChooserAura(content::WebContents* web_contents, 55 SkColor initial_color) 56 : web_contents_(web_contents) { 57 view_ = new views::ColorChooserView(this, initial_color); 58 widget_ = views::Widget::CreateWindowWithContext( 59 view_, web_contents->GetView()->GetNativeView()); 60 widget_->SetAlwaysOnTop(true); 61 widget_->Show(); 62 } 63 64 void ColorChooserAura::OnColorChosen(SkColor color) { 65 if (web_contents_) 66 web_contents_->DidChooseColorInColorChooser(color); 67 } 68 69 void ColorChooserAura::OnColorChooserDialogClosed() { 70 view_ = NULL; 71 widget_ = NULL; 72 DidEndColorChooser(); 73 } 74 75 void ColorChooserAura::End() { 76 if (widget_ && widget_->IsVisible()) { 77 view_->set_listener(NULL); 78 widget_->Close(); 79 view_ = NULL; 80 widget_ = NULL; 81 // DidEndColorChooser will invoke Browser::DidEndColorChooser, which deletes 82 // this. Take care of the call order. 83 DidEndColorChooser(); 84 } 85 } 86 87 void ColorChooserAura::DidEndColorChooser() { 88 DCHECK(current_color_chooser_ == this); 89 current_color_chooser_ = NULL; 90 if (web_contents_) 91 web_contents_->DidEndColorChooser(); 92 } 93 94 void ColorChooserAura::SetSelectedColor(SkColor color) { 95 if (view_) 96 view_->OnColorChanged(color); 97 } 98 99 // static 100 ColorChooserAura* ColorChooserAura::Open( 101 content::WebContents* web_contents, SkColor initial_color) { 102 if (current_color_chooser_) 103 current_color_chooser_->End(); 104 DCHECK(!current_color_chooser_); 105 current_color_chooser_ = new ColorChooserAura(web_contents, initial_color); 106 return current_color_chooser_; 107 } 108 109 } // namespace 110 111 namespace chrome { 112 113 content::ColorChooser* ShowColorChooser(content::WebContents* web_contents, 114 SkColor initial_color) { 115 return ColorChooserAura::Open(web_contents, initial_color); 116 } 117 118 } // namespace chrome 119