|
1 | 1 | import { View } from '../core/view'; |
2 | 2 | import { LinearGradient } from './linear-gradient'; |
3 | | -import { CoreTypes } from '../../core-types'; |
4 | | -import { isDataURI, isFileOrResourcePath, layout, RESOURCE_PREFIX, FILE_PREFIX } from '../../utils'; |
| 3 | +import { isDataURI, isFileOrResourcePath, RESOURCE_PREFIX, FILE_PREFIX } from '../../utils'; |
5 | 4 | import { parse } from '../../css-value'; |
6 | 5 | import { path, knownFolders } from '../../file-system'; |
7 | 6 | import * as application from '../../application'; |
8 | | -import { profile } from '../../profiling'; |
9 | | -import { CSSShadow } from './css-shadow'; |
10 | | -import { Length } from './style-properties'; |
11 | | -import { BackgroundClearFlags } from './background-common'; |
12 | 7 | export * from './background-common'; |
13 | 8 |
|
14 | | -interface AndroidView { |
15 | | - _cachedDrawable: android.graphics.drawable.Drawable.ConstantState | android.graphics.drawable.Drawable; |
16 | | -} |
17 | | - |
18 | | -// TODO: Change this implementation to use |
19 | | -// We are using "ad" here to avoid namespace collision with the global android object |
20 | | -export namespace ad { |
21 | | - let SDK: number; |
22 | | - function getSDK() { |
23 | | - if (!SDK) { |
24 | | - SDK = android.os.Build.VERSION.SDK_INT; |
25 | | - } |
26 | | - |
27 | | - return SDK; |
28 | | - } |
29 | | - |
30 | | - function isSetColorFilterOnlyWidget(nativeView: android.view.View): boolean { |
31 | | - // prettier-ignore |
32 | | - return ( |
33 | | - nativeView instanceof android.widget.Button |
34 | | - || (nativeView instanceof androidx.appcompat.widget.Toolbar && getSDK() >= 21) |
35 | | - // There is an issue with the DrawableContainer which was fixed |
36 | | - // for API version 21 and above: https://code.google.com/p/android/issues/detail?id=60183 |
37 | | - ); |
38 | | - } |
39 | | - |
40 | | - export function onBackgroundOrBorderPropertyChanged(view: View) { |
41 | | - const nativeView = <android.view.View>view.nativeViewProtected; |
42 | | - if (!nativeView) { |
43 | | - return; |
44 | | - } |
45 | | - |
46 | | - const background = view.style.backgroundInternal; |
47 | | - |
48 | | - if (background.clearFlags & BackgroundClearFlags.CLEAR_BOX_SHADOW || background.clearFlags & BackgroundClearFlags.CLEAR_BACKGROUND_COLOR) { |
49 | | - // clear background if we're clearing the box shadow |
50 | | - // or the background has been removed |
51 | | - nativeView.setBackground(null); |
52 | | - } |
53 | | - |
54 | | - let drawable = nativeView.getBackground(); |
55 | | - const androidView = (<any>view) as AndroidView; |
56 | | - // use undefined as not set. getBackground will never return undefined only Drawable or null; |
57 | | - if (androidView._cachedDrawable === undefined && drawable) { |
58 | | - const constantState = drawable.getConstantState(); |
59 | | - androidView._cachedDrawable = constantState || drawable; |
60 | | - } |
61 | | - const isBorderDrawable = drawable instanceof org.nativescript.widgets.BorderDrawable; |
62 | | - |
63 | | - // prettier-ignore |
64 | | - const onlyColor = !background.hasBorderWidth() |
65 | | - && !background.hasBorderRadius() |
66 | | - && !background.hasBoxShadow() |
67 | | - && !background.clipPath |
68 | | - && !background.image |
69 | | - && !!background.color; |
70 | | - |
71 | | - if (!isBorderDrawable && drawable instanceof android.graphics.drawable.ColorDrawable && onlyColor) { |
72 | | - drawable.setColor(background.color.android); |
73 | | - drawable.invalidateSelf(); |
74 | | - } else if (isSetColorFilterOnlyWidget(nativeView) && drawable && onlyColor) { |
75 | | - if (isBorderDrawable && androidView._cachedDrawable) { |
76 | | - if (!(androidView._cachedDrawable instanceof android.graphics.drawable.Drawable.ConstantState)) { |
77 | | - return; |
78 | | - } |
79 | | - |
80 | | - drawable = androidView._cachedDrawable.newDrawable(nativeView.getResources()); |
81 | | - nativeView.setBackground(drawable); |
82 | | - } |
83 | | - |
84 | | - const backgroundColor = ((<any>drawable).backgroundColor = background.color.android); |
85 | | - drawable.mutate(); |
86 | | - drawable.setColorFilter(backgroundColor, android.graphics.PorterDuff.Mode.SRC_IN); |
87 | | - drawable.invalidateSelf(); // Make sure the drawable is invalidated. Android forgets to invalidate it in some cases: toolbar |
88 | | - (<any>drawable).backgroundColor = backgroundColor; |
89 | | - } else if (!isBorderDrawable && onlyColor) { |
90 | | - // this is the fastest way to change only background color |
91 | | - nativeView.setBackgroundColor(background.color.android); |
92 | | - } else if (!background.isEmpty()) { |
93 | | - let backgroundDrawable = drawable; |
94 | | - |
95 | | - if (drawable instanceof org.nativescript.widgets.BoxShadowDrawable) { |
96 | | - // if we have BoxShadow's we have to get the underlying drawable |
97 | | - backgroundDrawable = drawable.getWrappedDrawable(); |
98 | | - } |
99 | | - |
100 | | - if (backgroundDrawable instanceof org.nativescript.widgets.BorderDrawable) { |
101 | | - refreshBorderDrawable(view, backgroundDrawable); |
102 | | - } else { |
103 | | - backgroundDrawable = new org.nativescript.widgets.BorderDrawable(layout.getDisplayDensity(), view.toString()); |
104 | | - refreshBorderDrawable(view, <org.nativescript.widgets.BorderDrawable>backgroundDrawable); |
105 | | - nativeView.setBackground(backgroundDrawable); |
106 | | - } |
107 | | - } else { |
108 | | - const cachedDrawable = androidView._cachedDrawable; |
109 | | - let defaultDrawable: android.graphics.drawable.Drawable = null; |
110 | | - if (cachedDrawable) { |
111 | | - if (cachedDrawable instanceof android.graphics.drawable.Drawable.ConstantState) { |
112 | | - defaultDrawable = cachedDrawable.newDrawable(nativeView.getResources()); |
113 | | - } else if (cachedDrawable instanceof android.graphics.drawable.Drawable) { |
114 | | - defaultDrawable = cachedDrawable; |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - nativeView.setBackground(defaultDrawable); |
119 | | - } |
120 | | - |
121 | | - if (background.hasBoxShadow()) { |
122 | | - drawBoxShadow(nativeView, view, background.getBoxShadow()); |
123 | | - } |
124 | | - |
125 | | - // TODO: Can we move BorderWidths as separate native setter? |
126 | | - // This way we could skip setPadding if borderWidth is not changed. |
127 | | - const leftPadding = Math.ceil(view.effectiveBorderLeftWidth + view.effectivePaddingLeft); |
128 | | - const topPadding = Math.ceil(view.effectiveBorderTopWidth + view.effectivePaddingTop); |
129 | | - const rightPadding = Math.ceil(view.effectiveBorderRightWidth + view.effectivePaddingRight); |
130 | | - const bottomPadding = Math.ceil(view.effectiveBorderBottomWidth + view.effectivePaddingBottom); |
131 | | - |
132 | | - nativeView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding); |
133 | | - |
134 | | - // reset clear flags |
135 | | - background.clearFlags = BackgroundClearFlags.NONE; |
136 | | - } |
137 | | -} |
138 | | - |
139 | 9 | function fromBase64(source: string): android.graphics.Bitmap { |
140 | 10 | const bytes = android.util.Base64.decode(source, android.util.Base64.DEFAULT); |
141 | 11 |
|
@@ -253,18 +123,6 @@ function createNativeCSSValueArray(css: string): androidNative.Array<org.natives |
253 | 123 | return nativeArray; |
254 | 124 | } |
255 | 125 |
|
256 | | -function drawBoxShadow(nativeView: android.view.View, view: View, boxShadow: CSSShadow) { |
257 | | - const config = { |
258 | | - shadowColor: boxShadow.color.android, |
259 | | - cornerRadius: Length.toDevicePixels(view.borderRadius as CoreTypes.LengthType, 0.0), |
260 | | - spreadRadius: Length.toDevicePixels(boxShadow.spreadRadius, 0.0), |
261 | | - blurRadius: Length.toDevicePixels(boxShadow.blurRadius, 0.0), |
262 | | - offsetX: Length.toDevicePixels(boxShadow.offsetX, 0.0), |
263 | | - offsetY: Length.toDevicePixels(boxShadow.offsetY, 0.0), |
264 | | - }; |
265 | | - org.nativescript.widgets.Utils.drawBoxShadow(nativeView, JSON.stringify(config)); |
266 | | -} |
267 | | - |
268 | 126 | export enum CacheMode { |
269 | 127 | none, |
270 | 128 | memory, |
|
0 commit comments