diff options
author | Dominique Louis <savagesoftware@gmail.com> | 2018-03-26 16:35:43 +0300 |
---|---|---|
committer | Dominique Louis <savagesoftware@gmail.com> | 2018-03-26 16:35:43 +0300 |
commit | f6edb28cdaf441e563efd9b6d63414b6d3a63e66 (patch) | |
tree | a97f3159270e05f149fe73f6215cca18c2cb606f /Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs | |
parent | 1b7362132a466acd37b5841e6ce4683b1d1bc08d (diff) |
Slight refactor to avoid duplice code.
Diffstat (limited to 'Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs')
-rw-r--r-- | Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs b/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs index 3eb923b..1cad5ef 100644 --- a/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs +++ b/Xamarin.PropertyEditing.Mac/PropertyTableDelegate.cs @@ -14,6 +14,9 @@ namespace Xamarin.PropertyEditing.Mac { bool goldenRatioApplied = false; + const string editorIdentifier = "editor"; + const string labelIdentifier = "label"; + public PropertyTableDelegate (PropertyTableDataSource datasource) { this.dataSource = datasource; @@ -43,10 +46,10 @@ namespace Xamarin.PropertyEditing.Mac // the table is looking for this method, picks it up automagically public override NSView GetView (NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item) { - var facade = (NSObjectFacade)item; - var vm = facade.Target as PropertyViewModel; - var group = facade.Target as IGroupingList<string, EditorViewModel>; - string cellIdentifier = (group == null) ? vm.GetType ().Name : group.Key; + PropertyViewModel vm; + IGroupingList<string, EditorViewModel> group; + string cellIdentifier; + GetVMGroupCellItendifiterFromFacade (item, out vm, out group, out cellIdentifier); // Let's make the columns look pretty if (!goldenRatioApplied) { @@ -61,10 +64,10 @@ namespace Xamarin.PropertyEditing.Mac // Setup view based on the column switch (tableColumn.Identifier) { case PropertyEditorPanel.PropertyListColId: - var view = (UnfocusableTextField)outlineView.MakeView ("label", this); + var view = (UnfocusableTextField)outlineView.MakeView (labelIdentifier, this); if (view == null) { view = new UnfocusableTextField { - Identifier = "label", + Identifier = labelIdentifier, Alignment = NSTextAlignment.Right, }; } @@ -83,13 +86,11 @@ namespace Xamarin.PropertyEditing.Mac if (vm == null) return null; - var editor = (PropertyEditorControl)outlineView.MakeView (cellIdentifier + "edits", this); - if (editor == null) { - editor = GetEditor (vm, outlineView); - // If still null we have no editor yet. - if (editor == null) - return new NSView (); - } + var editor = MakeEditorView (cellIdentifier + editorIdentifier, vm, outlineView); + + // If still null we have no editor yet. + if (editor == null) + return new NSView (); // we must reset these every time, as the view may have been reused editor.TableRow = outlineView.RowForItem (item); @@ -157,21 +158,22 @@ namespace Xamarin.PropertyEditing.Mac public override nfloat GetRowHeight (NSOutlineView outlineView, NSObject item) { - var facade = (NSObjectFacade)item; - var group = facade.Target as IGroupingList<string, EditorViewModel>; + PropertyViewModel vm; + IGroupingList<string, EditorViewModel> group; + string cellIdentifier; + GetVMGroupCellItendifiterFromFacade (item, out vm, out group, out cellIdentifier); + if (group != null) { return 30; } - var vm = (EditorViewModel)facade.Target; - var editor = (PropertyEditorControl)outlineView.MakeView (vm.GetType ().Name + "edits", this); + var editor = MakeEditorView (cellIdentifier + editorIdentifier, vm, outlineView); + + // If still null we have no editor yet. if (editor == null) { - editor = GetEditor (vm, outlineView); - // If still null we have no editor yet. - if (editor == null) { - return 22; - } + return 22; } + return editor.RowHeight; } @@ -189,6 +191,24 @@ namespace Xamarin.PropertyEditing.Mac return view; } + private void GetVMGroupCellItendifiterFromFacade (NSObject item, out PropertyViewModel vm, out IGroupingList<string, EditorViewModel> group, out string cellIdentifier) + { + var facade = (NSObjectFacade)item; + vm = facade.Target as PropertyViewModel; + group = facade.Target as IGroupingList<string, EditorViewModel>; + cellIdentifier = (group == null) ? vm.GetType ().Name : group.Key; + } + + private PropertyEditorControl MakeEditorView (string identifier, PropertyViewModel vm, NSOutlineView outlineView) + { + var editor = (PropertyEditorControl)outlineView.MakeView (identifier, this); + if (editor == null) { + editor = GetEditor (vm, outlineView); + } + + return editor; + } + private static readonly Dictionary<Type, Type> ViewModelTypes = new Dictionary<Type, Type> { {typeof (StringPropertyViewModel), typeof (StringEditorControl)}, {typeof (NumericPropertyViewModel<>), typeof (NumericEditorControl<>)}, |