mac: Add asynchronous ShowOpenDialog.
This commit is contained in:
parent
7e86ee37f3
commit
d3dd2b4332
2 changed files with 56 additions and 13 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "base/callback_forward.h"
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -23,12 +24,21 @@ enum FileDialogProperty {
|
||||||
FILE_DIALOG_CREATE_DIRECTORY = 8,
|
FILE_DIALOG_CREATE_DIRECTORY = 8,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef base::Callback<void(
|
||||||
|
bool result, std::vector<base::FilePath> paths)> OpenDialogCallback;
|
||||||
|
|
||||||
bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
const base::FilePath& default_path,
|
const base::FilePath& default_path,
|
||||||
int properties,
|
int properties,
|
||||||
std::vector<base::FilePath>* paths);
|
std::vector<base::FilePath>* paths);
|
||||||
|
|
||||||
|
void ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
|
const std::string& title,
|
||||||
|
const base::FilePath& default_path,
|
||||||
|
int properties,
|
||||||
|
const OpenDialogCallback& callback);
|
||||||
|
|
||||||
bool ShowSaveDialog(atom::NativeWindow* parent_window,
|
bool ShowSaveDialog(atom::NativeWindow* parent_window,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
const base::FilePath& default_path,
|
const base::FilePath& default_path,
|
||||||
|
|
|
@ -41,6 +41,23 @@ void SetupDialog(NSSavePanel* dialog,
|
||||||
[dialog setAllowsOtherFileTypes:YES];
|
[dialog setAllowsOtherFileTypes:YES];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetupDialogForProperties(NSOpenPanel* dialog, int properties) {
|
||||||
|
[dialog setCanChooseFiles:(properties & FILE_DIALOG_OPEN_FILE)];
|
||||||
|
if (properties & FILE_DIALOG_OPEN_DIRECTORY)
|
||||||
|
[dialog setCanChooseDirectories:YES];
|
||||||
|
if (properties & FILE_DIALOG_CREATE_DIRECTORY)
|
||||||
|
[dialog setCanCreateDirectories:YES];
|
||||||
|
if (properties & FILE_DIALOG_MULTI_SELECTIONS)
|
||||||
|
[dialog setAllowsMultipleSelection:YES];
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadDialogPaths(NSOpenPanel* dialog, std::vector<base::FilePath>* paths) {
|
||||||
|
NSArray* urls = [dialog URLs];
|
||||||
|
for (NSURL* url in urls)
|
||||||
|
if ([url isFileURL])
|
||||||
|
paths->push_back(base::FilePath(base::SysNSStringToUTF8([url path])));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
|
@ -52,14 +69,7 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
NSOpenPanel* dialog = [NSOpenPanel openPanel];
|
NSOpenPanel* dialog = [NSOpenPanel openPanel];
|
||||||
|
|
||||||
SetupDialog(dialog, title, default_path);
|
SetupDialog(dialog, title, default_path);
|
||||||
|
SetupDialogForProperties(dialog, properties);
|
||||||
[dialog setCanChooseFiles:(properties & FILE_DIALOG_OPEN_FILE)];
|
|
||||||
if (properties & FILE_DIALOG_OPEN_DIRECTORY)
|
|
||||||
[dialog setCanChooseDirectories:YES];
|
|
||||||
if (properties & FILE_DIALOG_CREATE_DIRECTORY)
|
|
||||||
[dialog setCanCreateDirectories:YES];
|
|
||||||
if (properties & FILE_DIALOG_MULTI_SELECTIONS)
|
|
||||||
[dialog setAllowsMultipleSelection:YES];
|
|
||||||
|
|
||||||
__block int chosen = -1;
|
__block int chosen = -1;
|
||||||
|
|
||||||
|
@ -79,14 +89,37 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
if (chosen == NSFileHandlingPanelCancelButton)
|
if (chosen == NSFileHandlingPanelCancelButton)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
NSArray* urls = [dialog URLs];
|
ReadDialogPaths(dialog, paths);
|
||||||
for (NSURL* url in urls)
|
|
||||||
if ([url isFileURL])
|
|
||||||
paths->push_back(base::FilePath(base::SysNSStringToUTF8([url path])));
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
|
const std::string& title,
|
||||||
|
const base::FilePath& default_path,
|
||||||
|
int properties,
|
||||||
|
const OpenDialogCallback& c) {
|
||||||
|
NSOpenPanel* dialog = [NSOpenPanel openPanel];
|
||||||
|
|
||||||
|
SetupDialog(dialog, title, default_path);
|
||||||
|
SetupDialogForProperties(dialog, 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;
|
||||||
|
[dialog beginSheetModalForWindow:window
|
||||||
|
completionHandler:^(NSInteger chosen) {
|
||||||
|
if (chosen == NSFileHandlingPanelCancelButton) {
|
||||||
|
callback.Run(false, std::vector<base::FilePath>());
|
||||||
|
} else {
|
||||||
|
std::vector<base::FilePath> paths;
|
||||||
|
ReadDialogPaths(dialog, &paths);
|
||||||
|
callback.Run(true, paths);
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
bool ShowSaveDialog(atom::NativeWindow* window,
|
bool ShowSaveDialog(atom::NativeWindow* window,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
const base::FilePath& default_path,
|
const base::FilePath& default_path,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue