Skip to content

Commit

Permalink
fix(platform): fix window width/height calculations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adamdbradley committed Nov 18, 2016
1 parent c4cf9df commit 4ed6b36
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
Expand Down

0 comments on commit 4ed6b36

Please sign in to comment.