From 46e0e2c5b67456a05f7098f4a24af230abfc5c29 Mon Sep 17 00:00:00 2001 From: foudfou Date: Sun, 14 Aug 2011 15:38:20 +0200 Subject: [PATCH] * minimal platform check (work in progress) * best practice try: remove event listeners * minor refactoring --- TODO | 4 ++ src/chrome/content/overlay.js | 18 +++++-- src/install.rdf | 1 + src/modules/MoztHandler.jsm | 96 +++++++++++++++++++++-------------- 4 files changed, 75 insertions(+), 44 deletions(-) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 0000000..e3b1431 --- /dev/null +++ b/TODO @@ -0,0 +1,4 @@ +* BUG: windows aren't restored at the same position, or with the same z-order +https://developer.mozilla.org/en/nsIWindowMediator#getZOrderXULWindowEnumerator%28%29 + +* make multi-platform. At least have js-ctypes library call dependant on OS detection. (best would be to have the OS-dependant modules loaded at startup) diff --git a/src/chrome/content/overlay.js b/src/chrome/content/overlay.js index 4ba5aaf..d762366 100644 --- a/src/chrome/content/overlay.js +++ b/src/chrome/content/overlay.js @@ -20,9 +20,9 @@ mozt.Main = { } if (!mozt.Handler.initialized) - mozt.Handler.init(); + var initOK = mozt.Handler.init(); - mozt.Debug.debug('Moztray LOADED !'); + mozt.Debug.debug('Moztray LOADED: ' + initOK); return true; }, @@ -52,6 +52,14 @@ mozt.Main = { // should be sufficient for a delayed Startup (no need for window.setTimeout()) // https://developer.mozilla.org/en/Extensions/Performance_best_practices_in_extensions // https://developer.mozilla.org/en/XUL_School/JavaScript_Object_Management.html -window.addEventListener("load", function (e) { mozt.Main.onLoad(); }, false); -window.addEventListener("unload", function(e) { mozt.Main.onQuit(); }, false); -// TODO: put all mozt into a module (jsm) so it's loaded only once +// https://developer.mozilla.org/en/Extensions/Performance_best_practices_in_extensions#Removing_Event_Listeners +window.addEventListener( + 'load', function (e) { + removeEventListener('load', arguments.callee, true); + mozt.Main.onLoad(); }, + false); +window.addEventListener( + 'unload', function (e) { + removeEventListener('unload', arguments.callee, true); + mozt.Main.onQuit(); }, + false); diff --git a/src/install.rdf b/src/install.rdf index d026bba..ca6d5e0 100644 --- a/src/install.rdf +++ b/src/install.rdf @@ -12,6 +12,7 @@ A system tray extension for linux. chrome://moztray/skin/icon32.png + Linux diff --git a/src/modules/MoztHandler.jsm b/src/modules/MoztHandler.jsm index 3a54477..8393fd1 100644 --- a/src/modules/MoztHandler.jsm +++ b/src/modules/MoztHandler.jsm @@ -35,76 +35,83 @@ var mozt_activateCb; // pointer to JS function. should not be eaten // (https://developer.mozilla.org/en/XUL_School/JavaScript_Object_Management) mozt.Handler = { initialized: false, - _windowsHidden: false, + _handledDOMWindows: [], - _getBaseWindow: function(win) { - var bw; - try { // thx Neil Deakin !! - bw = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor) - .getInterface(Components.interfaces.nsIWebNavigation) - .QueryInterface(Components.interfaces.nsIDocShellTreeItem) + _getBaseOrXULWindowFromDOMWindow: function(win, winType) { + let winInterface, winOut; + try { // thx Neil Deakin !! + winInterface = win.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation) + .QueryInterface(Ci.nsIDocShellTreeItem) .treeOwner - .QueryInterface(Components.interfaces.nsIInterfaceRequestor) - .getInterface(Components.interfaces.nsIBaseWindow); + .QueryInterface(Ci.nsIInterfaceRequestor); } catch (ex) { - bw = null; - setTimeout(function() {throw ex; }, 0); // ignore no-interface exception + mozt.Debug.debug(ex); + Components.utils.reportError(ex); + return null; } - return bw; - }, - _getAllWindows: function() { - mozt.Debug.debug("_getAllWindows"); - var baseWindows = new Array(); - var e = Services.wm.getEnumerator(null); - while (e.hasMoreElements()) { - var w = e.getNext(); - baseWindows[baseWindows.length] = this._getBaseWindow(w); + if (winType == "BaseWindow") + winOut = winInterface.getInterface(Ci.nsIBaseWindow); + else if (winType == "XUL") + winOut = winInterface.getInterface(Ci.nsIXULWindow); + else { + Components.utils.reportError("MOZTRAY: unknown winType '" + winType + "'"); + return null; } - return baseWindows; + + return winOut; }, /* - * might need to remember position... - * var outX = {}, outY = {}, outCX = {}, outCY = {}; - * bw.getPositionAndSize(outX, outY, outCX, outCY); - * mozt.Debug.debug("pos: " - * + outX.value + ", " - * + outY.value + ", " - * + outCX.value + ", " - * + outCY.value - * ); + * DAMN IT ! getZOrderDOMWindowEnumerator doesn't work on Linux :-( + * https://bugzilla.mozilla.org/show_bug.cgi?id=156333, and all windows + * seem to have the same zlevel ("normalZ") which is different from the + * z-order. There seems to be no means to get/set the z-order at this + * time... */ + _updateHandledDOMWindows: function() { + mozt.Debug.debug("_updateHandledDOMWindows"); + this._handledDOMWindows = []; + var windowsEnumerator = Services.wm.getEnumerator(null); // returns a nsIDOMWindow + while (windowsEnumerator.hasMoreElements()) { + this._handledDOMWindows[this._handledDOMWindows.length] = + windowsEnumerator.getNext(); + } + }, + showHideToTray: function(a1, a2, a3) { mozt.Debug.debug("showHideToTray"); - var baseWindows; try { - baseWindows = this._getAllWindows(); + this._updateHandledDOMWindows(); } catch (x) { mozt.Debug.debug(x); } - mozt.Debug.debug("baseWindows: " + baseWindows.length); - for(var i=0; i=0 will be checked ing install, so we shouldn't need to care + mozt.Debug.debug("OS=" + runtimeOS + ", XULrunner=" + xulVer); + if (runtimeOS != "Linux") { + Components.utils.reportError("MOZTRAY: only Linux platform supported at this time. Moztray not loaded"); + return; + // Cu.import("resource://moztray/MoztHandler-Linux.jsm"); + } + try { // instanciate tray icon