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

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

namespace Xamarin.PropertyEditing.Mac
{
	internal class BindingPathSelectorControl 
		: NotifyingView<CreateBindingViewModel>
	{
		private readonly PathOutlineView pathOutlineView;
		internal const string PathSelectorColumnColId = "PathSelectorColumn";

		public string CustomPath { 
			get { 
				return this.customPathControl.StringValue; 
			} 
		}
		private NSTextField customPathControl { get; }

		private const float HorizontalCustomOffSet = 30.5f;

		private HeaderView pathHeader;
		private NSView pathBox;

		public BindingPathSelectorControl (CreateBindingViewModel viewModel)
		{
			if (viewModel == null)
				throw new ArgumentNullException (nameof (viewModel));

			ViewModel = viewModel;

			TranslatesAutoresizingMaskIntoConstraints = false;

			this.pathBox = new NSView {
				TranslatesAutoresizingMaskIntoConstraints = false,
				WantsLayer = true,

				// Layer out of alphabetical order so that WantsLayer creates the layer first
				Layer = {
					CornerRadius = 1.0f,
					BorderColor = new CGColor (.5f, .5f, .5f, 1.0f),
					BorderWidth = 1,
				},
			};

			AddSubview (this.pathBox);

			this.pathHeader = new HeaderView {
				Title = Properties.Resources.Path,
			};
			this.pathHeader.HorizonalTitleOffset = -HorizontalCustomOffSet;

			this.pathBox.AddSubview (this.pathHeader);

			this.pathBox.AddConstraints (new[] {
				NSLayoutConstraint.Create (this.pathHeader, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.Top, 1f, 0f),
				NSLayoutConstraint.Create (this.pathHeader, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.Left, 1f, 0f),
				NSLayoutConstraint.Create (this.pathHeader, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.Width, 1f, 0f),
				NSLayoutConstraint.Create (this.pathHeader, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, BindingEditorWindow.HeaderHeight),
			});

			var customCheckBox = new NSButton {
				AccessibilityEnabled = true,
				AccessibilityTitle = Properties.Resources.AccessibilityBindingPathSelectorCustomCheckbox,
				ControlSize = NSControlSize.Small,
				Font = NSFont.FromFontName (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize),
				Title = Properties.Resources.Custom,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			customCheckBox.SetButtonType (NSButtonType.Switch);

			this.pathHeader.AddSubview (customCheckBox);
			this.pathHeader.AddConstraints (new[] {
				NSLayoutConstraint.Create (customCheckBox, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, this.pathHeader, NSLayoutAttribute.CenterX, 1, HorizontalCustomOffSet),
				NSLayoutConstraint.Create (customCheckBox, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.pathHeader, NSLayoutAttribute.CenterY, 1, 0f),
			});

			this.customPathControl = new NSTextField {
				AccessibilityEnabled = true,
				AccessibilityTitle = Properties.Resources.AccessibilityBindingPathSelectorCustomPath,
				ControlSize = NSControlSize.Regular,
				Enabled = false,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			var customPathHeightConstraint = NSLayoutConstraint.Create (this.customPathControl, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 0);

			this.pathBox.AddSubview (this.customPathControl);
			this.pathBox.AddConstraints (new[] {
				NSLayoutConstraint.Create (this.customPathControl, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.Top, 1f, 28f),
				NSLayoutConstraint.Create (this.customPathControl, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.Width, 1f, 0f),
				NSLayoutConstraint.Create (this.customPathControl, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.CenterX, 1, 0),
				customPathHeightConstraint,
			});

			// create a table view and a scroll view
			var outlineViewContainer = new NSScrollView {
				AccessibilityEnabled = true,
				AccessibilityTitle = Properties.Resources.AccessibilityBindingPathSelectorPathTable,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			customCheckBox.Activated += (sender, e) => {
				this.customPathControl.Enabled = customCheckBox.State == NSCellStateValue.On;
				customPathHeightConstraint.Constant = this.customPathControl.Enabled ? 22 : 0;
			};

			this.customPathControl.Changed += (sender, e) => {
				viewModel.Path = this.customPathControl.StringValue;
			};

			this.pathOutlineView = new PathOutlineView {

			};

			this.pathOutlineView.Activated += OnPathOutlineViewSelected;

			var pathColumn = new NSTableColumn (PathSelectorColumnColId);
			this.pathOutlineView.AddColumn (pathColumn);

			// Set OutlineTableColumn or the arrows showing children/expansion will not be drawn
			this.pathOutlineView.OutlineTableColumn = pathColumn;

			// add the panel to the window
			outlineViewContainer.DocumentView = this.pathOutlineView;

			this.pathBox.AddSubview (outlineViewContainer);

			this.pathBox.AddConstraints (new[] {
				NSLayoutConstraint.Create (outlineViewContainer, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.customPathControl, NSLayoutAttribute.Bottom, 1f, 0f),
				NSLayoutConstraint.Create (outlineViewContainer, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.Width, 1f, 0f),
				NSLayoutConstraint.Create (outlineViewContainer, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.CenterX, 1, 0),
				NSLayoutConstraint.Create (outlineViewContainer, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.pathBox, NSLayoutAttribute.Bottom, 1f, 0f),
			});

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.pathBox, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 0f),
				NSLayoutConstraint.Create (this.pathBox, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0f),
				NSLayoutConstraint.Create (this.pathBox, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1f, 0f),
				NSLayoutConstraint.Create (this.pathBox, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, 0f),
			});

			viewModel.PropertyChanged += OnPropertyChanged;
		}

		public override async void OnPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			if (e.PropertyName == nameof (CreateBindingViewModel.PropertyRoot)) {
				if (ViewModel.PropertyRoot != null) {
					this.pathOutlineView.PropertyTreeRoot = await ViewModel.PropertyRoot.Task;
				} else {
					this.pathOutlineView.PropertyTreeRoot = null;
				}
			}

			if (e.PropertyName == nameof (CreateBindingViewModel.Path)) {
				this.customPathControl.StringValue = ViewModel.Path ?? string.Empty;
			}
		}

		private void OnPathOutlineViewSelected (object sender, EventArgs e)
		{
			if (sender is PathOutlineView pov) {
				if (pov.SelectedRow != -1) {
					if (pov.ItemAtRow (pov.SelectedRow) is NSObjectFacade facade) {
						switch (facade.Target) {
						case PropertyTreeElement propertyTreeElement:
							ViewModel.SelectedPropertyElement = propertyTreeElement;
							break;

						default:
							break;
						}
					}
				}
			}
		}

	}
}