Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit f38a4a5

Browse files
authored
free allocations
format code
1 parent ef9df57 commit f38a4a5

16 files changed

+176
-140
lines changed

packages/file_selector/file_selector_windows/lib/file_selector_windows.dart

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class FileSelectorWindows extends FileSelectorPlatform {
1616
/// Creates a new instance of [FileSelectorApi].
1717
FileSelectorWindows()
1818
: _hostApi = FileSelectorApi(DialogWrapperFactory(
19-
FileDialogControllerFactory(), IFileDialogFactory()));
19+
FileDialogControllerFactory(),
20+
IFileDialogFactory(),
21+
));
2022

2123
/// Creates a fake implementation of [FileSelectorApi] for testing purposes.
2224
@visibleForTesting
@@ -36,11 +38,10 @@ class FileSelectorWindows extends FileSelectorPlatform {
3638
String? confirmButtonText,
3739
}) async {
3840
final List<String?> paths = _hostApi.showOpenDialog(
39-
SelectionOptions(
40-
allowedTypes: _allowedXTypeGroups(acceptedTypeGroups),
41-
),
42-
initialDirectory,
43-
confirmButtonText);
41+
SelectionOptions(allowedTypes: _allowedXTypeGroups(acceptedTypeGroups)),
42+
initialDirectory,
43+
confirmButtonText,
44+
);
4445
return paths.isEmpty ? null : XFile(paths.first!);
4546
}
4647

@@ -51,12 +52,10 @@ class FileSelectorWindows extends FileSelectorPlatform {
5152
String? confirmButtonText,
5253
}) async {
5354
final List<String?> paths = _hostApi.showOpenDialog(
54-
SelectionOptions(
55-
allowMultiple: true,
56-
allowedTypes: _allowedXTypeGroups(acceptedTypeGroups),
57-
),
58-
initialDirectory,
59-
confirmButtonText);
55+
SelectionOptions(allowMultiple: true, allowedTypes: _allowedXTypeGroups(acceptedTypeGroups)),
56+
initialDirectory,
57+
confirmButtonText,
58+
);
6059
return paths.map((String? path) => XFile(path!)).toList();
6160
}
6261

@@ -68,12 +67,11 @@ class FileSelectorWindows extends FileSelectorPlatform {
6867
String? confirmButtonText,
6968
}) async {
7069
final List<String?> paths = _hostApi.showSaveDialog(
71-
SelectionOptions(
72-
allowedTypes: _allowedXTypeGroups(acceptedTypeGroups),
73-
),
74-
initialDirectory,
75-
suggestedName,
76-
confirmButtonText);
70+
SelectionOptions(allowedTypes: _allowedXTypeGroups(acceptedTypeGroups)),
71+
initialDirectory,
72+
suggestedName,
73+
confirmButtonText,
74+
);
7775
return paths.isEmpty ? null : paths.first!;
7876
}
7977

@@ -83,12 +81,10 @@ class FileSelectorWindows extends FileSelectorPlatform {
8381
String? confirmButtonText,
8482
}) async {
8583
final List<String?> paths = _hostApi.showOpenDialog(
86-
SelectionOptions(
87-
selectFolders: true,
88-
allowedTypes: <XTypeGroup>[],
89-
),
90-
initialDirectory,
91-
confirmButtonText);
84+
SelectionOptions(selectFolders: true, allowedTypes: <XTypeGroup>[]),
85+
initialDirectory,
86+
confirmButtonText,
87+
);
9288
return paths.isEmpty ? null : paths.first!;
9389
}
9490
}

packages/file_selector/file_selector_windows/lib/src/file_selector_api.dart

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class FileSelectorApi {
2121
/// from the outside.
2222
@visibleForTesting
2323
FileSelectorApi.useFakeForegroundWindow(
24-
this._dialogWrapperFactory, this._foregroundWindow);
24+
this._dialogWrapperFactory,
25+
this._foregroundWindow,
26+
);
2527

2628
final DialogWrapperFactory _dialogWrapperFactory;
2729

@@ -33,8 +35,14 @@ class FileSelectorApi {
3335
String? initialDirectory,
3436
String? confirmButtonText,
3537
) =>
36-
_showDialog(_foregroundWindow, DialogMode.Open, options, initialDirectory,
37-
null, confirmButtonText);
38+
_showDialog(
39+
_foregroundWindow,
40+
DialogMode.open,
41+
options,
42+
initialDirectory,
43+
null,
44+
confirmButtonText,
45+
);
3846

