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

NumericTextField.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5293d7be34b56dedc79884b39cc8a4e90aa6168 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using AppKit;
using CoreGraphics;
using Foundation;
using ObjCRuntime;

namespace Xamarin.PropertyEditing.Mac
{
	internal class NumericTextField : PropertyTextField
	{
		public override bool CanBecomeKeyView { get { return Enabled; } }

		private NSText CachedCurrentEditor {
			get; set;
		}

		private string cachedValueString;

		public bool AllowNegativeValues {
			get; set;
		}

		public bool AllowRatios {
			get; set;
		}

		public ValidationType NumericMode {
			get; set;
		}

		public string FocusedFormat {
			get; set;
		}

		public string DisplayFormat
		{
			get; set;
		}

		public event EventHandler<bool> KeyArrowUp;
		public event EventHandler<bool> KeyArrowDown;
		public event EventHandler ValidatedEditingEnded;

		public NumericTextField ()
		{
			AllowNegativeValues = true;

			var keyUpDownDelegate = new KeyUpDownDelegate ();
			keyUpDownDelegate.KeyArrowUp += (sender, e) => { OnKeyArrowUp (e); };
			keyUpDownDelegate.KeyArrowDown += (sender, e) => { OnKeyArrowDown (e); };
			Delegate = keyUpDownDelegate;
		}

		public RowProxyResponder ProxyResponder
		{
			get {
				if (Delegate is KeyUpDownDelegate keydown) {
					return keydown.ProxyResponder;
				}
				return null;
			}
			set
			{
				if (Delegate is KeyUpDownDelegate keydown) {
					keydown.ProxyResponder = value;
				}
			}
		}

		public override CGSize IntrinsicContentSize => new CGSize(30, 20);

		public override bool ShouldBeginEditing (NSText textObject)
		{
			CachedCurrentEditor = textObject;
			this.cachedValueString = textObject.Value;

			if (AllowRatios)
				CachedCurrentEditor.Delegate = new RatioValidateDelegate (this);
			else {
				CachedCurrentEditor.Delegate = new NumericValidationDelegate (this);
			}
			return true;
		}

		protected virtual void OnKeyArrowUp (bool shiftPressed)
		{
			KeyArrowUp?.Invoke (this, shiftPressed);
		}

		protected virtual void OnKeyArrowDown (bool shiftPressed)
		{
			KeyArrowDown?.Invoke (this, shiftPressed);
		}

		public virtual void NotifyValidatedEditingEnded ()
		{
			ValidatedEditingEnded?.Invoke (this, EventArgs.Empty);
		}

		public virtual void ResetInvalidInput ()
		{
			this.StringValue = this.cachedValueString;
		}

		public static bool CheckIfNumber (string finalString, ValidationType mode, bool allowNegativeValues)
		{
			return mode == ValidationType.Decimal ?
				ValidateDecimal (finalString, allowNegativeValues) :
				ValidateInteger (finalString, allowNegativeValues);
		}

		public static bool ValidateDecimal (string finalString, bool allowNegativeValues)
		{
			//Checks parsing to number
			if (!double.TryParse (finalString, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentUICulture, out var value))
				return false;
			//Checks if needs to be possitive value
			if (!allowNegativeValues && value < 0)
				return false;
			//Checks a common validation
			return ViewModels.RatioViewModel.IsValidInput (finalString);
		}

		public static bool ValidateInteger (string finalString, bool allowNegativeValues)
		{
			//Checks parsing to number
			if (!int.TryParse (finalString, out var value))
				return false;
			//Checks if needs to be possitive value
			if (!allowNegativeValues && value < 0)
				return false;
			//Checks a common validation
			return ViewModels.RatioViewModel.IsValidInput (finalString);
		}

