stackPanel.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { Container } from "./container";
  2. import { Measure } from "../measure";
  3. import { Control } from "./control";
  4. /**
  5. * Class used to create a 2D stack panel container
  6. */
  7. export class StackPanel extends Container {
  8. private _isVertical = true;
  9. private _manualWidth = false;
  10. private _manualHeight = false;
  11. private _doNotTrackManualChanges = false;
  12. private _tempMeasureStore = Measure.Empty();
  13. /** Gets or sets a boolean indicating if the stack panel is vertical or horizontal*/
  14. public get isVertical(): boolean {
  15. return this._isVertical;
  16. }
  17. public set isVertical(value: boolean) {
  18. if (this._isVertical === value) {
  19. return;
  20. }
  21. this._isVertical = value;
  22. this._markAsDirty();
  23. }
  24. /**
  25. * Gets or sets panel width.
  26. * This value should not be set when in horizontal mode as it will be computed automatically
  27. */
  28. public set width(value: string | number) {
  29. if (!this._doNotTrackManualChanges) {
  30. this._manualWidth = true;
  31. }
  32. if (this._width.toString(this._host) === value) {
  33. return;
  34. }
  35. if (this._width.fromString(value)) {
  36. this._markAsDirty();
  37. }
  38. }
  39. public get width(): string | number {
  40. return this._width.toString(this._host);
  41. }
  42. /**
  43. * Gets or sets panel height.
  44. * This value should not be set when in vertical mode as it will be computed automatically
  45. */
  46. public set height(value: string | number) {
  47. if (!this._doNotTrackManualChanges) {
  48. this._manualHeight = true;
  49. }
  50. if (this._height.toString(this._host) === value) {
  51. return;
  52. }
  53. if (this._height.fromString(value)) {
  54. this._markAsDirty();
  55. }
  56. }
  57. public get height(): string | number {
  58. return this._height.toString(this._host);
  59. }
  60. /**
  61. * Creates a new StackPanel
  62. * @param name defines control name
  63. */
  64. constructor(public name?: string) {
  65. super(name);
  66. }
  67. protected _getTypeName(): string {
  68. return "StackPanel";
  69. }
  70. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  71. var stackWidth = 0;
  72. var stackHeight = 0;
  73. for (var child of this._children) {
  74. this._tempMeasureStore.copyFrom(child._currentMeasure);
  75. child._currentMeasure.copyFrom(parentMeasure);
  76. child._measure();
  77. if (this._isVertical) {
  78. child.top = stackHeight + "px";
  79. if (!child._top.ignoreAdaptiveScaling) {
  80. child._markAsDirty();
  81. }
  82. child._top.ignoreAdaptiveScaling = true;
  83. stackHeight += child._currentMeasure.height;
  84. if (child._currentMeasure.width > stackWidth) {
  85. stackWidth = child._currentMeasure.width;
  86. }
  87. child.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  88. } else {
  89. child.left = stackWidth + "px";
  90. if (!child._left.ignoreAdaptiveScaling) {
  91. child._markAsDirty();
  92. }
  93. child._left.ignoreAdaptiveScaling = true;
  94. stackWidth += child._currentMeasure.width;
  95. if (child._currentMeasure.height > stackHeight) {
  96. stackHeight = child._currentMeasure.height;
  97. }
  98. child.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  99. }
  100. child._currentMeasure.copyFrom(this._tempMeasureStore);
  101. }
  102. this._doNotTrackManualChanges = true;
  103. // Let stack panel width and height default to stackHeight and stackWidth if dimensions are not specified.
  104. // User can now define their own height and width for stack panel.
  105. let panelWidthChanged = false;
  106. let panelHeightChanged = false;
  107. let previousHeight = this.height;
  108. let previousWidth = this.width;
  109. if (!this._manualHeight) {
  110. // do not specify height if strictly defined by user
  111. this.height = stackHeight + "px";
  112. }
  113. if (!this._manualWidth) {
  114. // do not specify width if strictly defined by user
  115. this.width = stackWidth + "px";
  116. }
  117. panelWidthChanged = previousWidth !== this.width || !this._width.ignoreAdaptiveScaling;
  118. panelHeightChanged = previousHeight !== this.height || !this._height.ignoreAdaptiveScaling;
  119. if (panelHeightChanged) {
  120. this._height.ignoreAdaptiveScaling = true;
  121. }
  122. if (panelWidthChanged) {
  123. this._width.ignoreAdaptiveScaling = true;
  124. }
  125. this._doNotTrackManualChanges = false;
  126. if (panelWidthChanged || panelHeightChanged) {
  127. this._markAllAsDirty();
  128. }
  129. super._preMeasure(parentMeasure, context);
  130. }
  131. }