Skip to content

Commit 107bd84

Browse files
authored
Merge pull request #9 from Akylas/fix/android-background-color-reset
fix(android): reset background color when it becomes transparent or is removed
2 parents 830aa88 + c15a176 commit 107bd84

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎apps/automated/src/ui/view/view-tests.android.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,66 @@ export function testSetAndRemoveBackgroundColor() {
324324
});
325325
}
326326

327+
function getNativeBackgroundColor(v: View): number {
328+
const drawable = (<android.view.View>v.android).getBackground();
329+
if (drawable instanceof org.nativescript.widgets.BorderDrawable) {
330+
return drawable.getBackgroundColor();
331+
} else if (drawable instanceof android.graphics.drawable.ColorDrawable) {
332+
return drawable.getColor();
333+
}
334+
335+
return drawable ? undefined : 0;
336+
}
337+
338+
export function testSetTransparentAndRemoveBackgroundColorWithBorderRadius() {
339+
const lbl = new Label();
340+
lbl.text = 'segment';
341+
lbl.borderRadius = 8;
342+
lbl.backgroundColor = new Color('red');
343+
helper.buildUIAndRunTest(lbl, function (views: Array<View>) {
344+
helper.waitUntilLayoutReady(lbl);
345+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), new Color('red').android, 'BackgroundColor not applied correctly!');
346+
347+
lbl.backgroundColor = new Color('transparent');
348+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), 0, 'transparent BackgroundColor not applied!');
349+
350+
lbl.backgroundColor = new Color('red');
351+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), new Color('red').android, 'BackgroundColor not applied correctly!');
352+
353+
lbl.backgroundColor = unsetValue;
354+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), 0, 'BackgroundColor not removed!');
355+
});
356+
}
357+
358+
export function testToggleClassBackgroundColorFromCssVariableWithBorderRadius() {
359+
const stack = new StackLayout();
360+
stack.className = 'segments';
361+
stack.css = `
362+
.segments { --tile-color: red; }
363+
.segment { border-radius: 8; background-color: transparent; }
364+
.segment-selected { background-color: var(--tile-color); }
365+
.no-transparent-segment { border-radius: 8; }
366+
`;
367+
const lbl = new Label();
368+
lbl.text = 'segment';
369+
lbl.className = 'segment segment-selected';
370+
stack.addChild(lbl);
371+
helper.buildUIAndRunTest(stack, function (views: Array<View>) {
372+
helper.waitUntilLayoutReady(lbl);
373+
const red = new Color('red').android;
374+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), red, 'selected BackgroundColor not applied!');
375+
376+
lbl.className = 'segment';
377+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), 0, 'BackgroundColor should be transparent after deselection!');
378+
379+
lbl.className = 'no-transparent-segment segment-selected';
380+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), red, 'selected BackgroundColor not applied!');
381+
382+
lbl.className = 'no-transparent-segment';
383+
TKUnit.assertEqual(getNativeBackgroundColor(lbl), 0, 'BackgroundColor should be reset after deselection!');
384+
});
385+
}
386+
327387
export function testBackgroundImage() {
328388
const lbl = _createLabelWithBorder();
329389
lbl.className = 'myClass';

‎packages/core/ui/core/view/index.android.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,13 +1283,14 @@ export class View extends ViewCommon {
12831283
const nativeView = this.nativeViewProtected as NativeScriptAndroidView;
12841284
const canUseOutlineProvider = !background.hasBorderWidth() && !background.hasBoxShadows() && !background.clipPath && !background.image && SDK_VERSION >= 21 && (SDK_VERSION >= 33 || background.hasUniformBorderRadius());
12851285
if (onlyColor || canUseOutlineProvider) {
1286-
const backgroundColor = background.color?.android;
12871286
if (isBorderDrawable) {
12881287
// We need to duplicate the drawable or we lose the "default" cached drawable
12891288
backgroundDrawable = nativeView._cachedDrawable != null ? AndroidHelper.getCopyOrDrawable(nativeView._cachedDrawable, nativeView.getResources()) : null;
12901289
nativeView.setBackground(backgroundDrawable);
12911290
}
1292-
if (backgroundColor) {
1291+
// do not test the native color for truthiness: `transparent` is 0
1292+
if (background.color) {
1293+
const backgroundColor = background.color.android;
12931294
if (this.needsNativeDrawableFill && backgroundDrawable) {
12941295
backgroundDrawable.mutate();
12951296

@@ -1298,6 +1299,19 @@ export class View extends ViewCommon {
12981299
} else {
12991300
nativeView.setBackgroundColor(backgroundColor);
13001301
}
1302+
} else if (!isBorderDrawable && background.clearFlags & BackgroundClearFlags.CLEAR_BACKGROUND_COLOR) {
1303+
// background color was removed: restore the default background
1304+
if (this.needsNativeDrawableFill && backgroundDrawable) {
1305+
backgroundDrawable.mutate();
1306+
1307+
AndroidHelper.clearDrawableColor(backgroundDrawable);
1308+
backgroundDrawable.invalidateSelf();
1309+
} else {
1310+
const defaultDrawable = nativeView._cachedDrawable ?? null;
1311+
if (backgroundDrawable !== defaultDrawable) {
1312+
nativeView.setBackground(defaultDrawable);
1313+
}
1314+
}
13011315
}
13021316
// borderDrawable is slow
13031317
// let s use outline provider when we can

0 commit comments

Comments
 (0)