Class VFXScrollBarBehavior

java.lang.Object
io.github.palexdev.mfxcore.behavior.BehaviorBase<VFXScrollBar>
io.github.palexdev.virtualizedfx.controls.behaviors.VFXScrollBarBehavior

public class VFXScrollBarBehavior extends io.github.palexdev.mfxcore.behavior.BehaviorBase<VFXScrollBar>
Extension of BehaviorBase and default behavior implementation for VFXScrollBar.

This offers all the methods to manage scrolling and smooth scrolling, track press/release, buttons press/release, thumb press/drag/release. And a bunch of other misc methods.
  • Field Details

    • FIRST_TICK_DURATION

      protected Duration FIRST_TICK_DURATION
    • FIRST_TICK_CURVE

      protected Interpolator FIRST_TICK_CURVE
    • SMOOTH_SCROLL_DURATION

      protected Duration SMOOTH_SCROLL_DURATION
    • TRACK_SMOOTH_SCROLL_DURATION

      protected Duration TRACK_SMOOTH_SCROLL_DURATION
    • SMOOTH_SCROLL_CURVE

      protected Interpolator SMOOTH_SCROLL_CURVE
    • MAX_BUTTONS_SCROLL_DURATION

      protected Duration MAX_BUTTONS_SCROLL_DURATION
    • HOLD_DELAY

      protected Duration HOLD_DELAY
  • Constructor Details

    • VFXScrollBarBehavior

      public VFXScrollBarBehavior(VFXScrollBar bar)
  • Method Details

    • thumbPressed

      public void thumbPressed(MouseEvent me)
      Action performed when the thumb is pressed.

      The first steps are to stop any animation (see stopAnimations()) then requestFocus() and update the dragStart position using getMousePos(MouseEvent), in case the next user action is dragging the thumb.
    • thumbDragged

      public void thumbDragged(MouseEvent me)
      Action performed when the thumb is being dragged.

      First we call onDragging(boolean). Then we acquire the mouse position with getMousePos(MouseEvent) to compute the traveled distance since the press event as currentPos - dragStart.

      We convert the traveled distance to the corresponding delta value by using NumberUtils.mapOneRangeToAnother(double, DoubleRange, DoubleRange). A call to getAndSetScrollDirection(boolean) updates the VFXScrollBar.scrollDirectionProperty() and returns a multiplier (1 or -1) used to adjust the scroll bar's value by the found delta value: bar.setValue(bar.getValue() + deltaVal * mul).

    • thumbReleased

      public void thumbReleased(MouseEvent me)
      Action performed when the thumb is released.

      The dragStart position is reset to 0.0, and the dragging property set to false.
    • onDragging

      protected void onDragging(boolean dragging)
      Simply enables or disabled the ":dragging" PseudoClass on the scroll bar depending on the given parameter.
    • trackPressed

      public void trackPressed(MouseEvent me)
      Action performed when the track is pressed.

      The first steps are to stop any animation (see stopAnimations()) then requestFocus(). Then we check whether smooth scroll and track smooth scroll features have been enabled, in such case the method exists as the scroll will be handled by trackReleased(MouseEvent).

      Otherwise, we get the mouse position with getMousePos(MouseEvent) and the track length with getTrackLength(MouseEvent). We define the target value as mousePos / trackL and the delta value as targetVal - bar.getValue(). Finally, the scroll direction is determined by getAndSetScrollDirection(boolean).

      At this point, two animations are built:

      The first animation is responsible for the "first tick". When you press the track you expect the thumb to move towards the mouse position by the specified VFXScrollBar.trackIncrementProperty(). Then we must also consider what happens when the value has been adjusted, but the mouse is still pressed. Here's when the second animation comes into play. This animation basically detects if the mouse is still pressed (the delay is specified by HOLD_DELAY) and makes the thumb reposition towards the mouse position with a MomentumTransition. Before doing so, of course, it checks if the thumb is already at the mouse position or beyond, in such cases it simply does nothing.

      Both the second animation and the inner MomentumTransition are assigned to variables so that if any event occurs in the meanwhile which requires the animations to stop it can be done.
    • trackReleased

      public void trackReleased(MouseEvent me)
      Action performed when the track is released

      The first step is to stop any animation (see stopAnimations()).

      The rest of the method executes only if both the smooth scroll and track smooth scroll features are enabled.

      As usual,we get the mouse position with getMousePos(MouseEvent) and the track length with getTrackLength(MouseEvent). We define the target value as mousePos / trackL and the delta value as targetVal - bar.getValue(). Finally, the scroll direction is determined by getAndSetScrollDirection(boolean).

      The scroll bar's value is adjusted with a MomentumTransition.

    • getTrackLength

      protected double getTrackLength(MouseEvent me)
      Retrieves the scroll bar's track length from the given MouseEvent. This can be done on events intercepted by the track because we get the node thanks to PickResult.getIntersectedNode(). The returned value depends on the orientation.
    • buttonPressed

      public void buttonPressed(MouseEvent me, int mul)
      Action performed when either the increase or decrease buttons are pressed. The mul parameters indicates which one is pressed and therefore whether to increment (1) or decrement(-1) the VFXScrollBar.valueProperty() by the amount specified by the VFXScrollBar.unitIncrementProperty().

      The first step is to stop any animation (see stopAnimations()) and to acquire focus with requestFocus().

      Two animations are built:

      The first animation is responsible for the "first tick". When you press the button you expect the thumb to move by the specified VFXScrollBar.unitIncrementProperty(). Then we must also consider what happens when the value has been adjusted, but the mouse is still pressed. Here's when the second animation comes into play. It basically detects if the mouse is being hold (the delay is specified by HOLD_DELAY) and makes the thumb reposition with a ConsumerTransition.

    • buttonReleased

      public void buttonReleased(MouseEvent me)
      Action performed when either the increase or decrease buttons are released.

      Simply calls stopAnimations().

    • getMousePos

      protected double getMousePos(MouseEvent me)
      Obtains the mouse position from the given MouseEvent depending on the scroll bar's orientation.
    • getAndSetScrollDirection

      protected int getAndSetScrollDirection(boolean incrementing)
    • getScrollDelta

      protected double getScrollDelta(ScrollEvent se)
      Obtains the scroll delta from the given ScrollEvent depending on the scroll bar's orientation.
    • requestFocus

      protected void requestFocus()
      Requests focus for the scroll bar if it's not already focused and if it's focus traversable.
    • stopAnimations

      protected void stopAnimations()
      Stops any currently playing animation, including smooth scroll animations, hold animation (those responsible for detecting mouse press and hold), and any other scroll animation (typically the ones created inside hold animations)
    • firstTick

      protected Animation firstTick(double targetVal)
      Convenience method to build a Timeline animation for the "first tick". Uses FIRST_TICK_DURATION and FIRST_TICK_CURVE and moves the scroll bar's value towards the given target value.
    • onHold

      protected Animation onHold(EventHandler<ActionEvent> handler)
      Convenience method to build a PauseTransition used to detect "mouse hold" events. The duration is set to HOLD_DELAY and the given handler determines what happens when the animation ends.
    • withMomentum

      protected io.github.palexdev.mfxeffects.animations.MomentumTransition withMomentum(double delta, Duration duration)
      Convenience method to build a MomentumTransition using MomentumTransition.fromTime(double, double) with the two given parameters. Uses SMOOTH_SCROLL_CURVE as the interpolator.
    • scroll

      public void scroll(ScrollEvent se, Consumer<ScrollEvent> callback)
      Action performed when a ScrollEvent occurs.

      First the scroll delta is computed with getScrollDelta(ScrollEvent) and in case it's 0 it immediately exits.

      Then we determine the scroll direction with getAndSetScrollDirection(boolean) and depending on the VFXScrollBar.smoothScrollProperty() the scroll value is adjuster either by a MomentumTransition or by the setter.

      Overrides:
      scroll in class io.github.palexdev.mfxcore.behavior.BehaviorBase<VFXScrollBar>
    • dispose

      public void dispose()
      Overrides:
      dispose in class io.github.palexdev.mfxcore.behavior.BehaviorBase<VFXScrollBar>