win: Fix black ground when closing message box.
authorCheng Zhao <zcbenz@gmail.com>
Sun, 13 Jul 2014 01:50:16 +0000 (09:50 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 13 Jul 2014 01:50:16 +0000 (09:50 +0800)
atom/browser/ui/message_box_views.cc

index 75d9446..ed67c30 100644 (file)
@@ -30,6 +30,8 @@ namespace {
 // conflict with other groups that could be in the dialog content.
 const int kButtonGroup = 1127;
 
+class MessageDialogClientView;
+
 class MessageDialog : public views::WidgetDelegate,
                       public views::View,
                       public views::ButtonListener {
@@ -43,6 +45,7 @@ class MessageDialog : public views::WidgetDelegate,
   virtual ~MessageDialog();
 
   void Show(base::RunLoop* run_loop = NULL);
+  void Close();
 
   int GetResult() const;
 
@@ -54,7 +57,6 @@ class MessageDialog : public views::WidgetDelegate,
  private:
   // Overridden from views::WidgetDelegate:
   virtual base::string16 GetWindowTitle() const;
-  virtual void WindowClosing() OVERRIDE;
   virtual views::Widget* GetWidget() OVERRIDE;
   virtual const views::Widget* GetWidget() const OVERRIDE;
   virtual views::View* GetContentsView() OVERRIDE;
@@ -62,6 +64,7 @@ class MessageDialog : public views::WidgetDelegate,
   virtual ui::ModalType GetModalType() const OVERRIDE;
   virtual views::NonClientFrameView* CreateNonClientFrameView(
       views::Widget* widget) OVERRIDE;
+  virtual views::ClientView* CreateClientView(views::Widget* widget) OVERRIDE;
 
   // Overridden from views::View:
   virtual gfx::Size GetPreferredSize() OVERRIDE;
@@ -72,13 +75,12 @@ class MessageDialog : public views::WidgetDelegate,
   virtual void ButtonPressed(views::Button* sender,
                              const ui::Event& event) OVERRIDE;
 
-  bool should_close_;
   bool delete_on_close_;
   int result_;
   base::string16 title_;
 
   NativeWindow* parent_;
-  views::Widget* widget_;
+  scoped_ptr<views::Widget> widget_;
   views::MessageBoxView* message_box_view_;
   std::vector<views::LabelButton*> buttons_;
 
@@ -89,6 +91,25 @@ class MessageDialog : public views::WidgetDelegate,
   DISALLOW_COPY_AND_ASSIGN(MessageDialog);
 };
 
+class MessageDialogClientView : public views::ClientView {
+ public:
+  MessageDialogClientView(MessageDialog* dialog, views::Widget* widget)
+      : views::ClientView(widget, dialog),
+        dialog_(dialog) {
+  }
+
+  // views::ClientView:
+  virtual bool CanClose() OVERRIDE {
+    dialog_->Close();
+    return false;
+  }
+
+ private:
+  MessageDialog* dialog_;
+
+  DISALLOW_COPY_AND_ASSIGN(MessageDialogClientView);
+};
+
 ////////////////////////////////////////////////////////////////////////////////
 // MessageDialog, public:
 
@@ -98,12 +119,10 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
                              const std::string& title,
                              const std::string& message,
                              const std::string& detail)
-    : should_close_(false),
-      delete_on_close_(false),
+    : delete_on_close_(false),
       result_(-1),
       title_(base::UTF8ToUTF16(title)),
       parent_(parent_window),
-      widget_(NULL),
       message_box_view_(NULL),
       run_loop_(NULL),
       dialog_scope_(new NativeWindow::DialogScope(parent_window)) {
@@ -136,13 +155,12 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
 
   views::Widget::InitParams params;
   params.delegate = this;
+  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
   params.top_level = true;
-  if (parent_window)
-    params.parent = parent_window->GetNativeWindow();
-
-  // Use bubble style for dialog has a parent.
   if (parent_) {
+    params.parent = parent_->GetNativeWindow();
     params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
+    // Use bubble style for dialog has a parent.
     params.remove_standard_frame = true;
   }
 
@@ -151,7 +169,7 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
   widget_params.remove_standard_frame = true;
 #endif
 
-  widget_ = new views::Widget;
+  widget_.reset(new views::Widget);
   widget_->Init(params);
 
   // Bind to ESC.
@@ -166,6 +184,17 @@ void MessageDialog::Show(base::RunLoop* run_loop) {
   widget_->Show();
 }
 
+void MessageDialog::Close() {
+  dialog_scope_.reset();
+
+  if (delete_on_close_) {
+    callback_.Run(GetResult());
+    base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
+  } else if (run_loop_) {
+    run_loop_->Quit();
+  }
+}
+
 int MessageDialog::GetResult() const {
   // When the dialog is closed without choosing anything, we think the user
   // chose 'Cancel', otherwise we think the default behavior is chosen.
@@ -188,24 +217,12 @@ base::string16 MessageDialog::GetWindowTitle() const {
   return title_;
 }
 
-void MessageDialog::WindowClosing() {
-  should_close_ = true;
-  dialog_scope_.reset();
-
-  if (delete_on_close_) {
-    callback_.Run(GetResult());
-    base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
-  } else if (run_loop_) {
-    run_loop_->Quit();
-  }
-}
-
 views::Widget* MessageDialog::GetWidget() {
-  return widget_;
+  return widget_.get();
 }
 
 const views::Widget* MessageDialog::GetWidget() const {
-  return widget_;
+  return widget_.get();
 }
 
 views::View* MessageDialog::GetContentsView() {
@@ -243,6 +260,10 @@ views::NonClientFrameView* MessageDialog::CreateNonClientFrameView(
   return frame;
 }
 
+views::ClientView* MessageDialog::CreateClientView(views::Widget* widget) {
+  return new MessageDialogClientView(this, widget);
+}
+
 gfx::Size MessageDialog::GetPreferredSize() {
   gfx::Size size(0, buttons_[0]->GetPreferredSize().height());
   for (size_t i = 0; i < buttons_.size(); ++i)