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: 560a69bcb72795edba1303487c407f647634b766 (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
using System;
using AppKit;
using CoreGraphics;
using Foundation;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.Themes;

namespace Xamarin.PropertyEditing.Mac
{
	internal class NumericSpinEditor<T> : NumericSpinEditor
	{
	}

	internal class NumericSpinEditor : NSView, INSAccessibilityGroup
	{
		const int stepperSpace = 2;
		const int stepperWidth = 11;
		const int stepperTopHeight = 9;
		const int stepperBotHeight = 10;

		NumericTextField numericEditor;
		public NumericTextField NumericEditor {
			get { return numericEditor; }
		}

		UpSpinnerButton incrementButton;
		public UpSpinnerButton IncrementButton {
			get { return incrementButton; }
		}

		DownSpinnerButton decrementButton;
		public DownSpinnerButton DecrementButton {
			get { return 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)numericEditor.Cell).PlaceholderString; }
			set { ((NSTextFieldCell)numericEditor.Cell).PlaceholderString = value; }
		}

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

		protected virtual void OnConfigureNumericTextField ()
		{
			numericEditor.Formatter = formatter;
		}

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

		public virtual void Reset ()
		{
		}

		public NumericSpinEditor ()
		{
			TranslatesAutoresizingMaskIntoConstraints = false;
			var controlSize = NSControlSize.Small;

			incrementButton = new UpSpinnerButton ();

			decrementButton = new DownSpinnerButton ();

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

			numericEditor = new NumericTextField {
				Alignment = NSTextAlignment.Right,
				TranslatesAutoresizingMaskIntoConstraints = false,
				Font = NSFont.FromFontName (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize),
				ControlSize = controlSize,
			};

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

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

			numericEditor.ValidatedEditingEnded += (s, e) => {
				OnEditingEnded (s, e);
			};

			AddSubview (numericEditor);
			AddSubview (incrementButton);
			AddSubview (decrementButton);

			this.DoConstraints (new[] {
				numericEditor.ConstraintTo (this, (n, c) => n.Width == c.Width - (stepperWidth + stepperSpace + 1)),
				numericEditor.ConstraintTo (this, (n, c) => n.Height == PropertyEditorControl.DefaultControlHeight - 3),

				incrementButton.ConstraintTo (numericEditor, (s, n) => s.Left == n.Right + stepperSpace),
				incrementButton.ConstraintTo (numericEditor, (s, n) => s.Top == n.Top),
				incrementButton.ConstraintTo (numericEditor, (s, n) => s.Width == stepperWidth),
				incrementButton.ConstraintTo (numericEditor, (s, n) => s.Height == stepperTopHeight),

				decrementButton.ConstraintTo (numericEditor, (s, n) => s.Left == n.Right + stepperSpace),
				decrementButton.ConstraintTo (numericEditor, (s, n) => s.Top == n.Top + stepperTopHeight),
				decrementButton.ConstraintTo (numericEditor, (s, n) => s.Width == stepperWidth),
				decrementButton.ConstraintTo (numericEditor, (s, n) => s.Height == stepperBotHeight),
			});

			PropertyEditorPanel.ThemeManager.ThemeChanged += ThemeManager_ThemeChanged;

			UpdateTheme ();
		}

		protected override void Dispose (bool disposing)
		{
			if (disposing) {
				PropertyEditorPanel.ThemeManager.ThemeChanged -= ThemeManager_ThemeChanged;
			}
		}

		void ThemeManager_ThemeChanged (object sender, EventArgs e)
		{
			UpdateTheme ();
		}

		protected void UpdateTheme ()
		{
			this.Appearance = PropertyEditorPanel.ThemeManager.CurrentAppearance;
		}

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

		void SetValue (string value)
		{
			if (numericEditor.StringValue != value) {
				numericEditor.StringValue = value;
				NotifyingValueChanged (EventArgs.Empty);
			}
		}

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

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

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

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

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

			SetValue (newValue);
		}

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