From: Cheng Zhao Date: Mon, 23 Sep 2013 11:22:36 +0000 (+0800) Subject: mac: Add asynchronous ShowOpenDialog. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d3dd2b43327c7d2b6829a488b4c9a690fa492b9a;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git mac: Add asynchronous ShowOpenDialog. --- diff --git a/browser/ui/file_dialog.h b/browser/ui/file_dialog.h index 77e5ac4..250ba5c 100644 --- a/browser/ui/file_dialog.h +++ b/browser/ui/file_dialog.h @@ -8,6 +8,7 @@ #include #include +#include "base/callback_forward.h" #include "base/files/file_path.h" namespace atom { @@ -23,12 +24,21 @@ enum FileDialogProperty { FILE_DIALOG_CREATE_DIRECTORY = 8, }; +typedef base::Callback paths)> OpenDialogCallback; + bool ShowOpenDialog(atom::NativeWindow* parent_window, const std::string& title, const base::FilePath& default_path, int properties, std::vector* 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, const std::string& title, const base::FilePath& default_path, diff --git a/browser/ui/file_dialog_mac.mm b/browser/ui/file_dialog_mac.mm index 54a901c..539fe91 100644 --- a/browser/ui/file_dialog_mac.mm +++ b/browser/ui/file_dialog_mac.mm @@ -41,6 +41,23 @@ void SetupDialog(NSSavePanel* dialog, [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* paths) { + NSArray* urls = [dialog URLs]; + for (NSURL* url in urls) + if ([url isFileURL]) + paths->push_back(base::FilePath(base::SysNSStringToUTF8([url path]))); +} + } // namespace bool ShowOpenDialog(atom::NativeWindow* parent_window, @@ -52,14 +69,7 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window, NSOpenPanel* dialog = [NSOpenPanel openPanel]; SetupDialog(dialog, title, default_path); - - [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]; + SetupDialogForProperties(dialog, properties); __block int chosen = -1; @@ -79,14 +89,37 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window, if (chosen == NSFileHandlingPanelCancelButton) return false; - NSArray* urls = [dialog URLs]; - for (NSURL* url in urls) - if ([url isFileURL]) - paths->push_back(base::FilePath(base::SysNSStringToUTF8([url path]))); - + ReadDialogPaths(dialog, paths); 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()); + } else { + std::vector paths; + ReadDialogPaths(dialog, &paths); + callback.Run(true, paths); + } + }]; +} + bool ShowSaveDialog(atom::NativeWindow* window, const std::string& title, const base::FilePath& default_path,