Skip to content

[webview_flutter] Add android webSettings.setTextZoom api #3298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 8, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 3.4.0

* Adds support to set text zoom of a page. See `AndroidWebViewController.setTextZoom`.
* Aligns Dart and Flutter SDK constraints.

## 3.3.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,8 @@ public interface WebSettingsHostApi {

void setAllowFileAccess(@NonNull Long instanceId, @NonNull Boolean enabled);

void setTextZoom(@NonNull Long instanceId, @NonNull Long textZoom);

/** The codec used by WebSettingsHostApi. */
static MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
Expand Down Expand Up @@ -2050,6 +2052,39 @@ static void setup(BinaryMessenger binaryMessenger, WebSettingsHostApi api) {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.WebSettingsHostApi.setTextZoom", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
try {
ArrayList<Object> args = (ArrayList<Object>) message;
assert args != null;
Number instanceIdArg = (Number) args.get(0);
if (instanceIdArg == null) {
throw new NullPointerException("instanceIdArg unexpectedly null.");
}
Number textZoomArg = (Number) args.get(1);
if (textZoomArg == null) {
throw new NullPointerException("textZoomArg unexpectedly null.");
}
api.setTextZoom(
(instanceIdArg == null) ? null : instanceIdArg.longValue(),
(textZoomArg == null) ? null : textZoomArg.longValue());
wrapped.add(0, null);
} catch (Error | RuntimeException exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ public void setAllowFileAccess(Long instanceId, Boolean enabled) {
final WebSettings webSettings = (WebSettings) instanceManager.getInstance(instanceId);
webSettings.setAllowFileAccess(enabled);
}

@Override
public void setTextZoom(Long instanceId, Long textZoom) {
final WebSettings webSettings = (WebSettings) instanceManager.getInstance(instanceId);
webSettings.setTextZoom(textZoom.intValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ public void setBuiltInZoomControls() {
testHostApiImpl.setBuiltInZoomControls(0L, true);
verify(mockWebSettings).setBuiltInZoomControls(true);
}

@Test
public void setTextZoom() {
testHostApiImpl.setTextZoom(0L, 100L);
verify(mockWebSettings).setTextZoom(100);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ class WebSettings extends JavaObject {
return api.setAllowFileAccessFromInstance(this, enabled);
}

/// Sets the text zoom of the page in percent.
///
/// The default is 100. See https://developer.android.com/reference/android/webkit/WebSettings#setTextZoom(int)
Future<void> setTextZoom(int textZoom) {
return api.setSetTextZoomFromInstance(this, textZoom);
}

@override
WebSettings copy() {
return WebSettings.detached();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,28 @@ class WebSettingsHostApi {
return;
}
}

Future<void> setTextZoom(int arg_instanceId, int arg_textZoom) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.WebSettingsHostApi.setTextZoom', codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList = await channel
.send(<Object?>[arg_instanceId, arg_textZoom]) as List<Object?>?;
if (replyList == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyList.length > 1) {
throw PlatformException(
code: replyList[0]! as String,
message: replyList[1] as String?,
details: replyList[2],
);
} else {
return;
}
}
}

class JavaScriptChannelHostApi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ class WebSettingsHostApiImpl extends WebSettingsHostApi {
return setSupportZoom(instanceManager.getIdentifier(instance)!, support);
}

/// Helper method to convert instances ids to objects.
Future<void> setSetTextZoomFromInstance(
WebSettings instance,
int textZoom,
) {
return setTextZoom(instanceManager.getIdentifier(instance)!, textZoom);
}

/// Helper method to convert instances ids to objects.
Future<void> setLoadWithOverviewModeFromInstance(
WebSettings instance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ class AndroidWebViewController extends PlatformWebViewController {
return _webView.settings.setMediaPlaybackRequiresUserGesture(require);
}

/// Sets the text zoom of the page in percent.
///
/// The default is 100.
Future<void> setTextZoom(int textZoom) =>
_webView.settings.setTextZoom(textZoom);

/// Sets the callback that is invoked when the client should show a file
/// selector.
Future<void> setOnShowFileSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ abstract class WebSettingsHostApi {
void setBuiltInZoomControls(int instanceId, bool enabled);

void setAllowFileAccess(int instanceId, bool enabled);

void setTextZoom(int instanceId, int textZoom);
}

@HostApi(dartHostTestHandler: 'TestJavaScriptChannelHostApi')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_android
description: A Flutter plugin that provides a WebView widget on Android.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 3.3.2
version: 3.4.0

environment:
sdk: ">=2.17.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,22 @@ void main() {
verify(mockSettings.setMediaPlaybackRequiresUserGesture(true)).called(1);
});

test('setTextZoom', () async {
final MockWebView mockWebView = MockWebView();
final MockWebSettings mockSettings = MockWebSettings();
final AndroidWebViewController controller = createControllerWithMocks(
mockWebView: mockWebView,
mockSettings: mockSettings,
);

clearInteractions(mockWebView);

await controller.setTextZoom(100);

verify(mockWebView.settings).called(1);
verify(mockSettings.setTextZoom(100)).called(1);
});

test('webViewIdentifier', () {
final MockWebView mockWebView = MockWebView();
final InstanceManager instanceManager = InstanceManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,15 @@ class MockAndroidWebViewController extends _i1.Mock
returnValueForMissingStub: _i9.Future<void>.value(),
) as _i9.Future<void>);
@override
_i9.Future<void> setTextZoom(int? textZoom) => (super.noSuchMethod(
Invocation.method(
#setTextZoom,
[textZoom],
),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
) as _i9.Future<void>);
@override
_i9.Future<void> setOnShowFileSelector(
_i9.Future<List<String>> Function(_i8.FileSelectorParams)?
onShowFileSelector) =>
Expand Down Expand Up @@ -1737,6 +1746,15 @@ class MockWebSettings extends _i1.Mock implements _i2.WebSettings {
returnValueForMissingStub: _i9.Future<void>.value(),
) as _i9.Future<void>);
@override
_i9.Future<void> setTextZoom(int? textZoom) => (super.noSuchMethod(
Invocation.method(
#setTextZoom,
[textZoom],
),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
) as _i9.Future<void>);
@override
_i2.WebSettings copy() => (super.noSuchMethod(
Invocation.method(
#copy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ void main() {
test('copy', () {
expect(webSettings.copy(), isA<WebSettings>());
});

test('setTextZoom', () {
webSettings.setTextZoom(100);
verify(mockPlatformHostApi.setTextZoom(
webSettingsInstanceId,
100,
));
});
});

group('JavaScriptChannel', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,21 @@ class MockTestWebSettingsHostApi extends _i1.Mock
),
returnValueForMissingStub: null,
);
@override
void setTextZoom(
int? instanceId,
int? textZoom,
) =>
super.noSuchMethod(
Invocation.method(
#setTextZoom,
[
instanceId,
textZoom,
],
),
returnValueForMissingStub: null,
);
}

