From 4ed6b3666a1cc5ef1b18bae32b7c9a74af68438a Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Fri, 18 Nov 2016 15:27:01 -0600 Subject: [PATCH] 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 --- src/platform/platform.ts | 54 ++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/src/platform/platform.ts b/src/platform/platform.ts index 417311ea77..dac7d1aae3 100644 --- a/src/platform/platform.ts +++ b/src/platform/platform.ts @@ -42,6 +42,11 @@ export class Platform { private _bbActions: BackButtonAction[] = []; private _registry: {[name: string]: PlatformConfig}; private _default: string; + private _pW = 0; + private _pH = 0; + private _lW = 0; + private _lH = 0; + private _isPortrait: boolean = null; /** @private */ zone: NgZone; @@ -436,7 +441,8 @@ export class Platform { * which reduces the chance of multiple and expensive DOM reads. */ width(): number { - return windowDimensions().width; + this._calcDim(); + return this._isPortrait ? this._pW : this._lW; } /** @@ -445,14 +451,16 @@ export class Platform { * which reduces the chance of multiple and expensive DOM reads. */ height(): number { - return windowDimensions().height; + this._calcDim(); + return this._isPortrait ? this._pH : this._lH; } /** * Returns `true` if the app is in portait mode. */ isPortrait(): boolean { - return this.width() < this.height(); + this._calcDim(); + return this._isPortrait; } /** @@ -462,19 +470,49 @@ export class Platform { return !this.isPortrait(); } + /** + * @private + */ + _calcDim() { + if (this._isPortrait === null) { + const winDimensions = windowDimensions(); + const screenWidth = window.screen.width || winDimensions.width; + const screenHeight = window.screen.height || winDimensions.height; + + if (screenWidth < screenHeight) { + this._isPortrait = true; + if (this._pW < winDimensions.width) { + this._pW = winDimensions.width; + } + if (this._pH < winDimensions.height) { + this._pH = winDimensions.height; + } + + } else { + this._isPortrait = false; + if (this._lW < winDimensions.width) { + this._lW = winDimensions.width; + } + if (this._lH < winDimensions.height) { + this._lH = winDimensions.height; + } + } + } + } + /** * @private */ windowResize() { - const self = this; - clearTimeout(self._resizeTm); + clearTimeout(this._resizeTm); - self._resizeTm = setTimeout(() => { + this._resizeTm = setTimeout(() => { flushDimensionCache(); + this._isPortrait = null; - for (let i = 0; i < self._onResizes.length; i++) { + for (let i = 0; i < this._onResizes.length; i++) { try { - self._onResizes[i](); + this._onResizes[i](); } catch (e) { console.error(e); }