Home > Flex > Flex Tip of the Day: accelerator decorations on menus

Flex Tip of the Day: accelerator decorations on menus

April 23rd, 2007

If your Flex application has a menu bar and there are control-key or command-key accelerators for items in the menu bar, you’d like to label your menu bar items with the accelerators. So, for example, if your File / Save command has a Control-S accelerator, you’d like the menu item to read “Save” with a “Ctrl-S” label to the right.

Trouble is, there’s no way to specify decorations in the framework menu classes. So here’s a quick cookbook on how to do it yourself. The basic approach is to subclass MenuItemRenderer and add an additional right-aligned UITextField to display your accelerator decoration.

1. In your menu XML, add an @acceleratorKey attribute to each menu item that has one. So, for example, the entry for Save might say <menuitem label=”Save” acceleratorKey=”s”/>.

2. Subclass MenuItemRenderer, and override createChildren(), measure(), updateDisplayList(), and the data setter function. In the createChildren() override, create a UITextField and add it as a child. In measure(), adjust measuredWidth by the textWidth of the UITextField. In updateDisplayList(), set your UITextField’s x position, and in the data setter, get the accelerator decoration text out of the value object and set it into your UITextField.

3. Subclass MenuBar, and override getMenuAt(). First chain up to the superclass, then set its menuItemRenderer to a ClassFactory that takes your MenuItemRenderer subclass as its parameter.

If you are supporting both Mac and Windows, it’s probably a good idea to write some code in your MenuBar subclass that figures out whether you’re running on Mac or Windows and updates the decoration prefix to “Ctrl-” or “Cmd-” accordingly.

Technorati Tags: ,

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
Author: David Coletta Categories: Flex Tags:
  1. Wim
    April 23rd, 2007 at 06:21 | #1

    Can you please share the source code with us?

  2. david
    April 23rd, 2007 at 07:36 | #2

    Good suggestion. I’ll follow up with source in a later post.

  3. Wim
    April 23rd, 2007 at 07:57 | #3

    It would be cool if you could show us how to add a style to a menuitem at the same time… right now, only a background color can be specified. All other skins/styles are ignored. This makes the menubar very skinnable, but its items not.

  4. April 23rd, 2007 at 08:56 | #4

    I did something like this a while ago except I handeled it a bit diferently using the icon function to dynamically render an icon :) source included at:
    http://blog.xsive.co.nz/archives/152
    Styling is hard coded as this was a quick example but easy to implement with meta tags :)

  5. mike
    December 6th, 2007 at 10:38 | #5

    Hi david,

    Do you still plan on sharing the source code? I’d greatly appreciate it!

  6. David Coletta
    December 6th, 2007 at 10:57 | #6

    Sorry, my bad. I should have posted it when we were a standalone company. Now that we’re Adobe I have to go find out how to get permission…

  7. Steve
    March 14th, 2008 at 11:25 | #7

    Even if unable to post source, it’d be super helpful if you could expand or metacode this passage:

    2. Subclass MenuItemRenderer, and override createChildren(), measure(), updateDisplayList(), and the data setter function. In the createChildren() override, create a UITextField and add it as a child. In measure(), adjust measuredWidth by the textWidth of the UITextField. In updateDisplayList(), set your UITextField’s x position, and in the data setter, get the accelerator decoration text out of the value object and set it into your UITextField.

    very much appreciated

  8. March 17th, 2008 at 02:28 | #8

    Great suggestions… I put together some sample code that accomplishes this.

    http://www.rphelan.com/archives/7

    No support for mac yet (even though I develop on a mac), but that’s a great idea.

  9. March 19th, 2008 at 18:13 | #9
  10. March 21st, 2009 at 01:26 | #10

    As per steve, I seems a good way out. Has some one implemented his way ?

  1. September 19th, 2008 at 08:20 | #1
  2. September 30th, 2008 at 08:23 | #2
  3. October 22nd, 2008 at 23:51 | #3