/// A class which mocks [TestWebStorageHostApi].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ class MockWebSettings extends _i1.Mock implements _i2.WebSettings {
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setTextZoom(int? textZoom) => (super.noSuchMethod(
Invocation.method(
#setTextZoom,
[textZoom],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i2.WebSettings copy() => (super.noSuchMethod(
Invocation.method(
#copy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ abstract class TestWebSettingsHostApi {

void setAllowFileAccess(int instanceId, bool enabled);

void setTextZoom(int instanceId, int textZoom);

static void setup(TestWebSettingsHostApi? api,
{BinaryMessenger? binaryMessenger}) {
{
Expand Down Expand Up @@ -1012,6 +1014,28 @@ abstract class TestWebSettingsHostApi {
});
}
}
{
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.WebSettingsHostApi.setTextZoom', codec,
binaryMessenger: binaryMessenger);
if (api == null) {
channel.setMockMessageHandler(null);
} else {
channel.setMockMessageHandler((Object? message) async {
assert(message != null,
'Argument for dev.flutter.pigeon.WebSettingsHostApi.setTextZoom was null.');
final List<Object?> args = (message as List<Object?>?)!;
final int? arg_instanceId = (args[0] as int?);
assert(arg_instanceId != null,
'Argument for dev.flutter.pigeon.WebSettingsHostApi.setTextZoom was null, expected non-null int.');
final int? arg_textZoom = (args[1] as int?);
assert(arg_textZoom != null,
'Argument for dev.flutter.pigeon.WebSettingsHostApi.setTextZoom was null, expected non-null int.');
api.setTextZoom(arg_instanceId!, arg_textZoom!);
return <Object?>[];
});
}
}
}
}

Expand Down