Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ControlExtensions.cs « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd51e7443158299abda9b78be690a8f15a279292 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal static  class ControlExtensions
	{
		public static Task<ITypeInfo> RequestAt (this TypeRequestedEventArgs self, IHostResourceProvider hostResources, NSView source, AsyncValue<IReadOnlyDictionary<IAssemblyInfo, ILookup<string, ITypeInfo>>> assignableTypes)
		{
			var tcs = new TaskCompletionSource<ITypeInfo> ();

			var vm = new TypeSelectorViewModel (assignableTypes);
			var selector = new TypeSelectorControl {
				ViewModel = vm,
				Appearance = source.EffectiveAppearance
			};

			vm.PropertyChanged += (vms, ve) => {
				if (ve.PropertyName == nameof (TypeSelectorViewModel.SelectedType)) {
					tcs.TrySetResult (vm.SelectedType);
				}
			};

			var popover = new AutoClosePopOver (hostResources, hostResources.GetVibrantAppearance (source.EffectiveAppearance)) {
				Delegate = new PopoverDelegate<ITypeInfo> (tcs),
				ContentViewController = new NSViewController {
					View = selector,
					PreferredContentSize = new CoreGraphics.CGSize (360, 335)
				},
			};

			tcs.Task.ContinueWith (t => {
				// Focus back to the button that popped us up
				source.Superview?.Window?.MakeFirstResponder (source);

				popover.PerformClose (popover);
				popover.Dispose ();
			}, TaskScheduler.FromCurrentSynchronizationContext ());

			popover.Show (source.Frame, source.Superview, NSRectEdge.MinYEdge);
			return tcs.Task;
		}

		private class PopoverDelegate<T>
			: NSPopoverDelegate
		{
			public PopoverDelegate (TaskCompletionSource<T> tcs)
			{
				this.tcs = tcs;
			}

			public override void WillClose (NSNotification notification)
			{
				this.tcs.TrySetCanceled ();
			}

			private readonly TaskCompletionSource<T> tcs;
		}
	}
}