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

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

namespace Xamarin.PropertyEditing.Mac
{
	internal class PropertyContainer
		: NSView
	{
		public PropertyContainer (IHostResourceProvider hostResources, INativeContainer nativeView, bool includePropertyButton, float vertInset = 0)
		{
			if (hostResources == null)
				throw new ArgumentNullException (nameof (hostResources));

			NativeContainer = nativeView;

			AddSubview (this.label);

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Left, NSLayoutRelation.GreaterThanOrEqual, this, NSLayoutAttribute.Left, 1, 0f),
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, Mac.Layout.GoldenRatioLeft, 0f),
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),
			});

			if (NativeContainer != null) {
				AddSubview (NativeContainer.NativeView);
				NativeContainer.NativeView.TranslatesAutoresizingMaskIntoConstraints = false;

				if (NativeContainer.NativeView is PropertyEditorControl pec && pec.FirstKeyView != null) {
					AddConstraint (NSLayoutConstraint.Create (this.label, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, pec.FirstKeyView, NSLayoutAttribute.CenterY, 1, 0));
				} else {
					AddConstraint (NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, 3));
				}

				AddConstraints (new[] {
					NSLayoutConstraint.Create (NativeContainer.NativeView, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
					NSLayoutConstraint.Create (NativeContainer.NativeView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.label, NSLayoutAttribute.Right, 1f, LabelToControlSpacing),
					NSLayoutConstraint.Create (NativeContainer.NativeView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1f, vertInset)
				});

				if (includePropertyButton) {
					this.propertyButton = new PropertyButton (hostResources) {
						TranslatesAutoresizingMaskIntoConstraints = false
					};

					AddSubview (this.propertyButton);
					AddConstraints (new[] {
						NSLayoutConstraint.Create (this.propertyButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.label, NSLayoutAttribute.CenterY, 1, 0),
						NSLayoutConstraint.Create (NativeContainer.NativeView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.propertyButton, NSLayoutAttribute.Left, 1f, -EditorToButtonSpacing),
						NSLayoutConstraint.Create (this.propertyButton, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, -ButtonToWallSpacing),
						NSLayoutConstraint.Create (this.propertyButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, PropertyButton.DefaultSize),
					});
				} else {
					AddConstraint (NSLayoutConstraint.Create (NativeContainer.NativeView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0f));
				}
			} else {
				AddConstraint (NSLayoutConstraint.Create (this.label, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1, 0));
			}
		}

		public override bool PerformKeyEquivalent (NSEvent theEvent)
		{
			if (theEvent.KeyCode == (ushort)NSKey.I
			&& (theEvent.ModifierFlags & (NSEventModifierMask.ShiftKeyMask | NSEventModifierMask.ControlKeyMask))
			== (NSEventModifierMask.ShiftKeyMask | NSEventModifierMask.ControlKeyMask)) {
				if (theEvent.Window.FirstResponder is NSView fr) {
					var propertyContainer = FindPropertyContainer (fr); // Recursive on SuperView, up the chain
					if (propertyContainer != null) {
						SynchronizationContext.Current.Post (s => {
							propertyContainer.PropertyButton.PopUpContextMenu ();
						}, null);
						return true;
					}
				}
			}

			return base.PerformKeyEquivalent (theEvent);
		}

		private PropertyContainer FindPropertyContainer (NSView fr)
		{
			if (fr.Superview != null) {
				if (fr.Superview is PropertyContainer propertyContainer) {
					return propertyContainer;
				} else {
					return FindPropertyContainer (fr.Superview);
				}
			} else {
				return null;
			}
		}

		public string Label
		{
			get { return this.label.StringValue; }
			set { this.label.StringValue = value + ":"; }
		}

		protected INativeContainer NativeContainer
		{
			get;
		}

		protected UnfocusableTextField LabelControl => this.label;

		protected PropertyButton PropertyButton => this.propertyButton;

		internal const float LabelToControlSpacing = 8f;
		internal static float PropertyTotalWidth => PropertyButton.DefaultSize + ButtonToWallSpacing + EditorToButtonSpacing;

		private const float EditorToButtonSpacing = 4f;
		private const float ButtonToWallSpacing = 17f;

		private readonly PropertyButton propertyButton;
		private readonly UnfocusableTextField label = new UnfocusableTextField {
			Alignment = NSTextAlignment.Right,
			Cell = {
				LineBreakMode = NSLineBreakMode.TruncatingMiddle,
			},
			TranslatesAutoresizingMaskIntoConstraints = false,
		};
	}
}