		public static bool CheckIfRatio (string finalString, ValidationType mode, bool allowNegativeValues)
		{
			var parts = finalString.Split (ViewModels.RatioViewModel.SplitSeparators, StringSplitOptions.RemoveEmptyEntries);
			if (parts.Length == 2) {
				bool parsed = true;

				if (!CheckIfNumber (parts[0], mode, allowNegativeValues))
					parsed = false;

				if (!String.IsNullOrEmpty (parts[1]) && !CheckIfNumber (parts[1], mode, allowNegativeValues))
					parsed = false;

				if (parsed)
					return true;
			} else if (parts.Length == 1) { // We have a potential whole number, let's make sure
				if (CheckIfNumber (parts[0], mode, allowNegativeValues)) {
					return true;
				}
			}
			return false;
		}

		public override bool BecomeFirstResponder ()
		{
			if (FocusedFormat != null && Formatter is NSNumberFormatter numberFormatter) {
				numberFormatter.PositiveFormat = FocusedFormat;
			}
			return base.BecomeFirstResponder ();
		}

		public override void DidEndEditing (NSNotification notification)
		{
			if (DisplayFormat != null && Formatter is NSNumberFormatter numberFormatter) {
				numberFormatter.PositiveFormat = DisplayFormat;
			}
			base.DidEndEditing (notification);
		}
	}

	internal class KeyUpDownDelegate : TextNextResponderDelegate
	{
		public event EventHandler<bool> KeyArrowUp;
		public event EventHandler<bool> KeyArrowDown;

		public override bool DoCommandBySelector (NSControl control, NSTextView textView, Selector commandSelector)
		{
			if (!base.DoCommandBySelector(control, textView, commandSelector))
			{
				switch (commandSelector.Name) {
				case "moveUp:":
					OnKeyArrowUp ();
					break;
				case "moveDown:":
					OnKeyArrowDown ();
					break;
				case "moveUpAndModifySelection:":
					OnKeyArrowUp (true);
					break;
				case "moveDownAndModifySelection:":
					OnKeyArrowDown (true);
					break;
				default:
					return false;
				}
			}

			return true;
		}

		protected virtual void OnKeyArrowUp (bool shiftPressed = false)
		{
			KeyArrowUp?.Invoke (this, shiftPressed);
		}

		protected virtual void OnKeyArrowDown (bool shiftPressed = false)
		{
			KeyArrowDown?.Invoke (this, shiftPressed);
		}
	}

	internal abstract class TextViewValidationDelegate : NSTextViewDelegate
	{
		protected NumericTextField TextField {
			get; set;
		}

		public TextViewValidationDelegate (NumericTextField textField)
		{
			TextField = textField;
		}

		public override bool TextShouldBeginEditing (NSText textObject)
		{
			return TextField.ShouldBeginEditing (textObject);
		}

		public override bool TextShouldEndEditing (NSText textObject)
		{
			bool shouldEndEditing = false;
			if (!ValidateFinalString (textObject.Value)) {
				TextField.ResetInvalidInput ();
				AppKitFramework.NSBeep ();
				TextField.ShouldEndEditing (textObject);
			} else {
				shouldEndEditing = TextField.ShouldEndEditing (textObject);
			}

			return shouldEndEditing;
		}

		public override void TextDidEndEditing (NSNotification notification)
		{
			TextField.NotifyValidatedEditingEnded ();
			TextField.DidEndEditing (notification);
		}

		protected abstract bool ValidateFinalString (string value);
	}

	internal class NumericValidationDelegate : TextViewValidationDelegate
	{
		public NumericValidationDelegate (NumericTextField textField)
			: base (textField)
		{

		}

		protected override bool ValidateFinalString (string value)
		{
			return TextField.NumericMode == ValidationType.Decimal ?
				            NumericTextField.ValidateDecimal (value, TextField.AllowNegativeValues) :
				            NumericTextField.ValidateInteger (value, TextField.AllowNegativeValues);
		}
	}

	internal class RatioValidateDelegate : TextViewValidationDelegate
	{
		public RatioValidateDelegate (NumericTextField textField)
			: base (textField)
		{

		}

		protected override bool ValidateFinalString (string value)
		{
			if (NumericTextField.CheckIfRatio (value, ValidationType.Decimal, false) || NumericTextField.CheckIfNumber (value, ValidationType.Decimal, false))
				return true;
			return false;
		}
	}
}