8000 ref: Added function for retrieving drawable background color · NativeScript/NativeScript@a8a9e6c · GitHub
[go: up one dir, main page]

Skip to content

Commit a8a9e6c

Browse files
committed
ref: Added function for retrieving drawable background color
1 parent 9524783 commit a8a9e6c

File tree

8 files changed

+42
-40
lines changed

8 files changed

+42
-40
lines changed

apps/automated/src/ui/button/button-tests-native.android.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Color, Button, Utils, CoreTypes } from '@nativescript/core';
2+
import { AndroidHelper } from '@nativescript/core/ui/core/view';
23

34
export function getNativeText(button: Button): string {
45
return button.android.getText();
@@ -19,15 +20,7 @@ export function getNativeColor(button: Button): Color {
1920
}
2021

2122
export function getNativeBackgroundColor(button: Button): Color {
22-
let bg = <any>button.android.getBackground();
23-
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
24-
return new Color(bg.getBackgroundColor());
25-
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
26-
console.log(bg);
27-
return new Color(bg.getColor());
28-
} else {
29-
return new Color(bg.backgroundColor);
30-
}
23+
return AndroidHelper.getDrawableColor(button.android.getBackground());
3124
}
3225

3326
export function getNativeTextAlignment(button: Button): string {

apps/automated/src/ui/label/label-tests-native.android.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as labelModule from '@nativescript/core/ui/label';
2-
import { CoreTypes } from '@nativescript/core';
3-
import * as colorModule from '@nativescript/core/color';
2+
import { Color, CoreTypes } from '@nativescript/core';
3+
import { AndroidHelper } from '@nativescript/core/ui/core/view';
44

55
export function getNativeTextAlignment(label: labelModule.Label): string {
66
let gravity = label.android.getGravity();
@@ -20,13 +20,6 @@ export function getNativeTextAlignment(label: labelModule.Label): string {
2020
return 'unexpected value';
2121
}
2222

23-
export function getNativeBackgroundColor(label: labelModule.Label): colorModule.Color {
24-
let bg = <any>label.android.getBackground();
25-
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
26-
return new colorModule.Color(bg.getBackgroundColor());
27-
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
28-
return new colorModule.Color(bg.getColor());
29-
} else {
30-
return new colorModule.Color(bg.backgroundColor);
31-
}
23+
export function getNativeBackgroundColor(label: labelModule.Label): Color {
24+
return AndroidHelper.getDrawableColor(label.android.getBackground());
3225
}

apps/automated/src/ui/text-field/text-field-tests-native.android.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TextField, Color, Utils, CoreTypes } from '@nativescript/core';
2+
import { AndroidHelper } from '@nativescript/core/ui/core/view';
23

34
export function getNativeText(textField: TextField): string {
45
return textField.android.getText().toString();
@@ -29,15 +30,7 @@ export function getNativePlaceholderColor(textField: TextField): Color {
2930
}
3031

3132
export function getNativeBackgroundColor(textField: TextField): Color {
32-
let bg = <any>textField.android.getBackground();
33-
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
34-
return new Color(bg.getBackgroundColor());
35-
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
36-
console.log(bg);
37-
return new Color(bg.getColor());
38-
} else {
39-
return new Color(bg.backgroundColor);
40-
}
33+
return AndroidHelper.getDrawableColor(textField.android.getBackground());
4134
}
4235

4336
export function getNativeTextAlignment(textField: TextField): string {

apps/automated/src/ui/text-view/text-view-tests-native.android.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TextView, Color, Utils, CoreTypes } from '@nativescript/core';
2+
import { AndroidHelper } from '@nativescript/core/ui/core/view';
23

34
export function getNativeText(textView: TextView): string {
45
return textView.android.getText().toString();
@@ -27,14 +28,7 @@ export function getNativeColor(textView: TextView): Color {
2728
}
2829

2930
export function getNativeBackgroundColor(textView: TextView): Color {
30-
let bg = <any>textView.android.getBackground();
31-
if (bg instanceof org.nativescript.widgets.BorderDrawable) {
32-
return new Color(bg.getBackgroundColor());
33-
} else if (bg instanceof android.graphics.drawable.ColorDrawable) {
34-
return new Color(bg.getColor());
35-
} else {
36-
return new Color(bg.backgroundColor);
37-
}
31+
return AndroidHelper.getDrawableColor(textView.android.getBackground());
3832
}
3933

4034
export function getNativeTextAlignment(textView: TextView): string {

packages/core/ui/core/view/view-helper/index.android.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
1+
import { Color } from '../../../../color';
12
import { Trace } from '../../../../trace';
23
import { SDK_VERSION } from '../../../../utils/constants';
34

45
export * from './view-helper-common';
6+
export const IOSHelper = 0;
57

68
const androidxGraphics = androidx.core.graphics;
79

810
export class AndroidHelper {
11+
static getDrawableColor(drawable: android.graphics.drawable.Drawable): Color {
12+
if (!drawable) {
13+
return null;
14+
}
15+
16+
let color: number;
17+
18+
if (drawable instanceof org.nativescript.widgets.BorderDrawable) {
19+
color = drawable.getBackgroundColor();
20+
} else if (drawable instanceof android.graphics.drawable.ColorDrawable) {
21+
color = drawable.getColor();
22+
} else {
23+
// This is a way to retrieve drawable color when set using color filter
24+
color = (drawable as any)._backgroundColor;
25+
}
26+
27+
return new Color(color);
28+
}
29+
930
static setDrawableColor(color: number, drawable: android.graphics.drawable.Drawable, blendMode?: androidx.core.graphics.BlendModeCompat): void {
10-
// ColorDrawable is an old layer that had support for setColorFilter on API 21
31+
// ColorDrawable is an older class that had support for setColorFilter on API 21
1132
if (SDK_VERSION < 21 && drawable instanceof android.graphics.drawable.ColorDrawable) {
1233
drawable.setColor(color);
1334
} else {
1435
drawable.setColorFilter(androidxGraphics.BlendModeColorFilterCompat.createBlendModeColorFilterCompat(color, blendMode ?? androidxGraphics.BlendModeCompat.SRC_IN));
36+
37+
// This is a way to retrieve drawable color when set using color filter
38+
(drawable as any)._backgroundColor = color;
1539
}
1640
}
1741

1842
static clearDrawableColor(drawable: android.graphics.drawable.Drawable): void {
19-
// ColorDrawable is an old layer that had support for setColorFilter on API 21
43+
// ColorDrawable is an older class that had support for setColorFilter on API 21
2044
if (SDK_VERSION < 21 && drawable instanceof android.graphics.drawable.ColorDrawable) {
2145
drawable.setColor(-1);
2246
} else {
2347
drawable.clearColorFilter();
48+
49+
// This is a way to retrieve drawable color when set using color filter
50+
delete (drawable as any)._backgroundColor;
2451
}
2552
}
2653

packages/core/ui/core/view/view-helper/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class ViewHelper {
3737
* Various Android view helper methods
3838
*/
3939
export namespace AndroidHelper {
40+
export function getDrawableColor(drawable: any /* android.graphics.drawable.Drawable */): Color;
4041
export function setDrawableColor(color: number, drawable: any /* android.graphics.drawable.Drawable */, blendMode?: any /* androidx.core.graphics.BlendModeCompat */): void;
4142
export function clearDrawableColor(drawable: any /* android.graphics.drawable.Drawable */): void;
4243
export function getCopyOrDrawable(drawable: any /* android.graphics.drawable.Drawable */, resources?: any /* android.content.res.Resources */): any; /* android.graphics.drawable.Drawable */

packages/core/ui/core/view/view-helper/index.ios.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ios as iOSUtils, layout } from '../../../../utils';
77
import { Trace } from '../../../../trace';
88

99
export * from './view-helper-common';
10+
export const AndroidHelper = 0;
1011

1112
@NativeClass
1213
class UILayoutViewController extends UIViewController {

packages/core/ui/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type { BindingOptions } from './core/bindable';
1313
export { ControlStateChangeListener } from './core/control-state-change';
1414
export { ViewBase, eachDescendant, getAncestor, getViewById, booleanConverter, querySelectorAll } from './core/view-base';
1515
export type { ShowModalOptions } from './core/view-base';
16-
export { View, CSSType, ContainerView, ViewHelper, IOSHelper, isUserInteractionEnabledProperty, PseudoClassHandler, CustomLayoutView } from './core/view';
16+
export { View, CSSType, ContainerView, ViewHelper, AndroidHelper, IOSHelper, isUserInteractionEnabledProperty, PseudoClassHandler, CustomLayoutView } from './core/view';
1717
export type { Template, KeyedTemplate, ShownModallyData, AddArrayFromBuilder, AddChildFromBuilder, Size } from './core/view';
1818
export { Property, CoercibleProperty, InheritedProperty, CssProperty, InheritedCssProperty, ShorthandProperty, CssAnimationProperty, unsetValue, makeParser, makeValidator } from './core/properties';
1919
export { addWeakEventListener, removeWeakEventListener } from './core/weak-event-listener';

0 commit comments

Comments
 (0)
0