Improve model selection in the assistant (#12472)

https://github.com/zed-industries/zed/assets/482957/3b017850-b7b6-457a-9b2f-324d5533442e


Release Notes:

- Improved the UX for selecting a model in the assistant panel. You can
now switch model using just the keyboard by pressing `alt-m`. Also, when
switching models via the UI, settings will now be updated automatically.
This commit is contained in:
Antonio Scandurra 2024-05-30 12:36:07 +02:00 committed by GitHub
parent 5a149b970c
commit 6ff01b17ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 517 additions and 295 deletions

View file

@ -13,6 +13,51 @@ pub trait PopoverTrigger: IntoElement + Clickable + Selectable + 'static {}
impl<T: IntoElement + Clickable + Selectable + 'static> PopoverTrigger for T {}
pub struct PopoverMenuHandle<M>(Rc<RefCell<Option<PopoverMenuHandleState<M>>>>);
impl<M> Clone for PopoverMenuHandle<M> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<M> Default for PopoverMenuHandle<M> {
fn default() -> Self {
Self(Rc::default())
}
}
struct PopoverMenuHandleState<M> {
menu_builder: Rc<dyn Fn(&mut WindowContext) -> Option<View<M>>>,
menu: Rc<RefCell<Option<View<M>>>>,
}
impl<M: ManagedView> PopoverMenuHandle<M> {
pub fn show(&self, cx: &mut WindowContext) {
if let Some(state) = self.0.borrow().as_ref() {
show_menu(&state.menu_builder, &state.menu, cx);
}
}
pub fn hide(&self, cx: &mut WindowContext) {
if let Some(state) = self.0.borrow().as_ref() {
if let Some(menu) = state.menu.borrow().as_ref() {
menu.update(cx, |_, cx| cx.emit(DismissEvent));
}
}
}
pub fn toggle(&self, cx: &mut WindowContext) {
if let Some(state) = self.0.borrow().as_ref() {
if state.menu.borrow().is_some() {
self.hide(cx);
} else {
self.show(cx);
}
}
}
}
pub struct PopoverMenu<M: ManagedView> {
id: ElementId,
child_builder: Option<
@ -28,6 +73,7 @@ pub struct PopoverMenu<M: ManagedView> {
anchor: AnchorCorner,
attach: Option<AnchorCorner>,
offset: Option<Point<Pixels>>,
trigger_handle: Option<PopoverMenuHandle<M>>,
}
impl<M: ManagedView> PopoverMenu<M> {
@ -36,35 +82,17 @@ impl<M: ManagedView> PopoverMenu<M> {
self
}
pub fn with_handle(mut self, handle: PopoverMenuHandle<M>) -> Self {
self.trigger_handle = Some(handle);
self
}
pub fn trigger<T: PopoverTrigger>(mut self, t: T) -> Self {
self.child_builder = Some(Box::new(|menu, builder| {
let open = menu.borrow().is_some();
t.selected(open)
.when_some(builder, |el, builder| {
el.on_click({
move |_, cx| {
let Some(new_menu) = (builder)(cx) else {
return;
};
let menu2 = menu.clone();
let previous_focus_handle = cx.focused();
cx.subscribe(&new_menu, move |modal, _: &DismissEvent, cx| {
if modal.focus_handle(cx).contains_focused(cx) {
if let Some(previous_focus_handle) =
previous_focus_handle.as_ref()
{
cx.focus(previous_focus_handle);
}
}
*menu2.borrow_mut() = None;
cx.refresh();
})
.detach();
cx.focus_view(&new_menu);
*menu.borrow_mut() = Some(new_menu);
}
})
el.on_click(move |_, cx| show_menu(&builder, &menu, cx))
})
.into_any_element()
}));
@ -111,6 +139,32 @@ impl<M: ManagedView> PopoverMenu<M> {
}
}
fn show_menu<M: ManagedView>(
builder: &Rc<dyn Fn(&mut WindowContext) -> Option<View<M>>>,
menu: &Rc<RefCell<Option<View<M>>>>,
cx: &mut WindowContext,
) {
let Some(new_menu) = (builder)(cx) else {
return;
};
let menu2 = menu.clone();
let previous_focus_handle = cx.focused();
cx.subscribe(&new_menu, move |modal, _: &DismissEvent, cx| {
if modal.focus_handle(cx).contains_focused(cx) {
if let Some(previous_focus_handle) = previous_focus_handle.as_ref() {
cx.focus(previous_focus_handle);
}
}
*menu2.borrow_mut() = None;
cx.refresh();
})
.detach();
cx.focus_view(&new_menu);
*menu.borrow_mut() = Some(new_menu);
cx.refresh();
}
/// Creates a [`PopoverMenu`]
pub fn popover_menu<M: ManagedView>(id: impl Into<ElementId>) -> PopoverMenu<M> {
PopoverMenu {
@ -120,6 +174,7 @@ pub fn popover_menu<M: ManagedView>(id: impl Into<ElementId>) -> PopoverMenu<M>
anchor: AnchorCorner::TopLeft,
attach: None,
offset: None,
trigger_handle: None,
}
}
@ -190,6 +245,15 @@ impl<M: ManagedView> Element for PopoverMenu<M> {
(child_builder)(element_state.menu.clone(), self.menu_builder.clone())
});
if let Some(trigger_handle) = self.trigger_handle.take() {
if let Some(menu_builder) = self.menu_builder.clone() {
*trigger_handle.0.borrow_mut() = Some(PopoverMenuHandleState {
menu_builder,
menu: element_state.menu.clone(),
});
}
}
let child_layout_id = child_element
.as_mut()
.map(|child_element| child_element.request_layout(cx));