Update the FileDialog API, improve

This commit is contained in:
Joshua Granick
2015-08-22 17:10:51 -07:00
parent b5f1949577
commit 23e3882a24
5 changed files with 83 additions and 53 deletions

View File

@@ -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 Event<Void->Void> ();
public var onSelect = new Event<String->Void> ();
public var onSelectMultiple = new Event<Array<String>->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<String> {
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<String> = 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;
}

10
lime/ui/FileDialogType.hx Normal file
View File

@@ -0,0 +1,10 @@
package lime.ui;
enum FileDialogType {
OPEN;
OPEN_MULTIPLE;
SAVE;
}

View File

@@ -2,7 +2,7 @@
#define LIME_UI_FILE_DIALOG_H
#include <utils/QuickVec.h>
#include <vector>
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<const char*>* files, const char* filter = NULL, const char* defaultPath = NULL);
static void OpenFiles (std::vector<const char*>* files, const char* filter = NULL, const char* defaultPath = NULL);
static const char* SaveFile (const char* filter = NULL, const char* defaultPath = NULL);
};

View File

@@ -209,7 +209,7 @@ namespace lime {
value lime_file_dialog_open_files (value filter, value defaultPath) {
#ifdef LIME_NFD
QuickVec<const char*> files;
std::vector<const char*> files;
FileDialog::OpenFiles (&files, val_string (filter), val_string (defaultPath));
value result = alloc_array (files.size ());

View File

@@ -34,7 +34,7 @@ namespace lime {
}
void FileDialog::OpenFiles (QuickVec<const char*>* files, const char* filter, const char* defaultPath) {
void FileDialog::OpenFiles (std::vector<const char*>* files, const char* filter, const char* defaultPath) {
nfdpathset_t pathSet;
nfdresult_t result = NFD_OpenDialogMultiple (filter, defaultPath, &pathSet);