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

NumericSpinEditor.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de6f661336fbb44d9caf0aca66b9ef03f76cba17 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;

using AppKit;
using CoreGraphics;
using Foundation;
using ObjCRuntime;

namespace Xamarin.PropertyEditing.Mac
{
	internal class NumericSpinEditor<T>
		: NumericSpinEditor
	{
		public NumericSpinEditor (IHostResourceProvider hostResources)
			: base (hostResources)
		{
		}
	}

	internal class NumericSpinEditor
		: NSView, INSAccessibilityGroup
	{
		private NumericTextField numericEditor;
		public NumericTextField NumericEditor {
			get { return this.numericEditor; }
		}

		private SpinnerButton incrementButton;
		public SpinnerButton IncrementButton {
			get { return this.incrementButton; }
		}

		private SpinnerButton decrementButton;
		public SpinnerButton DecrementButton {
			get { return this.decrementButton; }
		}

		protected bool editing;

		public event EventHandler ValueChanged;
		public event EventHandler EditingEnded;

		public ValidationType NumericMode {
			get { return numericEditor.NumericMode; }
			set {
				this.numericEditor.NumericMode = value;
				Reset ();
			}
		}

		public string PlaceholderString {
			get { return ((NSTextFieldCell)this.numericEditor.Cell).PlaceholderString; }
			set { ((NSTextFieldCell)this.numericEditor.Cell).PlaceholderString = value; }
		}

		public override CGSize IntrinsicContentSize {
			get {
				var baseSize = this.numericEditor.IntrinsicContentSize;
				return new CGSize (baseSize.Width + 20, baseSize.Height);
			}
		}

		public NSColor BackgroundColor {
			get { return this.numericEditor.BackgroundColor; }
			set { this.numericEditor.BackgroundColor = value; }
		}

		public override nfloat BaselineOffsetFromBottom {
			get { return this.numericEditor.BaselineOffsetFromBottom; }
		}

		public int Digits {
			get { return (int)this.formatter.MaximumFractionDigits; }
			set { this.formatter.MaximumFractionDigits = value; }
		}

		public double Value {
			get { return this.numericEditor.DoubleValue; }
			set { SetValue (value); }
		}

		public string StringValue
		{
			get { return this.numericEditor.StringValue; }
			set { SetValue (value); }
		}

		public double MinimumValue {
			get { return this.formatter.Minimum.DoubleValue; }
			set { this.formatter.Minimum = new NSNumber (value); }
		}

		public double MaximumValue {
			get { return this.formatter.Maximum.DoubleValue; }
			set { this.formatter.Maximum = new NSNumber (value); }
		}

		private double incrementValue = 1.0f;
		public double IncrementValue {
			get { return this.incrementValue; }
			set { this.incrementValue = value; }
		}

		public string FocusedFormat
		{
			get { return this.numericEditor?.FocusedFormat; }
			set { this.numericEditor.FocusedFormat = value; }
		}

		public string DisplayFormat
		{
			get { return this.numericEditor?.DisplayFormat; }
			set { this.numericEditor.DisplayFormat = value; }
		}

		public bool Enabled {
			get { return this.numericEditor.Enabled; }
			set {
				this.numericEditor.Enabled = value;
				this.incrementButton.Enabled = value;
				this.decrementButton.Enabled = value;
			}
		}

		private NSNumberFormatter formatter;
		public NSNumberFormatter Formatter {
			get { return this.formatter; }
			set {
				this.formatter = value;
				this.numericEditor.Formatter = this.formatter;
			}
		}

		public bool IsIndeterminate {
			get { return !string.IsNullOrEmpty (this.numericEditor.StringValue); }
			set {
				if (value)
					this.numericEditor.StringValue = string.Empty;
			}
		}

		public bool Editable {
			get { return this.numericEditor.Editable; }
			set {
				this.numericEditor.Editable = value;
				this.incrementButton.Enabled = value;
				this.decrementButton.Enabled = value;
			}
		}

		public NSNumberFormatterStyle NumberStyle {
			get { return this.formatter.NumberStyle; }
			set { this.formatter.NumberStyle = value; }
		}

		public bool AllowRatios {
			get { return this.numericEditor.AllowRatios; }
			set { this.numericEditor.AllowRatios = value; }
		}

		public bool AllowNegativeValues {
			get { return this.numericEditor.AllowNegativeValues; }
			set { this.numericEditor.AllowNegativeValues = value; }
		}

		public override bool AccessibilityEnabled {
			get { return this.numericEditor.AccessibilityEnabled; }
			set { this.numericEditor.AccessibilityEnabled = value; }
		}

		public override string AccessibilityTitle {
			get { return this.numericEditor.AccessibilityTitle; }
			set { this.numericEditor.AccessibilityTitle = value; }
		}

		public virtual void Reset ()
		{
		}

		public NumericSpinEditor (IHostResourceProvider hostResources)
		{
			if (hostResources == null)
				throw new ArgumentNullException (nameof (hostResources));

			this.hostResources = hostResources;
			TranslatesAutoresizingMaskIntoConstraints = false;

			this.incrementButton = new SpinnerButton (this.hostResources, isUp: true) {
				TranslatesAutoresizingMaskIntoConstraints = false
			};

			this.decrementButton = new SpinnerButton (this.hostResources, isUp: false) {
				TranslatesAutoresizingMaskIntoConstraints = false
			};

			this.formatter = new NSNumberFormatter {
				FormatterBehavior = NSNumberFormatterBehavior.Version_10_4,
				Locale = NSLocale.CurrentLocale,
				MaximumFractionDigits = 15,
				Maximum = double.MaxValue,
				Minimum = double.MinValue,
				NumberStyle = NSNumberFormatterStyle.Decimal,
				UsesGroupingSeparator = false 
			};

			if (DisplayFormat != null)
				this.formatter.PositiveFormat = DisplayFormat;

			this.numericEditor = new NumericTextField {
				Alignment = NSTextAlignment.Right,
				TranslatesAutoresizingMaskIntoConstraints = false,
				Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
				ControlSize = NSControlSize.Small,
				Formatter = this.formatter
			};

			this.incrementButton.OnMouseLeftDown += (sender, e) => { IncrementNumericValue (); };
			this.decrementButton.OnMouseLeftDown += (sender, e) => { DecrementNumericValue (); };

			this.numericEditor.KeyArrowUp += (sender, e) => { IncrementNumericValue (e); };
			this.numericEditor.KeyArrowDown += (sender, e) => { DecrementNumericValue (e); };

			this.numericEditor.ValidatedEditingEnded += OnEditingEnded;

			AddSubview (this.numericEditor);
			AddSubview (this.incrementButton);
			AddSubview (this.decrementButton);

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.numericEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0),
				NSLayoutConstraint.Create (this.numericEditor, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),

				NSLayoutConstraint.Create (this.incrementButton, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.numericEditor, NSLayoutAttribute.Top, 1f, 0f),
				NSLayoutConstraint.Create (this.incrementButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.numericEditor, NSLayoutAttribute.Right, 1f, StepperSpace),
				NSLayoutConstraint.Create (this.incrementButton, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0),
				NSLayoutConstraint.Create (this.incrementButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, StepperWidth),
				NSLayoutConstraint.Create (this.incrementButton, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, StepperTopHeight),

				NSLayoutConstraint.Create (this.decrementButton, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.numericEditor,  NSLayoutAttribute.Top, 1f, StepperTopHeight),
				NSLayoutConstraint.Create (this.decrementButton, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0),
				NSLayoutConstraint.Create (this.decrementButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.numericEditor,  NSLayoutAttribute.Right, 1f, StepperSpace),
				NSLayoutConstraint.Create (this.decrementButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, StepperWidth),
				NSLayoutConstraint.Create (this.decrementButton, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, StepperBotHeight),
			});
		}

		virtual protected void OnEditingEnded (object sender, EventArgs e)
		{
			if (!this.editing) {
				this.editing = true;
				SetValue (numericEditor.StringValue);
				EditingEnded?.Invoke (this, EventArgs.Empty);
				NotifyingValueChanged (EventArgs.Empty);
				this.editing = false;
			}
		}

		void SetValue (string value)
		{
			if (this.numericEditor.StringValue != value) {
				this.numericEditor.StringValue = value;
			}
		}

		public void SetValue (double value)
		{
			SetValue (value.ToString ());
		}

		protected void NotifyingValueChanged (EventArgs eventArgs)
		{
			ValueChanged?.Invoke (this, eventArgs);
		}

		public void IncrementNumericValue (bool shiftPressed = false)
		{
			if (!this.editing) {
				this.editing = true;
				SetIncrementOrDecrementValue (shiftPressed ? 10 * this.incrementValue : this.incrementValue);
				this.editing = false;
			}
		}

		public void DecrementNumericValue (bool shiftPressed = false)
		{
			if (!editing) {
				this.editing = true;
				SetIncrementOrDecrementValue (-(shiftPressed ? 10 * this.incrementValue : this.incrementValue));
				this.editing = false;
			}
		}

		virtual protected void SetIncrementOrDecrementValue (double incDevValue)
		{
			// Constrain our value to our Min/Max before we set it
			var newValue = Clamp (this.numericEditor.DoubleValue + incDevValue);

			SetValue (newValue);
			NotifyingValueChanged (EventArgs.Empty);
		}

		public double Clamp (double value)
		{
			return (double)Decimal.Round ((decimal)(value < MinimumValue ? MinimumValue : value > MaximumValue ? MaximumValue : value), Digits);
		}

		private const int StepperSpace = 2;
		private const int StepperWidth = 11;
		private const int StepperTopHeight = 9;
		private const int StepperBotHeight = 10;

		private readonly IHostResourceProvider hostResources;
	}
}