Skip to content

Commit

Permalink
assistant: Preserve selection focus in the model selector (#23713)
Browse files Browse the repository at this point in the history
This PR fixes an incorrect behavior in the model selector where we were
removing the focus from the selected item back to the very first item of
the list. Now, if you click/hit return on an item, focus is preserved
there. In other words, focus is always initially in the selected item.

### Before

https://github.com/user-attachments/assets/62b72b1f-4e32-4b4a-adff-dcf9a2c13a28

### After

https://github.com/user-attachments/assets/a8528933-da01-481a-96f3-0173a39a03c0

Release Notes:

- N/A
  • Loading branch information
danilo-leal authored Jan 27, 2025
1 parent 93b62e0 commit 98ea058
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions crates/language_model_selector/src/language_model_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl PickerDelegate for LanguageModelPickerDelegate {
cx: &mut Context<Picker<Self>>,
) -> Task<()> {
let all_models = self.all_models.clone();
let current_index = self.selected_index;

let llm_registry = LanguageModelRegistry::global(cx);

Expand Down Expand Up @@ -243,18 +244,27 @@ impl PickerDelegate for LanguageModelPickerDelegate {

this.update_in(&mut cx, |this, window, cx| {
this.delegate.filtered_models = filtered_models;
this.delegate.set_selected_index(0, window, cx);
// Preserve selection focus
let new_index = if current_index >= this.delegate.filtered_models.len() {
0
} else {
current_index
};
this.delegate.set_selected_index(new_index, window, cx);
cx.notify();
})
.ok();
})
}

fn confirm(&mut self, _secondary: bool, _: &mut Window, cx: &mut Context<Picker<Self>>) {
fn confirm(&mut self, _secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
if let Some(model_info) = self.filtered_models.get(self.selected_index) {
let model = model_info.model.clone();
(self.on_model_changed)(model.clone(), cx);

let current_index = self.selected_index;
self.set_selected_index(current_index, window, cx);

cx.emit(DismissEvent);
}
}
Expand Down

0 comments on commit 98ea058

Please sign in to comment.