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

[url_launcher]added windowName parameter in url_launcher #2926

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 web only parameter

## 5.5.0

* Support Linux by default.
Expand Down
41 changes: 31 additions & 10 deletions packages/url_launcher/url_launcher/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
name: url_launcher_example
description: Demonstrates how to use the url_launcher plugin.
name: url_launcher_web
description: Web platform implementation of url_launcher
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_web
# 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.3
flutter:
plugin:
platforms:
web:
pluginClass: UrlLauncherPlugin
fileName: url_launcher_web.dart

dependencies:
url_launcher_platform_interface:
git:
url: https://github.com/balvinderz/plugins
ref: windowname
path: packages/url_launcher/url_launcher_platform_interface
platform_detect: ^1.4.0
flutter:
sdk: flutter
url_launcher:
path: ../
flutter_web_plugins:
sdk: flutter
meta: ^1.1.7

dev_dependencies:
e2e:
path: ../../../e2e
flutter_driver:
flutter_test:
sdk: flutter
url_launcher:
git:
url: https://github.com/balvinderz/plugins
ref: windowname
path: packages/url_launcher/url_launcher
pedantic: ^1.8.0
mockito: ^4.1.1
plugin_platform_interface: ^1.0.0

flutter:
uses-material-design: true
environment:
sdk: ">=2.2.0 <3.0.0"
flutter: ">=1.10.0 <2.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,32 @@ void main() {
const Map<String, String> defaultHeaders = {
'my_header_key': 'my_header_value'
};
verifyNever(mock.launch(defaultUrl,
useSafariVC: false,
useWebView: false,
enableDomStorage: false,
enableJavaScript: false,
universalLinksOnly: false,
headers: defaultHeaders));
verifyNever(mock.launch(
defaultUrl,
useSafariVC: false,
useWebView: false,
enableDomStorage: false,
enableJavaScript: false,
universalLinksOnly: false,
headers: defaultHeaders,
));

Finder browserlaunchBtn =
find.widgetWithText(RaisedButton, 'Launch in browser');
expect(browserlaunchBtn, findsOneWidget);
await tester.tap(browserlaunchBtn);

verify(mock.launch(defaultUrl,
useSafariVC: false,
useWebView: false,
enableDomStorage: false,
enableJavaScript: false,
universalLinksOnly: false,
headers: defaultHeaders))
.called(1);
verify(
mock.launch(
defaultUrl,
useSafariVC: false,
useWebView: false,
enableDomStorage: false,
enableJavaScript: false,
universalLinksOnly: false,
headers: defaultHeaders,
),
).called(1);
});
}

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
14 changes: 11 additions & 3 deletions 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 All @@ -22,13 +22,21 @@ flutter:
dependencies:
flutter:
sdk: flutter
url_launcher_platform_interface: ^1.0.4
url_launcher_platform_interface:
git:
url: https://github.com/balvinderz/plugins
ref: windowname
path: packages/url_launcher/url_launcher_platform_interface
Comment on lines +26 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the plugin unpublishable. Once we figure out all the rest of the changes, you'll have to split this PR in 3: one for each of the packages you've touched (and merge them in the correct order)

# The design on https://flutter.dev/go/federated-plugins was to leave
# this constraint as "any". We cannot do it right now as it fails pub publish
# validation, so we set a ^ constraint.
# TODO(amirh): Revisit this (either update this part in the design or the pub tool).
# https://github.com/flutter/flutter/issues/46264
url_launcher_web: ^0.1.0+1
url_launcher_web:
git:
url: https://github.com/balvinderz/plugins
ref: windowname
path: packages/url_launcher/url_launcher_web
url_launcher_linux: ^0.0.1
url_launcher_macos: ^0.0.1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.8

* Added webOnlyWindowName parameter

## 1.0.7

* Update lower bound of dart dependency to 2.1.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MethodChannelUrlLauncher extends UrlLauncherPlatform {
@required bool enableDomStorage,
@required bool universalLinksOnly,
@required Map<String, String> headers,
String webOnlyWindowName,
}) {
return _channel.invokeMethod<bool>(
'launch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ abstract class UrlLauncherPlatform extends PlatformInterface {
@required bool enableDomStorage,
@required bool universalLinksOnly,
@required Map<String, String> headers,
String webOnlyWindowName,
}) {
throw UnimplementedError('launch() has not been implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the url_launcher plugin.
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.7
version: 1.0.8

dependencies:
flutter:
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 "windowName" parameter that lets you launch url in new tab,same tab or in an iframe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call the parameter:

webOnlyWindowName everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this doc! :)


# 0.1.2+1

- Update docs
Expand Down
11 changes: 6 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,8 @@ 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);
}
}
12 changes: 8 additions & 4 deletions packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +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:
platforms:
Expand All @@ -14,7 +13,11 @@ flutter:
fileName: url_launcher_web.dart

dependencies:
url_launcher_platform_interface: ^1.0.1
url_launcher_platform_interface:
git:
url: https://github.com/balvinderz/plugins
ref: windowname
path: packages/url_launcher/url_launcher_platform_interface
platform_detect: ^1.4.0
flutter:
sdk: flutter
Expand All @@ -25,7 +28,8 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
url_launcher: ^5.2.5
url_launcher:
path: ../url_launcher
pedantic: ^1.8.0
mockito: ^4.1.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ void main() {

verify(mockWindow.open('http://www.google.com', ''));
});

test('https urls should be launched in a new window', () {
plugin.openNewWindow('https://www.google.com');

Expand All @@ -146,6 +145,20 @@ 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(() {
platform.configurePlatformForTesting(browser: platform.safari);
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:name@mydomain.com",
webOnlyWindowName: "_blank");
verify(mockWindow.open("mailto:name@mydomain.com", "_blank"));
});
});
});
});
Expand Down