-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathToggleSwitch.hx
More file actions
1198 lines (1014 loc) · 35.1 KB
/
ToggleSwitch.hx
File metadata and controls
1198 lines (1014 loc) · 35.1 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.core.FeathersControl;
import feathers.core.IFocusObject;
import feathers.core.IUIControl;
import feathers.core.IValidating;
import feathers.events.FeathersEvent;
import feathers.layout.Measurements;
import feathers.skins.IProgrammaticSkin;
import feathers.text.TextFormat;
import feathers.utils.ExclusivePointer;
import motion.Actuate;
import motion.actuators.SimpleActuator;
import motion.easing.IEasing;
import motion.easing.Quart;
import openfl.display.DisplayObject;
import openfl.display.Stage;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import openfl.events.MouseEvent;
import openfl.events.TouchEvent;
import openfl.text.AntiAliasType;
import openfl.text.GridFitType;
import openfl.text.TextField;
import openfl.ui.Keyboard;
#if air
import openfl.ui.Multitouch;
#end
/**
Similar to a light switch, with on and off states that may be toggled.
An alternative to a `Check`, especially on mobile.
The following example creates a toggle switch, programmatically selects it,
and listens for when the selection changes:
```haxe
var toggleSwitch = new ToggleSwitch();
toggleSwitch.selected = true;
toggleSwitch.addEventListener(Event.CHANGE, (event) -> {
var toggleSwitch = cast(event.currentTarget, ToggleSwitch);
trace("toggle switch changed: " + toggleSwitch.selected);
});
this.addChild(toggleSwitch);
```
@event openfl.events.Event.CHANGE Dispatched when `ToggleSwitch.selected`
changes.
@see [Tutorial: How to use the ToggleSwitch component](https://feathersui.com/learn/haxe-openfl/toggle-switch/)
@see `feathers.controls.Check`
@since 1.0.0
**/
@:event(openfl.events.Event.CHANGE)
@:styleContext
class ToggleSwitch extends FeathersControl implements IToggle implements IFocusObject {
/**
Creates a new `ToggleSwitch` object.
@since 1.0.0
**/
public function new(selected:Bool = false, ?changeListener:(Event) -> Void) {
initializeToggleSwitchTheme();
super();
this.selected = selected;
// MouseEvent.CLICK is dispatched only if the same object is under the
// pointer for both MouseEvent.MOUSE_DOWN and MouseEvent.MOUSE_UP. The
// thumb/track might change skins between MouseEvent.MOUSE_DOWN and
// MouseEvent.MOUSE_UP, and this would prevent MouseEvent.CLICK.
// setting mouseChildren to false keeps the button as the target.
this.mouseChildren = false;
// when focused, keyboard space/enter trigger MouseEvent.CLICK
this.buttonMode = true;
// a hand cursor only makes sense for hyperlinks
this.useHandCursor = false;
this.tabEnabled = true;
this.tabChildren = false;
this.focusRect = null;
this.addEventListener(KeyboardEvent.KEY_DOWN, toggleSwitch_keyDownHandler);
this.addEventListener(MouseEvent.MOUSE_DOWN, toggleSwitch_mouseDownHandler);
this.addEventListener(MouseEvent.CLICK, toggleSwitch_clickHandler);
this.addEventListener(TouchEvent.TOUCH_TAP, toggleSwitch_touchTapHandler);
if (changeListener != null) {
this.addEventListener(Event.CHANGE, changeListener);
}
}
private var onTextField:TextField;
private var offTextField:TextField;
private var _selected:Bool = false;
/**
Indicates if the toggle switch is selected or not.
The following example selects the toggle switch:
```haxe
toggleSwitch.selected = true;
```
Note: When changing the `selected` property programatically, the
position of the thumb is not animated. To change the selection with
animation, call the `setSelectionWithAnimation()` method.
@default false
@see `ToggleSwitch.setSelectionWithAnimation()`
@since 1.0.0
**/
@:bindable("change")
@:inspectable(defaultValue = "false")
public var selected(get, set):Bool;
private function get_selected():Bool {
return this._selected;
}
private function set_selected(value:Bool):Bool {
this._animateSelectionChange = false;
if (this._selected == value) {
return this._selected;
}
this._selected = value;
FeathersEvent.dispatch(this, Event.CHANGE);
this.setInvalid(SELECTION);
return this._selected;
}
private var _onText:String = null;
/**
The text displayed by the toggle switch when it is toggled on. If
`null`, no text is displayed.
The following example sets the label's on text:
```haxe
label.onText = "ON";
```
@default null
@see `Label.offText`
@see `Label.textFormat`
@since 1.3.0
**/
public var onText(get, set):String;
private function get_onText():String {
return this._onText;
}
private function set_onText(value:String):String {
if (this._onText == value) {
return this._onText;
}
this._onText = value;
this.setInvalid(DATA);
return this._onText;
}
private var _offText:String = null;
/**
The text displayed by the toggle switch when it is toggled off. If
`null`, no text is displayed.
The following example sets the label's off text:
```haxe
label.offText = "OFF";
```
@default null
@see `Label.onText`
@see `Label.textFormat`
@since 1.3.0
**/
public var offText(get, set):String;
private function get_offText():String {
return this._offText;
}
private function set_offText(value:String):String {
if (this._offText == value) {
return this._offText;
}
this._offText = value;
this.setInvalid(DATA);
return this._offText;
}
private var _previousOnText:String = null;
private var _previousOffText:String = null;
private var _previousOnTextFormat:TextFormat = null;
private var _previousOnSimpleTextFormat:openfl.text.TextFormat = null;
private var _previousOffTextFormat:TextFormat = null;
private var _previousOffSimpleTextFormat:openfl.text.TextFormat = null;
private var _updatedOnTextStyles = false;
private var _updatedOffTextStyles = false;
private var _onTextMeasuredWidth = 0.0;
private var _onTextMeasuredHeight = 0.0;
private var _offTextMeasuredWidth = 0.0;
private var _offTextMeasuredHeight = 0.0;
/**
The font styles used to render the toggle switch's text.
In the following example, the toggle switch's text formatting is
customized:
```haxe
toggleSwitch.textFormat = new TextFormat("Helvetica", 20, 0xcc0000);
```
@see `ToggleSwitch.onText`
@see `ToggleSwitch.offText`
@see `ToggleSwitch.disabledTextFormat`
@see `ToggleSwitch.embedFonts`
@since 1.3.0
**/
@:style
public var textFormat:AbstractTextFormat = null;
/**
The font styles used to render the toggle switch's text when the toggle
switch is disabled.
In the following example, the toggle switch's disabled text formatting
is customized:
```haxe
toggleSwitch.enabled = false;
toggleSwitch.disabledTextFormat = new TextFormat("Helvetica", 20, 0xee0000);
```
@see `ToggleSwitch.textFormat`
@since 1.3.0
**/
@:style
public var disabledTextFormat:AbstractTextFormat = null;
/**
Determines if an embedded font is used or not.
In the following example, the toggle switch uses embedded fonts:
```haxe
toggleSwitch.embedFonts = true;
```
@see `ToggleSwitch.textFormat`
@since 1.3.0
**/
@:style
public var embedFonts:Bool = false;
/**
Configures the `alpha` value of the toggle switch's text.
In the following example, the toggle switch's text alpha is customized:
```haxe
toggleSwitch.textAlpha = 0.5;
```
@see `ToggleSwitch.textFormat`
@see `ToggleSwitch.disabledTextAlpha`
@since 1.4.0
**/
@:style
@:inspectable(minValue = "0.0", maxValue = "1.0", verbose = "1")
public var textAlpha:Float = 1.0;
/**
When `disabledTextAlpha` is not `null`, sets the `alpha` property of the
text to this value when the the `enabled` property is set to `false`.
In the following example, the toggle switch's disabled text alpha is customized:
```haxe
toggleSwitch.disabledTextAlpha = 0.5;
```
@see `ToggleSwitch.textAlpha`
@since 1.4.0
**/
@:style
@:inspectable(minValue = "0.0", maxValue = "1.0", verbose = "1")
public var disabledTextAlpha:Null<Float> = null;
/**
Determines the type of anti-aliasing used for embedded fonts.
In the following example, the toggle switch uses advanced anti-aliasing:
```haxe
toggleSwitch.embedFonts = true;
toggleSwitch.antiAliasType = ADVANCED;
```
@see `ToggleSwitch.embedFonts`
@see `ToggleSwitch.gridFitType`
@since 1.4.0
**/
@:style
public var antiAliasType:AntiAliasType = NORMAL;
/**
Determines the type of anti-aliasing used for embedded fonts.
In the following example, the toggle switch uses sub-pixel grid fitting:
```haxe
toggleSwitch.embedFonts = true;
toggleSwitch.antiAliasType = ADVANCED;
toggleSwitch.gridFitType = SUBPIXEL;
```
@see `ToggleSwitch.antiAliasType`
@see `ToggleSwitch.embedFonts`
@since 1.4.0
**/
@:style
public var gridFitType:GridFitType = PIXEL;
private var _currentThumbSkin:DisplayObject = null;
private var _thumbSkinMeasurements:Measurements = null;
/**
The skin to use for the toggle switch's thumb.
In the following example, a thumb skin is passed to the toggle switch:
```haxe
var skin = new RectangleSkin();
skin.fill = SolidColor(0xcccccc);
toggleSwitch.thumbSkin = skin;
```
@see `ToggleSwitch.trackSkin`
@since 1.0.0
**/
@:style
public var thumbSkin:DisplayObject = null;
private var _currentTrackSkin:DisplayObject = null;
private var _trackSkinMeasurements:Measurements = null;
/**
The skin to use for the toggle switch's track.
In the following example, a track skin is passed to the toggle switch:
```haxe
var skin = new RectangleSkin();
skin.fill = SolidColor(0xcccccc);
toggleSwitch.trackSkin = skin;
```
@see `ToggleSwitch.secondaryTrackSkin`
@see `ToggleSwitch.thumbSkin`
@since 1.0.0
**/
@:style
public var trackSkin:DisplayObject = null;
private var _currentSecondaryTrackSkin:DisplayObject = null;
private var _secondaryTrackSkinMeasurements:Measurements = null;
/**
The skin to use for the toggle switch's optional secondary track. If a
toggle switch has one track, it will fill the entire length of the
toggle switch. If a toggle switch has a track and a secondary track, the
primary track will stretch between the left edge of the toggle switch
and the location of the slider's thumb, while the secondary track will
stretch from the location of the toggle switch's thumb to the right edge
of the toggle switch.
In the following example, a track skin and a secondary track skin are
passed to the toggle switch:
```haxe
var skin = new RectangleSkin();
skin.fill = SolidColor(0xaaaaaa);
toggleSwitch.trackSkin = skin;
var skin = new RectangleSkin();
skin.fill = SolidColor(0xcccccc);
toggleSwitch.secondaryTrackSkin = skin;
```
@see `ToggleSwitch.trackSkin`
@since 1.0.0
**/
@:style
public var secondaryTrackSkin:DisplayObject = null;
/**
The minimum space, measured in pixels, between the toggle switch's right
edge and the right edge of the thumb.
In the following example, the toggle switch's right padding is set to 20
pixels:
```haxe
toggleSwitch.paddingRight = 20.0;
```
@default 0.0
@since 1.0.0
**/
@:style
public var paddingRight:Float = 0.0;
/**
The minimum space, measured in pixels, between the toggle switch's left
edge and the left edge of the thumb.
In the following example, the toggle switch's left padding is set to 20
pixels:
```haxe
toggleSwitch.paddingLeft = 20.0;
```
@default 0.0
@since 1.0.0
**/
@:style
public var paddingLeft:Float = 0.0;
private var _toggleTween:SimpleActuator<Dynamic, Dynamic> = null;
/**
The duration, measured in seconds, of the animation when the toggle
switch is clicked or tap and the thumb slides to the other side.
In the following example, the duration of the animation that toggles the
thumb is set to 500 milliseconds:
```haxe
toggleSwitch.toggleDuration = 0.5;
```
@since 1.0.0
**/
@:style
public var toggleDuration:Float = 0.25;
/**
The easing function used for the animation when the toggle switch is
clicked or tap and the thumb slides to the other side.
In the following example, the ease of the animation that toggles the
thumb is customized:
```haxe
toggleSwitch.toggleEase = Elastic.easeOut;
```
@since 1.0.0
**/
@:style
public var toggleEase:IEasing = Quart.easeOut;
private var _dragStartX:Float;
private var _ignoreClick:Bool = false;
private var _animateSelectionChange:Bool = false;
override public function dispose():Void {
if (this._previousOnTextFormat != null) {
this._previousOnTextFormat.removeEventListener(Event.CHANGE, toggleSwitch_onTextFormat_changeHandler);
this._previousOnTextFormat = null;
}
if (this._previousOffTextFormat != null) {
this._previousOffTextFormat.removeEventListener(Event.CHANGE, toggleSwitch_offTextFormat_changeHandler);
this._previousOffTextFormat = null;
}
super.dispose();
}
/**
Changes the `selected` property and animates the position of the thumb.
@see `ToggleSwitch.selected`
@since 1.0.0
**/
public function setSelectionWithAnimation(selected:Bool):Bool {
if (this._selected == selected) {
return this._selected;
}
this.selected = selected;
this._animateSelectionChange = true;
return this._selected;
}
/**
Sets all padding properties to the same value.
@see `ToggleSwitch.paddingRight`
@see `ToggleSwitch.paddingLeft`
@since 1.0.0
**/
public function setPadding(value:Float):Void {
this.paddingRight = value;
this.paddingLeft = value;
}
private function initializeToggleSwitchTheme():Void {
#if !feathersui_disable_default_theme
feathers.themes.steel.components.SteelToggleSwitchStyles.initialize();
#end
}
override private function update():Void {
var dataInvalid = this.isInvalid(DATA);
var selectionInvalid = this.isInvalid(SELECTION);
var sizeInvalid = this.isInvalid(SIZE);
var stateInvalid = this.isInvalid(STATE);
var stylesInvalid = this.isInvalid(STYLES);
this._updatedOnTextStyles = false;
this._updatedOffTextStyles = false;
if (stylesInvalid) {
this.refreshThumb();
this.refreshTrack();
this.refreshSecondaryTrack();
}
if (dataInvalid) {
this.refreshTextFields();
}
if (dataInvalid || stylesInvalid || stateInvalid) {
this.refreshTextStyles();
}
if (dataInvalid || stylesInvalid || stateInvalid || sizeInvalid) {
this.refreshOnText(sizeInvalid);
this.refreshOffText(sizeInvalid);
}
if (selectionInvalid || stylesInvalid) {
this.refreshSelection();
}
if (stateInvalid || stylesInvalid) {
this.refreshEnabled();
}
sizeInvalid = this.measure() || sizeInvalid;
this.layoutContent();
}
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;
}
if (this._currentThumbSkin != null) {
this._thumbSkinMeasurements.restore(this._currentThumbSkin);
if ((this._currentThumbSkin is IValidating)) {
(cast this._currentThumbSkin : IValidating).validateNow();
}
}
if (this._currentTrackSkin != null) {
this._trackSkinMeasurements.restore(this._currentTrackSkin);
if ((this._currentTrackSkin is IValidating)) {
(cast this._currentTrackSkin : IValidating).validateNow();
}
}
if (this._currentSecondaryTrackSkin != null) {
this._secondaryTrackSkinMeasurements.restore(this._currentSecondaryTrackSkin);
if ((this._currentSecondaryTrackSkin is IValidating)) {
(cast this._currentSecondaryTrackSkin : IValidating).validateNow();
}
}
var newWidth = this.explicitWidth;
if (needsWidth) {
newWidth = 0.0;
if (this._currentTrackSkin != null) {
newWidth += this._currentTrackSkin.width;
if (this._currentSecondaryTrackSkin != null) {
newWidth += this._currentSecondaryTrackSkin.width;
}
}
if (this.onTextField != null) {
var onTextAndThumbWidth = this._onTextMeasuredWidth;
if (this._currentThumbSkin != null) {
onTextAndThumbWidth += this._currentThumbSkin.width;
}
if (newWidth < onTextAndThumbWidth) {
newWidth = onTextAndThumbWidth;
}
}
if (this.offTextField != null) {
var offTextAndThumbWidth = this._offTextMeasuredWidth;
if (this._currentThumbSkin != null) {
offTextAndThumbWidth += this._currentThumbSkin.width;
}
if (newWidth < offTextAndThumbWidth) {
newWidth = offTextAndThumbWidth;
}
}
}
var newHeight = this.explicitHeight;
if (needsHeight) {
newHeight = 0.0;
if (this._currentThumbSkin != null) {
newHeight += this._currentThumbSkin.height;
}
if (this._currentTrackSkin != null) {
if (newHeight < this._currentTrackSkin.height) {
newHeight = this._currentTrackSkin.height;
}
if (this._currentSecondaryTrackSkin != null && newHeight < this._currentSecondaryTrackSkin.height) {
newHeight = this._currentSecondaryTrackSkin.height;
}
}
if (this.onTextField != null && newHeight < this._onTextMeasuredHeight) {
newHeight = this._onTextMeasuredHeight;
}
if (this.offTextField != null && newHeight < this._offTextMeasuredHeight) {
newHeight = this._offTextMeasuredHeight;
}
}
// TODO: calculate min and max
var newMinWidth = newWidth;
var newMinHeight = newHeight;
var newMaxWidth = newWidth;
var newMaxHeight = newHeight;
return this.saveMeasurements(newWidth, newHeight, newMinWidth, newMinHeight, newMaxWidth, newMaxHeight);
}
private function refreshTextFields():Void {
if (this._onText == null) {
if (this.onTextField != null) {
this.removeChild(this.onTextField);
this.onTextField = null;
}
this._previousOnText = null;
this._previousOnTextFormat = null;
this._previousOnSimpleTextFormat = null;
} else if (this.onTextField == null) {
var index = 0;
if (this._currentSecondaryTrackSkin != null) {
index = 2;
} else if (this._currentTrackSkin != null) {
index = 1;
}
this.onTextField = new TextField();
this.addChildAt(this.onTextField, index);
}
if (this._offText == null) {
if (this.offTextField != null) {
this.removeChild(this.offTextField);
this.offTextField = null;
}
this._previousOffText = null;
this._previousOffTextFormat = null;
this._previousOffSimpleTextFormat = null;
} else if (this.offTextField == null) {
var index = 0;
if (this._currentSecondaryTrackSkin != null) {
index = 2;
} else if (this._currentTrackSkin != null) {
index = 1;
}
this.offTextField = new TextField();
this.addChildAt(this.offTextField, index);
}
}
private function refreshTextStyles():Void {
var textFormat = this.getCurrentTextFormat();
var simpleTextFormat = textFormat != null ? textFormat.toSimpleTextFormat() : null;
if (this.offTextField != null) {
if (this.offTextField.embedFonts != this.embedFonts) {
this.offTextField.embedFonts = this.embedFonts;
this._updatedOffTextStyles = true;
}
if (this.offTextField.antiAliasType != this.antiAliasType) {
this.offTextField.antiAliasType = this.antiAliasType;
this._updatedOffTextStyles = true;
}
if (this.offTextField.gridFitType != this.gridFitType) {
this.offTextField.gridFitType = this.gridFitType;
this._updatedOffTextStyles = true;
}
this.offTextField.alpha = this.getCurrentTextAlpha();
if (simpleTextFormat != this._previousOffSimpleTextFormat) {
if (this._previousOffTextFormat != null) {
this._previousOffTextFormat.removeEventListener(Event.CHANGE, toggleSwitch_offTextFormat_changeHandler);
}
// clear the selection before setting defaultTextFormat because any
// selection seems to prevent the defaultTextFormat from fully applying
if (this.offTextField.caretIndex != -1 && this.offTextField.selectionBeginIndex != this.offTextField.selectionEndIndex) {
// check for caretIndex != -1 first due to a bug in OpenFL
this.offTextField.setSelection(0, 0);
}
if (textFormat != null) {
textFormat.addEventListener(Event.CHANGE, toggleSwitch_offTextFormat_changeHandler, false, 0, true);
this.offTextField.defaultTextFormat = simpleTextFormat;
this._updatedOffTextStyles = true;
}
this._previousOffTextFormat = textFormat;
this._previousOffSimpleTextFormat = simpleTextFormat;
}
}
if (this.onTextField != null) {
if (this.onTextField.embedFonts != this.embedFonts) {
this.onTextField.embedFonts = this.embedFonts;
this._updatedOnTextStyles = true;
}
if (this.onTextField.antiAliasType != this.antiAliasType) {
this.onTextField.antiAliasType = this.antiAliasType;
this._updatedOnTextStyles = true;
}
if (this.onTextField.gridFitType != this.gridFitType) {
this.onTextField.gridFitType = this.gridFitType;
this._updatedOnTextStyles = true;
}
this.onTextField.alpha = this.getCurrentTextAlpha();
if (simpleTextFormat != this._previousOnSimpleTextFormat) {
if (this._previousOnTextFormat != null) {
this._previousOnTextFormat.removeEventListener(Event.CHANGE, toggleSwitch_onTextFormat_changeHandler);
}
// clear the selection before setting defaultTextFormat because any
// selection seems to prevent the defaultTextFormat from fully applying
if (this.onTextField.caretIndex != -1 && this.onTextField.selectionBeginIndex != this.onTextField.selectionEndIndex) {
// check for caretIndex != -1 first due to a bug in OpenFL
this.onTextField.setSelection(0, 0);
}
if (textFormat != null) {
textFormat.addEventListener(Event.CHANGE, toggleSwitch_onTextFormat_changeHandler, false, 0, true);
this.onTextField.defaultTextFormat = simpleTextFormat;
this._updatedOnTextStyles = true;
}
this._previousOnTextFormat = textFormat;
this._previousOnSimpleTextFormat = simpleTextFormat;
}
}
}
private function refreshOnText(forceMeasurement:Bool):Void {
if (this.onTextField == null) {
return;
}
// usually, hasText doesn't check the length, but TextField height may
// not be accurate with an empty string
var hasText = this._onText != null && this._onText.length > 0;
if (this._onText == this._previousOnText && !this._updatedOnTextStyles && !forceMeasurement) {
// nothing to refresh
return;
}
// set autoSize before text because setting text first can trigger an
// extra text engine reflow
this.onTextField.autoSize = LEFT;
if (hasText) {
this.onTextField.text = this._onText;
} else {
// zero-width space results in a more accurate height measurement
// than we'd get with an empty string
this.onTextField.text = "\u200b";
}
this._onTextMeasuredWidth = this.onTextField.textWidth + 4;
this._onTextMeasuredHeight = this.onTextField.height;
this.onTextField.autoSize = NONE;
if (!hasText) {
this.onTextField.text = "";
}
this._previousOnText = this._onText;
}
private function refreshOffText(forceMeasurement:Bool):Void {
if (this.offTextField == null) {
return;
}
// usually, hasText doesn't check the length, but TextField height may
// not be accurate with an empty string
var hasText = this._offText != null && this._offText.length > 0;
if (this._offText == this._previousOffText && !this._updatedOffTextStyles && !forceMeasurement) {
// nothing to refresh
return;
}
// set autoSize before text because setting text first can trigger an
// extra text engine reflow
this.offTextField.autoSize = LEFT;
if (hasText) {
this.offTextField.text = this._offText;
} else {
// zero-width space results in a more accurate height measurement
// than we'd get with an empty string
this.offTextField.text = "\u200b";
}
this._offTextMeasuredWidth = this.offTextField.textWidth + 4;
this._offTextMeasuredHeight = this.offTextField.height;
this.offTextField.autoSize = NONE;
if (!hasText) {
this.offTextField.text = "";
}
this._previousOffText = this._offText;
}
private function getCurrentTextFormat():TextFormat {
if (!this._enabled && this.disabledTextFormat != null) {
return this.disabledTextFormat;
}
return this.textFormat;
}
private function getCurrentTextAlpha():Float {
if (!this._enabled && this.disabledTextAlpha != null) {
return this.disabledTextAlpha;
}
return this.textAlpha;
}
private function refreshThumb():Void {
var oldSkin = this._currentThumbSkin;
this._currentThumbSkin = this.thumbSkin;
if (this._currentThumbSkin == oldSkin) {
return;
}
if (oldSkin != null && oldSkin.parent == this) {
if ((oldSkin is IProgrammaticSkin)) {
(cast oldSkin : IProgrammaticSkin).uiContext = null;
}
this.removeChild(oldSkin);
}
if (this._currentThumbSkin != null) {
if ((this._currentThumbSkin is IUIControl)) {
(cast this._currentThumbSkin : IUIControl).initializeNow();
}
if (this._thumbSkinMeasurements == null) {
this._thumbSkinMeasurements = new Measurements(this._currentThumbSkin);
} else {
this._thumbSkinMeasurements.save(this._currentThumbSkin);
}
if ((this._currentThumbSkin is IProgrammaticSkin)) {
(cast this._currentThumbSkin : IProgrammaticSkin).uiContext = this;
}
// add it in front of both the trackSkin and secondaryTrackSkin
this.addChild(this._currentThumbSkin);
} else {
this._thumbSkinMeasurements = null;
}
}
private function refreshTrack():Void {
var oldSkin = this._currentTrackSkin;
this._currentTrackSkin = this.trackSkin;
if (this._currentTrackSkin == oldSkin) {
return;
}
if (oldSkin != null && oldSkin.parent == this) {
if ((oldSkin is IProgrammaticSkin)) {
(cast oldSkin : IProgrammaticSkin).uiContext = null;
}
this.removeChild(oldSkin);
}
if (this._currentTrackSkin != null) {
if ((this._currentTrackSkin is IFocusObject)) {
(cast this._currentTrackSkin : IFocusObject).focusEnabled = false;
}
if ((this._currentTrackSkin is IUIControl)) {
(cast this._currentTrackSkin : IUIControl).initializeNow();
}
if (this._trackSkinMeasurements == null) {
this._trackSkinMeasurements = new Measurements(this._currentTrackSkin);
} else {
this._trackSkinMeasurements.save(this._currentTrackSkin);
}
if ((this._currentTrackSkin is IProgrammaticSkin)) {
(cast this._currentTrackSkin : IProgrammaticSkin).uiContext = this;
}
// always on the bottom
this.addChildAt(this._currentTrackSkin, 0);
} else {
this._trackSkinMeasurements = null;
}
}
private function refreshSecondaryTrack():Void {
var oldSkin = this._currentSecondaryTrackSkin;
this._currentSecondaryTrackSkin = this.secondaryTrackSkin;
if (this._currentSecondaryTrackSkin == oldSkin) {
return;
}
if (oldSkin != null && oldSkin.parent == this) {
if ((oldSkin is IProgrammaticSkin)) {
(cast oldSkin : IProgrammaticSkin).uiContext = null;
}
this.removeChild(oldSkin);
}
if (this._currentSecondaryTrackSkin != null) {
if ((this._currentSecondaryTrackSkin is IFocusObject)) {
(cast this._currentSecondaryTrackSkin : IFocusObject).focusEnabled = false;
}
if ((this._currentSecondaryTrackSkin is IUIControl)) {
(cast this._currentSecondaryTrackSkin : IUIControl).initializeNow();
}
if (this._secondaryTrackSkinMeasurements == null) {
this._secondaryTrackSkinMeasurements = new Measurements(this._currentSecondaryTrackSkin);
} else {
this._secondaryTrackSkinMeasurements.save(this._currentSecondaryTrackSkin);
}
if ((this._currentSecondaryTrackSkin is IProgrammaticSkin)) {
(cast this._currentSecondaryTrackSkin : IProgrammaticSkin).uiContext = this;
}
// in front of the trackSkin, if it exists
// otherwise, on the bottom
var index = this._currentTrackSkin != null ? 1 : 0;
this.addChildAt(this._currentSecondaryTrackSkin, index);
} else {
this._secondaryTrackSkinMeasurements = null;
}
}
private function refreshSelection():Void {
if ((this._currentThumbSkin is IToggle)) {
(cast this._currentThumbSkin : IToggle).selected = this._selected;
}
if ((this._currentTrackSkin is IToggle)) {
(cast this._currentTrackSkin : IToggle).selected = this._selected;
}
if ((this._currentSecondaryTrackSkin is IToggle)) {
(cast this._currentSecondaryTrackSkin : IToggle).selected = this._selected;
}
// stop the tween, no matter what
if (this._toggleTween != null) {
Actuate.stop(this._toggleTween, null, false, false);
this._toggleTween = null;
}
}
private function refreshEnabled():Void {
if ((this._currentThumbSkin is IUIControl)) {
(cast this._currentThumbSkin : IUIControl).enabled = this._enabled;
}
if ((this._currentTrackSkin is IUIControl)) {
(cast this._currentTrackSkin : IUIControl).enabled = this._enabled;
}
if ((this._currentSecondaryTrackSkin is IUIControl)) {
(cast this._currentSecondaryTrackSkin : IUIControl).enabled = this._enabled;
}
}