Make dialog.showOpenDialog accept callback.
This commit is contained in:
parent
d3dd2b4332
commit
c95cfc9540
2 changed files with 53 additions and 13 deletions
|
@ -33,10 +33,21 @@ v8::Handle<v8::Value> ToV8Value(const base::FilePath& path) {
|
||||||
return v8::String::New(path_string.data(), path_string.size());
|
return v8::String::New(path_string.data(), path_string.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Handle<v8::Value> ToV8Value(void*) {
|
||||||
|
return v8::Undefined();
|
||||||
|
}
|
||||||
|
|
||||||
v8::Handle<v8::Value> ToV8Value(int code) {
|
v8::Handle<v8::Value> ToV8Value(int code) {
|
||||||
return v8::Integer::New(code);
|
return v8::Integer::New(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Handle<v8::Value> ToV8Value(const std::vector<base::FilePath>& paths) {
|
||||||
|
v8::Handle<v8::Array> result = v8::Array::New(paths.size());
|
||||||
|
for (size_t i = 0; i < paths.size(); ++i)
|
||||||
|
result->Set(i, ToV8Value(paths[i]));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void CallV8Function(v8::Persistent<v8::Function> callback, T arg) {
|
void CallV8Function(v8::Persistent<v8::Function> callback, T arg) {
|
||||||
DCHECK(!callback.IsEmpty());
|
DCHECK(!callback.IsEmpty());
|
||||||
|
@ -47,6 +58,16 @@ void CallV8Function(v8::Persistent<v8::Function> callback, T arg) {
|
||||||
callback.Dispose(node_isolate);
|
callback.Dispose(node_isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void CallV8Function2(v8::Persistent<v8::Function> callback,
|
||||||
|
bool result,
|
||||||
|
T arg) {
|
||||||
|
if (result)
|
||||||
|
return CallV8Function<T>(callback, arg);
|
||||||
|
else
|
||||||
|
return CallV8Function<void*>(callback, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
void Initialize(v8::Handle<v8::Object> target) {
|
void Initialize(v8::Handle<v8::Object> target) {
|
||||||
v8::HandleScope scope;
|
v8::HandleScope scope;
|
||||||
|
|
||||||
|
@ -128,23 +149,41 @@ v8::Handle<v8::Value> ShowOpenDialog(const v8::Arguments &args) {
|
||||||
native_window = window->window();
|
native_window = window->window();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Persistent<v8::Function> callback;
|
||||||
|
if (args[4]->IsFunction()) {
|
||||||
|
callback = v8::Persistent<v8::Function>::New(
|
||||||
|
node_isolate,
|
||||||
|
v8::Local<v8::Function>::Cast(args[4]));
|
||||||
|
}
|
||||||
|
|
||||||
std::string title(*v8::String::Utf8Value(args[0]));
|
std::string title(*v8::String::Utf8Value(args[0]));
|
||||||
base::FilePath default_path(V8ValueToFilePath(args[1]));
|
base::FilePath default_path(V8ValueToFilePath(args[1]));
|
||||||
int properties = args[2]->IntegerValue();
|
int properties = args[2]->IntegerValue();
|
||||||
|
|
||||||
std::vector<base::FilePath> paths;
|
if (callback.IsEmpty()) {
|
||||||
if (!file_dialog::ShowOpenDialog(native_window,
|
std::vector<base::FilePath> paths;
|
||||||
title,
|
if (!file_dialog::ShowOpenDialog(native_window,
|
||||||
default_path,
|
title,
|
||||||
properties,
|
default_path,
|
||||||
&paths))
|
properties,
|
||||||
|
&paths))
|
||||||
|
return v8::Undefined();
|
||||||
|
|
||||||
|
v8::Handle<v8::Array> result = v8::Array::New(paths.size());
|
||||||
|
for (size_t i = 0; i < paths.size(); ++i)
|
||||||
|
result->Set(i, ToV8Value(paths[i]));
|
||||||
|
|
||||||
|
return scope.Close(result);
|
||||||
|
} else {
|
||||||
|
file_dialog::ShowOpenDialog(
|
||||||
|
native_window,
|
||||||
|
title,
|
||||||
|
default_path,
|
||||||
|
properties,
|
||||||
|
base::Bind(&CallV8Function2<const std::vector<base::FilePath>>,
|
||||||
|
callback));
|
||||||
return v8::Undefined();
|
return v8::Undefined();
|
||||||
|
}
|
||||||
v8::Handle<v8::Array> result = v8::Array::New(paths.size());
|
|
||||||
for (size_t i = 0; i < paths.size(); ++i)
|
|
||||||
result->Set(i, ToV8Value(paths[i]));
|
|
||||||
|
|
||||||
return scope.Close(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Handle<v8::Value> ShowSaveDialog(const v8::Arguments &args) {
|
v8::Handle<v8::Value> ShowSaveDialog(const v8::Arguments &args) {
|
||||||
|
|
|
@ -28,7 +28,8 @@ module.exports =
|
||||||
binding.showOpenDialog String(options.title),
|
binding.showOpenDialog String(options.title),
|
||||||
String(options.defaultPath),
|
String(options.defaultPath),
|
||||||
properties,
|
properties,
|
||||||
window
|
window,
|
||||||
|
callback
|
||||||
|
|
||||||
showSaveDialog: (window, options) ->
|
showSaveDialog: (window, options) ->
|
||||||
throw new TypeError('Invalid window') unless window?.constructor is BrowserWindow
|
throw new TypeError('Invalid window') unless window?.constructor is BrowserWindow
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue