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;
@ -277,8 +277,8 @@ void RunOpenFileDialog(
// Add the file filter control. // Add the file filter control.
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;
NSInteger result = [NSApp runModalForWindow:[view window]]; if (returnCode == NSFileHandlingPanelOKButton) {
if (result == NSFileHandlingPanelOKButton) { std::vector<base::FilePath> files;
NSArray *urls = [openPanel URLs]; files.reserve(openPanel.URLs.count);
int i, count = [urls count]; for (NSURL* url in openPanel.URLs) {
for (i=0; i<count; i++) { if (url.isFileURL)
NSURL* url = [urls objectAtIndex:i]; files.push_back(base::FilePath(url.path.UTF8String));
if ([url isFileURL]) }
files->push_back(base::FilePath(base::SysNSStringToUTF8([url path]))); callback.Run(filter_index_to_use, files);
} else {
callback.Run(filter_index_to_use, std::vector<base::FilePath>());
} }
} }];
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;
@ -349,32 +348,27 @@ bool RunSaveFileDialog(
// Add the file filter control. // Add the file filter control.
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;
if (resultCode == NSFileHandlingPanelOKButton) {
NSURL* url = savePanel.URL;
const char* path = url.path.UTF8String;
std::vector<base::FilePath> files(1, base::FilePath(path));
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) {
NSURL* url = [savePanel URL];
NSString* path = [url path];
*file = base::FilePath([path UTF8String]);
success = true;
}
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);
} }