1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use crate::*;

#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))]
pub(crate) struct State {
    /// Positive offset means scrolling down/right
    offset: Vec2,

    show_scroll: bool,

    /// Momentum, used for kinetic scrolling
    #[cfg_attr(feature = "persistence", serde(skip))]
    pub vel: Vec2,
    /// Mouse offset relative to the top of the handle when started moving the handle.
    scroll_start_offset_from_top: Option<f32>,
}

impl Default for State {
    fn default() -> Self {
        Self {
            offset: Vec2::ZERO,
            show_scroll: false,
            vel: Vec2::ZERO,
            scroll_start_offset_from_top: None,
        }
    }
}

// TODO: rename VScroll
/// Add vertical scrolling to a contained [`Ui`].
#[derive(Clone, Debug)]
#[must_use = "You should call .show()"]
pub struct ScrollArea {
    max_height: f32,
    always_show_scroll: bool,
    id_source: Option<Id>,
    offset: Option<Vec2>,
    scrolling_enabled: bool,
}

impl ScrollArea {
    /// Will make the area be as high as it is allowed to be (i.e. fill the [`Ui`] it is in)
    pub fn auto_sized() -> Self {
        Self::from_max_height(f32::INFINITY)
    }

    /// Use `f32::INFINITY` if you want the scroll area to expand to fit the surrounding Ui
    pub fn from_max_height(max_height: f32) -> Self {
        Self {
            max_height,
            always_show_scroll: false,
            id_source: None,
            offset: None,
            scrolling_enabled: true,
        }
    }

    /// If `false` (default), the scroll bar will be hidden when not needed/
    /// If `true`, the scroll bar will always be displayed even if not needed.
    pub fn always_show_scroll(mut self, always_show_scroll: bool) -> Self {
        self.always_show_scroll = always_show_scroll;
        self
    }

    /// A source for the unique `Id`, e.g. `.id_source("second_scroll_area")` or `.id_source(loop_index)`.
    pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
        self.id_source = Some(Id::new(id_source));
        self
    }

    /// Set the vertical scroll offset position.
    ///
    /// See also: [`Ui::scroll_to_cursor`](crate::ui::Ui::scroll_to_cursor) and
    /// [`Response::scroll_to_me`](crate::Response::scroll_to_me)
    pub fn scroll_offset(mut self, offset: f32) -> Self {
        self.offset = Some(Vec2::new(0.0, offset));
        self
    }

    /// Control the scrolling behavior
    /// If `true` (default), the scroll area will respond to user scrolling
    /// If `false`, the scroll area will not respond to user scrolling
    ///
    /// This can be used, for example, to optionally freeze scrolling while the user
    /// is inputing text in a `TextEdit` widget contained within the scroll area
    pub fn enable_scrolling(mut self, enable: bool) -> Self {
        self.scrolling_enabled = enable;
        self
    }
}

struct Prepared {
    id: Id,
    state: State,
    current_scroll_bar_width: f32,
    always_show_scroll: bool,
    inner_rect: Rect,
    content_ui: Ui,
    /// Relative coordinates: the offset and size of the view of the inner UI.
    /// `viewport.min == ZERO` means we scrolled to the top.
    viewport: Rect,
    scrolling_enabled: bool,
}

