diff options
author | Eric Maupin <ermaup@microsoft.com> | 2018-11-05 21:54:38 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2018-11-12 22:17:15 +0300 |
commit | 776e37bcf0202be6aa069e10b73726fea974bb48 (patch) | |
tree | a9ab256bcd31806cacd04720c0d627eb76187fd8 | |
parent | 86510b4bdab639c2171a2cfae08580879a35187b (diff) |
[mac] Update name header for single column
This also fixes a wasteful instance being created for the facade and should
fix reuse of it.
4 files changed, 109 insertions, 155 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/PanelHeaderEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/PanelHeaderEditorControl.cs index 6cdbf15..65ff1d9 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/PanelHeaderEditorControl.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/PanelHeaderEditorControl.cs @@ -1,44 +1,50 @@ using System; -using System.Collections; using System.ComponentModel; using System.IO; + using AppKit; + using Xamarin.PropertyEditing.Mac.Resources; using Xamarin.PropertyEditing.ViewModels; namespace Xamarin.PropertyEditing.Mac { - internal class PanelHeaderLabelControl : NSView + internal class PanelHeaderEditorControl + : NSView { - public const string PanelHeaderLabelIdentifierString = "PanelHeaderLabelIdentifier"; - - private NSImageView propertyIcon; + public PanelHeaderEditorControl () + { + NSControlSize controlSize = NSControlSize.Small; + TranslatesAutoresizingMaskIntoConstraints = false; - private PanelViewModel viewModel; + this.propertyObjectName = new NSTextField { + ControlSize = controlSize, + PlaceholderString = LocalizationResources.ObjectNamePlaceholder, + TranslatesAutoresizingMaskIntoConstraints = false, + }; - public PanelHeaderLabelControl (PanelViewModel viewModel) - { - if (viewModel == null) - throw new ArgumentNullException (nameof (viewModel)); + this.propertyObjectName.Activated += OnObjectNameEdited; - this.viewModel = viewModel; - this.viewModel.PropertyChanged += ViewModel_PropertyChanged; + AddSubview (this.propertyObjectName); - Identifier = PanelHeaderLabelIdentifierString; + this.typeDisplay = new UnfocusableTextField { + TranslatesAutoresizingMaskIntoConstraints = false, + }; + AddSubview (this.typeDisplay); - var propertyObjectNameLabel = new UnfocusableTextField { + this.objectNameLabel = new UnfocusableTextField { Alignment = NSTextAlignment.Right, StringValue = Properties.Resources.Name + ":", TranslatesAutoresizingMaskIntoConstraints = false, }; - AddSubview (propertyObjectNameLabel); + AddSubview (this.objectNameLabel); - var propertyTypeNameLabel = new UnfocusableTextField { + this.typeLabel = new UnfocusableTextField { Alignment = NSTextAlignment.Right, StringValue = Properties.Resources.Type + ":", TranslatesAutoresizingMaskIntoConstraints = false, }; - AddSubview (propertyTypeNameLabel); + AddSubview (this.typeLabel); this.propertyIcon = new NSImageView { ImageScaling = NSImageScale.AxesIndependently, @@ -46,149 +52,94 @@ namespace Xamarin.PropertyEditing.Mac }; AddSubview (this.propertyIcon); - this.DoConstraints (new NSLayoutConstraint[] { - propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Top == c.Top), - propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Left == c.Left + 182), - propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Width == 40), - propertyObjectNameLabel.ConstraintTo(this, (ol, c) => ol.Height == PropertyEditorControl.DefaultControlHeight), - - propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Top == c.Top + 22), - propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Left == c.Left + 182), - propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Width == 40), - propertyTypeNameLabel.ConstraintTo(this, (tl, c) => tl.Height == PropertyEditorControl.DefaultControlHeight), - - this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Top == c.Top + 5), - this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Left == c.Left + 32), - this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Width == 32), - this.propertyIcon.ConstraintTo(this, (ico, c) => ico.Height == 32), - }); + this.AddConstraints (new[] { - UpdateIcon (); - } + NSLayoutConstraint.Create (this.propertyIcon, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1, 32), + NSLayoutConstraint.Create (this.propertyIcon, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1, 32), + NSLayoutConstraint.Create (this.propertyIcon, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, 5), + NSLayoutConstraint.Create (this.propertyIcon, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 32), - private async void UpdateIcon () - { - Stream icon = await this.viewModel.GetIconAsync (); + NSLayoutConstraint.Create (this.objectNameLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, 5), + NSLayoutConstraint.Create (this.objectNameLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, .38196601125f, 0f), - if (icon != null) - this.propertyIcon.Image = NSImage.FromStream (icon); + NSLayoutConstraint.Create (this.propertyObjectName, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.objectNameLabel, NSLayoutAttribute.CenterY, 1, 0), + NSLayoutConstraint.Create (this.propertyObjectName, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.objectNameLabel, NSLayoutAttribute.Right, 1, 5), + NSLayoutConstraint.Create (this.propertyObjectName, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, -(14 + PropertyButton.DefaultSize)), - this.propertyIcon.Hidden = (icon == null); - } + NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.propertyObjectName, NSLayoutAttribute.Bottom, 1, 5), + NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, .38196601125f, 0f), - void ViewModel_PropertyChanged (object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == nameof (PanelViewModel.TypeName)) { - UpdateIcon (); - } + NSLayoutConstraint.Create (this.typeDisplay, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.typeLabel, NSLayoutAttribute.Top, 1, 0), + NSLayoutConstraint.Create (this.typeDisplay, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.propertyObjectName, NSLayoutAttribute.Width, 1, 0), + NSLayoutConstraint.Create (this.typeDisplay, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.typeLabel, NSLayoutAttribute.Right, 1, 5) + }); } - } - - internal class PanelHeaderEditorControl : PropertyEditorControl - { - private NSTextField propertyObjectName; - - private PanelViewModel viewModel; - public PanelHeaderEditorControl (PanelViewModel viewModel) + public PanelViewModel ViewModel { - if (viewModel == null) - throw new ArgumentNullException (nameof (viewModel)); + get { return this.viewModel; } + set { + if (this.viewModel == value) + return; - this.viewModel = viewModel; - this.viewModel.PropertyChanged += ViewModel_PropertyChanged; - - NSControlSize controlSize = NSControlSize.Small; - TranslatesAutoresizingMaskIntoConstraints = false; + if (this.viewModel != null) { + this.viewModel.PropertyChanged -= OnViewModelPropertyChanged; + } - this.propertyObjectName = new NSTextField { - ControlSize = controlSize, - Font = NSFont.FromFontName (DefaultFontName, DefaultFontSize), - PlaceholderString = LocalizationResources.ObjectNamePlaceholder, - TranslatesAutoresizingMaskIntoConstraints = false, - }; + this.viewModel = value; + if (this.viewModel != null) + this.viewModel.PropertyChanged += OnViewModelPropertyChanged; - this.propertyObjectName.Activated += PropertyObjectName_Activated; - - AddSubview (this.propertyObjectName); - - var propertyTypeName = new UnfocusableTextField { - StringValue = viewModel.TypeName, - TranslatesAutoresizingMaskIntoConstraints = false, - }; - AddSubview (propertyTypeName); - - this.DoConstraints (new NSLayoutConstraint[] { - this.propertyObjectName.ConstraintTo(this, (on, c) => on.Top == c.Top + 2), - this.propertyObjectName.ConstraintTo(this, (on, c) => on.Left == c.Left + 4), - this.propertyObjectName.ConstraintTo(this, (on, c) => on.Width == c.Width - 34), - this.propertyObjectName.ConstraintTo(this, (on, c) => on.Height == DefaultControlHeight - 3), - - propertyTypeName.ConstraintTo(this, (tn, c) => tn.Top == c.Top + 22), - propertyTypeName.ConstraintTo(this, (tn, c) => tn.Left == c.Left + 4), - propertyTypeName.ConstraintTo(this, (tn, c) => tn.Width == c.Width - 34), - propertyTypeName.ConstraintTo(this, (tn, c) => tn.Height == DefaultControlHeight), - }); - - // We won't enable or show the PropertyButton for the header - this.PropertyButton.Enabled = false; - this.PropertyButton.Hidden = true; - - UpdateValue (); + this.typeDisplay.StringValue = value?.TypeName ?? String.Empty; + UpdateValue (); + UpdateIcon (); + this.propertyObjectName.Editable = !this.viewModel.IsObjectNameReadOnly; + } } - void PropertyObjectName_Activated (object sender, EventArgs e) + public void SetNextKeyView (NSView nextKeyView) { - this.viewModel.ObjectName = this.propertyObjectName.StringValue; + this.propertyObjectName.NextKeyView = nextKeyView; } - public override NSView FirstKeyView => this.propertyObjectName; - - public override NSView LastKeyView => this.propertyObjectName; + private NSImageView propertyIcon; + private NSTextField propertyObjectName; + private UnfocusableTextField typeLabel, typeDisplay, objectNameLabel; - protected override void HandleErrorsChanged (object sender, DataErrorsChangedEventArgs e) - { - UpdateErrorsDisplayed (viewModel.GetErrors (viewModel.GetType ().Name)); - } + private PanelViewModel viewModel; - protected override void SetEnabled () + private void OnViewModelPropertyChanged (object sender, PropertyChangedEventArgs e) { - this.propertyObjectName.Editable = !this.viewModel.IsObjectNameReadOnly; + if (e.PropertyName == nameof (PanelViewModel.ObjectName)) { + UpdateValue (); + } else if (e.PropertyName == nameof (PanelViewModel.TypeName)) { + UpdateIcon (); + } else if (String.IsNullOrEmpty (e.PropertyName)) { + UpdateValue (); + UpdateIcon (); + } } - protected override void UpdateAccessibilityValues () + private void UpdateValue () { + this.propertyObjectName.StringValue = this.viewModel.ObjectName ?? string.Empty; this.propertyObjectName.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityObjectName, nameof (viewModel.ObjectName)); + this.propertyObjectName.Editable = !this.viewModel.IsObjectNameReadOnly; } - protected override void UpdateErrorsDisplayed (IEnumerable errors) + private void OnObjectNameEdited (object sender, EventArgs e) { - if (this.viewModel.HasErrors) { - SetErrors (errors); - } else { - SetErrors (null); - SetEnabled (); - } + this.viewModel.ObjectName = this.propertyObjectName.StringValue; } - protected override void UpdateValue () + private async void UpdateIcon () { - if (this.propertyObjectName != null) { - this.propertyObjectName.StringValue = this.viewModel.ObjectName ?? string.Empty; - this.propertyObjectName.Editable = !this.viewModel.IsObjectNameReadOnly; - } - } + Stream icon = await this.viewModel.GetIconAsync (); - void ViewModel_PropertyChanged (object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == nameof (PanelViewModel.ObjectName)) { - UpdateValue (); - } - } + if (icon != null) + this.propertyIcon.Image = NSImage.FromStream (icon); - public override nint GetHeight (EditorViewModel vm) - { - return 44; + this.propertyIcon.Hidden = (icon == null); } } } diff --git a/Xamarin.PropertyEditing.Mac/Controls/PropertyEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/PropertyEditorControl.cs index 6e49187..46bebb8 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/PropertyEditorControl.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/PropertyEditorControl.cs @@ -72,14 +72,18 @@ namespace Xamarin.PropertyEditing.Mac var tr = TableRow; if (tr > 0) { + NSView view; do { tr--; - ctrl = (TableView.GetView (0, tr, false) as EditorContainer)?.EditorView?.NativeView as PropertyEditorControl; + view = TableView.GetView (0, tr, false); + ctrl = (view as EditorContainer)?.EditorView?.NativeView as PropertyEditorControl; } while (tr > 0 && ctrl == null); if (ctrl != null) { ctrl.LastKeyView.NextKeyView = FirstKeyView; ctrl.UpdateKeyViews (); + } else if (tr == 0 && view is PanelHeaderEditorControl header) { + header.SetNextKeyView (FirstKeyView); } } } diff --git a/Xamarin.PropertyEditing.Mac/PropertyTableDataSource.cs b/Xamarin.PropertyEditing.Mac/PropertyTableDataSource.cs index 24a14cb..2334a6e 100644 --- a/Xamarin.PropertyEditing.Mac/PropertyTableDataSource.cs +++ b/Xamarin.PropertyEditing.Mac/PropertyTableDataSource.cs @@ -47,7 +47,7 @@ namespace Xamarin.PropertyEditing.Mac // We only want the Header to appear at the top of both Category and Name Modes, which means item is null in both. if (childIndex == 0 && item == null && !Filtering) - element = new PanelHeaderEditorControl (this.vm); + element = null; else { if (this.vm.ArrangeMode == PropertyArrangeMode.Name) element = Filtering ? this.vm.ArrangedEditors[0][(int)childIndex] : this.vm.ArrangedEditors[0][(int)childIndex - 1]; diff --git a/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs b/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs index d10439b..efd25e8 100644 --- a/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs +++ b/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs @@ -66,9 +66,6 @@ namespace Xamarin.PropertyEditing.Mac return labelContainer; } - if (evm == null) - return null; - NSView editorOrContainer = null; if (this.firstCache.TryGetValue (cellIdentifier, out IEditorView editor)) { this.firstCache.Remove (cellIdentifier); @@ -92,6 +89,8 @@ namespace Xamarin.PropertyEditing.Mac if (editor.IsDynamicallySized) { outlineView.NoteHeightOfRowsWithIndexesChanged (new NSIndexSet (index)); } + } else if (editorOrContainer is PanelHeaderEditorControl header) { + header.ViewModel = this.dataSource.DataContext; } return editorOrContainer; @@ -135,28 +134,28 @@ namespace Xamarin.PropertyEditing.Mac return 20; if (!this.registrations.TryGetValue (cellIdentifier, out EditorRegistration registration)) { - NSView editorOrContainer = GetEditor (cellIdentifier, vm, outlineView); - IEditorView view = ((editorOrContainer as EditorContainer)?.EditorView) ?? editorOrContainer as IEditorView; - - if (view == null) { - this.registrations[cellIdentifier] = registration = new EditorRegistration { - RowSize = PropertyEditorControl.DefaultControlHeight - }; - } else if (view.IsDynamicallySized) { - this.registrations[cellIdentifier] = registration = new EditorRegistration { - SizingInstance = view - }; + registration = new EditorRegistration (); - // We're cheating by declaring GetHeight should act static, so we can call it from - // an instance that is being used elsewhere. - this.firstCache[cellIdentifier] = view; + if (cellIdentifier == nameof(PanelHeaderEditorControl)) { + registration.RowSize = 44; } else { - this.registrations[cellIdentifier] = registration = new EditorRegistration { - RowSize = view.GetHeight (vm) - }; - - this.firstCache[cellIdentifier] = view; + NSView editorOrContainer = GetEditor (cellIdentifier, vm, outlineView); + IEditorView view = ((editorOrContainer as EditorContainer)?.EditorView) ?? editorOrContainer as IEditorView; + + if (view == null) { + registration.RowSize = PropertyEditorControl.DefaultControlHeight; + } else if (view.IsDynamicallySized) { + registration.SizingInstance = view; + } else { + this.registrations[cellIdentifier] = registration = new EditorRegistration { + RowSize = view.GetHeight (vm) + }; + + this.firstCache[cellIdentifier] = view; + } } + + this.registrations[cellIdentifier] = registration; } return registration.GetHeight (vm); @@ -204,7 +203,7 @@ namespace Xamarin.PropertyEditing.Mac return new EditorContainer (editor) { Identifier = identifier }; } else - return new PanelHeaderEditorControl (this.dataSource.DataContext); + return new PanelHeaderEditorControl (); } private void GetVMGroupCellItendifiterFromFacade (NSObject item, out EditorViewModel vm, out IGroupingList<string, EditorViewModel> group, out string cellIdentifier) @@ -212,7 +211,7 @@ namespace Xamarin.PropertyEditing.Mac var facade = (NSObjectFacade)item; vm = facade.Target as EditorViewModel; group = facade.Target as IGroupingList<string, EditorViewModel>; - cellIdentifier = facade.Target is PanelHeaderEditorControl pvh + cellIdentifier = facade.Target == null ? nameof (PanelHeaderEditorControl) : (group == null) ? vm.GetType ().FullName : group.Key; } |