Accessing the Right Click Menu in Flex

I'm unwinding after the Max conference. Instead of trying to jump right back into the swing of things, I thought I'd answer a few questions that people have asked me. This one has, sadly, been sitting in my inbox for about a year. Often when I don't have an immediate answer, these things back up until I get a chance to research it.

How can I keep track of the different builds of a Flash file. I usually have different servers that I update the .swf on but sometimes I am not sure which version is active and which version shows in my browser. I would like to right click and have a menu showing metadata or find out some way to include metadata in the file and an easy way to view it.

The easiest way to do this is to modify the context menu in your Flex application. You can check out the livedocs [with examples] for ContextMenu, and ContextMenuItem. It is relatively easy to set up:

var menuItem:ContextMenuItem = new ContextMenuItem("Version 1");
var customContextMenu:ContextMenu = new ContextMenu();
customContextMenu.customItems.push(menuItem);
<i>[something]</i>.contextMenu = customContextMenu;

Now your context menu will show up on [something] when you right click on that [something]. I had a bit of a stumbling block, though. I wanted the menu to show up anywhere I clicked in that application. If I set [something] to the Application tag; my custom context menu did not show up. After some experimentation I discovered that the right click would show up on a Panel, but not a Canvas. It would show up on a TextInput, but not a Container. I don't quite understand why. Context applies to controls, not containers. The reason it works with panel is because the panel is made up of other controls.

To get my context to show up, I needed to add a control. I decided to lean on an old HTML trick, spacer.gif. Add an image control with a transparent spacer.gif to your application. Set height and width to 100% and maintainAspectRatio to false. Attach the context menu to the image and you're good to go:

<mx:Image width="100%" height="100%" source="assets/spacer.gif" alpha="0" maintainAspectRatio="false" id="ImageTest" />

You can technically use any image as long as you set the alpha to 0 which means that the image will be completely transparent.

You can view the source here or test it here:

Looks like playing with the menu removes the "view source" link which is added when you export a release version. Oh, well.

To go back to the original question, you can easily populate that menu using dynamic data, or hard code it with each new build. I would suspect--although this is not my area of expertise--that you can set up an ANT script to modify the version number before compiling.

As always, thanks for reading. I love answering questions

Comments
Harry B. Garland's Gravatar I noticed that the right-click menu for one of my apps works fine on a PC/Flash 9 but not on a Mac OR Flash 10. I couldn't find any information regarding the limitations of the right-click, so I stopped using it completely so that I could distribute my software without worrying about what platforms it works with. If that image trick works, I might start using right-click again.

Here's a sample app if you're interested, where right-click works with a PC and Flash 9, but not a Mac, and not with Flash 10: http://hgarland.com/flex/MatePresentation/MatePres...
# Posted By Harry B. Garland | 12/2/08 5:34 PM
Jason The Saj's Gravatar Hi Jeffry,

The following line of code worked for me in implementing a global right-click. However, I did note that when I tried to publish with source code - I did not get the source code link in my right-click menu. Not sure if the problem is related.

Application.application.contextMenu = customContextMenu;

- Jason
# Posted By Jason The Saj | 12/4/08 3:23 PM
Jeffry Houser's Gravatar That sounds like a much cleaner approach.

In my experiments, when I tried to put the contextMenu on the container the context menu would only show up if I right clicked on items in that container; not for an empty container.

But, I don't have time right now to retest.
# Posted By Jeffry Houser | 12/4/08 4:06 PM
All Content Copyright 2005, 2006, 2007 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.8.