Skip to content

Commit

Permalink
refactor: replace parameters by _OpenSaveFileArgs
Browse files Browse the repository at this point in the history
The class `_OpenSaveFileArgs` has been introduced in
f56fac2. However, it wasn't used
consistently throughout the whole Windows implementation as each field
of `_OpenSaveFileArgs` was passed individually between functions.
Consistenly using an instance of `_OpenSaveFileArgs` minifies function
signatures and reduces the total lines of code by 50 lines.
  • Loading branch information
philenius committed Apr 5, 2023
1 parent 3472d5f commit eb5878c
Showing 1 changed file with 34 additions and 81 deletions.
115 changes: 34 additions & 81 deletions lib/src/windows/file_picker_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ class FilePickerWindows extends FilePicker {
}) async {
final port = ReceivePort();
await Isolate.spawn(
_callPickFiles,
_OpenSaveFileArgs(
port: port.sendPort,
dialogTitle: dialogTitle,
initialDirectory: initialDirectory,
type: type,
allowedExtensions: allowedExtensions,
allowCompression: allowCompression,
allowMultiple: allowMultiple,
lockParentWindow: lockParentWindow));
_callPickFiles,
_OpenSaveFileArgs(
port: port.sendPort,
dialogTitle: dialogTitle,
initialDirectory: initialDirectory,
type: type,
allowedExtensions: allowedExtensions,
allowCompression: allowCompression,
allowMultiple: allowMultiple,
lockParentWindow: lockParentWindow,
),
);
final fileNames = (await port.first) as List<String>?;
FilePickerResult? returnValue;
if (fileNames != null) {
Expand All @@ -55,29 +57,15 @@ class FilePickerWindows extends FilePicker {
return returnValue;
}

List<String>? _pickFiles({
String? dialogTitle,
String? initialDirectory,
FileType type = FileType.any,
List<String>? allowedExtensions,
bool allowCompression = true,
bool allowMultiple = false,
bool lockParentWindow = false,
}) {
List<String>? _pickFiles(_OpenSaveFileArgs args) {
final comdlg32 = DynamicLibrary.open('comdlg32.dll');

final getOpenFileNameW =
comdlg32.lookupFunction<GetOpenFileNameW, GetOpenFileNameWDart>(
'GetOpenFileNameW');

final Pointer<OPENFILENAMEW> openFileNameW = _instantiateOpenFileNameW(
allowMultiple: allowMultiple,
allowedExtensions: allowedExtensions,
dialogTitle: dialogTitle,
initialDirectory: initialDirectory,
type: type,
lockParentWindow: lockParentWindow,
);
final Pointer<OPENFILENAMEW> openFileNameW =
_instantiateOpenFileNameW(args);

final result = getOpenFileNameW(openFileNameW);
late final List<String>? files;
Expand Down Expand Up @@ -186,7 +174,7 @@ class FilePickerWindows extends FilePicker {
_callSaveFile,
_OpenSaveFileArgs(
port: port.sendPort,
fileName: fileName,
defaultFileName: fileName,
dialogTitle: dialogTitle,
initialDirectory: initialDirectory,
type: type,
Expand All @@ -197,28 +185,15 @@ class FilePickerWindows extends FilePicker {
return (await port.first) as String?;
}

String? _saveFile({
String? dialogTitle,
String? fileName,
String? initialDirectory,
FileType type = FileType.any,
List<String>? allowedExtensions,
bool lockParentWindow = false,
}) {
String? _saveFile(_OpenSaveFileArgs args) {
final comdlg32 = DynamicLibrary.open('comdlg32.dll');

final getSaveFileNameW =
comdlg32.lookupFunction<GetSaveFileNameW, GetSaveFileNameWDart>(
'GetSaveFileNameW');

final Pointer<OPENFILENAMEW> openFileNameW = _instantiateOpenFileNameW(
allowedExtensions: allowedExtensions,
defaultFileName: fileName,
dialogTitle: dialogTitle,
initialDirectory: initialDirectory,
type: type,
lockParentWindow: lockParentWindow,
);
final Pointer<OPENFILENAMEW> openFileNameW =
_instantiateOpenFileNameW(args);

final result = getSaveFileNameW(openFileNameW);
String? returnValue;
Expand Down Expand Up @@ -305,52 +280,43 @@ class FilePickerWindows extends FilePicker {
return filePaths;
}

Pointer<OPENFILENAMEW> _instantiateOpenFileNameW({
bool allowMultiple = false,
String? dialogTitle,
String? defaultFileName,
String? initialDirectory,
List<String>? allowedExtensions,
FileType type = FileType.any,
bool lockParentWindow = false,
bool confirmOverwrite = false,
}) {
Pointer<OPENFILENAMEW> _instantiateOpenFileNameW(_OpenSaveFileArgs args) {
final lpstrFileBufferSize = 8192 * maximumPathLength;
final Pointer<OPENFILENAMEW> openFileNameW = calloc<OPENFILENAMEW>();

openFileNameW.ref.lStructSize = sizeOf<OPENFILENAMEW>();
openFileNameW.ref.lpstrTitle =
(dialogTitle ?? defaultDialogTitle).toNativeUtf16();
(args.dialogTitle ?? defaultDialogTitle).toNativeUtf16();
openFileNameW.ref.lpstrFile = calloc.allocate<Utf16>(lpstrFileBufferSize);
openFileNameW.ref.lpstrFilter =
fileTypeToFileFilter(type, allowedExtensions).toNativeUtf16();
fileTypeToFileFilter(args.type, args.allowedExtensions).toNativeUtf16();
openFileNameW.ref.nMaxFile = lpstrFileBufferSize;
openFileNameW.ref.lpstrInitialDir =
(initialDirectory ?? '').toNativeUtf16();
(args.initialDirectory ?? '').toNativeUtf16();
openFileNameW.ref.flags =
ofnExplorer | ofnFileMustExist | ofnHideReadOnly | ofnNoChangeDir;

if (lockParentWindow) {
if (args.lockParentWindow) {
openFileNameW.ref.hwndOwner = _getWindowHandle();
}

if (allowMultiple) {
if (args.allowMultiple) {
openFileNameW.ref.flags |= ofnAllowMultiSelect;
}

if (confirmOverwrite) {
if (args.confirmOverwrite) {
openFileNameW.ref.flags |= ofnOverwritePrompt;
}

if (defaultFileName != null) {
validateFileName(defaultFileName);
if (args.defaultFileName != null) {
validateFileName(args.defaultFileName!);

final Uint16List nativeString = openFileNameW.ref.lpstrFile
.cast<Uint16>()
.asTypedList(maximumPathLength);
final safeName = defaultFileName.substring(
final safeName = args.defaultFileName!.substring(
0,
min(maximumPathLength - 1, defaultFileName.length),
min(maximumPathLength - 1, args.defaultFileName!.length),
);
final units = safeName.codeUnits;
nativeString.setRange(0, units.length, units);
Expand Down Expand Up @@ -384,33 +350,20 @@ class FilePickerWindows extends FilePicker {

static void _callPickFiles(_OpenSaveFileArgs args) {
final impl = FilePickerWindows();
args.port.send(impl._pickFiles(
dialogTitle: args.dialogTitle,
initialDirectory: args.initialDirectory,
type: args.type,
allowedExtensions: args.allowedExtensions,
allowCompression: args.allowCompression,
allowMultiple: args.allowMultiple,
lockParentWindow: args.lockParentWindow));
args.port.send(impl._pickFiles(args));
}

static void _callSaveFile(_OpenSaveFileArgs args) {
final impl = FilePickerWindows();
args.port.send(impl._saveFile(
dialogTitle: args.dialogTitle,
fileName: args.fileName,
initialDirectory: args.initialDirectory,
type: args.type,
allowedExtensions: args.allowedExtensions,
lockParentWindow: args.lockParentWindow));
args.port.send(impl._saveFile(args));
}
}

class _OpenSaveFileArgs {
final SendPort port;
final String? defaultFileName;
final String? dialogTitle;
final String? initialDirectory;
final String? fileName;
final FileType type;
final List<String>? allowedExtensions;
final bool allowCompression;
Expand All @@ -420,8 +373,8 @@ class _OpenSaveFileArgs {

_OpenSaveFileArgs({
required this.port,
this.defaultFileName,
this.dialogTitle,
this.fileName,
this.initialDirectory,
this.type = FileType.any,
this.allowedExtensions,
Expand Down

0 comments on commit eb5878c

Please sign in to comment.