From a37f86f294a8ead0264ff149392308101b9cb0a1 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 29 Nov 2023 22:39:15 -0700 Subject: [PATCH] Scroll the collab panel when keyboard focusing --- crates/collab_ui2/src/collab_panel.rs | 24 ++++++------------------ crates/gpui2/src/elements/div.rs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/crates/collab_ui2/src/collab_panel.rs b/crates/collab_ui2/src/collab_panel.rs index 8df0de797b..542dca52bc 100644 --- a/crates/collab_ui2/src/collab_panel.rs +++ b/crates/collab_ui2/src/collab_panel.rs @@ -1808,14 +1808,9 @@ impl CollabPanel { self.selection = Some(ix); } - // todo!() - // self.list_state.reset(self.entries.len()); - // if let Some(ix) = self.selection { - // self.list_state.scroll_to(ListOffset { - // item_ix: ix, - // offset_in_item: 0., - // }); - // } + if let Some(ix) = self.selection { + self.scroll_handle.scroll_to_item(ix) + } cx.notify(); } @@ -1825,14 +1820,9 @@ impl CollabPanel { self.selection = Some(ix - 1); } - // todo!() - // self.list_state.reset(self.entries.len()); - // if let Some(ix) = self.selection { - // self.list_state.scroll_to(ListOffset { - // item_ix: ix, - // offset_in_item: 0., - // }); - // } + if let Some(ix) = self.selection { + self.scroll_handle.scroll_to_item(ix) + } cx.notify(); } @@ -2350,8 +2340,6 @@ impl CollabPanel { } fn render_signed_in(&mut self, cx: &mut ViewContext) -> Div { - dbg!(&self.scroll_handle.top_item()); - v_stack() .size_full() .child( diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index 50bc678b42..3d360877a1 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -1491,4 +1491,22 @@ impl ScrollHandle { Err(ix) => ix.min(state.child_bounds.len().saturating_sub(1)), } } + + /// scroll_to_item scrolls the minimal amount to ensure that the item is + /// fully visible + pub fn scroll_to_item(&self, ix: usize) { + let state = self.0.borrow(); + + let Some(bounds) = state.child_bounds.get(ix) else { + return; + }; + + let scroll_offset = state.offset.borrow().y; + + if bounds.top() + scroll_offset < state.bounds.top() { + state.offset.borrow_mut().y = state.bounds.top() - bounds.top(); + } else if bounds.bottom() + scroll_offset > state.bounds.bottom() { + state.offset.borrow_mut().y = state.bounds.bottom() - bounds.bottom(); + } + } }