You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MeasuredSize.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import com.google.gwt.core.client.JsArrayString;
  6. import com.google.gwt.dom.client.Element;
  7. public class MeasuredSize {
  8. private int width = -1;
  9. private int height = -1;
  10. private int[] paddings = new int[4];
  11. private int[] borders = new int[4];
  12. private int[] margins = new int[4];
  13. private boolean heightChanged = true;
  14. private boolean widthChanged = true;
  15. private FastStringSet dependents = FastStringSet.create();
  16. public int getOuterHeight() {
  17. return height;
  18. }
  19. public int getOuterWidth() {
  20. return width;
  21. }
  22. public void addDependent(String pid) {
  23. dependents.add(pid);
  24. }
  25. public void removeDependent(String pid) {
  26. dependents.remove(pid);
  27. }
  28. public boolean hasDependents() {
  29. return !dependents.isEmpty();
  30. }
  31. public JsArrayString getDependents() {
  32. return dependents.dump();
  33. }
  34. private static int sumWidths(int[] sizes) {
  35. return sizes[1] + sizes[3];
  36. }
  37. private static int sumHeights(int[] sizes) {
  38. return sizes[0] + sizes[2];
  39. }
  40. public int getInnerHeight() {
  41. return height - sumHeights(margins) - sumHeights(borders)
  42. - sumHeights(paddings);
  43. }
  44. public int getInnerWidth() {
  45. return width - sumWidths(margins) - sumWidths(borders)
  46. - sumWidths(paddings);
  47. }
  48. public void setOuterHeight(int height) {
  49. if (this.height != height) {
  50. heightChanged = true;
  51. this.height = height;
  52. }
  53. }
  54. public void setOuterWidth(int width) {
  55. if (this.width != width) {
  56. widthChanged = true;
  57. this.width = width;
  58. }
  59. }
  60. public int getBorderHeight() {
  61. return sumHeights(borders);
  62. }
  63. public int getBorderWidth() {
  64. return sumWidths(borders);
  65. }
  66. public int getPaddingHeight() {
  67. return sumHeights(paddings);
  68. }
  69. public int getPaddingWidth() {
  70. return sumWidths(paddings);
  71. }
  72. public int getMarginHeight() {
  73. return sumHeights(margins);
  74. }
  75. public int getMarginWidth() {
  76. return sumWidths(margins);
  77. }
  78. public int getMarginTop() {
  79. return margins[0];
  80. }
  81. public int getMarginRight() {
  82. return margins[1];
  83. }
  84. public int getMarginBottom() {
  85. return margins[2];
  86. }
  87. public int getMarginLeft() {
  88. return margins[3];
  89. }
  90. public int getBorderTop() {
  91. return margins[0];
  92. }
  93. public int getBorderRight() {
  94. return margins[1];
  95. }
  96. public int getBorderBottom() {
  97. return margins[2];
  98. }
  99. public int getBorderLeft() {
  100. return margins[3];
  101. }
  102. public int getPaddingTop() {
  103. return paddings[0];
  104. }
  105. public int getPaddingRight() {
  106. return paddings[1];
  107. }
  108. public int getPaddingBottom() {
  109. return paddings[2];
  110. }
  111. public int getPaddingLeft() {
  112. return paddings[3];
  113. }
  114. boolean measure(Element element) {
  115. boolean wasHeightChanged = heightChanged;
  116. boolean wasWidthChanged = widthChanged;
  117. ComputedStyle computedStyle = new ComputedStyle(element);
  118. int[] paddings = computedStyle.getPadding();
  119. if (!heightChanged && hasHeightChanged(this.paddings, paddings)) {
  120. heightChanged = true;
  121. }
  122. if (!widthChanged && hasWidthChanged(this.paddings, paddings)) {
  123. widthChanged = true;
  124. }
  125. this.paddings = paddings;
  126. int[] margins = computedStyle.getMargin();
  127. if (!heightChanged && hasHeightChanged(this.margins, margins)) {
  128. heightChanged = true;
  129. }
  130. if (!widthChanged && hasWidthChanged(this.margins, margins)) {
  131. widthChanged = true;
  132. }
  133. this.margins = margins;
  134. int[] borders = computedStyle.getBorder();
  135. if (!heightChanged && hasHeightChanged(this.borders, borders)) {
  136. heightChanged = true;
  137. }
  138. if (!widthChanged && hasWidthChanged(this.borders, borders)) {
  139. widthChanged = true;
  140. }
  141. this.borders = borders;
  142. int requiredHeight = Util.getRequiredHeight(element);
  143. int marginHeight = sumHeights(margins);
  144. setOuterHeight(requiredHeight + marginHeight);
  145. int requiredWidth = Util.getRequiredWidth(element);
  146. int marginWidth = sumWidths(margins);
  147. setOuterWidth(requiredWidth + marginWidth);
  148. return wasHeightChanged != heightChanged
  149. || wasWidthChanged != widthChanged;
  150. }
  151. void clearDirtyState() {
  152. heightChanged = widthChanged = false;
  153. }
  154. public boolean isHeightNeedsUpdate() {
  155. return heightChanged;
  156. }
  157. public boolean isWidthNeedsUpdate() {
  158. return widthChanged;
  159. }
  160. private static boolean hasWidthChanged(int[] sizes1, int[] sizes2) {
  161. return sizes1[1] != sizes2[1] || sizes1[3] != sizes2[3];
  162. }
  163. private static boolean hasHeightChanged(int[] sizes1, int[] sizes2) {
  164. return sizes1[0] != sizes2[0] || sizes1[2] != sizes2[2];
  165. }
  166. public void setWidthNeedsUpdate() {
  167. widthChanged = true;
  168. }
  169. public void setHeightNeedsUpdate() {
  170. heightChanged = true;
  171. }
  172. }