simplify scroll animations

This commit is contained in:
nightwing 2012-04-24 22:10:50 +04:00
commit c72357c201
2 changed files with 54 additions and 62 deletions

View file

@ -964,22 +964,20 @@ var Editor = function(renderer, session) {
var config = this.renderer.layerConfig; var config = this.renderer.layerConfig;
var rows = dir * Math.floor(config.height / config.lineHeight); var rows = dir * Math.floor(config.height / config.lineHeight);
renderer.scrollBy(0, rows * config.lineHeight);
if (select == null)
return;
this.$blockScrolling++; this.$blockScrolling++;
if (select) { if (select == true) {
this.selection.$moveSelection(function(){ this.selection.$moveSelection(function(){
this.moveCursorBy(rows, 0); this.moveCursorBy(rows, 0);
}); });
} else { } else if (select == false) {
this.selection.moveCursorBy(rows, 0); this.selection.moveCursorBy(rows, 0);
this.selection.clearSelection(); this.selection.clearSelection();
} }
this.$blockScrolling--; this.$blockScrolling--;
this.renderer.animateScrolling(function() {
renderer.scrollBy(0, rows * config.lineHeight);
}, this);
}; };
this.selectPageDown = function() { this.selectPageDown = function() {
@ -1081,18 +1079,6 @@ var Editor = function(renderer, session) {
this.scrollToLine(lineNumber - 1, true, animate); this.scrollToLine(lineNumber - 1, true, animate);
}; };
this.$gotoLine = function(row, column, animate, center) {
var _self = this;
//@todo hide cursor
this.scrollToLine(row, center, animate, function(){
_self.$blockScrolling += 1;
_self.moveCursorTo(row, column || 0);
_self.$blockScrolling -= 1;
});
}
this.navigateTo = function(row, column) { this.navigateTo = function(row, column) {
this.clearSelection(); this.clearSelection();
this.moveCursorTo(row, column); this.moveCursorTo(row, column);
@ -1149,18 +1135,17 @@ var Editor = function(renderer, session) {
}; };
this.navigateFileEnd = function() { this.navigateFileEnd = function() {
//this.selection.moveCursorFileEnd(); this.renderer.animateScrolling(function() {
var doc = this.session.getDocument(); this.selection.moveCursorFileEnd();
var row = doc.getLength() - 1; this.clearSelection();
}, this);
this.$gotoLine(row, doc.getLine(row).length);
this.clearSelection();
}; };
this.navigateFileStart = function() { this.navigateFileStart = function() {
//this.selection.moveCursorFileStart(); this.renderer.animateScrolling(function() {
this.$gotoLine(0, 0); this.selection.moveCursorFileStart();
this.clearSelection(); this.clearSelection();
}, this);
}; };
this.navigateWordRight = function() { this.navigateWordRight = function() {
@ -1265,24 +1250,14 @@ var Editor = function(renderer, session) {
var range = this.$search.find(this.session); var range = this.$search.find(this.session);
if (range) { if (range) {
this.session.unfold(range);
this.$blockScrolling += 1; this.$blockScrolling += 1;
this.session.unfold(range);
this.selection.setSelectionRange(range); this.selection.setSelectionRange(range);
this.$blockScrolling -= 1; this.$blockScrolling -= 1;
if (this.getAnimatedScroll()) { this.renderer.animateScrolling(function() {
var cursor = this.getCursorPosition();
if (!this.isRowFullyVisible(cursor.row))
this.scrollToLine(cursor.row, true, animate);
//@todo scroll X
//if (!this.isColumnFullyVisible(cursor.column))
//this.scrollToRow(cursor.column);
}
else {
this.renderer.scrollSelectionIntoView(range.start, range.end); this.renderer.scrollSelectionIntoView(range.start, range.end);
} }, this);
} }
}; };

View file

@ -108,7 +108,8 @@ var VirtualRenderer = function(container, theme) {
this.scrollBar = new ScrollBar(container); this.scrollBar = new ScrollBar(container);
this.scrollBar.addEventListener("scroll", function(e) { this.scrollBar.addEventListener("scroll", function(e) {
_self.session.setScrollTop(e.data); if (!_self.$inScrollAnimation)
_self.session.setScrollTop(e.data);
}); });
this.scrollTop = 0; this.scrollTop = 0;
@ -746,35 +747,51 @@ var VirtualRenderer = function(container, theme) {
for (i = 0; i < l; ++i) for (i = 0; i < l; ++i)
steps.push(func(i / this.STEPS, fromValue, toValue - fromValue)); steps.push(func(i / this.STEPS, fromValue, toValue - fromValue));
steps.push(toValue);
return steps; return steps;
}; };
this.scrollToLine = function(line, center, animate, callback) { this.scrollToLine = function(line, center, animate) {
var pos = this.$cursorLayer.getPixelPosition({row: line, column: 0}); var pos = this.$cursorLayer.getPixelPosition({row: line, column: 0});
var offset = pos.top; var offset = pos.top;
if (center) if (center)
offset -= this.$size.scrollerHeight / 2; offset -= this.$size.scrollerHeight / 2;
if (animate !== false if (animate) {
&& this.$animatedScroll && Math.abs(offset - this.scrollTop) < 100000) { this.animateScrolling(function() {
this.session.setScrollTop(offset);
}, this);
} else {
this.session.setScrollTop(offset);
}
};
this.animateScrolling = function(scrollFunc, self) {
var fromValue = this.scrollTop;
scrollFunc.call(self);
var toValue = this.scrollTop;
if (this.$animatedScroll && Math.abs(fromValue - toValue) < 100000) {
var _self = this; var _self = this;
var steps = _self.$calcSteps(this.scrollTop, offset); var steps = _self.$calcSteps(fromValue, toValue);
this.$inScrollAnimation = true;
clearInterval(this.$timer); clearInterval(this.$timer);
this.$timer = setInterval(function() {
_self.session.setScrollTop(steps.shift());
if (!steps.length) { _self.session.setScrollTop(steps.shift());
callback && callback(); this.$timer = setInterval(function() {
if (steps.length) {
_self.session.setScrollTop(steps.shift());
// trick session to think it's already scrolled to not loose toValue
_self.session.$scrollTop = toValue;
} else {
this.$inScrollAnimation = false;
clearInterval(_self.$timer); clearInterval(_self.$timer);
_self.session.$scrollTop = -1;
_self.session.setScrollTop(toValue);
} }
}, 10); }, 10);
} }
else {
this.session.setScrollTop(offset);
}
}; };
this.scrollToY = function(scrollTop) { this.scrollToY = function(scrollTop) {