« Welcome Mike Stall to the DLR team! | Main | IronRuby Talks »

November 05, 2007

IronRuby on Silverlight at RubyConf

It's always a fun time at RubyConf. So, when you have a 9:00am talk, what do you do the night before? Well, hack in new features, of course! At 2:30am, Tomas, John and I removed the last grotesque hack from our Silverlight demo (grab the sources from here):

IronRuby in Silverlight Screenshot

This is one of the first (to my knowledge) examples of a code-first Silverlight 1.1 application. Most Silverlight applications that generate UIs do so by creating a XAML string that they feed to the XAML parser. When you have a language as beautiful as Ruby, it's a shame to be creating trees via strings.

Let's look at the code that generates the Storyboard for the 'bounce' effect for the pictures. So instead of this:

<Storyboard x:Name="Timeline1" TargetName="ScaleTransform1">
  <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
      Storyboard.TargetProperty="ScaleX">     <SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="0.200"/>     <SplineDoubleKeyFrame KeyTime="00:00:00.2" Value="0.935"/>     <SplineDoubleKeyFrame KeyTime="00:00:00.3" Value="0.852"/>     <SplineDoubleKeyFrame KeyTime="00:00:00.4" Value="0.935"/>   </DoubleAnimationUsingKeyFrames>   <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
      Storyboard.TargetProperty="ScaleY">     <SplineDoubleKeyFrame KeyTime="00:00:00.0" Value="0.200"/>     <SplineDoubleKeyFrame KeyTime="00:00:00.2" Value="0.935"/>     <SplineDoubleKeyFrame KeyTime="00:00:00.3" Value="0.852"/>     <SplineDoubleKeyFrame KeyTime="00:00:00.4" Value="0.935"/>   </DoubleAnimationUsingKeyFrames> </Storyboard>

You can do:

class BounceAnimation < AnimationBase
  def initialize(scale_transform_element)
    @obj = Wpf.build(Storyboard, :name => random_name,
        :target_name => scale_transform_element) {

      add(DoubleAnimationUsingKeyFrames, :begin_time=>'00:00:00',
        :target_property => "ScaleX") {

        add(SplineDoubleKeyFrame, :key_time => '00:00:00.0', :value => 0.200)
        add(SplineDoubleKeyFrame, :key_time => '00:00:00.2', :value => 0.935)
        add(SplineDoubleKeyFrame, :key_time => '00:00:00.3', :value => 0.852)
        add(SplineDoubleKeyFrame, :key_time => '00:00:00.4', :value => 0.935)
      }

      add(DoubleAnimationUsingKeyFrames, :begin_time=>'00:00:00',
        :target_property => "ScaleY") {

        add(SplineDoubleKeyFrame, :key_time => '00:00:00.0', :value => 0.200)
        add(SplineDoubleKeyFrame, :key_time => '00:00:00.2', :value => 0.935)
        add(SplineDoubleKeyFrame, :key_time => '00:00:00.3', :value => 0.852)
        add(SplineDoubleKeyFrame, :key_time => '00:00:00.4', :value => 0.935)
      }
    }
  end
end

The advantage of the latter is that the animation is a named entity that can be parameterized or composed with other animations. It's also a great example of how far we have come in IronRuby. If you look at SplineDoubleKeyFrame, we need to convert the string '00:00:00.0' to a KeyTime object. We do so by monkey-patching DoubleKeyFrame, which is a base class of SplineDoubleKeyFrame:

class DoubleKeyFrame
  alias_method :old_key_time=, :key_time=
  def key_time=(time_span)
    self.old_key_time = KeyTime.from_time_span(TimeSpan.parse(time_span))
  end
end

I'm overriding the implementation of the key_time property setter with a custom property setter that does the type conversion. The idea is to define type converters at the point in the inheritance hierarchy where the property is introduced; once you've done so, you've modified the behavior across the system.

If you look at the code in Silverlight.rb, you'll see many more examples of these kinds of type converters. In the future, I suspect that the type converters in Silverlight.rb will be generated by reflecting over the WPF APIs in Silverlight. Look for more metaprogramming goodness for Silverlight as both our implementation and Silverlight matures.

Unfortunately, you can't run these bits yet. We're using a private build of Silverlight to run this stuff today. But once we (DLR) sync up with the next CTP of Silverlight, you'll be able to run IronRuby in your browser. Fun times.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2419952/23073186

Listed below are links to weblogs that reference IronRuby on Silverlight at RubyConf:

Comments

I like the idea, but the XML version looks more concise in this instance. I'd probably use xml builder to create the XAML markup - this would also give you composability.

When you say "run Ruby in the browser," does this mean that we'll be able to do DOM manipulation via Ruby? Because IMO that's when the fun begins.

@Dan:

But you now need to round-trip through the XAML parser, and the thing that you are composing would not be Ruby either.

@Jeremy:

Yes- this is definitely a scenario that SL suports through the HTML bridge. There are some interesting ideas that some folks had at RubyConf about building a browser-agnostic DOM + WPF programming layer in Ruby.

True. I guess it depends on the application, and how you want to couple the definition of the WPF tree with its instantiation.

Post a comment

If you have a TypeKey or TypePad account, please Sign In

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from John Lam. Make your own badge here.

April 2008

Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Blog powered by TypePad