hyuskui

Model Selector

Two menus in the prompt composer. One picks the model the agent runs, the other picks how hard it thinks.

Loading GPUI…
use gpui::{div, prelude::*, px, Context, Entity, FontWeight, Subscription, Window};

use crate::icons::{icon, ARROW_UP, CHEVRON_DOWN, PLUS};
use crate::theme::ActiveTheme;
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::dropdown_menu::{DropdownMenu, MenuItem};
use crate::ui::input::{InputEvent, TextInput};
use crate::ui::popover::Align;
use crate::ui::textarea::Textarea;

/// Models grouped by provider, as the menu lists them.
const PROVIDERS: [(&str, &[&str]); 2] = [
    ("OpenAI", &["GPT-5", "GPT-5 mini"]),
    ("Anthropic", &["Claude Sonnet 4.5", "Claude Haiku 4.5"]),
];

const EFFORTS: [&str; 3] = ["Low", "Medium", "High"];

/// The menus are 192px wide, like shadcn's `min-w-48`.
const MENU_WIDTH: f32 = 192.0;

pub struct ModelSelectorPattern {
    composer: Option<Entity<TextInput>>,
    _change: Option<Subscription>,
    /// The chosen model's name.
    model: &'static str,
    /// Index into `EFFORTS`.
    effort: usize,
    model_open: bool,
    effort_open: bool,
}

impl Default for ModelSelectorPattern {
    fn default() -> Self {
        Self {
            composer: None,
            _change: None,
            model: "GPT-5",
            effort: 1,
            model_open: false,
            effort_open: false,
        }
    }
}

impl Render for ModelSelectorPattern {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        if self.composer.is_none() {
            let composer = cx.new(|cx| {
                Textarea::new(cx)
                    .multi_line(2)
                    .placeholder("Ask the agent to change your code...")
                    .bare()
            });
            // The send button's state follows the text, so re-render on every edit.
            self._change = Some(cx.subscribe(&composer, |_, _, _: &InputEvent, cx| cx.notify()));
            self.composer = Some(composer);
        }
        let Some(composer) = self.composer.clone() else {
            return div();
        };

        let theme = cx.theme();
        let (fg, muted_fg, muted, border, bg, primary_fg) = (
            theme.foreground,
            theme.muted_foreground,
            theme.muted,
            theme.border,
            theme.background,
            theme.primary_foreground,
        );
        let empty = composer.read(cx).text().trim().is_empty();

        // An outlined pill, like shadcn's `rounded-full` outline button at size sm.
        let pill = move || {
            div()
                .h(px(32.0))
                .flex()
                .items_center()
                .gap_2()
                .px_3()
                .rounded_full()
                .border_1()
                .border_color(border)
                .bg(bg)
                .text_sm()
                .font_weight(FontWeight::MEDIUM)
                .text_color(muted_fg)
                .cursor_pointer()
                .hover(move |style| style.bg(muted).text_color(fg))
        };
        let chevron = move || icon(CHEVRON_DOWN).size(px(16.0)).text_color(muted_fg);

        let mut model_menu = DropdownMenu::new("model-menu", self.model_open)
            .align(Align::Start)
            .width(px(MENU_WIDTH))
            .trigger(pill().child(self.model).child(chevron()))
            .on_open_change(cx.listener(|this, open: &bool, _, cx| {
                this.model_open = *open;
                cx.notify();
            }));
        for (ix, (provider, models)) in PROVIDERS.into_iter().enumerate() {
            if ix > 0 {
                model_menu = model_menu.separator();
            }
            model_menu = model_menu.label(provider);
            for &model in models {
                model_menu = model_menu.item(
                    MenuItem::new(model)
                        .radio(self.model == model)
                        .on_select(cx.listener(move |this, _, _, cx| {
                            this.model = model;
                            cx.notify();
                        })),
                );
            }
        }

