Friday, October 26, 2007

Silverlight Host Loosing Mouse Capture...

So one issue we have had to deal with in Silverlight is loosing Mouse Capture. So inside the context of a silverlight application this isn't an issue. The problem is when we have a small embeded silverlight app, say a media player for instance that we want to be able to be dragged and resized dynamically accross the page over HTML content and back. We can build this application so it has a typical windows like drag corner that you can left down click on and then drag out to resize. In theory this works. The issue is if the mouse ever gets off of the silverlight app surface, which is very easy todo, Silverlight still thinks it has the mouse captured but events are no longer received and the on mouse up never gets fired. Further when you move back on to the Silverlight surface you still have managed to loose the mouse and code can get easily confused ie: say your using a flag to determine if the mouse is captured since silverlight currently doesn't have a 'isMouseCaptured' property etc.

So now we have framed the problem here is how we solved it. So even though the media player visually one size when we first catch the mouse down on the drag corner we actually change the size of the underlying aghost for silverlight to be an extra buffer of so many pixels. So now the aghost makes it much harder for the user to get the mouse off of the silverlight surface so we basically run around the screen and chase after the mouse. On drop then we resize the aghost to match the current drag corner position. On IE this works really well, on firefox it is more problematic but does work well if done correctly. On Safari on the Mac the drag works well but Safari is generally more touchy around the html dom.

Another point or rather surprise for me was making this same player work on Safari on the Mac... lets not get confused with Safari on windows which as of this post blows up on contact with silverlight. But anyway. on the Mac, Silverlight runs as smoothly as in IE especially with this particular mouse capture issue. Hmm... makes me wonder if it is just Firefox...

1 comment:

  1. For detec mouse capturing try to use code like this:

    bool IsMouseCaptured = false;
    if (!((FrameworkElement)sender).CaptureMouse())
    IsMouseCaptured = true;