1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 13:52:26 +01:00

Make completion generally niftier (add a rate limiting timer)

This commit is contained in:
Kris Maglione
2008-10-03 03:08:51 +00:00
parent b7b8b86b11
commit 36ca0c98a8
6 changed files with 160 additions and 130 deletions

View File

@@ -1246,13 +1246,11 @@ liberator.Buffer = function () //{{{
// TODO: make this an XBL element rather than messing with the content // TODO: make this an XBL element rather than messing with the content
// document // document
var doc = frames[next].document; var doc = frames[next].document;
var indicator = doc.createElement("div"); var indicator =
indicator.id = "liberator-frame-indicator"; <div id="liberator-frame-indicator"
// NOTE: need to set a high z-index - it's a crapshoot! style="background-color: red; opacity: 0.5; z-index: 999
var style = "background-color: red; opacity: 0.5; z-index: 999;" + position: fixed; top: 0; bottom: 0; left: 0; right: 0"/>;
"position: fixed; top: 0; bottom: 0; left: 0; right: 0;"; doc.body.appendChild(liberator.util.xmlToDom(indicator));
indicator.setAttribute("style", style);
doc.body.appendChild(indicator);
// remove the frame indicator // remove the frame indicator
setTimeout(function () doc.body.removeChild(indicator), 500); setTimeout(function () doc.body.removeChild(indicator), 500);

View File

@@ -32,10 +32,26 @@ liberator.Completion = function () //{{{
////////////////////// PRIVATE SECTION ///////////////////////////////////////// ////////////////////// PRIVATE SECTION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
var completionService = Components.classes["@mozilla.org/browser/global-history;2"]
.getService(Components.interfaces.nsIAutoCompleteSearch);
// the completion substrings, used for showing the longest common match // the completion substrings, used for showing the longest common match
var substrings = []; var substrings = [];
var urlResultsCache = null; var historyCache = [];
var urlCompletionCache = []; var historyResult = null;
var completionCache = [];
var historyTimer = new liberator.util.Timer(50, 100, function () {
let comp = [];
for (let i in liberator.util.range(0, historyResult.matchCount))
comp.push([historyResult.getValueAt(i),
historyResult.getCommentAt(i)]);
//let foo = ["", "IGNORED", "FAILURE", "NOMATCH", "SUCCESS", "NOMATCH_ONGOING", "SUCCESS_ONGOING"];
liberator.commandline.setCompletions(completionCache.concat(comp));
historyCache = comp;
});
// function uses smartcase // function uses smartcase
// list = [ [['com1', 'com2'], 'text'], [['com3', 'com4'], 'text'] ] // list = [ [['com1', 'com2'], 'text'], [['com3', 'com4'], 'text'] ]
@@ -439,46 +455,33 @@ liberator.Completion = function () //{{{
var autoCompletions = liberator.options["wildoptions"].indexOf("auto") >= 0; var autoCompletions = liberator.options["wildoptions"].indexOf("auto") >= 0;
var suggestEngineAlias = liberator.options["suggestengines"] || "google"; var suggestEngineAlias = liberator.options["suggestengines"] || "google";
// join all completion arrays together // join all completion arrays together
for (let i = 0; i < cpt.length; i++) for (let c in liberator.util.arrayIter(cpt))
{ {
if (cpt[i] == "s") if (c == "s")
completions = completions.concat(this.search(filter)[1]); completions = completions.concat(this.search(filter)[1]);
else if (cpt[i] == "f") else if (c == "f")
completions = completions.concat(this.file(filter, false)[1]); completions = completions.concat(this.file(filter, false)[1]);
else if (!autoCompletions && cpt[i] == "b") else if (c == "S")
completions = completions.concat(liberator.bookmarks.get(filter));
else if (!autoCompletions && cpt[i] == "h")
completions = completions.concat(liberator.history.get(filter));
else if (cpt[i] == "S")
completions = completions.concat(this.searchEngineSuggest(filter, suggestEngineAlias)[1]); completions = completions.concat(this.searchEngineSuggest(filter, suggestEngineAlias)[1]);
else if (autoCompletions && cpt[i] == "l") // add completions like Firefox's smart location bar else if (c == "b" && !autoCompletions)
completions = completions.concat(liberator.bookmarks.get(filter));
else if (c == "h" && !autoCompletions)
completions = completions.concat(liberator.history.get(filter));
else if (c == "l") // add completions like Firefox's smart location bar
{ {
var completionService = Components.classes["@mozilla.org/browser/global-history;2"] completionCache = completions;
.getService(Components.interfaces.nsIAutoCompleteSearch); completionService.startSearch(filter, "", historyResult, {
completionService.startSearch(filter, "", urlResultsCache, {
onSearchResult: function (search, result) { onSearchResult: function (search, result) {
//if (result.searchResult != result.RESULT_SUCCESS) historyResult = result;
// return; historyTimer.tell();
//liberator.log(result.searchResult); if (result.searchResult <= result.RESULT_SUCCESS)
//var res = "";// + util.objectToString(result) + "\n---\n"; historyTimer.force();
//liberator.log(result.matchCount + " matches: " + result.searchResult);
var comp = [];
//if (result.searchResult == result.RESULT_SUCCESS)
// urlResultsCache = result;
for (let i = 0; i < result.matchCount; i++)
{
comp.push([result.getValueAt(i), result.getCommentAt(i)]);
}
urlCompletionCache = comp;
if (comp.length > 0 || result.searchResult == result.RESULT_SUCCESS)
liberator.commandline.setCompletions(completions.concat(comp));
} }
}); });
} }
} }
return [start, completions.concat(urlCompletionCache)]; return [start, completions.concat(historyCache)];
}, },
userCommand: function (filter) userCommand: function (filter)

