Skip to content

Commit

Permalink
Designer: Fix parent fill color handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bheston committed Oct 18, 2024
1 parent ebf5f06 commit f2eb50a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
11 changes: 8 additions & 3 deletions packages/adaptive-ui-designer-core/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ export abstract class PluginNode {
*/
public abstract get parent(): PluginNode | null;

/**
* Gets the effective fill color of this node, either locally applied or from an ancestor.
*/
public abstract get effectiveFillColor(): Color | null;

/**
* Gets the style properties this node supports.
*/
Expand Down Expand Up @@ -332,9 +337,9 @@ export abstract class PluginNode {
this._additionalData.set(AdditionalDataKeys.codeGenName, this.codeGenName);
}

if (!this._additionalData.has(AdditionalDataKeys.toolParentFillColor) && this.parent?.fillColor) {
// console.log("PluginNode.get_additionalData - adding:", AdditionalDataKeys.toolParentFillColor, this.debugInfo, formatHex8(this.parent?.fillColor));
this._additionalData.set(AdditionalDataKeys.toolParentFillColor, formatHex8(this.parent.fillColor));
if (!this._additionalData.has(AdditionalDataKeys.toolParentFillColor) && this.parent?.effectiveFillColor) {
// console.log("PluginNode.get_additionalData - adding:", AdditionalDataKeys.toolParentFillColor, this.debugInfo, formatHex8(this.parent?.effectiveFillColor));
this._additionalData.set(AdditionalDataKeys.toolParentFillColor, formatHex8(this.parent.effectiveFillColor));
}

return this._additionalData;
Expand Down
14 changes: 12 additions & 2 deletions packages/adaptive-ui-designer-figma-plugin/src/figma/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ export class FigmaPluginNode extends PluginNode {
return null;
}

// console.log(" get parent");
// console.log(" get parent", this.debugInfo);
return FigmaPluginNode.get(parent, false);
}

Expand All @@ -776,14 +776,24 @@ export class FigmaPluginNode extends PluginNode {
return null;
}

public get effectiveFillColor(): Color | null {
if (this.fillColor) {
return this.fillColor;
}
if (this.parent) {
return this.parent.fillColor;
}
return null;
}

private darkTarget: number = (-0.1 + Math.sqrt(0.21)) / 2;

public handleManualDarkMode(): boolean {
if (isInstanceNode(this._node)) {
if (this._node.variantProperties) {
const currentDarkMode = this._node.variantProperties["Dark mode"];
if (currentDarkMode) {
const color = this.parent?.fillColor;
const color = this.parent?.effectiveFillColor;
if (color) {
const containerIsDark = wcagLuminance(color) <= this.darkTarget;
// eslint-disable-next-line max-len
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ElementsController {
this.rootElement.removeChild(child)
);

this.resetFillColor(this.rootElement);
this.resetFillColor();

this.controller.selectedNodes.forEach(node => this.setupDesignTokenElement(this.rootElement, node));
}
Expand Down Expand Up @@ -182,13 +182,15 @@ export class ElementsController {

/**
* Resets the `fill-color` token value on the full element tree.
*
* @param element - The top element, recursive
*/
public resetFillColor(element: FASTElement) {
public resetFillColor() {
this.resetFillColorForElement(this.rootElement);
}

private resetFillColorForElement(element: FASTElement) {
this.setDesignTokenForElement(element, fillColor, null);
element.childNodes.forEach(child =>
this.resetFillColor(child as unknown as FASTElement)
this.resetFillColorForElement(child as unknown as FASTElement)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export class UIController {
*/
public refreshSelectedNodes(reason: string = "refreshSelectedNodes"): void {
// console.log(" Evaluating all design tokens for all selected nodes");
this._elements.resetFillColor();

this.evaluateEffectiveAppliedStyleValues(this._selectedNodes);

const message: PluginMessage = {
Expand Down Expand Up @@ -358,15 +360,15 @@ export class UIController {
if (valueOriginal instanceof Color) {
const swatch = valueOriginal;
value = formatHex8(swatch.color);
// valueDebug = swatch;
// valueDebug = swatch.toColorString();
} else if (typeof valueOriginal === "string") {
if (valueOriginal.startsWith("calc")) {
const ret = calc(valueOriginal as string);
// console.log(` calc ${value} returns ${ret}`);
value = ret;
}
}
// const fillColorValue = this._elements.getDesignTokenValue(node, fillColor);
// const fillColorValue = (this._elements.getDesignTokenValue(node, fillColor) as Swatch).toColorString();
// console.log(" evaluateEffectiveAppliedDesignToken", target, " : ", token.name, " -> ", value, valueDebug, `(from ${source})`, "fillColor", fillColorValue);

const applied = new AppliedStyleValue(value);
Expand Down

0 comments on commit f2eb50a

Please sign in to comment.