-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathNumericStepper.hx
More file actions
1477 lines (1232 loc) · 45.2 KB
/
NumericStepper.hx
File metadata and controls
1477 lines (1232 loc) · 45.2 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Feathers UI
Copyright 2026 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.controls;
import feathers.controls.Button;
import feathers.controls.IRange;
import feathers.controls.TextInput;
import feathers.core.FeathersControl;
import feathers.core.IStageFocusDelegate;
import feathers.core.InvalidationFlag;
import feathers.events.FeathersEvent;
import feathers.layout.Direction;
import feathers.layout.HorizontalAlign;
import feathers.layout.Measurements;
import feathers.utils.AbstractDisplayObjectFactory;
import feathers.utils.DisplayObjectFactory;
import feathers.utils.ExclusivePointer;
import feathers.utils.MathUtil;
import openfl.display.InteractiveObject;
import openfl.display.Stage;
import openfl.errors.ArgumentError;
import openfl.events.Event;
import openfl.events.FocusEvent;
import openfl.events.KeyboardEvent;
import openfl.events.MouseEvent;
import openfl.events.TimerEvent;
import openfl.events.TouchEvent;
import openfl.ui.Keyboard;
import openfl.utils.Timer;
#if air
import openfl.ui.Multitouch;
#end
/**
Select a value between a minimum and a maximum by using increment and
decrement buttons or typing in a value in a text input.
The following example sets the stepper's range and listens for when
the value changes:
```haxe
var stepper = new NumericStepper();
stepper.minimum = 0.0;
stepper.maximum = 100.0;
stepper.step = 1.0;
stepper.value = 12.0;
stepper.addEventListener(Event.CHANGE, stepper_changeHandler);
addChild(stepper);
```
@event openfl.events.Event.CHANGE Dispatched when `NumericStepper.value`
changes.
@see [How to use the NumericStepper component](https://feathersui.com/learn/haxe-openfl/numeric-stepper/)
@since 1.0.0
**/
@:event(openfl.events.Event.CHANGE)
class NumericStepper extends FeathersControl implements IRange implements IStageFocusDelegate {
private static final INVALIDATION_FLAG_DECREMENT_BUTTON_FACTORY = InvalidationFlag.CUSTOM("decrementButtonFactory");
private static final INVALIDATION_FLAG_INCREMENT_BUTTON_FACTORY = InvalidationFlag.CUSTOM("incrementButtonFactory");
private static final INVALIDATION_FLAG_TEXT_INPUT_FACTORY = InvalidationFlag.CUSTOM("textInputFactory");
/**
The variant used to style the decrement `Button` child component in a theme.
To override this default variant, set the
`NumericStepper.customDecrementButtonVariant` property.
@see `NumericStepper.customDecrementButtonVariant`
@see `feathers.style.IVariantStyleObject.variant`
@see [Feathers UI User Manual: Themes](https://feathersui.com/learn/haxe-openfl/themes/)
@since 1.0.0
**/
public static final CHILD_VARIANT_DECREMENT_BUTTON = "numericStepper_decrementButton";
/**
The variant used to style the increment `Button` child component in a theme.
To override this default variant, set the
`NumericStepper.customIncrementButtonVariant` property.
@see `NumericStepper.customIncrementButtonVariant`
@see `feathers.style.IVariantStyleObject.variant`
@see [Feathers UI User Manual: Themes](https://feathersui.com/learn/haxe-openfl/themes/)
@since 1.0.0
**/
public static final CHILD_VARIANT_INCREMENT_BUTTON = "numericStepper_incrementButton";
/**
The variant used to style the `TextInput` child component in a theme.
To override this default variant, set the
`NumericStepper.customTextInputVariant` property.
@see `NumericStepper.customTextInputVariant`
@see `feathers.style.IVariantStyleObject.variant`
@see [Feathers UI User Manual: Themes](https://feathersui.com/learn/haxe-openfl/themes/)
@since 1.0.0
**/
public static final CHILD_VARIANT_TEXT_INPUT = "numericStepper_textInput";
private static final defaultDecrementButtonFactory = DisplayObjectFactory.withClass(Button);
private static final defaultIncrementButtonFactory = DisplayObjectFactory.withClass(Button);
private static final defaultTextInputFactory = DisplayObjectFactory.withClass(TextInput);
/**
Creates a new `NumericStepper` object with the given arguments.
@since 1.0.0
**/
public function new(value:Float = 0.0, minimum:Float = 0.0, maximum:Float = 1.0, ?changeListener:(Event) -> Void) {
initializeNumericStepperTheme();
super();
this.addEventListener(FocusEvent.FOCUS_IN, numericStepper_focusInHandler);
this.minimum = minimum;
this.maximum = maximum;
this.value = value;
if (changeListener != null) {
this.addEventListener(Event.CHANGE, changeListener);
}
}
private var decrementButton:Button;
private var incrementButton:Button;
private var textInput:TextInput;
private var decrementButtonMeasurements = new Measurements();
private var incrementButtonMeasurements = new Measurements();
private var textInputMeasurements = new Measurements();
private var _isDefaultValue = true;
private var _value:Float = 0.0;
/**
The value of the stepper, which must be between the `minimum` and the
`maximum`.
When the `value` property changes, the stepper will dispatch an event of
type `Event.CHANGE`.
In the following example, the value is changed to `12.0`:
```haxe
stepper.minimum = 0.0;
stepper.maximum = 100.0;
stepper.step = 1.0;
stepper.value = 12.0;
```
@default 0.0
@see `NumericStepper.minimum`
@see `NumericStepper.maximum`
@see `NumericStepper.step`
@see [`openfl.events.Event.CHANGE`](https://api.openfl.org/openfl/events/Event.html#CHANGE)
@since 1.0.0
**/
@:bindable("change")
@:inspectable(defaultValue = "0.0")
public var value(get, set):Float;
private function get_value():Float {
return this._value;
}
private function set_value(value:Float):Float {
// don't restrict a value that has been passed in from an external
// source to the minimum/maximum/snapInterval
// assume that the user knows what they are doing
if (this._value == value) {
return this._value;
}
this._isDefaultValue = false;
this._value = value;
this.setInvalid(DATA);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._value;
}
private var _minimum:Float = 0.0;
/**
The stepper's value cannot be smaller than the minimum.
In the following example, the minimum is set to `-100.0`:
```haxe
stepper.minimum = -100.0;
stepper.maximum = 100.0;
stepper.step = 1.0;
stepper.value = 50.0;
```
@default 0.0
@see `NumericStepper.value`
@see `NumericStepper.maximum`
@since 1.0.0
**/
@:inspectable(defaultValue = "0.0")
public var minimum(get, set):Float;
private function get_minimum():Float {
return this._minimum;
}
private function set_minimum(value:Float):Float {
if (this._minimum == value) {
return this._minimum;
}
this._minimum = value;
this.setInvalid(DATA);
return this._minimum;
}
private var _maximum:Float = 1.0;
/**
The stepper's value cannot be larger than the maximum.
In the following example, the maximum is set to `100.0`:
```haxe
stepper.minimum = 0.0;
stepper.maximum = 100.0;
stepper.step = 1.0;
stepper.value = 12.0;
```
@default 1.0
@see `NumericStepper.value`
@see `NumericStepper.minimum`
@since 1.0.0
**/
@:inspectable(defaultValue = "1.0")
public var maximum(get, set):Float;
private function get_maximum():Float {
return this._maximum;
}
private function set_maximum(value:Float):Float {
if (this._maximum == value) {
return this._maximum;
}
this._maximum = value;
this.setInvalid(DATA);
return this._maximum;
}
// this should not be 0.0 by default because 0.0 breaks keyboard events
private var _step:Float = 0.01;
/**
Indicates the amount that `value` is changed when the stepper has focus
and one of the arrow keys is pressed.
The value should always be greater than `0.0` to ensure that the numeric
stepper reacts to keyboard events when focused, and to ensure that the
increment and decrement buttons change the value when they are
triggered.
In the following example, the step is changed to `1.0`:
```haxe
stepper.minimum = 0.0;
stepper.maximum = 100.0;
stepper.step = 1.0;
stepper.value = 10.0;
```
@default 0.01
@see `NumericStepper.value`
@see `NumericStepper.minimum`
@see `NumericStepper.maximum`
@see `NumericStepper.snapInterval`
@since 1.0.0
**/
@:inspectable(defaultValue = "0.01")
public var step(get, set):Float;
private function get_step():Float {
return this._step;
}
private function set_step(value:Float):Float {
if (this._step == value) {
return this._step;
}
this._step = value;
this.setInvalid(DATA);
return this._step;
}
private var _snapInterval:Float = 0.0;
/**
When the stepper's `value` changes, it may be "snapped" to the nearest
multiple of `snapInterval`. If `snapInterval` is `0.0`, the `value` is
not snapped.
In the following example, the snap inverval is changed to `1.0`:
```haxe
stepper.minimum = 0.0;
stepper.maximum = 100.0;
stepper.step = 1.0;
stepper.snapInterval = 1.0;
stepper.value = 10.0;
```
@default 0.0
@see `NumericStepper.step`
@since 1.0.0
**/
public var snapInterval(get, set):Float;
private function get_snapInterval():Float {
return this._snapInterval;
}
private function set_snapInterval(value:Float):Float {
if (this._snapInterval == value) {
return this._snapInterval;
}
this._snapInterval = value;
this.setInvalid(DATA);
return this._snapInterval;
}
private var _repeatTimer:Timer;
private var _currentRepeatAction:() -> Void = null;
private var _repeatDelay:Float = 0.05;
/**
The time, in seconds, before actions triggered by pressing the increment
and decrement buttons are repeated. The first repeat happens after a
delay that is five times longer than the following repeats.
In the following example, the stepper's repeat delay is set to
500 milliseconds:
```haxe
stepper.repeatDelay = 0.5;
```
To disable repeat, set `repeatDelay` to `0.0` seconds.
```haxe
// disable repeat
stepper.repeatDelay = 0.0;
```
@default 0.05
@since 1.4.0
**/
public var repeatDelay(get, set):Float;
private function get_repeatDelay():Float {
return this._repeatDelay;
}
private function set_repeatDelay(value:Float):Float {
if (this._repeatDelay == value) {
return this._repeatDelay;
}
this._repeatDelay = value;
if (this._repeatDelay == 0.0) {
this.cleanupRepeatTimer();
}
return this._repeatDelay;
}
private var _editable:Bool = true;
/**
Indicates if the text input is editable.
The following example disables editing:
```haxe
stepper.editable = false;
```
@since 1.0.0
**/
public var editable(get, set):Bool;
private function get_editable():Bool {
return this._editable;
}
private function set_editable(value:Bool):Bool {
if (this._editable == value) {
return this._editable;
}
this._editable = value;
this.setInvalid(STATE);
return this._editable;
}
private var _enableButtonsAtRangeLimits:Bool = true;
/**
Indicates if the decrement button is disabled when the value is equal to
minimum, and if the increment button is disabled when the value is equal
to the maximum.
If the buttons remain enabled, and the user attempts to decrement beyond
the minimum, or increment beyond the maximum, triggering the button will
have no effect, except visually.
The following example disables the buttons at range limits
```haxe
stepper.enableButtonsAtRangeLimits = false;
```
@since 1.2.0
**/
public var enableButtonsAtRangeLimits(get, set):Bool;
private function get_enableButtonsAtRangeLimits():Bool {
return this._enableButtonsAtRangeLimits;
}
private function set_enableButtonsAtRangeLimits(value:Bool):Bool {
if (this._enableButtonsAtRangeLimits == value) {
return this._enableButtonsAtRangeLimits;
}
this._enableButtonsAtRangeLimits = value;
this.setInvalid(STATE);
return this._enableButtonsAtRangeLimits;
}
private var _valueFormatFunction:(Float) -> String;
/**
A callback that formats the numeric stepper's value as a string to
display to the user.
In the following example, the stepper's value format function is
customized:
```haxe
stepper.valueFormatFunction = function(value:Float):String
{
return currencyFormatter.format(value, true);
};
```
@since 1.0.0
**/
public var valueFormatFunction(get, set):(Float) -> String;
private function get_valueFormatFunction():(Float) -> String {
return this._valueFormatFunction;
}
private function set_valueFormatFunction(value:(Float) -> String):(Float) -> String {
if (this._valueFormatFunction == value) {
return this._valueFormatFunction;
}
this._valueFormatFunction = value;
this.setInvalid(DATA);
return this._valueFormatFunction;
}
private var _valueParseFunction:(String) -> Float;
/**
A callback that accepts the displayed text of the numeric stepper and
converts it to a simple `Float` value.
In the following example, the stepper's value parse function is
customized:
```haxe
stepper.valueParseFunction = (displayedText:String):Float
{
return currencyFormatter.parse(displayedText).value;
};
```
@since 1.0.0
**/
public var valueParseFunction(get, set):(String) -> Float;
private function get_valueParseFunction():(String) -> Float {
return this._valueParseFunction;
}
private function set_valueParseFunction(value:(String) -> Float):(String) -> Float {
if (this._valueParseFunction == value) {
return this._valueParseFunction;
}
this._valueParseFunction = value;
this.setInvalid(DATA);
return this._valueParseFunction;
}
@:dox(hide)
public var stageFocusTarget(get, never):InteractiveObject;
private function get_stageFocusTarget():InteractiveObject {
return this.textInput;
}
/**
The baseline of the text, measured from the top of the control. May be
used in layouts.
Note: This property may not return the correct value when the control is
in an invalid state. To be safe, call `validateNow()` before accessing
this value.
@since 1.4.0
**/
public var baseline(get, never):Float;
private function get_baseline():Float {
if (this.textInput == null) {
return 0.0;
}
return this.textInput.baseline;
}
private var _oldDecrementButtonFactory:DisplayObjectFactory<Dynamic, Button>;
private var _decrementButtonFactory:DisplayObjectFactory<Dynamic, Button>;
/**
Creates the decrement button, which must be of type
`feathers.controls.Button`.
In the following example, a custom decrement button factory is provided:
```haxe
stepper.decrementButtonFactory = () ->
{
return new Button();
};
```
@see `feathers.controls.Button`
@since 1.0.0
**/
public var decrementButtonFactory(get, set):AbstractDisplayObjectFactory<Dynamic, Button>;
private function get_decrementButtonFactory():AbstractDisplayObjectFactory<Dynamic, Button> {
return this._decrementButtonFactory;
}
private function set_decrementButtonFactory(value:AbstractDisplayObjectFactory<Dynamic, Button>):AbstractDisplayObjectFactory<Dynamic, Button> {
if (this._decrementButtonFactory == value) {
return this._decrementButtonFactory;
}
this._decrementButtonFactory = value;
this.setInvalid(INVALIDATION_FLAG_DECREMENT_BUTTON_FACTORY);
return this._decrementButtonFactory;
}
private var _oldIncrementButtonFactory:DisplayObjectFactory<Dynamic, Button>;
private var _incrementButtonFactory:DisplayObjectFactory<Dynamic, Button>;
/**
Creates the increment button, which must be of type
`feathers.controls.Button`.
In the following example, a custom increment button factory is provided:
```haxe
stepper.incrementButtonFactory = () ->
{
return new Button();
};
```
@see `feathers.controls.Button`
@since 1.0.0
**/
public var incrementButtonFactory(get, set):AbstractDisplayObjectFactory<Dynamic, Button>;
private function get_incrementButtonFactory():AbstractDisplayObjectFactory<Dynamic, Button> {
return this._incrementButtonFactory;
}
private function set_incrementButtonFactory(value:AbstractDisplayObjectFactory<Dynamic, Button>):AbstractDisplayObjectFactory<Dynamic, Button> {
if (this._incrementButtonFactory == value) {
return this._incrementButtonFactory;
}
this._incrementButtonFactory = value;
this.setInvalid(INVALIDATION_FLAG_INCREMENT_BUTTON_FACTORY);
return this._incrementButtonFactory;
}
private var _oldTextInputFactory:DisplayObjectFactory<Dynamic, TextInput>;
private var _textInputFactory:DisplayObjectFactory<Dynamic, TextInput>;
/**
Creates the text input, which must be of type
`feathers.controls.TextInput`.
In the following example, a custom text input factory is provided:
```haxe
stepper.textInputFactory = () ->
{
return new TextInput();
};
```
@see `feathers.controls.TextInput`
@since 1.0.0
**/
public var textInputFactory(get, set):AbstractDisplayObjectFactory<Dynamic, TextInput>;
private function get_textInputFactory():AbstractDisplayObjectFactory<Dynamic, TextInput> {
return this._textInputFactory;
}
private function set_textInputFactory(value:AbstractDisplayObjectFactory<Dynamic, TextInput>):AbstractDisplayObjectFactory<Dynamic, TextInput> {
if (this._textInputFactory == value) {
return this._textInputFactory;
}
this._textInputFactory = value;
this.setInvalid(INVALIDATION_FLAG_TEXT_INPUT_FACTORY);
return this._textInputFactory;
}
private var _previousCustomDecrementButtonVariant:String = null;
/**
A custom variant to set on the decrement button, instead of
`NumericStepper.CHILD_VARIANT_DECREMENT_BUTTON`.
The `customDecrementButtonVariant` will be not be used if the result of
`decrementButtonFactory` already has a variant set.
@see `NumericStepper.CHILD_VARIANT_DECREMENT_BUTTON`
@see `feathers.style.IVariantStyleObject.variant`
@since 1.0.0
**/
@:style
public var customDecrementButtonVariant:String = null;
private var _previousCustomIncrementButtonVariant:String = null;
/**
A custom variant to set on the increment button, instead of
`NumericStepper.CHILD_VARIANT_INCREMENT_BUTTON`.
The `customIncrementButtonVariant` will be not be used if the result of
`incrementButtonFactory` already has a variant set.
@see `NumericStepper.CHILD_VARIANT_INCREMENT_BUTTON`
@see `feathers.style.IVariantStyleObject.variant`
@since 1.0.0
**/
@:style
public var customIncrementButtonVariant:String = null;
private var _previousCustomTextInputVariant:String = null;
/**
A custom variant to set on the text input, instead of
`NumericStepper.CHILD_VARIANT_TEXT_INPUT`.
The `customTextInputVariant` will be not be used if the result of
`textInputFactory` already has a variant set.
@see `NumericStepper.CHILD_VARIANT_TEXT_INPUT`
@see `feathers.style.IVariantStyleObject.variant`
@since 1.0.0
**/
@:style
public var customTextInputVariant:String = null;
/**
The horizontal position of the text input, relative to the buttons.
**Note:** The `HorizontalAlign.JUSTIFY` constant is not supported by this
component.
@see `feathers.layout.HorizontalAlign.LEFT`
@see `feathers.layout.HorizontalAlign.CENTER`
@see `feathers.layout.HorizontalAlign.RIGHT`
@since 1.0.0
**/
@:style
public var textInputPosition:HorizontalAlign = CENTER;
/**
How the buttons are positioned, relative to each other.
@since 1.0.0
**/
@:style
public var buttonDirection:Direction = HORIZONTAL;
/**
The space, in pixels, between the text input sub-component and other
content.
@since 1.1.0
**/
@:style
public var textInputGap:Float = 0.0;
/**
The space, in pixels, between buttons, if they are positioned next to
each other.
@since 1.1.0
**/
@:style
public var buttonGap:Float = 0.0;
private var _pendingTextInputChanges:Bool = false;
override public function showFocus(show:Bool):Void {
super.showFocus(show);
if (this.textInput != null) {
this.textInput.showFocus(show);
}
}
/**
Applies the `minimum`, `maximum`, and `snapInterval` restrictions to the
current `value`.
Because it's possible to set `value` to a numeric value that is outside
the allowed range, or to a value that has not been snapped to the
interval, this method may be called to apply the restrictions manually.
@since 1.0.0
**/
public function applyValueRestrictions():Void {
this.value = this.restrictValue(this._value);
}
override public function dispose():Void {
this.cleanupRepeatTimer();
this.destroyDecrementButton();
this.destroyIncrementButton();
this.destroyTextInput();
super.dispose();
}
private function initializeNumericStepperTheme():Void {
#if !feathersui_disable_default_theme
feathers.themes.steel.components.SteelNumericStepperStyles.initialize();
#end
}
override private function initialize():Void {
super.initialize();
// if the user hasn't changed the value, automatically restrict it based
// on things like minimum, maximum, and snapInterval
// if the user has changed the value, assume that they know what they're
// doing and don't want hand holding
if (this._isDefaultValue) {
// use the setter
this.value = this.restrictValue(this._value);
}
}
override private function update():Void {
var dataInvalid = this.isInvalid(DATA);
var sizeInvalid = this.isInvalid(SIZE);
var stateInvalid = this.isInvalid(STATE);
var stylesInvalid = this.isInvalid(STYLES);
if (this._previousCustomTextInputVariant != this.customTextInputVariant) {
this.setInvalidationFlag(INVALIDATION_FLAG_TEXT_INPUT_FACTORY);
}
if (this._previousCustomDecrementButtonVariant != this.customDecrementButtonVariant) {
this.setInvalidationFlag(INVALIDATION_FLAG_DECREMENT_BUTTON_FACTORY);
}
if (this._previousCustomIncrementButtonVariant != this.customIncrementButtonVariant) {
this.setInvalidationFlag(INVALIDATION_FLAG_INCREMENT_BUTTON_FACTORY);
}
var decrementButtonFactoryInvalid = this.isInvalid(INVALIDATION_FLAG_DECREMENT_BUTTON_FACTORY);
var incrementButtonFactoryInvalid = this.isInvalid(INVALIDATION_FLAG_INCREMENT_BUTTON_FACTORY);
var textInputFactoryInvalid = this.isInvalid(INVALIDATION_FLAG_TEXT_INPUT_FACTORY);
if (decrementButtonFactoryInvalid) {
this.createDecrementButton();
}
if (incrementButtonFactoryInvalid) {
this.createIncrementButton();
}
if (textInputFactoryInvalid) {
this.createTextInput();
}
if (stateInvalid || dataInvalid) {
this.refreshEnabled();
}
if (stateInvalid) {
this.refreshEditable();
}
if (dataInvalid) {
this.refreshMeasureText();
}
if (dataInvalid) {
this.refreshTextInputData();
}
sizeInvalid = this.measure() || sizeInvalid;
this.layoutContent();
this._previousCustomTextInputVariant = this.customTextInputVariant;
this._previousCustomDecrementButtonVariant = this.customDecrementButtonVariant;
this._previousCustomIncrementButtonVariant = this.customIncrementButtonVariant;
}
private function measure():Bool {
var needsWidth = this.explicitWidth == null;
var needsHeight = this.explicitHeight == null;
var needsMinWidth = this.explicitMinWidth == null;
var needsMinHeight = this.explicitMinHeight == null;
var needsMaxWidth = this.explicitMaxWidth == null;
var needsMaxHeight = this.explicitMaxHeight == null;
if (!needsWidth && !needsHeight && !needsMinWidth && !needsMinHeight && !needsMaxWidth && !needsMaxHeight) {
return false;
}
this.decrementButtonMeasurements.restore(this.decrementButton);
this.decrementButton.validateNow();
this.incrementButtonMeasurements.restore(this.incrementButton);
this.incrementButton.validateNow();
this.textInputMeasurements.restore(this.textInput);
this.textInput.validateNow();
var newWidth = this.explicitWidth;
if (needsWidth) {
var buttonWidth = Math.max(this.decrementButton.width, this.incrementButton.width);
if (this.buttonDirection == VERTICAL) {
if (this.textInputPosition == CENTER) {
newWidth = Math.max(this.textInput.width, buttonWidth);
} else {
newWidth = this.textInput.width + this.textInputGap + buttonWidth;
}
} else { // HORIZONTAL
if (this.textInputPosition == CENTER) {
newWidth = (2.0 * (buttonWidth + this.textInputGap)) + this.textInput.width;
} else {
newWidth = (2.0 * buttonWidth) + this.buttonGap + this.textInputGap + this.textInput.width;
}
}
}
var newHeight = this.explicitHeight;
if (needsHeight) {
var buttonHeight = Math.max(this.decrementButton.height, this.incrementButton.height);
if (this.buttonDirection == VERTICAL) {
if (this.textInputPosition == CENTER) {
newHeight = (2.0 * (buttonHeight + this.textInputGap)) + this.textInput.height;
} else {
newHeight = Math.max(this.textInput.height, (2.0 * buttonHeight) + this.buttonGap);
}
} else { // HORIZONTAL
newHeight = Math.max(this.textInput.height, buttonHeight);
}
}
var newMinWidth = this.explicitMinWidth;
if (needsMinWidth) {
var buttonWidth = Math.max(this.decrementButton.width, this.incrementButton.width);
if (this.buttonDirection == VERTICAL) {
if (this.textInputPosition == CENTER) {
newMinWidth = Math.max(this.textInput.minWidth, buttonWidth);
} else {
newMinWidth = this.textInput.minWidth + this.textInputGap + buttonWidth;
}
} else { // HORIZONTAL
if (this.textInputPosition == CENTER) {
newMinWidth = (2.0 * (buttonWidth + this.textInputGap)) + this.textInput.minWidth;
} else {
newMinWidth = (2.0 * buttonWidth) + this.buttonGap + this.textInputGap + this.textInput.minWidth;
}
}
}
var newMinHeight = this.explicitMinHeight;
if (needsMinHeight) {
var buttonHeight = Math.max(this.decrementButton.height, this.incrementButton.height);
if (this.buttonDirection == VERTICAL) {
if (this.textInputPosition == CENTER) {
newMinHeight = (2.0 * (buttonHeight + this.textInputGap)) + this.textInput.minHeight;
} else {
newMinHeight = Math.max(this.textInput.minHeight, (2.0 * buttonHeight) + this.buttonGap);
}
} else { // HORIZONTAL
newMinHeight = Math.max(this.textInput.minHeight, buttonHeight);
}
}
return this.saveMeasurements(newWidth, newHeight, newMinWidth, newMinHeight);
}
private function refreshMeasureText():Void {
var maxCharactersBeforeDecimal = Std.int(Math.max(Math.max(Std.string(Std.int(this._minimum)).length, Std.string(Std.int(this._maximum)).length),
Std.string(Std.int(this._step)).length));
// roundToPrecision() helps us to avoid numbers like 1.00000000000000001
// caused by the inaccuracies of floating point math.
var maxCharactersAfterDecimal = Std.int(Math.max(Math.max(Std.string(MathUtil.roundToPrecision(this._minimum - Std.int(this._minimum), 10)).length,
Std.string(MathUtil.roundToPrecision(this._maximum - Std.int(this._maximum), 10)).length),
Std.string(MathUtil.roundToPrecision(this._step - Std.int(this._step), 10)).length))
- 2;
if (maxCharactersAfterDecimal < 0) {
maxCharactersAfterDecimal = 0;
}
var measureText = "";
for (i in 0...(maxCharactersBeforeDecimal + maxCharactersAfterDecimal)) {
measureText += "0";
}
if (maxCharactersAfterDecimal > 0) {
measureText += ".";
}
this.textInput.measureText = measureText;
}
private function refreshEnabled():Void {
this.decrementButton.enabled = this._enabled && (this._enableButtonsAtRangeLimits || this._value != this._minimum);
this.incrementButton.enabled = this._enabled && (this._enableButtonsAtRangeLimits || this._value != this._maximum);
this.textInput.enabled = this._enabled;
}
private function refreshEditable():Void {
this.textInput.editable = this._editable;
}
private function refreshTextInputData():Void {
// roundToPrecision() helps us to avoid numbers like 1.00000000000000001
// caused by the inaccuracies of floating point math.
var valueToFormat = MathUtil.roundToPrecision(this._value, 10);
if (this._valueFormatFunction != null) {
this.textInput.text = this._valueFormatFunction(valueToFormat);
} else {
this.textInput.text = Std.string(valueToFormat);
}
}
private function parseTextInputValue():Void {
var newValue = this._value;
if (this._valueParseFunction != null) {
newValue = this._valueParseFunction(this.textInput.text);
} else {
newValue = Std.parseFloat(this.textInput.text);
}
if (!Math.isNaN(newValue)) {
newValue = this.restrictValue(newValue);
this.value = newValue;
}
// we need to force invalidation just to be sure that the text input
// is displaying the correct value.