move visible row and cursor pixel position

calculation to the window model
This commit is contained in:
Fabian Jakobs 2011-07-27 18:12:43 +02:00
commit 1018f51b5e
5 changed files with 55 additions and 115 deletions

View file

@ -125,6 +125,8 @@ var Editor = function(windowView, buffer) {
this.setSession = function(session) { this.setSession = function(session) {
if (this.session == session) if (this.session == session)
return; return;
this.windowModel.setBuffer(session);
if (this.session) { if (this.session) {
var oldSession = this.session; var oldSession = this.session;
@ -937,11 +939,11 @@ var Editor = function(windowView, buffer) {
this.getFirstVisibleRow = function() { this.getFirstVisibleRow = function() {
return this.renderer.getFirstVisibleRow(); return this.windowModel.getFirstVisibleRow();
}; };
this.getLastVisibleRow = function() { this.getLastVisibleRow = function() {
return this.renderer.getLastVisibleRow(); return this.windowModel.getLastVisibleRow();
}; };
this.isRowVisible = function(row) { this.isRowVisible = function(row) {

View file

@ -51,7 +51,7 @@ var Window = exports.Window = function(theme) {
this.theme = null; this.theme = null;
this.setTheme(theme); this.setTheme(theme);
this._buffer = null; this.buffer = null;
this.layerConfig = { this.layerConfig = {
width : 1, width : 1,
@ -91,12 +91,52 @@ var Window = exports.Window = function(theme) {
oop.implement(this, EventEmitter); oop.implement(this, EventEmitter);
// VIEWPORT COMPUTATIONS
this.getFirstVisibleRow = function() {
return this.layerConfig.firstRow;
};
this.getLastVisibleRow = function() {
return this.layerConfig.lastRow;
};
this.getFirstFullyVisibleRow = function() {
return this.layerConfig.firstRow + (this.layerConfig.offset === 0 ? 0 : 1);
};
this.getLastFullyVisibleRow = function() {
var flint = Math.floor((this.layerConfig.height + this.layerConfig.offset) / this.characterSize.height);
return this.layerConfig.firstRow - 1 + flint;
};
this.getCursorPixelPosition = function(onScreen) {
if (!this.buffer) {
return {
left: 0,
top: 0
}
}
var position = this.buffer.selection.getCursor();
var pos = this.buffer.documentToScreenPosition(position);
var cursorLeft = Math.round(
this.padding
+ pos.column * this.characterSize.width
);
var cursorTop = (pos.row - (onScreen ? this.layerConfig.firstRowScreen : 0))
* this.characterSize.height;
return {
left : cursorLeft,
top : cursorTop
};
};
this.setBuffer = function(buffer) { this.setBuffer = function(buffer) {
if (this._buffer === buffer) if (this.buffer === buffer)
return; return;
var oldValue = this._buffer; var oldValue = this.buffer;
this._buffer = buffer; this.buffer = buffer;
this._emit("changeBuffer", {oldValue: oldValue, value: buffer}); this._emit("changeBuffer", {oldValue: oldValue, value: buffer});
}; };

View file

@ -89,33 +89,8 @@ var Cursor = function(model, parentEl) {
}, 1000); }, 1000);
}; };
this.getPixelPosition = function(onScreen) {
if (!this.config || !this.session) {
return {
left : 0,
top : 0
};
}
var position = this.session.selection.getCursor();
var pos = this.session.documentToScreenPosition(position);
var cursorLeft = Math.round(
this.model.padding
+ pos.column * this.config.characterWidth
);
var cursorTop = (pos.row - (onScreen ? this.config.firstRowScreen : 0))
* this.config.lineHeight;
return {
left : cursorLeft,
top : cursorTop
};
};
this.update = function(config) { this.update = function(config) {
this.config = config; this.pixelPos = this.model.getCursorPixelPosition(true);
this.pixelPos = this.getPixelPosition(true);
this.cursor.style.left = this.pixelPos.left + "px"; this.cursor.style.left = this.pixelPos.left + "px";
this.cursor.style.top = this.pixelPos.top + "px"; this.cursor.style.top = this.pixelPos.top + "px";

View file

@ -89,8 +89,6 @@ var WindowView = function(windowModel, container) {
windowModel.setComputedCharacterSize(measureText.getCharacterSize()); windowModel.setComputedCharacterSize(measureText.getCharacterSize());
this.$cursorLayer = new CursorLayer(windowModel, this.content); this.$cursorLayer = new CursorLayer(windowModel, this.content);
this.$cursorPadding = 8;
this.$horizScroll = true; this.$horizScroll = true;
this.scrollBar = new ScrollBar(container); this.scrollBar = new ScrollBar(container);
@ -278,7 +276,7 @@ var WindowView = function(windowModel, container) {
if (useragent.isIE) if (useragent.isIE)
return; return;
var pos = this.$cursorLayer.getPixelPosition(); var pos = this.model.getCursorPixelPosition();
if (!pos) if (!pos)
return; return;
@ -289,23 +287,6 @@ var WindowView = function(windowModel, container) {
textarea.style.top = (bounds.top + pos.top - this.scrollTop + offset) + "px"; textarea.style.top = (bounds.top + pos.top - this.scrollTop + offset) + "px";
}; };
this.getFirstVisibleRow = function() {
return this.model.layerConfig.firstRow;
};
this.getFirstFullyVisibleRow = function() {
return this.model.layerConfig.firstRow + (this.model.layerConfig.offset === 0 ? 0 : 1);
};
this.getLastFullyVisibleRow = function() {
var flint = Math.floor((this.model.layerConfig.height + this.model.layerConfig.offset) / this.model.layerConfig.lineHeight);
return this.model.layerConfig.firstRow - 1 + flint;
};
this.getLastVisibleRow = function() {
return this.model.layerConfig.lastRow;
};
this.updatePadding = function() { this.updatePadding = function() {
this.$textLayer.element.style.padding = "0 " + this.model.padding + "px"; this.$textLayer.element.style.padding = "0 " + this.model.padding + "px";
this.$loop.schedule(this.CHANGE_FULL); this.$loop.schedule(this.CHANGE_FULL);
@ -565,7 +546,7 @@ var WindowView = function(windowModel, container) {
if (this.model.size.scrollerHeight === 0) if (this.model.size.scrollerHeight === 0)
return; return;
var pos = this.$cursorLayer.getPixelPosition(); var pos = this.model.getCursorPixelPosition();
var left = pos.left + this.model.padding; var left = pos.left + this.model.padding;
var top = pos.top; var top = pos.top;
@ -688,7 +669,7 @@ var WindowView = function(windowModel, container) {
this.$composition.innerHTML = " "; this.$composition.innerHTML = " ";
var pos = this.$cursorLayer.getPixelPosition(); var pos = this.model.getCursorPixelPosition();
var style = this.$composition.style; var style = this.$composition.style;
style.top = pos.top + "px"; style.top = pos.top + "px";
style.left = (pos.left + this.model.padding) + "px"; style.left = (pos.left + this.model.padding) + "px";

View file

@ -42,28 +42,10 @@ var Window = require("ace/model/window").Window;
exports.MockRenderer = exports.WindowViewMock = WindowViewMock = function(visibleRowCount) { exports.MockRenderer = exports.WindowViewMock = WindowViewMock = function(visibleRowCount) {
this.container = document.createElement("div"); this.container = document.createElement("div");
this.visibleRowCount = visibleRowCount || 20;
this.model = null;
this.layerConfig = {
firstVisibleRow : 0,
lastVisibleRow : this.visibleRowCount
};
this.isWindowViewMock = true; this.isWindowViewMock = true;
this.model = new Window(); this.model = new Window();
}; };
WindowViewMock.prototype.getFirstVisibleRow = function() {
return this.layerConfig.firstVisibleRow;
};
WindowViewMock.prototype.getLastVisibleRow = function() {
return this.layerConfig.lastVisibleRow;
};
WindowViewMock.prototype.getContainerElement = function() { WindowViewMock.prototype.getContainerElement = function() {
return this.container; return this.container;
}; };
@ -76,9 +58,6 @@ WindowViewMock.prototype.getTextAreaContainer = function() {
return this.container; return this.container;
}; };
WindowViewMock.prototype.moveTextAreaToCursor = function() {
};
WindowViewMock.prototype.setModel = function(model) { WindowViewMock.prototype.setModel = function(model) {
this.model = model; this.model = model;
}; };
@ -91,51 +70,14 @@ WindowViewMock.prototype.getSession = function(session) {
return this.session; return this.session;
}; };
WindowViewMock.prototype.setTokenizer = function() {
};
WindowViewMock.prototype.updateCursor = function() {
};
WindowViewMock.prototype.scrollToLine = function(line, center) {
var lineHeight = { lineHeight: 16 };
var row = 0;
for (var l = 1; l < line; l++) {
row += this.session.getRowHeight(lineHeight, l-1) / lineHeight.lineHeight;
}
if (center) {
row -= this.visibleRowCount / 2;
}
this.scrollToRow(row);
};
WindowViewMock.prototype.scrollCursorIntoView = function() {
var cursor = this.session.getSelection().getCursor();
if (cursor.row < this.layerConfig.firstVisibleRow) {
this.scrollToRow(cursor.row);
}
else if (cursor.row > this.layerConfig.lastVisibleRow) {
this.scrollToRow(cursor.row);
}
};
WindowViewMock.prototype.scrollToRow = function(row) {
var row = Math.min(this.session.getLength() - this.visibleRowCount, Math.max(0,
row));
this.layerConfig.firstVisibleRow = row;
this.layerConfig.lastVisibleRow = row + this.visibleRowCount;
};
WindowViewMock.prototype.getScrollTopRow = function() {
return this.layerConfig.firstVisibleRow;
};
[ [
"moveTextAreaToCursor",
"setTokenizer",
"draw", "draw",
"updateLines", "updateLines",
"updateBackMarkers", "updateBackMarkers",
"updateFrontMarkers", "updateFrontMarkers",
"updateCursor",
"updateShowInvisibles", "updateShowInvisibles",
"updatePrintMargin", "updatePrintMargin",
"updateShowGutter", "updateShowGutter",