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

Commit

Permalink
[url_launcher, url_launcher_web]Added webOnlyWindowName parameter to …
Browse files Browse the repository at this point in the history
…launch() (#2979)
  • Loading branch information
balvinderz authored Aug 28, 2020
1 parent 161aed2 commit 7c49361
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.5.1

* Added webOnlyWindowName parameter to launch()

## 5.5.0

* Support Linux by default.
Expand Down
5 changes: 5 additions & 0 deletions packages/url_launcher/url_launcher/lib/url_launcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ import 'package:url_launcher_platform_interface/url_launcher_platform_interface.
/// [enableDomStorage] is an Android only setting. If true, WebView enable
/// DOM storage.
/// [headers] is an Android only setting that adds headers to the WebView.
/// [webOnlyWindowName] is an Web only setting . _blank opens the new url in new tab ,
/// _self opens the new url in current tab.
/// Default behaviour is to open the url in new tab.
///
/// Note that if any of the above are set to true but the URL is not a web URL,
/// this will throw a [PlatformException].
Expand All @@ -63,6 +66,7 @@ Future<bool> launch(
bool universalLinksOnly,
Map<String, String> headers,
Brightness statusBarBrightness,
String webOnlyWindowName,
}) async {
assert(urlString != null);
final Uri url = Uri.parse(urlString.trimLeft());
Expand Down Expand Up @@ -93,6 +97,7 @@ Future<bool> launch(
enableDomStorage: enableDomStorage ?? false,
universalLinksOnly: universalLinksOnly ?? false,
headers: headers ?? <String, String>{},
webOnlyWindowName: webOnlyWindowName,
);
assert(previousAutomaticSystemUiAdjustment != null);
if (statusBarBrightness != null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher
description: Flutter plugin for launching a URL on Android and iOS. Supports
web, phone, SMS, and email schemes.
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher
version: 5.5.0
version: 5.5.1

flutter:
plugin:
Expand Down
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.3

- Added webOnlyWindowName parameter to launch()

# 0.1.2+1

- Update docs
Expand Down
12 changes: 7 additions & 5 deletions packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
bool _isSafariTargetTopScheme(String url) =>
_safariTargetTopSchemes.contains(_getUrlScheme(url));

/// Opens the given [url] in a new window.
/// Opens the given [url] in the specified [webOnlyWindowName].
///
/// Returns the newly created window.
@visibleForTesting
html.WindowBase openNewWindow(String url) {
html.WindowBase openNewWindow(String url, {String webOnlyWindowName}) {
// We need to open mailto, tel and sms urls on the _top window context on safari browsers.
// See https://github.com/flutter/flutter/issues/51461 for reference.
final target =
browser.isSafari && _isSafariTargetTopScheme(url) ? '_top' : '';
final target = webOnlyWindowName ??
((browser.isSafari && _isSafariTargetTopScheme(url)) ? '_top' : '');
return _window.open(url, target);
}

Expand All @@ -65,7 +65,9 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
@required bool enableDomStorage,
@required bool universalLinksOnly,
@required Map<String, String> headers,
String webOnlyWindowName,
}) {
return Future<bool>.value(openNewWindow(url) != null);
return Future<bool>.value(
openNewWindow(url, webOnlyWindowName: webOnlyWindowName) != null);
}
}
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/u
# 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.1.2+1
version: 0.1.3

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ void main() {

verify(mockWindow.open('sms:+19725551212?body=hello%20there', ''));
});
test('setting oOnlyLinkTarget as _self opens the url in the same tab',
() {
plugin.openNewWindow("https://www.google.com",
webOnlyWindowName: "_self");
verify(mockWindow.open('https://www.google.com', '_self'));
});

test('setting webOnlyLinkTarget as _blank opens the url in a new tab',
() {
plugin.openNewWindow("https://www.google.com",
webOnlyWindowName: "_blank");
verify(mockWindow.open('https://www.google.com', '_blank'));
});

group('Safari', () {
setUp(() {
Expand Down Expand Up @@ -181,6 +194,13 @@ void main() {
verify(
mockWindow.open('sms:+19725551212?body=hello%20there', '_top'));
});
test(
'mailto urls should use _blank if webOnlyWindowName is set as _blank',
() {
plugin.openNewWindow("mailto:[email protected]",
webOnlyWindowName: "_blank");
verify(mockWindow.open("mailto:[email protected]", "_blank"));
});
});
});
});
Expand Down

0 comments on commit 7c49361

Please sign in to comment.