Add DialogSettings helper struct

This commit is contained in:
Kevin Sawicki 2017-02-07 17:32:58 -08:00
parent e7410976f0
commit 867bb5a94e
9 changed files with 151 additions and 235 deletions

View file

@ -36,24 +36,20 @@ void OnFileFilterDataDestroyed(std::string* file_extension) {
class FileChooserDialog {
public:
FileChooserDialog(GtkFileChooserAction action,
atom::NativeWindow* parent_window,
const std::string& title,
const std::string& button_label,
const base::FilePath& default_path,
const Filters& filters)
: parent_(static_cast<atom::NativeWindowViews*>(parent_window)),
filters_(filters) {
const DialogSettings& settings)
: parent_(static_cast<atom::NativeWindowViews*>(settings.parent_window)),
filters_(settings.filters) {
const char* confirm_text = GTK_STOCK_OK;
if (!button_label.empty())
confirm_text = button_label.c_str();
if (!settings.button_label.empty())
confirm_text = settings.button_label.c_str();
else if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
confirm_text = GTK_STOCK_SAVE;
else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
confirm_text = GTK_STOCK_OPEN;
dialog_ = gtk_file_chooser_dialog_new(
title.c_str(),
settings.title.c_str(),
NULL,
action,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
@ -71,20 +67,20 @@ class FileChooserDialog {
if (action != GTK_FILE_CHOOSER_ACTION_OPEN)
gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog_), TRUE);
if (!default_path.empty()) {
if (base::DirectoryExists(default_path)) {
if (!settings.default_path.empty()) {
if (base::DirectoryExists(settings.default_path)) {
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog_),
default_path.value().c_str());
settings.default_path.value().c_str());
} else {
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog_),
default_path.DirName().value().c_str());
settings.default_path.DirName().value().c_str());
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog_),
default_path.BaseName().value().c_str());
settings.default_path.BaseName().value().c_str());
}
}
if (!filters.empty())
AddFilters(filters);
if (!settings.filters.empty())
AddFilters(settings.filters);
}
~FileChooserDialog() {
@ -230,19 +226,13 @@ base::FilePath FileChooserDialog::AddExtensionForFilename(
} // namespace
bool ShowOpenDialog(atom::NativeWindow* parent_window,
const std::string& title,
const std::string& button_label,
const base::FilePath& default_path,
const Filters& filters,
int properties,
bool ShowOpenDialog(const DialogSettings& settings,
std::vector<base::FilePath>* paths) {
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
if (properties & FILE_DIALOG_OPEN_DIRECTORY)
if (settings.properties & FILE_DIALOG_OPEN_DIRECTORY)
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
FileChooserDialog open_dialog(action, parent_window, title, button_label,
default_path, filters);
open_dialog.SetupProperties(properties);
FileChooserDialog open_dialog(action, settings);
open_dialog.SetupProperties(settings.properties);
gtk_widget_show_all(open_dialog.dialog());
int response = gtk_dialog_run(GTK_DIALOG(open_dialog.dialog()));
@ -254,30 +244,19 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window,
}
}
void ShowOpenDialog(atom::NativeWindow* parent_window,
const std::string& title,
const std::string& button_label,
const base::FilePath& default_path,
const Filters& filters,
int properties,
void ShowOpenDialog(const DialogSettings& settings,
const OpenDialogCallback& callback) {
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
if (properties & FILE_DIALOG_OPEN_DIRECTORY)
if (settings.properties & FILE_DIALOG_OPEN_DIRECTORY)
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
FileChooserDialog* open_dialog = new FileChooserDialog(
action, parent_window, title, button_label, default_path, filters);
open_dialog->SetupProperties(properties);
FileChooserDialog* open_dialog = new FileChooserDialog(action, settings);
open_dialog->SetupProperties(settings.properties);
open_dialog->RunOpenAsynchronous(callback);
}
bool ShowSaveDialog(atom::NativeWindow* parent_window,
const std::string& title,
const std::string& button_label,
const base::FilePath& default_path,
const Filters& filters,
bool ShowSaveDialog(const DialogSettings& settings,
base::FilePath* path) {
FileChooserDialog save_dialog(GTK_FILE_CHOOSER_ACTION_SAVE, parent_window,
title, button_label, default_path, filters);
FileChooserDialog save_dialog(GTK_FILE_CHOOSER_ACTION_SAVE, settings);
gtk_widget_show_all(save_dialog.dialog());
int response = gtk_dialog_run(GTK_DIALOG(save_dialog.dialog()));
if (response == GTK_RESPONSE_ACCEPT) {
@ -288,15 +267,10 @@ bool ShowSaveDialog(atom::NativeWindow* parent_window,
}
}
void ShowSaveDialog(atom::NativeWindow* parent_window,
const std::string& title,
const std::string& button_label,
const base::FilePath& default_path,
const Filters& filters,
void ShowSaveDialog(const DialogSettings& settings,
const SaveDialogCallback& callback) {
FileChooserDialog* save_dialog = new FileChooserDialog(
GTK_FILE_CHOOSER_ACTION_SAVE, parent_window, title, button_label,
default_path, filters);
GTK_FILE_CHOOSER_ACTION_SAVE, settings);
save_dialog->RunSaveAsynchronous(callback);
}