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: f1d92ac5324494baeccf9968674b0fd0d05c8556 (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
using System;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	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);

			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)
		{
			return new EntryPropertyEditorDelegate<T> (viewModel);
		}

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

	internal class EntryPropertyEditorDelegate<T>
		: NSTextFieldDelegate
	{
		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;
	}
}