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.

LayoutManagerIE8.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import com.google.gwt.dom.client.Document;
  21. import com.google.gwt.dom.client.Element;
  22. import com.google.gwt.dom.client.Node;
  23. import com.google.gwt.user.client.ui.RootPanel;
  24. /**
  25. * Alternative MeasuredSize storage for IE8. Storing any information in a DOM
  26. * element in IE8 seems to make the browser think the element has changed in a
  27. * way that requires a reflow. To work around that, the MeasureData is instead
  28. * stored in Map for IE8.
  29. *
  30. * This implementation is injected for IE8 by a replace-with definition in the
  31. * GWT module.
  32. *
  33. * @author Vaadin Ltd
  34. * @since 7.0.0
  35. */
  36. public class LayoutManagerIE8 extends LayoutManager {
  37. private Map<Element, MeasuredSize> measuredSizes = new HashMap<Element, MeasuredSize>();
  38. // this method is needed to test for memory leaks (see
  39. // LayoutMemoryUsageIE8ExtensionConnector) but can be private
  40. private int getMeasuredSizesMapSize() {
  41. return measuredSizes.size();
  42. }
  43. @Override
  44. protected void setMeasuredSize(Element element, MeasuredSize measuredSize) {
  45. if (measuredSize != null) {
  46. measuredSizes.put(element, measuredSize);
  47. } else {
  48. measuredSizes.remove(element);
  49. }
  50. // clear any values that are saved to the element
  51. if (super.getMeasuredSize(element, null) != null) {
  52. super.setMeasuredSize(element, null);
  53. }
  54. }
  55. @Override
  56. protected MeasuredSize getMeasuredSize(Element element,
  57. MeasuredSize defaultSize) {
  58. MeasuredSize measured = measuredSizes.get(element);
  59. if (measured != null) {
  60. return measured;
  61. } else {
  62. // check if saved to the element instead
  63. MeasuredSize measuredSize = super.getMeasuredSize(element, null);
  64. if (measuredSize != null) {
  65. // move the value back to the map
  66. setMeasuredSize(element, measuredSize);
  67. return measuredSize;
  68. }
  69. return defaultSize;
  70. }
  71. }
  72. @Override
  73. public void cleanMeasuredSizes() {
  74. Profiler.enter("LayoutManager.cleanMeasuredSizes");
  75. // #12688: IE8 was leaking memory when adding&removing components.
  76. // Uses IE specific logic to figure if an element has been removed from
  77. // DOM or not. For removed elements the measured size is stored within
  78. // the element in case the element gets re-attached.
  79. Node rootNode = Document.get().getBody();
  80. Iterator<Element> i = measuredSizes.keySet().iterator();
  81. while (i.hasNext()) {
  82. Element e = i.next();
  83. if (!rootNode.isOrHasChild(e)) {
  84. // Store in element in case is still needed.
  85. // Not attached, so reflow isn't a problem.
  86. super.setMeasuredSize(e, measuredSizes.get(e));
  87. i.remove();
  88. }
  89. }
  90. Profiler.leave("LayoutManager.cleanMeasuredSizes");
  91. }
  92. @Override
  93. protected void performBrowserLayoutHacks() {
  94. Profiler.enter("LayoutManagerIE8.performBrowserLayoutHacks");
  95. /*
  96. * Fixes IE8 issues where IE8 sometimes forgets to update the size of
  97. * the containing element. To force a reflow by modifying the magical
  98. * zoom property.
  99. */
  100. WidgetUtil.forceIE8Redraw(RootPanel.get().getElement());
  101. Profiler.leave("LayoutManagerIE8.performBrowserLayoutHacks");
  102. }
  103. }