win: Implement asynchronous ShowMessageBox.
This commit is contained in:
parent
4000734504
commit
085f0a2544
1 changed files with 42 additions and 16 deletions
|
@ -39,7 +39,14 @@ class MessageDialog : public base::MessageLoop::Dispatcher,
|
||||||
const std::string& detail);
|
const std::string& detail);
|
||||||
virtual ~MessageDialog();
|
virtual ~MessageDialog();
|
||||||
|
|
||||||
int result() const { return result_; }
|
void Show();
|
||||||
|
|
||||||
|
int GetResult() const;
|
||||||
|
|
||||||
|
void set_callback(const MessageBoxCallback& callback) {
|
||||||
|
delete_on_close_ = true;
|
||||||
|
callback_ = callback;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Overridden from MessageLoop::Dispatcher:
|
// Overridden from MessageLoop::Dispatcher:
|
||||||
|
@ -64,11 +71,13 @@ class MessageDialog : public base::MessageLoop::Dispatcher,
|
||||||
const ui::Event& event) OVERRIDE;
|
const ui::Event& event) OVERRIDE;
|
||||||
|
|
||||||
bool should_close_;
|
bool should_close_;
|
||||||
|
bool delete_on_close_;
|
||||||
int result_;
|
int result_;
|
||||||
string16 title_;
|
string16 title_;
|
||||||
views::Widget* widget_;
|
views::Widget* widget_;
|
||||||
views::MessageBoxView* message_box_view_;
|
views::MessageBoxView* message_box_view_;
|
||||||
std::vector<views::LabelButton*> buttons_;
|
std::vector<views::LabelButton*> buttons_;
|
||||||
|
MessageBoxCallback callback_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(MessageDialog);
|
DISALLOW_COPY_AND_ASSIGN(MessageDialog);
|
||||||
};
|
};
|
||||||
|
@ -83,6 +92,7 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
|
||||||
const std::string& message,
|
const std::string& message,
|
||||||
const std::string& detail)
|
const std::string& detail)
|
||||||
: should_close_(false),
|
: should_close_(false),
|
||||||
|
delete_on_close_(false),
|
||||||
result_(-1),
|
result_(-1),
|
||||||
title_(UTF8ToUTF16(title)),
|
title_(UTF8ToUTF16(title)),
|
||||||
widget_(NULL),
|
widget_(NULL),
|
||||||
|
@ -125,12 +135,30 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
|
||||||
|
|
||||||
set_background(views::Background::CreateSolidBackground(
|
set_background(views::Background::CreateSolidBackground(
|
||||||
skia::COLORREFToSkColor(GetSysColor(COLOR_WINDOW))));
|
skia::COLORREFToSkColor(GetSysColor(COLOR_WINDOW))));
|
||||||
widget_->Show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageDialog::~MessageDialog() {
|
MessageDialog::~MessageDialog() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MessageDialog::Show() {
|
||||||
|
widget_->Show();
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
if (result_ == -1) {
|
||||||
|
for (size_t i = 0; i < buttons_.size(); ++i)
|
||||||
|
if (LowerCaseEqualsASCII(buttons_[i]->GetText(), "cancel")) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return result_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// MessageDialog, private:
|
// MessageDialog, private:
|
||||||
|
|
||||||
|
@ -146,6 +174,11 @@ string16 MessageDialog::GetWindowTitle() const {
|
||||||
|
|
||||||
void MessageDialog::WindowClosing() {
|
void MessageDialog::WindowClosing() {
|
||||||
should_close_ = true;
|
should_close_ = true;
|
||||||
|
|
||||||
|
if (delete_on_close_) {
|
||||||
|
callback_.Run(GetResult());
|
||||||
|
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
views::Widget* MessageDialog::GetWidget() {
|
views::Widget* MessageDialog::GetWidget() {
|
||||||
|
@ -233,6 +266,7 @@ int ShowMessageBox(NativeWindow* parent_window,
|
||||||
const std::string& message,
|
const std::string& message,
|
||||||
const std::string& detail) {
|
const std::string& detail) {
|
||||||
MessageDialog dialog(parent_window, type, buttons, title, message, detail);
|
MessageDialog dialog(parent_window, type, buttons, title, message, detail);
|
||||||
|
dialog.Show();
|
||||||
{
|
{
|
||||||
base::MessageLoop::ScopedNestableTaskAllower allow(
|
base::MessageLoop::ScopedNestableTaskAllower allow(
|
||||||
base::MessageLoopForUI::current());
|
base::MessageLoopForUI::current());
|
||||||
|
@ -240,18 +274,7 @@ int ShowMessageBox(NativeWindow* parent_window,
|
||||||
run_loop.Run();
|
run_loop.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the dialog is closed without choosing anything, we think the user
|
return dialog.GetResult();
|
||||||
// chose 'Cancel', otherwise we think the default behavior is chosen.
|
|
||||||
if (dialog.result() == -1) {
|
|
||||||
for (size_t i = 0; i < buttons.size(); ++i)
|
|
||||||
if (LowerCaseEqualsASCII(buttons[i], "cancel")) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return dialog.result();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShowMessageBox(NativeWindow* parent_window,
|
void ShowMessageBox(NativeWindow* parent_window,
|
||||||
|
@ -261,8 +284,11 @@ void ShowMessageBox(NativeWindow* parent_window,
|
||||||
const std::string& message,
|
const std::string& message,
|
||||||
const std::string& detail,
|
const std::string& detail,
|
||||||
const MessageBoxCallback& callback) {
|
const MessageBoxCallback& callback) {
|
||||||
callback.Run(ShowMessageBox(
|
// The dialog would be deleted when the dialog is closed.
|
||||||
parent_window, type, buttons, title, message, detail));
|
MessageDialog* dialog = new MessageDialog(
|
||||||
|
parent_window, type, buttons, title, message, detail);
|
||||||
|
dialog->set_callback(callback);
|
||||||
|
dialog->Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
Loading…
Reference in a new issue