        let mut effort_menu = DropdownMenu::new("effort-menu", self.effort_open)
            .align(Align::Start)
            .width(px(MENU_WIDTH))
            .trigger(
                pill()
                    .child(div().text_color(muted_fg).child("Effort"))
                    .child(div().text_color(fg).child(EFFORTS[self.effort]))
                    .child(chevron()),
            )
            .on_open_change(cx.listener(|this, open: &bool, _, cx| {
                this.effort_open = *open;
                cx.notify();
            }))
            .label("Effort");
        for (ix, effort) in EFFORTS.into_iter().enumerate() {
            effort_menu = effort_menu.item(
                MenuItem::new(effort)
                    .radio(self.effort == ix)
                    .on_select(cx.listener(move |this, _, _, cx| {
                        this.effort = ix;
                        cx.notify();
                    })),
            );
        }

        let attach = Button::new("attach", "")
            .variant(ButtonVariant::Outline)
            .size(ButtonSize::Sm)
            .icon_only()
            .rounded_full()
            .leading(icon(PLUS).size(px(16.0)).text_color(muted_fg));
        let send = Button::new("send", "")
            .size(ButtonSize::Sm)
            .icon_only()
            .rounded_full()
            .disabled(empty)
            .leading(icon(ARROW_UP).size(px(16.0)).text_color(primary_fg))
            .on_click(cx.listener(|this, _, window, cx| {
                if let Some(composer) = &this.composer {
                    composer.update(cx, |input, cx| input.set_text("", window, cx));
                }
            }));

        // Held near the top so both menus have room to open below it.
        div().w(px(512.0)).h(px(330.0)).child(
            div()
                .w_full()
                .flex()
                .flex_col()
                .rounded(px(18.0))
                .border_1()
                .border_color(gpui::transparent_black())
                .bg(theme.input.opacity(0.5))
                .child(div().min_h(px(64.0)).p_3().child(composer))
                .child(
                    div()
                        .flex()
                        .items_center()
                        .gap_2()
                        .px(px(10.0))
                        .pb_2()
                        .child(attach)
                        .child(div().flex_1())
                        .child(model_menu)
                        .child(effort_menu)
                        .child(send),
                ),
        )
    }
}

