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 31KB

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