This section describes how to save the state of a XUL window.
When building a large application, you will typically want to be able to save some of the state of a window across sessions. For example, the window should remember whether which toolbars are collapsed even after the user exits.
One possibility would be to write a script to collect information about what you would like to save and then save it to a file. However, that would be a pain to do for every application. Convieniently, XUL provides such a mechanism to save the state of a window.
The information is collected and stored in a RDF file in the same directory as other user preferences. It holds state information about each window. This method has the advantage that it works with Mozilla user profiles, so that each user can have different settings.
XUL allows you to save the state of any element. You will typically want to save toolbar states, window positions and whether certain panels are displayed or not.
To allow the saving of state, you simply add a persist attribute to the element which holds a value you want to save. The persist attribute should be set to a space-separated list of attributes of the element that you want to save.
For example, to save the position of a window, you would do the following:
<window width="200" height="300" persist="width height" . . . |
The two attributes of the window element, the width and the height will be saved. You could add additional attributes by adding a space and another attribute to the persist.
The state of a toolbar is determined by the collapsed attribute on the toolbar element. So, to save the state, you could do the following:
<toolbar persist="collapsed"> |
You can add the persist attribute to any element and store any attribute. You might use unusual values if you adjust attributes using a script.
Let's add the persist attribute to some of the elements in the find files dialog. Let's say we save the position of the window, the size of the window and the toolbar state. To do this, we need to modify the window and the toolbar.
<window id="findfile-window" title="Find Files" persist="screenX screenY width height" xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> . . . <menubar persist="collapsed"> . . . <toolbar persist="collapsed"> |
This will cause the x and y position of the window, the width and height of the window and the collapsed state of the menubar and toolbar to be saved. We could extend it further to save the collapsed state of the splitter. It doesn't really make sense to save the current tab state.
(Next) Next, we'll see how to add some event handlers to XUL elements.