1use std::rc::Rc;
2
3use gpui::ClickEvent;
4use ui::{prelude::*, IconButtonShape, Tooltip};
5
6use crate::context::{Context, ContextKind};
7
8#[derive(IntoElement)]
9pub enum ContextPill {
10 Added {
11 context: Context,
12 dupe_name: bool,
13 on_remove: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext)>>,
14 },
15 Suggested {
16 name: SharedString,
17 kind: ContextKind,
18 on_add: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>,
19 },
20}
21
22impl ContextPill {
23 pub fn new_added(
24 context: Context,
25 dupe_name: bool,
26 on_remove: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext)>>,
27 ) -> Self {
28 Self::Added {
29 context,
30 dupe_name,
31 on_remove,
32 }
33 }
34
35 pub fn new_suggested(
36 name: SharedString,
37 kind: ContextKind,
38 on_add: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>,
39 ) -> Self {
40 Self::Suggested { name, kind, on_add }
41 }
42
43 pub fn id(&self) -> ElementId {
44 match self {
45 Self::Added { context, .. } => {
46 ElementId::NamedInteger("context-pill".into(), context.id.0)
47 }
48 Self::Suggested { .. } => "suggested-context-pill".into(),
49 }
50 }
51
52 pub fn kind(&self) -> &ContextKind {
53 match self {
54 Self::Added { context, .. } => &context.kind,
55 Self::Suggested { kind, .. } => kind,
56 }
57 }
58}
59
60impl RenderOnce for ContextPill {
61 fn render(self, cx: &mut WindowContext) -> impl IntoElement {
62 let icon = match &self.kind() {
63 ContextKind::File => IconName::File,
64 ContextKind::Directory => IconName::Folder,
65 ContextKind::FetchedUrl => IconName::Globe,
66 ContextKind::Thread => IconName::MessageCircle,
67 };
68
69 let color = cx.theme().colors();
70
71 let base_pill = h_flex()
72 .id(self.id())
73 .pl_1()
74 .pb(px(1.))
75 .border_1()
76 .rounded_md()
77 .gap_1()
78 .child(Icon::new(icon).size(IconSize::XSmall).color(Color::Muted));
79
80 match &self {
81 ContextPill::Added {
82 context,
83 dupe_name,
84 on_remove,
85 } => base_pill
86 .bg(color.element_background)
87 .border_color(color.border.opacity(0.5))
88 .pr(if on_remove.is_some() { px(2.) } else { px(4.) })
89 .child(Label::new(context.name.clone()).size(LabelSize::Small))
90 .when_some(context.parent.as_ref(), |element, parent_name| {
91 if *dupe_name {
92 element.child(
93 Label::new(parent_name.clone())
94 .size(LabelSize::XSmall)
95 .color(Color::Muted),
96 )
97 } else {
98 element
99 }
100 })
101 .when_some(context.tooltip.clone(), |element, tooltip| {
102 element.tooltip(move |cx| Tooltip::text(tooltip.clone(), cx))
103 })
104 .when_some(on_remove.as_ref(), |element, on_remove| {
105 element.child(
106 IconButton::new(("remove", context.id.0), IconName::Close)
107 .shape(IconButtonShape::Square)
108 .icon_size(IconSize::XSmall)
109 .tooltip(|cx| Tooltip::text("Remove Context", cx))
110 .on_click({
111 let on_remove = on_remove.clone();
112 move |event, cx| on_remove(event, cx)
113 }),
114 )
115 }),
116 ContextPill::Suggested { name, kind, on_add } => base_pill
117 .cursor_pointer()
118 .pr_1()
119 .border_color(color.border_variant.opacity(0.5))
120 .hover(|style| style.bg(color.element_hover.opacity(0.5)))
121 .child(
122 Label::new(name.clone())
123 .size(LabelSize::Small)
124 .color(Color::Muted),
125 )
126 .child(
127 Label::new(match kind {
128 ContextKind::File => "Open File",
129 ContextKind::Thread | ContextKind::Directory | ContextKind::FetchedUrl => {
130 "Active"
131 }
132 })
133 .size(LabelSize::XSmall)
134 .color(Color::Muted),
135 )
136 .child(
137 Icon::new(IconName::Plus)
138 .size(IconSize::XSmall)
139 .into_any_element(),
140 )
141 .tooltip(|cx| Tooltip::with_meta("Suggested Context", None, "Click to add it", cx))
142 .on_click({
143 let on_add = on_add.clone();
144 move |event, cx| on_add(event, cx)
145 }),
146 }
147 }
148}