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

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