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.

ScrollbarBundle.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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.widget.escalator;
  17. import com.google.gwt.animation.client.AnimationScheduler;
  18. import com.google.gwt.animation.client.AnimationScheduler.AnimationSupportDetector;
  19. import com.google.gwt.core.client.Scheduler;
  20. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  21. import com.google.gwt.dom.client.Element;
  22. import com.google.gwt.dom.client.Style.Display;
  23. import com.google.gwt.dom.client.Style.Overflow;
  24. import com.google.gwt.dom.client.Style.Unit;
  25. import com.google.gwt.dom.client.Style.Visibility;
  26. import com.google.gwt.event.shared.EventHandler;
  27. import com.google.gwt.event.shared.GwtEvent;
  28. import com.google.gwt.event.shared.HandlerManager;
  29. import com.google.gwt.event.shared.HandlerRegistration;
  30. import com.google.gwt.user.client.DOM;
  31. import com.google.gwt.user.client.Event;
  32. import com.google.gwt.user.client.EventListener;
  33. import com.google.gwt.user.client.Timer;
  34. import com.vaadin.client.BrowserInfo;
  35. import com.vaadin.client.DeferredWorker;
  36. import com.vaadin.client.WidgetUtil;
  37. import com.vaadin.client.widget.grid.events.ScrollEvent;
  38. import com.vaadin.client.widget.grid.events.ScrollHandler;
  39. /**
  40. * An element-like bundle representing a configurable and visual scrollbar in
  41. * one axis.
  42. *
  43. * @since 7.4
  44. * @author Vaadin Ltd
  45. * @see VerticalScrollbarBundle
  46. * @see HorizontalScrollbarBundle
  47. */
  48. public abstract class ScrollbarBundle implements DeferredWorker {
  49. private static final boolean SUPPORTS_REQUEST_ANIMATION_FRAME = new AnimationSupportDetector()
  50. .isNativelySupported();
  51. private class ScrollEventFirer {
  52. private final ScheduledCommand fireEventCommand = () -> {
  53. /*
  54. * Some kind of native-scroll-event related asynchronous problem
  55. * occurs here (at least on desktops) where the internal bookkeeping
  56. * isn't up to date with the real scroll position. The weird thing
  57. * is, that happens only once, and if you drag scrollbar fast
  58. * enough. After it has failed once, it never fails again.
  59. *
  60. * Theory: the user drags the scrollbar, and this command is
  61. * executed before the browser has a chance to fire a scroll event
  62. * (which normally would correct this situation). This would explain
  63. * why slow scrolling doesn't trigger the problem, while fast
  64. * scrolling does.
  65. *
  66. * To make absolutely sure that we have the latest scroll position,
  67. * let's update the internal value.
  68. *
  69. * This might lead to a slight performance hit (on my computer it
  70. * was never more than 3ms on either of Chrome 38 or Firefox 31). It
  71. * also _slightly_ counteracts the purpose of the internal
  72. * bookkeeping. But since getScrollPos is called 3 times (on one
  73. * direction) per scroll loop, it's still better to have take this
  74. * small penalty than removing it altogether.
  75. */
  76. updateScrollPosFromDom();
  77. getHandlerManager().fireEvent(new ScrollEvent());
  78. isBeingFired = false;
  79. };
  80. private boolean isBeingFired;
  81. public void scheduleEvent() {
  82. if (!isBeingFired) {
  83. /*
  84. * We'll gather all the scroll events, and only fire once, once
  85. * everything has calmed down.
  86. */
  87. if (SUPPORTS_REQUEST_ANIMATION_FRAME) {
  88. // Chrome MUST use this as deferred commands will sometimes
  89. // be run with a 300+ ms delay when scrolling.
  90. AnimationScheduler.get().requestAnimationFrame(
  91. timestamp -> fireEventCommand.execute());
  92. } else {
  93. // Does not support requestAnimationFrame and the fallback
  94. // uses a delay of 16ms, we stick to the old deferred
  95. // command which uses a delay of 0ms
  96. Scheduler.get().scheduleDeferred(fireEventCommand);
  97. }
  98. isBeingFired = true;
  99. }
  100. }
  101. }
  102. /**
  103. * The orientation of the scrollbar.
  104. */
  105. public enum Direction {
  106. VERTICAL, HORIZONTAL;
  107. }
  108. private class TemporaryResizer {
  109. private static final int TEMPORARY_RESIZE_DELAY = 1000;
  110. private final Timer timer = new Timer() {
  111. @Override
  112. public void run() {
  113. internalSetScrollbarThickness(1);
  114. root.getStyle().setVisibility(Visibility.HIDDEN);
  115. }
  116. };
  117. public void show() {
  118. internalSetScrollbarThickness(OSX_INVISIBLE_SCROLLBAR_FAKE_SIZE_PX);
  119. root.getStyle().setVisibility(Visibility.VISIBLE);
  120. timer.schedule(TEMPORARY_RESIZE_DELAY);
  121. }
  122. }
  123. /**
  124. * A means to listen to when the scrollbar handle in a
  125. * {@link ScrollbarBundle} either appears or is removed.
  126. */
  127. public interface VisibilityHandler extends EventHandler {
  128. /**
  129. * This method is called whenever the scrollbar handle's visibility is
  130. * changed in a {@link ScrollbarBundle}.
  131. *
  132. * @param event
  133. * the {@link VisibilityChangeEvent}
  134. */
  135. void visibilityChanged(VisibilityChangeEvent event);
  136. }
  137. public static class VisibilityChangeEvent
  138. extends GwtEvent<VisibilityHandler> {
  139. public static final Type<VisibilityHandler> TYPE = new Type<ScrollbarBundle.VisibilityHandler>() {
  140. @Override
  141. public String toString() {
  142. return "VisibilityChangeEvent";
  143. }
  144. };
  145. private final boolean isScrollerVisible;
  146. private VisibilityChangeEvent(boolean isScrollerVisible) {
  147. this.isScrollerVisible = isScrollerVisible;
  148. }
  149. /**
  150. * Checks whether the scroll handle is currently visible or not.
  151. *
  152. * @return <code>true</code> if the scroll handle is currently visible.
  153. * <code>false</code> if not.
  154. */
  155. public boolean isScrollerVisible() {
  156. return isScrollerVisible;
  157. }
  158. @Override
  159. public Type<VisibilityHandler> getAssociatedType() {
  160. return TYPE;
  161. }
  162. @Override
  163. protected void dispatch(VisibilityHandler handler) {
  164. handler.visibilityChanged(this);
  165. }
  166. }
  167. /**
  168. * The pixel size for OSX's invisible scrollbars.
  169. * <p>
  170. * Touch devices don't show a scrollbar at all, so the scrollbar size is
  171. * irrelevant in their case. There doesn't seem to be any other popular
  172. * platforms that has scrollbars similar to OSX. Thus, this behavior is
  173. * tailored for OSX only, until additional platforms start behaving this
  174. * way.
  175. */
  176. private static final int OSX_INVISIBLE_SCROLLBAR_FAKE_SIZE_PX = 13;
  177. /**
  178. * A representation of a single vertical scrollbar.
  179. *
  180. * @see VerticalScrollbarBundle#getElement()
  181. */
  182. public static final class VerticalScrollbarBundle extends ScrollbarBundle {
  183. @Override
  184. public void setStylePrimaryName(String primaryStyleName) {
  185. super.setStylePrimaryName(primaryStyleName);
  186. root.addClassName(primaryStyleName + "-scroller-vertical");
  187. }
  188. @Override
  189. protected void internalSetScrollPos(int px) {
  190. root.setScrollTop(px);
  191. }
  192. @Override
  193. protected int internalGetScrollPos() {
  194. return root.getScrollTop();
  195. }
  196. @Override
  197. protected void internalSetScrollSize(double px) {
  198. scrollSizeElement.getStyle().setHeight(px, Unit.PX);
  199. }
  200. @Override
  201. protected String internalGetScrollSize() {
  202. return scrollSizeElement.getStyle().getHeight();
  203. }
  204. @Override
  205. protected void internalSetOffsetSize(double px) {
  206. root.getStyle().setHeight(px, Unit.PX);
  207. }
  208. @Override
  209. public String internalGetOffsetSize() {
  210. return root.getStyle().getHeight();
  211. }
  212. @Override
  213. protected void internalSetScrollbarThickness(double px) {
  214. root.getStyle().setPaddingRight(px, Unit.PX);
  215. root.getStyle().setWidth(0, Unit.PX);
  216. scrollSizeElement.getStyle().setWidth(px, Unit.PX);
  217. }
  218. @Override
  219. protected String internalGetScrollbarThickness() {
  220. return scrollSizeElement.getStyle().getWidth();
  221. }
  222. @Override
  223. protected void internalForceScrollbar(boolean enable) {
  224. if (enable) {
  225. root.getStyle().setOverflowY(Overflow.SCROLL);
  226. } else {
  227. root.getStyle().clearOverflowY();
  228. }
  229. }
  230. @Override
  231. public Direction getDirection() {
  232. return Direction.VERTICAL;
  233. }
  234. }
  235. /**
  236. * A representation of a single horizontal scrollbar.
  237. *
  238. * @see HorizontalScrollbarBundle#getElement()
  239. */
  240. public static final class HorizontalScrollbarBundle
  241. extends ScrollbarBundle {
  242. @Override
  243. public void setStylePrimaryName(String primaryStyleName) {
  244. super.setStylePrimaryName(primaryStyleName);
  245. root.addClassName(primaryStyleName + "-scroller-horizontal");
  246. }
  247. @Override
  248. protected void internalSetScrollPos(int px) {
  249. root.setScrollLeft(px);
  250. }
  251. @Override
  252. protected int internalGetScrollPos() {
  253. return root.getScrollLeft();
  254. }
  255. @Override
  256. protected void internalSetScrollSize(double px) {
  257. scrollSizeElement.getStyle().setWidth(px, Unit.PX);
  258. }
  259. @Override
  260. protected String internalGetScrollSize() {
  261. return scrollSizeElement.getStyle().getWidth();
  262. }
  263. @Override
  264. protected void internalSetOffsetSize(double px) {
  265. root.getStyle().setWidth(px, Unit.PX);
  266. }
  267. @Override
  268. public String internalGetOffsetSize() {
  269. return root.getStyle().getWidth();
  270. }
  271. @Override
  272. protected void internalSetScrollbarThickness(double px) {
  273. root.getStyle().setPaddingBottom(px, Unit.PX);
  274. root.getStyle().setHeight(0, Unit.PX);
  275. scrollSizeElement.getStyle().setHeight(px, Unit.PX);
  276. }
  277. @Override
  278. protected String internalGetScrollbarThickness() {
  279. return scrollSizeElement.getStyle().getHeight();
  280. }
  281. @Override
  282. protected void internalForceScrollbar(boolean enable) {
  283. if (enable) {
  284. root.getStyle().setOverflowX(Overflow.SCROLL);
  285. } else {
  286. root.getStyle().clearOverflowX();
  287. }
  288. }
  289. @Override
  290. public Direction getDirection() {
  291. return Direction.HORIZONTAL;
  292. }
  293. }
  294. protected final Element root = DOM.createDiv();
  295. protected final Element scrollSizeElement = DOM.createDiv();
  296. protected boolean isInvisibleScrollbar = false;
  297. private double scrollPos = 0;
  298. private double maxScrollPos = 0;
  299. private boolean scrollHandleIsVisible = false;
  300. private boolean isLocked = false;
  301. /** @deprecated access via {@link #getHandlerManager()} instead. */
  302. @Deprecated
  303. private HandlerManager handlerManager;
  304. private TemporaryResizer invisibleScrollbarTemporaryResizer = new TemporaryResizer();
  305. private final ScrollEventFirer scrollEventFirer = new ScrollEventFirer();
  306. private HandlerRegistration scrollSizeTemporaryScrollHandler;
  307. private HandlerRegistration offsetSizeTemporaryScrollHandler;
  308. private HandlerRegistration scrollInProgress;
  309. private ScrollbarBundle() {
  310. root.appendChild(scrollSizeElement);
  311. root.getStyle().setDisplay(Display.NONE);
  312. root.setTabIndex(-1);
  313. }
  314. protected abstract String internalGetScrollSize();
  315. /**
  316. * Sets the primary style name.
  317. *
  318. * @param primaryStyleName
  319. * The primary style name to use
  320. */
  321. public void setStylePrimaryName(String primaryStyleName) {
  322. root.setClassName(primaryStyleName + "-scroller");
  323. }
  324. /**
  325. * Gets the root element of this scrollbar-composition.
  326. *
  327. * @return the root element
  328. */
  329. public final Element getElement() {
  330. return root;
  331. }
  332. /**
  333. * Modifies the scroll position of this scrollbar by a number of pixels.
  334. * <p>
  335. * <em>Note:</em> Even though {@code double} values are used, they are
  336. * currently only used as integers as large {@code int} (or small but fast
  337. * {@code long}). This means, all values are truncated to zero decimal
  338. * places.
  339. *
  340. * @param delta
  341. * the delta in pixels to change the scroll position by
  342. */
  343. public final void setScrollPosByDelta(double delta) {
  344. if (delta != 0) {
  345. setScrollPos(getScrollPos() + delta);
  346. }
  347. }
  348. /**
  349. * Modifies {@link #root root's} dimensions in the axis the scrollbar is
  350. * representing.
  351. *
  352. * @param px
  353. * the new size of {@link #root} in the dimension this scrollbar
  354. * is representing
  355. */
  356. protected abstract void internalSetOffsetSize(double px);
  357. /**
  358. * Sets the length of the scrollbar.
  359. *
  360. * @param px
  361. * the length of the scrollbar in pixels
  362. */
  363. public final void setOffsetSize(final double px) {
  364. /*
  365. * This needs to be made step-by-step because IE8 flat-out refuses to
  366. * fire a scroll event when the scroll size becomes smaller than the
  367. * offset size. All other browser need to suffer alongside.
  368. */
  369. boolean newOffsetSizeIsGreaterThanScrollSize = px > getScrollSize();
  370. boolean offsetSizeBecomesGreaterThanScrollSize = showsScrollHandle()
  371. && newOffsetSizeIsGreaterThanScrollSize;
  372. if (offsetSizeBecomesGreaterThanScrollSize && getScrollPos() != 0) {
  373. if (offsetSizeTemporaryScrollHandler != null) {
  374. offsetSizeTemporaryScrollHandler.removeHandler();
  375. }
  376. // must be a field because Java insists.
  377. offsetSizeTemporaryScrollHandler = addScrollHandler(
  378. new ScrollHandler() {
  379. @Override
  380. public void onScroll(ScrollEvent event) {
  381. setOffsetSizeNow(px);
  382. }
  383. });
  384. setScrollPos(0);
  385. } else {
  386. setOffsetSizeNow(px);
  387. }
  388. }
  389. private void setOffsetSizeNow(double px) {
  390. internalSetOffsetSize(Math.max(0, px));
  391. recalculateMaxScrollPos();
  392. forceScrollbar(showsScrollHandle());
  393. fireVisibilityChangeIfNeeded();
  394. if (offsetSizeTemporaryScrollHandler != null) {
  395. offsetSizeTemporaryScrollHandler.removeHandler();
  396. offsetSizeTemporaryScrollHandler = null;
  397. }
  398. }
  399. /**
  400. * Force the scrollbar to be visible with CSS. In practice, this means to
  401. * set either <code>overflow-x</code> or <code>overflow-y</code> to "
  402. * <code>scroll</code>" in the scrollbar's direction.
  403. * <p>
  404. * This method is an IE8 workaround, since it doesn't always show scrollbars
  405. * with <code>overflow: auto</code> enabled.
  406. * <p>
  407. * Firefox on the other hand loses pending scroll events when the scrollbar
  408. * is hidden, so the event must be fired manually.
  409. * <p>
  410. * When IE8 support is dropped, this should really be simplified.
  411. */
  412. protected void forceScrollbar(boolean enable) {
  413. if (enable) {
  414. root.getStyle().clearDisplay();
  415. } else {
  416. if (BrowserInfo.get().isFirefox()) {
  417. /*
  418. * This is related to the Firefox workaround in setScrollSize
  419. * for setScrollPos(0)
  420. */
  421. scrollEventFirer.scheduleEvent();
  422. }
  423. root.getStyle().setDisplay(Display.NONE);
  424. }
  425. internalForceScrollbar(enable);
  426. }
  427. protected abstract void internalForceScrollbar(boolean enable);
  428. /**
  429. * Gets the length of the scrollbar.
  430. *
  431. * @return the length of the scrollbar in pixels
  432. */
  433. public double getOffsetSize() {
  434. return parseCssDimensionToPixels(internalGetOffsetSize());
  435. }
  436. public abstract String internalGetOffsetSize();
  437. /**
  438. * Sets the scroll position of the scrollbar in the axis the scrollbar is
  439. * representing.
  440. * <p>
  441. * <em>Note:</em> Even though {@code double} values are used, they are
  442. * currently only used as integers as large {@code int} (or small but fast
  443. * {@code long}). This means, all values are truncated to zero decimal
  444. * places.
  445. *
  446. * @param px
  447. * the new scroll position in pixels
  448. */
  449. public final void setScrollPos(double px) {
  450. if (isLocked()) {
  451. return;
  452. }
  453. double oldScrollPos = scrollPos;
  454. scrollPos = Math.max(0, Math.min(maxScrollPos, truncate(px)));
  455. if (!WidgetUtil.pixelValuesEqual(oldScrollPos, scrollPos)) {
  456. if (scrollInProgress == null) {
  457. // Only used for tracking that there is "workPending"
  458. scrollInProgress = addScrollHandler(new ScrollHandler() {
  459. @Override
  460. public void onScroll(ScrollEvent event) {
  461. scrollInProgress.removeHandler();
  462. scrollInProgress = null;
  463. }
  464. });
  465. }
  466. if (isInvisibleScrollbar) {
  467. invisibleScrollbarTemporaryResizer.show();
  468. }
  469. /*
  470. * This is where the value needs to be converted into an integer no
  471. * matter how we flip it, since GWT expects an integer value.
  472. * There's no point making a JSNI method that accepts doubles as the
  473. * scroll position, since the browsers themselves don't support such
  474. * large numbers (as of today, 25.3.2014). This double-ranged is
  475. * only facilitating future virtual scrollbars.
  476. */
  477. internalSetScrollPos(toInt32(scrollPos));
  478. }
  479. }
  480. /**
  481. * Should be called whenever this bundle is attached to the DOM (typically,
  482. * from the onLoad of the containing widget). Used to ensure the DOM scroll
  483. * position is maintained when detaching and reattaching the bundle.
  484. *
  485. * @since 7.4.1
  486. */
  487. public void onLoad() {
  488. internalSetScrollPos(toInt32(scrollPos));
  489. }
  490. /**
  491. * Truncates a double such that no decimal places are retained.
  492. * <p>
  493. * E.g. {@code trunc(2.3d) == 2.0d} and {@code trunc(-2.3d) == -2.0d}.
  494. *
  495. * @param num
  496. * the double value to be truncated
  497. * @return the {@code num} value without any decimal digits
  498. */
  499. private static double truncate(double num) {
  500. if (num > 0) {
  501. return Math.floor(num);
  502. } else {
  503. return Math.ceil(num);
  504. }
  505. }
  506. /**
  507. * Modifies the element's scroll position (scrollTop or scrollLeft).
  508. * <p>
  509. * <em>Note:</em> The parameter here is a type of integer (instead of a
  510. * double) by design. The browsers internally convert all double values into
  511. * an integer value. To make this fact explicit, this API has chosen to
  512. * force integers already at this level.
  513. *
  514. * @param px
  515. * integer pixel value to scroll to
  516. */
  517. protected abstract void internalSetScrollPos(int px);
  518. /**
  519. * Gets the scroll position of the scrollbar in the axis the scrollbar is
  520. * representing.
  521. *
  522. * @return the new scroll position in pixels
  523. */
  524. public final double getScrollPos() {
  525. assert internalGetScrollPos() == toInt32(
  526. scrollPos) : "calculated scroll position (" + scrollPos
  527. + ") did not match the DOM element scroll position ("
  528. + internalGetScrollPos() + ")";
  529. return scrollPos;
  530. }
  531. /**
  532. * Retrieves the element's scroll position (scrollTop or scrollLeft).
  533. * <p>
  534. * <em>Note:</em> The parameter here is a type of integer (instead of a
  535. * double) by design. The browsers internally convert all double values into
  536. * an integer value. To make this fact explicit, this API has chosen to
  537. * force integers already at this level.
  538. *
  539. * @return integer pixel value of the scroll position
  540. */
  541. protected abstract int internalGetScrollPos();
  542. /**
  543. * Modifies {@link #scrollSizeElement scrollSizeElement's} dimensions in
  544. * such a way that the scrollbar is able to scroll a certain number of
  545. * pixels in the axis it is representing.
  546. *
  547. * @param px
  548. * the new size of {@link #scrollSizeElement} in the dimension
  549. * this scrollbar is representing
  550. */
  551. protected abstract void internalSetScrollSize(double px);
  552. /**
  553. * Sets the amount of pixels the scrollbar needs to be able to scroll
  554. * through.
  555. *
  556. * @param px
  557. * the number of pixels the scrollbar should be able to scroll
  558. * through
  559. */
  560. public final void setScrollSize(final double px) {
  561. /*
  562. * This needs to be made step-by-step because IE8 flat-out refuses to
  563. * fire a scroll event when the scroll size becomes smaller than the
  564. * offset size. All other browser need to suffer alongside.
  565. *
  566. * This really should be changed to not use any temporary scroll
  567. * handlers at all once IE8 support is dropped, like now done only for
  568. * Firefox.
  569. */
  570. boolean newScrollSizeIsSmallerThanOffsetSize = px <= getOffsetSize();
  571. boolean scrollSizeBecomesSmallerThanOffsetSize = showsScrollHandle()
  572. && newScrollSizeIsSmallerThanOffsetSize;
  573. if (scrollSizeBecomesSmallerThanOffsetSize && getScrollPos() != 0) {
  574. /*
  575. * For whatever reason, Firefox loses the scroll event in this case
  576. * and the onscroll handler is never called (happens when reducing
  577. * size from 1000 items to 1 while being scrolled a bit down, see
  578. * #19802). Based on the comment above, only IE8 should really use
  579. * 'delayedSizeSet'
  580. */
  581. boolean delayedSizeSet = !BrowserInfo.get().isFirefox();
  582. if (delayedSizeSet) {
  583. if (scrollSizeTemporaryScrollHandler != null) {
  584. scrollSizeTemporaryScrollHandler.removeHandler();
  585. }
  586. scrollSizeTemporaryScrollHandler = addScrollHandler(
  587. new ScrollHandler() {
  588. @Override
  589. public void onScroll(ScrollEvent event) {
  590. setScrollSizeNow(px);
  591. }
  592. });
  593. }
  594. setScrollPos(0);
  595. if (!delayedSizeSet) {
  596. setScrollSizeNow(px);
  597. }
  598. } else {
  599. setScrollSizeNow(px);
  600. }
  601. }
  602. private void setScrollSizeNow(double px) {
  603. internalSetScrollSize(Math.max(0, px));
  604. recalculateMaxScrollPos();
  605. forceScrollbar(showsScrollHandle());
  606. fireVisibilityChangeIfNeeded();
  607. if (scrollSizeTemporaryScrollHandler != null) {
  608. scrollSizeTemporaryScrollHandler.removeHandler();
  609. scrollSizeTemporaryScrollHandler = null;
  610. }
  611. }
  612. /**
  613. * Gets the amount of pixels the scrollbar needs to be able to scroll
  614. * through.
  615. *
  616. * @return the number of pixels the scrollbar should be able to scroll
  617. * through
  618. */
  619. public double getScrollSize() {
  620. return parseCssDimensionToPixels(internalGetScrollSize());
  621. }
  622. /**
  623. * Modifies {@link #scrollSizeElement scrollSizeElement's} dimensions in the
  624. * opposite axis to what the scrollbar is representing.
  625. *
  626. * @param px
  627. * the dimension that {@link #scrollSizeElement} should take in
  628. * the opposite axis to what the scrollbar is representing
  629. */
  630. protected abstract void internalSetScrollbarThickness(double px);
  631. /**
  632. * Sets the scrollbar's thickness.
  633. * <p>
  634. * If the thickness is set to 0, the scrollbar will be treated as an
  635. * "invisible" scrollbar. This means, the DOM structure will be given a
  636. * non-zero size, but {@link #getScrollbarThickness()} will still return the
  637. * value 0.
  638. *
  639. * @param px
  640. * the scrollbar's thickness in pixels
  641. */
  642. public final void setScrollbarThickness(double px) {
  643. isInvisibleScrollbar = (px == 0);
  644. if (isInvisibleScrollbar) {
  645. Event.sinkEvents(root, Event.ONSCROLL);
  646. Event.setEventListener(root, new EventListener() {
  647. @Override
  648. public void onBrowserEvent(Event event) {
  649. invisibleScrollbarTemporaryResizer.show();
  650. }
  651. });
  652. root.getStyle().setVisibility(Visibility.HIDDEN);
  653. } else {
  654. Event.sinkEvents(root, 0);
  655. Event.setEventListener(root, null);
  656. root.getStyle().clearVisibility();
  657. }
  658. internalSetScrollbarThickness(Math.max(1d, px));
  659. }
  660. /**
  661. * Gets the scrollbar's thickness as defined in the DOM.
  662. *
  663. * @return the scrollbar's thickness as defined in the DOM, in pixels
  664. */
  665. protected abstract String internalGetScrollbarThickness();
  666. /**
  667. * Gets the scrollbar's thickness.
  668. * <p>
  669. * This value will differ from the value in the DOM, if the thickness was
  670. * set to 0 with {@link #setScrollbarThickness(double)}, as the scrollbar is
  671. * then treated as "invisible."
  672. *
  673. * @return the scrollbar's thickness in pixels
  674. */
  675. public final double getScrollbarThickness() {
  676. if (!isInvisibleScrollbar) {
  677. return parseCssDimensionToPixels(internalGetScrollbarThickness());
  678. } else {
  679. return 0;
  680. }
  681. }
  682. /**
  683. * Checks whether the scrollbar's handle is visible.
  684. * <p>
  685. * In other words, this method checks whether the contents is larger than
  686. * can visually fit in the element.
  687. *
  688. * @return <code>true</code> if the scrollbar's handle is visible
  689. */
  690. public boolean showsScrollHandle() {
  691. return getScrollSize() - getOffsetSize() > WidgetUtil.PIXEL_EPSILON;
  692. }
  693. public void recalculateMaxScrollPos() {
  694. double scrollSize = getScrollSize();
  695. double offsetSize = getOffsetSize();
  696. maxScrollPos = Math.max(0, scrollSize - offsetSize);
  697. // make sure that the correct max scroll position is maintained.
  698. setScrollPos(scrollPos);
  699. }
  700. /**
  701. * This is a method that JSNI can call to synchronize the object state from
  702. * the DOM.
  703. */
  704. private final void updateScrollPosFromDom() {
  705. /*
  706. * TODO: this method probably shouldn't be called from Escalator's JSNI,
  707. * but probably could be handled internally by this listening to its own
  708. * element. Would clean up the code quite a bit. Needs further
  709. * investigation.
  710. */
  711. int newScrollPos = internalGetScrollPos();
  712. if (!isLocked()) {
  713. scrollPos = newScrollPos;
  714. scrollEventFirer.scheduleEvent();
  715. } else if (scrollPos != newScrollPos) {
  716. // we need to actually undo the setting of the scroll.
  717. internalSetScrollPos(toInt32(scrollPos));
  718. }
  719. }
  720. protected HandlerManager getHandlerManager() {
  721. if (handlerManager == null) {
  722. handlerManager = new HandlerManager(this);
  723. }
  724. return handlerManager;
  725. }
  726. /**
  727. * Adds handler for the scrollbar handle visibility.
  728. *
  729. * @param handler
  730. * the {@link VisibilityHandler} to add
  731. * @return {@link HandlerRegistration} used to remove the handler
  732. */
  733. public HandlerRegistration addVisibilityHandler(
  734. final VisibilityHandler handler) {
  735. return getHandlerManager().addHandler(VisibilityChangeEvent.TYPE,
  736. handler);
  737. }
  738. private void fireVisibilityChangeIfNeeded() {
  739. final boolean oldHandleIsVisible = scrollHandleIsVisible;
  740. scrollHandleIsVisible = showsScrollHandle();
  741. if (oldHandleIsVisible != scrollHandleIsVisible) {
  742. final VisibilityChangeEvent event = new VisibilityChangeEvent(
  743. scrollHandleIsVisible);
  744. getHandlerManager().fireEvent(event);
  745. }
  746. }
  747. /**
  748. * Converts a double into an integer by JavaScript's terms.
  749. * <p>
  750. * Implementation copied from {@link Element#toInt32(double)}.
  751. *
  752. * @param val
  753. * the double value to convert into an integer
  754. * @return the double value converted to an integer
  755. */
  756. private static native int toInt32(double val)
  757. /*-{
  758. return Math.round(val) | 0;
  759. }-*/;
  760. /**
  761. * Locks or unlocks the scrollbar bundle.
  762. * <p>
  763. * A locked scrollbar bundle will refuse to scroll, both programmatically
  764. * and via user-triggered events.
  765. *
  766. * @param isLocked
  767. * <code>true</code> to lock, <code>false</code> to unlock
  768. */
  769. public void setLocked(boolean isLocked) {
  770. this.isLocked = isLocked;
  771. }
  772. /**
  773. * Checks whether the scrollbar bundle is locked or not.
  774. *
  775. * @return <code>true</code> if the scrollbar bundle is locked
  776. */
  777. public boolean isLocked() {
  778. return isLocked;
  779. }
  780. /**
  781. * Returns the scroll direction of this scrollbar bundle.
  782. *
  783. * @return the scroll direction of this scrollbar bundle
  784. */
  785. public abstract Direction getDirection();
  786. /**
  787. * Adds a scroll handler to the scrollbar bundle.
  788. *
  789. * @param handler
  790. * the handler to add
  791. * @return the registration object for the handler registration
  792. */
  793. public HandlerRegistration addScrollHandler(final ScrollHandler handler) {
  794. return getHandlerManager().addHandler(ScrollEvent.TYPE, handler);
  795. }
  796. private static double parseCssDimensionToPixels(String size) {
  797. /*
  798. * Sizes of elements are calculated from CSS rather than
  799. * element.getOffset*() because those values are 0 whenever display:
  800. * none. Because we know that all elements have populated
  801. * CSS-dimensions, it's better to do it that way.
  802. *
  803. * Another solution would be to make the elements visible while
  804. * measuring and then re-hide them, but that would cause unnecessary
  805. * reflows that would probably kill the performance dead.
  806. */
  807. if (size.isEmpty()) {
  808. return 0;
  809. } else {
  810. assert size.endsWith("px") : "Can't parse CSS dimension \"" + size
  811. + "\"";
  812. return Double.parseDouble(size.substring(0, size.length() - 2));
  813. }
  814. }
  815. @Override
  816. public boolean isWorkPending() {
  817. // Need to include scrollEventFirer.isBeingFired as it might use
  818. // requestAnimationFrame - which is not automatically checked
  819. return scrollSizeTemporaryScrollHandler != null
  820. || offsetSizeTemporaryScrollHandler != null
  821. || scrollInProgress != null || scrollEventFirer.isBeingFired;
  822. }
  823. }