From 86f3fc2a1cc354b48a1f211896c6674dabf3a1d3 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 28 Jul 2011 10:38:59 +0200 Subject: [PATCH] move more scrolling code to the window model --- lib/ace/editor.js | 24 ++++++------- lib/ace/model/window.js | 56 +++++++++++++++++++++++++++++ lib/ace/mouse_handler.js | 4 +-- lib/ace/view/window_view.js | 72 ------------------------------------- 4 files changed, 70 insertions(+), 86 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 9a27c9b2..8191c7d8 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -147,7 +147,7 @@ var Editor = function(windowView, buffer) { selection.removeEventListener("changeCursor", this.$onCursorChange); selection.removeEventListener("changeSelection", this.$onSelectionChange); - this.session.setScrollTopRow(this.renderer.getScrollTopRow()); + this.session.setScrollTopRow(this.windowModel.getScrollTopRow()); } this.session = session; @@ -204,7 +204,7 @@ var Editor = function(windowView, buffer) { this.onChangeBreakpoint(); this.onChangeAnnotation(); this.session.getUseWrapMode() && this.renderer.adjustWrapLimit(); - this.renderer.scrollToRow(session.getScrollTopRow()); + this.windowModel.scrollToRow(session.getScrollTopRow()); this.renderer.updateFull(); this._dispatchEvent("changeSession", { @@ -323,7 +323,7 @@ var Editor = function(windowView, buffer) { this.renderer.updateCursor(); if (!this.$blockScrolling) { - this.renderer.scrollCursorIntoView(); + this.windowModel.scrollCursorIntoView(); } // move text input over the cursor @@ -951,16 +951,16 @@ var Editor = function(windowView, buffer) { }; this.$getVisibleRowCount = function() { - return this.renderer.getScrollBottomRow() - this.renderer.getScrollTopRow() + 1; + return this.windowModel.getScrollBottomRow() - this.windowModel.getScrollTopRow() + 1; }; this.$getPageDownRow = function() { - return this.renderer.getScrollBottomRow(); + return this.windowModel.getScrollBottomRow(); }; this.$getPageUpRow = function() { - var firstRow = this.renderer.getScrollTopRow(); - var lastRow = this.renderer.getScrollBottomRow(); + var firstRow = this.windowModel.getScrollTopRow(); + var lastRow = this.windowModel.getScrollBottomRow(); return firstRow - (lastRow - firstRow); }; @@ -977,7 +977,7 @@ var Editor = function(windowView, buffer) { }; this.selectPageUp = function() { - var visibleRows = this.renderer.getScrollTopRow() - this.renderer.getScrollBottomRow(); + var visibleRows = this.windowModel.getScrollTopRow() - this.windowModel.getScrollBottomRow(); var row = this.$getPageUpRow() + Math.round(visibleRows / 2); this.scrollPageUp(); @@ -1009,21 +1009,21 @@ var Editor = function(windowView, buffer) { }; this.scrollPageUp = function() { - this.renderer.scrollToRow(this.$getPageUpRow()); + this.windowModel.scrollToRow(this.$getPageUpRow()); }; this.scrollToRow = function(row) { - this.renderer.scrollToRow(row); + this.windowModel.scrollToRow(row); }; this.scrollToLine = function(line, center) { - this.renderer.scrollToLine(line, center); + this.windowModel.scrollToLine(line, center); }; this.centerSelection = function() { var range = this.getSelectionRange(); var line = Math.floor(range.start.row + (range.end.row - range.start.row) / 2); - this.renderer.scrollToLine(line, true); + this.windowModel.scrollToLine(line, true); }; this.getCursorPosition = function() { diff --git a/lib/ace/model/window.js b/lib/ace/model/window.js index 0ce73dca..c85a22d8 100644 --- a/lib/ace/model/window.js +++ b/lib/ace/model/window.js @@ -160,11 +160,67 @@ var Window = exports.Window = function(theme) { this._emit("changeScrollLeft"); }; + this.scrollBy = function(deltaX, deltaY) { + deltaY && this.scrollToY(this.scrollTop + deltaY); + deltaX && this.scrollToX(this.scrollLeft + deltaX); + }; this.getScrollLeft = function() { return this.scrollLeft; }; + this.getScrollTopRow = function() { + return this.scrollTop / this.characterSize.height; + }; + + this.getScrollBottomRow = function() { + return Math.max(0, Math.floor((this.scrollTop + this.size.scrollerHeight) / this.characterSize.height) - 1); + }; + + this.scrollToRow = function(row) { + this.scrollToY(row * this.characterSize.height); + }; + + this.scrollToLine = function(line, center) { + var lineHeight = { lineHeight: this.characterSize.height }; + var offset = 0; + for (var l = 1; l < line; l++) { + offset += this.buffer.getRowHeight(lineHeight, l-1); + } + + if (center) { + offset -= this.size.scrollerHeight / 2; + } + this.scrollToY(offset); + }; + + this.scrollCursorIntoView = function() { + // the editor is not visible + if (this.size.scrollerHeight === 0) + return; + + var pos = this.getCursorPixelPosition(); + + var left = pos.left; + var top = pos.top; + + if (this.scrollTop > top) { + this.scrollToY(top); + } + + if (this.scrollTop + this.size.scrollerHeight < top + this.characterSize.height) + this.scrollToY(top + this.characterSize.height - this.size.scrollerHeight); + + var scrollLeft = this.scrollLeft; + + if (scrollLeft > left) { + this.scrollToX(left); + } + + if (scrollLeft + this.size.scrollerWidth < left + this.characterSize.width) + this.scrollToX(Math.round(left + this.characterSize.width - this.size.scrollerWidth)); + }; + // SETTINGS this.setBuffer = function(buffer) { diff --git a/lib/ace/mouse_handler.js b/lib/ace/mouse_handler.js index cf5ccb9a..f77eb2d1 100644 --- a/lib/ace/mouse_handler.js +++ b/lib/ace/mouse_handler.js @@ -261,7 +261,7 @@ var MouseHandler = function(editor) { editor.selection.selectToPosition(cursor); } - editor.renderer.scrollCursorIntoView(); + editor.windowModel.scrollCursorIntoView(); }; var onDragSelectionInterval = function() { @@ -307,7 +307,7 @@ var MouseHandler = function(editor) { this.onMouseWheel = function(e) { var speed = this.$scrollSpeed * 2; - this.editor.renderer.scrollBy(e.wheelX * speed, e.wheelY * speed); + this.editor.windowModel.scrollBy(e.wheelX * speed, e.wheelY * speed); return event.preventDefault(e); }; diff --git a/lib/ace/view/window_view.js b/lib/ace/view/window_view.js index b108aecc..887567fe 100644 --- a/lib/ace/view/window_view.js +++ b/lib/ace/view/window_view.js @@ -453,12 +453,6 @@ var WindowView = function(windowModel, container) { this.content.style.width = longestLine + "px"; this.content.style.height = minHeight + "px"; - // scroller.scrollWidth was smaller than scrollLeft we needed - if (this.$desiredScrollLeft) { - this.model.scrollToX(this.$desiredScrollLeft); - this.$desiredScrollLeft = 0; - } - // Horizontal scrollbar visibility may have changed, which changes // the client height of the scroller if (horizScrollChanged) @@ -541,67 +535,6 @@ var WindowView = function(windowModel, container) { this.$cursorLayer.showCursor(); }; - this.scrollCursorIntoView = function() { - // the editor is not visible - if (this.model.size.scrollerHeight === 0) - return; - - var pos = this.model.getCursorPixelPosition(); - - var left = pos.left; - var top = pos.top; - - if (this.model.scrollTop > top) { - this.model.scrollToY(top); - } - - if (this.model.scrollTop + this.model.size.scrollerHeight < top + this.lineHeight) { - this.model.scrollToY(top + this.lineHeight - this.model.size.scrollerHeight); - } - - var scrollLeft = this.model.scrollLeft; - - if (scrollLeft > left) { - this.model.scrollToX(left); - } - - if (scrollLeft + this.model.size.scrollerWidth < left + this.characterWidth) { - if (left > this.model.layerConfig.width) - this.$desiredScrollLeft = left + 2 * this.characterWidth; - else - this.model.scrollToX(Math.round(left + this.characterWidth - this.model.size.scrollerWidth)); - } - }; - - this.getScrollLeft = function() { - return this.model.scrollLeft; - }; - - this.getScrollTopRow = function() { - return this.model.scrollTop / this.lineHeight; - }; - - this.getScrollBottomRow = function() { - return Math.max(0, Math.floor((this.model.scrollTop + this.model.size.scrollerHeight) / this.lineHeight) - 1); - }; - - this.scrollToRow = function(row) { - this.model.scrollToY(row * this.lineHeight); - }; - - this.scrollToLine = function(line, center) { - var lineHeight = { lineHeight: this.lineHeight }; - var offset = 0; - for (var l = 1; l < line; l++) { - offset += this.session.getRowHeight(lineHeight, l-1); - } - - if (center) { - offset -= this.model.size.scrollerHeight / 2; - } - this.model.scrollToY(offset); - }; - this.updateScrollLeft = function() { this.scroller.scrollLeft = this.model.scrollLeft; }; @@ -610,11 +543,6 @@ var WindowView = function(windowModel, container) { this.$loop.schedule(this.CHANGE_SCROLL); }; - this.scrollBy = function(deltaX, deltaY) { - deltaY && this.model.scrollToY(this.model.scrollTop + deltaY); - deltaX && this.model.scrollToX(this.model.scrollLeft + deltaX); - }; - this.screenToTextCoordinates = function(pageX, pageY) { var canvasPos = this.scroller.getBoundingClientRect();