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
This commit is contained in:
Adam Bradley
2016-11-18 15:27:01 -06:00
parent c4cf9df387
commit 4ed6b3666a

View File

@ -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);
}