Skip to content

Commit

Permalink
[file_selector] Add macOS support (flutter#4381)
Browse files Browse the repository at this point in the history
Brings file_selector_macos into flutter/plugins from FDE, with the following changes:
- Refactored slightly to allow for unit tests of almost all of the native code
- Added native unit test coverage
- Translated to Swift, to follow repo conventions for macOS plugins
- Added an in-package example (almost an exact duplicate of the app-facing version, but written against the platform interface, as is our current practice)
- Moved to an in-package method channel. As part of that, moved the flattening of type groups from Swift to Dart.

Does not currently include native UI tests to allow for end-to-end testing (since Flutter integration tests can't be used). They should be added later (they are currently blocked on flutter/flutter#90673), but the unit tests give substantial coverage, making it substantially better to move the plugin now to get those tests running.

macOS portion of flutter/flutter#70221
  • Loading branch information
stuartmorgan committed Mar 4, 2022
1 parent bed9aa7 commit 77e841d
Show file tree
Hide file tree
Showing 53 changed files with 3,171 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,4 @@ task:
native_test_script:
- ./script/tool_runner.sh native-test --macos
drive_script:
- ./script/tool_runner.sh drive-examples --macos
- ./script/tool_runner.sh drive-examples --macos --exclude=script/configs/exclude_integration_macos.yaml
5 changes: 5 additions & 0 deletions packages/file_selector/file_selector_macos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dart_tool
.packages
.flutter-plugins
.flutter-plugins-dependencies
pubspec.lock
10 changes: 10 additions & 0 deletions packages/file_selector/file_selector_macos/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 6d1c244b79f3a2747281f718297ce248bd5ad099
channel: master

project_type: plugin
6 changes: 6 additions & 0 deletions packages/file_selector/file_selector_macos/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Below is a list of people and organizations that have contributed
# to the Flutter project. Names should be added to the list like so:
#
# Name/Organization <email address>

Google Inc.
26 changes: 26 additions & 0 deletions packages/file_selector/file_selector_macos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## 0.8.2

* Moves source to flutter/plugins.
* Adds native unit tests.
* Converts native implementation to Swift.
* Switches to an internal method channel implementation.

## 0.0.4+1

* Update README

## 0.0.4

* Treat empty filter lists the same as null.

## 0.0.3

* Fix README

## 0.0.2

* Update SDK constraint to signal compatibility with null safety.

## 0.0.1

* Initial macOS implementation of `file_selector`.
25 changes: 25 additions & 0 deletions packages/file_selector/file_selector_macos/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright 2013 The Flutter Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 changes: 34 additions & 0 deletions packages/file_selector/file_selector_macos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# file\_selector\_macos

The macOS implementation of [`file_selector`][1].

## Usage

### Importing the package

This implementation has not yet been endorsed, meaning that you need to
[depend on `file_selector_macos`][2] in addition to
[depending on `file_selector`][3].

Once your pubspec includes the macOS implementation, you can use the
`file_selector` APIs normally. You should not use the `file_selector_macos`
APIs directly.

### Entitlements

You will need to [add an entitlement][4] for either read-only access:
```
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
```
or read/write access:
```
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
```
depending on your use case.

[1]: https://pub.dev/packages/file_selector
[2]: https://pub.dev/packages/file_selector_macos/install
[3]: https://pub.dev/packages/file_selector/install
[4]: https://flutter.dev/desktop#entitlements-and-the-app-sandbox
48 changes: 48 additions & 0 deletions packages/file_selector/file_selector_macos/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Currently only web supported
android/
ios/

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 10 additions & 0 deletions packages/file_selector/file_selector_macos/example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 7736f3bc90270dcb0480db2ccffbf1d13c28db85
channel: dev

project_type: app
4 changes: 4 additions & 0 deletions packages/file_selector/file_selector_macos/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# `file_selector_macos` example

Demonstrates macOS implementation of the
[`file_selector` plugin](https://pub.dev/packages/file_selector).
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 a directory using `getDirectoryPath`,
/// then displays the selected directory in a dialog.
class GetDirectoryPage extends StatelessWidget {
Future<void> _getDirectoryPath(BuildContext context) async {
const String confirmButtonText = 'Choose';
final String? directoryPath =
await FileSelectorPlatform.instance.getDirectoryPath(
confirmButtonText: confirmButtonText,
);
if (directoryPath == null) {
// Operation was canceled by the user.
return;
}
await showDialog<void>(
context: context,
builder: (BuildContext context) => TextDisplay(directoryPath),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Open a text file'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.blue,
onPrimary: Colors.white,
),
child: const Text('Press to ask user to choose a directory'),
onPressed: () => _getDirectoryPath(context),
),
],
),
),
);
}
}

/// Widget that displays a text file in a dialog.
class TextDisplay extends StatelessWidget {
/// Creates a `TextDisplay`.
const TextDisplay(this.directoryPath);

/// The path selected in the dialog.
final String directoryPath;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Selected Directory'),
content: Scrollbar(
child: SingleChildScrollView(
child: Text(directoryPath),
),
),
actions: <Widget>[
TextButton(
child: const Text('Close'),
onPressed: () => Navigator.pop(context),
),
],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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:flutter/material.dart';

/// Home Page of the application.
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final ButtonStyle style = ElevatedButton.styleFrom(
primary: Colors.blue,
onPrimary: Colors.white,
);
return Scaffold(
appBar: AppBar(
title: const Text('File Selector Demo Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: style,
child: const Text('Open a text file'),
onPressed: () => Navigator.pushNamed(context, '/open/text'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open an image'),
onPressed: () => Navigator.pushNamed(context, '/open/image'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open multiple images'),
onPressed: () => Navigator.pushNamed(context, '/open/images'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Save a file'),
onPressed: () => Navigator.pushNamed(context, '/save/text'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open a get directory dialog'),
onPressed: () => Navigator.pushNamed(context, '/directory'),
),
],
),
),
);
}
}
37 changes: 37 additions & 0 deletions packages/file_selector/file_selector_macos/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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:example/get_directory_page.dart';
import 'package:example/home_page.dart';
import 'package:example/open_image_page.dart';
import 'package:example/open_multiple_images_page.dart';
import 'package:example/open_text_page.dart';
import 'package:example/save_text_page.dart';
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

/// MyApp is the Main Application.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'File Selector Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
routes: <String, WidgetBuilder>{
'/open/image': (BuildContext context) => OpenImagePage(),
'/open/images': (BuildContext context) => OpenMultipleImagesPage(),
'/open/text': (BuildContext context) => OpenTextPage(),
'/save/text': (BuildContext context) => SaveTextPage(),
'/directory': (BuildContext context) => GetDirectoryPage(),
},
);
}
}
Loading

0 comments on commit 77e841d

Please sign in to comment.