Skip to content

Commit 12b69d6

Browse files
authored
[web] Grow the iOS view when the browser toolbar collapses (flutter#193744)
Fixes flutter#193743 ## Problem On iOS, the full-page Flutter view is sized from `documentElement.clientHeight`. When the page scrolls and Safari or Chrome collapses its toolbar, the visible area grows but `clientHeight` keeps the toolbar-expanded height, so the view stays short and the page shows through as a white strip at the bottom. ## Fix On iOS, use `visualViewport.height` when it is taller than `clientHeight` and has the same width, and keep `clientHeight` otherwise, so the view never shrinks and the post-rotation case from flutter#81430 is unchanged. The keyboard inset uses the same height, because a height-only change would report the toolbar's space as an open keyboard. ## Demo Swipe up on an iPhone until the browser toolbar collapses. Before, with a white strip at the bottom: https://flutter-demo-71-before.web.app After, with no strip: https://flutter-demo-71-after.web.app
1 parent 383b618 commit 12b69d6

2 files changed

Lines changed: 126 additions & 13 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/view_embedder/dimensions_provider/full_page_dimensions_provider.dart‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,12 @@ class FullPageDimensionsProvider extends DimensionsProvider {
8787

8888
if (viewport != null) {
8989
if (ui_web.browser.operatingSystem == ui_web.OperatingSystem.iOs) {
90-
/// Chrome on iOS reports incorrect viewport.height when app
91-
/// starts in portrait orientation and the phone is rotated to
92-
/// landscape.
93-
///
94-
/// We instead use documentElement clientWidth/Height to read
95-
/// accurate physical size. VisualViewport api is only used during
96-
/// text editing to make sure inset is correctly reported to
97-
/// framework.
98-
final double docWidth = domDocument.documentElement!.clientWidth;
99-
final double docHeight = domDocument.documentElement!.clientHeight;
100-
windowInnerWidth = docWidth * devicePixelRatio;
101-
windowInnerHeight = docHeight * devicePixelRatio;
90+
// Chrome on iOS reports incorrect viewport.height when app
91+
// starts in portrait orientation and the phone is rotated to
92+
// landscape, so the size is read from documentElement instead. See
93+
// `_iOSHeight` for when the height comes from the visual viewport.
94+
windowInnerWidth = domDocument.documentElement!.clientWidth * devicePixelRatio;
95+
windowInnerHeight = _iOSHeight(viewport) * devicePixelRatio;
10296
} else {
10397
windowInnerWidth = viewport.width! * devicePixelRatio;
10498
windowInnerHeight = viewport.height! * devicePixelRatio;
@@ -110,6 +104,28 @@ class FullPageDimensionsProvider extends DimensionsProvider {
110104
return ui.Size(windowInnerWidth, windowInnerHeight);
111105
}
112106

107+
/// The view height on iOS, in CSS pixels.
108+
///
109+
/// This is normally `documentElement.clientHeight`. When the page scrolls,
110+
/// iOS collapses the browser toolbar and the visible area grows, but
111+
/// `clientHeight` keeps the toolbar-expanded height. In that case the view
112+
/// uses `visualViewport.height` once it is at least a pixel taller, but only
113+
/// while the visual viewport has the same width as the document, so a stale
114+
/// value from the previous orientation during a rotation is ignored.
115+
///
116+
/// The view never shrinks below `clientHeight`. A visual viewport that is
117+
/// too short, as Chrome on iOS reports after rotating, or one that the
118+
/// on-screen keyboard shrinks, keeps the `clientHeight` view, and the
119+
/// keyboard is reported as an inset.
120+
double _iOSHeight(DomVisualViewport viewport) {
121+
final DomElement documentElement = domDocument.documentElement!;
122+
final double docWidth = documentElement.clientWidth;
123+
final double docHeight = documentElement.clientHeight;
124+
final double viewportHeight = viewport.height!;
125+
final bool sameOrientation = (viewport.width! - docWidth).abs() < 1;
126+
return sameOrientation && viewportHeight - docHeight >= 1 ? viewportHeight : docHeight;
127+
}
128+
113129
@override
114130
ViewPadding computeKeyboardInsets(double physicalHeight, bool isEditingOnMobile) {
115131
final double devicePixelRatio = EngineFlutterDisplay.instance.devicePixelRatio;
@@ -118,7 +134,9 @@ class FullPageDimensionsProvider extends DimensionsProvider {
118134

119135
if (viewport != null) {
120136
if (ui_web.browser.operatingSystem == ui_web.OperatingSystem.iOs && !isEditingOnMobile) {
121-
windowInnerHeight = domDocument.documentElement!.clientHeight * devicePixelRatio;
137+
// Must match computePhysicalSize, or the difference would be reported
138+
// as an open keyboard.
139+
windowInnerHeight = _iOSHeight(viewport) * devicePixelRatio;
122140
} else {
123141
windowInnerHeight = viewport.height! * devicePixelRatio;
124142
}

‎engine/src/flutter/lib/web_ui/test/engine/view_embedder/dimensions_provider/full_page_dimensions_provider_test.dart‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:js_interop';
7+
import 'dart:js_interop_unsafe';
68

79
import 'package:test/bootstrap/browser.dart';
810
import 'package:test/test.dart';
911
import 'package:ui/src/engine.dart';
1012
import 'package:ui/ui.dart' as ui show Size;
13+
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
1114

1215
void main() {
1316
internalBootstrapBrowserTest(() => doTests);
@@ -311,4 +314,96 @@ void doTests() {
311314
expect(completer.future, completion(isTrue));
312315
});
313316
});
317+
318+
group('on iOS', () {
319+
late FullPageDimensionsProvider provider;
320+
final fakes = <(JSObject, String)>[];
321+
322+
final JSObject visualViewport = globalContext.getProperty<JSObject>('visualViewport'.toJS);
323+
final JSObject documentElement = globalContext
324+
.getProperty<JSObject>('document'.toJS)
325+
.getProperty<JSObject>('documentElement'.toJS);
326+
327+
// Shadows a read-only browser property with a fixed value until tearDown.
328+
void fake(JSObject target, String name, double value) {
329+
objectConstructor.defineProperty(
330+
target,
331+
name,
332+
DomPropertyDataDescriptor(value: value, configurable: true),
333+
);
334+
fakes.add((target, name));
335+
}
336+
337+
// An iPhone document in portrait with the browser toolbar expanded. Each
338+
// test picks the visual viewport size.
339+
void fakeIPhone({double viewportWidth = 430, required double viewportHeight}) {
340+
fake(documentElement, 'clientWidth', 430);
341+
fake(documentElement, 'clientHeight', 775);
342+
fake(visualViewport, 'width', viewportWidth);
343+
fake(visualViewport, 'height', viewportHeight);
344+
}
345+
346+
setUp(() {
347+
ui_web.browser.debugOperatingSystemOverride = ui_web.OperatingSystem.iOs;
348+
EngineFlutterDisplay.instance.debugOverrideDevicePixelRatio(2);
349+
provider = FullPageDimensionsProvider();
350+
});
351+
352+
tearDown(() {
353+
provider.close();
354+
for (final (JSObject target, String name) in fakes) {
355+
target.delete(name.toJS);
356+
}
357+
fakes.clear();
358+
ui_web.browser.debugOperatingSystemOverride = null;
359+
EngineFlutterDisplay.instance.debugOverrideDevicePixelRatio(null);
360+
});
361+
362+
test('grows the view when the browser toolbar collapses', () {
363+
// Collapsing the toolbar grows the visual viewport, while
364+
// documentElement.clientHeight keeps the toolbar-expanded height.
365+
fakeIPhone(viewportHeight: 815);
366+
367+
expect(provider.computePhysicalSize(), const ui.Size(430 * 2, 815 * 2));
368+
});
369+
370+
test('reports no keyboard inset when the browser toolbar collapses', () {
371+
fakeIPhone(viewportHeight: 815);
372+
final double physicalHeight = provider.computePhysicalSize().height;
373+
374+
final ViewPadding computed = provider.computeKeyboardInsets(physicalHeight, false);
375+
376+
expect(computed.bottom, 0);
377+
});
378+
379+
test('does not shrink the view for a shorter visual viewport', () {
380+
// The on-screen keyboard shrinks the visual viewport.
381+
fakeIPhone(viewportHeight: 426);
382+
383+
expect(provider.computePhysicalSize(), const ui.Size(430 * 2, 775 * 2));
384+
});
385+
386+
test('ignores sub-pixel visual viewport growth', () {
387+
fakeIPhone(viewportHeight: 775.4);
388+
389+
expect(provider.computePhysicalSize(), const ui.Size(430 * 2, 775 * 2));
390+
});
391+
392+
test('reports the keyboard inset while editing', () {
393+
fakeIPhone(viewportHeight: 426);
394+
final double physicalHeight = provider.computePhysicalSize().height;
395+
396+
final ViewPadding computed = provider.computeKeyboardInsets(physicalHeight, true);
397+
398+
expect(computed.bottom, (775 - 426) * 2);
399+
});
400+
401+
test('ignores a visual viewport left over from the other orientation', () {
402+
// Mid-rotation, the visual viewport can still report the previous
403+
// orientation's size.
404+
fakeIPhone(viewportWidth: 932, viewportHeight: 853);
405+
406+
expect(provider.computePhysicalSize(), const ui.Size(430 * 2, 775 * 2));
407+
});
408+
});
314409
}

0 commit comments

Comments
 (0)