Skip to main content

blit_widget/scroll/
area.rs

1use blit::{
2    Animation, Axis, Id, Rect, Scalar, Scope, Ui,
3    state::{NoLayout, Open},
4};
5
6use super::{CONTENT, Goal, Request, ScrollItem, ScrollLayout, ScrollState, Scrollbar, THUMB, TRACK, animate, update};
7use crate::Context;
8
9blit::builder! {
10    /// use for arbitrary content such as forms and small collections
11    /// builds all content every frame
12    pub struct Scroll<'a, T: Scalar> {
13        new(state: &'a mut ScrollState<T>),
14        axis: Axis = Axis::Vertical,
15        #[into]
16        animation: Animation = Animation::default(),
17        scroll_speed: f32 = 1.0,
18        inertia_friction: f32 = 2.5,
19        minimum_thumb_extent: T = T::from_f32(1.0),
20        /// scrollbars cover the content instead of taking space beside it
21        floating: bool = false,
22    }
23}
24
25impl<'state, T: Scalar> Scroll<'state, T> {
26    pub fn begin<'ui, C, S>(self, mut ui: Ui<'ui, C, S>) -> ScrollContent<'state, 'ui, C>
27    where
28        C: Context<Scalar = T>,
29        S: NoLayout,
30    {
31        let Self {
32            state,
33            axis,
34            animation,
35            scroll_speed,
36            inertia_friction,
37            minimum_thumb_extent,
38            floating,
39        } = self;
40        let parts = ui.scope();
41        let (thumb_active, viewport_known) = update(state, &mut ui, parts, axis, scroll_speed, inertia_friction);
42        // a focus change reveals the focused node wherever it is
43        if ui.focus_moved()
44            && let Some(focused) = ui.focus_within()
45        {
46            let content_id = ui.resolve(parts, CONTENT);
47            let content = ui.node(content_id).area();
48            if let Some(content) = content
49                && let Some(node) = ui.node(focused).area()
50            {
51                state.scroll_to(reveal_offset(state, axis, content, node));
52            }
53        }
54        let reveal = match state.request.target {
55            Goal::Node(target) => Some(target),
56            _ => {
57                animate(state, &mut ui, animation, viewport_known);
58                None
59            }
60        };
61        let offset = state.offset;
62        let viewport = ui
63            .layout(ScrollLayout {
64                axis,
65                offset,
66                viewport_extent: state.viewport_extent,
67                scrollbar: Scrollbar {
68                    minimum: minimum_thumb_extent,
69                    floating,
70                },
71            })
72            .clip(T::ZERO);
73        ScrollContent {
74            state,
75            viewport,
76            parts,
77            axis,
78            animation,
79            viewport_known,
80            thumb_active,
81            offset,
82            reveal,
83        }
84    }
85}
86
87#[must_use]
88pub struct ScrollContent<'state, 'ui, C: Context> {
89    state: &'state mut ScrollState<C::Scalar>,
90    viewport: Ui<'ui, C, Open<ScrollLayout<C::Scalar>>>,
91    parts: Scope,
92    axis: Axis,
93    animation: Animation,
94    viewport_known: bool,
95    thumb_active: bool,
96    offset: C::Scalar,
97    reveal: Option<Id>,
98}
99
100impl<'ui, C: Context> ScrollContent<'_, 'ui, C> {
101    pub fn content(mut self, content: impl FnOnce(Ui<'_, C>)) -> ScrollBar<'ui, C> {
102        let mut revealed = None;
103        self.viewport
104            .child_in(self.parts, CONTENT)
105            .item(ScrollItem::Content)
106            .build(|mut ui: Ui<'_, C>| {
107                if let Some(target) = self.reveal {
108                    revealed = ui.area().zip(ui.node(target).area());
109                }
110                content(ui)
111            });
112        match (self.reveal, revealed) {
113            (Some(_), Some((content, node))) => {
114                self.state.request.target = Goal::Offset(reveal_offset(self.state, self.axis, content, node));
115                animate(self.state, &mut self.viewport, self.animation, self.viewport_known);
116                if self.state.offset != self.offset {
117                    self.viewport.request_frame();
118                }
119            }
120            (Some(_), None) if core::mem::replace(&mut self.state.request.missed, true) => {
121                self.state.request = Request::default()
122            }
123            (Some(_), None) => self.viewport.request_frame(),
124            (None, _) => {}
125        }
126        ScrollBar {
127            viewport: self.viewport,
128            parts: self.parts,
129            thumb_active: self.thumb_active,
130        }
131    }
132}
133
134#[must_use]
135pub struct ScrollBar<'a, C: Context> {
136    viewport: Ui<'a, C, Open<ScrollLayout<C::Scalar>>>,
137    parts: Scope,
138    thumb_active: bool,
139}
140
141impl<C: Context> ScrollBar<'_, C> {
142    pub fn scrollbar(mut self, track: impl FnOnce(Ui<'_, C>), thumb: impl FnOnce(Ui<'_, C>, bool)) {
143        self.viewport
144            .child_in(self.parts, TRACK)
145            .item(ScrollItem::Track)
146            .build(track);
147        let thumb_active = self.thumb_active;
148        self.viewport
149            .child_in(self.parts, THUMB)
150            .item(ScrollItem::Thumb)
151            .build(|ui: Ui<'_, C>| thumb(ui, thumb_active));
152    }
153}
154
155/// the offset that moves the least to bring `node` into view, from previous layout areas
156fn reveal_offset<T: Scalar>(state: &ScrollState<T>, axis: Axis, content: Rect<T>, node: Rect<T>) -> T {
157    let start = match axis {
158        Axis::Horizontal => node.x - content.x,
159        Axis::Vertical => node.y - content.y,
160    };
161    let end = start.endpoint(axis.extent(node.size()));
162    state.offset.clamp((end - state.viewport_extent).min(start), start)
163}