mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
bb435a2b11
Summary: In order to support AnimatedColor.setValue for platform colors, we need to pass the platform color object to the native animated node which will then resolve and apply the color. Thus, the approach is: - Add a new API updateAnimatedNodeConfig to NativeAnimatedModule - [JS] On AnimatedColor.setValue, if the value is a platform color, then we call updateAnimatedNodeConfig - [Android] We introduce AnimatedNodeWithUpdateableConfig interface with a method updateConfig. On ColorAnimatedNode.java, we use updateConfig to resolve and apply the color Changelog: [Internal][Fixed] - Use context from view when resolving platform color Reviewed By: javache, mdvacca Differential Revision: D34025193 fbshipit-source-id: 8b368f6b7cb2cf7cebe8b66461cd4185cbadd44c
328 lines
8.9 KiB
JavaScript
328 lines
8.9 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import AnimatedValue from './AnimatedValue';
|
|
import AnimatedWithChildren from './AnimatedWithChildren';
|
|
import normalizeColor from '../../StyleSheet/normalizeColor';
|
|
import {processColorObject} from '../../StyleSheet/PlatformColorValueTypes';
|
|
import NativeAnimatedHelper from '../NativeAnimatedHelper';
|
|
|
|
import type {PlatformConfig} from '../AnimatedPlatformConfig';
|
|
import type {ColorValue} from '../../StyleSheet/StyleSheet';
|
|
import type {NativeColorValue} from '../../StyleSheet/PlatformColorValueTypes';
|
|
|
|
type ColorListenerCallback = (value: string) => mixed;
|
|
export type RgbaValue = {
|
|
+r: number,
|
|
+g: number,
|
|
+b: number,
|
|
+a: number,
|
|
...
|
|
};
|
|
type RgbaAnimatedValue = {
|
|
+r: AnimatedValue,
|
|
+g: AnimatedValue,
|
|
+b: AnimatedValue,
|
|
+a: AnimatedValue,
|
|
...
|
|
};
|
|
|
|
const defaultColor: RgbaValue = {r: 0, g: 0, b: 0, a: 1.0};
|
|
let _uniqueId = 1;
|
|
|
|
/* eslint no-bitwise: 0 */
|
|
function processColor(color?: ?ColorValue): ?(RgbaValue | NativeColorValue) {
|
|
if (color === undefined || color === null) {
|
|
return null;
|
|
}
|
|
|
|
let normalizedColor = normalizeColor(color);
|
|
if (normalizedColor === undefined || normalizedColor === null) {
|
|
return null;
|
|
}
|
|
|
|
if (typeof normalizedColor === 'object') {
|
|
const processedColorObj = processColorObject(normalizedColor);
|
|
if (processedColorObj != null) {
|
|
return processedColorObj;
|
|
}
|
|
} else if (typeof normalizedColor === 'number') {
|
|
const r = (normalizedColor & 0xff000000) >>> 24;
|
|
const g = (normalizedColor & 0x00ff0000) >>> 16;
|
|
const b = (normalizedColor & 0x0000ff00) >>> 8;
|
|
const a = (normalizedColor & 0x000000ff) / 255;
|
|
|
|
return {r, g, b, a};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function isRgbaValue(value: any): boolean {
|
|
return (
|
|
value &&
|
|
typeof value.r === 'number' &&
|
|
typeof value.g === 'number' &&
|
|
typeof value.b === 'number' &&
|
|
typeof value.a === 'number'
|
|
);
|
|
}
|
|
|
|
function isRgbaAnimatedValue(value: any): boolean {
|
|
return (
|
|
value &&
|
|
value.r instanceof AnimatedValue &&
|
|
value.g instanceof AnimatedValue &&
|
|
value.b instanceof AnimatedValue &&
|
|
value.a instanceof AnimatedValue
|
|
);
|
|
}
|
|
|
|
export default class AnimatedColor extends AnimatedWithChildren {
|
|
r: AnimatedValue;
|
|
g: AnimatedValue;
|
|
b: AnimatedValue;
|
|
a: AnimatedValue;
|
|
nativeColor: Object;
|
|
_listeners: {
|
|
[key: string]: {
|
|
r: string,
|
|
g: string,
|
|
b: string,
|
|
a: string,
|
|
...
|
|
},
|
|
...
|
|
};
|
|
|
|
constructor(valueIn?: ?(RgbaValue | RgbaAnimatedValue | ColorValue)) {
|
|
super();
|
|
let value: RgbaValue | RgbaAnimatedValue | ColorValue =
|
|
valueIn ?? defaultColor;
|
|
this.setValue(value);
|
|
this._listeners = {};
|
|
}
|
|
|
|
/**
|
|
* Directly set the value. This will stop any animations running on the value
|
|
* and update all the bound properties.
|
|
*/
|
|
setValue(value: RgbaValue | RgbaAnimatedValue | ColorValue): void {
|
|
if (isRgbaAnimatedValue(value)) {
|
|
// $FlowIgnore[incompatible-cast] - Type is verified above
|
|
const rgbaAnimatedValue: RgbaAnimatedValue = (value: RgbaAnimatedValue);
|
|
this.r = rgbaAnimatedValue.r;
|
|
this.g = rgbaAnimatedValue.g;
|
|
this.b = rgbaAnimatedValue.b;
|
|
this.a = rgbaAnimatedValue.a;
|
|
} else {
|
|
// Handle potential parsable string color or platform color object
|
|
if (!isRgbaValue(value)) {
|
|
// $FlowIgnore[incompatible-cast] - Type is verified via conditionals
|
|
value = processColor((value: ColorValue)) ?? defaultColor;
|
|
}
|
|
|
|
if (!isRgbaValue(value)) {
|
|
// We are using a platform color
|
|
this.nativeColor = value;
|
|
value = defaultColor;
|
|
}
|
|
|
|
if (isRgbaValue(value)) {
|
|
// $FlowIgnore[incompatible-cast] - Type is verified via conditionals
|
|
const rgbaValue: RgbaValue = (value: RgbaValue);
|
|
|
|
if (this.r) {
|
|
this.r.setValue(rgbaValue.r);
|
|
} else {
|
|
this.r = new AnimatedValue(rgbaValue.r);
|
|
}
|
|
|
|
if (this.g) {
|
|
this.g.setValue(rgbaValue.g);
|
|
} else {
|
|
this.g = new AnimatedValue(rgbaValue.g);
|
|
}
|
|
|
|
if (this.b) {
|
|
this.b.setValue(rgbaValue.b);
|
|
} else {
|
|
this.b = new AnimatedValue(rgbaValue.b);
|
|
}
|
|
|
|
if (this.a) {
|
|
this.a.setValue(rgbaValue.a);
|
|
} else {
|
|
this.a = new AnimatedValue(rgbaValue.a);
|
|
}
|
|
}
|
|
|
|
if (this.nativeColor) {
|
|
if (!this.__isNative) {
|
|
this.__makeNative();
|
|
}
|
|
|
|
const nativeTag = this.__getNativeTag();
|
|
NativeAnimatedHelper.API.setWaitingForIdentifier(nativeTag.toString());
|
|
NativeAnimatedHelper.API.updateAnimatedNodeConfig(
|
|
nativeTag,
|
|
this.__getNativeConfig(),
|
|
);
|
|
NativeAnimatedHelper.API.unsetWaitingForIdentifier(
|
|
nativeTag.toString(),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets an offset that is applied on top of whatever value is set, whether
|
|
* via `setValue`, an animation, or `Animated.event`. Useful for compensating
|
|
* things like the start of a pan gesture.
|
|
*/
|
|
setOffset(offset: RgbaValue): void {
|
|
this.r.setOffset(offset.r);
|
|
this.g.setOffset(offset.g);
|
|
this.b.setOffset(offset.b);
|
|
this.a.setOffset(offset.a);
|
|
}
|
|
|
|
/**
|
|
* Merges the offset value into the base value and resets the offset to zero.
|
|
* The final output of the value is unchanged.
|
|
*/
|
|
flattenOffset(): void {
|
|
this.r.flattenOffset();
|
|
this.g.flattenOffset();
|
|
this.b.flattenOffset();
|
|
this.a.flattenOffset();
|
|
}
|
|
|
|
/**
|
|
* Sets the offset value to the base value, and resets the base value to
|
|
* zero. The final output of the value is unchanged.
|
|
*/
|
|
extractOffset(): void {
|
|
this.r.extractOffset();
|
|
this.g.extractOffset();
|
|
this.b.extractOffset();
|
|
this.a.extractOffset();
|
|
}
|
|
|
|
/**
|
|
* Adds an asynchronous listener to the value so you can observe updates from
|
|
* animations. This is useful because there is no way to synchronously read
|
|
* the value because it might be driven natively.
|
|
*
|
|
* Returns a string that serves as an identifier for the listener.
|
|
*/
|
|
addListener(callback: ColorListenerCallback): string {
|
|
const id = String(_uniqueId++);
|
|
const jointCallback = ({value: number}) => {
|
|
callback(this.__getValue());
|
|
};
|
|
this._listeners[id] = {
|
|
r: this.r.addListener(jointCallback),
|
|
g: this.g.addListener(jointCallback),
|
|
b: this.b.addListener(jointCallback),
|
|
a: this.a.addListener(jointCallback),
|
|
};
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* Unregister a listener. The `id` param shall match the identifier
|
|
* previously returned by `addListener()`.
|
|
*/
|
|
removeListener(id: string): void {
|
|
this.r.removeListener(this._listeners[id].r);
|
|
this.g.removeListener(this._listeners[id].g);
|
|
this.b.removeListener(this._listeners[id].b);
|
|
this.a.removeListener(this._listeners[id].a);
|
|
delete this._listeners[id];
|
|
}
|
|
|
|
/**
|
|
* Remove all registered listeners.
|
|
*/
|
|
removeAllListeners(): void {
|
|
this.r.removeAllListeners();
|
|
this.g.removeAllListeners();
|
|
this.b.removeAllListeners();
|
|
this.a.removeAllListeners();
|
|
this._listeners = {};
|
|
}
|
|
|
|
/**
|
|
* Stops any running animation or tracking. `callback` is invoked with the
|
|
* final value after stopping the animation, which is useful for updating
|
|
* state to match the animation position with layout.
|
|
*/
|
|
stopAnimation(callback?: (value: string) => void): void {
|
|
this.r.stopAnimation();
|
|
this.g.stopAnimation();
|
|
this.b.stopAnimation();
|
|
this.a.stopAnimation();
|
|
callback && callback(this.__getValue());
|
|
}
|
|
|
|
/**
|
|
* Stops any animation and resets the value to its original.
|
|
*/
|
|
resetAnimation(callback?: (value: string) => void): void {
|
|
this.r.resetAnimation();
|
|
this.g.resetAnimation();
|
|
this.b.resetAnimation();
|
|
this.a.resetAnimation();
|
|
callback && callback(this.__getValue());
|
|
}
|
|
|
|
__getValue(): string {
|
|
return `rgba(${this.r.__getValue()}, ${this.g.__getValue()}, ${this.b.__getValue()}, ${this.a.__getValue()})`;
|
|
}
|
|
|
|
__attach(): void {
|
|
this.r.__addChild(this);
|
|
this.g.__addChild(this);
|
|
this.b.__addChild(this);
|
|
this.a.__addChild(this);
|
|
super.__attach();
|
|
}
|
|
|
|
__detach(): void {
|
|
this.r.__removeChild(this);
|
|
this.g.__removeChild(this);
|
|
this.b.__removeChild(this);
|
|
this.a.__removeChild(this);
|
|
super.__detach();
|
|
}
|
|
|
|
__makeNative(platformConfig: ?PlatformConfig) {
|
|
this.r.__makeNative(platformConfig);
|
|
this.g.__makeNative(platformConfig);
|
|
this.b.__makeNative(platformConfig);
|
|
this.a.__makeNative(platformConfig);
|
|
super.__makeNative(platformConfig);
|
|
}
|
|
|
|
__getNativeConfig(): {...} {
|
|
return {
|
|
type: 'color',
|
|
r: this.r.__getNativeTag(),
|
|
g: this.g.__getNativeTag(),
|
|
b: this.b.__getNativeTag(),
|
|
a: this.a.__getNativeTag(),
|
|
nativeColor: this.nativeColor,
|
|
};
|
|
}
|
|
}
|