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

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

namespace Xamarin.PropertyEditing.Mac
{
	internal class EditorContainer
		: NSView
	{
		public EditorContainer (IHostResourceProvider hostResources, IEditorView editorView)
		{
			if (hostResources == null)
				throw new ArgumentNullException (nameof (hostResources));

			EditorView = editorView;

			AddSubview (this.label);

			this.AddConstraints (new[] {
				NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 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, PropertyEditorControl.DefaultControlHeight),
			});

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

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

		public IEditorView EditorView {
			get;
		}

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

		public NSView LeftEdgeView
		{
			get { return this.leftEdgeView; }
			set
			{
				if (this.leftEdgeView != null) {
					this.leftEdgeView.RemoveFromSuperview ();
					RemoveConstraints (new[] { this.leftEdgeLeftConstraint, this.leftEdgeVCenterConstraint });
					this.leftEdgeLeftConstraint.Dispose ();
					this.leftEdgeLeftConstraint = null;
					this.leftEdgeVCenterConstraint.Dispose ();
					this.leftEdgeVCenterConstraint = null;
				}

				this.leftEdgeView = value;

				if (value != null) {
					AddSubview (value);

					value.TranslatesAutoresizingMaskIntoConstraints = false;
					this.leftEdgeLeftConstraint = NSLayoutConstraint.Create (this.leftEdgeView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 4);
					this.leftEdgeVCenterConstraint = NSLayoutConstraint.Create (this.leftEdgeView, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1, 0);

					AddConstraints (new[] { this.leftEdgeLeftConstraint, this.leftEdgeVCenterConstraint });
				}
			}
		}

		public override void ViewWillMoveToSuperview (NSView newSuperview)
		{
			if (newSuperview == null && EditorView != null)
				EditorView.ViewModel = null;

			base.ViewWillMoveToSuperview (newSuperview);
		}

		private UnfocusableTextField label = new UnfocusableTextField {
			Alignment = NSTextAlignment.Right,
			Cell = {
				LineBreakMode = NSLineBreakMode.TruncatingHead,
			},
			TranslatesAutoresizingMaskIntoConstraints = false,
		};

#if DEBUG // Currently only used to highlight which controls haven't been implemented
		public NSColor LabelTextColor {
			set { this.label.TextColor = value; }
		}
#endif

		private NSView leftEdgeView;
		private NSLayoutConstraint leftEdgeLeftConstraint, leftEdgeVCenterConstraint;
		private readonly IHostResourceProvider hostResources;
		private const float LabelToControlSpacing = 5f;
	}
}