use crate::t3::base::{Spin, fmt_elapsed, use_ticker};
use crate::t3::press::{self, Handler, rem};
use crate::{
icons::CarbonIcon,
motion::{self, cubic_bezier_at},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
Animation, AnimationExt as _, App, ElementId, Hsla, IntoElement, ParentElement, RenderOnce,
SharedString, StatefulInteractiveElement as _, Styled, Window, div,
prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
use std::time::Duration;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum VoiceState {
#[default]
Recording,
Starting,
Downloading,
Transcribing,
Cleaning,
Error,
}
impl VoiceState {
pub const ALL: [VoiceState; 6] = [
Self::Recording,
Self::Starting,
Self::Downloading,
Self::Transcribing,
Self::Cleaning,
Self::Error,
];
pub const fn name(self) -> &'static str {
match self {
Self::Recording => "recording",
Self::Starting => "starting",
Self::Downloading => "downloading",
Self::Transcribing => "transcribing",
Self::Cleaning => "cleaning",
Self::Error => "error",
}
}
}
#[derive(Clone)]
pub struct VoiceTexts {
pub recording: SharedString,
pub starting: SharedString,
pub downloading: Rc<dyn Fn(u8) -> SharedString>,
pub transcribing: SharedString,
pub cleaning: SharedString,
pub failed: SharedString,
pub dismiss: SharedString,
pub discard: SharedString,
pub stop: SharedString,
}
impl Default for VoiceTexts {
fn default() -> Self {
Self {
recording: "Recording".into(),
starting: "Starting…".into(),
downloading: Rc::new(|n| format!("Downloading model {n}%").into()),
transcribing: "Transcribing…".into(),
cleaning: "Cleaning up…".into(),
failed: "Voice input failed".into(),
dismiss: "Dismiss".into(),
discard: "Discard recording".into(),
stop: "Stop recording".into(),
}
}
}
pub const WAVE_PERIOD: Duration = Duration::from_millis(900);
pub fn wave_scale(t: f32) -> f32 {
let t = t.rem_euclid(1.);
let (from, to, local) = if t < 0.5 {
(0.25, 1., t * 2.)
} else {
(1., 0.25, (t - 0.5) * 2.)
};
from + (to - from) * cubic_bezier_at(0.42, 0., 0.58, 1., local)
}
pub const fn wave_delay(i: usize) -> u64 {
((i * 83) % 600) as u64
}
#[derive(IntoElement)]
pub struct VoiceInputPill {
id: ElementId,
state: VoiceState,
progress: u8,
seconds: u64,
error: Option<SharedString>,
texts: VoiceTexts,
on_stop: Option<Handler>,
on_cancel: Option<Handler>,
}
impl VoiceInputPill {
pub fn new(id: impl Into<ElementId>, state: VoiceState) -> Self {
Self {
id: id.into(),
state,
progress: 0,
seconds: 0,
error: None,
texts: VoiceTexts::default(),
on_stop: None,
on_cancel: None,
}
}
pub fn progress(mut self, progress: u8) -> Self {
self.progress = progress;
self
}
pub fn seconds(mut self, seconds: u64) -> Self {
self.seconds = seconds;
self
}
pub fn error(mut self, error: impl Into<SharedString>) -> Self {
self.error = Some(error.into());
self
}
pub fn texts(mut self, texts: VoiceTexts) -> Self {
self.texts = texts;
self
}
pub fn on_stop(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_stop = Some(Rc::new(f));
self
}
pub fn on_cancel(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_cancel = Some(Rc::new(f));
self
}
}
#[allow(clippy::too_many_arguments)]
fn pill_button(
id: ElementId,
label: SharedString,
size: f32,
fill: Option<Hsla>,
hover_fill: Option<Hsla>,
icon: CarbonIcon,
icon_size: f32,
on_click: Option<Handler>,
window: &mut Window,
cx: &mut App,
) -> gpui_kit::base::Button {
let (hovered, hover) = press::hover((id.clone(), "hover"), window, cx);
let (focus, ring) = press::focus((id.clone(), "focus"), window, cx);
let bg = if hovered { hover_fill.or(fill) } else { fill };
gpui_kit::base::Button::new(id)
.track_focus(&focus)
.accessibility_label(label)
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(size))
.cursor_pointer()
.when_some(bg, |this, bg| this.bg(bg))
.on_hover(press::track_hover(hover))
.when_some(on_click, |this, f| this.on_click(move |_, w, cx| f(w, cx)))
.child(press::icon(icon, icon_size, color(ColorRole::IconPrimary)))
.when(ring, |this| this.child(press::ring()))
}
impl RenderOnce for VoiceInputPill {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let recording = self.state == VoiceState::Recording;
let t = use_ticker((id.clone(), "ticker"), self.seconds, recording, window, cx);
let texts = self.texts.clone();
let pill = div()
.flex()
.flex_row()
.flex_none()
.items_center()
.gap(rem(8.))
.h(rem(32.))
.pl(rem(12.))
.pr(rem(4.))
.text_color(color(ColorRole::TextPrimary))
.text_size(rem(12.))
.line_height(rem(16.))
.whitespace_nowrap();
if self.state == VoiceState::Error {
return pill
.pr(rem(8.))
.bg(color(ColorRole::NotificationBackgroundError))
.child(press::icon(
CarbonIcon::WarningAltFilled,
16.,
color(ColorRole::SupportError),
))
.child(self.error.clone().unwrap_or(texts.failed.clone()))
.child(pill_button(
(id.clone(), "dismiss").into(),
texts.dismiss.clone(),
24.,
None,
None,
CarbonIcon::Close,
12.,
self.on_cancel.clone(),
window,
cx,
))
.into_any_element();
}
if !recording {
let text = match self.state {
VoiceState::Starting => texts.starting.clone(),
VoiceState::Downloading => (texts.downloading)(self.progress),
VoiceState::Transcribing => texts.transcribing.clone(),
_ => texts.cleaning.clone(),
};
return pill
.bg(color(ColorRole::LayerAccent01))
.child(Spin::new((id.clone(), "spin")))
.child(text)
.into_any_element();
}
let reduced = motion::reduced_motion(cx);
let red = color(ColorRole::SupportError);
let wave = div()
.flex()
.flex_row()
.items_center()
.gap(px(2.))
.h(px(16.));
let wave = if reduced {
wave.children((0..12).map(|_| div().w(px(2.)).h(px(16. * 0.6)).bg(red)))
.into_any_element()
} else {
wave.with_animation(
(id.clone(), "wave"),
Animation::new(WAVE_PERIOD).repeat(),
move |this, t| {
this.children((0..12).map(|i| {
let delay = wave_delay(i) as f32 / WAVE_PERIOD.as_millis() as f32;
div().w(px(2.)).h(px(16. * wave_scale(t - delay))).bg(red)
}))
},
)
.into_any_element()
};
pill.bg(color(ColorRole::NotificationBackgroundError))
.child(wave)
.child(texts.recording.clone())
.child(
div()
.text_color(color(ColorRole::TextSecondary))
.child(SharedString::from(fmt_elapsed(t))),
)
.child(pill_button(
(id.clone(), "discard").into(),
texts.discard.clone(),
32.,
None,
Some(color(ColorRole::LayerHover01)),
CarbonIcon::Close,
16.,
self.on_cancel.clone(),
window,
cx,
))
.child(pill_button(
(id.clone(), "stop").into(),
texts.stop.clone(),
32.,
Some(color(ColorRole::LayerAccent01)),
Some(color(ColorRole::LayerAccentHover01)),
CarbonIcon::Checkmark,
12.,
self.on_stop.clone(),
window,
cx,
))
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_wave_breathes_between_a_quarter_and_full() {
assert!((wave_scale(0.) - 0.25).abs() < 1e-4);
assert!((wave_scale(0.5) - 1.).abs() < 1e-4);
assert!((wave_scale(1.) - 0.25).abs() < 1e-4);
assert_eq!(wave_delay(1), 83);
assert_eq!(wave_delay(8), 64);
}
}