Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit ed9edd3

Browse files
author
Marcel Gerber
authored
Merge pull request #11293 from adobe/marcel/scroll-marks-fixes
Improve search tick mark positioning
2 parents 1053768 + 6da3ed8 commit ed9edd3

1 file changed

Lines changed: 65 additions & 13 deletions

File tree

src/search/ScrollTrackMarkers.js

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,43 @@ define(function (require, exports, module) {
6767
* @type {?jQueryObject}
6868
*/
6969
var $markedTickmark;
70+
71+
/**
72+
* Vertical space above and below the scrollbar
73+
* @type {number}
74+
*/
75+
var scrollbarTrackOffset;
76+
77+
switch (brackets.platform) {
78+
case "win": // Custom scrollbar CSS has no gap around the track
79+
scrollbarTrackOffset = 0;
80+
break;
81+
case "mac": // Native scrollbar has padding around the track
82+
scrollbarTrackOffset = 4;
83+
break;
84+
case "linux": // Custom scrollbar CSS has assymmetrical gap; this approximates it
85+
scrollbarTrackOffset = 2;
86+
break;
87+
}
7088

89+
/**
90+
* Vertical space above and below the scrollbar.
91+
* @return {number} amount Value in pixels
92+
*/
93+
function getScrollbarTrackOffset() {
94+
return scrollbarTrackOffset;
95+
}
7196

97+
/**
98+
* Sets how much vertical space there's above and below the scrollbar, which depends
99+
* on the OS and may also be affected by extensions
100+
* @param {number} offset Value in pixels
101+
*/
102+
function setScrollbarTrackOffset(offset) {
103+
scrollbarTrackOffset = offset;
104+
}
105+
106+
72107
function _getScrollbar(editor) {
73108
// Be sure to select only the direct descendant, not also elements within nested inline editors
74109
return $(editor.getRootElement()).children(".CodeMirror-vscrollbar");
@@ -81,16 +116,8 @@ define(function (require, exports, module) {
81116
trackHt = $sb[0].offsetHeight;
82117

83118
if (trackHt > 0) {
84-
// Scrollbar visible: determine offset of track from top of scrollbar
85-
if (brackets.platform === "win") {
86-
trackOffset = 0; // Custom scrollbar CSS has no gap around the track
87-
} else if (brackets.platform === "mac") {
88-
trackOffset = 4; // Native scrollbar has padding around the track
89-
} else { //(Linux)
90-
trackOffset = 2; // Custom scrollbar CSS has assymmetrical gap; this approximates it
91-
}
119+
trackOffset = getScrollbarTrackOffset();
92120
trackHt -= trackOffset * 2;
93-
94121
} else {
95122
// No scrollbar: use the height of the entire code content
96123
var codeContainer = $(editor.getRootElement()).find("> .CodeMirror-scroll > .CodeMirror-sizer > div > .CodeMirror-lines > div")[0];
@@ -101,9 +128,31 @@ define(function (require, exports, module) {
101128

102129
/** Add all the given tickmarks to the DOM in a batch */
103130
function _renderMarks(posArray) {
104-
var html = "";
131+
var html = "",
132+
cm = editor._codeMirror,
133+
editorHt = cm.getScrollerElement().scrollHeight;
134+
135+
// We've pretty much taken these vars and the getY function from CodeMirror's annotatescrollbar addon
136+
// https://github.com/codemirror/CodeMirror/blob/master/addon/scroll/annotatescrollbar.js
137+
var wrapping = cm.getOption("lineWrapping"),
138+
singleLineH = wrapping && cm.defaultTextHeight() * 1.5,
139+
curLine = null,
140+
curLineObj = null;
141+
142+
function getY(cm, pos) {
143+
if (curLine !== pos.line) {
144+
curLine = pos.line;
145+
curLineObj = cm.getLineHandle(curLine);
146+
}
147+
if (wrapping && curLineObj.height > singleLineH) {
148+
return cm.charCoords(pos, "local").top;
149+
}
150+
return cm.heightAtLine(curLineObj, "local");
151+
}
152+
105153
posArray.forEach(function (pos) {
106-
var top = Math.round(pos.line / editor.lineCount() * trackHt) + trackOffset;
154+
var cursorTop = getY(cm, pos),
155+
top = Math.round(cursorTop / editorHt * trackHt) + trackOffset;
107156
top--; // subtract ~1/2 the ht of a tickmark to center it on ideal pos
108157

109158
html += "<div class='tickmark' style='top:" + top + "px'></div>";
@@ -140,8 +189,8 @@ define(function (require, exports, module) {
140189
return;
141190
}
142191

143-
var $sb = _getScrollbar(editor);
144-
var $overlay = $("<div class='tickmark-track'></div>");
192+
var $sb = _getScrollbar(editor),
193+
$overlay = $("<div class='tickmark-track'></div>");
145194
$sb.parent().append($overlay);
146195

147196
_calcScaling();
@@ -201,4 +250,7 @@ define(function (require, exports, module) {
201250
exports.setVisible = setVisible;
202251
exports.addTickmarks = addTickmarks;
203252
exports.markCurrent = markCurrent;
253+
254+
exports.getScrollbarTrackOffset = getScrollbarTrackOffset;
255+
exports.setScrollbarTrackOffset = setScrollbarTrackOffset;
204256
});

0 commit comments

Comments
 (0)