1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 22:07:58 +01:00

Fix some cleanup issues.

--HG--
branch : bootstrapped
This commit is contained in:
Kris Maglione
2010-12-24 18:32:22 -05:00
parent 2f081bd805
commit d3d60548f1
8 changed files with 176 additions and 138 deletions

2
common/bootstrap.js vendored
View File

@@ -171,10 +171,10 @@ function init() {
function shutdown(data, reason) { function shutdown(data, reason) {
dump("dactyl: bootstrap: shutdown\n"); dump("dactyl: bootstrap: shutdown\n");
services.observer.notifyObservers(null, "dactyl-cleanup", null);
for (let factory in values(components)) for (let factory in values(components))
// TODO: Categories; // TODO: Categories;
factory.unregister(); factory.unregister();
services.observer.notifyObservers(null, "dactyl-cleanup", null);
} }
function reasonToString(reason) { function reasonToString(reason) {

View File

@@ -147,6 +147,40 @@ const Buffer = Module("buffer", {
let elem = event.originalTarget; let elem = event.originalTarget;
buffer.viewSource([elem.getAttribute("href"), Number(elem.getAttribute("line"))]) buffer.viewSource([elem.getAttribute("href"), Number(elem.getAttribute("line"))])
}; };
this.replaceProgressListener(this.progressListener);
},
cleanup: function () {
for (let prop in properties(this.progressListener))
if (!this.progressListener.__lookupGetter__(prop) &&
!callable(this.progressListener[prop]))
this.origProgressListener[prop] = this.progressListener[prop]
this.replaceProgressListener(this.origProgressListener);
},
replaceProgressListener: function (newListener) {
// I hate this whole hack. --Kris
let obj = window.XULBrowserWindow, getter;
for (let prop in properties(obj))
if ((getter = obj.__lookupGetter__(prop)) && !obj.__lookupSetter__(prop)) {
newListener.__defineGetter__(prop, getter);
delete obj[prop];
}
this.origProgressListener = window.XULBrowserWindow;
try {
config.browser.removeProgressListener(window.XULBrowserWindow);
}
catch (e) {} // Why? --djk
config.browser.addProgressListener(newListener, Ci.nsIWebProgress.NOTIFY_ALL);
window.XULBrowserWindow = newListener;
window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem).treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIXULWindow)
.XULBrowserWindow = newListener;
}, },
destroy: function () { destroy: function () {
@@ -1502,26 +1536,6 @@ const Buffer = Module("buffer", {
}; };
}, },
events: function () { events: function () {
try {
config.browser.removeProgressListener(window.XULBrowserWindow);
}
catch (e) {} // Why? --djk
// I hate this whole hack. --Kris
let obj = window.XULBrowserWindow, getter;
for (let p in properties(obj))
if ((getter = obj.__lookupGetter__(p)) && !obj.__lookupSetter__(p)) {
this.progressListener.__defineGetter__(p, getter);
delete obj[p];
}
config.browser.addProgressListener(this.progressListener, Ci.nsIWebProgress.NOTIFY_ALL);
window.XULBrowserWindow = this.progressListener;
window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem).treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIXULWindow)
.XULBrowserWindow = this.progressListener;
events.addSessionListener(config.browser, "DOMContentLoaded", this.closure.onDOMContentLoaded, true); events.addSessionListener(config.browser, "DOMContentLoaded", this.closure.onDOMContentLoaded, true);
events.addSessionListener(config.browser, "load", this.closure.onPageLoad, true); events.addSessionListener(config.browser, "load", this.closure.onPageLoad, true);
events.addSessionListener(config.browser, "scroll", this.closure._updateBufferPosition, false); events.addSessionListener(config.browser, "scroll", this.closure._updateBufferPosition, false);

View File

