diff --git a/packages/core/ui/core/view/index.android.ts b/packages/core/ui/core/view/index.android.ts index 437307f5a2..837accf220 100644 --- a/packages/core/ui/core/view/index.android.ts +++ b/packages/core/ui/core/view/index.android.ts @@ -1319,10 +1319,37 @@ export class View extends ViewCommon { // let s use outline provider when we can if (shouldClipToOutline) { ViewHelper.setOutlineProvider(nativeView, background.borderTopLeftRadius, background.borderTopRightRadius, background.borderBottomRightRadius, background.borderBottomLeftRadius); - + } + if (nativeView._borderForeground) { + nativeView.setForeground(null); + nativeView._borderForeground = null; } } else { - ViewHelper.clearOutlineProvider(nativeView); + // the view clips its children to its rounded corners (outline). The border is then drawn by the foreground, over the children: + // the background drawable would be under them + const clipsChildren = + background.hasBorderRadius() && + !background.hasBoxShadows() && + !background.clipPath && + SDK_VERSION >= 21 && + (!background.hasBorderWidth() || SDK_VERSION >= 23) && + (SDK_VERSION >= 33 || background.hasUniformBorderRadius()); + if (clipsChildren) { + ViewHelper.setOutlineProvider(nativeView, background.borderTopLeftRadius, background.borderTopRightRadius, background.borderBottomRightRadius, background.borderBottomLeftRadius); + } else { + ViewHelper.clearOutlineProvider(nativeView); + } + const borderOnTop = clipsChildren && background.hasBorderWidth(); + if (borderOnTop) { + if (!nativeView._borderForeground) { + nativeView._borderForeground = new org.nativescript.widgets.BorderDrawable(layout.getDisplayDensity(), this.toString()); + } + refreshBorderDrawable(this, nativeView._borderForeground, 'border'); + nativeView.setForeground(nativeView._borderForeground); + } else if (nativeView._borderForeground) { + nativeView.setForeground(null); + nativeView._borderForeground = null; + } if (background.clearFlags & BackgroundClearFlags.CLEAR_BACKGROUND_COLOR) { if (backgroundDrawable) { backgroundDrawable.mutate(); @@ -1342,10 +1369,10 @@ export class View extends ViewCommon { } else { if (isBorderDrawable) { // org.nativescript.widgets.BorderDrawable - refreshBorderDrawable(this, backgroundDrawable); + refreshBorderDrawable(this, backgroundDrawable, borderOnTop ? 'background' : 'all'); } else { const borderDrawable = new org.nativescript.widgets.BorderDrawable(layout.getDisplayDensity(), this.toString()); - refreshBorderDrawable(this, borderDrawable); + refreshBorderDrawable(this, borderDrawable, borderOnTop ? 'background' : 'all'); nativeView.setBackground(borderDrawable); } } diff --git a/packages/core/ui/styling/background.android.ts b/packages/core/ui/styling/background.android.ts index 7dd8c5569d..fdf0f0da15 100644 --- a/packages/core/ui/styling/background.android.ts +++ b/packages/core/ui/styling/background.android.ts @@ -34,7 +34,10 @@ function fromGradient(gradient: LinearGradient): org.nativescript.widgets.Linear } const pattern = /url\(('|")(.*?)\1\)/; -export function refreshBorderDrawable(view: View, borderDrawable: org.nativescript.widgets.BorderDrawable) { +/** + * `mode`: `background` draws no border, `border` draws only the border (used as the foreground of a view clipping its children to its rounded corners) + */ +export function refreshBorderDrawable(view: View, borderDrawable: org.nativescript.widgets.BorderDrawable, mode: 'all' | 'background' | 'border' = 'all') { const nativeView = view.nativeViewProtected; const context = nativeView.getContext(); @@ -81,10 +84,10 @@ export function refreshBorderDrawable(view: View, borderDrawable: org.nativescri background.borderBottomColor ? background.borderBottomColor.android : blackColor, background.borderLeftColor ? background.borderLeftColor.android : blackColor, - background.borderTopWidth, - background.borderRightWidth, - background.borderBottomWidth, - background.borderLeftWidth, + mode === 'background' ? 0 : background.borderTopWidth, + mode === 'background' ? 0 : background.borderRightWidth, + mode === 'background' ? 0 : background.borderBottomWidth, + mode === 'background' ? 0 : background.borderLeftWidth, background.borderTopLeftRadius, background.borderTopRightRadius, @@ -93,10 +96,10 @@ export function refreshBorderDrawable(view: View, borderDrawable: org.nativescri background.clipPath instanceof ClipPathFunction ? background.clipPath.toString() : background.clipPath, - background.color ? background.color.android : 0, - imageUri, - bitmap, - gradient, + mode !== 'border' && background.color ? background.color.android : 0, + mode === 'border' ? null : imageUri, + mode === 'border' ? null : bitmap, + mode === 'border' ? null : gradient, context, background.repeat, background.position, diff --git a/packages/core/ui/styling/background.d.ts b/packages/core/ui/styling/background.d.ts index 077f9fe911..a03765729b 100644 --- a/packages/core/ui/styling/background.d.ts +++ b/packages/core/ui/styling/background.d.ts @@ -90,4 +90,4 @@ export namespace ad { export function onBackgroundOrBorderPropertyChanged(v: View); } -export function refreshBorderDrawable(view: View, borderDrawable: any /* org.nativescript.widgets.BorderDrawable */): void; +export function refreshBorderDrawable(view: View, borderDrawable: any /* org.nativescript.widgets.BorderDrawable */, mode?: 'all' | 'background' | 'border'): void; diff --git a/packages/core/ui/utils.d.ts b/packages/core/ui/utils.d.ts index 71007012db..82914d80bb 100644 --- a/packages/core/ui/utils.d.ts +++ b/packages/core/ui/utils.d.ts @@ -1,5 +1,6 @@ export interface NativeScriptAndroidView extends android.view.View { _cachedDrawable: android.graphics.drawable.Drawable; + _borderForeground?: org.nativescript.widgets.BorderDrawable; } export interface NativeScriptUIView extends UIView {