Closes #4798 This PR implements a scrollbar for the terminal by turning `ScrollableHandle` into a trait, allowing us to implement a custom scroll handle, `TerminalScrollHandle`. It works by converting terminal lines into pixels that `ScrollableHandle` understands. When `ScrollableHandle` provides a changed offset (e.g., when you drag the scrollbar), we convert this pixel offset back into the number of lines to scroll and update the terminal content accordingly. While the current version works as expected, I believe the scrollbar's offset updates could potentially be turned into an event. This event could then be subscribed to in `TerminalView`, not needing to update the terminal's offset in the `render` method as it might have performance implications. Further ideas on this are welcome. Preview: https://github.com/user-attachments/assets/560f0aac-4544-4007-8f0b-8833386f608f Todo: - [x] Experiment with custom scrollbar responding to terminal mouse scroll - [x] Refactor existing scrollbar handle into a trait - [x] Update terminal to use the scrollbar trait instead of a custom scrollbar implementation - [x] Figure out how scrollbar events like mouse drag should notify the terminal to update its state - [x] Code clean up - [x] Scrollbar hide setting for terminal Release Notes: - Added scrollbar to the terminal
92 lines
2.5 KiB
Rust
92 lines
2.5 KiB
Rust
use std::{
|
|
any::Any,
|
|
cell::{Cell, RefCell},
|
|
rc::Rc,
|
|
};
|
|
|
|
use gpui::{size, Bounds, Point};
|
|
use terminal::Terminal;
|
|
use ui::{px, ContentSize, Pixels, ScrollableHandle};
|
|
|
|
#[derive(Debug)]
|
|
struct ScrollHandleState {
|
|
line_height: Pixels,
|
|
total_lines: usize,
|
|
viewport_lines: usize,
|
|
display_offset: usize,
|
|
}
|
|
|
|
impl ScrollHandleState {
|
|
fn new(terminal: &Terminal) -> Self {
|
|
Self {
|
|
line_height: terminal.last_content().size.line_height,
|
|
total_lines: terminal.total_lines(),
|
|
viewport_lines: terminal.viewport_lines(),
|
|
display_offset: terminal.last_content().display_offset,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct TerminalScrollHandle {
|
|
state: Rc<RefCell<ScrollHandleState>>,
|
|
pub future_display_offset: Rc<Cell<Option<usize>>>,
|
|
}
|
|
|
|
impl TerminalScrollHandle {
|
|
pub fn new(terminal: &Terminal) -> Self {
|
|
Self {
|
|
state: Rc::new(RefCell::new(ScrollHandleState::new(terminal))),
|
|
future_display_offset: Rc::new(Cell::new(None)),
|
|
}
|
|
}
|
|
|
|
pub fn update(&self, terminal: &Terminal) {
|
|
*self.state.borrow_mut() = ScrollHandleState::new(terminal);
|
|
}
|
|
}
|
|
|
|
impl ScrollableHandle for TerminalScrollHandle {
|
|
fn content_size(&self) -> Option<ContentSize> {
|
|
let state = self.state.borrow();
|
|
Some(ContentSize {
|
|
size: size(px(0.), px(state.total_lines as f32 * state.line_height.0)),
|
|
scroll_adjustment: Some(Point::new(px(0.), px(0.))),
|
|
})
|
|
}
|
|
|
|
fn offset(&self) -> Point<Pixels> {
|
|
let state = self.state.borrow();
|
|
let scroll_offset = state.total_lines - state.viewport_lines - state.display_offset;
|
|
Point::new(
|
|
px(0.),
|
|
-px(scroll_offset as f32 * self.state.borrow().line_height.0),
|
|
)
|
|
}
|
|
|
|
fn set_offset(&self, point: Point<Pixels>) {
|
|
let state = self.state.borrow();
|
|
let offset_delta = (point.y.0 / state.line_height.0).round() as i32;
|
|
|
|
let max_offset = state.total_lines - state.viewport_lines;
|
|
let display_offset = ((max_offset as i32 + offset_delta) as usize).min(max_offset);
|
|
|
|
self.future_display_offset.set(Some(display_offset));
|
|
}
|
|
|
|
fn viewport(&self) -> Bounds<Pixels> {
|
|
let state = self.state.borrow();
|
|
Bounds::new(
|
|
Point::new(px(0.), px(0.)),
|
|
size(
|
|
px(0.),
|
|
px(state.viewport_lines as f32 * state.line_height.0),
|
|
),
|
|
)
|
|
}
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
}
|