From 23e3882a24846dfbae2684e23d662fe3d080680e Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Sat, 22 Aug 2015 17:10:51 -0700 Subject: [PATCH] Update the FileDialog API, improve --- lime/ui/FileDialog.hx | 118 +++++++++++++++++------------- lime/ui/FileDialogType.hx | 10 +++ project/include/ui/FileDialog.h | 4 +- project/src/ExternalInterface.cpp | 2 +- project/src/ui/FileDialog.cpp | 2 +- 5 files changed, 83 insertions(+), 53 deletions(-) create mode 100644 lime/ui/FileDialogType.hx diff --git a/lime/ui/FileDialog.hx b/lime/ui/FileDialog.hx index 0338464e2..4b3122b95 100644 --- a/lime/ui/FileDialog.hx +++ b/lime/ui/FileDialog.hx @@ -1,6 +1,8 @@ package lime.ui; +import lime.app.Event; +import lime.system.BackgroundWorker; import lime.system.System; import lime.utils.Resource; @@ -12,73 +14,91 @@ import sys.io.File; class FileDialog { - /** - * Open an "Open File" dialog window, and browse for an individual file - * @param filter (Optional) A file filter to use, such as "png,jpg;txt", "txt" or null (Default: null) - * @param defaultPath (Optional) The default path to use when browsing - * @return The selected file path or null if the dialog was canceled - */ - public static function openFile (filter = null, defaultPath:String = null):String { + public var onCancel = new EventVoid> (); + public var onSelect = new EventVoid> (); + public var onSelectMultiple = new Event->Void> (); + public var type (default, null):FileDialogType; + + + public function new (type:FileDialogType) { - #if (cpp || neko || nodejs) - return lime_file_dialog_open_file (filter, defaultPath); - #else - return null; - #end + this.type = type; } - /** - * Open an "Open File" dialog window, and browse for one or multiple files - * @param filter (Optional) A file filter to use, such as "png,jpg;txt", "txt" or null (Default: null) - * @param defaultPath (Optional) The default path to use when browsing - * @return An list of the file paths that were selected - */ - public static function openFiles (filter = null, defaultPath:String = null):Array { + public function browse (filter:String = null, defaultPath:String = null):Bool { - #if (cpp || neko || nodejs) - return lime_file_dialog_open_files (filter, defaultPath); - #else - return []; - #end + #if desktop - } - - - /** - * Open a "Save File" dialog and save the specified String or Bytes data - * @param data String or Bytes data to save - * @param filter (Optional) A file filter to use, such as "png,jpg;txt", "txt" or null (Default: null) - * @param defaultPath (Optional) The default path to use when browsing - * @return The path of the saved file, or null if the data was not saved - */ - public static function saveFile (data:Resource, filter = null, defaultPath:String = null):String { + var worker = new BackgroundWorker (); - var path = null; - - #if (cpp || neko || nodejs) - - path = lime_file_dialog_save_file (filter, defaultPath); - - if (path != null) { + worker.doWork.add (function (_) { - try { + switch (type) { - File.saveBytes (path, data); + case OPEN: + + worker.onComplete.dispatch (lime_file_dialog_open_file (filter, defaultPath)); - } catch (e:Dynamic) { + case OPEN_MULTIPLE: + + worker.onComplete.dispatch (lime_file_dialog_open_files (filter, defaultPath)); - path = null; + case SAVE: + + worker.onComplete.dispatch (lime_file_dialog_open_files (filter, defaultPath)); } - } + }); + + worker.onComplete.add (function (result) { + + switch (type) { + + case OPEN, SAVE: + + var path:String = cast result; + + if (path == null) { + + onSelect.dispatch (path); + + } else { + + onCancel.dispatch (); + + } + + case OPEN_MULTIPLE: + + var paths:Array = cast result; + + if (paths != null && paths.length > 0) { + + onSelectMultiple.dispatch (paths); + + } else { + + onCancel.dispatch (); + + } + + } + + }); + + worker.run (); + return true; + + #else + + onCancel.dispatch (); + return false; #end - return path; - } diff --git a/lime/ui/FileDialogType.hx b/lime/ui/FileDialogType.hx new file mode 100644 index 000000000..de9bfc0aa --- /dev/null +++ b/lime/ui/FileDialogType.hx @@ -0,0 +1,10 @@ +package lime.ui; + + +enum FileDialogType { + + OPEN; + OPEN_MULTIPLE; + SAVE; + +} \ No newline at end of file diff --git a/project/include/ui/FileDialog.h b/project/include/ui/FileDialog.h index ea44b3aea..a40c61777 100644 --- a/project/include/ui/FileDialog.h +++ b/project/include/ui/FileDialog.h @@ -2,7 +2,7 @@ #define LIME_UI_FILE_DIALOG_H -#include +#include namespace lime { @@ -13,7 +13,7 @@ namespace lime { public: static const char* OpenFile (const char* filter = NULL, const char* defaultPath = NULL); - static void OpenFiles (QuickVec* files, const char* filter = NULL, const char* defaultPath = NULL); + static void OpenFiles (std::vector* files, const char* filter = NULL, const char* defaultPath = NULL); static const char* SaveFile (const char* filter = NULL, const char* defaultPath = NULL); }; diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index ba415c3f5..21dc2c78d 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -209,7 +209,7 @@ namespace lime { value lime_file_dialog_open_files (value filter, value defaultPath) { #ifdef LIME_NFD - QuickVec files; + std::vector files; FileDialog::OpenFiles (&files, val_string (filter), val_string (defaultPath)); value result = alloc_array (files.size ()); diff --git a/project/src/ui/FileDialog.cpp b/project/src/ui/FileDialog.cpp index 01b3750d3..4d6211a70 100644 --- a/project/src/ui/FileDialog.cpp +++ b/project/src/ui/FileDialog.cpp @@ -34,7 +34,7 @@ namespace lime { } - void FileDialog::OpenFiles (QuickVec* files, const char* filter, const char* defaultPath) { + void FileDialog::OpenFiles (std::vector* files, const char* filter, const char* defaultPath) { nfdpathset_t pathSet; nfdresult_t result = NFD_OpenDialogMultiple (filter, defaultPath, &pathSet);