Archive for the ‘.NET’ Category

GObservableCollection

Tuesday, December 16th, 2014

In the last year working at Xamarin, I have learned lots of new things (.NET, Cocoa, …), and since the beginning of that, I was thinking on bringing some of that nice stuff to GNOME, but didn’t really had the chance to finish anything. But, fortunately, being free now (on vacation), I finally finished the 1st thing: GObservableCollection, a thread-safe collection implementation which emits signals on changes.

It is based on ideas from .NET’s ObservableCollection and concurrent collections, which I’ve used successfully for building a multi-thread data processing app (with one thread updating the collection and another consuming it), so I thought it would be a good addition to GLib’s API. This class can be used on single-threaded apps to easily get notifications for changes in a collection, and in multi-threaded ones for, as mentioned above, easily share data between different threads (as can be seen on the simple test I wrote).

This is the 1st working version, so for sure it will need improvements, but instead of keeping it private for a few more months, I thought it would be better getting some feedback before I submit it as a patch for GLib’s GIO (if that’s the best place for it, which I guess it is).

C#/Cocoa – Animate a split view’s collapsing/expanding

Wednesday, June 25th, 2014

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 :-D

And yes, Xamarin is hiring. If interested, drop me a mail, or just apply directly, as you wish.