getFiles method

List<File> getFiles()

Returns a list of File objects from the selected files.

Returns an empty list if the user cancels the dialog.

Implementation

List<File> getFiles() {
  var didUserCancel = false;
  final filePaths = <String>[];

  final fileDialog = _createDialog(multiSelect: true);

  final hr = fileDialog.show(NULL);
  if (FAILED(hr)) {
    if (hr == HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_CANCELLED)) {
      didUserCancel = true;
    } else {
      throw WindowsException(hr);
    }
  } else {
    final ppsi = calloc<Pointer<COMObject>>();
    var hr = fileDialog.getResults(ppsi);
    if (FAILED(hr)) throw WindowsException(hr);

    using((arena) {
      final itemArray = IShellItemArray(ppsi.cast());
      final pdwNumItems = arena<Uint32>();
      hr = itemArray.getCount(pdwNumItems);
      if (FAILED(hr)) throw WindowsException(hr);

      for (var i = 0; i < pdwNumItems.value; i++) {
        final ppsi = calloc<Pointer<COMObject>>();
        hr = itemArray.getItemAt(i, ppsi);
        if (FAILED(hr)) throw WindowsException(hr);

        final item = IShellItem(ppsi.cast());
        final ppszName = arena<Pointer<Utf16>>();

        hr = item.getDisplayName(SIGDN.SIGDN_FILESYSPATH, ppszName);
        if (FAILED(hr)) throw WindowsException(hr);

        final filePath = ppszName.value.toDartString();
        filePaths.add(filePath);
      }
    });
  }

  return didUserCancel ? [] : List<File>.from(filePaths.map(File.new));
}