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

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