Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ScrollbarBundle.java 29KB

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