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};
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) axis: ScrollbarAxis,
17}
18
19impl EditorScrollbarContext {
20    pub fn axis(&self) -> ScrollbarAxis {
21        self.axis
22    }
23
24    /// The editor's viewport without padding, for consumers that need its
25    /// text layout size. Scrollbars should use [`ScrollbarHandle::content_size`]
26    /// and [`ScrollbarHandle::viewport_bounds`] to calculate the scroll range;
27    /// both include the padding around this viewport.
28    pub fn scroll_viewport_size(&self) -> Size<Pixels> {
29        self.scroll_viewport
30    }
31}
32
33impl ScrollbarHandle for EditorScrollbarContext {
34    fn viewport_bounds(&self) -> Bounds<Pixels> {
35        self.viewport
36    }
37    fn content_size(&self) -> Size<Pixels> {
38        self.content + (self.viewport.size - self.scroll_viewport)
39    }
40    fn offset(&self) -> Point<Pixels> {
41        self.handle.offset()
42    }
43    fn set_offset(&self, offset: Point<Pixels>) {
44        self.handle.set_offset(offset);
45    }
46}
47
48/// Supply a scrollbar from the consuming UI library. None retains Base's default.
49pub type EditorScrollbarRenderer =
50    Rc<dyn Fn(EditorScrollbarContext, &mut Window, &mut App) -> AnyElement>;