Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2013 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5428273 to your computer and use it in GitHub Desktop.
Save anonymous/5428273 to your computer and use it in GitHub Desktop.
Experimenting with using a file chooser in a Chrome App written in Dart. Got stuck again :(
import 'dart:async';
import 'dart:chrome';
import 'dart:html';
import 'package:js/js.dart' as js;
void main() {
var open = new ButtonElement()..text = 'Open';
document.body.children.add(open);
write(s) => document.body.children.add(new DivElement()..text = s);
open.onClick.listen((_) {
FileEntry.choose()
.then((entry) {
entry.displayPath.then(write);
// Yikes
// Can't use dart's file reader as entry is a proxy not a Dart FileEntry/Blob.
// Not sure how to call the javascript ctor new FileReader() via js_interop.
//var reader = new FileReader();
//reader.readAsText(entry);
});
});
}
class ChooseEntryType {
const ChooseEntryType(this._value);
final String _value;
toString() => _value;
}
const OPEN_FILE = const ChooseEntryType("openFile");
const OPEN_FILE_WRITABLE = const ChooseEntryType("openWritableFile");
const SAVE_FILE = const ChooseEntryType("saveFile");
class AcceptOption {
AcceptOption(this.description, this.mimeTypes, this.extensions);
final String description;
final List<String> mimeTypes;
final List<String> extensions;
}
class FileEntry {
final _proxy;
FileEntry._internal(this._proxy);
void release() {
js.release(_proxy);
}
static Future<FileEntry> choose(
{ChooseEntryType type: OPEN_FILE,
String suggestedName : '',
List<AcceptOption> accepts : const [],
bool acceptsAllTypes: true}) {
var cpl = new Completer<String>();
js.scoped(() {
var options = {'type': type.toString(),
'suggestedName': suggestedName,
'accepts': js.array(accepts), //TODO
'acceptsAllTypes': acceptsAllTypes};
js.context.chrome.fileSystem.chooseEntry(js.map(options),
new js.Callback.once((fileEntry) {
cpl.complete(new FileEntry._internal(fileEntry));
}));
});
return cpl.future;
}
static FileEntry getById(String id) {
var fileEntryProxy;
js.scoped(() {
fileEntryProxy = js.context.chrome.fileSystem.getEntryById(id);
js.retain(fileEntryProxy);
});
return new FileEntry._internal(fileEntryProxy);
}
String get id {
var entryId;
js.scoped(() {
entryId = js.context.chrome.fileSystem.getEntryId(_proxy);
});
return entryId;
}
Future<String> get displayPath {
var cpl = new Completer<String>();
js.scoped(() {
js.context.chrome.fileSystem.getDisplayPath(_proxy,
new js.Callback.once((path) {
cpl.complete(path);
}));
});
return cpl.future;
}
Future<FileEntry> makeWritable() {
throw new UnimplementedError();
}
Future<bool> get isWritable {
throw new UnimplementedError();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment