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

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

namespace Xamarin.PropertyEditing.Mac
{
	internal class CollectionInlineEditorControl
		: PropertyEditorControl<CollectionPropertyViewModel>
	{
		const int DefaultDelayTime = 500;

		public CollectionInlineEditorControl (IHostResourceProvider hostResources)
			: base (hostResources)
		{
			this.label = new UnfocusableTextField {
				TranslatesAutoresizingMaskIntoConstraints = false,
				StringValue = Properties.Resources.CollectionValue,
			};

			AddSubview (this.label);

			this.openCollection = new FocusableButton {
				Title = Properties.Resources.CollectionEditButton,
				BezelStyle = NSBezelStyle.Rounded,
				AccessibilityEnabled = true,
				ProxyResponder = new ProxyResponder (this, ProxyRowType.SingleView),
				AccessibilityHelp = Properties.Resources.AccessibilityCollectionHelp
			};

			this.openCollection.Activated += (o, e) => {
				var parentWindow = Window;
				
				var w = new CollectionEditorWindow (hostResources, ViewModel) {
					Appearance = EffectiveAppearance
				};

				var result = (NSModalResponse)(int)NSApplication.SharedApplication.RunModalForWindow (w);

				//after run modal our FocusedWindow is null, we set the parent again
				parentWindow?.MakeKeyAndOrderFront (parentWindow);

				if (result != NSModalResponse.OK)
					ViewModel.CancelCommand.Execute (null);
				else
					ViewModel.CommitCommand.Execute (null);

				//small hack to override vs4mac default focus
				System.Threading.Tasks.Task.Delay (DefaultDelayTime)
				.ContinueWith (t => {
					parentWindow?.MakeFirstResponder (openCollection);
				}, TaskScheduler.FromCurrentSynchronizationContext ());
			};

			AddSubview (this.openCollection);

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 0),
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1, 0),
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, 0),
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.openCollection, NSLayoutAttribute.Left, 1, -4),

				NSLayoutConstraint.Create (this.openCollection, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0),
				NSLayoutConstraint.Create (this.openCollection, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1, 0),
				NSLayoutConstraint.Create (this.openCollection, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1, DefaultButtonWidth),
			});

			AppearanceChanged ();
		}

		public override NSView FirstKeyView => this.openCollection;
		public override NSView LastKeyView => this.openCollection;

		protected override void SetEnabled ()
		{
			base.SetEnabled ();
			this.openCollection.Enabled = ViewModel?.Property.CanWrite ?? false;
		}

		protected override void UpdateAccessibilityValues ()
		{
			base.UpdateAccessibilityValues ();

			this.openCollection.AccessibilityTitle = (ViewModel != null) ? String.Format (Properties.Resources.AccessibilityCollection, ViewModel.Property.Name) : null;
		}

		private readonly UnfocusableTextField label;
		private readonly NSButton openCollection;

		protected override void AppearanceChanged ()
		{
			base.AppearanceChanged ();

			this.label.TextColor = HostResources.GetNamedColor (NamedResources.DescriptionLabelColor);
		}
	}
}