Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/src/file_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ abstract final class FilePicker {
bool readSequential = false,
bool cancelUploadOnWindowBlur = true,
AndroidSAFOptions? androidSafOptions,
int? parentWindowHandle,
}) {
return FilePickerPlatform.instance.pickFiles(
dialogTitle: dialogTitle,
Expand All @@ -102,6 +103,7 @@ abstract final class FilePicker {
readSequential: readSequential,
cancelUploadOnWindowBlur: cancelUploadOnWindowBlur,
androidSafOptions: androidSafOptions,
parentWindowHandle: parentWindowHandle,
);
}

Expand Down Expand Up @@ -221,12 +223,14 @@ abstract final class FilePicker {
bool lockParentWindow = false,
String? initialDirectory,
AndroidSAFOptions? androidSafOptions,
int? parentWindowHandle,
}) {
return FilePickerPlatform.instance.getDirectoryPath(
dialogTitle: dialogTitle,
lockParentWindow: lockParentWindow,
initialDirectory: initialDirectory,
androidSafOptions: androidSafOptions,
parentWindowHandle: parentWindowHandle,
);
}

Expand Down Expand Up @@ -272,6 +276,7 @@ abstract final class FilePicker {
required Uint8List bytes,
Function(FilePickerStatus)? onFileLoading,
bool lockParentWindow = false,
int? parentWindowHandle,
}) {
return FilePickerPlatform.instance.saveFile(
dialogTitle: dialogTitle,
Expand All @@ -282,6 +287,7 @@ abstract final class FilePicker {
bytes: bytes,
onFileLoading: onFileLoading,
lockParentWindow: lockParentWindow,
parentWindowHandle: parentWindowHandle,
);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/platform/file_picker_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class FilePickerPlatform extends PlatformInterface {
bool readSequential = false,
bool cancelUploadOnWindowBlur = true,
AndroidSAFOptions? androidSafOptions,
int? parentWindowHandle,
}) async {
throw UnimplementedError('pickFiles() has not been implemented.');
}
Expand Down Expand Up @@ -76,6 +77,7 @@ abstract class FilePickerPlatform extends PlatformInterface {
bool lockParentWindow = false,
String? initialDirectory,
AndroidSAFOptions? androidSafOptions,
int? parentWindowHandle,
}) async {
throw UnimplementedError('getDirectoryPath() has not been implemented.');
}
Expand All @@ -91,6 +93,7 @@ abstract class FilePickerPlatform extends PlatformInterface {
required Uint8List bytes,
Function(FilePickerStatus)? onFileLoading,
bool lockParentWindow = false,
int? parentWindowHandle,
}) async {
throw UnimplementedError('saveFile() has not been implemented.');
}
Expand Down
21 changes: 19 additions & 2 deletions lib/src/platform/windows/file_picker_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FilePickerWindows extends FilePickerPlatform {
int compressionQuality = 0,
bool cancelUploadOnWindowBlur = true,
AndroidSAFOptions? androidSafOptions,
int? parentWindowHandle,
}) async {
final port = ReceivePort();
await Isolate.spawn(
Expand All @@ -50,6 +51,7 @@ class FilePickerWindows extends FilePickerPlatform {
allowedExtensions: allowedExtensions,
allowMultiple: allowMultiple,
lockParentWindow: lockParentWindow,
parentWindowHandle: parentWindowHandle,
),
);
final fileNames = (await port.first) as List<String>?;
Expand Down Expand Up @@ -104,18 +106,21 @@ class FilePickerWindows extends FilePickerPlatform {
bool lockParentWindow = false,
String? initialDirectory,
AndroidSAFOptions? androidSafOptions,
int? parentWindowHandle,
}) async {
return compute(_getDirectoryPathIsolate, {
'dialogTitle': dialogTitle,
'initialDirectory': initialDirectory,
'lockParentWindow': lockParentWindow,
'parentWindowHandle': parentWindowHandle,
});
}

String? _getDirectoryPathIsolate(Map<String, Object?> args) {
String? dialogTitle = args['dialogTitle'] as String?;
String? initialDirectory = args['initialDirectory'] as String?;
bool lockParentWindow = args['lockParentWindow'] as bool? ?? false;
int? parentWindowHandle = args['parentWindowHandle'] as int?;

final hr = CoInitializeEx(
COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE,
Expand Down Expand Up @@ -151,7 +156,13 @@ class FilePickerWindows extends FilePickerPlatform {
}

try {
fileDialog.show(lockParentWindow ? GetForegroundWindow() : null);
fileDialog.show(
lockParentWindow
? (parentWindowHandle != null
? parentWindowHandle
: GetForegroundWindow())
: null,
);
} on WindowsException catch (e) {
if (e.hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) {
return null;
Expand Down Expand Up @@ -187,6 +198,7 @@ class FilePickerWindows extends FilePickerPlatform {
required Uint8List bytes,
Function(FilePickerStatus)? onFileLoading,
bool lockParentWindow = false,
int? parentWindowHandle,
}) async {
final port = ReceivePort();
await Isolate.spawn(
Expand All @@ -200,6 +212,7 @@ class FilePickerWindows extends FilePickerPlatform {
allowedExtensions: allowedExtensions,
lockParentWindow: lockParentWindow,
confirmOverwrite: true,
parentWindowHandle: parentWindowHandle,
),
);
final savedFilePath = (await port.first) as String?;
Expand Down Expand Up @@ -336,7 +349,9 @@ class FilePickerWindows extends FilePickerPlatform {
ofnExplorer | ofnFileMustExist | ofnHideReadOnly | ofnNoChangeDir;

if (args.lockParentWindow) {
openFileNameW.ref.hwndOwner = _getWindowHandle();
openFileNameW.ref.hwndOwner = args.parentWindowHandle != null
? Pointer.fromAddress(args.parentWindowHandle!)
: _getWindowHandle();
}

if (args.allowMultiple) {
Expand Down Expand Up @@ -412,6 +427,7 @@ class _OpenSaveFileArgs {
final bool allowMultiple;
final bool lockParentWindow;
final bool confirmOverwrite;
final int? parentWindowHandle;

_OpenSaveFileArgs({
required this.port,
Expand All @@ -423,5 +439,6 @@ class _OpenSaveFileArgs {
this.allowMultiple = false,
this.lockParentWindow = false,
this.confirmOverwrite = false,
this.parentWindowHandle,
});
}