linux: Disable parent window when showing modal dialogs (#6160)

This commit is contained in:
Cheng Zhao 2016-06-21 08:54:55 +00:00 committed by GitHub
parent 00f5fa440d
commit 816b2ea8f2
2 changed files with 20 additions and 13 deletions

View file

@ -4,7 +4,7 @@
#include "atom/browser/ui/file_dialog.h" #include "atom/browser/ui/file_dialog.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window_views.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
@ -40,7 +40,8 @@ class FileChooserDialog {
const std::string& button_label, const std::string& button_label,
const base::FilePath& default_path, const base::FilePath& default_path,
const Filters& filters) const Filters& filters)
: dialog_scope_(parent_window), : parent_(static_cast<atom::NativeWindowViews*>(parent_window)),
dialog_scope_(parent_window),
filters_(filters) { filters_(filters) {
const char* confirm_text = GTK_STOCK_OK; const char* confirm_text = GTK_STOCK_OK;
@ -58,9 +59,10 @@ class FileChooserDialog {
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
confirm_text, GTK_RESPONSE_ACCEPT, confirm_text, GTK_RESPONSE_ACCEPT,
NULL); NULL);
if (parent_window) { if (parent_) {
gfx::NativeWindow window = parent_window->GetNativeWindow(); parent_->SetEnabled(false);
libgtk2ui::SetGtkTransientForAura(dialog_, window); libgtk2ui::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
} }
if (action == GTK_FILE_CHOOSER_ACTION_SAVE) if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
@ -69,8 +71,6 @@ class FileChooserDialog {
if (action != GTK_FILE_CHOOSER_ACTION_OPEN) if (action != GTK_FILE_CHOOSER_ACTION_OPEN)
gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog_), TRUE); gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog_), TRUE);
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
if (!default_path.empty()) { if (!default_path.empty()) {
if (base::DirectoryExists(default_path)) { if (base::DirectoryExists(default_path)) {
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog_), gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog_),
@ -89,6 +89,8 @@ class FileChooserDialog {
virtual ~FileChooserDialog() { virtual ~FileChooserDialog() {
gtk_widget_destroy(dialog_); gtk_widget_destroy(dialog_);
if (parent_)
parent_->SetEnabled(true);
} }
void RunAsynchronous() { void RunAsynchronous() {
@ -143,6 +145,7 @@ class FileChooserDialog {
void AddFilters(const Filters& filters); void AddFilters(const Filters& filters);
base::FilePath AddExtensionForFilename(const gchar* filename) const; base::FilePath AddExtensionForFilename(const gchar* filename) const;
atom::NativeWindowViews* parent_;
atom::NativeWindow::DialogScope dialog_scope_; atom::NativeWindow::DialogScope dialog_scope_;
GtkWidget* dialog_; GtkWidget* dialog_;

View file

@ -5,7 +5,7 @@
#include "atom/browser/ui/message_box.h" #include "atom/browser/ui/message_box.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window_views.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
@ -36,7 +36,8 @@ class GtkMessageBox {
const std::string& detail, const std::string& detail,
const gfx::ImageSkia& icon) const gfx::ImageSkia& icon)
: dialog_scope_(parent_window), : dialog_scope_(parent_window),
cancel_id_(cancel_id) { cancel_id_(cancel_id),
parent_(static_cast<NativeWindowViews*>(parent_window)) {
// Create dialog. // Create dialog.
dialog_ = gtk_message_dialog_new( dialog_ = gtk_message_dialog_new(
nullptr, // parent nullptr, // parent
@ -75,14 +76,17 @@ class GtkMessageBox {
} }
// Parent window. // Parent window.
if (parent_window) { if (parent_) {
gfx::NativeWindow window = parent_window->GetNativeWindow(); parent_->SetEnabled(false);
libgtk2ui::SetGtkTransientForAura(dialog_, window); libgtk2ui::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
} }
} }
~GtkMessageBox() { ~GtkMessageBox() {
gtk_widget_destroy(dialog_); gtk_widget_destroy(dialog_);
if (parent_)
parent_->SetEnabled(true);
} }
GtkMessageType GetMessageType(MessageBoxType type) { GtkMessageType GetMessageType(MessageBoxType type) {
@ -123,7 +127,6 @@ class GtkMessageBox {
} }
int RunSynchronous() { int RunSynchronous() {
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
Show(); Show();
int response = gtk_dialog_run(GTK_DIALOG(dialog_)); int response = gtk_dialog_run(GTK_DIALOG(dialog_));
if (response < 0) if (response < 0)
@ -149,6 +152,7 @@ class GtkMessageBox {
// The id to return when the dialog is closed without pressing buttons. // The id to return when the dialog is closed without pressing buttons.
int cancel_id_; int cancel_id_;
NativeWindowViews* parent_;
GtkWidget* dialog_; GtkWidget* dialog_;
MessageBoxCallback callback_; MessageBoxCallback callback_;