Skip to content

Commit 8840124

Browse files
jgw96adamdbradley
authored andcommitted
fix(platform): orientation detected correctly
1 parent 40b5ecb commit 8840124

File tree

2 files changed

+136
-7
lines changed

2 files changed

+136
-7
lines changed

src/platform/platform.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,30 +564,48 @@ export class Platform {
564564
// we're not forcing many layouts
565565
// if _isPortrait is null then that means
566566
// the dimensions needs to be looked up again
567-
if (this._isPortrait === null) {
567+
// this also has to cover an edge case that only
568+
// happens on iOS 10 (not other versions of iOS)
569+
// where window.innerWidth is always bigger than
570+
// window.innerHeight when it is first measured,
571+
// even when the device is in portrait but
572+
// the second time it is measured it is correct.
573+
// Hopefully this check will not be needed in the future
574+
if (this._isPortrait === null || this._isPortrait === false && this._win['innerWidth'] < this._win['innerHeight']) {
568575
var win = this._win;
569576

570577
// we're keeping track of portrait and landscape dimensions
571578
// separately because the virtual keyboard can really mess
572579
// up accurate values when the keyboard is up
573580
if (win.screen.width > 0 && win.screen.height > 0) {
574-
if (win.screen.width < win.screen.height) {
581+
if (win['innerWidth'] < win['innerHeight']) {
575582

576-
if (this._pW < win['innerWidth']) {
583+
// the device is in portrait
584+
if (this._pW <= win['innerWidth']) {
585+
console.debug('setting _isPortrait to true');
577586
this._isPortrait = true;
578587
this._pW = win['innerWidth'];
579588
}
580-
if (this._pH < win['innerHeight']) {
589+
if (this._pH <= win['innerHeight']) {
590+
console.debug('setting _isPortrait to true');
581591
this._isPortrait = true;
582592
this._pH = win['innerHeight'];
583593
}
584594

585595
} else {
586-
if (this._lW < win['innerWidth']) {
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+
}
601+
// the device is in landscape
602+
if (this._lW <= win['innerWidth']) {
603+
console.debug('setting _isPortrait to false');
587604
this._isPortrait = false;
588605
this._lW = win['innerWidth'];
589606
}
590-
if (this._lH < win['innerHeight']) {
607+
if (this._lH <= win['innerHeight']) {
608+
console.debug('setting _isPortrait to false');
591609
this._isPortrait = false;
592610
this._lH = win['innerHeight'];
593611
}
@@ -788,7 +806,9 @@ export class Platform {
788806
timerId = setTimeout(() => {
789807
// setting _isPortrait to null means the
790808
// dimensions will need to be looked up again
791-
this._isPortrait = null;
809+
if (this.hasFocusedTextInput() === false) {
810+
this._isPortrait = null;
811+
}
792812

793813
for (let i = 0; i < this._onResizes.length; i++) {
794814
try {

src/platform/test/platform.spec.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,115 @@ describe('Platform', () => {
6464

6565
});
6666

67+
describe('orientation', () => {
68+
it('Should return true if orientation is landscape', () => {
69+
expect(plt.isLandscape()).toEqual(true);
70+
});
71+
72+
it('Should return false if orientation is not landscape', () => {
73+
let portraitWindow: any = {
74+
innerWidth: 200,
75+
innerHeight: 300,
76+
screen: {
77+
width: 200,
78+
height: 300
79+
}
80+
};
81+
plt.setWindow(portraitWindow);
82+
83+
expect(plt.isLandscape()).toEqual(false);
84+
});
85+
86+
it('Should return true if orientation is landscape but window.screen shows portrait', () => {
87+
// Even though we do not use window.screen.height/width
88+
// anymore beyond checking if they are > 0
89+
// as that api is broken on iOS, we should still check
90+
// this edge case
91+
92+
let iOSLandscapeWindow: any = {
93+
innerWidth: 300,
94+
innerHeight: 200,
95+
screen: {
96+
width: 200,
97+
height: 300
98+
}
99+
};
100+
plt.setWindow(iOSLandscapeWindow);
101+
102+
expect(plt.isLandscape()).toEqual(true);
103+
});
104+
105+
it('Should return false if orientation is not portrait', () => {
106+
expect(plt.isPortrait()).toEqual(false);
107+
});
108+
109+
it('Should return true if orientation is portrait', () => {
110+
let portraitWindow: any = {
111+
innerWidth: 200,
112+
innerHeight: 300,
113+
screen: {
114+
width: 200,
115+
height: 300
116+
}
117+
};
118+
plt.setWindow(portraitWindow);
119+
120+
expect(plt.isPortrait()).toEqual(true);
121+
});
122+
123+
it('Should return false when orientation is landscape and then true when changed to portrait', () => {
124+
125+
// start in landscape
126+
expect(plt.isPortrait()).toEqual(false);
127+
128+
let portraitWindow: any = {
129+
innerWidth: 200,
130+
innerHeight: 300,
131+
screen: {
132+
width: 200,
133+
height: 300
134+
}
135+
};
136+
// change to portrait
137+
plt.setWindow(portraitWindow);
138+
139+
expect(plt.isPortrait()).toEqual(true);
140+
});
141+
142+
it('Should return true when orientation is landscape and then false when changed to portrait', () => {
143+
144+
// start in landscape
145+
expect(plt.isLandscape()).toEqual(true);
146+
147+
let portraitWindow: any = {
148+
innerWidth: 200,
149+
innerHeight: 300,
150+
screen: {
151+
width: 200,
152+
height: 300
153+
}
154+
};
155+
// change to portrait
156+
plt.setWindow(portraitWindow);
157+
158+
expect(plt.isLandscape()).toEqual(false);
159+
});
160+
161+
it('Should return a number that is equal to window.innerWidth', () => {
162+
let type = typeof plt.width();
163+
164+
expect(type).toEqual('number');
165+
expect(plt.width()).toEqual(window.innerWidth);
166+
});
167+
168+
it('Should return a number that is equal to window.innerHeight', () => {
169+
let type = typeof plt.height();
170+
171+
expect(type).toEqual('number');
172+
expect(plt.height()).toEqual(window.innerHeight);
173+
});
174+
});
175+
67176
it('should set core as the fallback', () => {
68177
plt.setDefault('core');
69178
plt.setQueryParams('');

0 commit comments

Comments
 (0)