| 1 | import { EventData, Observable, Page, Color, TabView, TabViewItem, StackLayout, Label, Utils, GridLayout, ItemSpec } from '@nativescript/core'; |
| 2 | |
| 3 | class TabViewDemoModel extends Observable { |
| 4 | private page: Page; |
| 5 | private tabView: TabView; |
| 6 | |
| 7 | init(page: Page) { |
| 8 | this.page = page; |
| 9 | this.tabView = page.getViewById('demoTabView') as TabView; |
| 10 | |
| 11 | // Ensure initial font icon family so initial font:// icons render as expected |
| 12 | this.applyFontFamily('ns-playground-font'); |
| 13 | |
| 14 | // Give an initial color to demonstrate colorization of font icons |
| 15 | this.applyItemColor(new Color('#65ADF1')); |
| 16 | } |
| 17 | |
| 18 | useSysIcons = () => { |
| 19 | if (!this.tabView || !this.tabView.items) return; |
| 20 | |
| 21 | // Common SF Symbol names on iOS. Android will not render sys:// and this is expected. |
| 22 | const sysIcons = ['house.fill', 'star.fill', 'gearshape.fill']; |
| 23 | this.setIcons(sysIcons.map((name) => `sys://${name}`)); |
| 24 | }; |
| 25 | |
| 26 | useFontIcons = () => { |
| 27 | if (!this.tabView || !this.tabView.items) return; |
| 28 | |
| 29 | // Use simple glyphs A/B/C for reliability across fonts |
| 30 | const fontIcons = ['A', 'B', 'C'].map((c) => `font://${c}`); |
| 31 | this.setIcons(fontIcons); |
| 32 | this.applyFontFamily('ns-playground-font'); |
| 33 | }; |
| 34 | |
| 35 | clearIcons = () => { |
| 36 | if (!this.tabView || !this.tabView.items) return; |
| 37 | this.tabView.items.forEach((item) => { |
| 38 | item.iconSource = undefined; |
| 39 | }); |
| 40 | }; |
| 41 | |
| 42 | private setIcons(iconSources: string[]) { |
| 43 | const items = this.tabView.items as TabViewItem[]; |
| 44 | let isSystemIcons: boolean; |
| 45 | for (let i = 0; i < items.length; i++) { |
| 46 | const iconSource = iconSources[i % iconSources.length]; |
| 47 | items[i].iconSource = iconSource; |
| 48 | if (iconSource.startsWith(Utils.SYSTEM_PREFIX)) { |
| 49 | isSystemIcons = true; |
| 50 | } |
| 51 | } |
| 52 | if (__APPLE__) { |
| 53 | this.tabView.tabTextFontSize = isSystemIcons ? 11 : null; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private applyFontFamily(family: string) { |
| 58 | if (!this.tabView || !this.tabView.items) return; |
| 59 | (this.tabView.items as TabViewItem[]).forEach((item) => { |
| 60 | // Use indexer to avoid TS typing gap in core .d.ts |