Developing Widgets with Flash
by Tripp Bridges, Flash Platform Engineer
Yahoo! Widgets provided support for Flash with their 4.5 release. This opens up new possibilities allowing Flash developers to create cool desktop applications by easily wrapping them with the Widget Engine. The Flash Platform team has created tools and components that make this even simpler.
Navigation
- Overview
- How to Create Widgets with Flash Applications
- Using the Badge Kit
- ASTRA Components
- Next Steps
Download the Flickr Slideshow Widget.
Download the Aquarium Widget.
What You Need
- Badge Kit (deprecated)
- ASTRA Flash Components
Overview
In this article, we'll learn how to embed Flash applications in the Widget engine using the following tools:
- Badge Kit (deprecated) - an XML-based framework for rapid development of small interactive Flash applications.
- ASTRA Components - a collection of well-documented, skinnable components that can be used in Actionscript 3 applications.
We will use the Badge Kit to build a Slideshow Widget that uses the Flickr API to retrieve and display images. After that, we will use the ASTRA Components to create an Aquarium Widget.
![]() Flickr Widget | ![]()
Aquarium Widget |
How to Create Widgets with Flash Applications
We will begin by learning how to create a Yahoo! Widget from a Flash application. We will look at the files and directory structure of a Widget. Then, we will break down the files necessary to construct your Widget. When constructing a Widget, you will need to create the following directory structure within your Widget's folder.
Widget Directory Structure
At the root level of your directory, there is a folder named Contents. It contains a .kon file, a Widget.xml file and a Resources folder. At a minimum, your resources folder will contain your Flash file. If there are any additional assets, they will also go in the Resources folder. You may choose to create subdirectories for your convenience. (e.g. Resources -> Images -> My Images)
The .kon file
The .kon file is the main file of a Widget. Below is an example.
There are many top level nodes in the basic hierarchy of the .kon file. You will see the following sections:
action- defines when and how a Widget executes code that is triggered automatically rather than by a user.window- defines the main window of the Widget. It describes the size and position and contains all visual elements including our Flash node.preference- defines preference setting and associated attributes.about-box- allows us to define images for an about box.
For embedding Flash, we need the window section.
The extensive list of attributes and functions can be found in the Konfabulator Reference but we will examine the ones that are used in our examples. The size of a swf is determined by width and height parameters. The coordinates of a swf are defined by hOffset and vOffset attributes in conjunction with hAlign and vAlign attributes.
By default, the hOffset and vOffset represent the x and y coordinates respectively. If hAlign is right, then the hOffset would be used to right align the object and if vAlign is set to bottom then the vOffset would be used to bottom align the object. The positioning attributes below:
hAlign- defines whether the Flash object is left or right aligned. The default alignment is left.vAlign- defines whether the Flash object is top or bottom aligned. The default value is top.hOffset- defines the horizontal position of the Flash object dependent on thehAlignvalue.vOffset- defines the vertical position of the Flash object dependent on thevAlignvalue.
The path of the Flash file is placed in the src node. If the application shares data with other domains, the allowNetworking attribute must be included. Additionally, the Widget.xml file will need to contain a security node specifying the allowed domains.
The Widget.xml file
The Widget.xml file contains the metadata and security settings for a Widget project. If you want to include external data in your application, you must include this file with a security node. The security node contains the following elements:
filesystem- restricts file system accesshttp- restricts http requestscommand- restricts system commandshotkey- restricts global hotkey settingscom/Applescript- restricts com requests and Applescript execution
Below is a security node that allows for access to any domain:
Once an application is built, the Widget Converter is used to convert it to a Widget file. The Widget Converter can be downloaded from the Widget Downloads page. After you have downloaded and opened the Widget, you can then drag your folder to the Widget, set its menu to convert to flat file and click the "convert" button.
Using the Badge Kit: Flickr Slideshow
Now that we know how to set up our files to create Widgets, let's create one. In this exercise, we are going to use the Badge Kit to create a Flash application and integrate it as a Widget. The Badge Kit is an an XML-based framework for rapid development of small interactive Flash applications. You can learn how to use the Badge Kit very quickly, and most of the usage scenarios require no knowledge of Flash. If you need a primer on using the Badge Kit, go here.
Creating the Badge
In this example, we are using a flickr service to retrieve photos. Once the photos are retrieved, they are assigned to a Slideshow component that we have created. Below is the content.xml for this badge.
Our resources node includes the base_components.swf, the default resource swf for badges, and a slideshow_component.swf that contains our custom slideshow component. In our services node, we have our flickr service. Once our photos are retrieved with this service, our slides are assigned to our Slideshow component through its addSlides method. The principle child nodes in our elements node are a background image, a title label and our slideshow component. We have broken down the xml used to create the flickr badge. All that is left is to turn it into a Yahoo! Widget.
Creating the Widget
As discussed earlier, our flickr_slideshow directory contains a directory named Contents which contain the following:
- flickr_slideshow.kon file
- Widget.xml file
- Resources directory
Our resources directory contains 3 pngs and a Badge directory. The pngs are used as images for a background, a handle and an about screen. The Badge directory contains all the files from our flickr_slideshow badge.
Our application needs to load data from flickr so we have to set the necessary permissions in our Widget.xml file. The security node has a child node for each security setting. To allow for http requests, we will set the security to all in the http node.
The widget node is the root node of our flickr_slideShow.kon file. It contains a window node, an about-box node and multiple preference and action nodes. Our window node contains all of our visual elements.
imagenode with a name of titleImage. This node loads the image that is used for the draggable handle of our Widget.textnode with a name of titleText. This node loads the text field for the draggable handle of our Widget.flashnode with a name of flashView. This node loads our Flash movie. Notice how we have added the allowNetworking="all" to allow for our flickr communications.
Items placed in our preference nodes will be available to the Widget user for customization. We have preference nodes for userId, background and delay. Each node has the following child nodes:
titletells the konfabulator what to display to the usertypetells the konfabulator what user control to display to the user. (e.g. A type of text will generate a text field and a type of color will generate a color picker)defaultvaluewill set the default value for the item.
The scripts that tie the preferences to the elements are located in the action nodes. In this case, the action node is used to pass flashVars to the flash embed. Our first node, has a trigger attribute value of onLoad. This script will execute every time the Widget loads. See the node below.
If we look at the code above, we see that the values of the preferences nodes are used to customize the Widget. The value of the userId is used to build a flickrUrl and passed as the flashVar value of userId. The flickUrl variable is passed as the flashVar value of flickUrl. The value of the delay node is passed as the delay flashVar. The value of the background node is set as the background color of the Flash movie.
Our second action node is almost identical to the first one. There are two differences. The trigger attribute is set to onPreferencesChanged which means that this script will execute when the user changes the preferences of the Widget. Since the flashVars have changed, we reload the Widget for the changes to take effect (Theoretically, flashView.reload() would be preferred but due to a known issue, this command doesn't reload the flash file properly).
Download the Yahoo! Flickr Slideshow Widget here.
We have seen how the Konfabulator works, how to build a Flash desktop application with the Badge Kit and the Konfabulator. In our next section, we will explore other options for building a Flash desktop application with the Konfabulator.
ASTRA Components: Aquarium
The Aquarium Widget shows how you can leverage our ASTRA Flash Components to create applications that can then be embedded into a Widget.
Astra Components
ASTRA, the ActionScript Toolkit for Rich Applications, is a collection of Flash and Flex components, code libraries, toolkits and utilties developed by Yahoo! for ActionScript developers. The ASTRA Components use the existing component architecture and compliment the set provided in Adobe Flash CS3.- Tree - a UI component that offers a convenient representation of hierarchical information (file folders, product catalogs, nested categories, etc.).
- Menu - a UI component that displays a hierarchical menu structure that can be used to provide convenient access to various application functionality, sorted by categories.
- TabBar - a UI component used for switching between different application states. Implemented as a dynamically resizing row of Tab buttons, whose labels are controlled by a DataProvider.
- Autocomplete - a specialized text entry field that provides a list of suggestions from a supplied data set based on the characters entered by the user.
- Charts (BarChart, ColumnChart, LineChart, and PieChart) - a set of visualization components that show tabular data in various graphical representations.
- MenuBar - a UI component that renders hierarchical data as a row of buttons with nested menus that can be used to provide convenient access to various application functionality.
- AlertManager - a UI component that creates and manages the queuing of alert windows in a Flash Application.
- AudioPlayback - a UI component that creates a set of controls for audio playback.
The Aquarium
In this example, we will take our Aquarium application and create a Widget. The Aquarium is a fun application that uses the Tree, Menu, TabBar, AutoComplete and Charts components. As such it shows how to integrate these components in a real application and how to customize these components for special use.
Since we do not need to specify any security settings for this application, we do not need a Widget.xml file. Below is the kon file for the aquarium Widget.
We have a flash node, a single preference for background color and two action nodes that set the background color of the swf when it loads and when the user changes the preference setting.
Download the Yahoo! Aquarium Widget here.
Next Steps
In this article, we have explored two approaches to rapid development of Flash Applications, The Badge Kit and the ASTRA Flash Component set. We have also learned how to leverage the Yahoo! Widget engine to turn our Flash movie into a desktop application. This should provide you with all the tools you need to start creating Flash Widgets of your own. If you would like to get more in depth with any of the topics, check out the links below:
Additionally, there is always a wealth of knowledge to be found at our Flash Developer Center. So, if you have not done so already, download the Badge Kit, ASTRA Components and Konfabulator so you can start building your own Flash Widgets today.
If you have any additional questions, join the Yahoo! Flash Developer Group and be heard!