Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of StackPanel layout warning #14549

Merged
merged 8 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/dev/gui/src/2D/controls/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,26 @@ export class Container extends Control {
this._measureForChildren.copyFrom(this._currentMeasure);
}

protected _getAdaptDimTo(dim: "width" | "height"): boolean {
if (dim === "width") {
return this.adaptWidthToChildren;
} else {
return this.adaptHeightToChildren;
}
}

public isDimensionFullyDefined(dim: "width" | "height"): boolean {
if (this._getAdaptDimTo(dim)) {
for (const child of this.children) {
if (!child.isDimensionFullyDefined(dim)) {
return false;
}
}
return true;
}
return super.isDimensionFullyDefined(dim);
}

/**
* Serializes the current control
* @param serializationObject defined the JSON serialized object
Expand Down
23 changes: 23 additions & 0 deletions packages/dev/gui/src/2D/controls/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,29 @@ export class Control implements IAnimatable {
this.getDescendants().forEach((child) => child._markAllAsDirty());
}

/**
* A control has a dimension fully defined if that dimension doesn't depend on the parent's dimension.
* As an example, a control that has dimensions in pixels is fully defined, while in percentage is not fully defined.
* @param dim the dimension to check (width or height)
* @returns if the dimension is fully defined
*/
public isDimensionFullyDefined(dim: "width" | "height"): boolean {
return this.getDimension(dim).isPixel;
}

/**
* Gets the dimension of the control along a specified axis
* @param dim the dimension to retrieve (width or height)
* @returns the dimension value along the specified axis
*/
public getDimension(dim: "width" | "height"): ValueAndUnit {
if (dim === "width") {
return this._width;
} else {
return this._height;
}
}

/**
* Clones a control and its descendants
* @param host the texture where the control will be instantiated. Can be empty, in which case the control will be created on the same texture
Expand Down
44 changes: 26 additions & 18 deletions packages/dev/gui/src/2D/controls/stackPanel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Tools } from "core/Misc/tools";

import { Container } from "./container";
import type { Measure } from "../measure";
import { Control } from "./control";
import { RegisterClass } from "core/Misc/typeStore";
import { serialize } from "core/Misc/decorators";
import type { AdvancedDynamicTexture } from "../advancedDynamicTexture";
import type { ICanvasRenderingContext } from "core/Engines/ICanvas";
import type { TextBlock } from "./textBlock";
import { TextWrapping } from "./textBlock";
import { Logger } from "core/Misc/logger";

/**
* Class used to create a 2D stack panel container
Expand Down Expand Up @@ -165,10 +162,8 @@ export class StackPanel extends Container {
child._top.ignoreAdaptiveScaling = true;
}

if (child._height.isPercentage && !child._automaticSize && !(child as TextBlock).resizeToFit && !(child as Container).adaptHeightToChildren) {
if (!this.ignoreLayoutWarnings) {
Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using height in percentage mode inside a vertical StackPanel`);
}
if (!this.ignoreLayoutWarnings && !child.isDimensionFullyDefined("height")) {
Logger.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using height in percentage mode inside a vertical StackPanel`, 1);
} else {
stackHeight += child._currentMeasure.height + child._paddingTopInPixels + child._paddingBottomInPixels + (index < childrenCount - 1 ? this._spacing : 0);
}
Expand All @@ -179,16 +174,8 @@ export class StackPanel extends Container {
child._left.ignoreAdaptiveScaling = true;
}

if (
child._width.isPercentage &&
!child._automaticSize &&
child.getClassName() === "TextBlock" &&
(child as TextBlock).textWrapping !== TextWrapping.Clip &&
!(child as TextBlock).forceResizeWidth
) {
if (!this.ignoreLayoutWarnings) {
Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using width in percentage mode inside a horizontal StackPanel`);
}
if (!this.ignoreLayoutWarnings && !child.isDimensionFullyDefined("width")) {
Logger.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using width in percentage mode inside a horizontal StackPanel`, 1);
} else {
stackWidth += child._currentMeasure.width + child._paddingLeftInPixels + child._paddingRightInPixels + (index < childrenCount - 1 ? this._spacing : 0);
}
Expand Down Expand Up @@ -236,6 +223,27 @@ export class StackPanel extends Container {
super._postMeasure();
}

private _getManualDim(dim: "width" | "height") {
if (dim === "width") {
return this._manualWidth;
} else {
return this._manualHeight;
}
}

public isDimensionFullyDefined(dim: "width" | "height"): boolean {
if (dim === "height" ? this.isVertical : !this.isVertical && !this._getManualDim(dim)) {
for (const child of this._children) {
if (!child.isDimensionFullyDefined(dim)) {
return false;
}
}
return true;
}

return this.getDimension(dim).isPixel || this._getAdaptDimTo(dim);
}

/**
* Serializes the current control
* @param serializationObject defined the JSON serialized object
Expand Down
7 changes: 7 additions & 0 deletions packages/dev/gui/src/2D/controls/textBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,13 @@ export class TextBlock extends Control {
return newHeight;
}

public isDimensionFullyDefined(dim: "width" | "height"): boolean {
if (this.resizeToFit) {
return true;
}
return super.isDimensionFullyDefined(dim);
}

/**
* Given a width constraint applied on the text block, find the expected height
* @returns expected height
Expand Down