Skip to content

Commit

Permalink
fix(keyboard): android scroll stuck
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adamdbradley committed Sep 15, 2014
1 parent b873190 commit 74de015
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 135 deletions.
5 changes: 5 additions & 0 deletions js/angular/service/viewService.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ function($rootScope, $state, $location, $document, $animate, $ionicPlatform, $io
$ionicViewService.disableRegisterByTagName('ion-side-menus');
}

// always reset the keyboard state when change stage
$rootScope.$on('$stateChangeStart', function(){
ionic.keyboard.hide();
});

$rootScope.$on('viewState.changeHistory', function(e, data) {
if(!data) return;

Expand Down
66 changes: 41 additions & 25 deletions js/utils/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var keyboardIsOpen;
var keyboardActiveElement;
var keyboardFocusOutTimer;
var keyboardFocusInTimer;
var keyboardPollHeightTimer;
var keyboardLastShow = 0;

var KEYBOARD_OPEN_CSS = 'keyboard-open';
Expand All @@ -75,6 +76,41 @@ ionic.keyboard = {
isOpen: false,
height: null,
landscape: false,

hide: function() {
clearTimeout(keyboardFocusInTimer);
clearTimeout(keyboardFocusOutTimer);
clearTimeout(keyboardPollHeightTimer);

console.log('keyboardHide');
ionic.keyboard.isOpen = false;

ionic.trigger('resetScrollView', {
target: keyboardActiveElement
}, true);

ionic.requestAnimationFrame(function(){
document.body.classList.remove(KEYBOARD_OPEN_CSS);
});

// the keyboard is gone now, remove the touchmove that disables native scroll
if (window.navigator.msPointerEnabled) {
document.removeEventListener("MSPointerMove", keyboardPreventDefault);
} else {
document.removeEventListener('touchmove', keyboardPreventDefault);
}
document.removeEventListener('keydown', keyboardOnKeyDown);

if( keyboardHasPlugin() ) {
cordova.plugins.Keyboard.close();
}
},

show: function() {
if( keyboardHasPlugin() ) {
cordova.plugins.Keyboard.show();
}
}
};

function keyboardInit() {
Expand Down Expand Up @@ -126,22 +162,23 @@ function keyboardSetShow(e) {

keyboardFocusInTimer = setTimeout(function(){
if ( keyboardLastShow + 350 > Date.now() ) return;
console.log('keyboardSetShow');
keyboardLastShow = Date.now();
var keyboardHeight;
var elementBounds = keyboardActiveElement.getBoundingClientRect();
var count = 0;

var pollKeyboardHeight = setInterval(function(){
keyboardPollHeightTimer = setInterval(function(){

keyboardHeight = keyboardGetHeight();
if (count > 10){
clearInterval(pollKeyboardHeight);
clearInterval(keyboardPollHeightTimer);
//waited long enough, just guess
keyboardHeight = 275;
}
if (keyboardHeight){
clearInterval(keyboardPollHeightTimer);
keyboardShow(e.target, elementBounds.top, elementBounds.bottom, keyboardViewportHeight, keyboardHeight);
clearInterval(pollKeyboardHeight);
}
count++;

Expand Down Expand Up @@ -192,28 +229,7 @@ function keyboardShow(element, elementTop, elementBottom, viewportHeight, keyboa
function keyboardFocusOut(e) {
clearTimeout(keyboardFocusOutTimer);

keyboardFocusOutTimer = setTimeout(keyboardHide, 350);
}

function keyboardHide() {
console.log('keyboardHide');
ionic.keyboard.isOpen = false;

ionic.trigger('resetScrollView', {
target: keyboardActiveElement
}, true);

ionic.requestAnimationFrame(function(){
document.body.classList.remove(KEYBOARD_OPEN_CSS);
});

// the keyboard is gone now, remove the touchmove that disables native scroll
if (window.navigator.msPointerEnabled) {
document.removeEventListener("MSPointerMove", keyboardPreventDefault);
} else {
document.removeEventListener('touchmove', keyboardPreventDefault);
}
document.removeEventListener('keydown', keyboardOnKeyDown);
keyboardFocusOutTimer = setTimeout(ionic.keyboard.hide, 350);
}

function keyboardUpdateViewportHeight() {
Expand Down
12 changes: 7 additions & 5 deletions js/views/scrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,13 @@ ionic.views.Scroll = ionic.views.View.inherit({

self.resetScrollView = function(e) {
//return scrollview to original height once keyboard has hidden
self.isScrolledIntoView = false;
container.style.height = "";
container.style.overflow = "";
self.resize();
ionic.scroll.isScrolling = false;
if(self.isScrolledIntoView) {
self.isScrolledIntoView = false;
container.style.height = "";
container.style.overflow = "";
self.resize();
ionic.scroll.isScrolling = false;
}
};

//Broadcasted when keyboard is shown on some platforms.
Expand Down
Loading

0 comments on commit 74de015

Please sign in to comment.