diff options
author | Eric Maupin <ermaup@microsoft.com> | 2018-09-18 23:41:14 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2018-10-03 20:27:14 +0300 |
commit | 3d7b1d872a4ffcc73d4ff05288fb7519414692da (patch) | |
tree | ff471461059e040408dc81dcff12f61d6460446e /Xamarin.PropertyEditing.Mac/Controls/Custom | |
parent | f58aea14ef0b2646df97d51d8c73a957fe5d6851 (diff) |
[mac] Reuse tabs
Diffstat (limited to 'Xamarin.PropertyEditing.Mac/Controls/Custom')
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs | 63 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/UnderlinedTabViewController.cs | 65 |
2 files changed, 77 insertions, 51 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs index 36d7ceb..588b4f6 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs @@ -1,3 +1,5 @@ +using System;
+using System.Linq;
using System.Collections.Generic; using System.ComponentModel; using AppKit; @@ -17,29 +19,39 @@ namespace Xamarin.PropertyEditing.Mac EdgeInsets = new NSEdgeInsets (0, 12, 12, 12); } - Dictionary<CommonBrushType, int> BrushTypeTable = new Dictionary<CommonBrushType, int> (); - bool inhibitSelection; - public override void OnViewModelChanged (BrushPropertyViewModel oldModel) { this.inhibitSelection = true; - base.OnViewModelChanged (oldModel); - - foreach (var item in TabViewItems) { - RemoveTabViewItem (item); - item.Dispose (); + base.OnViewModelChanged (oldModel);
+
+ var existing = new HashSet<CommonBrushType> (ViewModel?.BrushTypes?.Values ?? Array.Empty<CommonBrushType> ());
+ existing.IntersectWith (this.brushTypeTable.Keys);
+
+ var removed = new HashSet<CommonBrushType> (this.brushTypeTable.Keys);
+ removed.ExceptWith (existing);
+
+ foreach (var item in removed.Select (t => new { Type = t, Tab = TabView.Items[this.brushTypeTable[t]] }).ToArray()) {
+ RemoveTabViewItem (item.Tab);
+ item.Tab.Dispose ();
+ this.brushTypeTable.Remove (item.Type);
} - BrushTypeTable.Clear (); if (ViewModel == null) return; +
+ int i = -1; + foreach (var kvp in ViewModel.BrushTypes) {
+ i++;
+ this.brushTypeTable[kvp.Value] = i;
+ if (existing.Contains (kvp.Value)) {
+ ((NotifyingViewController<BrushPropertyViewModel>)TabViewItems[i].ViewController).ViewModel = ViewModel;
+ continue;
+ }
- foreach (var key in ViewModel.BrushTypes.Keys) { var item = new NSTabViewItem (); - item.Label = key; - var brushType = ViewModel.BrushTypes [key]; + item.Label = kvp.Key; - switch (brushType) { + switch (kvp.Value) { case CommonBrushType.Solid: var solid = new SolidColorBrushEditorViewController (); solid.ViewModel = ViewModel; @@ -67,7 +79,8 @@ namespace Xamarin.PropertyEditing.Mac item.ViewController = gradient; item.ToolTip = item.Label; item.Image = NSImage.ImageNamed ("property-brush-gradient-16"); - break; + break;
+ default: case CommonBrushType.NoBrush: var none = new EmptyBrushEditorViewController (); none.ViewModel = ViewModel; @@ -75,16 +88,15 @@ namespace Xamarin.PropertyEditing.Mac item.ToolTip = Properties.Resources.NoBrush; item.Image = NSImage.ImageNamed ("property-brush-none-16"); break; - } - if (item.ViewController != null) { - BrushTypeTable [brushType] = TabViewItems.Length; - AddTabViewItem (item); - } + }
+ + InsertTabViewItem (item, i); } - if (BrushTypeTable.TryGetValue (ViewModel.SelectedBrushType, out var index)) { + if (this.brushTypeTable.TryGetValue (ViewModel.SelectedBrushType, out int index)) { SelectedTabViewItemIndex = index; - } + }
+ this.inhibitSelection = false; } @@ -93,8 +105,8 @@ namespace Xamarin.PropertyEditing.Mac base.OnPropertyChanged (sender, args); switch (args.PropertyName) { case nameof (BrushPropertyViewModel.SelectedBrushType): - if (BrushTypeTable.TryGetValue (ViewModel.SelectedBrushType, out var index)) { - this.SelectedTabViewItemIndex = index; + if (this.brushTypeTable.TryGetValue (ViewModel.SelectedBrushType, out int index)) { + SelectedTabViewItemIndex = index; } break; } @@ -128,6 +140,9 @@ namespace Xamarin.PropertyEditing.Mac this.inhibitSelection = true; base.ViewDidLoad (); this.inhibitSelection = false; - } + }
+
+ private readonly Dictionary<CommonBrushType, int> brushTypeTable = new Dictionary<CommonBrushType, int> ();
+ private bool inhibitSelection; } } diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/UnderlinedTabViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/UnderlinedTabViewController.cs index 51fd587..cd573f0 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/UnderlinedTabViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/UnderlinedTabViewController.cs @@ -8,28 +8,16 @@ namespace Xamarin.PropertyEditing.Mac : NotifyingTabViewController<TViewModel> where TViewModel : NotifyingObject { - public override void AddTabViewItem (NSTabViewItem item) + public override void AddTabViewItem (NSTabViewItem tabViewItem) { - base.AddTabViewItem (item); + base.AddTabViewItem (tabViewItem);
+ this.tabStack.AddView (GetView (tabViewItem), NSStackViewGravity.Leading); + }
- NSView tabView; - if (item.Image != null) { - tabView = new UnderlinedImageView (item.Image.Name) { - Selected = this.tabStack.Views.Length == SelectedTabViewItemIndex, - ToolTip = item.ToolTip
- }; - } else { - tabView = new UnderlinedTextField { - BackgroundColor = NSColor.Clear, - Editable = false, - Bezeled = false, - StringValue = item.Label, - Selected = this.tabStack.Views.Length == SelectedTabViewItemIndex, - ToolTip = item.ToolTip
- }; - }
-
- this.tabStack.AddView (tabView, NSStackViewGravity.Leading); + public override void InsertTabViewItem (NSTabViewItem tabViewItem, nint index) + { + base.InsertTabViewItem (tabViewItem, index);
+ this.tabStack.InsertView (GetView (tabViewItem), (nuint)index, NSStackViewGravity.Leading); } public override void RemoveTabViewItem (NSTabViewItem tabViewItem) @@ -42,13 +30,6 @@ namespace Xamarin.PropertyEditing.Mac base.RemoveTabViewItem (tabViewItem); } - private NSStackView outerStack; - private NSStackView innerStack; - private NSStackView tabStack = new NSStackView () { - Spacing = 2f, - }; - - private NSEdgeInsets edgeInsets = new NSEdgeInsets (0, 0, 0, 0); public NSEdgeInsets EdgeInsets { get => this.edgeInsets; set { @@ -98,6 +79,36 @@ namespace Xamarin.PropertyEditing.Mac this.innerStack.AddView (this.tabStack, NSStackViewGravity.Leading); this.innerStack.AddView (TabView, NSStackViewGravity.Bottom); View = this.outerStack; + }
+
+ private NSStackView outerStack;
+ private NSStackView innerStack;
+ private NSStackView tabStack = new NSStackView () {
+ Spacing = 2f,
+ };
+
+ private NSEdgeInsets edgeInsets = new NSEdgeInsets (0, 0, 0, 0);
+
+ private NSView GetView (NSTabViewItem item)
+ {
+ NSView tabView;
+ if (item.Image != null) {
+ tabView = new UnderlinedImageView (item.Image.Name) {
+ Selected = this.tabStack.Views.Length == SelectedTabViewItemIndex,
+ ToolTip = item.ToolTip
+ };
+ } else {
+ tabView = new UnderlinedTextField {
+ BackgroundColor = NSColor.Clear,
+ Editable = false,
+ Bezeled = false,
+ StringValue = item.Label,
+ Selected = this.tabStack.Views.Length == SelectedTabViewItemIndex,
+ ToolTip = item.ToolTip
+ };
+ }
+
+ return tabView;
} } } |