One of the best ways to learn about the gadget API is to modify an existing gadget. This tutorial leads you through modifying the Hello World gadget included in the SDK. You'll change the gadget's XML and JavaScript code, making it do a bit of animation. When the code is complete, you'll package the gadget so others can run it. After finishing this tutorial, you should be ready to start exploring the gadget API.
Note: In general, you should use the Gadget Designer when creating a gadget. You won't need the Gadget Designer for this tutorial because you'll just be tweaking the behavior of an existing gadget.
This tutorial uses the Hello World sample that's included in the Google Desktop SDK. To run the sample, you must have Google Desktop installed.
Do this:
Do this:
api/samples/gadgets/HelloWorld
folder and name it MyHelloWorld
.MyHelloWorld
in a convenient place,
such as your desktop.
Before modifying the files under MyHelloWorld
,
you should know something about them.
File Name | Type | Description |
---|---|---|
en/strings.xml |
XML file | Defines strings used by
the default, English version of this gadget.
To define the "Hello World!" string
that this gadget displays,
en/strings.xml uses the following code:
<HELLO_WORLD>Hello World!</HELLO_WORLD> .
|
background.png |
image | The background of the gadget. Using a PNG image lets this gadget be transparent — whatever's underneath the gadget onscreen is visible through the gadget's corners and shadow. |
gadget.gmanifest |
Google Desktop manifest file | Specifies the gadget's metadata, such as its ID, version, minimum Google Desktop version, name, and description. |
icon_large.gif |
image | A 32x32-pixel icon associated with the gadget. |
icon_small.png |
image | A 24x24 version of the gadget's icon, with transparency. |
main.js |
JavaScript source file | Implements a click-handling function. |
main.xml |
XML file | Defines the gadget's appearance and specifies which function handles clicks. |
The next step is to run your copy of Hello World, both to make sure it works and to see what it does.
Do this:
MyHelloWorld
folder.
gadget.gmanifest
file.
You should see an installation dialog.
MyHelloWorld
gadget:
api/samples/gadgets
folder,
open a sample gadget's subfolder, such as MenuItems
.gadget.gmanifest
file.MyHelloWorld
has all the files shown in the table.Now that you've seen what Hello World does, it's time to make it a little more interesting. In the next couple of steps, you'll change the code so that the text spins around once when the user clicks it.
Here's the old code in the main.xml
file:
<!-- OLD code -->
<view width="250" height="150">
<script src="main.js" />
<img src="background.png" />
<label x="0" y="60" align="center" width="250" size="15" enabled="true"
onclick="onTextClick();">&HELLO_WORLD;</label>
</view>
The preceding XML code defines the gadget's display:
a view containing a background image and a label that displays
"Hello World!".
The code also specifies that the gadget has scripting code
in the file main.js
,
and that the onTextClick
function
(defined in main.js
)
should handle user clicks on the label.
To spin the text,
you'll need to add a bit of code to main.xml
.
The first thing to add is a name (mylabel
) for the label,
so that the JavaScript code
can change the label's properties.
A few other changes are needed
to make the spinning look good —
specifying the label's position differently
and defining the point of rotation
with pinX
and pinY
.
Do this:
MyHelloWorld/main.xml
in a text editor
that can handle UNIX-style line endings.
(WordPad works; NotePad doesn't.)
<!-- NEW code --> <view width="250" height="150"> <script src="main.js" /> <img src="background.png" /> <label x="125" y="70" align="center" width="250" size="15" enabled="true" name="mylabel" pinX="125" pinY="8" onclick="onTextClick();">&HELLO_WORLD;</label> </view>
Here's the old code in the main.js
file:
// OLD code
function onTextClick() {
view.alert(HELLO_WORLD);
}
This code defines an event handler that displays a dialog. The event handler is called whenever the user clicks the text in the gadget.
Do this:
MyHelloWorld/main.js
in a text editor.// NEW code function onTextClick() { view.beginAnimation(doRotation, 0, 360, 500); } function doRotation() { mylabel.rotation = event.value; }
This code makes the text spin clockwise exactly once,
taking half a second (500 milliseconds) to do so.
It does this by setting the angle (rotation
)
of the label multiple times,
starting with an angle of 0 degrees
and increasing to a final angle of 360 degrees.
The beginAnimation
method
kicks off the animation
by setting up a timer that
repeatedly does two things:
sets event.value
and then
calls doRotation
.
The doRotation
function
changes the text's angle
by setting the label's rotation
property
to event.value
.
Now that you've edited the main.js
and main.xml
files,
you can run your gadget!
Do this:
gadget.gmanifest
file.The "Hello World!" text will spin completely around once, as caught midway through in this figure:
Now that your gadget is working, you need to update its metadata. The most important string to change is your gadget's ID. For both your gadget and the original gadget to run at the same time, each one needs to have a unique ID.
Do this:
MyHelloWorld/gadget.gmanifest
in a text editor.uuidgen
,
Guidgen.exe
, or Uuidgen.exe
.
If you don't have any of those tools,
search the Web for
[uuid generator]
.
<id>
value in gadget.gmanifest
to be the unique identifier you just generated.<author>
value in
gadget.gmanifest
.MyHelloWorld/en/strings.xml
in your text editor.strings.xml
with the gadget's
new name,
description, copyright, website, and about information.
So far,
you've been running your gadget
by double-clicking its gadget.gmanifest
file.
That's fine while you're debugging,
but a finished gadget needs to be packaged into a single file
for distribution.
In this step, you'll create a .gg
package —
a ZIP archive
of the files that define your gadget's appearance and behavior.
You can then double-click the .gg
file
to run the gadget.
Do this:
MyHelloWorld
folder.
For example, remove automatically created backup
and Thumbs.db
files.
MyHelloWorld
folder.
On Windows, you can do this by selecting
everything in the MyHelloWorld
folder,
right-clicking a file,
and choosing Send To > Compressed (zipped) Folder.
Note:
The structure of the ZIP file should match
the MyHelloWorld
folder's structure.
For example, gadget.gmanifest
and main.xml
should be at the top level of the ZIP file,
not in a folder.
MyHelloWorld.gg
and put it in a convenient place,
outside of the MyHelloWorld
folder..gg
file.
If your gadget starts running, you're done!If you want to keep on playing with Hello World, here are some ideas:
background.png
.<img>
element from main.xml
.
The gadget will still appear,
but clicking on the text won't usually work.
(A label must be on top of a colored div or an image
for the label to detect clicks on its background.)label
reference documentation,
and then modify MyHelloWorld
's label.
For example:
view
reference documentation,
and then change the resize behavior of
MyHelloWorld
's view.Here are some ideas for when you're ready to move on from Hello World: