kas_core/core/
scroll_traits.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Scroll bar traits
7
8use crate::Widget;
9use crate::event::EventCx;
10use crate::geom::{Offset, Size};
11#[allow(unused)] use crate::{Events, Layout};
12
13/// Additional functionality on scrollable widgets
14///
15/// This trait should be implemented by widgets supporting scrolling, enabling
16/// a parent to control scrolling.
17///
18/// If the widget scrolls itself it should set a scroll action via [`EventCx::set_scroll`].
19pub trait Scrollable: Widget {
20    /// Get the content size (the inner size of the scrolled pane)
21    ///
22    /// This is used to determine the amount of scrolling required, hence it is
23    /// acceptable to return 0 on non-scrolled dimensions.
24    ///
25    /// Note: this is called *before* [`Layout::set_rect`], thus must may need
26    /// to perform independent calculation of the content size.
27    fn content_size(&self) -> Size;
28
29    /// Get the maximum scroll offset
30    ///
31    /// Note: the minimum scroll offset is always zero.
32    ///
33    /// Note: this is called immediately after [`Layout::set_rect`], thus should
34    /// be updated there (as well as by [`Events::update`] if appropriate).
35    fn max_scroll_offset(&self) -> Offset;
36
37    /// Get the current scroll offset
38    ///
39    /// Contents of the scroll region are translated by this offset (to convert
40    /// coordinates from the outer region to the scroll region, add this offset).
41    ///
42    /// The offset is restricted between [`Offset::ZERO`] and
43    /// [`Self::max_scroll_offset`].
44    fn scroll_offset(&self) -> Offset;
45
46    /// Set the scroll offset
47    ///
48    /// This may be used for programmatic scrolling, e.g. by a wrapping widget
49    /// with scroll controls. Note that calling this method directly on the
50    /// scrolling widget will not update any controls in a wrapping widget.
51    ///
52    /// The offset is clamped to the available scroll range and applied. The
53    /// resulting offset is returned.
54    fn set_scroll_offset(&mut self, cx: &mut EventCx, offset: Offset) -> Offset;
55}