Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent_ui: Fix elicitation radio indicator alignment (#63885)

itsfuad7362739f944b
diff --git a/crates/agent_ui/src/conversation_view/elicitation.rs b/crates/agent_ui/src/conversation_view/elicitation.rs
index 872eafbb4e6..f98dc34fd26 100644
--- a/crates/agent_ui/src/conversation_view/elicitation.rs
+++ b/crates/agent_ui/src/conversation_view/elicitation.rs
@@ -4,11 +4,11 @@ use collections::{HashMap, HashSet};
use component::{Component, ComponentScope, example_group_with_title, single_example};
use editor::Editor;
use futures::channel::oneshot;
-use gpui::{AnyElement, App, Div, Empty, Entity, Focusable, Hsla, SharedString, Window, div};
+use gpui::{AnyElement, App, Div, Empty, Entity, Focusable, Role, SharedString, Window, div};
use std::collections::BTreeMap;
use std::rc::Rc;
use ui::{
- Button, Checkbox, Color, Icon, IconName, IconSize, Indicator, Label, LabelSize, ToggleState,
+ Button, Checkbox, ChoiceCard, Color, Icon, IconName, IconSize, Label, LabelSize, ToggleState,
prelude::*,
};
@@ -1990,6 +1990,7 @@ impl<'a> ElicitationCard<'a> {
.into_any_element();
}
+ let group_label = label.clone();
let label = if error.is_some() {
Label::new(label).size(LabelSize::Small).color(Color::Error)
} else {
@@ -2040,10 +2041,10 @@ impl<'a> ElicitationCard<'a> {
};
self.render_single_select(
field_name,
+ group_label,
value.as_ref(),
options,
error.is_some(),
- cx,
)
}
ElicitationFieldState::MultiSelect(selected) => {
@@ -2054,44 +2055,26 @@ impl<'a> ElicitationCard<'a> {
_ => Vec::new(),
};
v_flex()
+ .id(format!("elicitation-multi-{}-{field_name}", self.entry_ix))
+ .role(Role::Group)
+ .aria_label(group_label)
.gap_1()
.children(options.into_iter().map(|option| {
let is_selected = selected.contains(&option.value);
- let checkbox_state = if is_selected {
- ToggleState::Selected
- } else {
- ToggleState::Unselected
- };
- let row_background = Self::option_row_background(is_selected, cx);
- let hover_background =
- Self::option_row_hover_background(is_selected, cx);
let on_multi_select_change =
self.handlers.on_multi_select_change.clone();
let elicitation_id = self.elicitation.id.clone();
let field_name = field_name.to_string();
- let value = option.value.clone();
- let checkbox_id = format!(
- "elicitation-multi-{}-{field_name}-{}",
+ let card_id = format!(
+ "elicitation-multi-option-{}-{field_name}-{}",
self.entry_ix, option.value
);
- h_flex()
- .id(SharedString::from(format!(
- "elicitation-multi-option-{}-{field_name}-{}",
- self.entry_ix, option.value
- )))
- .tab_index(0)
- .w_full()
- .min_h(rems_from_px(28_f32))
- .items_start()
- .gap_1p5()
- .rounded_sm()
- .border_1()
- .border_color(field_border_color.opacity(0.5))
- .bg(row_background)
- .px_2()
- .py_1()
- .hover(move |this| this.bg(hover_background).cursor_pointer())
- .focus_visible(|this| this.border_color(focused_border_color))
+ let value = option.value;
+ ChoiceCard::checkbox(card_id, option.label, is_selected)
+ .when_some(option.description, |this, description| {
+ this.description(description)
+ })
+ .invalid(error.is_some())
.on_click(move |_, _window, cx| {
on_multi_select_change(
elicitation_id.clone(),
@@ -2101,8 +2084,6 @@ impl<'a> ElicitationCard<'a> {
cx,
);
})
- .child(div().child(Checkbox::new(checkbox_id, checkbox_state)))
- .child(Self::render_option_content(option))
}))
.into_any_element()
}
@@ -2116,51 +2097,38 @@ impl<'a> ElicitationCard<'a> {
fn render_single_select(
&self,
field_name: &str,
+ group_label: SharedString,
selected_value: Option<&String>,
options: Vec<ElicitationOption>,
has_error: bool,
- cx: &App,
) -> AnyElement {
let entry_ix = self.entry_ix;
- let border_color = if has_error {
- Color::Error.color(cx)
- } else {
- cx.theme().colors().border.opacity(0.8)
- };
let elicitation_id = self.elicitation.id.clone();
let field_name = field_name.to_string();
let on_single_select_change = self.handlers.on_single_select_change.clone();
v_flex()
+ .id(format!("elicitation-select-{entry_ix}-{field_name}"))
+ .role(Role::RadioGroup)
+ .aria_label(group_label)
.gap_1()
- .children(options.into_iter().map(move |option| {
- let option_value = option.value.clone();
- let option_id =
- format!("elicitation-select-option-{entry_ix}-{field_name}-{option_value}");
+ .children(options.into_iter().map(|option| {
+ let card_id = format!(
+ "elicitation-select-option-{entry_ix}-{field_name}-{}",
+ option.value
+ );
let is_selected =
selected_value.is_some_and(|selected_value| selected_value == &option.value);
- let row_background = Self::option_row_background(is_selected, cx);
- let hover_background = Self::option_row_hover_background(is_selected, cx);
- let control_background = Self::option_control_background(cx);
+ let option_value = option.value;
let elicitation_id = elicitation_id.clone();
let field_name = field_name.clone();
let on_single_select_change = on_single_select_change.clone();
- h_flex()
- .id(option_id)
- .tab_index(0)
- .w_full()
- .min_h(rems_from_px(28_f32))
- .items_start()
- .gap_1p5()
- .rounded_sm()
- .border_1()
- .border_color(border_color.opacity(0.5))
- .bg(row_background)
- .px_2()
- .py_1()
- .hover(move |this| this.bg(hover_background).cursor_pointer())
- .focus_visible(|this| this.border_color(cx.theme().colors().border_focused))
+ ChoiceCard::radio(card_id, option.label, is_selected)
+ .when_some(option.description, |this, description| {
+ this.description(description)
+ })
+ .invalid(has_error)
.on_click(move |_, _window, cx| {
on_single_select_change(
elicitation_id.clone(),
@@ -2169,79 +2137,10 @@ impl<'a> ElicitationCard<'a> {
cx,
);
})
- .child(
- div()
- .size(Checkbox::container_size())
- .flex_none()
- .flex()
- .items_center()
- .justify_center()
- .child(Self::render_radio_indicator(
- is_selected,
- border_color,
- control_background,
- )),
- )
- .child(Self::render_option_content(option))
}))
.into_any_element()
}
- fn render_option_content(option: ElicitationOption) -> Div {
- v_flex()
- .min_w_0()
- .flex_1()
- .gap_0p5()
- .child(Label::new(option.label).size(LabelSize::Small))
- .when_some(option.description, |this, description| {
- this.child(
- Label::new(description)
- .size(LabelSize::Small)
- .color(Color::Muted),
- )
- })
- }
-
- fn option_row_background(is_selected: bool, cx: &App) -> Hsla {
- let editor_background = cx.theme().colors().editor_background;
- if is_selected {
- editor_background.blend(Color::Accent.color(cx).opacity(0.08))
- } else {
- editor_background
- }
- }
-
- fn option_row_hover_background(is_selected: bool, cx: &App) -> Hsla {
- let editor_background = cx.theme().colors().editor_background;
- if is_selected {
- editor_background.blend(Color::Accent.color(cx).opacity(0.1))
- } else {
- cx.theme()
- .colors()
- .element_background
- .blend(cx.theme().colors().editor_foreground.opacity(0.025))
- }
- }
-
- fn option_control_background(cx: &App) -> Hsla {
- cx.theme().colors().editor_background
- }
-
- fn render_radio_indicator(is_selected: bool, border_color: Hsla, background: Hsla) -> Div {
- div()
- .size_3()
- .flex()
- .items_center()
- .justify_center()
- .rounded_full()
- .border_1()
- .border_color(border_color)
- .bg(background)
- .when(is_selected, |this| {
- this.child(Indicator::dot().color(Color::Accent))
- })
- }
-
fn render_url_elicitation(&self, mode: &acp::ElicitationUrlMode) -> AnyElement {
v_flex()
.gap_2()
diff --git a/crates/ui/src/components.rs b/crates/ui/src/components.rs
index 12d5946627e..be1363fec7a 100644
--- a/crates/ui/src/components.rs
+++ b/crates/ui/src/components.rs
@@ -4,6 +4,7 @@ mod banner;
mod button;
mod callout;
mod chip;
+mod choice_card;
mod collab;
mod context_menu;
mod count_badge;
@@ -47,6 +48,7 @@ pub use banner::*;
pub use button::*;
pub use callout::*;
pub use chip::*;
+pub use choice_card::*;
pub use collab::*;
pub use context_menu::*;
pub use count_badge::*;
diff --git a/crates/ui/src/components/choice_card.rs b/crates/ui/src/components/choice_card.rs
new file mode 100644
index 00000000000..c3a1c402c5a
--- /dev/null
+++ b/crates/ui/src/components/choice_card.rs
@@ -0,0 +1,337 @@
+use gpui::{BorderStyle, ClickEvent, Corners, Edges, Hsla, Role, Toggled, canvas, quad};
+
+use crate::{Checkbox, ToggleState, prelude::*};
+
+enum ChoiceCardKind {
+ Radio,
+ Checkbox,
+}
+
+/// A full-width, clickable card representing a single choice, with a leading radio or checkbox
+/// indicator, a label, and an optional description.
+///
+/// The card owns its focus, click handling, and accessibility semantics. Place related cards in
+/// a container with the `RadioGroup` (for radio cards) or `Group` role so assistive technology
+/// announces them together.
+#[derive(IntoElement, RegisterComponent)]
+pub struct ChoiceCard {
+ id: ElementId,
+ kind: ChoiceCardKind,
+ label: SharedString,
+ description: Option<SharedString>,
+ is_selected: bool,
+ invalid: bool,
+ on_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
+}
+
+impl ChoiceCard {
+ /// A card for one of several mutually exclusive choices, with a radio indicator.
+ /// Place sibling cards in a `Role::RadioGroup` container.
+ pub fn radio(
+ id: impl Into<ElementId>,
+ label: impl Into<SharedString>,
+ is_selected: bool,
+ ) -> Self {
+ Self::new(id, ChoiceCardKind::Radio, label, is_selected)
+ }
+
+ /// A card for an independently toggled choice, with a checkbox indicator.
+ /// Place sibling cards in a `Role::Group` container.
+ pub fn checkbox(
+ id: impl Into<ElementId>,
+ label: impl Into<SharedString>,
+ is_selected: bool,
+ ) -> Self {
+ Self::new(id, ChoiceCardKind::Checkbox, label, is_selected)
+ }
+
+ fn new(
+ id: impl Into<ElementId>,
+ kind: ChoiceCardKind,
+ label: impl Into<SharedString>,
+ is_selected: bool,
+ ) -> Self {
+ Self {
+ id: id.into(),
+ kind,
+ label: label.into(),
+ description: None,
+ is_selected,
+ invalid: false,
+ on_click: None,
+ }
+ }
+
+ /// Secondary text shown below the label and announced by assistive technology.
+ pub fn description(mut self, description: impl Into<SharedString>) -> Self {
+ self.description = Some(description.into());
+ self
+ }
+
+ /// Draws the card border, and the radio indicator if any, in the error color.
+ pub fn invalid(mut self, invalid: bool) -> Self {
+ self.invalid = invalid;
+ self
+ }
+
+ /// The card is only focusable and shows a pointer cursor once a click handler is set.
+ pub fn on_click(
+ mut self,
+ handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
+ ) -> Self {
+ self.on_click = Some(Box::new(handler));
+ self
+ }
+
+ fn render_radio_indicator(is_selected: bool, border_color: Hsla, background: Hsla) -> Div {
+ // Sharing the checkbox slot size keeps radio and checkbox labels aligned.
+ div()
+ .size(Checkbox::container_size())
+ .flex_none()
+ .flex()
+ .items_center()
+ .justify_center()
+ .child(
+ div().size_3().flex_none().child(
+ canvas(
+ |_, _, _| {},
+ move |bounds, _, window, cx| {
+ let bounds = window.pixel_snap_bounds(bounds);
+ let radius = bounds.size.width.min(bounds.size.height) / 2.;
+ window.paint_quad(quad(
+ bounds,
+ Corners::all(radius),
+ background,
+ Edges::all(px(1.)),
+ border_color,
+ BorderStyle::Solid,
+ ));
+
+ if is_selected {
+ // Equal whole-device-pixel insets keep both circles concentric
+ // when their independently rounded diameters would have different parity.
+ let scale_factor = window.scale_factor();
+ let inset =
+ px((f32::from(radius) * scale_factor / 2.).round()
+ / scale_factor);
+ let dot_bounds = bounds.dilate(-inset);
+ window.paint_quad(quad(
+ dot_bounds,
+ Corners::all(
+ dot_bounds.size.width.min(dot_bounds.size.height) / 2.,
+ ),
+ Color::Accent.color(cx),
+ Edges::default(),
+ Hsla::transparent_black(),
+ BorderStyle::Solid,
+ ));
+ }
+ },
+ )
+ .size_full(),
+ ),
+ )
+ }
+}
+
+impl RenderOnce for ChoiceCard {
+ fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
+ let colors = cx.theme().colors();
+ let border_color = if self.invalid {
+ Color::Error.color(cx)
+ } else {
+ colors.border.opacity(0.8)
+ };
+ let focused_border_color = colors.border_focused;
+ let editor_background = colors.editor_background;
+ let (background, hover_background) = if self.is_selected {
+ let accent = Color::Accent.color(cx);
+ (
+ editor_background.blend(accent.opacity(0.08)),
+ editor_background.blend(accent.opacity(0.1)),
+ )
+ } else {
+ (
+ editor_background,
+ colors
+ .element_background
+ .blend(colors.editor_foreground.opacity(0.025)),
+ )
+ };
+
+ let (role, indicator) = match self.kind {
+ ChoiceCardKind::Radio => (
+ Role::RadioButton,
+ Self::render_radio_indicator(self.is_selected, border_color, editor_background)
+ .into_any_element(),
+ ),
+ ChoiceCardKind::Checkbox => {
+ let checkbox_state = if self.is_selected {
+ ToggleState::Selected
+ } else {
+ ToggleState::Unselected
+ };
+ (
+ Role::CheckBox,
+ div()
+ .child(Checkbox::new((self.id.clone(), "checkbox"), checkbox_state))
+ .into_any_element(),
+ )
+ }
+ };
+
+ h_flex()
+ .id(self.id)
+ .role(role)
+ .aria_label(self.label.clone())
+ .when_some(self.description.clone(), |this, description| {
+ this.aria_description(description)
+ })
+ .aria_toggled(if self.is_selected {
+ Toggled::True
+ } else {
+ Toggled::False
+ })
+ .w_full()
+ .min_h(rems_from_px(28_f32))
+ .items_start()
+ .gap_1p5()
+ .rounded_sm()
+ .border_1()
+ .border_color(border_color.opacity(0.5))
+ .bg(background)
+ .px_2()
+ .py_1()
+ .hover(move |this| this.bg(hover_background))
+ .focus_visible(move |this| this.border_color(focused_border_color))
+ .when_some(self.on_click, |this, on_click| {
+ this.tab_index(0).cursor_pointer().on_click(on_click)
+ })
+ .child(indicator)
+ .child(
+ v_flex()
+ .min_w_0()
+ .flex_1()
+ .gap_0p5()
+ .child(Label::new(self.label).size(LabelSize::Small))
+ .when_some(self.description, |this, description| {
+ this.child(
+ Label::new(description)
+ .size(LabelSize::Small)
+ .color(Color::Muted),
+ )
+ }),
+ )
+ }
+}
+
+impl Component for ChoiceCard {
+ fn scope() -> ComponentScope {
+ ComponentScope::Input
+ }
+
+ fn description() -> &'static str {
+ "A clickable card for a single choice, with a radio or checkbox indicator, a label, and an optional description."
+ }
+
+ fn preview(_window: &mut Window, _cx: &mut App) -> AnyElement {
+ v_flex()
+ .gap_6()
+ .child(example_group_with_title(
+ "Radio",
+ vec![
+ single_example(
+ "Unselected",
+ Self::radio("choice-radio-unselected", "Unselected option", false)
+ .into_any_element(),
+ ),
+ single_example(
+ "Selected",
+ Self::radio("choice-radio-selected", "Selected option", true)
+ .into_any_element(),
+ ),
+ single_example(
+ "With Description",
+ Self::radio("choice-radio-description", "Option", true)
+ .description("A longer explanation of what this option does.")
+ .into_any_element(),
+ ),
+ single_example(
+ "Invalid",
+ Self::radio("choice-radio-invalid", "Invalid option", false)
+ .invalid(true)
+ .into_any_element(),
+ ),
+ single_example(
+ "Selected and Invalid",
+ Self::radio("choice-radio-selected-invalid", "Invalid option", true)
+ .invalid(true)
+ .into_any_element(),
+ ),
+ ],
+ ))
+ .child(example_group_with_title(
+ "Checkbox",
+ vec![
+ single_example(
+ "Unselected",
+ Self::checkbox("choice-checkbox-unselected", "Unselected option", false)
+ .into_any_element(),
+ ),
+ single_example(
+ "Selected",
+ Self::checkbox("choice-checkbox-selected", "Selected option", true)
+ .into_any_element(),
+ ),
+ single_example(
+ "With Description",
+ Self::checkbox("choice-checkbox-description", "Option", true)
+ .description("A longer explanation of what this option does.")
+ .into_any_element(),
+ ),
+ single_example(
+ "Invalid",
+ Self::checkbox("choice-checkbox-invalid", "Invalid option", false)
+ .invalid(true)
+ .into_any_element(),
+ ),
+ single_example(
+ "Selected and Invalid",
+ Self::checkbox("choice-checkbox-selected-invalid", "Invalid option", true)
+ .invalid(true)
+ .into_any_element(),
+ ),
+ ],
+ ))
+ .child(example_group_with_title(
+ "Long Text",
+ vec![
+ single_example(
+ "Radio",
+ Self::radio(
+ "choice-radio-long-text",
+ "Allow the agent to read and modify every file in this project, including files ignored by version control",
+ true,
+ )
+ .description(
+ "The agent will be able to create, edit, and delete files without asking for confirmation each time. You can revoke this permission at any point from the agent settings.",
+ )
+ .into_any_element(),
+ ),
+ single_example(
+ "Checkbox",
+ Self::checkbox(
+ "choice-checkbox-long-text",
+ "Include terminal output, diagnostics, and recently opened buffers as additional context",
+ false,
+ )
+ .description(
+ "Additional context can improve results for larger tasks, but it increases the size of each request and may take longer to process.",
+ )
+ .into_any_element(),
+ )
+ ],
+ ))
+ .into_any_element()
+ }
+}