impl ScrollArea {
    fn begin(self, ui: &mut Ui) -> Prepared {
        let Self {
            max_height,
            always_show_scroll,
            id_source,
            offset,
            scrolling_enabled,
        } = self;

        let ctx = ui.ctx().clone();

        let id_source = id_source.unwrap_or_else(|| Id::new("scroll_area"));
        let id = ui.make_persistent_id(id_source);
        let mut state = *ctx.memory().id_data.get_or_default::<State>(id);

        if let Some(offset) = offset {
            state.offset = offset;
        }

        // content: size of contents (generally large; that's why we want scroll bars)
        // outer: size of scroll area including scroll bar(s)
        // inner: excluding scroll bar(s). The area we clip the contents to.

        let max_scroll_bar_width = max_scroll_bar_width_with_margin(ui);

        let current_scroll_bar_width = if always_show_scroll {
            max_scroll_bar_width
        } else {
            max_scroll_bar_width * ui.ctx().animate_bool(id, state.show_scroll)
        };

        let available_outer = ui.available_rect_before_wrap();

        let outer_size = vec2(
            available_outer.width(),
            available_outer.height().at_most(max_height),
        );

        let inner_size = outer_size - vec2(current_scroll_bar_width, 0.0);
        let inner_rect = Rect::from_min_size(available_outer.min, inner_size);

        let mut content_ui = ui.child_ui(
            Rect::from_min_size(
                inner_rect.min - state.offset,
                vec2(inner_size.x, f32::INFINITY),
            ),
            *ui.layout(),
        );
        let mut content_clip_rect = inner_rect.expand(ui.visuals().clip_rect_margin);
        content_clip_rect = content_clip_rect.intersect(ui.clip_rect());
        content_clip_rect.max.x = ui.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible
        content_ui.set_clip_rect(content_clip_rect);

        let viewport = Rect::from_min_size(Pos2::ZERO + state.offset, inner_size);

        Prepared {
            id,
            state,
            current_scroll_bar_width,
            always_show_scroll,
            inner_rect,
            content_ui,
            viewport,
            scrolling_enabled,
        }
    }

    /// Show the `ScrollArea`, and add the contents to the viewport.
    ///
    /// If the inner area can be very long, consider using [`Self::show_rows`] instead.
    pub fn show<R>(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R {
        self.show_viewport(ui, |ui, _viewport| add_contents(ui))
    }

    /// Efficiently show only the visible part of a large number of rows.
    ///
    /// ```
    /// # let ui = &mut egui::Ui::__test();
    /// let text_style = egui::TextStyle::Body;
    /// let row_height = ui.fonts()[text_style].row_height();
    /// // let row_height = ui.spacing().interact_size.y; // if you are adding buttons instead of labels.
    /// let num_rows = 10_000;
    /// egui::ScrollArea::auto_sized().show_rows(ui, row_height, num_rows, |ui, row_range| {
    ///     for row in row_range {
    ///         let text = format!("Row {}/{}", row + 1, num_rows);
    ///         ui.label(text);
    ///     }
    /// });
    pub fn show_rows<R>(
        self,
        ui: &mut Ui,
        row_height_sans_spacing: f32,
        num_rows: usize,
        add_contents: impl FnOnce(&mut Ui, std::ops::Range<usize>) -> R,
    ) -> R {
        let spacing = ui.spacing().item_spacing;
        let row_height_with_spacing = row_height_sans_spacing + spacing.y;
        self.show_viewport(ui, |ui, viewport| {
            ui.set_height((row_height_with_spacing * num_rows as f32 - spacing.y).at_least(0.0));

            let min_row = (viewport.min.y / row_height_with_spacing)
                .floor()
                .at_least(0.0) as usize;
            let max_row = (viewport.max.y / row_height_with_spacing).ceil() as usize + 1;
            let max_row = max_row.at_most(num_rows);

            let y_min = ui.max_rect().top() + min_row as f32 * row_height_with_spacing;
            let y_max = ui.max_rect().top() + max_row as f32 * row_height_with_spacing;
            let mut viewport_ui = ui.child_ui(
                Rect::from_x_y_ranges(ui.max_rect().x_range(), y_min..=y_max),
                *ui.layout(),
            );

            viewport_ui.skip_ahead_auto_ids(min_row); // Make sure we get consistent IDs.

            add_contents(&mut viewport_ui, min_row..max_row)
        })
    }

    /// This can be used to only paint the visible part of the contents.
    ///
    /// `add_contents` is past the viewport, which is the relative view of the content.
    /// So if the passed rect has min = zero, then show the top left content (the user has not scrolled).
    pub fn show_viewport<R>(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui, Rect) -> R) -> R {
        let mut prepared = self.begin(ui);
        let ret = add_contents(&mut prepared.content_ui, prepared.viewport);
        prepared.end(ui);
        ret
    }
}

