use gpui::prelude::*;
use gpui::{div, px, App, ClickEvent, ElementId, IntoElement, SharedString, Window};
use crate::devtools::Probed;
use crate::input::ClickHandler;
use crate::theme::{theme, Size};
#[derive(Debug, Clone, Default)]
pub struct AISource {
pub title: String,
pub location: String,
pub excerpt: Option<String>,
}
impl AISource {
pub fn new(title: impl Into<String>, location: impl Into<String>) -> Self {
AISource {
title: title.into(),
location: location.into(),
excerpt: None,
}
}
pub fn excerpt(mut self, excerpt: impl Into<String>) -> Self {
self.excerpt = Some(excerpt.into());
self
}
}
#[derive(IntoElement)]
pub struct AICitation {
id: ElementId,
index: usize,
label: Option<SharedString>,
size: Size,
on_click: Option<ClickHandler>,
}
impl AICitation {
pub fn new(id: impl Into<ElementId>, index: usize) -> Self {
AICitation {
id: id.into(),
index,
label: None,
size: Size::Xs,
on_click: None,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = size;
self
}
pub fn on_click(
mut self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.on_click = Some(Box::new(handler));
self
}
}
impl RenderOnce for AICitation {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let t = theme(cx);
let font = t.font_size(self.size);
let dimmed = t.dimmed().hsla();
let text_color = t.text().hsla();
let surface = t.surface_hover().hsla();
let primary = t.primary().hsla();
let clickable = self.on_click.is_some();
div()
.id(self.id)
.flex()
.items_center()
.gap(px(4.0))
.h(px(font * 1.7))
.px(px(6.0))
.rounded(px(font))
.bg(surface)
.text_size(px(font))
.text_color(dimmed)
.when(clickable, |chip| {
chip
.cursor_pointer()
.hover(move |chip| chip.text_color(primary))
})
.child(
div()
.text_color(text_color)
.child(SharedString::from(self.index.to_string())),
)
.children(self.label)
.when_some(self.on_click, |chip, handler| {
chip.on_click(move |event, window, cx| handler(event, window, cx))
})
.probe("AICitation")
}
}