views:

341

answers:

2

If my own sidebar is left open when Firefox is closed, it is shown again on startup. I find this undesirable and would rather it remain hidden until opened manually. Is it possible to stop this happening?

I tried adding this bit of code to my extension's initialisation function in order to close the sidebar if it does appear:

toggleSidebar("mySidebar", false);

This doesn't seem too work too consistently - it seems to ignore the false parameter and just toggles the sidebar! When it does work correctly it has unwanted side effects - I need to open and close the sidebar once before it will show any content. Weird, but I assume part of Firefox's view as to the sidebar's visibility has got out of sync.

It seems others are having the same trouble on the MozillaZine forums.

A: 

toggleSidebar("mySidebar", false); doesn't work because the second param means "forceOpen" not an open/close flag.

As someone answered on Mozillazine this is because Firefox just preserves sidebar state across sessions. Are you sure you need to be different here?

Here's the code in Firefox that opens the sidebar on startup:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#999 (the lines may be off when you access this later - it's the link to the latest version of the source)

1000     let box = document.getElementById("sidebar-box");
1001     if (box.hasAttribute("sidebarcommand")) {

And here's where the sidebarcommand attribute gets persisted:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#1422

1373 function BrowserShutdown()
...
1426   if (!enumerator.hasMoreElements()) {
1427     document.persist("sidebar-box", "sidebarcommand");

I suggest trying this:
If current window is the last one and your sidebar is open,

  • either close the sidebar before BrowserShutdown (it's registered as an unload handler for the window)
  • trick the BrowserShutdown to persist the right attributes (corresponding to the "sidebar not opened" case)
  • or after BrowserShutdown overwrite the persisted values with the ones you need.

If you come up with the working solution, please share it with others, preferably on MDC: https://developer.mozilla.org/En/Code_snippets/Sidebar

Nickolay
Cool. I had thoughts about the forceOpen flag not being open/close, but that's confirmed it. Presume passing false is going to do me no harm.I feel I need to be different because my sidebar only makes sense when there's a valid URL in the URL bar. I could just display a generic sidebar in that case I guess.I'll give some of those ideas a go. Thanks.
Mat
+2  A: 

You could also add an observer to the shutdown process, and close the sidebar there. I had the same issue as you, and ended up going that route, since we already had an observer set up to do some other cleanup.

The code looks like this, during the initialization of the main overlay:

    var current = this;
    var quitObserver = {
   QueryInterface: function(aIID) {
      if (aIID.equals(Components.interfaces.nsIObserver) || aIID.equals(Components.interfaces.nsISupports)) {
         return this;
      }
      throw Components.results.NS_NOINTERFACE;
   },
   observe: function(aSubject,aTopic,aData ) {
      var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
      var sidebar  = mainWindow.document.getElementById("sidebar");
      if (sidebar.contentWindow.location.href == "chrome://daisy/content/sidebar.xul") {
         mainWindow.toggleSidebar('openDaisySidebar', false);
      }
   }
};

setTimeout(function() {
   var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
   var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
   observerService.addObserver(quitObserver,"quit-application-granted",false);
},2000);

Hope that helps.

jbrugge
Cunning! I had difficulty getting this to work (not least due to lack of debugging at shutdown!), but eventually found it worked for quit-application-requested. I found that although I could still get the window, it had no document by the time quit-application-granted fired.
Mat