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
66
67
|
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 NSPopover {
Behavior = NSPopoverBehavior.Transient,
Delegate = new PopoverDelegate<ITypeInfo> (tcs),
ContentViewController = new NSViewController {
View = selector,
PreferredContentSize = new CoreGraphics.CGSize (360, 335)
},
};
popover.SetAppearance (hostResources.GetVibrantAppearance (source.EffectiveAppearance));
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;
}
}
}
|