Skip to content

Commit

Permalink
[file_selector] Add getDirectoryPaths implementation for Windows (#3704)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmorgan authored Apr 29, 2023
1 parent 6dc28c9 commit 815cfca
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/file_selector/file_selector_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.2

* Adds `getDirectoryPaths` implementation.

## 0.9.1+8

* Sets a cmake_policy compatibility version to fix build warnings.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
import 'package:flutter/material.dart';

/// Screen that allows the user to select one or more directories using `getDirectoryPaths`,
/// then displays the selected directories in a dialog.
class GetMultipleDirectoriesPage extends StatelessWidget {
/// Default Constructor
const GetMultipleDirectoriesPage({super.key});

Future<void> _getDirectoryPaths(BuildContext context) async {
const String confirmButtonText = 'Choose';
final List<String?> directoriesPaths =
await FileSelectorPlatform.instance.getDirectoryPaths(
confirmButtonText: confirmButtonText,
);
if (directoriesPaths.isEmpty) {
// Operation was canceled by the user.
return;
}
if (context.mounted) {
await showDialog<void>(
context: context,
builder: (BuildContext context) =>
TextDisplay(directoriesPaths.join('\n')),
);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Select multiple directories'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
// ignore: deprecated_member_use
primary: Colors.blue,
// ignore: deprecated_member_use
onPrimary: Colors.white,
),
child: const Text(
'Press to ask user to choose multiple directories'),
onPressed: () => _getDirectoryPaths(context),
),
],
),
),
);
}
}

/// Widget that displays a text file in a dialog.
class TextDisplay extends StatelessWidget {
/// Creates a `TextDisplay`.
const TextDisplay(this.directoryPaths, {super.key});

/// The paths selected in the dialog.
final String directoryPaths;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Selected Directories'),
content: Scrollbar(
child: SingleChildScrollView(
child: Text(directoryPaths),
),
),
actions: <Widget>[
TextButton(
child: const Text('Close'),
onPressed: () => Navigator.pop(context),
),
],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class HomePage extends StatelessWidget {
child: const Text('Open a get directory dialog'),
onPressed: () => Navigator.pushNamed(context, '/directory'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open a get directories dialog'),
onPressed: () =>
Navigator.pushNamed(context, '/multi-directories'),
),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:flutter/material.dart';

import 'get_directory_page.dart';
import 'get_multiple_directories_page.dart';
import 'home_page.dart';
import 'open_image_page.dart';
import 'open_multiple_images_page.dart';
Expand Down Expand Up @@ -36,6 +37,8 @@ class MyApp extends StatelessWidget {
'/open/text': (BuildContext context) => const OpenTextPage(),
'/save/text': (BuildContext context) => SaveTextPage(),
'/directory': (BuildContext context) => const GetDirectoryPage(),
'/multi-directories': (BuildContext context) =>
const GetMultipleDirectoriesPage()
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
flutter: ">=3.3.0"

dependencies:
file_selector_platform_interface: ^2.2.0
file_selector_platform_interface: ^2.4.0
file_selector_windows:
# When depending on this package from a real application you should use:
# file_selector_windows: ^x.y.z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ class FileSelectorWindows extends FileSelectorPlatform {
confirmButtonText);
return paths.isEmpty ? null : paths.first!;
}

@override
Future<List<String>> getDirectoryPaths({
String? initialDirectory,
String? confirmButtonText,
}) async {
final List<String?> paths = await _hostApi.showOpenDialog(
SelectionOptions(
allowMultiple: true,
selectFolders: true,
allowedTypes: <TypeGroup>[],
),
initialDirectory,
confirmButtonText);
return paths.isEmpty ? <String>[] : List<String>.from(paths);
}
}

