Toolkit Preferences System

Introduction

This document describes the Aviary Toolkit's new <preferences> system. Using this system it is possible to create preferences windows that display and operate appropriately on various platforms: (Windows, MacOS X and GNOME)

Use

Code for a typical preferences window may look something like this:

        <prefwindow id="appPreferences"
                       xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
          <prefpane id="pane1" label="&pane1.title;">
            <preferences>
              <preference id="pref1" name="pref.name" type="bool"/>
            </preferences>
            
            .. UI elements that refer to the preferences above
          </prefpane>
          <prefpane id="pane2" label="&pane2.title;" src="chrome://url/to/pane.xul"/>
        </prefwindow>
      

Pane content can be specified inline or an external chrome URI supplied for pane content to be loaded in via a dynamic overlay. You should be careful to read the HIGs for the platforms you are targeting and use the XUL preprocessor if necessary to set different window titles as appropriate. You should also be careful to specify the width of the window (in em) as appropriate using the preprocessor for each targeted platform, as well as the height (in em) for platforms where the window size does not change as the selected panel is changed (e.g. Windows).

Reference

<preferences>

<preferences> is a container for <preference> elements.

Interface Methods/Properties

See preferences.xml for a full listing

<preference>

<preference> is a representation of a preference.

XUL Syntax

See Firefox usage for examples.

Interface Methods/Properties

See preferences.xml for a full listing

void setElementValue(in DOMElement element); - initializes the supplied element from the value stored in the preference. This function calls onpreferenceread

type getElementValue(in DOMElement element); - retrieves the value that should be written to preferences based on the current state of the supplied element. This function calls onpreferencewrite

void updateElements(); - update all elements that observe this preference.

onpreferenceread/onpreferencewrite

Often you will have UI whose construction does not map easily to a given preference type. For example, you may have a checkbox that is checked when an integer preference value is 3 and not when it's 2. To initialize this UI element you can't rely on the default initialization routine, since both values are meaningless to the checkbox element. You need to write translation functions to translate the preference value into an initialization value for the UI element, and to translate the UI element's value into something to write to the preferences file. This is what onpreferenceread/ onpreferencewrite are for.

onpreferenceread is called when an element is initialized from preferences. If you supply an implementation of this event, your implementation will be invoked during initialization and you can return the value with which to initialize the UI element with, or undefined to tell the preferences system to initialize the UI element with the default value (i.e. to attempt to initialize with the preference value). In the above example, you might write the checkbox like this:

        <checkbox preference="foo.bar" onpreferenceread="return onPreferenceRead();"/>
        
        .. script:
        
        function onPreferenceRead()
        {
          var preference = document.getElementById("foo.bar");
          return preference.value == 3;
          
          // If foo.bar was boolean and we wanted to use its value to initialize 
          // the checkbox, we could still implement this method if we wanted to
          // perform any other initialization actions at this time. 
        }
       

onpreferencewrite is called when preferences are being written - the preferences system asks each element to translate its current state into a value suitable for writing to the specified preference. You can return a special value or undefined to tell the preferences system to use its standard means for obtaining the value. In the above example:

        <checkbox preference="foo.bar" onpreferencewrite="return onPreferenceWrite();"/>
        
        .. script:
        
        function onPreferenceWrite()
        {
          var checkbox = document.getElementById("checkbox");
          return checkbox.checked ? 3 : 2;
        }
        
        // If foo.bar was boolean and we wanted to use its value to write to
        // preferences, we could still implement this method if we wanted to 
        // perform any other initialization actions at this time. 
      

onchange

When a preference value changes, an onchange/change event is fired on the <preference> element. You can handle this if you wish to.

<prefwindow>

<prefwindow> is the documentElement of preferences windows.

XUL Syntax

See Firefox usage for examples.

type="child" - a sub dialog of a top level preferences window, usually modal. In instant-apply conditions (MacOS X, GNOME), when this sub dialog is confirmed, its preferences are written. In non-instant-apply conditions (Windows) when this sub dialog is confirmed, changes made in it are saved in the calling window's <preferences> set and are saved when the calling window is confirmed.

Interface Methods/Properties

See preferences.xml for a full listing

void addPane(in DOMElement pane); - adds a preferences pane to the preferences window. You must supply the DOMElement for the >prefpane> you are adding.

DOMWindow openSubDialog(in string url, in string features, in object params); - opens a modal sub dialog as a child of the preferences window.

DOMWindow openWindow(in string windowType, in string url, in string features, in object params); - opens a non-modal sub window as a child of the preferences window. You can supply a window type so that any existing open windows of this type will be focused instead of opening a new one.

<prefpane>

<prefpane> is the representation of a page of preferences.

XUL Syntax

See Firefox usage for examples

This element may contain all of its content inline, or it may refer to an external overlay chrome URI which will be loaded when the pane is selected using the src="chrome://uri/to/overlay.xul" attribute.

Interface Methods/Properties

See preferences.xml for a full listing

void writePreferences(in boolean flushToDisk); - writes all changes in this pane to preferences, optionally flushes to disk.

DOMElement preferenceForElement(in DOMElement element); - obtains the <preference> element that the supplied element observes.

Events

onpaneload/paneload is fired on the pane element when the pane is loaded (e.g. when the overlay is merged.)