hyuskui

Prompt Input

A message field with attachments and a send button.

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

use crate::icons::{icon, ARROW_UP, CHEVRON_DOWN, FILE_TEXT, PLUS, X};
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"];

/// Stand-ins for the files a picker would return; + attaches the next one.
const FILES: [&str; 4] = ["brief.pdf", "schema.ts", "mockup.png", "notes.md"];

/// One attached file.
struct File {
    /// Never reused, so each chip keeps its own element state.
    id: usize,
    name: &'static str,
}

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

impl Default for PromptInputPattern {
    fn default() -> Self {
        Self {
            composer: None,
            _change: None,
            files: Vec::new(),
            next_id: 0,
            model: "GPT-5",
            effort: 1,
            model_open: false,
            effort_open: false,
        }
    }
}

impl PromptInputPattern {
    fn attach(&mut self, cx: &mut Context<Self>) {
        let name = FILES[self.next_id % FILES.len()];
        self.files.push(File { id: self.next_id, name });
        self.next_id += 1;
        cx.notify();
    }

    /// Sending clears the text and the attachments, like the form's `onSubmit`.
    fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let Some(composer) = self.composer.clone() else {
            return;
        };
        if composer.read(cx).text().trim().is_empty() {
            return;
        }
        composer.update(cx, |input, cx| input.set_text("", window, cx));
        self.files.clear();
        cx.notify();
    }
}

impl Render for PromptInputPattern {
    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(1)
                    .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 can_send = !composer.read(cx).text().trim().is_empty();

