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

PathOutlineView.cs « BindingEditor « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 124cee6285ac41485dbd8892e13e16e9887ee416 (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
using System;
using System.Collections.Generic;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class PathOutlineView : BaseSelectorOutlineView
	{
		private IReadOnlyCollection<object> itemsSource;
		private PropertyTreeRoot propertyTreeRoot;

		private void SetItemsSource (IReadOnlyCollection<object> value, string targetName)
		{
			if (this.itemsSource != value) {
				this.itemsSource = value;

				DataSource = new PathOutlineViewDataSource (this.itemsSource, targetName); ;
				Delegate = new PathOutlineViewDelegate ();

				ReloadData ();

				ExpandItem (ItemAtRow (0));
			}
		}

		public PropertyTreeRoot PropertyTreeRoot
		{
			get { return this.propertyTreeRoot; }
			internal set
			{
				if (this.propertyTreeRoot != value) {
					this.propertyTreeRoot = value;
					if (this.propertyTreeRoot != null) {
						SetItemsSource (this.propertyTreeRoot.Children, this.propertyTreeRoot.TargetType.Name);
					} else {
						SetItemsSource (null, string.Empty);
					}
				}
			}
		}
	}

	internal class PathOutlineViewDelegate : NSOutlineViewDelegate
	{
		private const string PathIdentifier = "path";

		public override NSView GetView (NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
		{
			var labelContainer = (UnfocusableTextField)outlineView.MakeView (PathIdentifier, this);
			if (labelContainer == null) {
				labelContainer = new UnfocusableTextField {
					Identifier = PathIdentifier,
				};
			}
			var target = (item as NSObjectFacade).Target;

			switch (target) {
			case PropertyTreeElement propertyTreeElement:
				labelContainer.StringValue = string.Format ("{0}: ({1})", propertyTreeElement.Property.Name, propertyTreeElement.Property.RealType.Name);
				break;

			case string targetName:
				labelContainer.StringValue = targetName;
				break;

			default:
				labelContainer.StringValue = Properties.Resources.TypeNotSupported;
				break;
			}

			return labelContainer;
		}

		public override bool ShouldSelectItem (NSOutlineView outlineView, NSObject item)
		{
			if (item is NSObjectFacade facade) {
				switch (facade.Target) {
				case PropertyTreeElement propertyTreeElement:
					var propertyTreeResult = propertyTreeElement.Children.Task.Result;
					return propertyTreeResult.Count == 0;

				default:
					return false;
				}
			} else {
				return false;
			}
		}
	}

	internal class PathOutlineViewDataSource : NSOutlineViewDataSource
	{
		private readonly IReadOnlyCollection<object> itemsSource;
		private readonly string targetName;

		internal PathOutlineViewDataSource (IReadOnlyCollection<object> itemsSource, string targetName)
		{
			this.itemsSource = itemsSource;
			this.targetName = targetName;
		}

		public override nint GetChildrenCount (NSOutlineView outlineView, NSObject item)
		{
			if (item == null) {
				return this.itemsSource != null ? this.itemsSource.Count + 1 : 0;
			} else {
				var target = (item as NSObjectFacade).Target;
				switch (target) {
				case PropertyTreeElement propertyTreeElement:
					IReadOnlyCollection<PropertyTreeElement> propertyTrees = propertyTreeElement.Children.Task.Result;
					return propertyTrees.Count;

				case string targetName:
					return this.itemsSource.Count;

				default:
					return 0;
				}
			}
		}

		public override NSObject GetChild (NSOutlineView outlineView, nint childIndex, NSObject item)
		{
			if (childIndex == 0 && item == null) {
				return new NSObjectFacade (targetName);
			}

			if (item is NSObjectFacade objectFacade) {
				var target = objectFacade.Target;
				switch (target) {
				case PropertyTreeElement propertyTreeElement:
					IReadOnlyCollection<PropertyTreeElement> propertyTrees = propertyTreeElement.Children.Task.Result;
					return new NSObjectFacade (propertyTrees.ElementAt ((int)childIndex));

				case string targetName:
					return new NSObjectFacade (this.itemsSource.ElementAt ((int)childIndex));

				default:
					return null;
				}

			}
			return null;
		}

		public override bool ItemExpandable (NSOutlineView outlineView, NSObject item)
		{
			if (item is NSObjectFacade objectFacade) {
				var target = objectFacade.Target;
				switch (target) {
				case PropertyTreeElement propertyTreeElement:
					IReadOnlyCollection<PropertyTreeElement> propertyTrees = propertyTreeElement.Children.Task.Result;
					return propertyTrees.Count > 0;

				case string targetName:
					return true;

				default:
					return false;
				}
			}

			return false;
		}
	}
}