List<TypeGroup> _typeGroupsFromXTypeGroups(List<XTypeGroup>? xtypes) {
Expand Down
4 changes: 2 additions & 2 deletions packages/file_selector/file_selector_windows/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: file_selector_windows
description: Windows implementation of the file_selector plugin.
repository: https://github.com/flutter/packages/tree/main/packages/file_selector/file_selector_windows
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.9.1+8
version: 0.9.2

environment:
sdk: ">=2.18.0 <4.0.0"
Expand All @@ -18,7 +18,7 @@ flutter:

dependencies:
cross_file: ^0.3.1
file_selector_platform_interface: ^2.2.0
file_selector_platform_interface: ^2.4.0
flutter:
sdk: flutter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,37 @@ void main() {
});
});

group('#getDirectoryPaths', () {
setUp(() {
when(mockApi.showOpenDialog(any, any, any))
.thenReturn(<String?>['foo', 'bar']);
});

test('simple call works', () async {
final List<String?> paths = await plugin.getDirectoryPaths();

expect(paths[0], 'foo');
expect(paths[1], 'bar');
final VerificationResult result =
verify(mockApi.showOpenDialog(captureAny, null, null));
final SelectionOptions options = result.captured[0] as SelectionOptions;
expect(options.allowMultiple, true);
expect(options.selectFolders, true);
});

test('passes initialDirectory correctly', () async {
await plugin.getDirectoryPath(initialDirectory: '/example/directory');

verify(mockApi.showOpenDialog(any, '/example/directory', null));
});

test('passes confirmButtonText correctly', () async {
await plugin.getDirectoryPath(confirmButtonText: 'Open Directory');

verify(mockApi.showOpenDialog(any, null, 'Open Directory'));
});
});

group('#getSavePath', () {
setUp(() {
when(mockApi.showSaveDialog(any, any, any, any))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,55 @@ TEST(FileSelectorPlugin, TestGetDirectorySimple) {
EXPECT_EQ(std::get<std::string>(paths[0]), "C:\\Program Files");
}

TEST(FileSelectorPlugin, TestGetDirectoryMultiple) {
const HWND fake_window = reinterpret_cast<HWND>(1337);
// These are actual files, but since the plugin implementation doesn't
// validate the types of items returned from the system dialog, they are fine
// to use for unit tests.
ScopedTestFileIdList fake_selected_dir_1;
ScopedTestFileIdList fake_selected_dir_2;
LPCITEMIDLIST fake_selected_dirs[] = {
fake_selected_dir_1.file(),
fake_selected_dir_2.file(),
};
IShellItemArrayPtr fake_result_array;
::SHCreateShellItemArrayFromIDLists(2, fake_selected_dirs,
&fake_result_array);

bool shown = false;
MockShow show_validator = [&shown, fake_result_array, fake_window](
const TestFileDialogController& dialog,
HWND parent) {
shown = true;
EXPECT_EQ(parent, fake_window);

// Validate options.
FILEOPENDIALOGOPTIONS options;
dialog.GetOptions(&options);
EXPECT_NE(options & FOS_ALLOWMULTISELECT, 0U);
EXPECT_NE(options & FOS_PICKFOLDERS, 0U);

return MockShowResult(fake_result_array);
};

FileSelectorPlugin plugin(
[fake_window] { return fake_window; },
std::make_unique<TestFileDialogControllerFactory>(show_validator));
ErrorOr<EncodableList> result = plugin.ShowOpenDialog(
CreateOptions(AllowMultipleArg(true), SelectFoldersArg(true),
EncodableList()),
nullptr, nullptr);

EXPECT_TRUE(shown);
ASSERT_FALSE(result.has_error());
const EncodableList& paths = result.value();
EXPECT_EQ(paths.size(), 2);
EXPECT_EQ(std::get<std::string>(paths[0]),
Utf8FromUtf16(fake_selected_dir_1.path()));
EXPECT_EQ(std::get<std::string>(paths[1]),
Utf8FromUtf16(fake_selected_dir_2.path()));
}

TEST(FileSelectorPlugin, TestGetDirectoryCancel) {
const HWND fake_window = reinterpret_cast<HWND>(1337);

Expand Down

0 comments on commit 815cfca

Please sign in to comment.