context_pill.rs

 1use std::rc::Rc;
 2
 3use gpui::ClickEvent;
 4use ui::{prelude::*, IconButtonShape};
 5
 6use crate::context::Context;
 7
 8#[derive(IntoElement)]
 9pub struct ContextPill {
10    context: Context,
11    on_remove: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext)>>,
12}
13
14impl ContextPill {
15    pub fn new(context: Context) -> Self {
16        Self {
17            context,
18            on_remove: None,
19        }
20    }
21
22    pub fn on_remove(mut self, on_remove: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>) -> Self {
23        self.on_remove = Some(on_remove);
24        self
25    }
26}
27
28impl RenderOnce for ContextPill {
29    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
30        let padding_right = if self.on_remove.is_some() {
31            px(2.)
32        } else {
33            px(4.)
34        };
35
36        h_flex()
37            .gap_1()
38            .pl_1()
39            .pr(padding_right)
40            .pb(px(1.))
41            .border_1()
42            .border_color(cx.theme().colors().border.opacity(0.5))
43            .bg(cx.theme().colors().element_background)
44            .rounded_md()
45            .child(
46                Icon::new(self.context.icon)
47                    .size(IconSize::XSmall)
48                    .color(Color::Muted),
49            )
50            .child(Label::new(self.context.name.clone()).size(LabelSize::Small))
51            .when_some(self.on_remove, |parent, on_remove| {
52                parent.child(
53                    IconButton::new(("remove", self.context.id.0), IconName::Close)
54                        .shape(IconButtonShape::Square)
55                        .icon_size(IconSize::XSmall)
56                        .on_click({
57                            let on_remove = on_remove.clone();
58                            move |event, cx| on_remove(event, cx)
59                        }),
60                )
61            })
62    }
63}