@@ -5,7 +5,9 @@ import { TabContentItem } from "../tab-navigation-base/tab-content-item";
5
5
import { TextTransform } from "../text-base" ;
6
6
7
7
// Requires
8
- import { TabNavigationBase , itemsProperty , selectedIndexProperty , tabStripProperty } from "../tab-navigation-base/tab-navigation-base" ;
8
+ import {
9
+ TabNavigationBase , getIconSpecSize , itemsProperty , selectedIndexProperty , tabStripProperty
10
+ } from "../tab-navigation-base/tab-navigation-base" ;
9
11
import { Font } from "../styling/font" ;
10
12
import { getTransformedText } from "../text-base" ;
11
13
import { CSSType , Color } from "../core/view" ;
@@ -166,85 +168,6 @@ function initializeNativeClasses() {
166
168
AttachStateChangeListener = new AttachListener ( ) ;
167
169
}
168
170
169
- function createTabItemSpec ( tabStripItem : TabStripItem ) : org . nativescript . widgets . TabItemSpec {
170
- const tabItemSpec = new org . nativescript . widgets . TabItemSpec ( ) ;
171
-
172
- if ( tabStripItem . isLoaded ) {
173
- const titleLabel = tabStripItem . label ;
174
- let title = titleLabel . text ;
175
-
176
- // TEXT-TRANSFORM
177
- const textTransform = titleLabel . style . textTransform ;
178
- if ( textTransform ) {
179
- title = getTransformedText ( title , textTransform ) ;
180
- }
181
- tabItemSpec . title = title ;
182
-
183
- // BACKGROUND-COLOR
184
- const backgroundColor = tabStripItem . style . backgroundColor ;
185
- if ( backgroundColor ) {
186
- tabItemSpec . backgroundColor = backgroundColor . android ;
187
- }
188
-
189
- // COLOR
190
- const color = titleLabel . style . color ;
191
- if ( color ) {
192
- tabItemSpec . color = color . android ;
193
- }
194
-
195
- // FONT
196
- const fontInternal = titleLabel . style . fontInternal ;
197
- if ( fontInternal ) {
198
- tabItemSpec . fontSize = fontInternal . fontSize ;
199
- tabItemSpec . typeFace = fontInternal . getAndroidTypeface ( ) ;
200
- }
201
-
202
- // ICON
203
- const iconSource = tabStripItem . image && tabStripItem . image . src ;
204
- if ( iconSource ) {
205
- if ( iconSource . indexOf ( RESOURCE_PREFIX ) === 0 ) {
206
- tabItemSpec . iconId = ad . resources . getDrawableId ( iconSource . substr ( RESOURCE_PREFIX . length ) ) ;
207
- if ( tabItemSpec . iconId === 0 ) {
208
- // TODO:
209
- // traceMissingIcon(iconSource);
210
- }
211
- } else {
212
- const icon = _getIcon ( tabStripItem ) ;
213
-
214
- if ( icon ) {
215
- // TODO: Make this native call that accepts string so that we don't load Bitmap in JS.
216
- // tslint:disable-next-line:deprecation
217
- tabItemSpec . iconDrawable = icon ;
218
- } else {
219
- // TODO:
220
- // traceMissingIcon(iconSource);
221
- }
222
- }
223
- }
224
- }
225
-
226
- return tabItemSpec ;
227
- }
228
-
229
- function _getIcon ( tabStripItem : TabStripItem ) : android . graphics . drawable . BitmapDrawable {
230
- const iconSource = tabStripItem . image && tabStripItem . image . src ;
231
-
232
- let is : ImageSource ;
233
- if ( isFontIconURI ( iconSource ) ) {
234
- const fontIconCode = iconSource . split ( "//" ) [ 1 ] ;
235
- const target = tabStripItem . image ? tabStripItem . image : tabStripItem ;
236
- const font = target . style . fontInternal ;
237
- const color = target . style . color ;
238
- is = fromFontIconCode ( fontIconCode , font , color ) ;
239
- } else {
240
- is = fromFileOrResource ( iconSource ) ;
241
- }
242
-
243
- const image = new android . graphics . drawable . BitmapDrawable ( application . android . context . getResources ( ) , is . android ) ;
244
-
245
- return image ;
246
- }
247
-
248
171
function setElevation ( bottomNavigationBar : org . nativescript . widgets . BottomNavigationBar ) {
249
172
const compat = < any > androidx . core . view . ViewCompat ;
250
173
if ( compat . setElevation ) {
@@ -566,7 +489,7 @@ export class BottomNavigation extends TabNavigationBase {
566
489
items . forEach ( ( item , i , arr ) => {
567
490
( < any > item ) . index = i ;
568
491
if ( items [ i ] ) {
569
- const tabItemSpec = createTabItemSpec ( items [ i ] ) ;
492
+ const tabItemSpec = this . createTabItemSpec ( items [ i ] ) ;
570
493
tabItems . push ( tabItemSpec ) ;
571
494
}
572
495
} ) ;
@@ -579,6 +502,111 @@ export class BottomNavigation extends TabNavigationBase {
579
502
} ) ;
580
503
}
581
504
505
+ private createTabItemSpec ( tabStripItem : TabStripItem ) : org . nativescript . widgets . TabItemSpec {
506
+ const tabItemSpec = new org . nativescript . widgets . TabItemSpec ( ) ;
507
+
508
+ if ( tabStripItem . isLoaded ) {
509
+ const titleLabel = tabStripItem . label ;
510
+ let title = titleLabel . text ;
511
+
512
+ // TEXT-TRANSFORM
513
+ const textTransform = titleLabel . style . textTransform ;
514
+ if ( textTransform ) {
515
+ title = getTransformedText ( title , textTransform ) ;
516
+ }
517
+ tabItemSpec . title = title ;
518
+
519
+ // BACKGROUND-COLOR
520
+ const backgroundColor = tabStripItem . style . backgroundColor ;
521
+ if ( backgroundColor ) {
522
+ tabItemSpec . backgroundColor = backgroundColor . android ;
523
+ }
524
+
525
+ // COLOR
526
+ const color = titleLabel . style . color ;
527
+ if ( color ) {
528
+ tabItemSpec . color = color . android ;
529
+ }
530
+
531
+ // FONT
532
+ const fontInternal = titleLabel . style . fontInternal ;
533
+ if ( fontInternal ) {
534
+ tabItemSpec . fontSize = fontInternal . fontSize ;
535
+ tabItemSpec . typeFace = fontInternal . getAndroidTypeface ( ) ;
536
+ }
537
+
538
+ // ICON
539
+ const iconSource = tabStripItem . image && tabStripItem . image . src ;
540
+ if ( iconSource ) {
541
+ if ( iconSource . indexOf ( RESOURCE_PREFIX ) === 0 ) {
542
+ tabItemSpec . iconId = ad . resources . getDrawableId ( iconSource . substr ( RESOURCE_PREFIX . length ) ) ;
543
+ if ( tabItemSpec . iconId === 0 ) {
544
+ // TODO:
545
+ // traceMissingIcon(iconSource);
546
+ }
547
+ } else {
548
+ const icon = this . getIcon ( tabStripItem ) ;
549
+
550
+ if ( icon ) {
551
+ // TODO: Make this native call that accepts string so that we don't load Bitmap in JS.
552
+ // tslint:disable-next-line:deprecation
553
+ tabItemSpec . iconDrawable = icon ;
554
+ } else {
555
+ // TODO:
556
+ // traceMissingIcon(iconSource);
557
+ }
558
+ }
559
+ }
560
+ }
561
+
562
+ return tabItemSpec ;
563
+ }
564
+
565
+ private getIcon ( tabStripItem : TabStripItem ) : android . graphics . drawable . BitmapDrawable {
566
+ const iconSource = tabStripItem . image && tabStripItem . image . src ;
567
+
568
+ let is : ImageSource ;
569
+ if ( isFontIconURI ( iconSource ) ) {
570
+ const fontIconCode = iconSource . split ( "//" ) [ 1 ] ;
571
+ const target = tabStripItem . image ? tabStripItem . image : tabStripItem ;
572
+ const font = target . style . fontInternal ;
573
+ const color = target . style . color ;
574
+ is = fromFontIconCode ( fontIconCode , font , color ) ;
575
+ } else {
576
+ is = fromFileOrResource ( iconSource ) ;
577
+ }
578
+
579
+ let imageDrawable : android . graphics . drawable . BitmapDrawable ;
580
+ if ( is && is . android ) {
581
+ let image = is . android ;
582
+
583
+ if ( this . tabStrip && this . tabStrip . isIconSizeFixed ) {
584
+ image = this . getFixedSizeIcon ( image ) ;
585
+ }
586
+
587
+ imageDrawable = new android . graphics . drawable . BitmapDrawable ( application . android . context . getResources ( ) , image ) ;
588
+ } else {
589
+ // TODO
590
+ // traceMissingIcon(iconSource);
591
+ }
592
+
593
+ return imageDrawable ;
594
+ }
595
+
596
+ private getFixedSizeIcon ( image : android . graphics . Bitmap ) : android . graphics . Bitmap {
597
+ const inWidth = image . getWidth ( ) ;
598
+ const inHeight = image . getHeight ( ) ;
599
+
600
+ const iconSpecSize = getIconSpecSize ( { width : inWidth , height : inHeight } ) ;
601
+
602
+ const widthPixels = iconSpecSize . width * layout . getDisplayDensity ( ) ;
603
+ const heightPixels = iconSpecSize . height * layout . getDisplayDensity ( ) ;
604
+
605
+ const scaledImage = android . graphics . Bitmap . createScaledBitmap ( image , widthPixels , heightPixels , true ) ;
606
+
607
+ return scaledImage ;
608
+ }
609
+
582
610
public updateAndroidItemAt ( index : number , spec : org . nativescript . widgets . TabItemSpec ) {
583
611
this . _bottomNavigationBar . updateItemAt ( index , spec ) ;
584
612
}
@@ -598,14 +626,14 @@ export class BottomNavigation extends TabNavigationBase {
598
626
public setTabBarItemTitle ( tabStripItem : TabStripItem , value : string ) : void {
599
627
// TODO: Should figure out a way to do it directly with the the nativeView
600
628
const tabStripItemIndex = this . tabStrip . items . indexOf ( tabStripItem ) ;
601
- const tabItemSpec = createTabItemSpec ( tabStripItem ) ;
629
+ const tabItemSpec = this . createTabItemSpec ( tabStripItem ) ;
602
630
this . updateAndroidItemAt ( tabStripItemIndex , tabItemSpec ) ;
603
631
}
604
632
605
633
public setTabBarItemBackgroundColor ( tabStripItem : TabStripItem , value : android . graphics . drawable . Drawable | Color ) : void {
606
634
// TODO: Should figure out a way to do it directly with the the nativeView
607
635
const tabStripItemIndex = this . tabStrip . items . indexOf ( tabStripItem ) ;
608
- const tabItemSpec = createTabItemSpec ( tabStripItem ) ;
636
+ const tabItemSpec = this . createTabItemSpec ( tabStripItem ) ;
609
637
this . updateAndroidItemAt ( tabStripItemIndex , tabItemSpec ) ;
610
638
}
611
639
@@ -621,7 +649,7 @@ export class BottomNavigation extends TabNavigationBase {
621
649
const index = ( < any > tabStripItem ) . index ;
622
650
const tabBarItem = this . _bottomNavigationBar . getViewForItemAt ( index ) ;
623
651
const imgView = < android . widget . ImageView > tabBarItem . getChildAt ( 0 ) ;
624
- const drawable = _getIcon ( tabStripItem ) ;
652
+ const drawable = this . getIcon ( tabStripItem ) ;
625
653
626
654
imgView . setImageDrawable ( drawable ) ;
627
655
}
0 commit comments