3947
/// Displays a dialog used to save a file.
4048
List<String?> showSaveDialog(
@@ -43,18 +51,24 @@ class FileSelectorApi {
4351
String? suggestedName,
4452
String? confirmButtonText,
4553
) =>
46-
_showDialog(_foregroundWindow, DialogMode.Save, options, initialDirectory,
47-
suggestedName, confirmButtonText);
54+
_showDialog(
55+
_foregroundWindow,
56+
DialogMode.save,
57+
options,
58+
initialDirectory,
59+
suggestedName,
60+
confirmButtonText,
61+
);
4862

4963
List<String?> _showDialog(
50-
int parentWindow,
51-
DialogMode mode,
52-
SelectionOptions options,
53-
String? initialDirectory,
54-
String? suggestedName,
55-
String? confirmLabel) {
56-
final DialogWrapper dialogWrapper =
57-
_dialogWrapperFactory.createInstance(mode);
64+
int parentWindow,
65+
DialogMode mode,
66+
SelectionOptions options,
67+
String? initialDirectory,
68+
String? suggestedName,
69+
String? confirmLabel,
70+
) {
71+
final DialogWrapper dialogWrapper = _dialogWrapperFactory.createInstance(mode);
5872
if (!SUCCEEDED(dialogWrapper.lastResult)) {
5973
throw WindowsException(E_FAIL);
6074
}

packages/file_selector/file_selector_windows/lib/src/file_selector_dart/dialog_mode.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/// The kind of file dialog to show.
66
enum DialogMode {
77
/// Used for chosing files.
8-
Open,
8+
open,
99

1010
/// Used for chosing a directory to save a file.
11-
Save
11+
save
1212
}

packages/file_selector/file_selector_windows/lib/src/file_selector_dart/dialog_wrapper.dart

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import 'shell_win32_api.dart';
2121
class DialogWrapper {
2222
/// Creates a DialogWrapper using a [IFileDialogControllerFactory] and a [DialogMode].
2323
/// It is also responsible of creating a [IFileDialog].
24-
DialogWrapper(IFileDialogControllerFactory fileDialogControllerFactory,
25-
IFileDialogFactory fileDialogFactory, this._dialogMode)
26-
: _isOpenDialog = _dialogMode == DialogMode.Open {
24+
DialogWrapper(
25+
IFileDialogControllerFactory fileDialogControllerFactory,
26+
IFileDialogFactory fileDialogFactory,
27+
this._dialogMode,
28+
) : _isOpenDialog = _dialogMode == DialogMode.open {
2729
try {
2830
final IFileDialog dialog = fileDialogFactory.createInstace(_dialogMode);
2931
_dialogController = fileDialogControllerFactory.createController(dialog);
@@ -37,39 +39,40 @@ class DialogWrapper {
3739

3840
/// Creates a DialogWrapper for testing purposes.
3941
@visibleForTesting
40-
DialogWrapper.withFakeDependencies(FileDialogController dialogController,
41-
this._dialogMode, this._shellWin32Api)
42-
: _isOpenDialog = _dialogMode == DialogMode.Open,
42+
DialogWrapper.withFakeDependencies(
43+
FileDialogController dialogController,
44+
this._dialogMode,
45+
this._shellWin32Api,
46+
) : _isOpenDialog = _dialogMode == DialogMode.open,
4347
_dialogController = dialogController;
4448

45-
int _lastResult = S_OK;
46-
4749
final DialogMode _dialogMode;
4850

4951
final bool _isOpenDialog;
5052

51-
final String _allowAnyValue = 'Any';
53+
late final FileDialogController _dialogController;
5254

53-
final String _allowAnyExtension = '*.*';
55+
late final ShellWin32Api _shellWin32Api;
5456

55-
late FileDialogController _dialogController;
57+
static const String _allowAnyValue = 'Any';
5658

57-
late ShellWin32Api _shellWin32Api;
59+
static const String _allowAnyExtension = '*.*';
5860

5961
/// Returns the result of the last Win32 API call related to this object.
6062
int get lastResult => _lastResult;
6163

64+
int _lastResult = S_OK;
65+
6266
/// Attempts to set the default folder for the dialog to [path], if it exists.
6367
void setFolder(String path) {
64-
if (path == null || path.isEmpty) {
68+
if (path.isEmpty) {
6569
return;
6670
}
6771

6872
using((Arena arena) {
6973
final Pointer<GUID> ptrGuid = GUIDFromString(IID_IShellItem);
7074
final Pointer<Pointer<COMObject>> ptrPath = arena<Pointer<COMObject>>();
71-
_lastResult =
72-
_shellWin32Api.createItemFromParsingName(path, ptrGuid, ptrPath);
75+
_lastResult = _shellWin32Api.createItemFromParsingName(path, ptrGuid, ptrPath);
7376

7477
if (!SUCCEEDED(_lastResult)) {
7578
return;
@@ -125,19 +128,19 @@ class DialogWrapper {
125128
}
126129

127130
using((Arena arena) {
128-
final Pointer<COMDLG_FILTERSPEC> registerFilterSpecification =
129-
arena<COMDLG_FILTERSPEC>(filterSpecification.length);
130-
131+
final Pointer<COMDLG_FILTERSPEC> registerFilterSpecification = arena<COMDLG_FILTERSPEC>(filterSpecification.length);
131132
int index = 0;
132133
for (final String key in filterSpecification.keys) {
133134
registerFilterSpecification[index]
134-
..pszName = TEXT(key)
135-
..pszSpec = TEXT(filterSpecification[key]!);
136-
index++;
135+
..pszName = key.toNativeUtf16(allocator: arena)
136+
..pszSpec = filterSpecification[key]!.toNativeUtf16(allocator: arena);
137+
index += 1;
137138
}
138139

139140
_lastResult = _dialogController.setFileTypes(
140-
filterSpecification.length, registerFilterSpecification);
141+
filterSpecification.length,
142+
registerFilterSpecification,
143+
);
141144
});
142145
}
143146

@@ -150,11 +153,9 @@ class DialogWrapper {
150153
late List<String>? files;
151154

152155
using((Arena arena) {
153-
final Pointer<Pointer<COMObject>> shellItemArrayPtr =
154-
arena<Pointer<COMObject>>();
156+
final Pointer<Pointer<COMObject>> shellItemArrayPtr = arena<Pointer<COMObject>>();
155157
final Pointer<Uint32> shellItemCountPtr = arena<Uint32>();
156-
final Pointer<Pointer<COMObject>> shellItemPtr =
157-
arena<Pointer<COMObject>>();
158+
final Pointer<Pointer<COMObject>> shellItemPtr = arena<Pointer<COMObject>>();
158159

159160
files =
160161
_getFilePathList(shellItemArrayPtr, shellItemCountPtr, shellItemPtr);
@@ -179,7 +180,7 @@ class DialogWrapper {
179180
if (!SUCCEEDED(_lastResult)) {
180181
return null;
181182
}
182-
for (int index = 0; index < shellItemCountPtr.value; index++) {
183+
for (int index = 0; index < shellItemCountPtr.value; index += 1) {
183184
shellItemResources.getItemAt(index, shellItemPtr);
184185
final IShellItem shellItem = IShellItem(shellItemPtr.cast());
185186
files.add(_shellWin32Api.getPathForShellItem(shellItem));

packages/file_selector/file_selector_windows/lib/src/file_selector_dart/dialog_wrapper_factory.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class DialogWrapperFactory {
1212
/// Creates a [DialogWrapperFactory] that makes use of [IFileDialogControllerFactory] and [IFileDialogFactory]
1313
/// to create [DialogWrapper] instances.
1414
DialogWrapperFactory(
15-
this._fileDialogControllerFactory, this._fileDialogFactory);
15+
this._fileDialogControllerFactory,
16+
this._fileDialogFactory,
17+
);
1618

1719
final IFileDialogControllerFactory _fileDialogControllerFactory;
1820

@@ -21,6 +23,9 @@ class DialogWrapperFactory {
2123
/// Creates a [DialogWrapper] based on [dialogMode].
2224
DialogWrapper createInstance(DialogMode dialogMode) {
2325
return DialogWrapper(
24-
_fileDialogControllerFactory, _fileDialogFactory, dialogMode);
26+
_fileDialogControllerFactory,
27+
_fileDialogFactory,
28+
dialogMode,
29+
);
2530
}
2631
}

packages/file_selector/file_selector_windows/lib/src/file_selector_dart/file_dialog_controller.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:ffi';
66

7+
import 'package:ffi/ffi.dart';
78
import 'package:win32/win32.dart';
89

910
import 'ifile_open_dialog_factory.dart';
@@ -17,24 +18,25 @@ class FileDialogController {
1718
/// It also receives an IFileOpenDialogFactory to construct [IFileOpenDialog]
1819
/// instances.
1920
FileDialogController(
20-
IFileDialog fileDialog, IFileOpenDialogFactory iFileOpenDialogFactory)
21-
: _fileDialog = fileDialog,
21+
IFileDialog fileDialog,
22+
IFileOpenDialogFactory iFileOpenDialogFactory,
23+
) : _fileDialog = fileDialog,
2224
_iFileOpenDialogFactory = iFileOpenDialogFactory;
2325

2426
/// The [IFileDialog] to work with.
2527
final IFileDialog _fileDialog;
2628

27-
/// The [IFileOpenDialogFactory] to work construc [IFileOpenDialog] instances.
29+
/// The [IFileOpenDialogFactory] to create [IFileOpenDialog] instances.
2830
final IFileOpenDialogFactory _iFileOpenDialogFactory;
2931

3032
/// Sets the default folder for the dialog to [path]. It also returns the operation result.
31-
int setFolder(Pointer<COMObject> path) {
32-
return _fileDialog.setFolder(path);
33-
}
33+
int setFolder(Pointer<COMObject> path) => _fileDialog.setFolder(path);
3434

3535
/// Sets the file [name] that is initially shown in the IFileDialog. It also returns the operation result.
3636
int setFileName(String name) {
37-
return _fileDialog.setFileName(TEXT(name));
37+
return using((Arena arena) {
38+
return _fileDialog.setFileName(name.toNativeUtf16(allocator: arena));
39+
}, malloc);
3840
}
3941

4042
/// Sets the allowed file type extensions in the IFileOpenDialog. It also returns the operation result.
@@ -44,7 +46,9 @@ class FileDialogController {
4446

4547
/// Sets the label of the confirmation button. It also returns the operation result. It also returns the operation result.
4648
int setOkButtonLabel(String text) {
47-
return _fileDialog.setOkButtonLabel(TEXT(text));
49+
return using((Arena arena) {
50+
return _fileDialog.setOkButtonLabel(text.toNativeUtf16(allocator: arena));
51+
}, malloc);
4852
}
4953

5054
/// Gets the IFileDialog's [options](https://pub.dev/documentation/win32/latest/winrt/FILEOPENDIALOGOPTIONS-class.html),
@@ -55,14 +59,10 @@ class FileDialogController {
5559

5660
/// Sets the [options](https://pub.dev/documentation/win32/latest/winrt/FILEOPENDIALOGOPTIONS-class.html),
5761
/// which is a bitfield, into the IFileDialog. It also returns the operation result.
58-
int setOptions(int options) {
59-
return _fileDialog.setOptions(options);
60-
}
62+
int setOptions(int options) => _fileDialog.setOptions(options);
6163

6264
/// Shows an IFileDialog using the given parent. It returns the operation result.
63-
int show(int parent) {
64-
return _fileDialog.show(parent);
65-
}
65+
int show(int parent) => _fileDialog.show(parent);
6666

6767
/// Return results from an IFileDialog. This should be used when selecting
6868
/// single items. It also returns the operation result.
@@ -79,7 +79,7 @@ class FileDialogController {
7979
try {
8080
fileOpenDialog = _iFileOpenDialogFactory.from(_fileDialog);
8181
return fileOpenDialog.getResults(outItems);
82-
} catch (_) {
82+
} catch (exception) {
8383
return E_FAIL;
8484
} finally {
8585
fileOpenDialog?.release();

packages/file_selector/file_selector_windows/lib/src/file_selector_dart/ifile_dialog_factory.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import 'dialog_mode.dart';
1010
class IFileDialogFactory {
1111
/// Creates the corresponding IFileDialog instace. The caller is responsible of releasing the resource.
1212
IFileDialog createInstace(DialogMode dialogMode) {
13-
if (dialogMode == DialogMode.Open) {
14-
return FileOpenDialog.createInstance();
13+
switch (dialogMode) {
14+
case DialogMode.open:
15+
return FileOpenDialog.createInstance();
16+
case DialogMode.save:
17+
return FileSaveDialog.createInstance();
1518
}
16-
17-
return FileSaveDialog.createInstance();
1819
}
1920
}

0 commit comments

Comments
 (0)