Skip to content

Commit 74de015

Browse files
committed
fix(keyboard): android scroll stuck
When a state change happens, ensure the keyboard is hidden. When a keyboard is hidden, ensure all pending timers our cleared. When resetting scrollView, ensure it’s only doing it when it has to. For testing: #1670 #2192
1 parent b873190 commit 74de015

File tree

4 files changed

+319
-135
lines changed

4 files changed

+319
-135
lines changed

Diff for: js/angular/service/viewService.js

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ function($rootScope, $state, $location, $document, $animate, $ionicPlatform, $io
3030
$ionicViewService.disableRegisterByTagName('ion-side-menus');
3131
}
3232

33+
// always reset the keyboard state when change stage
34+
$rootScope.$on('$stateChangeStart', function(){
35+
ionic.keyboard.hide();
36+
});
37+
3338
$rootScope.$on('viewState.changeHistory', function(e, data) {
3439
if(!data) return;
3540

Diff for: js/utils/keyboard.js

+41-25
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var keyboardIsOpen;
6666
var keyboardActiveElement;
6767
var keyboardFocusOutTimer;
6868
var keyboardFocusInTimer;
69+
var keyboardPollHeightTimer;
6970
var keyboardLastShow = 0;
7071

7172
var KEYBOARD_OPEN_CSS = 'keyboard-open';
@@ -75,6 +76,41 @@ ionic.keyboard = {
7576
isOpen: false,
7677
height: null,
7778
landscape: false,
79+
80+
hide: function() {
81+
clearTimeout(keyboardFocusInTimer);
82+
clearTimeout(keyboardFocusOutTimer);
83+
clearTimeout(keyboardPollHeightTimer);
84+
85+
console.log('keyboardHide');
86+
ionic.keyboard.isOpen = false;
87+
88+
ionic.trigger('resetScrollView', {
89+
target: keyboardActiveElement
90+
}, true);
91+
92+
ionic.requestAnimationFrame(function(){
93+
document.body.classList.remove(KEYBOARD_OPEN_CSS);
94+
});
95+
96+
// the keyboard is gone now, remove the touchmove that disables native scroll
97+
if (window.navigator.msPointerEnabled) {
98+
document.removeEventListener("MSPointerMove", keyboardPreventDefault);
99+
} else {
100+
document.removeEventListener('touchmove', keyboardPreventDefault);
101+
}
102+
document.removeEventListener('keydown', keyboardOnKeyDown);
103+
104+
if( keyboardHasPlugin() ) {
105+
cordova.plugins.Keyboard.close();
106+
}
107+
},
108+
109+
show: function() {
110+
if( keyboardHasPlugin() ) {
111+
cordova.plugins.Keyboard.show();
112+
}
113+
}
78114
};
79115

80116
function keyboardInit() {
@@ -126,22 +162,23 @@ function keyboardSetShow(e) {
126162

127163
keyboardFocusInTimer = setTimeout(function(){
128164
if ( keyboardLastShow + 350 > Date.now() ) return;
165+
console.log('keyboardSetShow');
129166
keyboardLastShow = Date.now();
130167
var keyboardHeight;
131168
var elementBounds = keyboardActiveElement.getBoundingClientRect();
132169
var count = 0;
133170

134-
var pollKeyboardHeight = setInterval(function(){
171+
keyboardPollHeightTimer = setInterval(function(){
135172

136173
keyboardHeight = keyboardGetHeight();
137174
if (count > 10){
138-
clearInterval(pollKeyboardHeight);
175+
clearInterval(keyboardPollHeightTimer);
139176
//waited long enough, just guess
140177
keyboardHeight = 275;
141178
}
142179
if (keyboardHeight){
180+
clearInterval(keyboardPollHeightTimer);
143181
keyboardShow(e.target, elementBounds.top, elementBounds.bottom, keyboardViewportHeight, keyboardHeight);
144-
clearInterval(pollKeyboardHeight);
145182
}
146183
count++;
147184

@@ -192,28 +229,7 @@ function keyboardShow(element, elementTop, elementBottom, viewportHeight, keyboa
192229
function keyboardFocusOut(e) {
193230
clearTimeout(keyboardFocusOutTimer);
194231

195-
keyboardFocusOutTimer = setTimeout(keyboardHide, 350);
196-
}
197-
198-
function keyboardHide() {
199-
console.log('keyboardHide');
200-
ionic.keyboard.isOpen = false;
201-
202-
ionic.trigger('resetScrollView', {
203-
target: keyboardActiveElement
204-
}, true);
205-
206-
ionic.requestAnimationFrame(function(){
207-
document.body.classList.remove(KEYBOARD_OPEN_CSS);
208-
});
209-
210-
// the keyboard is gone now, remove the touchmove that disables native scroll
211-
if (window.navigator.msPointerEnabled) {
212-
document.removeEventListener("MSPointerMove", keyboardPreventDefault);
213-
} else {
214-
document.removeEventListener('touchmove', keyboardPreventDefault);
215-
}
216-
document.removeEventListener('keydown', keyboardOnKeyDown);
232+
keyboardFocusOutTimer = setTimeout(ionic.keyboard.hide, 350);
217233
}
218234

219235
function keyboardUpdateViewportHeight() {

Diff for: js/views/scrollView.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,13 @@ ionic.views.Scroll = ionic.views.View.inherit({
687687

688688
self.resetScrollView = function(e) {
689689
//return scrollview to original height once keyboard has hidden
690-
self.isScrolledIntoView = false;
691-
container.style.height = "";
692-
container.style.overflow = "";
693-
self.resize();
694-
ionic.scroll.isScrolling = false;
690+
if(self.isScrolledIntoView) {
691+
self.isScrolledIntoView = false;
692+
container.style.height = "";
693+
container.style.overflow = "";
694+
self.resize();
695+
ionic.scroll.isScrolling = false;
696+
}
695697
};
696698

697699
//Broadcasted when keyboard is shown on some platforms.

0 commit comments

Comments
 (0)