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

ObjectEditorControl.cs « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ab9749bb6e8aa462a7e241e855945f58741d6f56 (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;
using System.ComponentModel;
using System.Threading.Tasks;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class ObjectEditorControl
		: PropertyEditorControl<ObjectPropertyViewModel>
	{
		public ObjectEditorControl (IHostResourceProvider hostResources)
			: base (hostResources)
		{
			this.typeLabel = new UnfocusableTextField {
				TranslatesAutoresizingMaskIntoConstraints = false
			};
			AddSubview (this.typeLabel);

			this.createObject = new NSButton {
				Title = Properties.Resources.New,
				TranslatesAutoresizingMaskIntoConstraints = false,
				BezelStyle = NSBezelStyle.Rounded
			};
			this.createObject.Activated += OnNewPressed;
			AddSubview (this.createObject);

			this.buttonConstraint = NSLayoutConstraint.Create (this.createObject, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this.typeLabel, NSLayoutAttribute.Trailing, 1f, 12);

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1f, 0f),
				NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
				NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, 0),
				this.buttonConstraint,
				NSLayoutConstraint.Create (this.createObject, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1, 0).WithPriority (NSLayoutPriority.DefaultLow),
				NSLayoutConstraint.Create (this.createObject, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
				NSLayoutConstraint.Create (this.createObject, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1f, 70f),
			});
		}

		public override NSView FirstKeyView => this.createObject;

		public override NSView LastKeyView => this.createObject;

		protected override void UpdateValue ()
		{
		}

		protected override void UpdateErrorsDisplayed (IEnumerable errors)
		{
		}

		protected override void HandleErrorsChanged (object sender, DataErrorsChangedEventArgs e)
		{
		}

		protected override void SetEnabled ()
		{
			this.createObject.Enabled = ViewModel.Property.CanWrite;
		}

		protected override void UpdateAccessibilityValues ()
		{
			this.createObject.AccessibilityTitle = String.Format (Properties.Resources.NewInstanceForProperty, ViewModel.Property.Name);
		}

		protected override void OnViewModelChanged (PropertyViewModel oldModel)
		{
			base.OnViewModelChanged (oldModel);

			if (oldModel is ObjectPropertyViewModel ovm) {
				ovm.TypeRequested -= OnTypeRequested;
				ovm.CreateInstanceCommand.CanExecuteChanged -= OnCreateInstanceExecutableChanged;
			}

			if (ViewModel != null) {
				ViewModel.TypeRequested += OnTypeRequested;
				ViewModel.CreateInstanceCommand.CanExecuteChanged += OnCreateInstanceExecutableChanged;

				OnPropertyChanged (ViewModel, new PropertyChangedEventArgs (null));
			}
		}

		protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			switch (e.PropertyName) {
			case nameof (ObjectPropertyViewModel.ValueType):
				UpdateTypeLabel ();
				break;
			case null:
			case "":
				UpdateTypeLabel ();
				UpdateCreateInstanceCommand ();
				break;
			}

			base.OnPropertyChanged (sender, e);
		}

		private readonly UnfocusableTextField typeLabel;
		private readonly NSButton createObject;
		private readonly NSLayoutConstraint buttonConstraint;

		private void OnCreateInstanceExecutableChanged (object sender, EventArgs e)
		{
			UpdateCreateInstanceCommand ();
		}

		private void OnTypeRequested (object sender, TypeRequestedEventArgs e)
		{
			var tcs = new TaskCompletionSource<ITypeInfo> ();
			e.SelectedType = tcs.Task;

			var vm = new TypeSelectorViewModel (ViewModel.AssignableTypes);
			var selector = new TypeSelectorControl {
				ViewModel = vm,
				Appearance = EffectiveAppearance
			};

			vm.PropertyChanged += (vms, ve) => {
				if (ve.PropertyName == nameof (TypeSelectorViewModel.SelectedType)) {
					tcs.TrySetResult (vm.SelectedType);
				}
			};

			var popover = new NSPopover {
				Behavior = NSPopoverBehavior.Transient,
				Delegate = new PopoverDelegate<ITypeInfo> (tcs),
				ContentViewController = new NSViewController {
					View = selector,
					PreferredContentSize = new CoreGraphics.CGSize (360, 335)
				},
			};

			popover.SetAppearance (HostResources.GetVibrantAppearance (EffectiveAppearance));

			tcs.Task.ContinueWith (t => {
				popover.PerformClose (popover);
				popover.Dispose ();
			}, TaskScheduler.FromCurrentSynchronizationContext());

			popover.Show (this.createObject.Frame, this, NSRectEdge.MinYEdge);
		}

		private void UpdateTypeLabel ()
		{
			if (ViewModel.ValueType == null) {
				this.typeLabel.StringValue = String.Empty;
				this.buttonConstraint.Active = false;
			} else {
				this.typeLabel.StringValue = $"({ViewModel.ValueType.Name})";
				this.buttonConstraint.Active = true;
			}
		}

		private void UpdateCreateInstanceCommand()
		{
			this.createObject.Enabled = ViewModel.CreateInstanceCommand.CanExecute (null);
		}

		private void OnNewPressed (object sender, EventArgs e)
		{
			ViewModel.CreateInstanceCommand.Execute (null);
		}

		private class PopoverDelegate<T>
			: NSPopoverDelegate
		{
			public PopoverDelegate (TaskCompletionSource<T> tcs)
			{
				this.tcs = tcs;
			}

			public override void WillClose (NSNotification notification)
			{
				this.tcs.TrySetCanceled ();
			}

			private readonly TaskCompletionSource<T> tcs;
		}
	}
}