Skip to content

Visual Studio Code, rebuilt in Rust

Rust

feat(workbench): add a shared scrollable element for panels and widgets

rokartur78ee19fe4f0a
diff --git a/native/crates/app/src/workbench/list_scroll.rs b/native/crates/app/src/workbench/list_scroll.rs
index 15a9fd5a7c2..ebfce845ec1 100644
--- a/native/crates/app/src/workbench/list_scroll.rs
+++ b/native/crates/app/src/workbench/list_scroll.rs
@@ -5,6 +5,7 @@
//! Keyboard Shortcuts table each keep their GPUI scroll state; this file drives it. A notebook editor's cell list
//! keeps its own scroll top (notebook_editor.rs).
+use std::cell::RefCell;
use std::collections::HashMap;
use std::time::{Duration, Instant};
@@ -51,6 +52,8 @@ pub enum ListId {
MarkerMessage,
/// The parameter hints widget's signature and docs (parameterHintsWidget.ts `DomScrollableElement`).
ParameterHints,
+ /// Any other list, tree or widget in a ScrollableElement, by its element id (`Workbench::scrollable`).
+ Scrollable(&'static str),
}
/// The GPUI state a list scrolls with: a scrolled div or uniform list, a `list` of measured rows, or a notebook
@@ -127,7 +130,11 @@ struct SmoothScroll {
}
#[derive(Default)]
-pub struct ListScrolls(HashMap<ListId, ListScroll>);
+pub struct ListScrolls {
+ lists: HashMap<ListId, ListScroll>,
+ /// The scroll handles of `ListId::Scrollable` elements, made on first draw.
+ handles: RefCell<HashMap<&'static str, ScrollHandle>>,
+}
impl Workbench {
pub(super) fn list_scroller(&self, list: ListId) -> ListScroller {
@@ -145,9 +152,36 @@ impl Workbench {
ListId::CommentThread { controller, thread } => ListScroller::Div(self.floating_comments_scroll(controller, thread)),
ListId::MarkerMessage => ListScroller::Div(self.marker_navigation.scroll.clone()),
ListId::ParameterHints => ListScroller::Div(self.parameter_hints.body_scroll.clone()),
+ ListId::Scrollable(id) => ListScroller::Div(self.scrollable_handle(id)),
}
}
+ /// scrollableElement.ts around `content`: it scrolls vertically in the handle kept under `id`, with the list's
+ /// wheel, fading scrollbars and top shadow over it. The returned box takes `content`'s place in its parent; a view
+ /// that reveals rows itself passes its own `handle`.
+ pub(super) fn scrollable(&self, id: &'static str, handle: Option<&ScrollHandle>, content: gpui::Stateful<gpui::Div>, cx: &mut Context<Self>) -> gpui::Stateful<gpui::Div> {
+ let handle = match handle {
+ Some(handle) => {
+ self.list_scrolls.handles.borrow_mut().insert(id, handle.clone());
+ handle.clone()
+ }
+ None => self.scrollable_handle(id),
+ };
+ div()
+ .id(id)
+ .relative()
+ .min_h_0()
+ .min_w_0()
+ .flex()
+ .flex_col()
+ .child(content.flex_1().min_h_0().min_w_0().overflow_y_scroll().track_scroll(&handle))
+ .child(self.list_scroll_layer(ListId::Scrollable(id), cx))
+ }
+
+ fn scrollable_handle(&self, id: &'static str) -> ScrollHandle {
+ self.list_scrolls.handles.borrow_mut().entry(id).or_default().clone()
+ }
+
/// `Scrollable.validateScrollPosition`: clamped to the content. Returns whether it moved.
fn set_list_position(&mut self, list: ListId, position: Point<f32>) -> bool {
let scroller = self.list_scroller(list);
@@ -183,7 +217,7 @@ impl Workbench {
}
fn list_scroll(&mut self, list: ListId) -> &mut ListScroll {
- self.list_scrolls.0.entry(list).or_default()
+ self.list_scrolls.lists.entry(list).or_default()
}
pub(super) fn horizontal_scrolling(&self, list: ListId) -> bool {
@@ -392,7 +426,7 @@ impl Workbench {
// gotoErrorWidget.ts creates its ScrollableElement with `useShadows: false`.
let shadow = (position.y > 0. && list != ListId::MarkerMessage).then(|| self.scroll_shadow());
let now = Instant::now();
- let (mut opacity, mut shown, animating, drag) = match self.list_scrolls.0.get(&list) {
+ let (mut opacity, mut shown, animating, drag) = match self.list_scrolls.lists.get(&list) {
Some(scroll) => (scroll.bars.opacity(now), scroll.bars.shown, scroll.bars.in_transition(now), scroll.drag),
None => (0., false, false, None),
};
@@ -525,7 +559,7 @@ impl Workbench {
/// Whether the pointer is over the list, and since when.
pub(super) fn list_hover(&self, list: ListId) -> Option<(bool, Instant)> {
- let scroll = self.list_scrolls.0.get(&list)?;
+ let scroll = self.list_scrolls.lists.get(&list)?;
Some((scroll.bars.mouse_over, scroll.hover_changed?))
}
diff --git a/native/crates/app/src/workbench/tree_guides.rs b/native/crates/app/src/workbench/tree_guides.rs
index c583c80ad74..82f10a2a30a 100644
--- a/native/crates/app/src/workbench/tree_guides.rs
+++ b/native/crates/app/src/workbench/tree_guides.rs
@@ -192,7 +192,7 @@ impl Workbench {
self.search.focus_and_select(Some(row.key));
window.focus(&self.search.focus.clone(), cx);
}
- ListId::KeybindingsTable | ListId::SettingsTree | ListId::EditorTabs { .. } | ListId::Notebook(_) | ListId::CommentThread { .. } | ListId::MarkerMessage | ListId::ParameterHints => unreachable!("only the Explorer and Search lists are trees"),
+ ListId::KeybindingsTable | ListId::SettingsTree | ListId::EditorTabs { .. } | ListId::Notebook(_) | ListId::CommentThread { .. } | ListId::MarkerMessage | ListId::ParameterHints | ListId::Scrollable(_) => unreachable!("only the Explorer and Search lists are trees"),
}
cx.notify();
}
@@ -202,7 +202,7 @@ impl Workbench {
ListId::Explorer => self.folder.as_ref().map(|tree| tree.rows().iter().map(|row| row.depth).collect()).unwrap_or_default(),
ListId::Outline => self.notebook_outline_depths().unwrap_or_else(|| self.outline_shown_rows().into_iter().map(|(_, depth)| depth).collect()),
ListId::Search => self.search.rows(&self.search_folders(), Some(&self.collapse_results())).iter().map(|row| row.depth).collect(),
- ListId::KeybindingsTable | ListId::SettingsTree | ListId::EditorTabs { .. } | ListId::Notebook(_) | ListId::CommentThread { .. } | ListId::MarkerMessage | ListId::ParameterHints => Vec::new(),
+ ListId::KeybindingsTable | ListId::SettingsTree | ListId::EditorTabs { .. } | ListId::Notebook(_) | ListId::CommentThread { .. } | ListId::MarkerMessage | ListId::ParameterHints | ListId::Scrollable(_) => Vec::new(),
}
}