My favorites | English | Sign in

Build, Integrate & Sell your App - Google Apps Marketplace!

Google Desktop APIs (Labs)

Using Parameters in Desktop Gadget Programming

This article was written and submitted by an external developer. The Google Desktop Team thanks Teodor Filimon for his time and expertise.


Teodor Filimon, Google Desktop Developer
August 2008
"The early bird catches the worm."

Introduction

Computer programs often change over time, so we have to make sure that they can accept changes without hurting their built-in structures. It pays to plan ahead and apply a few tricks to save you time and headache. :-)

Parameter files

The first piece of advice is to isolate parameters, making them easy to find and change. One good technique is to place them in a separate .js file. Among the script files you already use you could have a parameters.js file for that specific task.


Parameters script file
Figure 1: Example of a parameters file

Being a script file, it can be included in main.xml just like the main.js file:

<script src="parameters.js" />

The next section shows you how to actually create and use the parameters, as well as store them across sessions.

Encapsulating parameters in an object

What we do next is define an object that can help us handle parameters. Let's call it ParametersObject. Our desired parameters are the properties of the class. We then instantiate this class into an object called parameters. The example below is what I used in the Web TV and Radio gadget. Not all gadgets require so many parameters, but it just goes to show you the wide range of needs parameters can take care of.

parameters=new ParametersObject();

function ParametersObject(){ // An object that encapsulates some parameters.

  // Default size
  this.defaultGadgetWidth=345;
  this.defaultGadgetHeight=254;

  // An empty space in the lateral parts of the gadget which makes it look good relative to other gadgets
  this.lateralEmptySpace=3;

  // Minimum size
  this.minWidth=256+2*this.lateralEmptySpace;
  this.minHeight=190;

  this.defaultVolume=25; //in percents

  //measured in milliseconds, the parameter below says how much to wait on mouse over before displaying the controls
  //(the mouse could be just passing over the gadget)
  this.safeInterfaceDisplayTime=465;

  this.timeToWaitBeforeCheckingForUpdates=3300000; //55 minutes*60 seconds*1000 milliseconds
  //based on ergonomics statistics of how long people will or should stay in front of the computer, could change

  //useful for skinning in the future:
  this.maxOpacity=255;
  this.minVisibleOpacity=160;

  //opacity step (higher means less CPU is used) - must be a divisor of 'maxOpacity'
  this.opacityStep=17;
  //the parameter above is strongly connected with the next one (a simple delay in milliseconds)
  this.CPUstep=40;

  this.channelUpdateInterval=255; //in minutes (of gadget usage, not computer usage)

  //URLs for files with update info, stats, etc.
  this.WebTvVersionUrl="http://teodorfilimon.com/files/Web-TV-Radio/WebTvRadioVersion.htm";
  this.WebTvWhatsnew="http://teodorfilimon.com/files/Web-TV-Radio/WebTvRadioWhatsnew.htm";
  this.WebTvCentral="http://teodorfilimon.com/files/Web-TV-Radio/WebTvRadioCentral.php";
  this.WebTvWebPage="http://teodorfilimon.com/index_files/Web-TV-Radio-gadget.html";

  //highlight UI elements when user enters the 'All Channels' dialog
  this.highlightAllChannelsWindow=true;

  //...
}

Creating the object this way allows us to create multiple configuration presets that overwrite the active set of parameters. Also notice the comments for each parameter, which are especially important for open source projects.

Usage

Next let's look at a snippet of code from the same gadget that shows you how exactly to reference a parameter. You just have to access the property within the parameters object like this: parameters.defaultVolume.

function _onOpen(){

    //setting default options
    options.putDefaultValue("width",parameters.defaultGadgetWidth);
    options.putDefaultValue("height",parameters.defaultGadgetHeight);

    //...

    //resizing to last known size
    view.resizeTo(options.getValue("width"),options.getValue("height"));
}

You can see in this example how some default options are set based on the parameters. With each resize the "width" and "height" entries in options are changed.

Conclusion

As you have seen, parameters can help you in the short term when testing and debugging, or in the long run when maintaining and expanding a gadget.

Resources

Author Bio

Teodor Filimon

I'm a natural born programmer. My first contact with a techno-gadget was Star Trek (remember those cool sensors?). I used to fill up a whole room with drawings of them when I was only 3 years old. Generally, I find a lot of inspiration for intuitive interfaces in things with "star" in their names (like Star Wars, Stargate,... :-) . I like the border between interface and functionality the essence of a program, I think. Anyway, I'm a software engineering student now, and you can learn more about me and what I'm thinking and doing at my website or blog. My best gadgets are Web TV and Radio and DigiWatch.


Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.