Visualize Your Ideas With Code
feat: simplify size access further (#66)
Jacob9091a5e05d8f
diff --git a/packages/2d/src/components/Node.ts b/packages/2d/src/components/Node.tsindex c7ad2e2c..b28a7505 100644--- a/packages/2d/src/components/Node.ts+++ b/packages/2d/src/components/Node.ts@@ -1,4 +1,10 @@-import {compound, computed, initialize, property} from '../decorators';+import {+ compound,+ computed,+ initialize,+ Property,+ property,+} from '../decorators';import {Vector2,transformPoint,@@ -19,6 +25,7 @@ import {InterpolationFunction,TimingFunction,tween,+ map,} from '@motion-canvas/core/lib/tweening';import {Layout, LayoutProps, Length, ResolvedLayoutMode} from '../layout';import {ComponentChild, ComponentChildren} from './types';@@ -51,30 +58,37 @@ export class Node<TProps extends NodeProps = NodeProps> {@property(0)public declare readonly x: Signal<number, this>;-- public saveX() {- this.x(this.computedX());- }-- @computed()- protected computedX(): number {+ protected readonly customX = createSignal(0, map, this);+ protected getX(): number {return this.computedLayout().x;}+ protected setX(value: SignalValue<number>) {+ this.customX(value);+ }@property(0)public declare readonly y: Signal<number, this>;-- public saveY() {- this.y(this.computedY());- }-- @computed()- protected computedY(): number {+ protected readonly customY = createSignal(0, map, this);+ protected getY(): number {return this.computedLayout().y;}+ protected setY(value: SignalValue<number>) {+ this.customY(value);+ }@property(null)- public declare readonly width: Signal<Length, this>;+ public declare readonly width: Property<Length, number, this>;+ protected readonly customWidth = createSignal<Length, this>(+ undefined,+ undefined,+ this,+ );+ protected getWidth(): number {+ return this.computedLayout().width;+ }+ protected setWidth(value: SignalValue<Length>) {+ this.customWidth(value);+ }@threadable()protected *tweenWidth(@@ -83,12 +97,12 @@ export class Node<TProps extends NodeProps = NodeProps> {timingFunction: TimingFunction,interpolationFunction: InterpolationFunction<Length>,): ThreadGenerator {- const width = this.width();+ const width = this.customWidth();let from: number;if (typeof width === 'number') {from = width;} else {- from = this.computedWidth();+ from = this.width();}let to: number;@@ -96,7 +110,7 @@ export class Node<TProps extends NodeProps = NodeProps> {to = value;} else {this.width(value);- to = this.computedWidth();+ to = this.width();}this.width(from);@@ -108,17 +122,20 @@ export class Node<TProps extends NodeProps = NodeProps> {this.layout.releaseSize();}- public saveWidth() {- this.width(this.computedWidth());+ @property(null)+ public declare readonly height: Property<Length, number, this>;+ protected readonly customHeight = createSignal<Length, this>(+ undefined,+ undefined,+ this,+ );+ protected getHeight(): number {+ return this.computedLayout().height;}-- protected computedWidth(): number {- return this.computedLayout().width;+ protected setHeight(value: SignalValue<Length>) {+ this.customHeight(value);}- @property(null)- public declare readonly height: Signal<Length, this>;-@threadable()protected *tweenHeight(value: SignalValue<Length>,@@ -126,12 +143,12 @@ export class Node<TProps extends NodeProps = NodeProps> {timingFunction: TimingFunction,interpolationFunction: InterpolationFunction<Length>,): ThreadGenerator {- const height = this.height();+ const height = this.customHeight();let from: number;if (typeof height === 'number') {from = height;} else {- from = this.computedHeight();+ from = this.height();}let to: number;@@ -139,7 +156,7 @@ export class Node<TProps extends NodeProps = NodeProps> {to = value;} else {this.height(value);- to = this.computedHeight();+ to = this.height();}this.height(from);@@ -151,17 +168,21 @@ export class Node<TProps extends NodeProps = NodeProps> {this.layout.releaseSize();}- public saveHeight() {- this.width(this.computedHeight());- }-- protected computedHeight(): number {- return this.computedLayout().height;- }-@compound(['width', 'height'])@property(undefined, sizeLerp)- public declare readonly size: Signal<{width: Length; height: Length}, this>;+ public declare readonly size: Property<+ {width: Length; height: Length},+ Size,+ this+ >;++ @computed()+ protected customSize(): {width: Length; height: Length} {+ return {+ width: this.customWidth(),+ height: this.customHeight(),+ };+ }@threadable()protected *tweenSize(@@ -170,10 +191,10 @@ export class Node<TProps extends NodeProps = NodeProps> {timingFunction: TimingFunction,interpolationFunction: InterpolationFunction<Size>,): ThreadGenerator {- const size = this.size();+ const size = this.customSize();let from: Size;if (typeof size.height !== 'number' || typeof size.width !== 'number') {- from = this.computedSize();+ from = this.size();} else {from = <Size>size;}@@ -187,7 +208,7 @@ export class Node<TProps extends NodeProps = NodeProps> {to = value;} else {this.size(value);- to = this.computedSize();+ to = this.size();}this.size(from);@@ -199,19 +220,6 @@ export class Node<TProps extends NodeProps = NodeProps> {this.size(value);}- public saveSize() {- this.size(this.computedSize());- }-- @computed()- protected computedSize(): Size {- const layout = this.computedLayout();- return {- height: layout.height,- width: layout.width,- };- }-@property(0)public declare readonly rotation: Signal<number, this>;@@ -345,8 +353,8 @@ export class Node<TProps extends NodeProps = NodeProps> {const mode = this.mode();if (mode !== 'enabled') {return {- x: this.x(),- y: this.y(),+ x: this.customX(),+ y: this.customY(),width: rect.width,height: rect.height,};@@ -389,7 +397,10 @@ export class Node<TProps extends NodeProps = NodeProps> {@computed()protected updateLayout() {this.applyLayoutChanges();- this.layout.setWidth(this.width()).setHeight(this.height()).apply();+ this.layout+ .setWidth(this.customWidth())+ .setHeight(this.customHeight())+ .apply();for (const child of this.children()) {child.updateLayout();}diff --git a/packages/2d/src/decorators/property.ts b/packages/2d/src/decorators/property.tsindex 9d980308..0251d57e 100644--- a/packages/2d/src/decorators/property.ts+++ b/packages/2d/src/decorators/property.ts@@ -14,30 +14,41 @@ import {SignalGetter,SignalSetter,SignalTween,+ SignalUtils,} from '@motion-canvas/core/lib/utils';export function capitalize<T extends string>(value: T): Capitalize<T> {return <Capitalize<T>>(value[0].toUpperCase() + value.slice(1));}-export type PropertyOwner<TValue> = {- [key: `get${Capitalize<string>}`]: SignalGetter<TValue> | undefined;- [key: `set${Capitalize<string>}`]: SignalSetter<TValue> | undefined;- [key: `tween${Capitalize<string>}`]: SignalTween<TValue> | undefined;+export interface Property<+ TSetterValue,+ TGetterValue extends TSetterValue,+ TOwner,+> extends SignalGetter<TGetterValue>,+ SignalSetter<TSetterValue>,+ SignalTween<TSetterValue>,+ SignalUtils<TOwner> {}++export type PropertyOwner<TGetterValue, TSetterValue> = {+ [key: `get${Capitalize<string>}`]: SignalGetter<TGetterValue> | undefined;+ [key: `set${Capitalize<string>}`]: SignalSetter<TSetterValue> | undefined;+ [key: `tween${Capitalize<string>}`]: SignalTween<TSetterValue> | undefined;};export function createProperty<- TValue,- TNode extends PropertyOwner<TValue>,+ TSetterValue,+ TGetterValue extends TSetterValue,+ TNode extends PropertyOwner<TGetterValue, TSetterValue>,TProperty extends string & keyof TNode,>(node: TNode,property: TProperty,- initial?: TValue,- defaultInterpolation: InterpolationFunction<TValue> = deepLerp,-): Signal<TValue, TNode> {- let getter: () => TValue;- let setter: (value: SignalValue<TValue>) => void;+ initial?: TSetterValue,+ defaultInterpolation: InterpolationFunction<TSetterValue> = deepLerp,+): Property<TSetterValue, TGetterValue, TNode> {+ let getter: SignalGetter<TGetterValue>;+ let setter: SignalSetter<TSetterValue>;const originalGetter = node[`get${capitalize(property)}`];const originalSetter = node[`set${capitalize(property)}`];@@ -49,9 +60,11 @@ export function createProperty<);}- let signal: Signal<TValue, TNode> | null = null;+ let signal: Property<TSetterValue, TGetterValue, TNode> | null = null;if (!originalGetter || !originalSetter) {- signal = createSignal(initial, defaultInterpolation, node);+ signal = <Property<TSetterValue, TGetterValue, TNode>>(+ (<unknown>createSignal(initial, defaultInterpolation, node))+ );if (!tweener) {return signal;}@@ -63,12 +76,12 @@ export function createProperty<setter = originalSetter.bind(node);}- const handler = <Signal<TValue, TNode>>(+ const handler = <Property<TSetterValue, TGetterValue, TNode>>(function (- newValue?: SignalValue<TValue>,+ newValue?: SignalValue<TSetterValue>,duration?: number,timingFunction: TimingFunction = easeInOutCubic,- interpolationFunction: InterpolationFunction<TValue> = defaultInterpolation,+ interpolationFunction: InterpolationFunction<TSetterValue> = defaultInterpolation,) {if (newValue === undefined) {return getter();@@ -113,7 +126,7 @@ export function createProperty<value: () => setter(getter()),});- if (initial !== undefined && originalSetter) {+ if (initial !== undefined && !signal) {setter(initial);}diff --git a/packages/core/src/utils/createSignal.ts b/packages/core/src/utils/createSignal.tsindex 1bcaf192..71980582 100644--- a/packages/core/src/utils/createSignal.ts+++ b/packages/core/src/utils/createSignal.ts@@ -29,10 +29,7 @@ export interface SignalTween<TValue> {): ThreadGenerator;}-export interface Signal<TValue, TReturn = void>- extends SignalSetter<TValue, TReturn>,- SignalGetter<TValue>,- SignalTween<TValue> {+export interface SignalUtils<TReturn> {/*** Reset the signal to its initial value (if one has been set).*@@ -64,6 +61,12 @@ export interface Signal<TValue, TReturn = void>save(): TReturn;}+export interface Signal<TValue, TReturn = void>+ extends SignalSetter<TValue, TReturn>,+ SignalGetter<TValue>,+ SignalTween<TValue>,+ SignalUtils<TReturn> {}+const collectionStack: DependencyContext[] = [];export function startCollecting(context: DependencyContext) {