fix orientation-change can not work normally (#16774)

* fix orientation-change can not work normally
This commit is contained in:
bofeng-song
2024-03-22 09:27:54 +08:00
committed by GitHub
parent 559bfc9467
commit 5da2f9b63a
9 changed files with 167 additions and 30 deletions
+1 -1
View File
@@ -176,7 +176,7 @@ class ScreenAdapter extends EventTarget {
this.emit('window-resize', event.width, event.height, event.windowId);
};
jsb.onOrientationChanged = (event): void => {
this.emit('orientation-change');
this.emit('orientation-change', this.orientation);
};
}
}
+70 -19
View File
@@ -377,11 +377,21 @@ class ScreenAdapter extends EventTarget {
});
window.addEventListener('resize', (): void => {
if (!this.handleResizeEvent) {
return;
}
// if (!this.handleResizeEvent) {
// return;
// }
this._resizeFrame();
});
const adapter = this;
const notifyOrientationChange = (orientation): void => {
if (orientation === adapter._orientation) {
return;
}
adapter._orientation = orientation;
adapter.emit('orientation-change', orientation);
};
if (typeof window.matchMedia === 'function') {
const updateDPRChangeListener = (): void => {
const dpr = window.devicePixelRatio;
@@ -392,21 +402,53 @@ class ScreenAdapter extends EventTarget {
}, { once: true });
};
updateDPRChangeListener();
}
window.addEventListener('orientationchange', (): void => {
if (this._orientationChangeTimeoutId !== -1) {
clearTimeout(this._orientationChangeTimeoutId);
}
this._orientationChangeTimeoutId = setTimeout((): void => {
if (!this.handleResizeEvent) {
return;
const mediaQueryPortrait = window.matchMedia('(orientation: portrait)');
const mediaQueryLandscape = window.matchMedia('(orientation: landscape)');
const handleOrientationChange = (): void => {
let tmpOrientation = adapter._orientation;
const orientationType = screen.orientation.type;
if (mediaQueryPortrait.matches) {
if (orientationType === 'portrait-primary') {
tmpOrientation = Orientation.PORTRAIT;
} else {
tmpOrientation = Orientation.PORTRAIT_UPSIDE_DOWN;
}
} else {
if (orientationType === 'landscape-primary') {
tmpOrientation = Orientation.LANDSCAPE_LEFT;
} else if (orientationType === 'landscape-secondary') {
tmpOrientation = Orientation.LANDSCAPE_RIGHT;
}
}
this._updateFrameState();
this._resizeFrame();
this.emit('orientation-change', this.windowSize.width, this.windowSize.height);
this._orientationChangeTimeoutId = -1;
}, EVENT_TIMEOUT);
});
notifyOrientationChange(tmpOrientation);
};
mediaQueryPortrait.addEventListener('change', handleOrientationChange);
mediaQueryLandscape.addEventListener('change', handleOrientationChange);
} else {
const adapter = this;
const handleOrientationChange = (): void => {
let tmpOrientation = adapter._orientation;
switch (window.orientation) {
case 0:
tmpOrientation = Orientation.PORTRAIT;
break;
case 90:
// Handle landscape orientation, top side facing to the right
tmpOrientation = Orientation.LANDSCAPE_RIGHT;
break;
case -90:
// Handle landscape orientation, top side facing to the left
tmpOrientation = Orientation.LANDSCAPE_LEFT;
break;
case 180:
tmpOrientation = Orientation.PORTRAIT_UPSIDE_DOWN;
break;
}
notifyOrientationChange(tmpOrientation);
};
window.addEventListener('orientationchange', handleOrientationChange);
}
document.addEventListener(this._fn.fullscreenchange, () => {
this._onFullscreenChange?.();
this.emit('fullscreen-change', this.windowSize.width, this.windowSize.height);
@@ -440,7 +482,13 @@ class ScreenAdapter extends EventTarget {
this._gameFrame.style.width = `${sizeInCssPixels.width}px`;
this._gameFrame.style.height = `${sizeInCssPixels.height}px`;
} else {
const winWidth = window.innerWidth; const winHeight = window.innerHeight;
const winWidth = window.innerWidth;
let winHeight = window.innerHeight;
//On certain devices, window.innerHeight may not account for the height of the virtual keyboard, so dynamic calculation is necessary.
const inputHeight = document.body.scrollHeight - winHeight;
if (winHeight < inputHeight) {
winHeight += inputHeight;
}
if (this.isFrameRotated) {
this._gameFrame.style['-webkit-transform'] = 'rotate(90deg)';
this._gameFrame.style.transform = 'rotate(90deg)';
@@ -504,7 +552,10 @@ class ScreenAdapter extends EventTarget {
const height = window.innerHeight;
const isBrowserLandscape = width > height;
this.isFrameRotated = systemInfo.isMobile
&& ((isBrowserLandscape && orientation === Orientation.PORTRAIT) || (!isBrowserLandscape && orientation === Orientation.LANDSCAPE));
&& ((isBrowserLandscape && orientation === Orientation.PORTRAIT) ||
(!isBrowserLandscape && (orientation === Orientation.LANDSCAPE ||
orientation === Orientation.LANDSCAPE_LEFT ||
orientation === Orientation.LANDSCAPE_RIGHT)));
}
private _updateContainer (): void {
if (!this._gameContainer) {