Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UnderlinedTabViewController.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4911171a9eda69562382f858aef10cf1943a1cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;

using AppKit;
using CoreGraphics;
using Foundation;

namespace Xamarin.PropertyEditing.Mac
{
	internal class UnderlinedTabViewController<TViewModel>
		: NotifyingTabViewController<TViewModel>
		where TViewModel : NotifyingObject
	{
		public UnderlinedTabViewController (IHostResourceProvider hostResources)
		{
			if (hostResources == null)
				throw new ArgumentNullException (nameof (hostResources));

			HostResources = hostResources;
		}

		public string TabBackgroundColor
		{
			get => this.tabBackground;
			set
			{
				if (this.tabContainer != null)
					this.tabContainer.FillColorName = value;

				this.tabBackground = value;
			}
		}

		public string TabBorderColor
		{
			get => this.tabBorder;
			set
			{
				if (this.border != null)
					this.border.FillColorName = value;

				this.tabBorder = value;
			}
		}

		public override void InsertTabViewItem (NSTabViewItem tabViewItem, nint index)
		{
			this.tabStack.InsertView (GetView (tabViewItem), (nuint)index, NSStackViewGravity.Leading);
			base.InsertTabViewItem (tabViewItem, index);
		}

		public override void RemoveTabViewItem (NSTabViewItem tabViewItem)
		{
			int index = (int)TabView.IndexOf (tabViewItem);
			NSView tabView = this.tabStack.Views[index];
			this.tabStack.RemoveView (tabView);
			tabView.Dispose ();

			base.RemoveTabViewItem (tabViewItem);
		}

		/// <remarks>This doesn't actually support independent left/right padding, they'll be added and content centered.</remarks>
		public NSEdgeInsets ContentPadding {
			get => this.edgeInsets;
			set {
				this.edgeInsets = value;
				UpdatePadding ();
			}
		}

		public override void MouseDown (NSEvent theEvent)
		{
			NSView hit = View.HitTest (View.Superview.ConvertPointFromView (theEvent.LocationInWindow, null));
			if (!(hit is IUnderliningTabView))
				return;

			SelectedTabViewItemIndex = Array.IndexOf (this.tabStack.Views, hit);
		}

		public override void DidSelect (NSTabView tabView, NSTabViewItem item)
		{
			base.DidSelect (tabView, item);

			int i = (int)TabView.IndexOf (item);
			SetSelected (i);
		}

		public override nint SelectedTabViewItemIndex
		{
			get => base.SelectedTabViewItemIndex;
			set
			{
				base.SelectedTabViewItemIndex = value;
				SetSelected ((int)value);
			}
		}

		public override void LoadView ()
		{
			this.innerStack = new NSStackView () {
				Spacing = 0,
				Alignment = NSLayoutAttribute.Left,
				Orientation = NSUserInterfaceLayoutOrientation.Vertical,
			};

			NSView tabs = this.tabStack;
			if (TabBackgroundColor != null) {
				this.tabContainer = new DynamicBox (HostResources, TabBackgroundColor) {
					ContentView = this.tabStack
				};

				tabs = this.tabContainer;
			}

			this.innerStack.AddView (tabs, NSStackViewGravity.Top);

			if (TabBorderColor != null) {
				this.border = new DynamicBox (HostResources, TabBorderColor)	{
					Frame = new CGRect (0, 0, 1, 1),
					AutoresizingMask = NSViewResizingMask.WidthSizable
				};

				this.innerStack.AddView (this.border, NSStackViewGravity.Top);
			}

			this.innerStack.AddView (TabView, NSStackViewGravity.Bottom);

			View = this.innerStack;

			if (TabBackgroundColor != null) {
				this.innerStack.AddConstraint (NSLayoutConstraint.Create (this.tabContainer, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.innerStack, NSLayoutAttribute.Width, 1, 0));
			}

			this.innerStack.AddConstraint (NSLayoutConstraint.Create (TabView, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, this.innerStack, NSLayoutAttribute.CenterX, 1, 0));

			UpdatePadding ();
		}

		protected IHostResourceProvider HostResources
		{
			get;
		}

		private NSLayoutConstraint sidePadding, topPadding, bottomPadding;

		private string tabBackground, tabBorder;
		private IUnderliningTabView selected;
		private DynamicBox tabContainer, border;
		private NSStackView innerStack;
		private readonly NSStackView tabStack = new NSStackView () {
			Spacing = 1f,
		};

		protected NSStackView TabStack => this.tabStack;

		private NSEdgeInsets edgeInsets = new NSEdgeInsets (0, 0, 0, 0);

		private void UpdatePadding()
		{
			if (this.innerStack == null)
				return;

			if (this.sidePadding != null)
				this.innerStack.RemoveConstraints (new[] { this.sidePadding, this.topPadding, this.bottomPadding });

			this.sidePadding = NSLayoutConstraint.Create (TabView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.innerStack, NSLayoutAttribute.Width, 1, -(ContentPadding.Left + ContentPadding.Right));
			this.bottomPadding = NSLayoutConstraint.Create (TabView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.innerStack, NSLayoutAttribute.Bottom, 1, -(ContentPadding.Bottom));

			NSView bottomItem = this.border ?? this.tabContainer ?? (NSView)this.tabStack;
			this.topPadding = NSLayoutConstraint.Create (TabView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, bottomItem, NSLayoutAttribute.Bottom, 1, ContentPadding.Top);

			this.innerStack.AddConstraints (new[] { this.sidePadding, this.topPadding, this.bottomPadding });
		}

		private void SetSelected (int index)
		{
			if (this.selected != null) {
				this.selected.Selected = false;
			}

			this.selected = this.tabStack.Views[index] as IUnderliningTabView;
			if (this.selected != null)
				this.selected.Selected = true;
		}

		private NSView GetView (NSTabViewItem item)
		{
			var id = item.Identifier as NSObjectFacade;
			NSView tabView;
			if (id != null) {
				tabView = new UnderlinedImageView (HostResources, ((string)id.Target)) {
					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;
		}
	}
}