        // shadcn's outline `InputGroupButton` at size sm: muted text that brightens on hover
        // and while its menu is open.
        let pill = move |open: bool| {
            let color: Hsla = if open { fg } else { muted_fg };
            div()
                .h(px(32.0))
                .flex()
                .items_center()
                .gap_2()
                .px_3()
                .rounded_full()
                .border_1()
                .border_color(border)
                .bg(if open { muted } else { bg })
                .text_sm()
                .font_weight(FontWeight::MEDIUM)
                .text_color(color)
                .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(192.0))
            .trigger(pill(self.model_open).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(144.0))
            .trigger(
                pill(self.effort_open)
                    .child(div().text_color(muted_fg).child("Effort"))
                    .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();
                },
            )));
        }

        // shadcn's `Attachment size="sm"`: a 160px card with a 32px file tile and a remove button.
        let chips = self.files.iter().map(|file| {
            let id = file.id;
            div()
                .flex()
                .items_center()
                .gap(px(10.0))
                .min_w(px(160.0))
                .p(px(6.0))
                .rounded(px(18.0))
                .border_1()
                .border_color(border)
                .bg(bg)
                .text_xs()
                .font_weight(FontWeight::MEDIUM)
                .text_color(fg)
                .child(
                    div()
                        .flex()
                        .flex_none()
                        .items_center()
                        .justify_center()
                        .size(px(32.0))
                        .rounded(px(10.0))
                        .bg(muted)
                        .child(icon(FILE_TEXT).size(px(16.0)).text_color(fg)),
                )
                .child(div().flex_1().min_w_0().truncate().line_height(px(15.0)).child(file.name))
                .child(
                    div()
                        .id(("remove", id))
                        .flex()
                        .flex_none()
                        .items_center()
                        .justify_center()
                        .size(px(24.0))
                        .rounded_full()
                        .cursor_pointer()
                        .hover(move |style| style.bg(muted))
                        .child(icon(X).size(px(16.0)).text_color(fg))
                        .on_click(cx.listener(move |this, _, _, cx| {
                            this.files.retain(|file| file.id != id);
                            cx.notify();
                        })),
                )
        });
        let has_files = !self.files.is_empty();

        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))
            .on_click(cx.listener(|this, _, _, cx| this.attach(cx)));
        let send = Button::new("send", "")
            .size(ButtonSize::Sm)
            .icon_only()
            .rounded_full()
            .disabled(!can_send)
            .leading(icon(ARROW_UP).size(px(16.0)).text_color(primary_fg))
            .on_click(cx.listener(|this, _, window, cx| this.submit(window, cx)));

        div().w(px(512.0)).py_6().child(
            // shadcn's prompt `InputGroup`: input/50 fill, 18px corners, addons above and below.
            div()
                .w_full()
                .flex()
                .flex_col()
                .rounded(px(18.0))
                .border_1()
                .border_color(gpui::transparent_black())
                .bg(theme.input.opacity(0.5))
                // Enter sends and Shift+Enter breaks the line, like the form's `onKeyDown`.
                .capture_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| {
                    let keystroke = &event.keystroke;
                    if keystroke.key == "enter" && !keystroke.modifiers.shift {
                        cx.stop_propagation();
                        this.submit(window, cx);
                    }
                }))
                .when(has_files, |this| {
                    this.child(div().flex().flex_wrap().gap_2().px(px(10.0)).pt_2().pb(px(6.0)).children(chips))
                })
                .child(div().min_h(px(64.0)).p_3().child(composer))
                .child(
                    div()
                        .flex()
                        .items_center()
                        .gap_2()
                        .px(px(10.0))
                        .pt(px(6.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/prompt_input.rs and declare the module. The demo plays scripted data on a timer; drive it from your agent's events instead.

    src/ai/prompt_input.rs
    use gpui::{div, prelude::*, px, Context, Entity, FontWeight, Hsla, KeyDownEvent, Subscription, Window};
    
    use crate::icons::{icon, ARROW_UP, CHEVRON_DOWN, FILE_TEXT, PLUS, X};
    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"];
    
    /// Stand-ins for the files a picker would return; + attaches the next one.
    const FILES: [&str; 4] = ["brief.pdf", "schema.ts", "mockup.png", "notes.md"];
    
    /// One attached file.
    struct File {
        /// Never reused, so each chip keeps its own element state.
        id: usize,
        name: &'static str,
    }
    
    pub struct PromptInputPattern {
        composer: Option<Entity<TextInput>>,
        _change: Option<Subscription>,
        files: Vec<File>,
        next_id: usize,
        /// The chosen model's name.
        model: &'static str,
        /// Index into `EFFORTS`.
        effort: usize,
        model_open: bool,
        effort_open: bool,
    }
    
    impl Default for PromptInputPattern {
        fn default() -> Self {
            Self {
                composer: None,
                _change: None,
                files: Vec::new(),
                next_id: 0,
                model: "GPT-5",
                effort: 1,
                model_open: false,
                effort_open: false,
            }
        }
    }
    
    impl PromptInputPattern {
        fn attach(&mut self, cx: &mut Context<Self>) {
            let name = FILES[self.next_id % FILES.len()];
            self.files.push(File { id: self.next_id, name });
            self.next_id += 1;
            cx.notify();
        }
    
        /// Sending clears the text and the attachments, like the form's `onSubmit`.
        fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
            let Some(composer) = self.composer.clone() else {
                return;
            };
            if composer.read(cx).text().trim().is_empty() {
                return;
            }
            composer.update(cx, |input, cx| input.set_text("", window, cx));
            self.files.clear();
            cx.notify();
        }
    }
    
    impl Render for PromptInputPattern {
        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(1)
                        .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 can_send = !composer.read(cx).text().trim().is_empty();
    
            // shadcn's outline `InputGroupButton` at size sm: muted text that brightens on hover
            // and while its menu is open.
            let pill = move |open: bool| {
                let color: Hsla = if open { fg } else { muted_fg };
                div()
                    .h(px(32.0))
                    .flex()
                    .items_center()
                    .gap_2()
                    .px_3()
                    .rounded_full()
                    .border_1()
                    .border_color(border)
                    .bg(if open { muted } else { bg })
                    .text_sm()
                    .font_weight(FontWeight::MEDIUM)
                    .text_color(color)
                    .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(192.0))
                .trigger(pill(self.model_open).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(144.0))
                .trigger(
                    pill(self.effort_open)
                        .child(div().text_color(muted_fg).child("Effort"))
                        .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();
                    },
                )));
            }
    
            // shadcn's `Attachment size="sm"`: a 160px card with a 32px file tile and a remove button.
            let chips = self.files.iter().map(|file| {
                let id = file.id;
                div()
                    .flex()
                    .items_center()
                    .gap(px(10.0))
                    .min_w(px(160.0))
                    .p(px(6.0))
                    .rounded(px(18.0))
                    .border_1()
                    .border_color(border)
                    .bg(bg)
                    .text_xs()
                    .font_weight(FontWeight::MEDIUM)
                    .text_color(fg)
                    .child(
                        div()
                            .flex()
                            .flex_none()
                            .items_center()
                            .justify_center()
                            .size(px(32.0))
                            .rounded(px(10.0))
                            .bg(muted)
                            .child(icon(FILE_TEXT).size(px(16.0)).text_color(fg)),
                    )
                    .child(div().flex_1().min_w_0().truncate().line_height(px(15.0)).child(file.name))
                    .child(
                        div()
                            .id(("remove", id))
                            .flex()
                            .flex_none()
                            .items_center()
                            .justify_center()
                            .size(px(24.0))
                            .rounded_full()
                            .cursor_pointer()
                            .hover(move |style| style.bg(muted))
                            .child(icon(X).size(px(16.0)).text_color(fg))
                            .on_click(cx.listener(move |this, _, _, cx| {
                                this.files.retain(|file| file.id != id);
                                cx.notify();
                            })),
                    )
            });
            let has_files = !self.files.is_empty();
    
            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))
                .on_click(cx.listener(|this, _, _, cx| this.attach(cx)));
            let send = Button::new("send", "")
                .size(ButtonSize::Sm)
                .icon_only()
                .rounded_full()
                .disabled(!can_send)
                .leading(icon(ARROW_UP).size(px(16.0)).text_color(primary_fg))
                .on_click(cx.listener(|this, _, window, cx| this.submit(window, cx)));
    
            div().w(px(512.0)).py_6().child(
                // shadcn's prompt `InputGroup`: input/50 fill, 18px corners, addons above and below.
                div()
                    .w_full()
                    .flex()
                    .flex_col()
                    .rounded(px(18.0))
                    .border_1()
                    .border_color(gpui::transparent_black())
                    .bg(theme.input.opacity(0.5))
                    // Enter sends and Shift+Enter breaks the line, like the form's `onKeyDown`.
                    .capture_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| {
                        let keystroke = &event.keystroke;
                        if keystroke.key == "enter" && !keystroke.modifiers.shift {
                            cx.stop_propagation();
                            this.submit(window, cx);
                        }
                    }))
                    .when(has_files, |this| {
                        this.child(div().flex().flex_wrap().gap_2().px(px(10.0)).pt_2().pb(px(6.0)).children(chips))
                    })
                    .child(div().min_h(px(64.0)).p_3().child(composer))
                    .child(
                        div()
                            .flex()
                            .items_center()
                            .gap_2()
                            .px(px(10.0))
                            .pt(px(6.0))
                            .pb_2()
                            .child(attach)
                            .child(div().flex_1())
                            .child(model_menu)
                            .child(effort_menu)
                            .child(send),
                    ),
            )
        }
    }