@@ -135,9 +135,25 @@ const CommandWidgets = Class("CommandWidgets", {
getGroup: function (value) { getGroup: function (value) {
if (this.command && !options.get("guioptions").has("M")) if (this.command && !options.get("guioptions").has("M"))
return this.statusbar; return this.statusbar;
let statusElem = this.statusbar.message; let statusElem = this.statusbar.message;
if (value && statusElem.inputField.editor.rootElement.scrollWidth > statusElem.scrollWidth) if (!statusElem.editor) {
util.dump(commandline == window.dactyl.modules.commandline,
window.dactyl.modules.commandline.widgets == this,
commandline.widgets == this);
util.dumpStack();
for (let i=0, e = statusElem; e; e = e.parentNode)
util.dump(i++, e == statusline._statusLine.firstChild,
e == statusline.widgets.container,
e);
util.dump(statusElem == statusline.widgets.message);
util.dump(statusline._statusLine.firstChild == statusline.widgets.container);
}
else {
if (value && statusElem.editor.rootElement.scrollWidth > statusElem.scrollWidth)
return this.commandbar; return this.commandbar;
}
return this.activeGroup.mode; return this.activeGroup.mode;
} }
}); });
@@ -165,13 +181,13 @@ const CommandWidgets = Class("CommandWidgets", {
const self = this; const self = this;
this.elements[obj.name] = obj; this.elements[obj.name] = obj;
function get(id) obj.getElement ? obj.getElement(id) : document.getElementById(id); function get(prefix, map, id) (obj.getElement || util.identity)(map[id] || document.getElementById(prefix + id));
this.active.__defineGetter__(obj.name, function () self.activeGroup[obj.name][obj.name]); this.active.__defineGetter__(obj.name, function () self.activeGroup[obj.name][obj.name]);
this.activeGroup.__defineGetter__(obj.name, function () self.getGroup(obj.name)); this.activeGroup.__defineGetter__(obj.name, function () self.getGroup(obj.name));
memoize(this.statusbar, obj.name, function () get("dactyl-statusline-field-" + (obj.id || obj.name))); memoize(this.statusbar, obj.name, function () get("dactyl-statusline-field-", statusline.widgets, (obj.id || obj.name)));
memoize(this.commandbar, obj.name, function () get("dactyl-" + (obj.id || obj.name))); memoize(this.commandbar, obj.name, function () get("dactyl-", {}, (obj.id || obj.name)));
if (!(obj.noValue || obj.getValue)) if (!(obj.noValue || obj.getValue))
Object.defineProperty(this, obj.name, Modes.boundProperty({ Object.defineProperty(this, obj.name, Modes.boundProperty({
@@ -259,8 +275,7 @@ const CommandWidgets = Class("CommandWidgets", {
multilineInput: Class.memoize(function () document.getElementById("dactyl-multiline-input")), multilineInput: Class.memoize(function () document.getElementById("dactyl-multiline-input")),
mowContainer: Class.memoize(function () this.multilineOutput.parentNode) mowContainer: Class.memoize(function () this.multilineOutput.parentNode)
}, { }, {
getEditor: function (id) { getEditor: function (elem) {
let elem = document.getElementById(id);
elem.inputField.QueryInterface(Ci.nsIDOMNSEditableElement); elem.inputField.QueryInterface(Ci.nsIDOMNSEditableElement);
return elem; return elem;
} }

View File

@@ -53,7 +53,8 @@ const Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
observe: { observe: {
"dactyl-cleanup": function () { "dactyl-cleanup": function () {
for (let [, mod] in iter(array(values(modules)).reverse())) for (let name in values(Object.getOwnPropertyNames(modules).reverse())) {
let mod = modules[name];
if (mod instanceof ModuleBase) { if (mod instanceof ModuleBase) {
if ("cleanup" in mod) if ("cleanup" in mod)
mod.cleanup(); mod.cleanup();
@@ -61,6 +62,7 @@ const Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
mod.destroy(); mod.destroy();
} }
} }
}
}, },
/** @property {string} The name of the current user profile. */ /** @property {string} The name of the current user profile. */

View File

@@ -10,6 +10,19 @@
const StatusLine = Module("statusline", { const StatusLine = Module("statusline", {
init: function () { init: function () {
this._statusLine = document.getElementById("status-bar");
this.statusBar = document.getElementById("addon-bar") || this._statusLine;
this.statusBar.collapsed = true; // it is later restored unless the user sets laststatus=0
if (this.statusBar.localName == "toolbar") {
styles.system.add("addon-bar", config.styleableChrome, <css><![CDATA[
#status-bar { margin-top: 0 !important; }
#addon-bar { padding: 0 !important; min-height: 18px !important; }
#addon-bar > statusbar { -moz-box-flex: 1 }
#addon-bar > #addonbar-closebutton { visibility: collapse; }
#addon-bar > xul|toolbarspring { visibility: collapse; }
]]></css>);
}
let _commandline = "if (window.dactyl) return dactyl.modules.commandline"; let _commandline = "if (window.dactyl) return dactyl.modules.commandline";
let prepend = <e4x xmlns={XUL} xmlns:dactyl={NS}> let prepend = <e4x xmlns={XUL} xmlns:dactyl={NS}>
@@ -21,8 +34,8 @@ const StatusLine = Module("statusline", {
<hbox class="dactyl-container" dactyl:highlight="CmdLine StatusCmdLine"> <hbox class="dactyl-container" dactyl:highlight="CmdLine StatusCmdLine">
<label class="plain" key="mode" crop="end" collapsed="true"/> <label class="plain" key="mode" crop="end" collapsed="true"/>
<stack flex="1" class="dactyl-container" dactyl:highlight="CmdLine StatusCmdLine"> <stack flex="1" class="dactyl-container" dactyl:highlight="CmdLine StatusCmdLine">
<textbox class="plain" key="url" crop="end" flex="1" readonly="true"/> <textbox key="url" crop="end" flex="1" readonly="true" class="plain dactyl-status-field-url"/>
<textbox class="plain" key="message" crop="end" flex="1" readonly="true" dactyl:highlight="Normal StatusNormal"/> <textbox key="message" crop="end" flex="1" readonly="true" class="plain" dactyl:highlight="Normal StatusNormal"/>
</stack> </stack>
</hbox> </hbox>
@@ -45,31 +58,11 @@ const StatusLine = Module("statusline", {
</statusbar> </statusbar>
</e4x>; </e4x>;
for each (let attr in prepend..@key) util.dump("statusbar: load overlay");
attr.parent().@id = "dactyl-statusline-field-" + attr;
util.overlayWindow(window, { util.overlayWindow(window, {
objects: this.widgets = { get status() this.container }, objects: this.widgets = { get status() this.container },
prepend: prepend.elements() prepend: prepend.elements()
}); });
this._statusLine = document.getElementById("status-bar");
this.statusBar = document.getElementById("addon-bar") || this._statusLine;
this.statusBar.collapsed = true; // it is later restored unless the user sets laststatus=0
if (this.statusBar.localName == "toolbar") {
styles.system.add("addon-bar", config.styleableChrome, <css><![CDATA[
#status-bar { margin-top: 0 !important; }
#addon-bar { padding: 0 !important; min-height: 18px !important; }
#addon-bar > statusbar { -moz-box-flex: 1 }
#addon-bar > #addonbar-closebutton { visibility: collapse; }
#addon-bar > xul|toolbarspring { visibility: collapse; }
]]></css>);
let parent = this.widgets.status.parentNode;
parent.removeChild(this.widgets.status);
parent.insertBefore(this.widgets.status, parent.firstChild);
}
}, },
get visible() !this.statusBar.collapsed && !this.statusBar.hidden, get visible() !this.statusBar.collapsed && !this.statusBar.hidden,

View File

@@ -142,25 +142,39 @@ const ObjectStore = Class("ObjectStore", StoreBase, {
__iterator__: function () Iterator(this._object), __iterator__: function () Iterator(this._object),
}); });
var keys = {};
var observers = {};
const Storage = Module("Storage", { const Storage = Module("Storage", {
alwaysReload: {}, alwaysReload: {},
init: function () {
this.cleanup();
},
cleanup: function () {
for (let key in keys(this.keys))
delete this[key];
for (let ary in values(this.observers))
for (let obj in values(ary))
if (obj.ref && obj.ref.get())
delete obj.ref.get().dactylStorageRefs;
this.keys = {};
this.observers = {};
},
newObject: function newObject(key, constructor, params) { newObject: function newObject(key, constructor, params) {
if (params == null || !isObject(params)) if (params == null || !isObject(params))
throw Error("Invalid argument type"); throw Error("Invalid argument type");
if (!(key in keys) || params.reload || this.alwaysReload[key]) { if (!(key in this.keys) || params.reload || this.alwaysReload[key]) {
if (key in this && !(params.reload || this.alwaysReload[key])) if (key in this && !(params.reload || this.alwaysReload[key]))
throw Error(); throw Error();
let load = function () loadData(key, params.store, params.type || myObject); let load = function () loadData(key, params.store, params.type || myObject);
keys[key] = new constructor(key, params.store, load, params);
keys[key].timer = new Timer(1000, 10000, function () storage.save(key)); this.keys[key] = new constructor(key, params.store, load, params);
this.__defineGetter__(key, function () keys[key]); this.keys[key].timer = new Timer(1000, 10000, function () storage.save(key));
this.__defineGetter__(key, function () this.keys[key]);
} }
return keys[key]; return this.keys[key];
}, },
newMap: function newMap(key, options) { newMap: function newMap(key, options) {
@@ -182,38 +196,36 @@ const Storage = Module("Storage", {
callbackRef = { get: function () callback }; callbackRef = { get: function () callback };
} }
this.removeDeadObservers(); this.removeDeadObservers();
if (!(key in observers)) if (!(key in this.observers))
observers[key] = []; this.observers[key] = [];
if (!observers[key].some(function (o) o.callback.get() == callback)) if (!this.observers[key].some(function (o) o.callback.get() == callback))
observers[key].push({ ref: ref && Cu.getWeakReference(ref), callback: callbackRef }); this.observers[key].push({ ref: ref && Cu.getWeakReference(ref), callback: callbackRef });
}, },
removeObserver: function (key, callback) { removeObserver: function (key, callback) {
this.removeDeadObservers(); this.removeDeadObservers();
if (!(key in observers)) if (!(key in this.observers))
return; return;
observers[key] = observers[key].filter(function (elem) elem.callback.get() != callback); this.observers[key] = this.observers[key].filter(function (elem) elem.callback.get() != callback);
if (observers[key].length == 0) if (this.observers[key].length == 0)
delete obsevers[key]; delete obsevers[key];
}, },
removeDeadObservers: function () { removeDeadObservers: function () {
for (let [key, ary] in Iterator(observers)) { for (let [key, ary] in Iterator(this.observers)) {
observers[key] = ary = ary.filter(function (o) o.callback.get() && (!o.ref || o.ref.get() && o.ref.get().dactylStorageRefs)); this.observers[key] = ary = ary.filter(function (o) o.callback.get() && (!o.ref || o.ref.get() && o.ref.get().dactylStorageRefs));
if (!ary.length) if (!ary.length)
delete observers[key]; delete this.observers[key];
} }
}, },
get observers() observers,
fireEvent: function fireEvent(key, event, arg) { fireEvent: function fireEvent(key, event, arg) {
this.removeDeadObservers(); this.removeDeadObservers();
if (key in observers) if (key in this.observers)
// Safe, since we have our own Array object here. // Safe, since we have our own Array object here.
for each (let observer in observers[key]) for each (let observer in this.observers[key])
observer.callback.get()(key, event, arg); observer.callback.get()(key, event, arg);
if (key in keys) if (key in this.keys)
this[key].timer.tell(); this[key].timer.tell();
}, },
@@ -223,11 +235,11 @@ const Storage = Module("Storage", {
}, },
save: function save(key) { save: function save(key) {
saveData(keys[key]); saveData(this.keys[key]);
}, },
saveAll: function storeAll() { saveAll: function storeAll() {
for each (let obj in keys) for each (let obj in this.keys)
saveData(obj); saveData(obj);
}, },
@@ -237,7 +249,7 @@ const Storage = Module("Storage", {
if (val && !this._privateMode) if (val && !this._privateMode)
this.saveAll(); this.saveAll();
if (!val && this._privateMode) if (!val && this._privateMode)
for (let key in keys) for (let key in this.keys)
this.load(key); this.load(key);
return this._privateMode = Boolean(val); return this._privateMode = Boolean(val);
} }

View File

@@ -57,8 +57,8 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
cleanup: function cleanup() { cleanup: function cleanup() {
for (let win in iter(services.windowMediator.getEnumerator(null))) { for (let win in iter(services.windowMediator.getEnumerator(null))) {
for (let elem in values(win.document.dactylOverlayElements || [])) for (let elem in values(win.document.dactylOverlayElements || []))
if (elem.get() && elem.get().parentNode) if (elem.parentNode)
elem.get().parentNode.removeChild(elem.get()); elem.parentNode.removeChild(elem);
delete win.document.dactylOverlayElements; delete win.document.dactylOverlayElements;
delete win.document.dactylOverlays; delete win.document.dactylOverlays;
} }
@@ -849,7 +849,7 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
let tag = "<" + [namespaced(elem)].concat( let tag = "<" + [namespaced(elem)].concat(
[namespaced(a) + "=" + template.highlight(a.value, true) [namespaced(a) + "=" + template.highlight(a.value, true)
for ([i, a] in array.iterItems(elem.attributes))]).join(" "); for ([i, a] in array.iterItems(elem.attributes))]).join(" ");
return tag + (hasChildren ? "/>" : ">...</" + namespaced(elem) + ">"); return tag + (!hasChildren ? "/>" : ">...</" + namespaced(elem) + ">");
} }
catch (e) { catch (e) {
return {}.toString.call(elem); return {}.toString.call(elem);
@@ -907,62 +907,6 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
return color ? string : [s for each (s in string)].join(""); return color ? string : [s for each (s in string)].join("");
}, },
_loadOverlays: function _loadOverlays(window) {
if (!window.dactylOverlays)
window.dactylOverlays = [];
util.dump("load overlays", window.document.documentURI);
for each (let obj in util.overlays[window.document.documentURI] || []) {
if (window.dactylOverlays.indexOf(obj) >= 0)
continue;
window.dactylOverlays.push(obj);
this._loadOverlay(window, obj(window));
}
},
_loadOverlay: function _loadOverlay(window, obj) {
let doc = window.document;
if (!doc.dactylOverlayElements)
doc.dactylOverlayElements = [];
function overlay(key, fn) {
if (obj[key]) {
let iterator = Iterator(obj[key]);
if (!isObject(obj[key]))
iterator = ([elem.@id, elem.*, elem.@*::*] for each (elem in obj[key]));
for (let [elem, xml, attr] in iterator) {
if (elem = doc.getElementById(elem)) {
let node = util.xmlToDom(xml, doc, obj.objects);
for (let n in array.iterValues(node.childNodes))
doc.dactylOverlayElements.push(Cu.getWeakReference(n));
fn(elem, node);
for each (let attr in attr || []) // FIXME: Cleanup...
elem.setAttributeNS(attr.namespace(), attr.localName(), attr);
}
}
}
}
overlay("before", function (elem, dom) elem.parentNode.insertBefore(dom, elem));
overlay("after", function (elem, dom) elem.parentNode.insertBefore(dom, elem.nextSibling));
overlay("append", function (elem, dom) elem.appendChild(dom));
overlay("prepend", function (elem, dom) elem.insertBefore(dom, elem.firstChild));
if (obj.init)
obj.init(window);
if (obj.load)
if (doc.readyState === "complete")
obj.load(window);
else
doc.addEventListener("load", wrapCallback(function load(event) {
if (event.originalTarget === event.target) {
doc.removeEventListener("load", load.wrapper, true);
obj.load(window, event);
}
}), true);
},
observe: { observe: {
"dactyl-cleanup": function () { "dactyl-cleanup": function () {
util.dump("dactyl: util: observe: dactyl-cleanup"); util.dump("dactyl: util: observe: dactyl-cleanup");
@@ -1004,6 +948,64 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
} }
}, },
_loadOverlays: function _loadOverlays(window) {
if (!window.dactylOverlays)
window.dactylOverlays = [];
util.dump("load overlays", window.document.documentURI);
for each (let obj in util.overlays[window.document.documentURI] || []) {
if (window.dactylOverlays.indexOf(obj) >= 0)
continue;
window.dactylOverlays.push(obj);
this._loadOverlay(window, obj(window));
}
},
_loadOverlay: function _loadOverlay(window, obj) {
let doc = window.document;
if (!doc.dactylOverlayElements)
doc.dactylOverlayElements = [];
util.dump("load overlay", doc.documentURI, String(obj).substr(0, 60));
function overlay(key, fn) {
if (obj[key]) {
let iterator = Iterator(obj[key]);
if (!isObject(obj[key]))
iterator = ([elem.@id, elem.elements(), elem.@*::*.(function::name() != "id")] for each (elem in obj[key]));
for (let [elem, xml, attr] in iterator) {
if (elem = doc.getElementById(elem)) {
let node = util.xmlToDom(xml, doc, obj.objects);
for (let n in array.iterValues(node.childNodes))
doc.dactylOverlayElements.push(n);
fn(elem, node);
for each (let attr in attr || []) // FIXME: Cleanup...
elem.setAttributeNS(attr.namespace(), attr.localName(), attr);
}
}
}
}
overlay("before", function (elem, dom) elem.parentNode.insertBefore(dom, elem));
overlay("after", function (elem, dom) elem.parentNode.insertBefore(dom, elem.nextSibling));
overlay("append", function (elem, dom) elem.appendChild(dom));
overlay("prepend", function (elem, dom) elem.insertBefore(dom, elem.firstChild));
if (obj.init)
obj.init(window);
if (obj.load)
if (doc.readyState === "complete")
obj.load(window);
else
doc.addEventListener("load", wrapCallback(function load(event) {
if (event.originalTarget === event.target) {
doc.removeEventListener("load", load.wrapper, true);
obj.load(window, event);
}
}), true);
},
overlayWindow: function (url, fn) { overlayWindow: function (url, fn) {
if (url instanceof Ci.nsIDOMWindow) if (url instanceof Ci.nsIDOMWindow)
util._loadOverlay(url, fn); util._loadOverlay(url, fn);

View File

@@ -122,7 +122,7 @@ statusbarpanel {
background: transparent; background: transparent;
} }
#dactyl-statusline-field-url { .dactyl-status-field-url {
background-color: inherit !important; background-color: inherit !important;
color: inherit !important; color: inherit !important;
} }