View File

@@ -176,11 +176,10 @@ liberator.Search = function () //{{{
} }
var baseNode = doc.createElementNS("http://www.w3.org/1999/xhtml", "span"); var baseNode = doc.createElementNS("http://www.w3.org/1999/xhtml", "span");
baseNode.setAttribute("style", liberator.options["hlsearchstyle"]); <span style={liberator.options["hlsearchstyle"] +
baseNode.style.display = "inline"; "; display: inline; font-size: inherit; padding: 0"}
baseNode.style.fontSize = "inherit"; class="__liberator-search"/>
baseNode.style.padding = "0"; baseNode = liberator.util.xmlToDom(baseNode);
baseNode.className = "__liberator-search";
var body = doc.body; var body = doc.body;
var count = body.childNodes.length; var count = body.childNodes.length;

View File

@@ -742,15 +742,15 @@ liberator.Options = function () //{{{
if (!filter) if (!filter)
{ {
let options = [[prefix + option.name, option.description] let opts = [[prefix + option.name, option.description]
for (option in options)]; for (option in options)];
return [0, options]; return [0, opts];
} }
else if (filter.indexOf("=") == -1) else if (filter.indexOf("=") == -1)
{ {
for (let option in options) for (let option in options)
optionCompletions.push([[prefix + name, option.description] optionCompletions.push([[prefix + name, option.description]
for (name in option.names) for each (name in option.names)
if (name.indexOf(filter) == 0)]); if (name.indexOf(filter) == 0)]);
// Flatten array. // Flatten array.
optionCompletions = Array.concat.apply(Array, optionCompletions); optionCompletions = Array.concat.apply(Array, optionCompletions);

View File

@@ -105,6 +105,13 @@ liberator.CommandLine = function () //{{{
var wildIndex = 0; // keep track how often we press <Tab> in a row var wildIndex = 0; // keep track how often we press <Tab> in a row
var startHints = false; // whether we're waiting to start hints mode var startHints = false; // whether we're waiting to start hints mode
var statusTimer = new liberator.util.Timer(50, 100, function ()
liberator.statusline.updateProgress("match " + (completionIndex + 1) + " of " + completions.length));
var autocompleteTimer = new liberator.util.Timer(50, 100, function (command) {
var res = liberator.completion.ex(command);
liberator.commandline.setCompletions(res[1], res[0]);
});
// the containing box for the promptWidget and commandWidget // the containing box for the promptWidget and commandWidget
var commandlineWidget = document.getElementById("liberator-commandline"); var commandlineWidget = document.getElementById("liberator-commandline");
// the prompt for the current command, for example : or /. Can be blank // the prompt for the current command, for example : or /. Can be blank
@@ -115,11 +122,13 @@ liberator.CommandLine = function () //{{{
// the widget used for multiline output // the widget used for multiline output
var multilineOutputWidget = document.getElementById("liberator-multiline-output"); var multilineOutputWidget = document.getElementById("liberator-multiline-output");
var stylesheet = multilineOutputWidget.contentDocument.createElement("link"); var stylesheet =
stylesheet.setAttribute("rel", "stylesheet"); <link rel="stylesheet" type="text/css"
stylesheet.setAttribute("type", "text/css"); href={"chrome://" + liberator.config.name.toLowerCase() + "/skin/vimperator.css"}/>;
stylesheet.setAttribute("href", "chrome://" + liberator.config.name.toLowerCase() + "/skin/vimperator.css");
multilineOutputWidget.contentDocument.getElementsByTagName("head")[0].appendChild(stylesheet); stylesheet = liberator.util.xmlToDom(stylesheet, multilineOutputWidget.contentDocument);
multilineOutputWidget.contentDocument.getElementsByTagName("head")[0]
.appendChild(stylesheet);
multilineOutputWidget.contentDocument.body.id = "liberator-multiline-output-content"; multilineOutputWidget.contentDocument.body.id = "liberator-multiline-output-content";
@@ -152,10 +161,7 @@ liberator.CommandLine = function () //{{{
liberator.registerCallback("change", liberator.modes.EX, function (command) { liberator.registerCallback("change", liberator.modes.EX, function (command) {
if (liberator.options["wildoptions"].indexOf("auto") >= 0) if (liberator.options["wildoptions"].indexOf("auto") >= 0)
{ autocompleteTimer.tell(command);
var res = liberator.completion.ex(command);
liberator.commandline.setCompletions(res[1], res[0]);
}
else else
completionIndex = UNINITIALIZED; completionIndex = UNINITIALIZED;
}); });
@@ -558,6 +564,7 @@ liberator.CommandLine = function () //{{{
historyIndex = UNINITIALIZED; historyIndex = UNINITIALIZED;
completionIndex = UNINITIALIZED; completionIndex = UNINITIALIZED;
autocompleteTimer.reset();
liberator.modes.push(liberator.modes.COMMAND_LINE, currentExtendedMode); liberator.modes.push(liberator.modes.COMMAND_LINE, currentExtendedMode);
setHighlightGroup(this.HL_NORMAL); setHighlightGroup(this.HL_NORMAL);
@@ -805,6 +812,7 @@ liberator.CommandLine = function () //{{{
completions.sort(function (a, b) String.localeCompare(a[0], b[0])); completions.sort(function (a, b) String.localeCompare(a[0], b[0]));
completionList.setItems(completions); completionList.setItems(completions);
statusTimer.reset();
} }
if (completions.length == 0) if (completions.length == 0)
@@ -833,7 +841,7 @@ liberator.CommandLine = function () //{{{
// FIXME: this innocent looking line is the source of a big performance // FIXME: this innocent looking line is the source of a big performance
// problem, when keeping <Tab> pressed down, so disable it for now // problem, when keeping <Tab> pressed down, so disable it for now
// liberator.statusline.updateProgress("match " + (completionIndex + 1) + " of " + completions.length); statusTimer.tell();
} }
// the following line is not inside if (hasList) for list:longest,full // the following line is not inside if (hasList) for list:longest,full
@@ -1200,10 +1208,9 @@ liberator.ItemList = function (id) //{{{
var doc = iframe.contentDocument; var doc = iframe.contentDocument;
var container = iframe.parentNode; var container = iframe.parentNode;
var stylesheet = doc.createElement("link"); var stylesheet = liberator.util.xmlToDom(
stylesheet.setAttribute("rel", "stylesheet"); <link rel="stylesheet" type="text/css"
stylesheet.setAttribute("type", "text/css"); href={"chrome://" + liberator.config.name.toLowerCase() + "/skin/vimperator.css"}/>, doc);
stylesheet.setAttribute("href", "chrome://" + liberator.config.name.toLowerCase() + "/skin/vimperator.css");
doc.getElementsByTagName("head")[0].appendChild(stylesheet); doc.getElementsByTagName("head")[0].appendChild(stylesheet);
doc.body.id = id + "-content"; doc.body.id = id + "-content";
@@ -1227,44 +1234,20 @@ liberator.ItemList = function (id) //{{{
catch (e) {} // for muttator! catch (e) {} // for muttator!
// TODO: temporary, to be changed/removed // TODO: temporary, to be changed/removed
function createRow(a, b, c) function createRow(a, b, c, dom)
{ {
var row = doc.createElement("tr"); let row =
row.setAttribute("class", "liberator-compitem"); <tr class="liberator-compitem">
var icon = doc.createElement("td"); <td style="width: 16px"/>
icon.setAttribute("style", "width: 16px"); <td style="width: 45%; overflow: hidden">{b}</td>
<td style="color: gray">{c}</td>
</tr>
if (a) if (a)
{ row.td[0].* = <img width="16px" height="16px" src={a}/>;
var img = doc.createElement("img");
img.setAttribute("width", "16px");
img.setAttribute("height", "16px");
img.setAttribute("src", a);
icon.appendChild(img);
}
row.appendChild(icon);
var title = doc.createElement("td");
title.appendChild(doc.createTextNode(b));
title.setAttribute("style", "width: 45%; overflow:hidden");
row.appendChild(title);
var url = doc.createElement("td");
url.setAttribute("style", "color: gray");
url.appendChild(doc.createTextNode(c));
row.appendChild(url);
return row;
var row = doc.createElement("tr");
row.setAttribute("class", "liberator-compitem");
var icon = doc.createElement("td");
XML.prettyPrinting = false;
icon.innerHTML = <>
<td style="vertical-align: middle"><img src={a} width="16px" height="16px"/></td>
<td style="vertical-align:middle">{b}<br/><span style="color: green">{c}</span></td>;
</>;
row.appendChild(icon);
if (dom)
return liberator.util.xmlToDom(row, doc);
return row; return row;
} }
@@ -1296,7 +1279,7 @@ liberator.ItemList = function (id) //{{{
*/ */
function fill(offset) function fill(offset)
{ {
if (listOffset == offset || offset < 0 || offset >= completions.length) if (listOffset == offset || offset < 0 || offset >= completions.length && completions.length)
return; return;
if (listIndex > -1 && offset == listOffset + 1) if (listIndex > -1 && offset == listOffset + 1)
@@ -1310,10 +1293,11 @@ liberator.ItemList = function (id) //{{{
} }
catch (e) {} catch (e) {}
var row = createRow(icon, completions[offset + maxItems - 1][0], completions[offset + maxItems - 1][1]); var row = createRow(icon, completions[offset + maxItems - 1][0], completions[offset + maxItems - 1][1], true);
var e = doc.getElementsByTagName("tbody"); var e = doc.getElementsByTagName("tbody");
e[e.length - 1].removeChild(e[e.length - 1].firstChild); e = e[e.length - 1];
e[e.length - 1].appendChild(row); e.removeChild(e.firstChild);
e.appendChild(row);
completionElements = doc.getElementsByClassName("liberator-compitem"); // TODO: make faster completionElements = doc.getElementsByClassName("liberator-compitem"); // TODO: make faster
return; return;
} }
@@ -1327,58 +1311,43 @@ liberator.ItemList = function (id) //{{{
icon = faviconService.getFaviconImageForPage(uri).spec; icon = faviconService.getFaviconImageForPage(uri).spec;
} }
catch (e) {} catch (e) {}
var row = createRow(icon, completions[offset][0], completions[offset][1]); var row = createRow(icon, completions[offset][0], completions[offset][1], true);
var e = doc.getElementsByTagName("tbody"); var e = doc.getElementsByTagName("tbody");
e[e.length - 1].removeChild(e[e.length - 1].lastChild); e = e[e.length - 1];
e[e.length - 1].insertBefore(row, e[e.length - 1].firstChild); e.removeChild(e.lastChild);
e.insertBefore(row, e.firstChild);
completionElements = doc.getElementsByClassName("liberator-compitem"); // TODO: make faster completionElements = doc.getElementsByClassName("liberator-compitem"); // TODO: make faster
return; return;
} }
listOffset = offset; listOffset = offset;
// do a full refill of the list: // do a full refill of the list:
doc.body.innerHTML = ""; doc.body.innerHTML = "";
var div = doc.createElement("div"); let div = <div class="ex-command-output hl-Normal">
div.setAttribute("class", "ex-command-output hl-Normal"); <span class="hl-Title" style="font-weight: bold">Completions:</span>
div.innerHTML = "<span class=\"hl-Title\" style=\"font-weight: bold\">Completions:</span>"; <table width="100%" style="table-layout: fixed; width: 100%"><tbody/></table>
var table = doc.createElement("table"); </div>;
table.setAttribute("width", "100%"); let tbody = div..tbody;
table.setAttribute("style", "table-layout: fixed; width: 100%");
//div.appendChild(table);
var tbody = doc.createElement("tbody");
table.appendChild(tbody);
for (let i = 0; i < completions.length; i++) for (let [i, elem] in Iterator(completions))
{ {
var elem = completions[i];
if (i >= listOffset && i - listOffset < maxItems) if (i >= listOffset && i - listOffset < maxItems)
{ {
var icon = ""; let icon = "";
try try
{ {
var uri = ioService.newURI(elem[0], null, null); let uri = ioService.newURI(elem[0], null, null);
icon = faviconService.getFaviconImageForPage(uri).spec; icon = faviconService.getFaviconImageForPage(uri).spec;
//dump(icon + "\n");
} }
catch (e) {} catch (e) {}
if (i == -132434) tbody.* += createRow(icon, elem[0], elem[1]);
{
var row = doc.createElement("tr");
row.setAttribute("align", "center");
row.innerHTML = '<th width="16px"></th><th style="background-color: gray !important;"><span style="font-weight: bold;">Bookmarks</span></th>';
tbody.appendChild(row);
}
else
{
tbody.appendChild(createRow(icon, elem[0], elem[1]));
}
} }
}; };
doc.body.appendChild(div); doc.body.appendChild(liberator.util.xmlToDom(div, doc));
doc.body.appendChild(table);
completionElements = doc.getElementsByClassName("liberator-compitem"); completionElements = doc.getElementsByClassName("liberator-compitem");
autoSize(); autoSize();