Installation

  1. Add the components

    $ cargo hyuskui add button dropdown-menu input popover textarea icons

    Uses Button, Dropdown Menu, Input, Popover and Textarea.

  2. Copy the pattern

    Paste the code into src/ai/model_selector.rs and declare the module. The demo plays scripted data on a timer; drive it from your agent's events instead.

    src/ai/model_selector.rs
    use gpui::{div, prelude::*, px, Context, Entity, FontWeight, Subscription, Window};
    
    use crate::icons::{icon, ARROW_UP, CHEVRON_DOWN, PLUS};
    use crate::theme::ActiveTheme;
    use crate::ui::button::{Button, ButtonSize, ButtonVariant};
    use crate::ui::dropdown_menu::{DropdownMenu, MenuItem};
    use crate::ui::input::{InputEvent, TextInput};
    use crate::ui::popover::Align;
    use crate::ui::textarea::Textarea;
    
    /// Models grouped by provider, as the menu lists them.
    const PROVIDERS: [(&str, &[&str]); 2] = [
        ("OpenAI", &["GPT-5", "GPT-5 mini"]),
        ("Anthropic", &["Claude Sonnet 4.5", "Claude Haiku 4.5"]),
    ];
    
    const EFFORTS: [&str; 3] = ["Low", "Medium", "High"];
    
    /// The menus are 192px wide, like shadcn's `min-w-48`.
    const MENU_WIDTH: f32 = 192.0;
    
    pub struct ModelSelectorPattern {
        composer: Option<Entity<TextInput>>,
        _change: Option<Subscription>,
        /// The chosen model's name.
        model: &'static str,
        /// Index into `EFFORTS`.
        effort: usize,
        model_open: bool,
        effort_open: bool,
    }
    
    impl Default for ModelSelectorPattern {
        fn default() -> Self {
            Self {
                composer: None,
                _change: None,
                model: "GPT-5",
                effort: 1,
                model_open: false,
                effort_open: false,
            }
        }
    }
    
    impl Render for ModelSelectorPattern {
        fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
            if self.composer.is_none() {
                let composer = cx.new(|cx| {
                    Textarea::new(cx)
                        .multi_line(2)
                        .placeholder("Ask the agent to change your code...")
                        .bare()
                });
                // The send button's state follows the text, so re-render on every edit.
                self._change = Some(cx.subscribe(&composer, |_, _, _: &InputEvent, cx| cx.notify()));
                self.composer = Some(composer);
            }
            let Some(composer) = self.composer.clone() else {
                return div();
            };
    
            let theme = cx.theme();
            let (fg, muted_fg, muted, border, bg, primary_fg) = (
                theme.foreground,
                theme.muted_foreground,
                theme.muted,
                theme.border,
                theme.background,
                theme.primary_foreground,
            );
            let empty = composer.read(cx).text().trim().is_empty();
    
            // An outlined pill, like shadcn's `rounded-full` outline button at size sm.
            let pill = move || {
                div()
                    .h(px(32.0))
                    .flex()
                    .items_center()
                    .gap_2()
                    .px_3()
                    .rounded_full()
                    .border_1()
                    .border_color(border)
                    .bg(bg)
                    .text_sm()
                    .font_weight(FontWeight::MEDIUM)
                    .text_color(muted_fg)
                    .cursor_pointer()
                    .hover(move |style| style.bg(muted).text_color(fg))
            };
            let chevron = move || icon(CHEVRON_DOWN).size(px(16.0)).text_color(muted_fg);
    
            let mut model_menu = DropdownMenu::new("model-menu", self.model_open)
                .align(Align::Start)
                .width(px(MENU_WIDTH))
                .trigger(pill().child(self.model).child(chevron()))
                .on_open_change(cx.listener(|this, open: &bool, _, cx| {
                    this.model_open = *open;
                    cx.notify();
                }));
            for (ix, (provider, models)) in PROVIDERS.into_iter().enumerate() {
                if ix > 0 {
                    model_menu = model_menu.separator();
                }
                model_menu = model_menu.label(provider);
                for &model in models {
                    model_menu = model_menu.item(
                        MenuItem::new(model)
                            .radio(self.model == model)
                            .on_select(cx.listener(move |this, _, _, cx| {
                                this.model = model;
                                cx.notify();
                            })),
                    );
                }
            }
    
            let mut effort_menu = DropdownMenu::new("effort-menu", self.effort_open)
                .align(Align::Start)
                .width(px(MENU_WIDTH))
                .trigger(
                    pill()
                        .child(div().text_color(muted_fg).child("Effort"))
                        .child(div().text_color(fg).child(EFFORTS[self.effort]))
                        .child(chevron()),
                )
                .on_open_change(cx.listener(|this, open: &bool, _, cx| {
                    this.effort_open = *open;
                    cx.notify();
                }))
                .label("Effort");
            for (ix, effort) in EFFORTS.into_iter().enumerate() {
                effort_menu = effort_menu.item(
                    MenuItem::new(effort)
                        .radio(self.effort == ix)
                        .on_select(cx.listener(move |this, _, _, cx| {
                            this.effort = ix;
                            cx.notify();
                        })),
                );
            }
    
            let attach = Button::new("attach", "")
                .variant(ButtonVariant::Outline)
                .size(ButtonSize::Sm)
                .icon_only()
                .rounded_full()
                .leading(icon(PLUS).size(px(16.0)).text_color(muted_fg));
            let send = Button::new("send", "")
                .size(ButtonSize::Sm)
                .icon_only()
                .rounded_full()
                .disabled(empty)
                .leading(icon(ARROW_UP).size(px(16.0)).text_color(primary_fg))
                .on_click(cx.listener(|this, _, window, cx| {
                    if let Some(composer) = &this.composer {
                        composer.update(cx, |input, cx| input.set_text("", window, cx));
                    }
                }));
    
            // Held near the top so both menus have room to open below it.
            div().w(px(512.0)).h(px(330.0)).child(
                div()
                    .w_full()
                    .flex()
                    .flex_col()
                    .rounded(px(18.0))
                    .border_1()
                    .border_color(gpui::transparent_black())
                    .bg(theme.input.opacity(0.5))
                    .child(div().min_h(px(64.0)).p_3().child(composer))
                    .child(
                        div()
                            .flex()
                            .items_center()
                            .gap_2()
                            .px(px(10.0))
                            .pb_2()
                            .child(attach)
                            .child(div().flex_1())
                            .child(model_menu)
                            .child(effort_menu)
                            .child(send),
                    ),
            )
        }
    }