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        h_flex()
31            .gap_1()
32            .pl_1p5()
33            .pr_0p5()
34            .pb(px(1.))
35            .border_1()
36            .border_color(cx.theme().colors().border.opacity(0.5))
37            .bg(cx.theme().colors().element_background)
38            .rounded_md()
39            .child(Label::new(self.context.name.clone()).size(LabelSize::Small))
40            .when_some(self.on_remove, |parent, on_remove| {
41                parent.child(
42                    IconButton::new(("remove", self.context.id.0), IconName::Close)
43                        .shape(IconButtonShape::Square)
44                        .icon_size(IconSize::XSmall)
45                        .on_click({
46                            let on_remove = on_remove.clone();
47                            move |event, cx| on_remove(event, cx)
48                        }),
49                )
50            })
51    }
52}