diff options
author | Eric Maupin <ermaup@microsoft.com> | 2019-02-12 20:57:17 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2019-02-21 01:45:03 +0300 |
commit | 82cc1d7043a0bcdcd2fc41df66a4bf880a4568ae (patch) | |
tree | ac6813b917038cdf10470220f951be2a53717e2e /Xamarin.PropertyEditing.Mac | |
parent | 28eb6e12eb1b4d7ff80cbe0fd2bf0ea4012cdaa6 (diff) |
[mac] Make type requests reusable
Diffstat (limited to 'Xamarin.PropertyEditing.Mac')
3 files changed, 66 insertions, 48 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/ControlExtensions.cs b/Xamarin.PropertyEditing.Mac/Controls/ControlExtensions.cs new file mode 100644 index 0000000..3e94781 --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/ControlExtensions.cs @@ -0,0 +1,64 @@ +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 => { + 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; + } + } +} diff --git a/Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs index ab9749b..9e71316 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs @@ -110,38 +110,7 @@ namespace Xamarin.PropertyEditing.Mac private void OnTypeRequested (object sender, TypeRequestedEventArgs e) { - var tcs = new TaskCompletionSource<ITypeInfo> (); - e.SelectedType = tcs.Task; - - var vm = new TypeSelectorViewModel (ViewModel.AssignableTypes); - var selector = new TypeSelectorControl { - ViewModel = vm, - Appearance = 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 (EffectiveAppearance)); - - tcs.Task.ContinueWith (t => { - popover.PerformClose (popover); - popover.Dispose (); - }, TaskScheduler.FromCurrentSynchronizationContext()); - - popover.Show (this.createObject.Frame, this, NSRectEdge.MinYEdge); + e.SelectedType = e.RequestAt (HostResources, this.createObject, ViewModel.AssignableTypes); } private void UpdateTypeLabel () @@ -164,21 +133,5 @@ namespace Xamarin.PropertyEditing.Mac { ViewModel.CreateInstanceCommand.Execute (null); } - - 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; - } } } diff --git a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj index b3c9134..15e8123 100644 --- a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj +++ b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj @@ -149,6 +149,7 @@ <Compile Include="Controls\TypeSelectorControl.cs" />
<Compile Include="Controls\TypeSelectorWindow.cs" />
<Compile Include="Controls\Custom\PropertyTextField.cs" />
+ <Compile Include="Controls\ControlExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controls\" />
|