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

Commit

Permalink
Update Find tickmarks on window resize. Add new Async debouncing util to
Browse files Browse the repository at this point in the history
help with this.
Cleanup older code: no hand cursor since tickmarks not clickable for now,
move no-op check into ScrollTrackMarkers
  • Loading branch information
peterflynn committed Sep 13, 2013
1 parent 5f7ce7c commit 293ef35
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 52 deletions.
8 changes: 2 additions & 6 deletions src/search/FindReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ define(function (require, exports, module) {
});
});
state.marked.length = 0;
ScrollTrackMarkers.clear(cm);
ScrollTrackMarkers.clear();
}

function clearSearch(cm) {
Expand Down Expand Up @@ -197,12 +197,8 @@ define(function (require, exports, module) {


function toggleHighlighting(editor, enabled) {
// Temporarily change selection color to improve highlighting - see LESS code for details
if (enabled) {
if ($(editor.getRootElement()).hasClass("find-highlighting")) {
return;
}

// Temporarily change selection color to improve highlighting - see LESS code for details
$(editor.getRootElement()).addClass("find-highlighting");
} else {
$(editor.getRootElement()).removeClass("find-highlighting");
Expand Down
119 changes: 83 additions & 36 deletions src/search/ScrollTrackMarkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,83 +22,130 @@
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets */
/*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");
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 = [];

/** Clear any markers in the editor's tickmark track, but leave it visible */
function clear(codeMirror) {
$(".tickmark-track", codeMirror.getWrapperElement()).empty();

/** 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
} 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(editor, visible) {
var rootElem = editor.getRootElement();
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) {
return;
}

// TODO: what if window resized while Find is active?

var rootElem = editor.getRootElement();
var $sb = $(".CodeMirror-vscrollbar", rootElem);
var $overlay = $("<div class='tickmark-track'></div>");
$sb.parent().append($overlay);

trackHt = $sb[0].offsetHeight;
_calcScaling();

if (trackHt > 0) {
// Scrollbar visible: determine offset of track from top of scrollbar
if (brackets.platform === "win") {
console.log("WIN");
trackOffset = 17; // Up arrow pushes down track
} else {
console.log("MAC");
trackOffset = 0; // No arrows
// 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 {
// No scrollbar: use the height of the entire code content
trackHt = $(".CodeMirror-sizer", rootElem)[0].offsetHeight;
trackOffset = 0;
}

trackHt -= trackOffset * 2;

}));

} else {
$(".tickmark-track", rootElem).remove();
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(editor, pos) {
function addTickmark(curEditor, pos) {
console.assert(editor === curEditor);

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);
marks.push(pos);
_renderMark(pos);
}


Expand Down
2 changes: 0 additions & 2 deletions src/styles/brackets.less
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,6 @@ a, img {
.tickmark {
position: absolute;
width: 16px;
pointer-events: auto;
cursor: pointer;

height: 1px;
background-color: #eddd23;
Expand Down
37 changes: 31 additions & 6 deletions src/utils/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, window */
/*global define, $, setTimeout, clearTimeout */

/**
* Utilities for working with Deferred, Promise, and other asynchronous processes.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
function whenIdle(idleDelay, callback) {
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;
});
60 changes: 58 additions & 2 deletions test/spec/Async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, beforeEach, afterEach, it, runs, waits, waitsFor, waitsForDone, expect, $ */
/*global define, describe, beforeEach, afterEach, it, runs, waits, waitsFor, waitsForDone, expect, $, jasmine */

define(function (require, exports, module) {
"use strict";
Expand Down Expand Up @@ -304,7 +304,8 @@ define(function (require, exports, module) {
});

});



describe("Async PromiseQueue", function () {
var queue, calledFns;

Expand Down Expand Up @@ -440,5 +441,60 @@ define(function (require, exports, module) {
expect(queue._curPromise).toBe(null);
});
});


describe("whenIdle()", function () {
var spy, handler;
beforeEach(function () {
spy = jasmine.createSpy();
handler = Async.whenIdle(100, spy);
});

function ping(expectedCallCount) {
runs(function () {
expect(spy.callCount).toBe(expectedCallCount);
handler();
expect(spy.callCount).toBe(expectedCallCount);
});
}

it("should execute at end of tight sequence", function () {
ping(0);
waits(10);
ping(0);
waits(10);
ping(0);
waits(10);

waits(100);
runs(function () {
expect(spy.callCount).toBe(1);
});
});

it("should execute during gap and at end of sequence", function () {
ping(0);
waits(10);
ping(0);
waits(10);

waits(100);
runs(function () {
expect(spy.callCount).toBe(1);
});

ping(1);
waits(10);
runs(function () {
expect(spy.callCount).toBe(1);
});

waits(100);
runs(function () {
expect(spy.callCount).toBe(2);
});
});
});

});
});

0 comments on commit 293ef35

Please sign in to comment.