use crate::components::{Button, ButtonSize, ButtonVariant};
use crate::icons::{CarbonIcon, Icon};
use crate::t3::press;
use crate::{theme::color, tokens::ColorRole};
use gpui_kit::{
App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, Window, div, prelude::FluentBuilder as _,
};
use std::rc::Rc;
pub const REST_OPACITY: f32 = 0.75;
pub fn turn_footer_opacity(hovered: bool, focus_within: bool) -> f32 {
if hovered || focus_within {
1.
} else {
REST_OPACITY
}
}
#[derive(Clone, Debug)]
pub struct TurnFooterTexts {
pub copy: SharedString,
pub fork: SharedString,
pub retry: SharedString,
}
impl Default for TurnFooterTexts {
fn default() -> Self {
Self {
copy: "Copy response".into(),
fork: "Fork from here".into(),
retry: "Retry".into(),
}
}
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct TurnFooter {
id: ElementId,
model: Option<SharedString>,
tokens: Option<SharedString>,
time: Option<SharedString>,
on_copy: Option<Handler>,
on_fork: Option<Handler>,
on_retry: Option<Handler>,
texts: TurnFooterTexts,
}
impl TurnFooter {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
model: None,
tokens: None,
time: None,
on_copy: None,
on_fork: None,
on_retry: None,
texts: TurnFooterTexts::default(),
}
}
pub fn model(mut self, model: impl Into<SharedString>) -> Self {
self.model = Some(model.into());
self
}
pub fn tokens(mut self, tokens: impl Into<SharedString>) -> Self {
self.tokens = Some(tokens.into());
self
}
pub fn time(mut self, time: impl Into<SharedString>) -> Self {
self.time = Some(time.into());
self
}
pub fn on_copy(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_copy = Some(Rc::new(f));
self
}
pub fn on_fork(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_fork = Some(Rc::new(f));
self
}
pub fn on_retry(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_retry = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: TurnFooterTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for TurnFooter {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id;
let (hovered, hover) = press::hover((id.clone(), "hover"), window, cx);
let actions = [
("copy", CarbonIcon::Copy, self.texts.copy, self.on_copy),
("fork", CarbonIcon::Fork, self.texts.fork, self.on_fork),
("retry", CarbonIcon::Renew, self.texts.retry, self.on_retry),
];
let mut focus_within = false;
let buttons: Vec<Button> = actions
.into_iter()
.map(|(key, icon, label, handler)| {
let (focus, _) = press::focus((id.clone(), key), window, cx);
focus_within |= focus.is_focused(window);
Button::new((id.clone(), key), label)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.icon(Icon::carbon(icon))
.icon_only(true)
.track_focus(&focus)
.when_some(handler, |b, f| {
b.on_click(move |_, window, cx| f(window, cx))
})
})
.collect();
let meta: Vec<SharedString> = [self.model, self.tokens, self.time]
.into_iter()
.flatten()
.collect();
div()
.id(id)
.flex()
.flex_row()
.items_center()
.opacity(turn_footer_opacity(hovered, focus_within))
.on_hover(press::track_hover(hover))
.children(buttons)
.when(!meta.is_empty(), |this| {
this.child(
div()
.flex()
.flex_row()
.gap(press::rem(8.))
.ml(press::rem(8.))
.text_size(press::rem(12.))
.line_height(press::rem(16.))
.text_color(color(ColorRole::TextHelper))
.whitespace_nowrap()
.children(meta),
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rests_at_75_percent_until_hovered_or_focused() {
assert_eq!(turn_footer_opacity(false, false), 0.75);
assert_eq!(turn_footer_opacity(true, false), 1.);
assert_eq!(turn_footer_opacity(false, true), 1.);
}
#[test]
fn button_names_follow_the_reference() {
let t = TurnFooterTexts::default();
assert_eq!(t.copy, "Copy response");
assert_eq!(t.fork, "Fork from here");
assert_eq!(t.retry, "Retry");
}
}