Skip to content

Commit 15bffa2

Browse files
authored
Fix the inconsistency issue between iOS and Android regarding the window.orientation values, which may cause incorrect orientation to be returned (#16936)
* Fix the inconsistency issue between iOS and Android regarding the window.orientation values, which may cause incorrect orientation to be returned
1 parent 724f2f6 commit 15bffa2

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

pal/screen-adapter/web/screen-adapter.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { Size } from '../../../cocos/core/math';
3131
import { Orientation } from '../enum-type';
3232
import legacyCC from '../../../predefine';
3333
import { checkPalIntegrity, withImpl } from '../../integrity-check';
34+
import { OS } from '../../../pal/system-info/enum-type';
3435

3536
interface ICachedStyle {
3637
width: string;
@@ -397,10 +398,7 @@ class ScreenAdapter extends EventTarget {
397398
this.emit('orientation-change', orientation);
398399
};
399400

400-
const getOrientation = (rotateAngle: number | string): Orientation => {
401-
if (typeof rotateAngle === 'string') {
402-
rotateAngle = parseInt(rotateAngle, 10);
403-
}
401+
const getOrientation = (): Orientation => {
404402
let tmpOrientation = Orientation.PORTRAIT;
405403
switch (window.orientation) {
406404
case 0:
@@ -447,24 +445,31 @@ class ScreenAdapter extends EventTarget {
447445

448446
const mediaQueryPortrait = window.matchMedia('(orientation: portrait)');
449447
const mediaQueryLandscape = window.matchMedia('(orientation: landscape)');
448+
// eslint-disable-next-line no-restricted-globals
449+
const hasScreeOrientation = screen.orientation;
450450
handleOrientationChange = (): void => {
451451
let tmpOrientation: Orientation = this._orientationDevice;
452-
// eslint-disable-next-line no-restricted-globals
453-
if (!screen.orientation) {
454-
tmpOrientation = getOrientation(window.orientation);
455-
} else {
456-
// eslint-disable-next-line no-restricted-globals
457-
const orientationType = screen.orientation.type;
458-
if (mediaQueryPortrait.matches) {
452+
if (mediaQueryPortrait.matches) {
453+
tmpOrientation = Orientation.PORTRAIT;
454+
if (hasScreeOrientation) {
455+
// eslint-disable-next-line no-restricted-globals
456+
const orientationType = screen.orientation.type;
459457
if (orientationType === 'portrait-primary') {
460458
tmpOrientation = Orientation.PORTRAIT;
461459
} else {
462460
tmpOrientation = Orientation.PORTRAIT_UPSIDE_DOWN;
463461
}
464-
} else if (orientationType === 'landscape-primary') {
465-
tmpOrientation = Orientation.LANDSCAPE_LEFT;
466-
} else if (orientationType === 'landscape-secondary') {
467-
tmpOrientation = Orientation.LANDSCAPE_RIGHT;
462+
}
463+
} else if (mediaQueryLandscape.matches) {
464+
tmpOrientation = Orientation.LANDSCAPE;
465+
if (hasScreeOrientation) {
466+
// eslint-disable-next-line no-restricted-globals
467+
const orientationType = screen.orientation.type;
468+
if (orientationType === 'landscape-primary') {
469+
tmpOrientation = Orientation.LANDSCAPE_LEFT;
470+
} else {
471+
tmpOrientation = Orientation.LANDSCAPE_RIGHT;
472+
}
468473
}
469474
}
470475
notifyOrientationChange(tmpOrientation);
@@ -473,7 +478,7 @@ class ScreenAdapter extends EventTarget {
473478
mediaQueryLandscape.addEventListener('change', orientationChangeCallback);
474479
} else {
475480
handleOrientationChange = (): void => {
476-
const tmpOrientation = getOrientation(window.orientation);
481+
const tmpOrientation = getOrientation();
477482
notifyOrientationChange(tmpOrientation);
478483
};
479484
window.addEventListener('orientationchange', orientationChangeCallback);
@@ -513,9 +518,9 @@ class ScreenAdapter extends EventTarget {
513518
} else {
514519
const winWidth = window.innerWidth;
515520
let winHeight = window.innerHeight;
516-
//On certain devices, window.innerHeight may not account for the height of the virtual keyboard, so dynamic calculation is necessary.
521+
//On certain android devices, window.innerHeight may not account for the height of the virtual keyboard, so dynamic calculation is necessary.
517522
const inputHeight = document.body.scrollHeight - winHeight;
518-
if (winHeight < inputHeight) {
523+
if (systemInfo.os === OS.ANDROID && winHeight < inputHeight) {
519524
winHeight += inputHeight;
520525
}
521526
if (this.isFrameRotated) {

0 commit comments

Comments
 (0)