diff options
author | Eric Maupin <me@ermau.com> | 2019-02-21 18:39:46 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-21 18:39:46 +0300 |
commit | 188bdd4e8d3f200b5506ae473743cdf8a0aa0872 (patch) | |
tree | 339db39469d3148a37476603af97d4081e368e88 /Xamarin.PropertyEditing.Mac | |
parent | 74cd11cc4e3972b8ca00eacd5c5015e796eac375 (diff) | |
parent | af9ca3b7b44d4a9d00ec76e9116a2c9d204ae501 (diff) |
Merge pull request #541 from xamarin/ermau-type-selector-editor
Type selector editor
Diffstat (limited to 'Xamarin.PropertyEditing.Mac')
5 files changed, 219 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/Controls/TypeEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/TypeEditorControl.cs new file mode 100644 index 0000000..a7ca19e --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/TypeEditorControl.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections; +using System.ComponentModel; +using System.Threading.Tasks; +using AppKit; +using Foundation; +using Xamarin.PropertyEditing.ViewModels; + +namespace Xamarin.PropertyEditing.Mac +{ + internal class TypeEditorControl + : PropertyEditorControl<TypePropertyViewModel> + { + public TypeEditorControl (IHostResourceProvider hostResources) + : base (hostResources) + { + this.typeLabel = new UnfocusableTextField { + TranslatesAutoresizingMaskIntoConstraints = false + }; + AddSubview (this.typeLabel); + + this.selectType = new NSButton { + Title = Properties.Resources.Select, + TranslatesAutoresizingMaskIntoConstraints = false, + BezelStyle = NSBezelStyle.Rounded + }; + this.selectType.Activated += OnSelectPressed; + AddSubview (this.selectType); + + this.buttonConstraint = NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this.typeLabel, NSLayoutAttribute.Trailing, 1f, 12); + + AddConstraints (new[] { + NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1f, 0f), + NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f), + NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, 0), + this.buttonConstraint, + NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1, 0).WithPriority (NSLayoutPriority.DefaultLow), + NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f), + NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1f, 70f), + }); + } + + public override NSView FirstKeyView => this.selectType; + + public override NSView LastKeyView => this.selectType; + + protected override void UpdateValue () + { + } + + protected override void UpdateErrorsDisplayed (IEnumerable errors) + { + } + + protected override void HandleErrorsChanged (object sender, DataErrorsChangedEventArgs e) + { + } + + protected override void SetEnabled () + { + this.selectType.Enabled = ViewModel.Property.CanWrite; + } + + protected override void UpdateAccessibilityValues () + { + this.selectType.AccessibilityTitle = String.Format (Properties.Resources.SelectTypeForProperty, ViewModel.Property.Name); + } + + protected override void OnViewModelChanged (PropertyViewModel oldModel) + { + base.OnViewModelChanged (oldModel); + + if (oldModel is TypePropertyViewModel tvm) { + tvm.TypeRequested -= OnTypeRequested; + } + + if (ViewModel != null) { + ViewModel.TypeRequested += OnTypeRequested; + + OnPropertyChanged (ViewModel, new PropertyChangedEventArgs (null)); + } + } + + protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) { + case nameof (TypePropertyViewModel.Value): + UpdateTypeLabel (); + break; + case null: + case "": + UpdateTypeLabel (); + UpdateCreateInstanceCommand (); + break; + } + + base.OnPropertyChanged (sender, e); + } + + private readonly UnfocusableTextField typeLabel; + private readonly NSButton selectType; + private readonly NSLayoutConstraint buttonConstraint; + + private void OnCreateInstanceExecutableChanged (object sender, EventArgs e) + { + UpdateCreateInstanceCommand (); + } + + private void OnTypeRequested (object sender, TypeRequestedEventArgs e) + { + e.SelectedType = e.RequestAt (HostResources, this.selectType, ViewModel.AssignableTypes); + } + + private void UpdateTypeLabel () + { + if (ViewModel.Value == null) { + this.typeLabel.StringValue = String.Empty; + this.buttonConstraint.Active = false; + } else { + this.typeLabel.StringValue = $"({ViewModel.Value.Name})"; + this.buttonConstraint.Active = true; + } + } + + private void UpdateCreateInstanceCommand () + { + this.selectType.Enabled = ViewModel.SelectTypeCommand.CanExecute (null); + } + + private void OnSelectPressed (object sender, EventArgs e) + { + ViewModel.SelectTypeCommand.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/PropertyEditorSelector.cs b/Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs index cb96191..29bc5b2 100644 --- a/Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs +++ b/Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs @@ -55,6 +55,7 @@ namespace Xamarin.PropertyEditing.Mac {typeof (ThicknessPropertyViewModel), typeof (CommonThicknessEditorControl) }, {typeof (PropertyGroupViewModel), typeof (GroupEditorControl)}, {typeof (ObjectPropertyViewModel), typeof (ObjectEditorControl)}, + {typeof (TypePropertyViewModel), typeof (TypeEditorControl)}, }; } diff --git a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj index ef68417..5573e89 100644 --- a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj +++ b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj @@ -149,6 +149,8 @@ <Compile Include="Controls\TypeSelectorControl.cs" />
<Compile Include="Controls\TypeSelectorWindow.cs" />
<Compile Include="Controls\Custom\PropertyTextField.cs" />
+ <Compile Include="Controls\ControlExtensions.cs" />
+ <Compile Include="Controls\TypeEditorControl.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controls\" />
|