Skip to content

Commit 4ed6b36

Browse files
committed
fix(platform): fix window width/height calculations
It was possible for the incorrect window width and height to be remember when the keyboard was showing. This makes sure to keep the most accurate dimensions whether the app started either landscape or portrait, if the keyboard was opened or closed, and if the app was rotated. Related #6228
1 parent c4cf9df commit 4ed6b36

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

src/platform/platform.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export class Platform {
4242
private _bbActions: BackButtonAction[] = [];
4343
private _registry: {[name: string]: PlatformConfig};
4444
private _default: string;
45+
private _pW = 0;
46+
private _pH = 0;
47+
private _lW = 0;
48+
private _lH = 0;
49+
private _isPortrait: boolean = null;
4550

4651
/** @private */
4752
zone: NgZone;
@@ -436,7 +441,8 @@ export class Platform {
436441
* which reduces the chance of multiple and expensive DOM reads.
437442
*/
438443
width(): number {
439-
return windowDimensions().width;
444+
this._calcDim();
445+
return this._isPortrait ? this._pW : this._lW;
440446
}
441447

442448
/**
@@ -445,14 +451,16 @@ export class Platform {
445451
* which reduces the chance of multiple and expensive DOM reads.
446452
*/
447453
height(): number {
448-
return windowDimensions().height;
454+
this._calcDim();
455+
return this._isPortrait ? this._pH : this._lH;
449456
}
450457

451458
/**
452459
* Returns `true` if the app is in portait mode.
453460
*/
454461
isPortrait(): boolean {
455-
return this.width() < this.height();
462+
this._calcDim();
463+
return this._isPortrait;
456464
}
457465

458466
/**
@@ -462,19 +470,49 @@ export class Platform {
462470
return !this.isPortrait();
463471
}
464472

473+
/**
474+
* @private
475+
*/
476+
_calcDim() {
477+
if (this._isPortrait === null) {
478+
const winDimensions = windowDimensions();
479+
const screenWidth = window.screen.width || winDimensions.width;
480+
const screenHeight = window.screen.height || winDimensions.height;
481+
482+
if (screenWidth < screenHeight) {
483+
this._isPortrait = true;
484+
if (this._pW < winDimensions.width) {
485+
this._pW = winDimensions.width;
486+
}
487+
if (this._pH < winDimensions.height) {
488+
this._pH = winDimensions.height;
489+
}
490+
491+
} else {
492+
this._isPortrait = false;
493+
if (this._lW < winDimensions.width) {
494+
this._lW = winDimensions.width;
495+
}
496+
if (this._lH < winDimensions.height) {
497+
this._lH = winDimensions.height;
498+
}
499+
}
500+
}
501+
}
502+
465503
/**
466504
* @private
467505
*/
468506
windowResize() {
469-
const self = this;
470-
clearTimeout(self._resizeTm);
507+
clearTimeout(this._resizeTm);
471508

472-
self._resizeTm = setTimeout(() => {
509+
this._resizeTm = setTimeout(() => {
473510
flushDimensionCache();
511+
this._isPortrait = null;
474512

475-
for (let i = 0; i < self._onResizes.length; i++) {
513+
for (let i = 0; i < this._onResizes.length; i++) {
476514
try {
477-
self._onResizes[i]();
515+
this._onResizes[i]();
478516
} catch (e) {
479517
console.error(e);
480518
}

0 commit comments

Comments
 (0)