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

@ -44,25 +44,23 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
}
void SetupDialog(NSSavePanel* dialog,
const std::string& title,
const std::string& button_label,
const base::FilePath& default_path,
const Filters& filters) {
if (!title.empty())
[dialog setTitle:base::SysUTF8ToNSString(title)];
const DialogSettings& settings) {
if (!settings.title.empty())
[dialog setTitle:base::SysUTF8ToNSString(settings.title)];
if (!button_label.empty())
[dialog setPrompt:base::SysUTF8ToNSString(button_label)];
if (!settings.button_label.empty())
[dialog setPrompt:base::SysUTF8ToNSString(settings.button_label)];
NSString* default_dir = nil;
NSString* default_filename = nil;
if (!default_path.empty()) {
if (base::DirectoryExists(default_path)) {
default_dir = base::SysUTF8ToNSString(default_path.value());
if (!settings.default_path.empty()) {
if (base::DirectoryExists(settings.default_path)) {
default_dir = base::SysUTF8ToNSString(settings.default_path.value());
} else {
default_dir = base::SysUTF8ToNSString(default_path.DirName().value());
default_dir =
base::SysUTF8ToNSString(settings.default_path.DirName().value());
default_filename =
base::SysUTF8ToNSString(default_path.BaseName().value());
base::SysUTF8ToNSString(settings.default_path.BaseName().value());
}
}
@ -71,10 +69,10 @@ void SetupDialog(NSSavePanel* dialog,
if (default_filename)
[dialog setNameFieldStringValue:default_filename];
if (filters.empty())
if (settings.filters.empty())
[dialog setAllowsOtherFileTypes:YES];
else
SetAllowedFileTypes(dialog, filters);
SetAllowedFileTypes(dialog, settings.filters);
}
void SetupDialogForProperties(NSOpenPanel* dialog, int properties) {
@ -117,20 +115,15 @@ void ReadDialogPaths(NSOpenPanel* dialog, std::vector<base::FilePath>* paths) {
} // 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) {
DCHECK(paths);
NSOpenPanel* dialog = [NSOpenPanel openPanel];
SetupDialog(dialog, title, button_label, default_path, filters);
SetupDialogForProperties(dialog, properties);
SetupDialog(dialog, settings);
SetupDialogForProperties(dialog, settings.properties);
int chosen = RunModalDialog(dialog, parent_window);
int chosen = RunModalDialog(dialog, settings.parent_window);
if (chosen == NSFileHandlingPanelCancelButton)
return false;
@ -138,23 +131,20 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window,
return true;
}
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& c) {
NSOpenPanel* dialog = [NSOpenPanel openPanel];
SetupDialog(dialog, title, button_label, default_path, filters);
SetupDialogForProperties(dialog, properties);
SetupDialog(dialog, settings);
SetupDialogForProperties(dialog, settings.properties);
// Duplicate the callback object here since c is a reference and gcd would
// only store the pointer, by duplication we can force gcd to store a copy.
__block OpenDialogCallback callback = c;
NSWindow* window = parent_window ? parent_window->GetNativeWindow() : NULL;
NSWindow* window = settings.parent_window ?
settings.parent_window->GetNativeWindow() :
NULL;
[dialog beginSheetModalForWindow:window
completionHandler:^(NSInteger chosen) {
if (chosen == NSFileHandlingPanelCancelButton) {
@ -167,18 +157,14 @@ void ShowOpenDialog(atom::NativeWindow* parent_window,
}];
}
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) {
DCHECK(path);
NSSavePanel* dialog = [NSSavePanel savePanel];
SetupDialog(dialog, title, button_label, default_path, filters);
SetupDialog(dialog, settings);
int chosen = RunModalDialog(dialog, parent_window);
int chosen = RunModalDialog(dialog, settings.parent_window);
if (chosen == NSFileHandlingPanelCancelButton || ![[dialog URL] isFileURL])
return false;
@ -186,20 +172,18 @@ bool ShowSaveDialog(atom::NativeWindow* parent_window,
return true;
}
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& c) {
NSSavePanel* dialog = [NSSavePanel savePanel];
SetupDialog(dialog, title, button_label, default_path, filters);
SetupDialog(dialog, settings);
[dialog setCanSelectHiddenExtension:YES];
__block SaveDialogCallback callback = c;
NSWindow* window = parent_window ? parent_window->GetNativeWindow() : NULL;
NSWindow* window = settings.parent_window ?
settings.parent_window->GetNativeWindow() :
NULL;
[dialog beginSheetModalForWindow:window
completionHandler:^(NSInteger chosen) {
if (chosen == NSFileHandlingPanelCancelButton) {