-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTabBar.hx
More file actions
1468 lines (1277 loc) · 44.9 KB
/
TabBar.hx
File metadata and controls
1468 lines (1277 loc) · 44.9 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.dataRenderers.IDataRenderer;
import feathers.core.FeathersControl;
import feathers.core.IDataSelector;
import feathers.core.IFocusObject;
import feathers.core.IIndexSelector;
import feathers.core.IUIControl;
import feathers.core.IValidating;
import feathers.core.InvalidationFlag;
import feathers.data.IFlatCollection;
import feathers.data.TabBarItemState;
import feathers.events.FeathersEvent;
import feathers.events.FlatCollectionEvent;
import feathers.events.TabBarEvent;
import feathers.events.TriggerEvent;
import feathers.layout.ILayout;
import feathers.layout.ILayoutIndexObject;
import feathers.layout.LayoutBoundsResult;
import feathers.layout.Measurements;
import feathers.skins.IProgrammaticSkin;
import feathers.utils.AbstractDisplayObjectRecycler;
import feathers.utils.DisplayObjectRecycler;
import feathers.utils.MeasurementsUtil;
import haxe.ds.ObjectMap;
import haxe.ds.StringMap;
import openfl.display.DisplayObject;
import openfl.errors.IllegalOperationError;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import openfl.ui.Keyboard;
#if (openfl >= "9.1.0")
import openfl.utils.ObjectPool;
#else
import openfl._internal.utils.ObjectPool;
#end
/**
A line of tabs, where one may be selected at a time.
The following example sets the data provider, tells the tabs how to
interpret the data, selects the second tab, and listens for when the
selection changes:
```haxe
var tabs = new TabBar();
tabs.dataProvider = new ArrayCollection([
{ text: "Latest Posts" },
{ text: "Profile" },
{ text: "Settings" }
]);
tabBar.itemToText = (item:Dynamic) -> {
return item.text;
};
tabs.selectedIndex = 1;
tabs.addEventListener(Event.CHANGE, tabs_changeHandler);
this.addChild(tabs);
```
@event openfl.events.Event.CHANGE Dispatched when either
`TabBar.selectedItem` or `TabBar.selectedIndex` changes.
@event feathers.events.TabBarEvent.ITEM_TRIGGER Dispatched when the user
taps or clicks a tab. The pointer must remain within the bounds of the tab
on release, or the gesture will be ignored.
@see [Tutorial: How to use the TabBar component](https://feathersui.com/learn/haxe-openfl/tab-bar/)
@see `feathers.controls.navigators.TabNavigator`
@since 1.0.0
**/
@:event(openfl.events.Event.CHANGE)
@:event(feathers.events.TabBarEvent.ITEM_TRIGGER)
@:access(feathers.data.TabBarItemState)
@defaultXmlProperty("dataProvider")
@:styleContext
class TabBar extends FeathersControl implements IIndexSelector implements IDataSelector<Dynamic> implements IFocusObject {
private static final INVALIDATION_FLAG_TAB_FACTORY = InvalidationFlag.CUSTOM("tabFactory");
/**
The variant used to style the tab child components in a theme.
To override this default variant, set the
`TabBar.customTabVariant` property.
@see `TabBar.customTabVariant`
@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_TAB = "tabBar_tab";
private static final RESET_TAB_STATE = new TabBarItemState();
private static function defaultItemToText(data:Dynamic):String {
return Std.string(data);
}
private static function defaultItemToEnabled(data:Dynamic):Bool {
return true;
}
private static function defaultUpdateTab(tab:ToggleButton, state:TabBarItemState):Void {
tab.text = state.text;
}
private static function defaultResetTab(tab:ToggleButton, state:TabBarItemState):Void {
tab.text = null;
}
/**
Creates a new `TabBar` object.
@since 1.0.0
**/
public function new(?dataProvider:IFlatCollection<Dynamic>, ?changeListener:(Event) -> Void) {
initializeTabBarTheme();
super();
this.dataProvider = dataProvider;
this.tabEnabled = true;
this.tabChildren = false;
this.addEventListener(KeyboardEvent.KEY_DOWN, tabBar_keyDownHandler);
if (changeListener != null) {
this.addEventListener(Event.CHANGE, changeListener);
}
}
private var _dataProvider:IFlatCollection<Dynamic> = null;
/**
The collection of data displayed by the tab bar.
Items in the collection must be class instances or anonymous structures.
Do not add primitive values (such as strings, booleans, or numeric
values) directly to the collection.
Additionally, all items in the collection must be unique object
instances. Do not add the same instance to the collection more than
once because a runtime exception will be thrown.
The following example passes in a data provider and tells the tabs how
to interpret the data:
```haxe
tabBar.dataProvider = new ArrayCollection([
{ text: "Latest Posts" },
{ text: "Profile" },
{ text: "Settings" }
]);
tabBar.itemToText = (item:Dynamic) -> {
return item.text;
};
```
@default null
@see `feathers.data.ArrayCollection`
@since 1.0.0
**/
@:bindable("dataChange")
public var dataProvider(get, set):IFlatCollection<Dynamic>;
private function get_dataProvider():IFlatCollection<Dynamic> {
return this._dataProvider;
}
private function set_dataProvider(value:IFlatCollection<Dynamic>):IFlatCollection<Dynamic> {
if (this._dataProvider == value) {
return this._dataProvider;
}
if (this._dataProvider != null) {
this._dataProvider.removeEventListener(Event.CHANGE, tabBar_dataProvider_changeHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.ADD_ITEM, tabBar_dataProvider_addItemHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.REMOVE_ITEM, tabBar_dataProvider_removeItemHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.REPLACE_ITEM, tabBar_dataProvider_replaceItemHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.REMOVE_ALL, tabBar_dataProvider_removeAllHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.RESET, tabBar_dataProvider_resetHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.SORT_CHANGE, tabBar_dataProvider_sortChangeHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.FILTER_CHANGE, tabBar_dataProvider_filterChangeHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.UPDATE_ITEM, tabBar_dataProvider_updateItemHandler);
this._dataProvider.removeEventListener(FlatCollectionEvent.UPDATE_ALL, tabBar_dataProvider_updateAllHandler);
}
var oldSelectedIndex = this._selectedIndex;
var oldSelectedItem = this._selectedItem;
this._dataProvider = value;
if (this._dataProvider != null) {
this._dataProvider.addEventListener(Event.CHANGE, tabBar_dataProvider_changeHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.ADD_ITEM, tabBar_dataProvider_addItemHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.REMOVE_ITEM, tabBar_dataProvider_removeItemHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.REPLACE_ITEM, tabBar_dataProvider_replaceItemHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.REMOVE_ALL, tabBar_dataProvider_removeAllHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.RESET, tabBar_dataProvider_resetHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.SORT_CHANGE, tabBar_dataProvider_sortChangeHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.FILTER_CHANGE, tabBar_dataProvider_filterChangeHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.UPDATE_ITEM, tabBar_dataProvider_updateItemHandler);
this._dataProvider.addEventListener(FlatCollectionEvent.UPDATE_ALL, tabBar_dataProvider_updateAllHandler);
}
if (this._dataProvider == null || this._dataProvider.length == 0) {
this._selectedIndex = -1;
this._selectedItem = null;
} else {
this._selectedIndex = 0;
this._selectedItem = this._dataProvider.get(0);
}
if (this._selectedIndex != oldSelectedIndex || this._selectedItem != oldSelectedItem) {
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
}
this.setInvalid(DATA);
FeathersEvent.dispatch(this, "dataChange");
return this._dataProvider;
}
private var _selectedIndex:Int = -1;
/**
@see `feathers.core.IIndexSelector.selectedIndex`
**/
@:bindable("change")
@:inspectable
public var selectedIndex(get, set):Int;
private function get_selectedIndex():Int {
return this._selectedIndex;
}
private function set_selectedIndex(value:Int):Int {
if (this._dataProvider == null) {
value = -1;
}
if (this._selectedIndex == value) {
return this._selectedIndex;
}
if (value == -1) {
this._selectedIndex = -1;
this._selectedItem = null;
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedIndex;
}
this._selectedIndex = value;
this._selectedItem = this._dataProvider.get(this._selectedIndex);
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedIndex;
}
/**
@see `feathers.core.IIndexSelector.maxSelectedIndex`
**/
public var maxSelectedIndex(get, never):Int;
private function get_maxSelectedIndex():Int {
if (this._dataProvider == null) {
return -1;
}
return this._dataProvider.length - 1;
}
private var _selectedItem:Dynamic = null;
/**
@see `feathers.core.IDataSelector.selectedItem`
**/
@:bindable("change")
public var selectedItem(get, set):Dynamic;
private function get_selectedItem():Dynamic {
return this._selectedItem;
}
private function set_selectedItem(value:Dynamic):Dynamic {
if (value == null || this._dataProvider == null) {
// use the setter
this.selectedIndex = -1;
return this._selectedItem;
}
var index = this._dataProvider.indexOf(value);
if (index == -1) {
// use the setter
this.selectedIndex = -1;
return this._selectedItem;
}
if (this._selectedItem == value && this._selectedIndex == index) {
return this._selectedItem;
}
this._selectedIndex = index;
this._selectedItem = value;
this.setInvalid(SELECTION);
FeathersEvent.dispatch(this, Event.CHANGE);
return this._selectedItem;
}
private var _previousCustomTabVariant:String = null;
/**
A custom variant to set on all tabs, instead of
`TabBar.CHILD_VARIANT_TAB`.
The `customTabVariant` will be not be used if the result of
`tabRecycler.create()` already has a variant set.
@see `TabBar.CHILD_VARIANT_TAB`
@see `feathers.style.IVariantStyleObject.variant`
@since 1.0.0
**/
@:style
public var customTabVariant:String = null;
/**
Manages tabs used by the tab bar.
In the following example, the tab bar uses a custom tab renderer class:
```haxe
tabBar.tabRecycler = DisplayObjectRecycler.withClass(ToggleButton);
```
@since 1.0.0
**/
public var tabRecycler(get, set):AbstractDisplayObjectRecycler<Dynamic, TabBarItemState, ToggleButton>;
private function get_tabRecycler():AbstractDisplayObjectRecycler<Dynamic, TabBarItemState, ToggleButton> {
return this._defaultStorage.tabRecycler;
}
private function set_tabRecycler(value:AbstractDisplayObjectRecycler<Dynamic, TabBarItemState, ToggleButton>):AbstractDisplayObjectRecycler<Dynamic,
TabBarItemState, ToggleButton> {
if (this._defaultStorage.tabRecycler == value) {
return this._defaultStorage.tabRecycler;
}
this._defaultStorage.oldTabRecycler = this._defaultStorage.tabRecycler;
this._defaultStorage.tabRecycler = value;
this.setInvalid(INVALIDATION_FLAG_TAB_FACTORY);
return this._defaultStorage.tabRecycler;
}
private var _forceItemStateUpdate:Bool = false;
/**
Forces the `tabRecycler.update()` method to be called with the
`TabBarItemState` when the tab bar validates, even if the item's
state has not changed since the previous validation.
Before Feathers UI 1.2, `update()` was called more frequently, and this
property is provided to enable backwards compatibility, temporarily, to
assist in migration from earlier versions of Feathers UI.
In general, when this property needs to be enabled, its often because of
a missed call to `dataProvider.updateAt()` (preferred) or
`dataProvider.updateAll()` (less common).
The `forceItemStateUpdate` property may be removed in a future major
version, so it is best to avoid relying on it as a long-term solution.
@since 1.2.0
**/
public var forceItemStateUpdate(get, set):Bool;
private function get_forceItemStateUpdate():Bool {
return this._forceItemStateUpdate;
}
private function set_forceItemStateUpdate(value:Bool):Bool {
if (this._forceItemStateUpdate == value) {
return this._forceItemStateUpdate;
}
this._forceItemStateUpdate = value;
this.setInvalid(DATA);
return this._forceItemStateUpdate;
}
private var _recyclerMap:Map<String, DisplayObjectRecycler<Dynamic, TabBarItemState, ToggleButton>> = null;
private var _tabRecyclerIDFunction:(state:TabBarItemState) -> String;
/**
When a tab bar requires multiple tab styles, this function is used to
determine which style of tab is required for a specific item. Returns
the ID of the tab recycler to use for the item, or `null` if the default
`tabRecycler` should be used.
The following example provides an `tabRecyclerIDFunction`:
```haxe
var regularTabRecycler = DisplayObjectRecycler.withClass(ToggleButton);
var firstTabRecycler = DisplayObjectRecycler.withClass(MyCustomToggleButton);
tabBar.setTabRecycler("regular-tab", regularTabRecycler);
tabBar.setTabRecycler("first-tab", firstTabRecycler);
tabBar.tabRecyclerIDFunction = function(state:TabBarItemState):String {
if(state.index == 0) {
return "first-tab";
}
return "regular-tab";
};
```
@default null
@see `TabBar.setTabRecycler()`
@see `TabBar.tabRecycler
@since 1.0.0
**/
public var tabRecyclerIDFunction(get, set):(state:TabBarItemState) -> String;
private function get_tabRecyclerIDFunction():(state:TabBarItemState) -> String {
return this._tabRecyclerIDFunction;
}
private function set_tabRecyclerIDFunction(value:(state:TabBarItemState) -> String):(state:TabBarItemState) -> String {
if (this._tabRecyclerIDFunction == value) {
return this._tabRecyclerIDFunction;
}
this._tabRecyclerIDFunction = value;
this.setInvalid(INVALIDATION_FLAG_TAB_FACTORY);
return this._tabRecyclerIDFunction;
}
private var _defaultStorage:TabStorage = new TabStorage(null, DisplayObjectRecycler.withClass(ToggleButton));
private var _additionalStorage:Array<TabStorage> = null;
private var objectDataToTab = new ObjectMap<Dynamic, ToggleButton>();
private var stringDataToTab = new StringMap<ToggleButton>();
private var tabToItemState = new ObjectMap<ToggleButton, TabBarItemState>();
private var itemStatePool = new ObjectPool(() -> new TabBarItemState());
private var _unrenderedData:Array<Dynamic> = [];
private var _layoutItems:Array<DisplayObject> = [];
private var _ignoreSelectionChange = false;
private var _itemToText:(Dynamic) -> String = defaultItemToText;
/**
Converts an item to text to display within tab bar. By default, the
`toString()` method is called to convert an item to text. This method
may be replaced to provide custom text.
For example, consider the following item:
```haxe
{ text: "Example Item" }
```
If the `TabBar` should display the text "Example Item", a custom
implementation of `itemToText()` might look like this:
```haxe
tabBar.itemToText = (item:Dynamic) -> {
return item.text;
};
```
@since 1.0.0
**/
public var itemToText(get, set):(Dynamic) -> String;
private function get_itemToText():(Dynamic) -> String {
return this._itemToText;
}
private function set_itemToText(value:(Dynamic) -> String):(Dynamic) -> String {
if (value == null) {
value = defaultItemToText;
}
if (this._itemToText == value || Reflect.compareMethods(this._itemToText, value)) {
return this._itemToText;
}
this._itemToText = value;
this.setInvalid(DATA);
return this._itemToText;
}
private var _itemToEnabled:(Dynamic) -> Bool = defaultItemToEnabled;
/**
Determines if a tab should be enabled or disabled. By default, all
items are enabled, unless the `TabBar` is disabled. This method
may be replaced to provide a custom value for `enabled`.
For example, consider the following item:
```haxe
{ text: "Example Item", disable: true }
```
If the `TabBar` should disable an item if the `disable` field is
`true`, a custom implementation of `itemToEnabled()` might look like
this:
```haxe
tabBar.itemToEnabled = (item:Dynamic) -> {
return !item.disable;
};
```
@since 1.2.0
**/
public var itemToEnabled(get, set):(Dynamic) -> Bool;
private function get_itemToEnabled():(Dynamic) -> Bool {
return this._itemToEnabled;
}
private function set_itemToEnabled(value:(Dynamic) -> Bool):(Dynamic) -> Bool {
if (value == null) {
value = defaultItemToEnabled;
}
if (this._itemToEnabled == value || Reflect.compareMethods(this._itemToEnabled, value)) {
return this._itemToEnabled;
}
this._itemToEnabled = value;
this.setInvalid(DATA);
return this._itemToEnabled;
}
/**
The layout algorithm used to position and size the tabs.
By default, if no layout is provided by the time that the tab bar
initializes, a default layout that displays items horizontally will be
created.
The following example tells the tab bar to use a custom layout:
```haxe
var layout = new HorizontalDistributedLayout();
layout.maxItemWidth = 300.0;
tabBar.layout = layout;
```
@since 1.0.0
**/
@:style
public var layout:ILayout = null;
private var _currentBackgroundSkin:DisplayObject = null;
private var _backgroundSkinMeasurements:Measurements = null;
/**
The default background skin to display behind the tabs.
The following example passes a bitmap for the tab bar to use as a
background skin:
```haxe
tabBar.backgroundSkin = new Bitmap(bitmapData);
```
@default null
@see `TabBar.disabledBackgroundSkin`
@since 1.0.0
**/
@:style
public var backgroundSkin:DisplayObject = null;
/**
A background skin to display behind the tabs when the tab bar is
disabled.
The following example gives the tab bar a disabled background skin:
```haxe
tabBar.disabledBackgroundSkin = new Bitmap(bitmapData);
tabBar.enabled = false;
```
@default null
@see `TabBar.backgroundSkin`
@since 1.0.0
**/
@:style
public var disabledBackgroundSkin:DisplayObject = null;
private var _layoutMeasurements = new Measurements();
private var _layoutResult = new LayoutBoundsResult();
private var _ignoreChildChanges = false;
/**
Returns the current tab used to render a specific item from the data
provider. May return `null` if an item doesn't currently have a tab.
@since 1.0.0
**/
public function itemToTab(item:Dynamic):ToggleButton {
if (item == null) {
return null;
}
if ((item is String)) {
return this.stringDataToTab.get(cast item);
}
return this.objectDataToTab.get(item);
}
/**
Returns the current tab used to render the item at the specified index
in the data provider. May return `null` if an item doesn't currently
have a tab.
@since 1.0.0
**/
public function indexToTab(index:Int):ToggleButton {
if (this._dataProvider == null || index < 0 || index >= this._dataProvider.length) {
return null;
}
var item = this._dataProvider.get(index);
if ((item is String)) {
return this.stringDataToTab.get(cast item);
}
return this.objectDataToTab.get(item);
}
/**
Returns a `TabBarItemState` representing a specific item.
@since 1.3.0
**/
public function itemToItemState(item:Dynamic):TabBarItemState {
if (item == null) {
return null;
}
var itemState:TabBarItemState = null;
var tab:ToggleButton = null;
if ((item is String)) {
tab = this.stringDataToTab.get(cast item);
} else {
tab = this.objectDataToTab.get(item);
}
if (tab != null) {
itemState = this.tabToItemState.get(tab);
} else {
var index = this._dataProvider.indexOf(item);
if (index == -1) {
return null;
}
itemState = new TabBarItemState();
this.populateCurrentItemState(item, index, itemState, false);
}
return itemState;
}
/**
Returns the tab recycler associated with a specific ID. Returns `null`
if no recycler is associated with the ID.
@see `TabBar.tabRecyclerIDFunction`
@see `TabBar.setTabRecycler()`
@since 1.0.0
**/
public function getTabRecycler(id:String):DisplayObjectRecycler<Dynamic, TabBarItemState, ToggleButton> {
if (this._recyclerMap == null) {
return null;
}
return this._recyclerMap.get(id);
}
/**
Associates an tab recycler with an ID to allow multiple types
of tabs to be displayed in the tab bar. A custom `tabRecyclerIDFunction`
may be specified to return the ID of the recycler to use for a specific
item in the data provider.
To clear a recycler, pass in `null` as the value.
@see `TabBar.tabRecyclerIDFunction`
@see `TabBar.getTabRecycler()`
@since 1.0.0
**/
public function setTabRecycler(id:String, recycler:AbstractDisplayObjectRecycler<Dynamic, TabBarItemState, ToggleButton>):Void {
if (this._recyclerMap == null) {
this._recyclerMap = [];
}
if (recycler == null) {
this._recyclerMap.remove(id);
return;
}
this._recyclerMap.set(id, recycler);
this.setInvalid(INVALIDATION_FLAG_TAB_FACTORY);
}
override public function dispose():Void {
this.refreshInactiveTabs(this._defaultStorage, true);
if (this._additionalStorage != null) {
for (i in 0...this._additionalStorage.length) {
var storage = this._additionalStorage[i];
this.refreshInactiveTabs(storage, true);
}
}
// manually clear the selection so that removing the data provider
// doesn't result in Event.CHANGE getting dispatched
this._selectedItem = null;
this._selectedIndex = -1;
this.dataProvider = null;
super.dispose();
}
private function initializeTabBarTheme():Void {
#if !feathersui_disable_default_theme
feathers.themes.steel.components.SteelTabBarStyles.initialize();
#end
}
override private function update():Void {
var dataInvalid = this.isInvalid(DATA);
var layoutInvalid = this.isInvalid(LAYOUT);
var selectionInvalid = this.isInvalid(SELECTION);
var stateInvalid = this.isInvalid(STATE);
var stylesInvalid = this.isInvalid(STYLES);
if (this._previousCustomTabVariant != this.customTabVariant) {
this.setInvalidationFlag(INVALIDATION_FLAG_TAB_FACTORY);
}
var tabsInvalid = this.isInvalid(INVALIDATION_FLAG_TAB_FACTORY);
if (stylesInvalid || stateInvalid) {
this.refreshBackgroundSkin();
}
if (tabsInvalid || selectionInvalid || stateInvalid || dataInvalid) {
this.refreshTabs();
}
this.refreshViewPortBounds();
this.handleLayout();
this.handleLayoutResult();
this.layoutBackgroundSkin();
// final invalidation to avoid juggler next frame issues
this.validateChildren();
this._previousCustomTabVariant = this.customTabVariant;
}
private function refreshViewPortBounds():Void {
var needsMinWidth = this.explicitMinWidth == null;
var needsMinHeight = this.explicitMinHeight == null;
var needsMaxWidth = this.explicitMaxWidth == null;
var needsMaxHeight = this.explicitMaxHeight == null;
if (this._currentBackgroundSkin != null) {
MeasurementsUtil.resetFluidlyWithParent(this._backgroundSkinMeasurements, this._currentBackgroundSkin, this);
if ((this._currentBackgroundSkin is IValidating)) {
(cast this._currentBackgroundSkin : IValidating).validateNow();
}
}
var viewPortMinWidth = this.explicitMinWidth;
if (needsMinWidth) {
viewPortMinWidth = 0.0;
}
var viewPortMinHeight = this.explicitMinHeight;
if (needsMinHeight) {
viewPortMinHeight = 0.0;
}
var viewPortMaxWidth = this.explicitMaxWidth;
if (needsMaxWidth) {
viewPortMaxWidth = 1.0 / 0.0; // Math.POSITIVE_INFINITY bug workaround for swf
}
var viewPortMaxHeight = this.explicitMaxHeight;
if (needsMaxHeight) {
viewPortMaxHeight = 1.0 / 0.0; // Math.POSITIVE_INFINITY bug workaround for swf
}
if (this._backgroundSkinMeasurements != null) {
// because the layout might need it, we account for the
// dimensions of the background skin when determining the minimum
// dimensions of the view port.
if (this._backgroundSkinMeasurements.width != null) {
if (this._backgroundSkinMeasurements.width > viewPortMinWidth) {
viewPortMinWidth = this._backgroundSkinMeasurements.width;
}
} else if (this._backgroundSkinMeasurements.minWidth != null) {
if (this._backgroundSkinMeasurements.minWidth > viewPortMinWidth) {
viewPortMinWidth = this._backgroundSkinMeasurements.minWidth;
}
}
if (this._backgroundSkinMeasurements.height != null) {
if (this._backgroundSkinMeasurements.height > viewPortMinHeight) {
viewPortMinHeight = this._backgroundSkinMeasurements.height;
}
} else if (this._backgroundSkinMeasurements.minHeight != null) {
if (this._backgroundSkinMeasurements.minHeight > viewPortMinHeight) {
viewPortMinHeight = this._backgroundSkinMeasurements.minHeight;
}
}
}
this._layoutMeasurements.width = this.explicitWidth;
this._layoutMeasurements.height = this.explicitHeight;
this._layoutMeasurements.minWidth = viewPortMinWidth;
this._layoutMeasurements.minHeight = viewPortMinHeight;
this._layoutMeasurements.maxWidth = viewPortMaxWidth;
this._layoutMeasurements.maxHeight = viewPortMaxHeight;
}
private function handleLayout():Void {
var oldIgnoreChildChanges = this._ignoreChildChanges;
this._ignoreChildChanges = true;
this._layoutResult.reset();
if (this.layout != null) {
this.layout.layout(this._layoutItems, this._layoutMeasurements, this._layoutResult);
} else {
var viewPortWidth = this._layoutMeasurements.width;
if (viewPortWidth == null) {
if (this._layoutMeasurements.minWidth != null) {
viewPortWidth = this._layoutMeasurements.minWidth;
} else if (this._layoutMeasurements.minHeight != null) {
viewPortWidth = this._layoutMeasurements.minHeight;
} else {
viewPortWidth = 0.0;
}
}
var viewPortHeight = this._layoutMeasurements.height;
if (viewPortHeight == null) {
if (this._layoutMeasurements.minHeight != null) {
viewPortHeight = this._layoutMeasurements.minHeight;
} else if (this._layoutMeasurements.maxHeight != null) {
viewPortHeight = this._layoutMeasurements.maxHeight;
} else {
viewPortHeight = 0.0;
}
}
this._layoutResult.contentX = 0.0;
this._layoutResult.contentY = 0.0;
this._layoutResult.contentWidth = viewPortWidth;
this._layoutResult.contentHeight = viewPortHeight;
this._layoutResult.viewPortWidth = viewPortWidth;
this._layoutResult.viewPortHeight = viewPortHeight;
}
this._ignoreChildChanges = oldIgnoreChildChanges;
}
private function handleLayoutResult():Void {
var viewPortWidth = this._layoutResult.viewPortWidth;
var viewPortHeight = this._layoutResult.viewPortHeight;
this.saveMeasurements(viewPortWidth, viewPortHeight, viewPortWidth, viewPortHeight);
}
private function validateChildren():Void {
for (tab in this._layoutItems) {
if (!(tab is IValidating)) {
return;
}
(cast tab : IValidating).validateNow();
}
}
private function refreshTabs():Void {
if (this.tabRecycler.update == null) {
this.tabRecycler.update = defaultUpdateTab;
if (this.tabRecycler.reset == null) {
this.tabRecycler.reset = defaultResetTab;
}
}
if (this._recyclerMap != null) {
for (recycler in this._recyclerMap) {
if (recycler.update == null) {
if (recycler.update == null) {
recycler.update = defaultUpdateTab;
// don't replace reset if we didn't replace update too
if (recycler.reset == null) {
recycler.reset = defaultResetTab;
}
}
}
}
}
var tabsInvalid = this.isInvalid(INVALIDATION_FLAG_TAB_FACTORY);
this.refreshInactiveTabs(this._defaultStorage, tabsInvalid);
if (this._additionalStorage != null) {
for (i in 0...this._additionalStorage.length) {
var storage = this._additionalStorage[i];
this.refreshInactiveTabs(storage, tabsInvalid);
}
}
this.findUnrenderedData();
this.recoverInactiveTabs(this._defaultStorage);
if (this._additionalStorage != null) {
for (i in 0...this._additionalStorage.length) {
var storage = this._additionalStorage[i];
this.recoverInactiveTabs(storage);
}
}
this.renderUnrenderedData();
this.freeInactiveTabs(this._defaultStorage);
if (this._defaultStorage.inactiveTabs.length > 0) {
throw new IllegalOperationError('${Type.getClassName(Type.getClass(this))}: inactive tabs should be empty after updating.');
}
if (this._additionalStorage != null) {
for (i in 0...this._additionalStorage.length) {
var storage = this._additionalStorage[i];
this.freeInactiveTabs(storage);
if (storage.inactiveTabs.length > 0) {
throw new IllegalOperationError('${Type.getClassName(Type.getClass(this))}: inactive tabs ${storage.id} should be empty after updating.');
}
}
}
}
private function refreshInactiveTabs(storage:TabStorage, factoryInvalid:Bool):Void {
var temp = storage.inactiveTabs;
storage.inactiveTabs = storage.activeTabs;
storage.activeTabs = temp;
if (storage.activeTabs.length > 0) {
throw new IllegalOperationError('${Type.getClassName(Type.getClass(this))}: active tabs should be empty before updating.');
}
if (factoryInvalid) {
this.recoverInactiveTabs(storage);
this.freeInactiveTabs(storage);
storage.oldTabRecycler = null;
}
}
private function recoverInactiveTabs(storage:TabStorage):Void {
for (tab in storage.inactiveTabs) {
if (tab == null) {
continue;
}
var state = this.tabToItemState.get(tab);
if (state == null) {
continue;
}
var item = state.data;
this.tabToItemState.remove(tab);
if ((item is String)) {
this.stringDataToTab.remove(cast item);
} else {
this.objectDataToTab.remove(item);
}
tab.removeEventListener(TriggerEvent.TRIGGER, tabBar_tab_triggerHandler);
tab.removeEventListener(Event.CHANGE, tabBar_tab_changeHandler);
this.resetTab(tab, state, storage);
this.itemStatePool.release(state);
}
}
private function freeInactiveTabs(storage:TabStorage):Void {
var recycler = storage.oldTabRecycler != null ? storage.oldTabRecycler : storage.tabRecycler;
for (tab in storage.inactiveTabs) {
if (tab == null) {
continue;
}
this.destroyTab(tab, recycler);
}
#if (hl && haxe_ver < 4.3)
storage.inactiveTabs.splice(0, storage.inactiveTabs.length);
#else
storage.inactiveTabs.resize(0);
#end
}
private function refreshBackgroundSkin():Void {
var oldSkin = this._currentBackgroundSkin;
this._currentBackgroundSkin = this.getCurrentBackgroundSkin();
if (this._currentBackgroundSkin == oldSkin) {
return;
}
this.removeCurrentBackgroundSkin(oldSkin);
this.addCurrentBackgroundSkin(this._currentBackgroundSkin);
}
private function getCurrentBackgroundSkin():DisplayObject {
if (!this._enabled && this.disabledBackgroundSkin != null) {
return this.disabledBackgroundSkin;
}
return this.backgroundSkin;
}
private function addCurrentBackgroundSkin(skin:DisplayObject):Void {
if (skin == null) {
this._backgroundSkinMeasurements = null;
return;