When I started working at Xamarin, I had the intention to blog about new technologies I was learning, but it’s been already 6 months and it didn’t happen at all, so better to start late than never. I’ll start then with a nice piece of code I came up with, and which is this:
public static class CocoaExtensions
{
public static void AnimatedSetPositionOfDivider (this NSSplitView splitView, float position, int divider)
{
var view0 = splitView.Subviews [0];
var view1 = splitView.Subviews [1];
var newFrame0 = view0.Frame;
var newFrame1 = view1.Frame;
if (splitView.IsVertical) {
newFrame0.Width = position == 0 ? 0 : position - splitView.DividerThickness;
newFrame1.Width = position == splitView.MaxPositionOfDivider (divider)
? 0
: splitView.Bounds.Width - position - splitView.DividerThickness;
} else {
newFrame0.Height = position == 0 ? 0 : position - splitView.DividerThickness;
newFrame1.Height = position == splitView.MaxPositionOfDivider (divider)
? 0
: splitView.Bounds.Height - position - splitView.DividerThickness;
}
newFrame0.Width = newFrame0.Width < 0 ? 0 : newFrame0.Width;
newFrame0.Height = newFrame0.Height < 0 ? 0 : newFrame0.Height;
newFrame1.Width = newFrame1.Width < 0 ? 0 : newFrame1.Width;
newFrame1.Height = newFrame1.Height < 0 ? 0 : newFrame1.Height;
view0.Hidden = view1.Hidden = false;
view0.AutoresizesSubviews = view1.AutoresizesSubviews = true;
if ((newFrame0.Width == 0 && newFrame0.Height == 0) ||
(newFrame1.Width == 0 && newFrame1.Height == 0)) {
return;
}
var singleAnimation0 = new NSMutableDictionary ();
singleAnimation0 [NSViewAnimation.TargetKey] = view0;
singleAnimation0 [NSViewAnimation.EndFrameKey] = NSValue.FromRectangleF (newFrame0);
var singleAnimation1 = new NSMutableDictionary ();
singleAnimation1 [NSViewAnimation.TargetKey] = view1;
singleAnimation1 [NSViewAnimation.EndFrameKey] = NSValue.FromRectangleF (newFrame1);
var animation = new NSViewAnimation (new NSDictionary[] { singleAnimation0, singleAnimation1 });
animation.Duration = 0.25f;
animation.StartAnimation ();
}
}
The main reason to share this code is because I couldn’t find anything that worked to do that (animate the collapsing and expanding of a NSSplitView, which is, yes, you got it right, a split view, like GTK’s GtkPaned), so I hope it is useful for someone. But it also shows a few interesting things about both C# and Cocoa:
- The most obvious one: writing Cocoa apps in C# is much better than using Objective C (although, to be honest, I also like Objective C).
- Cocoa (and CoreAnimation) lets you easily add animations to your UI, by having the animations layer tightly integrated into the API. Of course, animations are not always great, but in some cases, like this one where the collapsing/expansion of the split view’s subviews is animated, it makes such a huge difference to the UI that it’s very nice to be able to do it that easily.
- C# allows extending existing classes, by writing extension methods (static methods in static classes that have a “this” modifier in the 1st argument, which specifies the class the method extends). This is a great way to extend existing classes, without having to do any subclassing. Once you have the extension method, you can just call it on any NSSplitView:
mySplitView.AnimatedSetPositionOfDivider (position, divider);
You can extend any class, and this what a lot of technologies (LINQ, Reactive Extensions, etc) in the .NET world use.
I started also, when I started working at Xamarin, getting some of the nice ideas from Cocoa and C# into GLib/GTK, so will publish that as soon as I get something useful from it. Hopefully it won’t be another 6 months 😀
And yes, Xamarin is hiring. If interested, drop me a mail, or just apply directly, as you wish.