From e2a447420078fa194f772dbc031d57e297cd3d40 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 21 Dec 2023 15:22:49 +0100 Subject: [PATCH 1/4] Allow scrolling saved conversations --- crates/assistant2/src/assistant_panel.rs | 45 +++++++++++++++--------- crates/gpui2/src/element.rs | 11 ++++++ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/crates/assistant2/src/assistant_panel.rs b/crates/assistant2/src/assistant_panel.rs index 7b15619758679b5e9246780813de63f0f1371261..2c1493cdfe93fdd5b392acd91390079bae9e378e 100644 --- a/crates/assistant2/src/assistant_panel.rs +++ b/crates/assistant2/src/assistant_panel.rs @@ -29,12 +29,12 @@ use editor::{ use fs::Fs; use futures::StreamExt; use gpui::{ - div, point, relative, rems, uniform_list, Action, AnyElement, AppContext, AsyncWindowContext, - ClipboardItem, Context, Div, EventEmitter, FocusHandle, Focusable, FocusableView, FontStyle, - FontWeight, HighlightStyle, InteractiveElement, IntoElement, Model, ModelContext, - ParentElement, Pixels, PromptLevel, Render, SharedString, StatefulInteractiveElement, Styled, - Subscription, Task, TextStyle, UniformListScrollHandle, View, ViewContext, VisualContext, - WeakModel, WeakView, WhiteSpace, WindowContext, + canvas, div, point, relative, rems, uniform_list, Action, AnyElement, AppContext, + AsyncWindowContext, AvailableSpace, ClipboardItem, Context, Div, EventEmitter, FocusHandle, + Focusable, FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, + IntoElement, Model, ModelContext, ParentElement, Pixels, PromptLevel, Render, SharedString, + StatefulInteractiveElement, Styled, Subscription, Task, TextStyle, UniformListScrollHandle, + View, ViewContext, VisualContext, WeakModel, WeakView, WhiteSpace, WindowContext, }; use language::{language_settings::SoftWrap, Buffer, LanguageRegistry, ToOffset as _}; use project::Project; @@ -1184,17 +1184,28 @@ impl Render for AssistantPanel { .child(if let Some(editor) = self.active_editor() { editor.clone().into_any_element() } else { - uniform_list( - cx.view().clone(), - "saved_conversations", - self.saved_conversations.len(), - |this, range, cx| { - range - .map(|ix| this.render_saved_conversation(ix, cx)) - .collect() - }, - ) - .track_scroll(self.saved_conversations_scroll_handle.clone()) + let view = cx.view().clone(); + let scroll_handle = self.saved_conversations_scroll_handle.clone(); + let conversation_count = self.saved_conversations.len(); + canvas(move |bounds, cx| { + uniform_list( + view, + "saved_conversations", + conversation_count, + |this, range, cx| { + range + .map(|ix| this.render_saved_conversation(ix, cx)) + .collect() + }, + ) + .track_scroll(scroll_handle) + .draw( + bounds.origin, + bounds.size, + cx, + ); + }) + .size_full() .into_any_element() }), ) diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index 4201123a10110f540258b8afc462aeab63b3fb1a..3901db73e05c6148b813dc3937dd42843b7bc6b7 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -23,6 +23,17 @@ pub trait IntoElement: Sized { self.into_element().into_any() } + fn draw(self, origin: Point, available_space: Size, cx: &mut WindowContext) + where + T: Clone + Default + Debug + Into, + { + let element = DrawableElement { + element: Some(self.into_element()), + phase: ElementDrawPhase::Start, + }; + DrawableElement::draw(element, origin, available_space.map(Into::into), cx); + } + fn draw_and_update_state( self, origin: Point, From 176a022b10ac6c9e4ca6237a6c44e73de4044a88 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 21 Dec 2023 15:23:16 +0100 Subject: [PATCH 2/4] Correctly clamp scroll offset vertically --- crates/gpui2/src/elements/div.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index e6fc58e872dbb5c054bfb0bd11a98f3713586a01..90af9e130e79a375cd339e03880790c8cc1acdd6 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -1472,7 +1472,7 @@ impl Interactivity { { let mut scroll_offset = scroll_offset.borrow_mut(); scroll_offset.x = scroll_offset.x.clamp(-scroll_max.width, px(0.)); - scroll_offset.y = scroll_offset.x.clamp(-scroll_max.height, px(0.)); + scroll_offset.y = scroll_offset.y.clamp(-scroll_max.height, px(0.)); } let interactive_bounds = interactive_bounds.clone(); From 5e4557ed1634807e9ae8199a80d059ac00c42f75 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 21 Dec 2023 15:26:41 +0100 Subject: [PATCH 3/4] Fix warnings --- crates/assistant2/src/assistant_panel.rs | 6 +++--- crates/terminal_view2/src/terminal_element.rs | 18 ++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/assistant2/src/assistant_panel.rs b/crates/assistant2/src/assistant_panel.rs index 2c1493cdfe93fdd5b392acd91390079bae9e378e..85d3f6f74083afe982ce7bc377426dcb08b45d05 100644 --- a/crates/assistant2/src/assistant_panel.rs +++ b/crates/assistant2/src/assistant_panel.rs @@ -30,9 +30,9 @@ use fs::Fs; use futures::StreamExt; use gpui::{ canvas, div, point, relative, rems, uniform_list, Action, AnyElement, AppContext, - AsyncWindowContext, AvailableSpace, ClipboardItem, Context, Div, EventEmitter, FocusHandle, - Focusable, FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, - IntoElement, Model, ModelContext, ParentElement, Pixels, PromptLevel, Render, SharedString, + AsyncWindowContext, ClipboardItem, Context, Div, EventEmitter, FocusHandle, Focusable, + FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, IntoElement, Model, + ModelContext, ParentElement, Pixels, PromptLevel, Render, SharedString, StatefulInteractiveElement, Styled, Subscription, Task, TextStyle, UniformListScrollHandle, View, ViewContext, VisualContext, WeakModel, WeakView, WhiteSpace, WindowContext, }; diff --git a/crates/terminal_view2/src/terminal_element.rs b/crates/terminal_view2/src/terminal_element.rs index f30c1cf1bc186f3d9f121739f6a00bdf59fb0c75..3b7b32dd6f609a2dc22668805cfa8adeda27bae0 100644 --- a/crates/terminal_view2/src/terminal_element.rs +++ b/crates/terminal_view2/src/terminal_element.rs @@ -1,11 +1,11 @@ use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; use gpui::{ - black, div, fill, point, px, red, relative, AnyElement, AsyncWindowContext, AvailableSpace, - BorrowWindow, Bounds, DispatchPhase, Element, ElementId, ExternalPaths, FocusHandle, Font, - FontStyle, FontWeight, HighlightStyle, Hsla, InteractiveElement, InteractiveElementState, - Interactivity, IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton, - Pixels, PlatformInputHandler, Point, Rgba, ShapedLine, Size, StatefulInteractiveElement, - Styled, TextRun, TextStyle, TextSystem, UnderlineStyle, WhiteSpace, WindowContext, + black, div, fill, point, px, red, relative, AnyElement, AsyncWindowContext, BorrowWindow, + Bounds, DispatchPhase, Element, ElementId, ExternalPaths, FocusHandle, Font, FontStyle, + FontWeight, HighlightStyle, Hsla, InteractiveElement, InteractiveElementState, Interactivity, + IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton, Pixels, + PlatformInputHandler, Point, Rgba, ShapedLine, StatefulInteractiveElement, Styled, TextRun, + TextStyle, TextSystem, UnderlineStyle, WhiteSpace, WindowContext, }; use itertools::Itertools; use language::CursorShape; @@ -854,10 +854,8 @@ impl Element for TerminalElement { }); } - if let Some(mut element) = layout.hyperlink_tooltip.take() { - let width: AvailableSpace = bounds.size.width.into(); - let height: AvailableSpace = bounds.size.height.into(); - element.draw(origin, Size { width, height }, cx) + if let Some(element) = layout.hyperlink_tooltip.take() { + element.draw(origin, bounds.size, cx) } }); } From 01947ed730056b3bfa89c3c5e3850838417f7a06 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 21 Dec 2023 15:32:05 +0100 Subject: [PATCH 4/4] Use existing AnyElement::draw --- crates/assistant2/src/assistant_panel.rs | 9 +++++---- crates/gpui2/src/element.rs | 11 ----------- crates/terminal_view2/src/terminal_element.rs | 16 ++++++++-------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/crates/assistant2/src/assistant_panel.rs b/crates/assistant2/src/assistant_panel.rs index 85d3f6f74083afe982ce7bc377426dcb08b45d05..d50fdccd6f5f6dbd2e65e515cbab60a2633f89fb 100644 --- a/crates/assistant2/src/assistant_panel.rs +++ b/crates/assistant2/src/assistant_panel.rs @@ -30,9 +30,9 @@ use fs::Fs; use futures::StreamExt; use gpui::{ canvas, div, point, relative, rems, uniform_list, Action, AnyElement, AppContext, - AsyncWindowContext, ClipboardItem, Context, Div, EventEmitter, FocusHandle, Focusable, - FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, IntoElement, Model, - ModelContext, ParentElement, Pixels, PromptLevel, Render, SharedString, + AsyncWindowContext, AvailableSpace, ClipboardItem, Context, Div, EventEmitter, FocusHandle, + Focusable, FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, + IntoElement, Model, ModelContext, ParentElement, Pixels, PromptLevel, Render, SharedString, StatefulInteractiveElement, Styled, Subscription, Task, TextStyle, UniformListScrollHandle, View, ViewContext, VisualContext, WeakModel, WeakView, WhiteSpace, WindowContext, }; @@ -1199,9 +1199,10 @@ impl Render for AssistantPanel { }, ) .track_scroll(scroll_handle) + .into_any_element() .draw( bounds.origin, - bounds.size, + bounds.size.map(AvailableSpace::Definite), cx, ); }) diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index 3901db73e05c6148b813dc3937dd42843b7bc6b7..4201123a10110f540258b8afc462aeab63b3fb1a 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -23,17 +23,6 @@ pub trait IntoElement: Sized { self.into_element().into_any() } - fn draw(self, origin: Point, available_space: Size, cx: &mut WindowContext) - where - T: Clone + Default + Debug + Into, - { - let element = DrawableElement { - element: Some(self.into_element()), - phase: ElementDrawPhase::Start, - }; - DrawableElement::draw(element, origin, available_space.map(Into::into), cx); - } - fn draw_and_update_state( self, origin: Point, diff --git a/crates/terminal_view2/src/terminal_element.rs b/crates/terminal_view2/src/terminal_element.rs index 3b7b32dd6f609a2dc22668805cfa8adeda27bae0..ec1e1736c31a3ab24911c83f46d2edf734a2b2eb 100644 --- a/crates/terminal_view2/src/terminal_element.rs +++ b/crates/terminal_view2/src/terminal_element.rs @@ -1,11 +1,11 @@ use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; use gpui::{ - black, div, fill, point, px, red, relative, AnyElement, AsyncWindowContext, BorrowWindow, - Bounds, DispatchPhase, Element, ElementId, ExternalPaths, FocusHandle, Font, FontStyle, - FontWeight, HighlightStyle, Hsla, InteractiveElement, InteractiveElementState, Interactivity, - IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton, Pixels, - PlatformInputHandler, Point, Rgba, ShapedLine, StatefulInteractiveElement, Styled, TextRun, - TextStyle, TextSystem, UnderlineStyle, WhiteSpace, WindowContext, + black, div, fill, point, px, red, relative, AnyElement, AsyncWindowContext, AvailableSpace, + BorrowWindow, Bounds, DispatchPhase, Element, ElementId, ExternalPaths, FocusHandle, Font, + FontStyle, FontWeight, HighlightStyle, Hsla, InteractiveElement, InteractiveElementState, + Interactivity, IntoElement, LayoutId, Model, ModelContext, ModifiersChangedEvent, MouseButton, + Pixels, PlatformInputHandler, Point, Rgba, ShapedLine, StatefulInteractiveElement, Styled, + TextRun, TextStyle, TextSystem, UnderlineStyle, WhiteSpace, WindowContext, }; use itertools::Itertools; use language::CursorShape; @@ -854,8 +854,8 @@ impl Element for TerminalElement { }); } - if let Some(element) = layout.hyperlink_tooltip.take() { - element.draw(origin, bounds.size, cx) + if let Some(mut element) = layout.hyperlink_tooltip.take() { + element.draw(origin, bounds.size.map(AvailableSpace::Definite), cx) } }); }