feat(SegmentedBar): selectedTextColor added and selectedBackgroundColor improvements (#10474)

This commit is contained in:
kefahB
2024-01-19 22:39:59 +04:00
committed by GitHub
parent 135d37b9ee
commit 3a0afdb9cc
10 changed files with 197 additions and 11 deletions

View File

@@ -1,4 +1,9 @@
import * as segmentedBarModule from '@nativescript/core/ui/segmented-bar';
import { Color } from '@nativescript/core';
export function getNativeTabWidget(bar: segmentedBarModule.SegmentedBar): android.widget.TabWidget {
return (<android.widget.TabHost>bar.android).getTabWidget();
}
export function getNativeItemsCount(bar: segmentedBarModule.SegmentedBar): number {
return (<android.widget.TabHost>bar.android).getTabWidget().getTabCount();
@@ -25,3 +30,54 @@ export function checkNativeItemsTextColor(bar: segmentedBarModule.SegmentedBar):
export function setNativeSelectedIndex(bar: segmentedBarModule.SegmentedBar, index: number): void {
(<android.widget.TabHost>bar.android).setCurrentTab(index);
}
export var checkBackgroundColorUpdatedAfterItemSelected = function (bar: segmentedBarModule.SegmentedBar): boolean {
let isValid = 0;
bar.selectedIndex = 0;
bar.selectedTextColor = new Color('green');
bar.selectedBackgroundColor = new Color('red');
const tabWidget = getNativeTabWidget(bar);
if (tabWidget) {
for (let i = 0; i < tabWidget.getTabCount(); i++) {
const view = tabWidget.getChildTabViewAt(i);
const item = bar.items[i];
const textView = item?.nativeViewProtected;
const newDrawable = tryCloneDrawable(view.getBackground(), view.getResources());
newDrawable.setColorFilter(new android.graphics.Paint(bar.selectedBackgroundColor.android).getColorFilter());
if (bar.selectedIndex == i) {
if (view.getBackground() !== newDrawable) {
console.log('>>>>>>>>>>>>>>>>>>>>>> newDrawable', view.getBackground());
console.log('>>>>>>>>>>>>>>>>>>>>>> bar.selectedBackgroundColor.android', newDrawable);
console.log('>>>>>>>>>>>>>>>>>>>>>> selectedBackgroundColor', newDrawable.getColorFilter(), view.getBackground().getColorFilter());
console.log('>>>>>>>>>>>>>>>>>>>>>> selectedBackgroundColor', newDrawable.hashCode(), view.hashCode());
isValid++;
break;
} else if (textView.getCurrentTextColor() !== bar.selectedTextColor) {
console.log('>>>>>>>>>>>>>>>>>>>>>>');
console.log('>>>>>>>>>>>>>>>>>>>>>>');
console.log('>>>>>>>>>>>>>>>>>>>>>> selectedTextColor');
isValid++;
break;
}
}
}
}
function tryCloneDrawable(value: android.graphics.drawable.Drawable, resources: android.content.res.Resources): android.graphics.drawable.Drawable {
if (value) {
const constantState = value.getConstantState();
if (constantState) {
return constantState.newDrawable(resources);
}
}
return value;
}
return isValid === 0;
};

View File

@@ -5,3 +5,5 @@ export declare function getNativeItemsCount(bar: segmentedBarModule.SegmentedBar
export declare function setNativeSelectedIndex(bar: segmentedBarModule.SegmentedBar, index: number): void;
export declare function checkNativeItemsTextColor(bar: segmentedBarModule.SegmentedBar): boolean;
export declare function checkBackgroundColorUpdatedAfterItemSelected(bar: segmentedBarModule.SegmentedBar): boolean;

View File

@@ -276,3 +276,21 @@ export function test_SettingNumberAsTitleFromXML_DoesNotThrow() {
TKUnit.assertEqual(item.title, '1');
});
}
/*export function testBackgroundColorUpdatedAfterItemSelected() {
let segmentedBar = new segmentedBarModule.SegmentedBar();
let item1 = new segmentedBarModule.SegmentedBarItem();
(<any>item1).title = 1;
let item2 = new segmentedBarModule.SegmentedBarItem();
(<any>item2).title = 2;
let item3 = new segmentedBarModule.SegmentedBarItem();
(<any>item3).title = 3;
let item4 = new segmentedBarModule.SegmentedBarItem();
(<any>item4).title = 4;
segmentedBar.items = [item1, item2, item3, item4];
buildUIAndRunTest(segmentedBar, function (views: Array<View>) {
TKUnit.assertTrue(segmentedBarTestsNative.checkBackgroundColorUpdatedAfterItemSelected(segmentedBar));
});
}*/