Wednesday, August 11, 2010

Synchronized Scrollbars

I recently had a need for two side-by-side TextAreas whose vertical scrollbars were synchronized. So if you drag the left scrollbar, the right one updates, and vice versa.

I've written a utility class called LinkedScrollers that synchronizes the scrolling of two Scrollers. It has 5 public properties:
  • enabled (defaults to true) - set to false to allow scrollbars to move independently
  • scroller1 - the first Scroller
  • scroller2 - the second Scroller
  • component1 - the first SkinnableComponent, e.g. List, TextArea
  • component2 - the second SkinnableComponent (List, TextArea, etc)
Originally I wanted to bind the scrollers directly to my LinkedScrollers in MXML like this:
<spark:LinkedScrollers scroller1="{list.scroller}" scroller2="{textArea.scroller}"/>
But unfortunately the scroller property on List/TextArea is not bindable, so that didn't work. You can still use the scroller1 and scroller2 in ActionScript (e.g. in the application's creationComplete handler) to set the scrollers.

So another solution is to set the component1 and component2 properties to the two SkinnableComponents that you want to link (Lists, TextAreas, etc). It should work for any SkinnableComponent that contains a "scroller" skin part.
E.g.
<spark:LinkedScrollers component1="{list}" component2="{textArea}"/>

Here it is in action, view source enabled (right click on the example below):


As you might guess, this follows on from my previous post on Spark TextArea With Line Numbers.

No comments: