Simplify the code

This commit is contained in:
Cheng Zhao 2015-07-07 17:03:47 +08:00
parent e627592eed
commit 810f14aecb

View file

@ -26,8 +26,7 @@ namespace {
class GtkMessageBox { class GtkMessageBox {
public: public:
GtkMessageBox(bool modal, GtkMessageBox(NativeWindow* parent_window,
NativeWindow* parent_window,
MessageBoxType type, MessageBoxType type,
const std::vector<std::string>& buttons, const std::vector<std::string>& buttons,
const std::string& title, const std::string& title,
@ -39,8 +38,7 @@ class GtkMessageBox {
// Create dialog. // Create dialog.
dialog_ = gtk_message_dialog_new( dialog_ = gtk_message_dialog_new(
nullptr, // parent nullptr, // parent
modal ? GTK_DIALOG_MODAL : // modal dialog static_cast<GtkDialogFlags>(0), // no flags
static_cast<GtkDialogFlags>(0), // no flags
GetMessageType(type), // type GetMessageType(type), // type
GTK_BUTTONS_NONE, // no buttons GTK_BUTTONS_NONE, // no buttons
"%s", message.c_str()); "%s", message.c_str());
@ -115,6 +113,16 @@ class GtkMessageBox {
gtk_window_present_with_time(GTK_WINDOW(dialog_), time); gtk_window_present_with_time(GTK_WINDOW(dialog_), time);
} }
int RunSynchronous() {
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
Show();
int response = gtk_dialog_run(GTK_DIALOG(dialog_));
if (response < 0)
return cancel_id_;
else
return response;
}
void RunAsynchronous(const MessageBoxCallback& callback) { void RunAsynchronous(const MessageBoxCallback& callback) {
callback_ = callback; callback_ = callback;
g_signal_connect(dialog_, "delete-event", g_signal_connect(dialog_, "delete-event",
@ -126,9 +134,6 @@ class GtkMessageBox {
CHROMEGTK_CALLBACK_1(GtkMessageBox, void, OnResponseDialog, int); CHROMEGTK_CALLBACK_1(GtkMessageBox, void, OnResponseDialog, int);
GtkWidget* dialog() const { return dialog_; }
int cancel_id() const { return cancel_id_; }
private: private:
atom::NativeWindow::DialogScope dialog_scope_; atom::NativeWindow::DialogScope dialog_scope_;
@ -160,13 +165,8 @@ int ShowMessageBox(NativeWindow* parent,
const std::string& message, const std::string& message,
const std::string& detail, const std::string& detail,
const gfx::ImageSkia& icon) { const gfx::ImageSkia& icon) {
GtkMessageBox box(true, parent, type, buttons, title, message, detail, icon); return GtkMessageBox(parent, type, buttons, title, message, detail,
box.Show(); icon).RunSynchronous();
int response = gtk_dialog_run(GTK_DIALOG(box.dialog()));
if (response < 0)
return box.cancel_id();
else
return response;
} }
void ShowMessageBox(NativeWindow* parent, void ShowMessageBox(NativeWindow* parent,
@ -177,19 +177,16 @@ void ShowMessageBox(NativeWindow* parent,
const std::string& detail, const std::string& detail,
const gfx::ImageSkia& icon, const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) { const MessageBoxCallback& callback) {
auto box = new GtkMessageBox( (new GtkMessageBox(parent, type, buttons, title, message, detail,
false, parent, type, buttons, title, message, detail, icon); icon))->RunAsynchronous(callback);
box->RunAsynchronous(callback);
} }
void ShowErrorBox(const base::string16& title, const base::string16& content) { void ShowErrorBox(const base::string16& title, const base::string16& content) {
if (Browser::Get()->is_ready()) { if (Browser::Get()->is_ready()) {
GtkMessageBox box(true, nullptr, MESSAGE_BOX_TYPE_ERROR, { "OK" }, "Error", GtkMessageBox(nullptr, MESSAGE_BOX_TYPE_ERROR, { "OK" }, "Error",
base::UTF16ToUTF8(title).c_str(), base::UTF16ToUTF8(title).c_str(),
base::UTF16ToUTF8(content).c_str(), base::UTF16ToUTF8(content).c_str(),
gfx::ImageSkia()); gfx::ImageSkia()).RunSynchronous();
box.Show();
gtk_dialog_run(GTK_DIALOG(box.dialog()));
} else { } else {
fprintf(stderr, fprintf(stderr,
ANSI_TEXT_BOLD ANSI_BACKGROUND_GRAY ANSI_TEXT_BOLD ANSI_BACKGROUND_GRAY