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.

FocusableScrollPanel.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.ui;
  17. import java.util.List;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.google.gwt.dom.client.DivElement;
  20. import com.google.gwt.dom.client.Document;
  21. import com.google.gwt.dom.client.Style;
  22. import com.google.gwt.dom.client.Style.Position;
  23. import com.google.gwt.dom.client.Style.Unit;
  24. import com.google.gwt.event.dom.client.HasScrollHandlers;
  25. import com.google.gwt.event.dom.client.ScrollEvent;
  26. import com.google.gwt.event.dom.client.ScrollHandler;
  27. import com.google.gwt.event.shared.HandlerRegistration;
  28. import com.google.gwt.user.client.DOM;
  29. import com.google.gwt.user.client.Event;
  30. import com.google.gwt.user.client.ui.ScrollPanel;
  31. import com.google.gwt.user.client.ui.Widget;
  32. import com.google.gwt.user.client.ui.impl.FocusImpl;
  33. import com.vaadin.client.BrowserInfo;
  34. /**
  35. * A scrollhandlers similar to {@link ScrollPanel}.
  36. *
  37. */
  38. public class FocusableScrollPanel extends SimpleFocusablePanel
  39. implements HasScrollHandlers, ScrollHandler {
  40. public FocusableScrollPanel() {
  41. // Prevent IE standard mode bug when a AbsolutePanel is contained.
  42. TouchScrollDelegate.enableTouchScrolling(this, getElement());
  43. Style style = getElement().getStyle();
  44. style.setProperty("zoom", "1");
  45. style.setPosition(Position.RELATIVE);
  46. browserInfo = BrowserInfo.get();
  47. }
  48. private DivElement focusElement;
  49. private BrowserInfo browserInfo;
  50. public FocusableScrollPanel(boolean useFakeFocusElement) {
  51. this();
  52. if (useFakeFocusElement) {
  53. focusElement = Document.get().createDivElement();
  54. }
  55. }
  56. private boolean useFakeFocusElement() {
  57. return focusElement != null;
  58. }
  59. @Override
  60. public void setWidget(Widget w) {
  61. super.setWidget(w);
  62. if (useFakeFocusElement()) {
  63. if (focusElement.getParentElement() == null) {
  64. Style style = focusElement.getStyle();
  65. style.setPosition(Position.FIXED);
  66. style.setTop(0, Unit.PX);
  67. style.setLeft(0, Unit.PX);
  68. if (browserInfo.isIE() || browserInfo.isEdge()) {
  69. // for #15294: artificially hide little bit more the
  70. // focusElement, otherwise IE will make the window to scroll
  71. // into it when focused
  72. style.setLeft(-999, Unit.PX);
  73. }
  74. getElement().appendChild(focusElement);
  75. /* Sink from focusElemet too as focusa and blur don't bubble */
  76. DOM.sinkEvents(focusElement, Event.FOCUSEVENTS);
  77. // revert to original, not focusable
  78. getElement().setPropertyObject("tabIndex", null);
  79. } else {
  80. moveFocusElementAfterWidget();
  81. }
  82. }
  83. }
  84. /**
  85. * Helper to keep focus element always in domChild[1]. Aids testing.
  86. */
  87. private void moveFocusElementAfterWidget() {
  88. getElement().insertAfter(focusElement, getWidget().getElement());
  89. }
  90. @Override
  91. public void setFocus(boolean focus) {
  92. if (useFakeFocusElement()) {
  93. if (focus) {
  94. FocusImpl.getFocusImplForPanel().focus(focusElement);
  95. } else {
  96. FocusImpl.getFocusImplForPanel().blur(focusElement);
  97. }
  98. } else {
  99. super.setFocus(focus);
  100. }
  101. }
  102. @Override
  103. public void setTabIndex(int tabIndex) {
  104. if (useFakeFocusElement()) {
  105. getElement().setTabIndex(-1);
  106. if (focusElement != null) {
  107. focusElement.setTabIndex(tabIndex);
  108. }
  109. } else {
  110. super.setTabIndex(tabIndex);
  111. }
  112. }
  113. @Override
  114. public HandlerRegistration addScrollHandler(ScrollHandler handler) {
  115. return addDomHandler(handler, ScrollEvent.getType());
  116. }
  117. /**
  118. * Gets the horizontal scroll position.
  119. *
  120. * @return the horizontal scroll position, in pixels
  121. */
  122. public int getHorizontalScrollPosition() {
  123. return getElement().getScrollLeft();
  124. }
  125. /**
  126. * Gets the vertical scroll position.
  127. *
  128. * @return the vertical scroll position, in pixels
  129. */
  130. public int getScrollPosition() {
  131. if (getElement().getPropertyJSO("_vScrollTop") != null) {
  132. return getElement().getPropertyInt("_vScrollTop");
  133. } else {
  134. return getElement().getScrollTop();
  135. }
  136. }
  137. /**
  138. * Sets the horizontal scroll position.
  139. *
  140. * @param position
  141. * the new horizontal scroll position, in pixels
  142. */
  143. public void setHorizontalScrollPosition(int position) {
  144. getElement().setScrollLeft(position);
  145. }
  146. /**
  147. * Sets the vertical scroll position.
  148. *
  149. * @param position
  150. * the new vertical scroll position, in pixels
  151. */
  152. public void setScrollPosition(int position) {
  153. if (BrowserInfo.get().isAndroidWithBrokenScrollTop()
  154. && BrowserInfo.get().requiresTouchScrollDelegate()) {
  155. List<com.google.gwt.dom.client.Element> elements = TouchScrollDelegate
  156. .getElements(getElement());
  157. for (com.google.gwt.dom.client.Element el : elements) {
  158. final Style style = el.getStyle();
  159. style.setProperty("webkitTransform",
  160. "translate3d(0px," + -position + "px,0px)");
  161. }
  162. getElement().setPropertyInt("_vScrollTop", position);
  163. } else {
  164. getElement().setScrollTop(position);
  165. }
  166. }
  167. @Override
  168. public void onScroll(ScrollEvent event) {
  169. Scheduler.get().scheduleDeferred(() -> {
  170. focusElement.getStyle().setTop(getScrollPosition(), Unit.PX);
  171. focusElement.getStyle().setLeft(getHorizontalScrollPosition(),
  172. Unit.PX);
  173. });
  174. }
  175. public com.google.gwt.user.client.Element getFocusElement() {
  176. if (useFakeFocusElement()) {
  177. return focusElement.cast();
  178. }
  179. return getElement();
  180. }
  181. }