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

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

namespace Xamarin.PropertyEditing.Mac
{
	class DelegatedRowTextFieldDelegate : NSTextFieldDelegate
	{
		public ProxyResponder ProxyResponder { get; set; }

		public override bool DoCommandBySelector (NSControl control, NSTextView textView, Selector commandSelector)
		{
			if (ProxyResponder != null)
			{
				switch (commandSelector.Name) {
				case "insertTab:":
					if (ProxyResponder.NextResponder ()) {
						return true;
					}
					break;
				case "insertBacktab:":
					if (ProxyResponder.PreviousResponder ()) {
						return true;
					}
					break;
				}
			} 
			return false;
		}
	}

	internal abstract class EntryPropertyEditor<T>
		: PropertyEditorControl<PropertyViewModel<T>>
	{
		public EntryPropertyEditor (IHostResourceProvider hostResources)
			: base (hostResources)
		{
			Entry = new PropertyTextField {
				BackgroundColor = NSColor.Clear,
				ControlSize = NSControlSize.Small,
				Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
				TranslatesAutoresizingMaskIntoConstraints = false,
			};
			AddSubview (Entry);

			Entry.Delegate = new DelegatedRowTextFieldDelegate ()
			{
				ProxyResponder = new ProxyResponder(this, ProxyRowType.SingleView)
			};

			RightEdgeConstraint = NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0);
			AddConstraints (new[] {
				NSLayoutConstraint.Create (Entry, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this,  NSLayoutAttribute.CenterY, 1f, 0),
				NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 0),
				RightEdgeConstraint,
				NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, -6),
			});
		}

		public override NSView FirstKeyView => Entry;
		public override NSView LastKeyView => Entry;

		protected PropertyTextField Entry
		{
			get;
		}

		protected NSLayoutConstraint RightEdgeConstraint { get; }

		protected override void OnViewModelChanged (PropertyViewModel oldModel)
		{
			base.OnViewModelChanged (oldModel);
			Entry.Delegate = (ViewModel != null) ? CreateDelegate (ViewModel) : null;
		}

		protected override void UpdateValue ()
		{
			Entry.StringValue = GetValue (ViewModel.Value) ?? String.Empty;
		}

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

		protected override void UpdateAccessibilityValues ()
		{
			Entry.AccessibilityEnabled = Entry.Enabled;
		}

		protected virtual EntryPropertyEditorDelegate<T> CreateDelegate (PropertyViewModel<T> viewModel)
		{
			var propertyEditorDelegate = new EntryPropertyEditorDelegate<T> (viewModel)
			{
				ProxyResponder = new ProxyResponder (this, ProxyRowType.SingleView)
			};
			return propertyEditorDelegate;
		}

		protected virtual string GetValue (T value)
		{
			return value?.ToString ();
		}
	}

	internal class EntryPropertyEditorDelegate<T>
		: DelegatedRowTextFieldDelegate
	{
		public EntryPropertyEditorDelegate (PropertyViewModel<T> viewModel)
		{
			if (viewModel == null)
				throw new ArgumentNullException (nameof (viewModel));

			ViewModel = viewModel;
		}

		protected PropertyViewModel<T> ViewModel
		{
			get;
		}

		public override void EditingBegan (NSNotification notification)
		{
			NSTextField text = (NSTextField)notification.Object;
			this.lastValid = text.StringValue;
		}

		public override void EditingEnded (NSNotification notification)
		{
			var text = (NSTextField)notification.Object;
			ViewModel.Value = GetValue (text.StringValue);
		}

		public override bool TextShouldEndEditing (NSControl control, NSText fieldEditor)
		{
			if (!CanGetValue (fieldEditor.Value)) {
				NSTextField text = (NSTextField)control;
				text.StringValue = this.lastValid;
				AppKitFramework.NSBeep ();
				return false;
			}

			return true;
		}

		protected virtual T GetValue (string value)
		{
			if (String.IsNullOrEmpty (value))
				return default (T);

			return (T)Convert.ChangeType (value, typeof(T));
		}

		protected virtual bool CanGetValue (string value)
		{
			return true;
		}

		private string lastValid;
	}
}