View File

@@ -28,6 +28,51 @@ the terms of any one of the MPL, the GPL or the LGPL.
liberator.util = { //{{{ liberator.util = { //{{{
Timer: function Timer(minInterval, maxInterval, callback)
{
let self = this;
let timer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
this.first = true; /* When set, trigger immediately. */
this.latest = 0;
this.notify = function (aTimer)
{
timer.cancel();
if (this.latest || this.first)
callback(this.arg);
else /* Don't trigger. Set this.first after the minimum interval. */
timer.initWithCallback(this, minInterval, timer.TYPE_ONE_SHOT);
this.first = (this.latest == 0);
this.latest = 0;
}
this.tell = function (arg)
{
if (arg != undefined)
this.arg = arg;
if (this.first || this.latest > Date.now())
return this.notify();
if (this.latest)
timer.cancel();
let timeout = minInterval;
if (this.latest)
timeout = Math.min(minInterval, this.latest - Date.now());
else
this.latest = Date.now() + maxInterval;
this.timer = timer.initWithCallback(this, timeout, timer.TYPE_ONE_SHOT);
}
this.reset = function ()
{
timer.cancel();
this.first = true;
}
this.flush = function ()
{
if (this.latest)
this.notify();
}
},
arrayIter: function (ary) arrayIter: function (ary)
{ {
let length = ary.length; let length = ary.length;
@@ -353,7 +398,23 @@ liberator.util = { //{{{
} }
return urls; return urls;
},
xmlToDom: function (node, doc)
{
switch (node.nodeKind())
{
case "text":
return doc.createTextNode(node);
case "element":
let domnode = doc.createElement(node.name());
for each (let attr in node.@*)
domnode.setAttribute(attr.name(), String(attr));
for each (let child in node.*)
domnode.appendChild(arguments.callee(child, doc));
return domnode;
} }
},
}; //}}} }; //}}}
// vim: set fdm=marker sw=4 ts=4 et: // vim: set fdm=marker sw=4 ts=4 et: