pub struct ScrollComponent {
    pub opacity: f64,
    pub timer_id: TimerToken,
    pub hovered: BarHoveredState,
    pub held: BarHeldState,
    pub enabled: ScrollbarsEnabled,
}
Expand description

Embeddable component exposing reusable scroll handling logic.

In most situations composing Scroll is a better idea for general UI construction. However some cases are not covered by composing those widgets, such as when a widget needs fine grained control over its scrolling state or doesn’t make sense to exist alone without scrolling behavior.

ScrollComponent contains the input-handling and scrollbar-positioning logic used by Scroll. It can be used to add this logic to a custom widget when the need arises.

It can be used like this:

  • Store an instance of ScrollComponent in your widget’s struct, and wrap the child widget to be scrolled in a ClipBox.
  • Call event and lifecycle with all event and lifecycle events before propagating them to children.
  • Call handle_scroll with all events after handling / propagating them.
  • Call draw_bars to draw the scrollbars.

Taking a look at the Scroll source code can be helpful. You can also do scrolling without wrapping a child in a ClipBox, but you will need to do certain event and paint transformations yourself; see the ClipBox source code for an example.

Fields§

§opacity: f64

Current opacity for both scrollbars

§timer_id: TimerToken

ID for the timer which schedules scrollbar fade out

§hovered: BarHoveredState

Which if any scrollbar is currently hovered by the mouse

§held: BarHeldState

Which if any scrollbar is currently being dragged by the mouse

§enabled: ScrollbarsEnabled

Which scrollbars are enabled

Implementations§

§

impl ScrollComponent

pub fn new() -> ScrollComponent

Constructs a new ScrollComponent for use.

pub fn are_bars_held(&self) -> bool

true if either scrollbar is currently held down/being dragged

pub fn reset_scrollbar_fade<F>(&mut self, request_timer: F, env: &Env)where F: FnOnce(Duration) -> TimerToken,

Makes the scrollbars visible, and resets the fade timer.

pub fn calc_vertical_bar_bounds(&self, port: &Viewport, env: &Env) -> Option<Rect>

Calculates the paint rect of the vertical scrollbar, or None if the vertical scrollbar is not visible.

pub fn calc_horizontal_bar_bounds( &self, port: &Viewport, env: &Env ) -> Option<Rect>

Calculates the paint rect of the horizontal scrollbar, or None if the horizontal scrollbar is not visible.

pub fn draw_bars(&self, ctx: &mut PaintCtx<'_, '_, '_>, port: &Viewport, env: &Env)

Draw scroll bars.

pub fn point_hits_vertical_bar( &self, port: &Viewport, pos: Point, env: &Env ) -> bool

Tests if the specified point overlaps the vertical scrollbar

Returns false if the vertical scrollbar is not visible

pub fn point_hits_horizontal_bar( &self, port: &Viewport, pos: Point, env: &Env ) -> bool

Tests if the specified point overlaps the horizontal scrollbar

Returns false if the horizontal scrollbar is not visible

pub fn event( &mut self, port: &mut Viewport, ctx: &mut EventCtx<'_, '_>, event: &Event, env: &Env )

Checks if the event applies to the scroll behavior, uses it, and marks it handled

Make sure to call on every event

pub fn handle_scroll( &mut self, port: &mut Viewport, ctx: &mut EventCtx<'_, '_>, event: &Event, env: &Env )

Applies mousewheel scrolling if the event has not already been handled

pub fn lifecycle( &mut self, ctx: &mut LifeCycleCtx<'_, '_>, event: &LifeCycle, env: &Env )

Perform any necessary action prompted by a lifecycle event

Make sure to call on every lifecycle event

Trait Implementations§

§

impl Clone for ScrollComponent

§

fn clone(&self) -> ScrollComponent

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for ScrollComponent

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for ScrollComponent

§

fn default() -> ScrollComponent

Returns the “default value” for a type. Read more
§

impl Copy for ScrollComponent

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more