diff options
Diffstat (limited to 'Xamarin.PropertyEditing.Mac/Controls')
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs | 184 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/TypeSelectorWindow.cs | 75 |
2 files changed, 259 insertions, 0 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs new file mode 100644 index 0000000..ab9749b --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/ObjectEditorControl.cs @@ -0,0 +1,184 @@ +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 ObjectEditorControl + : PropertyEditorControl<ObjectPropertyViewModel> + { + public ObjectEditorControl (IHostResourceProvider hostResources) + : base (hostResources) + { + this.typeLabel = new UnfocusableTextField { + TranslatesAutoresizingMaskIntoConstraints = false + }; + AddSubview (this.typeLabel); + + this.createObject = new NSButton { + Title = Properties.Resources.New, + TranslatesAutoresizingMaskIntoConstraints = false, + BezelStyle = NSBezelStyle.Rounded + }; + this.createObject.Activated += OnNewPressed; + AddSubview (this.createObject); + + this.buttonConstraint = NSLayoutConstraint.Create (this.createObject, 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.createObject, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1, 0).WithPriority (NSLayoutPriority.DefaultLow), + NSLayoutConstraint.Create (this.createObject, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f), + NSLayoutConstraint.Create (this.createObject, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1f, 70f), + }); + } + + public override NSView FirstKeyView => this.createObject; + + public override NSView LastKeyView => this.createObject; + + protected override void UpdateValue () + { + } + + protected override void UpdateErrorsDisplayed (IEnumerable errors) + { + } + + protected override void HandleErrorsChanged (object sender, DataErrorsChangedEventArgs e) + { + } + + protected override void SetEnabled () + { + this.createObject.Enabled = ViewModel.Property.CanWrite; + } + + protected override void UpdateAccessibilityValues () + { + this.createObject.AccessibilityTitle = String.Format (Properties.Resources.NewInstanceForProperty, ViewModel.Property.Name); + } + + protected override void OnViewModelChanged (PropertyViewModel oldModel) + { + base.OnViewModelChanged (oldModel); + + if (oldModel is ObjectPropertyViewModel ovm) { + ovm.TypeRequested -= OnTypeRequested; + ovm.CreateInstanceCommand.CanExecuteChanged -= OnCreateInstanceExecutableChanged; + } + + if (ViewModel != null) { + ViewModel.TypeRequested += OnTypeRequested; + ViewModel.CreateInstanceCommand.CanExecuteChanged += OnCreateInstanceExecutableChanged; + + OnPropertyChanged (ViewModel, new PropertyChangedEventArgs (null)); + } + } + + protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) { + case nameof (ObjectPropertyViewModel.ValueType): + UpdateTypeLabel (); + break; + case null: + case "": + UpdateTypeLabel (); + UpdateCreateInstanceCommand (); + break; + } + + base.OnPropertyChanged (sender, e); + } + + private readonly UnfocusableTextField typeLabel; + private readonly NSButton createObject; + private readonly NSLayoutConstraint buttonConstraint; + + private void OnCreateInstanceExecutableChanged (object sender, EventArgs e) + { + UpdateCreateInstanceCommand (); + } + + 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); + } + + private void UpdateTypeLabel () + { + if (ViewModel.ValueType == null) { + this.typeLabel.StringValue = String.Empty; + this.buttonConstraint.Active = false; + } else { + this.typeLabel.StringValue = $"({ViewModel.ValueType.Name})"; + this.buttonConstraint.Active = true; + } + } + + private void UpdateCreateInstanceCommand() + { + this.createObject.Enabled = ViewModel.CreateInstanceCommand.CanExecute (null); + } + + private void OnNewPressed (object sender, EventArgs e) + { + 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/TypeSelectorWindow.cs b/Xamarin.PropertyEditing.Mac/Controls/TypeSelectorWindow.cs new file mode 100644 index 0000000..de4da3c --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/TypeSelectorWindow.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +using AppKit; +using CoreGraphics; + +using Xamarin.PropertyEditing.ViewModels; + +namespace Xamarin.PropertyEditing.Mac +{ + internal class TypeSelectorWindow + : NSWindow + { + public TypeSelectorWindow (TypeSelectorViewModel viewModel) + : base (new CGRect (0, 0, 300, 300), NSWindowStyle.Titled | NSWindowStyle.Closable | NSWindowStyle.Resizable, NSBackingStore.Buffered, true) + { + Title = Properties.Resources.SelectObjectTitle; + + this.selector = new TypeSelectorControl { + ViewModel = viewModel, + TranslatesAutoresizingMaskIntoConstraints = false + }; + + ContentView.AddSubview (this.selector); + + this.ok = NSButton.CreateButton (Properties.Resources.OK, OnOked); + this.ok.TranslatesAutoresizingMaskIntoConstraints = false; + ContentView.AddSubview (this.ok); + + this.cancel = NSButton.CreateButton (Properties.Resources.Cancel, OnCanceled); + this.cancel.TranslatesAutoresizingMaskIntoConstraints = false; + ContentView.AddSubview (this.cancel); + + ContentView.AddConstraints (new[] { + NSLayoutConstraint.Create (this.selector, NSLayoutAttribute.Width, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Width, 1, -20), + NSLayoutConstraint.Create (this.selector, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 0), + NSLayoutConstraint.Create (this.selector, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.CenterX, 1, 0), + NSLayoutConstraint.Create (this.selector, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Top, 1, -10), + + NSLayoutConstraint.Create (this.ok, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Bottom, 1, -10), + NSLayoutConstraint.Create (this.ok, NSLayoutAttribute.Right, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Right, 1, -10), + + NSLayoutConstraint.Create (this.cancel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Left, 1, -10), + NSLayoutConstraint.Create (this.cancel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Bottom, 1, -10), + }); + } + + private TypeSelectorControl selector; + private NSButton ok, cancel; + + private void OnOked () + { + NSApplication.SharedApplication.StopModalWithCode ((int)NSModalResponse.OK); + Close (); + } + + private void OnCanceled () + { + NSApplication.SharedApplication.StopModalWithCode ((int)NSModalResponse.Cancel); + Close (); + } + + public static ITypeInfo RequestType (AsyncValue<IReadOnlyDictionary<IAssemblyInfo, ILookup<string, ITypeInfo>>> assignableTypes) + { + var w = new TypeSelectorWindow (new TypeSelectorViewModel (assignableTypes)); + + var result = (NSModalResponse)(int)NSApplication.SharedApplication.RunModalForWindow (w); + if (result != NSModalResponse.OK) + return null; + + return w.selector.ViewModel.SelectedType; + } + } +} |