Skip to main content

gpui_base/input/
scrollbar_presentation.rs

1//! Presentation hook for an editor's existing viewport, without a second scroll system.
2use crate::{ScrollbarAxis, ScrollbarHandle, ScrollbarPlacement};
3use gpui::{AnyElement, App, Bounds, Pixels, Point, ScrollHandle, Size, Window};
4use std::rc::Rc;
5
6/// Current-frame editor geometry and its retained scroll offset.
7/// The editor applies caret scrolling before invoking the presentation callback.
8/// Like other [`ScrollbarHandle`] implementations, the content size includes
9/// the viewport's padding, so subtracting the track size gives the scroll range.
10#[derive(Clone)]
11pub struct EditorScrollbarContext {
12    pub(crate) handle: ScrollHandle,
13    pub(crate) viewport: Bounds<Pixels>,
14    pub(crate) scroll_viewport: Size<Pixels>,
15    pub(crate) content: Size<Pixels>,
16    pub(crate) content_height_f64: f64,
17    pub(crate) scroll_y_f64: f64,
18    pub(crate) axis: ScrollbarAxis,
19    pub(crate) placement: ScrollbarPlacement,
20}
21
22impl EditorScrollbarContext {
23    pub fn axis(&self) -> ScrollbarAxis {
24        self.axis
25    }
26
27    /// Where the editor places its scrollbars, as set with
28    /// `scrollbar_placement`, for a supplied scrollbar to follow.
29    pub fn placement(&self) -> ScrollbarPlacement {
30        self.placement
31    }
32
33    /// Authoritative vertical content height in `f64` pixels (including padding
34    /// when combined via `scroll_viewport_size`).
35    pub fn content_height_f64(&self) -> f64 {
36        self.content_height_f64
37    }
38
39    /// Authoritative vertical scroll offset in `f64` pixels (`<= 0.0`).
40    pub fn scroll_y_f64(&self) -> f64 {
41        self.scroll_y_f64
42    }
43
44    /// The editor's viewport without padding, for consumers that need its
45    /// text layout size. Scrollbars should use [`ScrollbarHandle::content_size`]
46    /// and [`ScrollbarHandle::viewport_bounds`] to calculate the scroll range;
47    /// both include the padding around this viewport.
48    pub fn scroll_viewport_size(&self) -> Size<Pixels> {
49        self.scroll_viewport
50    }
51}
52
53impl ScrollbarHandle for EditorScrollbarContext {
54    fn viewport_bounds(&self) -> Bounds<Pixels> {
55        self.viewport
56    }
57    fn content_size(&self) -> Size<Pixels> {
58        self.content + (self.viewport.size - self.scroll_viewport)
59    }
60    fn offset(&self) -> Point<Pixels> {
61        self.handle.offset()
62    }
63    fn set_offset(&self, offset: Point<Pixels>) {
64        self.handle.set_offset(offset);
65    }
66}
67
68/// Supply a scrollbar from the consuming UI library. None retains Base's default.
69pub type EditorScrollbarRenderer =
70    Rc<dyn Fn(EditorScrollbarContext, &mut Window, &mut App) -> AnyElement>;