-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Show scroll track tickmarks for Find results #5191
Changes from 6 commits
e752e9a
1a2a12e
aba8da9
41b71aa
5f7ce7c
293ef35
859119f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */ | ||
/*global define, $, brackets, window */ | ||
|
||
|
||
/** | ||
* Manages tickmarks shown along the scrollbar track. | ||
* NOT yet intended for use by anyone other than the FindReplace module. | ||
* It is assumed that markers are always clear()ed when switching editors. | ||
*/ | ||
define(function (require, exports, module) { | ||
"use strict"; | ||
|
||
var Editor = require("editor/Editor"), | ||
EditorManager = require("editor/EditorManager"), | ||
Async = require("utils/Async"); | ||
|
||
|
||
/** @type {?Editor} Editor the markers are currently shown for, or null if not shown */ | ||
var editor; | ||
|
||
/** @type {number} Top of scrollbar track area, relative to top of scrollbar */ | ||
var trackOffset; | ||
|
||
/** @type {number} Height of scrollbar track area */ | ||
var trackHt; | ||
|
||
/** @type {!Array.<{line: number, ch: number}>} Text positions of markers */ | ||
var marks = []; | ||
|
||
|
||
/** Measure scrollbar track */ | ||
function _calcScaling() { | ||
var rootElem = editor.getRootElement(); | ||
var $sb = $(".CodeMirror-vscrollbar", rootElem); | ||
|
||
trackHt = $sb[0].offsetHeight; | ||
|
||
if (trackHt > 0) { | ||
// Scrollbar visible: determine offset of track from top of scrollbar | ||
if (brackets.platform === "win") { | ||
trackOffset = 17; // Up arrow pushes down track | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it safe to always assume the scroll up arrow is 17 pixels? Would it be safer to assume that the arrow is always square and use the width of the scrollbar instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (if we do end up keeping this as a hard-coded value, it should be declared as a constant) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the extra padding that CM places on the scrollbar div, I'd have to do something like this:
...which seems not necessarily less fragile than a magic number. So I'm sort of inclined to just hardcode it in a const, if you're ok with it. |
||
} else { | ||
trackOffset = 0; // No arrows | ||
} | ||
|
||
} else { | ||
// No scrollbar: use the height of the entire code content | ||
trackHt = $(".CodeMirror-sizer", rootElem)[0].offsetHeight; | ||
trackOffset = 0; | ||
} | ||
|
||
trackHt -= trackOffset * 2; | ||
} | ||
|
||
/** Add one tickmark to the DOM */ | ||
function _renderMark(pos) { | ||
var top = Math.round(pos.line / editor.lineCount() * trackHt) + trackOffset; | ||
top--; // subtract ~1/2 the ht of a tickmark to center it on ideal pos | ||
|
||
var $mark = $("<div class='tickmark' style='top:" + top + "px'></div>"); | ||
|
||
$(".tickmark-track", editor.getRootElement()).append($mark); | ||
} | ||
|
||
|
||
/** | ||
* Clear any markers in the editor's tickmark track, but leave it visible. Safe to call when | ||
* tickmark track is not visible also. | ||
*/ | ||
function clear() { | ||
if (editor) { | ||
$(".tickmark-track", editor.getRootElement()).empty(); | ||
marks = []; | ||
} | ||
} | ||
|
||
/** Add or remove the tickmark track from the editor's UI */ | ||
function setVisible(curEditor, visible) { | ||
// short-circuit no-ops | ||
if ((visible && curEditor === editor) || (!visible && !editor)) { | ||
return; | ||
} | ||
|
||
if (visible) { | ||
console.assert(!editor); | ||
editor = curEditor; | ||
|
||
// Don't support inline editors yet - search inside them is pretty screwy anyway (#2110) | ||
if (editor.getFirstVisibleLine() > 0 || editor.getLastVisibleLine() < editor.lineCount() - 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test will fail if the inline editor contains all the lines of the file. Yes, that is a degenerate case, but it is possible... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
return; | ||
} | ||
|
||
var rootElem = editor.getRootElement(); | ||
var $sb = $(".CodeMirror-vscrollbar", rootElem); | ||
var $overlay = $("<div class='tickmark-track'></div>"); | ||
$sb.parent().append($overlay); | ||
|
||
_calcScaling(); | ||
|
||
// Update tickmarks during window resize (whenever resizing has paused/stopped for > 1/3 sec) | ||
$(window).on("resize.ScrollTrackMarkers", Async.whenIdle(300, function () { | ||
if (marks.length) { | ||
_calcScaling(); | ||
$(".tickmark-track", editor.getRootElement()).empty(); | ||
marks.forEach(function (pos) { | ||
_renderMark(pos); | ||
}); | ||
} | ||
})); | ||
|
||
} else { | ||
console.assert(editor === curEditor); | ||
$(".tickmark-track", curEditor.getRootElement()).remove(); | ||
editor = null; | ||
marks = []; | ||
$(window).off("resize.ScrollTrackMarkers"); | ||
} | ||
} | ||
|
||
/** Add a tickmark to the editor's tickmark track, if it's visible */ | ||
function addTickmark(curEditor, pos) { | ||
console.assert(editor === curEditor); | ||
|
||
marks.push(pos); | ||
_renderMark(pos); | ||
} | ||
|
||
|
||
exports.clear = clear; | ||
exports.setVisible = setVisible; | ||
exports.addTickmark = addTickmark; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
|
||
|
||
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ | ||
/*global define, $, window */ | ||
/*global define, $, setTimeout, clearTimeout */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our coding conventions say that browser globals should be referenced via the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bah. Fixed :-) |
||
|
||
/** | ||
* Utilities for working with Deferred, Promise, and other asynchronous processes. | ||
|
@@ -225,7 +225,7 @@ define(function (require, exports, module) { | |
// if we've exhausted our maxBlockingTime | ||
if ((new Date()).getTime() - sliceStartTime >= maxBlockingTime) { | ||
//yield | ||
window.setTimeout(function () { | ||
setTimeout(function () { | ||
sliceStartTime = (new Date()).getTime(); | ||
result.resolve(); | ||
}, idleTime); | ||
|
@@ -301,11 +301,11 @@ define(function (require, exports, module) { | |
function withTimeout(promise, timeout) { | ||
var wrapper = new $.Deferred(); | ||
|
||
var timer = window.setTimeout(function () { | ||
var timer = setTimeout(function () { | ||
wrapper.reject(ERROR_TIMEOUT); | ||
}, timeout); | ||
promise.always(function () { | ||
window.clearTimeout(timer); | ||
clearTimeout(timer); | ||
}); | ||
|
||
// If the wrapper was already rejected due to timeout, the Promise's calls to resolve/reject | ||
|
@@ -419,14 +419,39 @@ define(function (require, exports, module) { | |
}); | ||
} | ||
}; | ||
|
||
|
||
/** | ||
* Implements "debouncing." Returns a function that can be called frequently, triggering 'callback' only when calls | ||
* to this function have paused for >= 'idleDelay' ms. The callback may be called multiple times, if there are | ||
* multiple idleDelay-sized gaps in the event sequence. Invoking the callback can be delayed *indefinitely* if the | ||
* event sequence continues forever with no idleDelay-sized gaps at all. | ||
* | ||
* @param {number} idleDelay Minimum delay (ms) before invoking callback. | ||
* @param {!function()} callback | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
function whenIdle(idleDelay, callback) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit: the underscore I also wonder if we should just call this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...or start using underscore? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dislike having the number second because it gets lost several lines below when the first arg is an inline closure... just like using setTimeout(). But I'm willing to cave on that if you'd rather have consistency. I deliberately shied away from calling this "debounce()" because I've had the impression that it's not a common enough term to be readily understood (in fact I've heard a few people confuse debounce vs. throttle, since they're both somewhat opaque terms). I've tried to use more plain-language, but still fairly compact, function names in this package as much as possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Although "doInParallel_aggregateErrors()" was not exactly an epic success on that naming-convention front :-P) |
||
var timer; | ||
return function () { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
timer = setTimeout(function () { | ||
timer = null; | ||
callback(); | ||
}, idleDelay); | ||
}; | ||
} | ||
|
||
|
||
// Define public API | ||
exports.doInParallel = doInParallel; | ||
exports.doSequentially = doSequentially; | ||
exports.doSequentiallyInBackground = doSequentiallyInBackground; | ||
exports.doSequentiallyInBackground = doSequentiallyInBackground; | ||
exports.doInParallel_aggregateErrors = doInParallel_aggregateErrors; | ||
exports.withTimeout = withTimeout; | ||
exports.ERROR_TIMEOUT = ERROR_TIMEOUT; | ||
exports.chain = chain; | ||
exports.PromiseQueue = PromiseQueue; | ||
exports.ERROR_TIMEOUT = ERROR_TIMEOUT; | ||
exports.whenIdle = whenIdle; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should update this comment to include "and hide track markers"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed