Mac: Fix file dialog behavior (issue #1919)

This commit is contained in:
Marshall Greenblatt 2016-06-15 13:43:58 -04:00
parent f5b910326d
commit 262e327c74
1 changed files with 35 additions and 47 deletions

View File

@ -235,8 +235,8 @@ namespace {
void RunOpenFileDialog( void RunOpenFileDialog(
const CefFileDialogRunner::FileChooserParams& params, const CefFileDialogRunner::FileChooserParams& params,
NSView* view, NSView* view,
int* filter_index, int filter_index,
std::vector<base::FilePath>* files) { CefFileDialogRunner::RunFileChooserCallback callback) {
NSOpenPanel* openPanel = [NSOpenPanel openPanel]; NSOpenPanel* openPanel = [NSOpenPanel openPanel];
base::string16 title; base::string16 title;
@ -278,7 +278,7 @@ void RunOpenFileDialog(
filter_delegate = filter_delegate =
[[CefFilterDelegate alloc] initWithPanel:openPanel [[CefFilterDelegate alloc] initWithPanel:openPanel
andAcceptFilters:params.accept_types andAcceptFilters:params.accept_types
andFilterIndex:*filter_index]; andFilterIndex:filter_index];
} }
// Further panel configuration. // Further panel configuration.
@ -294,28 +294,27 @@ void RunOpenFileDialog(
// Show panel. // Show panel.
[openPanel beginSheetModalForWindow:[view window] [openPanel beginSheetModalForWindow:[view window]
completionHandler:^(NSInteger returnCode) { completionHandler:^(NSInteger returnCode) {
[NSApp stopModalWithCode:returnCode]; int filter_index_to_use =
(filter_delegate != nil) ? [filter_delegate filter] : filter_index;
if (returnCode == NSFileHandlingPanelOKButton) {
std::vector<base::FilePath> files;
files.reserve(openPanel.URLs.count);
for (NSURL* url in openPanel.URLs) {
if (url.isFileURL)
files.push_back(base::FilePath(url.path.UTF8String));
}
callback.Run(filter_index_to_use, files);
} else {
callback.Run(filter_index_to_use, std::vector<base::FilePath>());
}
}]; }];
NSInteger result = [NSApp runModalForWindow:[view window]];
if (result == NSFileHandlingPanelOKButton) {
NSArray *urls = [openPanel URLs];
int i, count = [urls count];
for (i=0; i<count; i++) {
NSURL* url = [urls objectAtIndex:i];
if ([url isFileURL])
files->push_back(base::FilePath(base::SysNSStringToUTF8([url path])));
}
}
if (filter_delegate != nil)
*filter_index = [filter_delegate filter];
} }
bool RunSaveFileDialog( void RunSaveFileDialog(
const CefFileDialogRunner::FileChooserParams& params, const CefFileDialogRunner::FileChooserParams& params,
NSView* view, NSView* view,
int* filter_index, int filter_index,
base::FilePath* file) { CefFileDialogRunner::RunFileChooserCallback callback) {
NSSavePanel* savePanel = [NSSavePanel savePanel]; NSSavePanel* savePanel = [NSSavePanel savePanel];
base::string16 title; base::string16 title;
@ -350,31 +349,26 @@ bool RunSaveFileDialog(
filter_delegate = filter_delegate =
[[CefFilterDelegate alloc] initWithPanel:savePanel [[CefFilterDelegate alloc] initWithPanel:savePanel
andAcceptFilters:params.accept_types andAcceptFilters:params.accept_types
andFilterIndex:*filter_index]; andFilterIndex:filter_index];
} }
[savePanel setAllowsOtherFileTypes:YES]; [savePanel setAllowsOtherFileTypes:YES];
[savePanel setShowsHiddenFiles:!params.hidereadonly]; [savePanel setShowsHiddenFiles:!params.hidereadonly];
bool success = false;
// Show panel. // Show panel.
[savePanel beginSheetModalForWindow:[view window] [savePanel beginSheetModalForWindow:view.window
completionHandler:^(NSInteger resultCode) { completionHandler:^(NSInteger resultCode) {
[NSApp stopModalWithCode:resultCode]; int filter_index_to_use =
}]; (filter_delegate != nil) ? [filter_delegate filter] : filter_index;
NSInteger result = [NSApp runModalForWindow:[view window]]; if (resultCode == NSFileHandlingPanelOKButton) {
if (result == NSFileHandlingPanelOKButton) { NSURL* url = savePanel.URL;
NSURL* url = [savePanel URL]; const char* path = url.path.UTF8String;
NSString* path = [url path]; std::vector<base::FilePath> files(1, base::FilePath(path));
*file = base::FilePath([path UTF8String]); callback.Run(filter_index_to_use, files);
success = true; } else {
callback.Run(filter_index_to_use, std::vector<base::FilePath>());
} }
}];
if (filter_delegate != nil)
*filter_index = [filter_delegate filter];
return success;
} }
} // namespace } // namespace
@ -385,22 +379,16 @@ CefFileDialogRunnerMac::CefFileDialogRunnerMac() {
void CefFileDialogRunnerMac::Run(CefBrowserHostImpl* browser, void CefFileDialogRunnerMac::Run(CefBrowserHostImpl* browser,
const FileChooserParams& params, const FileChooserParams& params,
RunFileChooserCallback callback) { RunFileChooserCallback callback) {
std::vector<base::FilePath> files;
int filter_index = params.selected_accept_filter; int filter_index = params.selected_accept_filter;
NSView* owner = browser->GetWindowHandle(); NSView* owner = browser->GetWindowHandle();
if (params.mode == content::FileChooserParams::Open || if (params.mode == content::FileChooserParams::Open ||
params.mode == content::FileChooserParams::OpenMultiple || params.mode == content::FileChooserParams::OpenMultiple ||
params.mode == content::FileChooserParams::UploadFolder) { params.mode == content::FileChooserParams::UploadFolder) {
RunOpenFileDialog(params, owner, &filter_index, &files); RunOpenFileDialog(params, owner, filter_index, callback);
} else if (params.mode == content::FileChooserParams::Save) { } else if (params.mode == content::FileChooserParams::Save) {
base::FilePath file; RunSaveFileDialog(params, owner, filter_index, callback);
if (RunSaveFileDialog(params, owner, &filter_index, &file)) {
files.push_back(file);
}
} else { } else {
NOTIMPLEMENTED(); NOTIMPLEMENTED();
} }
callback.Run(filter_index, files);
} }