
This PR caps the width of the model label in the selector trigger to a certain size. This is fix the behavior of the popover dancing around, given its popover position is anchored to a certain edge of the trigger, and if the trigger size increases while you select different models with different name lengths, the popover dances around. ### Before https://github.com/user-attachments/assets/0854fa2b-9eb2-45fb-886d-bde1cd644dcf ### After Note how even though the second item has the largest label, the popover stays in place. https://github.com/user-attachments/assets/06b60030-65dc-4f06-b486-3045042bbff0 Fixing that then means truncating the model name to keep it constrained into a max-width. <img width="500" alt="Screenshot 2025-01-27 at 11 38 14 AM" src="https://github.com/user-attachments/assets/94ce9cc6-848c-4dac-86b8-321da75c3af3" /> Release Notes: - N/A
90 lines
3.1 KiB
Rust
90 lines
3.1 KiB
Rust
use assistant_settings::AssistantSettings;
|
|
use fs::Fs;
|
|
use gpui::{Entity, FocusHandle, SharedString};
|
|
use language_model::LanguageModelRegistry;
|
|
use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu};
|
|
use settings::update_settings_file;
|
|
use std::sync::Arc;
|
|
use ui::{prelude::*, ButtonLike, PopoverMenuHandle, Tooltip};
|
|
|
|
use crate::ToggleModelSelector;
|
|
|
|
pub struct AssistantModelSelector {
|
|
selector: Entity<LanguageModelSelector>,
|
|
menu_handle: PopoverMenuHandle<LanguageModelSelector>,
|
|
focus_handle: FocusHandle,
|
|
}
|
|
|
|
impl AssistantModelSelector {
|
|
pub(crate) fn new(
|
|
fs: Arc<dyn Fs>,
|
|
menu_handle: PopoverMenuHandle<LanguageModelSelector>,
|
|
focus_handle: FocusHandle,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) -> Self {
|
|
Self {
|
|
selector: cx.new(|cx| {
|
|
let fs = fs.clone();
|
|
LanguageModelSelector::new(
|
|
move |model, cx| {
|
|
update_settings_file::<AssistantSettings>(
|
|
fs.clone(),
|
|
cx,
|
|
move |settings, _cx| settings.set_model(model.clone()),
|
|
);
|
|
},
|
|
window,
|
|
cx,
|
|
)
|
|
}),
|
|
menu_handle,
|
|
focus_handle,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Render for AssistantModelSelector {
|
|
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let active_model = LanguageModelRegistry::read_global(cx).active_model();
|
|
let focus_handle = self.focus_handle.clone();
|
|
let model_name = match active_model {
|
|
Some(model) => model.name().0,
|
|
_ => SharedString::from("No model selected"),
|
|
};
|
|
|
|
LanguageModelSelectorPopoverMenu::new(
|
|
self.selector.clone(),
|
|
ButtonLike::new("active-model")
|
|
.style(ButtonStyle::Subtle)
|
|
.child(
|
|
h_flex()
|
|
.gap_0p5()
|
|
.child(
|
|
div().max_w_32().child(
|
|
Label::new(model_name)
|
|
.size(LabelSize::Small)
|
|
.color(Color::Muted)
|
|
.text_ellipsis()
|
|
.into_any_element(),
|
|
),
|
|
)
|
|
.child(
|
|
Icon::new(IconName::ChevronDown)
|
|
.color(Color::Muted)
|
|
.size(IconSize::XSmall),
|
|
),
|
|
)
|
|
.tooltip(move |window, cx| {
|
|
Tooltip::for_action_in(
|
|
"Change Model",
|
|
&ToggleModelSelector,
|
|
&focus_handle,
|
|
window,
|
|
cx,
|
|
)
|
|
}),
|
|
)
|
|
.with_handle(self.menu_handle.clone())
|
|
}
|
|
}
|