Skip to content

Commit 54e50f4

Browse files
jgw96manucorporat
authored andcommitted
chore(platform): resize works correctly
* chore(platform): resize works correctly * chore(platform): height correct after resize * test(platform): add new dimensions unit tests * chore(platform): works with keyboard too * chore(platform): store values * chore(platform): change const to var
1 parent da14042 commit 54e50f4

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

src/platform/platform.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -574,40 +574,43 @@ export class Platform {
574574
if (this._isPortrait === null || this._isPortrait === false && this._win['innerWidth'] < this._win['innerHeight']) {
575575
var win = this._win;
576576

577+
var innerWidth = win['innerWidth'];
578+
var innerHeight = win['innerHeight'];
579+
577580
// we're keeping track of portrait and landscape dimensions
578581
// separately because the virtual keyboard can really mess
579582
// up accurate values when the keyboard is up
580583
if (win.screen.width > 0 && win.screen.height > 0) {
581-
if (win['innerWidth'] < win['innerHeight']) {
584+
if (innerWidth < innerHeight) {
582585

583586
// the device is in portrait
584-
if (this._pW <= win['innerWidth']) {
587+
// we have to do fancier checking here
588+
// because of the virtual keyboard resizing
589+
// the window
590+
if (this._pW <= innerWidth) {
585591
console.debug('setting _isPortrait to true');
586592
this._isPortrait = true;
587-
this._pW = win['innerWidth'];
593+
this._pW = innerWidth;
588594
}
589-
if (this._pH <= win['innerHeight']) {
595+
596+
if (this._pH <= innerHeight) {
590597
console.debug('setting _isPortrait to true');
591598
this._isPortrait = true;
592-
this._pH = win['innerHeight'];
599+
this._pH = innerHeight;
593600
}
594601

595602
} else {
596-
if (this._lW > win['innerWidth']) {
597-
// Special case: keyboard is open and device is in portrait
598-
console.debug('setting _isPortrait to true while keyboard is open and device is portrait');
599-
this._isPortrait = true;
600-
}
601603
// the device is in landscape
602-
if (this._lW <= win['innerWidth']) {
604+
if (this._lW !== innerWidth) {
603605
console.debug('setting _isPortrait to false');
604606
this._isPortrait = false;
605-
this._lW = win['innerWidth'];
607+
this._lW = innerWidth;
606608
}
607-
if (this._lH <= win['innerHeight']) {
609+
610+
if (this._lH !== innerHeight) {
608611
console.debug('setting _isPortrait to false');
609612
this._isPortrait = false;
610-
this._lH = win['innerHeight'];
613+
this._lH = innerHeight;
611614
}
612615
}
613616

src/platform/test/platform.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,56 @@ describe('Platform', () => {
173173
});
174174
});
175175

176+
describe('dimensions', () => {
177+
it('should return the correct width of the window', () => {
178+
expect(plt.width()).toEqual(window.innerWidth);
179+
});
180+
181+
it('should return the correct height of the window', () => {
182+
expect(plt.height()).toEqual(window.innerHeight);
183+
});
184+
185+
it('should return the correct width of the window after resize', () => {
186+
187+
// start with default window
188+
expect(plt.width()).toEqual(window.innerWidth);
189+
190+
let resizedWindow: any = {
191+
innerWidth: 200,
192+
innerHeight: 300,
193+
screen: {
194+
width: 200,
195+
height: 300
196+
}
197+
};
198+
199+
// resize to smaller window
200+
plt.setWindow(resizedWindow);
201+
202+
expect(plt.width()).toEqual(resizedWindow.innerWidth);
203+
});
204+
205+
it('should return the correct height of the window after resize', () => {
206+
207+
// start with default window
208+
expect(plt.height()).toEqual(window.innerHeight);
209+
210+
let resizedWindow: any = {
211+
innerWidth: 200,
212+
innerHeight: 300,
213+
screen: {
214+
width: 200,
215+
height: 300
216+
}
217+
};
218+
219+
// resize to smaller window
220+
plt.setWindow(resizedWindow);
221+
222+
expect(plt.height()).toEqual(resizedWindow.innerHeight);
223+
});
224+
});
225+
176226
it('should set core as the fallback', () => {
177227
plt.setDefault('core');
178228
plt.setQueryParams('');

0 commit comments

Comments
 (0)