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 .px_1()
33 .border_1()
34 .border_color(cx.theme().colors().border)
35 .rounded_md()
36 .child(Label::new(self.context.name.clone()).size(LabelSize::Small))
37 .when_some(self.on_remove, |parent, on_remove| {
38 parent.child(
39 IconButton::new("remove", IconName::Close)
40 .shape(IconButtonShape::Square)
41 .icon_size(IconSize::XSmall)
42 .on_click({
43 let on_remove = on_remove.clone();
44 move |event, cx| on_remove(event, cx)
45 }),
46 )
47 })
48 }
49}