Skip to content

Commit

Permalink
[url_launcher] Add support for setting show title on Chrome Custom Ta…
Browse files Browse the repository at this point in the history
…bs (#6097)

- Adds showTitle option to platform interface & app-facing api
- Makes use of that parameter on android platform in order to set show title on Chrome Custom Tabs, when it is being used
- Makes android implementation use both legacy & new api (instead of passthrough from new to legacy), in order to allow use of the aforementioned parameter
- Adds a button to the example to test this parameter
- Adds tests

![Screenshot_20231018-095928](https://github.com/flutter/packages/assets/40719830/2f32d83f-066b-4048-a439-04e2d63befde)
(Notice title at the top of the page "CyLog Software - HTTP ...")

flutter/flutter#136784
  • Loading branch information
Alex-Usmanov authored Jun 3, 2024
1 parent 24b0eb5 commit 539a5f9
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/url_launcher/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 6.3.0

* Adds `BrowserConfiguration` parameter, to configure in-app browser views, such as Android Custom Tabs or SFSafariViewController.
* Adds `showTitle` to `BrowserConfiguration`, to allow showing webpage titles in in-app browser views.
* Updates minimum supported SDK version to Flutter 3.16/Dart 3.2.

## 6.2.6
Expand Down
17 changes: 17 additions & 0 deletions packages/url_launcher/url_launcher/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

Future<void> _launchInAppWithBrowserOptions(Uri url) async {
if (!await launchUrl(
url,
mode: LaunchMode.inAppBrowserView,
browserConfiguration: const BrowserConfiguration(showTitle: true),
)) {
throw Exception('Could not launch $url');
}
}

Future<void> _launchAsInAppWebViewWithCustomHeaders(Uri url) async {
if (!await launchUrl(
url,
Expand Down Expand Up @@ -220,6 +230,13 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Text('Launch in app + close after 5 seconds'),
),
const Padding(padding: EdgeInsets.all(16.0)),
ElevatedButton(
onPressed: () => setState(() {
_launched = _launchInAppWithBrowserOptions(toLaunch);
}),
child: const Text('Launch in app with title displayed'),
),
const Padding(padding: EdgeInsets.all(16.0)),
Link(
uri: Uri.parse(
'https://pub.dev/documentation/url_launcher/latest/link/link-library.html'),
Expand Down
12 changes: 11 additions & 1 deletion packages/url_launcher/url_launcher/lib/src/type_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ import 'types.dart';

/// Converts an (app-facing) [WebViewConfiguration] to a (platform interface)
/// [InAppWebViewConfiguration].
InAppWebViewConfiguration convertConfiguration(WebViewConfiguration config) {
InAppWebViewConfiguration convertWebViewConfiguration(
WebViewConfiguration config) {
return InAppWebViewConfiguration(
enableJavaScript: config.enableJavaScript,
enableDomStorage: config.enableDomStorage,
headers: config.headers,
);
}

/// Converts an (app-facing) [BrowserConfiguration] to a (platform interface)
/// [InAppBrowserConfiguration].
InAppBrowserConfiguration convertBrowserConfiguration(
BrowserConfiguration config) {
return InAppBrowserConfiguration(
showTitle: config.showTitle,
);
}

/// Converts an (app-facing) [LaunchMode] to a (platform interface)
/// [PreferredLaunchMode].
PreferredLaunchMode convertLaunchMode(LaunchMode mode) {
Expand Down
12 changes: 12 additions & 0 deletions packages/url_launcher/url_launcher/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ class WebViewConfiguration {
/// Not all browsers support this, so it is not guaranteed to be honored.
final Map<String, String> headers;
}

/// Additional configuration options for [LaunchMode.inAppBrowserView]
@immutable
class BrowserConfiguration {
/// Creates a new InAppBrowserConfiguration with given settings.
const BrowserConfiguration({this.showTitle = false});

/// Whether or not to show the webpage title.
///
/// May not be supported on all platforms.
final bool showTitle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Future<bool> launchUrlString(
String urlString, {
LaunchMode mode = LaunchMode.platformDefault,
WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
BrowserConfiguration browserConfiguration = const BrowserConfiguration(),
String? webOnlyWindowName,
}) async {
if ((mode == LaunchMode.inAppWebView ||
Expand All @@ -35,7 +36,8 @@ Future<bool> launchUrlString(
urlString,
LaunchOptions(
mode: convertLaunchMode(mode),
webViewConfiguration: convertConfiguration(webViewConfiguration),
webViewConfiguration: convertWebViewConfiguration(webViewConfiguration),
browserConfiguration: convertBrowserConfiguration(browserConfiguration),
webOnlyWindowName: webOnlyWindowName,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Future<bool> launchUrl(
Uri url, {
LaunchMode mode = LaunchMode.platformDefault,
WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
BrowserConfiguration browserConfiguration = const BrowserConfiguration(),
String? webOnlyWindowName,
}) async {
if ((mode == LaunchMode.inAppWebView ||
Expand All @@ -52,7 +53,8 @@ Future<bool> launchUrl(
url.toString(),
LaunchOptions(
mode: convertLaunchMode(mode),
webViewConfiguration: convertConfiguration(webViewConfiguration),
webViewConfiguration: convertWebViewConfiguration(webViewConfiguration),
browserConfiguration: convertBrowserConfiguration(browserConfiguration),
webOnlyWindowName: webOnlyWindowName,
),
);
Expand Down
6 changes: 3 additions & 3 deletions packages/url_launcher/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL. Supports
web, phone, SMS, and email schemes.
repository: https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
version: 6.2.6
version: 6.3.0

environment:
sdk: ">=3.2.0 <4.0.0"
Expand All @@ -28,13 +28,13 @@ flutter:
dependencies:
flutter:
sdk: flutter
url_launcher_android: ^6.2.0
url_launcher_android: ^6.3.0
url_launcher_ios: ^6.2.4
# Allow either the pure-native or Dart/native hybrid versions of the desktop
# implementations, as both are compatible.
url_launcher_linux: ^3.1.0
url_launcher_macos: ^3.1.0
url_launcher_platform_interface: ^2.2.0
url_launcher_platform_interface: ^2.3.0
url_launcher_web: ^2.2.0
url_launcher_windows: ^3.1.0

Expand Down
2 changes: 2 additions & 0 deletions packages/url_launcher/url_launcher/test/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void main() {
enableDomStorage: true,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
await followLink!();
Expand Down Expand Up @@ -92,6 +93,7 @@ void main() {
enableDomStorage: true,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
await followLink!();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MockUrlLauncher extends Fake
bool? enableJavaScript;
bool? enableDomStorage;
bool? universalLinksOnly;
bool? showTitle;
Map<String, String>? headers;
String? webOnlyWindowName;

Expand All @@ -41,6 +42,7 @@ class MockUrlLauncher extends Fake
required bool universalLinksOnly,
required Map<String, String> headers,
required String? webOnlyWindowName,
required bool showTitle,
}) {
this.url = url;
this.launchMode = launchMode;
Expand All @@ -51,6 +53,7 @@ class MockUrlLauncher extends Fake
this.universalLinksOnly = universalLinksOnly;
this.headers = headers;
this.webOnlyWindowName = webOnlyWindowName;
this.showTitle = showTitle;
}

// ignore: use_setters_to_change_properties
Expand Down Expand Up @@ -87,6 +90,7 @@ class MockUrlLauncher extends Fake
expect(universalLinksOnly, this.universalLinksOnly);
expect(headers, this.headers);
expect(webOnlyWindowName, this.webOnlyWindowName);
expect(webOnlyWindowName, this.webOnlyWindowName);
launchCalled = true;
return response!;
}
Expand All @@ -98,6 +102,7 @@ class MockUrlLauncher extends Fake
expect(options.webViewConfiguration.enableJavaScript, enableJavaScript);
expect(options.webViewConfiguration.enableDomStorage, enableDomStorage);
expect(options.webViewConfiguration.headers, headers);
expect(options.browserConfiguration.showTitle, showTitle);
expect(options.webOnlyWindowName, webOnlyWindowName);
launchCalled = true;
return response!;
Expand Down
12 changes: 12 additions & 0 deletions packages/url_launcher/url_launcher/test/src/legacy_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(await launch('http://flutter.dev/'), isTrue);
Expand All @@ -69,6 +70,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{'key': 'value'},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(
Expand All @@ -90,6 +92,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(await launch('http://flutter.dev/', forceSafariVC: true), isTrue);
Expand All @@ -106,6 +109,7 @@ void main() {
universalLinksOnly: true,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(
Expand All @@ -125,6 +129,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(await launch('http://flutter.dev/', forceWebView: true), isTrue);
Expand All @@ -141,6 +146,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(
Expand All @@ -160,6 +166,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(
Expand All @@ -179,6 +186,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(await launch('http://flutter.dev/', forceSafariVC: false), isTrue);
Expand All @@ -200,6 +208,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(await launch('mailto:gmail-noreply@google.com?subject=Hello'),
Expand Down Expand Up @@ -231,6 +240,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);

Expand Down Expand Up @@ -263,6 +273,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);

Expand Down Expand Up @@ -296,6 +307,7 @@ void main() {
universalLinksOnly: false,
headers: <String, String>{},
webOnlyWindowName: null,
showTitle: false,
)
..setResponse(true);
expect(
Expand Down
Loading

0 comments on commit 539a5f9

Please sign in to comment.