impl Prepared {
    fn end(self, ui: &mut Ui) {
        let Prepared {
            id,
            mut state,
            inner_rect,
            always_show_scroll,
            mut current_scroll_bar_width,
            content_ui,
            viewport: _,
            scrolling_enabled,
        } = self;

        let content_size = content_ui.min_size();

        // We take the scroll target so only this ScrollArea will use it.
        let scroll_target = content_ui.ctx().frame_state().scroll_target.take();
        if let Some((scroll_y, align)) = scroll_target {
            let center_factor = align.to_factor();

            let top = content_ui.min_rect().top();
            let visible_range = top..=top + content_ui.clip_rect().height();
            let offset_y = scroll_y - lerp(visible_range, center_factor);

            let mut spacing = ui.spacing().item_spacing.y;

            // Depending on the alignment we need to add or subtract the spacing
            spacing *= remap(center_factor, 0.0..=1.0, -1.0..=1.0);

            state.offset.y = offset_y + spacing;
        }

        let inner_rect = {
            let width = if inner_rect.width().is_finite() {
                inner_rect.width().max(content_size.x) // Expand width to fit content
            } else {
                // ScrollArea is in an infinitely wide parent
                content_size.x
            };

            let mut inner_rect =
                Rect::from_min_size(inner_rect.min, vec2(width, inner_rect.height()));

            // The window that egui sits in can't be expanded by egui, so we need to respect it:
            let max_x = ui.input().screen_rect().right()
                - current_scroll_bar_width
                - ui.spacing().item_spacing.x;
            inner_rect.max.x = inner_rect.max.x.at_most(max_x);
            // TODO: when we support it, we should maybe auto-enable
            // horizontal scrolling if this limit is reached

            inner_rect
        };

        let outer_rect = Rect::from_min_size(
            inner_rect.min,
            inner_rect.size() + vec2(current_scroll_bar_width, 0.0),
        );

        let content_is_too_small = content_size.y > inner_rect.height();

        if content_is_too_small {
            // Drag contents to scroll (for touch screens mostly):
            let sense = if self.scrolling_enabled {
                Sense::drag()
            } else {
                Sense::hover()
            };
            let content_response = ui.interact(inner_rect, id.with("area"), sense);

            let input = ui.input();
            if content_response.dragged() {
                state.offset.y -= input.pointer.delta().y;
                state.vel = input.pointer.velocity();
            } else {
                let stop_speed = 20.0; // Pixels per second.
                let friction_coeff = 1000.0; // Pixels per second squared.
                let dt = input.unstable_dt;

                let friction = friction_coeff * dt;
                if friction > state.vel.length() || state.vel.length() < stop_speed {
                    state.vel = Vec2::ZERO;
                } else {
                    state.vel -= friction * state.vel.normalized();
                    // Offset has an inverted coordinate system compared to
                    // the velocity, so we subtract it instead of adding it
                    state.offset.y -= state.vel.y * dt;
                    ui.ctx().request_repaint();
                }
            }
        }

        let max_offset = content_size.y - inner_rect.height();
        if scrolling_enabled && ui.rect_contains_pointer(outer_rect) {
            let mut frame_state = ui.ctx().frame_state();
            let scroll_delta = frame_state.scroll_delta;

            let scrolling_up = state.offset.y > 0.0 && scroll_delta.y > 0.0;
            let scrolling_down = state.offset.y < max_offset && scroll_delta.y < 0.0;

            if scrolling_up || scrolling_down {
                state.offset.y -= scroll_delta.y;
                // Clear scroll delta so no parent scroll will use it.
                frame_state.scroll_delta = Vec2::ZERO;
            }
        }

        let show_scroll_this_frame = content_is_too_small || always_show_scroll;

        let max_scroll_bar_width = max_scroll_bar_width_with_margin(ui);

        if show_scroll_this_frame && current_scroll_bar_width <= 0.0 {
            // Avoid frame delay; start showing scroll bar right away:
            current_scroll_bar_width = max_scroll_bar_width * ui.ctx().animate_bool(id, true);
        }

        if current_scroll_bar_width > 0.0 {
            let animation_t = current_scroll_bar_width / max_scroll_bar_width;
            // margin between contents and scroll bar
            let margin = animation_t * ui.spacing().item_spacing.x;
            let left = inner_rect.right() + margin;
            let right = outer_rect.right();
            let top = inner_rect.top();
            let bottom = inner_rect.bottom();

            let outer_scroll_rect = Rect::from_min_max(
                pos2(left, inner_rect.top()),
                pos2(right, inner_rect.bottom()),
            );

            let from_content =
                |content_y| remap_clamp(content_y, 0.0..=content_size.y, top..=bottom);

            let handle_rect = Rect::from_min_max(
                pos2(left, from_content(state.offset.y)),
                pos2(right, from_content(state.offset.y + inner_rect.height())),
            );

            let interact_id = id.with("vertical");
            let sense = if self.scrolling_enabled {
                Sense::click_and_drag()
            } else {
                Sense::hover()
            };
            let response = ui.interact(outer_scroll_rect, interact_id, sense);

            if let Some(pointer_pos) = response.interact_pointer_pos() {
                let scroll_start_offset_from_top =
                    state.scroll_start_offset_from_top.get_or_insert_with(|| {
                        if handle_rect.contains(pointer_pos) {
                            pointer_pos.y - handle_rect.top()
                        } else {
                            let handle_top_pos_at_bottom = bottom - handle_rect.height();
                            // Calculate the new handle top position, centering the handle on the mouse.
                            let new_handle_top_pos = (pointer_pos.y - handle_rect.height() / 2.0)
                                .clamp(top, handle_top_pos_at_bottom);
                            pointer_pos.y - new_handle_top_pos
                        }
                    });

                let new_handle_top = pointer_pos.y - *scroll_start_offset_from_top;
                state.offset.y = remap(new_handle_top, top..=bottom, 0.0..=content_size.y);
            } else {
                state.scroll_start_offset_from_top = None;
            }

            let unbounded_offset_y = state.offset.y;
            state.offset.y = state.offset.y.max(0.0);
            state.offset.y = state.offset.y.min(max_offset);

            if state.offset.y != unbounded_offset_y {
                state.vel = Vec2::ZERO;
            }

            // Avoid frame-delay by calculating a new handle rect:
            let mut handle_rect = Rect::from_min_max(
                pos2(left, from_content(state.offset.y)),
                pos2(right, from_content(state.offset.y + inner_rect.height())),
            );
            let min_handle_height = ui.spacing().scroll_bar_width;
            if handle_rect.size().y < min_handle_height {
                handle_rect = Rect::from_center_size(
                    handle_rect.center(),
                    vec2(handle_rect.size().x, min_handle_height),
                );
            }

            let visuals = if scrolling_enabled {
                ui.style().interact(&response)
            } else {
                &ui.style().visuals.widgets.inactive
            };

            ui.painter().add(epaint::Shape::rect_filled(
                outer_scroll_rect,
                visuals.corner_radius,
                ui.visuals().extreme_bg_color,
            ));

            ui.painter().add(epaint::Shape::rect_filled(
                handle_rect,
                visuals.corner_radius,
                visuals.bg_fill,
            ));
        }

        let size = vec2(
            outer_rect.size().x,
            outer_rect.size().y.min(content_size.y), // shrink if content is so small that we don't need scroll bars
        );
        ui.advance_cursor_after_rect(Rect::from_min_size(outer_rect.min, size));

        if show_scroll_this_frame != state.show_scroll {
            ui.ctx().request_repaint();
        }

        state.offset.y = state.offset.y.min(content_size.y - inner_rect.height());
        state.offset.y = state.offset.y.max(0.0);
        state.show_scroll = show_scroll_this_frame;

        ui.memory().id_data.insert(id, state);
    }
}

fn max_scroll_bar_width_with_margin(ui: &Ui) -> f32 {
    ui.spacing().item_spacing.x + ui.spacing().scroll_bar_width
}