Skip to content

Commit 6468c5e

Browse files
apremalalnploi
authored andcommitted
[webview_flutter] Add android webSettings.setTextZoom api (flutter#3298)
[webview_flutter] Add android `webSettings.setTextZoom` api
1 parent ecb13ac commit 6468c5e

16 files changed

+185
-2
lines changed

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 3.4.0
22

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

56
## 3.3.2

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/GeneratedAndroidWebView.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,8 @@ public interface WebSettingsHostApi {
16031603

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

1606+
void setTextZoom(@NonNull Long instanceId, @NonNull Long textZoom);
1607+
16061608
/** The codec used by WebSettingsHostApi. */
16071609
static MessageCodec<Object> getCodec() {
16081610
return new StandardMessageCodec();
@@ -2050,6 +2052,39 @@ static void setup(BinaryMessenger binaryMessenger, WebSettingsHostApi api) {
20502052
channel.setMessageHandler(null);
20512053
}
20522054
}
2055+
{
2056+
BasicMessageChannel<Object> channel =
2057+
new BasicMessageChannel<>(
2058+
binaryMessenger, "dev.flutter.pigeon.WebSettingsHostApi.setTextZoom", getCodec());
2059+
if (api != null) {
2060+
channel.setMessageHandler(
2061+
(message, reply) -> {
2062+
ArrayList<Object> wrapped = new ArrayList<Object>();
2063+
try {
2064+
ArrayList<Object> args = (ArrayList<Object>) message;
2065+
assert args != null;
2066+
Number instanceIdArg = (Number) args.get(0);
2067+
if (instanceIdArg == null) {
2068+
throw new NullPointerException("instanceIdArg unexpectedly null.");
2069+
}
2070+
Number textZoomArg = (Number) args.get(1);
2071+
if (textZoomArg == null) {
2072+
throw new NullPointerException("textZoomArg unexpectedly null.");
2073+
}
2074+
api.setTextZoom(
2075+
(instanceIdArg == null) ? null : instanceIdArg.longValue(),
2076+
(textZoomArg == null) ? null : textZoomArg.longValue());
2077+
wrapped.add(0, null);
2078+
} catch (Error | RuntimeException exception) {
2079+
ArrayList<Object> wrappedError = wrapError(exception);
2080+
wrapped = wrappedError;
2081+
}
2082+
reply.reply(wrapped);
2083+
});
2084+
} else {
2085+
channel.setMessageHandler(null);
2086+
}
2087+
}
20532088
}
20542089
}
20552090
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebSettingsHostApiImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,10 @@ public void setAllowFileAccess(Long instanceId, Boolean enabled) {
120120
final WebSettings webSettings = (WebSettings) instanceManager.getInstance(instanceId);
121121
webSettings.setAllowFileAccess(enabled);
122122
}
123+
124+
@Override
125+
public void setTextZoom(Long instanceId, Long textZoom) {
126+
final WebSettings webSettings = (WebSettings) instanceManager.getInstance(instanceId);
127+
webSettings.setTextZoom(textZoom.intValue());
128+
}
123129
}

packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebSettingsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,10 @@ public void setBuiltInZoomControls() {
107107
testHostApiImpl.setBuiltInZoomControls(0L, true);
108108
verify(mockWebSettings).setBuiltInZoomControls(true);
109109
}
110+
111+
@Test
112+
public void setTextZoom() {
113+
testHostApiImpl.setTextZoom(0L, 100L);
114+
verify(mockWebSettings).setTextZoom(100);
115+
}
110116
}

packages/webview_flutter/webview_flutter_android/lib/src/android_webview.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,13 @@ class WebSettings extends JavaObject {
591591
return api.setAllowFileAccessFromInstance(this, enabled);
592592
}
593593

594+
/// Sets the text zoom of the page in percent.
595+
///
596+
/// The default is 100. See https://developer.android.com/reference/android/webkit/WebSettings#setTextZoom(int)
597+
Future<void> setTextZoom(int textZoom) {
598+
return api.setSetTextZoomFromInstance(this, textZoom);
599+
}
600+
594601
@override
595602
WebSettings copy() {
596603
return WebSettings.detached();

packages/webview_flutter/webview_flutter_android/lib/src/android_webview.g.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,28 @@ class WebSettingsHostApi {
12501250
return;
12511251
}
12521252
}
1253+
1254+
Future<void> setTextZoom(int arg_instanceId, int arg_textZoom) async {
1255+
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
1256+
'dev.flutter.pigeon.WebSettingsHostApi.setTextZoom', codec,
1257+
binaryMessenger: _binaryMessenger);
1258+
final List<Object?>? replyList = await channel
1259+
.send(<Object?>[arg_instanceId, arg_textZoom]) as List<Object?>?;
1260+
if (replyList == null) {
1261+
throw PlatformException(
1262+
code: 'channel-error',
1263+
message: 'Unable to establish connection on channel.',
1264+
);
1265+
} else if (replyList.length > 1) {
1266+
throw PlatformException(
1267+
code: replyList[0]! as String,
1268+
message: replyList[1] as String?,
1269+
details: replyList[2],
1270+
);
1271+
} else {
1272+
return;
1273+
}
1274+
}
12531275
}
12541276

12551277
class JavaScriptChannelHostApi {

packages/webview_flutter/webview_flutter_android/lib/src/android_webview_api_impls.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ class WebSettingsHostApiImpl extends WebSettingsHostApi {
434434
return setSupportZoom(instanceManager.getIdentifier(instance)!, support);
435435
}
436436

437+
/// Helper method to convert instances ids to objects.
438+
Future<void> setSetTextZoomFromInstance(
439+
WebSettings instance,
440+
int textZoom,
441+
) {
442+
return setTextZoom(instanceManager.getIdentifier(instance)!, textZoom);
443+
}
444+
437445
/// Helper method to convert instances ids to objects.
438446
Future<void> setLoadWithOverviewModeFromInstance(
439447
WebSettings instance,

packages/webview_flutter/webview_flutter_android/lib/src/android_webview_controller.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ class AndroidWebViewController extends PlatformWebViewController {
355355
return _webView.settings.setMediaPlaybackRequiresUserGesture(require);
356356
}
357357

358+
/// Sets the text zoom of the page in percent.
359+
///
360+
/// The default is 100.
361+
Future<void> setTextZoom(int textZoom) =>
362+
_webView.settings.setTextZoom(textZoom);
363+
358364
/// Sets the callback that is invoked when the client should show a file
359365
/// selector.
360366
Future<void> setOnShowFileSelector(

packages/webview_flutter/webview_flutter_android/pigeons/android_webview.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ abstract class WebSettingsHostApi {
218218
void setBuiltInZoomControls(int instanceId, bool enabled);
219219

220220
void setAllowFileAccess(int instanceId, bool enabled);
221+
222+
void setTextZoom(int instanceId, int textZoom);
221223
}
222224

223225
@HostApi(dartHostTestHandler: 'TestJavaScriptChannelHostApi')

packages/webview_flutter/webview_flutter_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter_android
22
description: A Flutter plugin that provides a WebView widget on Android.
33
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 3.3.2
5+
version: 3.4.0
66

77
environment:
88
sdk: ">=2.17.0 <3.0.0"

0 commit comments

Comments
 (0)