add reveal text
This commit is contained in:
+43
-10
@@ -36,8 +36,8 @@ use state::{
|
|||||||
};
|
};
|
||||||
use ui::{
|
use ui::{
|
||||||
Avatar, COMPACT_REACTION_EMOJIS, IconName, MessageActions, MessageMedia, MessageRow,
|
Avatar, COMPACT_REACTION_EMOJIS, IconName, MessageActions, MessageMedia, MessageRow,
|
||||||
REACTION_EMOJIS, ReactionBar, RenderedText, ReplyPreview, WelcomeMessage, group_reactions,
|
REACTION_EMOJIS, ReactionBar, RenderedText, ReplyPreview, RevealText, WelcomeMessage,
|
||||||
opens_run, rendered_text,
|
group_reactions, opens_run, rendered_text,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::file::*;
|
use crate::file::*;
|
||||||
@@ -90,6 +90,9 @@ pub struct ChatPanel {
|
|||||||
speech_started: Option<instant::Instant>,
|
speech_started: Option<instant::Instant>,
|
||||||
/// Reads messages aloud
|
/// Reads messages aloud
|
||||||
speaker: Speaker,
|
speaker: Speaker,
|
||||||
|
|
||||||
|
/// The message being revealed in step with speech, with its estimated duration
|
||||||
|
speech_reveal: Option<(EventId, Duration)>,
|
||||||
/// Subject input state
|
/// Subject input state
|
||||||
subject_input: Entity<InputState>,
|
subject_input: Entity<InputState>,
|
||||||
/// Subject bar visibility
|
/// Subject bar visibility
|
||||||
@@ -222,6 +225,20 @@ impl ChatPanel {
|
|||||||
this.get_messages(window, cx);
|
this.get_messages(window, cx);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// End the speech reveal when the utterance finishes ahead of its estimate
|
||||||
|
let speaker = Speaker::new();
|
||||||
|
let end_events = speaker.end_events();
|
||||||
|
let reveal_end_task = cx.spawn(async move |this, cx| {
|
||||||
|
while end_events.recv_async().await.is_ok() {
|
||||||
|
this.update(cx, |this, cx| {
|
||||||
|
this.speech_reveal = None;
|
||||||
|
cx.notify();
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
focus_handle: cx.focus_handle(),
|
focus_handle: cx.focus_handle(),
|
||||||
id,
|
id,
|
||||||
@@ -234,7 +251,8 @@ impl ChatPanel {
|
|||||||
input,
|
input,
|
||||||
speech,
|
speech,
|
||||||
speech_started: None,
|
speech_started: None,
|
||||||
speaker: Speaker::new(),
|
speaker,
|
||||||
|
speech_reveal: None,
|
||||||
subject_input,
|
subject_input,
|
||||||
subject_bar,
|
subject_bar,
|
||||||
replies_to,
|
replies_to,
|
||||||
@@ -245,7 +263,7 @@ impl ChatPanel {
|
|||||||
reports_by_id,
|
reports_by_id,
|
||||||
uploading: false,
|
uploading: false,
|
||||||
subscriptions,
|
subscriptions,
|
||||||
tasks: vec![],
|
tasks: vec![reveal_end_task],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1174,6 +1192,15 @@ impl ChatPanel {
|
|||||||
nostr.read(cx).current_user(),
|
nostr.read(cx).current_user(),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
let body = match self
|
||||||
|
.speech_reveal
|
||||||
|
.as_ref()
|
||||||
|
.filter(|(reveal_id, _)| *reveal_id == id)
|
||||||
|
{
|
||||||
|
Some((_, duration)) => RevealText::new(message.preview(), *duration).into_any_element(),
|
||||||
|
None => rendered_text,
|
||||||
|
};
|
||||||
|
|
||||||
MessageRow::new(ix)
|
MessageRow::new(ix)
|
||||||
.show_author(show_author)
|
.show_author(show_author)
|
||||||
.hide_avatar(hide_avatar)
|
.hide_avatar(hide_avatar)
|
||||||
@@ -1198,7 +1225,7 @@ impl ChatPanel {
|
|||||||
.when(has_replies, |this| {
|
.when(has_replies, |this| {
|
||||||
this.children(self.render_message_replies(replies, cx))
|
this.children(self.render_message_replies(replies, cx))
|
||||||
})
|
})
|
||||||
.when(message.file.is_none(), |this| this.child(rendered_text))
|
.when(message.file.is_none(), |this| this.child(body))
|
||||||
.child(MessageMedia::new(&message.media))
|
.child(MessageMedia::new(&message.media))
|
||||||
.when_some(message.file.as_ref(), |this, file| {
|
.when_some(message.file.as_ref(), |this, file| {
|
||||||
this.child(self.render_message_file(&id, file, cx))
|
this.child(self.render_message_file(&id, file, cx))
|
||||||
@@ -1431,7 +1458,6 @@ impl ChatPanel {
|
|||||||
|
|
||||||
let reply_id = *id;
|
let reply_id = *id;
|
||||||
let dropdown_id = *id;
|
let dropdown_id = *id;
|
||||||
let speech_id = *id;
|
|
||||||
let public_key = *public_key;
|
let public_key = *public_key;
|
||||||
|
|
||||||
let actions = actions.reply(cx.listener(move |this, _event, _window, cx| {
|
let actions = actions.reply(cx.listener(move |this, _event, _window, cx| {
|
||||||
@@ -1447,13 +1473,20 @@ impl ChatPanel {
|
|||||||
actions
|
actions
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let speech_id = *id;
|
||||||
let actions = actions.speech(cx.listener(move |this, _event, window, cx| {
|
let actions = actions.speech(cx.listener(move |this, _event, window, cx| {
|
||||||
let text = this.message(&speech_id).map(|message| message.preview());
|
let text = this.message(&speech_id).map(|message| message.preview());
|
||||||
|
|
||||||
if let Some(text) = text
|
if let Some(text) = text {
|
||||||
&& let Err(error) = this.speaker.speak(&text)
|
match this.speaker.speak(&text) {
|
||||||
{
|
Ok(duration) => {
|
||||||
window.push_notification(Notification::error(error.to_string()), cx);
|
this.speech_reveal = Some((speech_id, duration));
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
window.push_notification(Notification::error(error.to_string()), cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ use settings::AppSettings;
|
|||||||
use smallvec::{SmallVec, smallvec};
|
use smallvec::{SmallVec, smallvec};
|
||||||
use speech::speaker::Speaker;
|
use speech::speaker::Speaker;
|
||||||
use state::{NostrRegistry, pick_file, upload_file};
|
use state::{NostrRegistry, pick_file, upload_file};
|
||||||
use ui::{Avatar, RenderedText, WelcomeMessage, rendered_text};
|
use ui::{Avatar, RenderedText, RevealText, WelcomeMessage, rendered_text};
|
||||||
use util::{attachment_name, display_name, opens_run, pin_label, report};
|
use util::{attachment_name, display_name, opens_run, pin_label, report};
|
||||||
|
|
||||||
mod details;
|
mod details;
|
||||||
@@ -323,6 +323,8 @@ pub struct GroupPanel {
|
|||||||
speech_started: Option<instant::Instant>,
|
speech_started: Option<instant::Instant>,
|
||||||
/// Reads messages aloud
|
/// Reads messages aloud
|
||||||
speaker: Speaker,
|
speaker: Speaker,
|
||||||
|
/// The message being revealed in step with speech, with its estimated duration
|
||||||
|
speech_reveal: Option<(EventId, Duration)>,
|
||||||
/// Uploaded, non-encrypted attachments waiting to be sent.
|
/// Uploaded, non-encrypted attachments waiting to be sent.
|
||||||
attachments: Vec<Url>,
|
attachments: Vec<Url>,
|
||||||
/// Whether an upload is in flight.
|
/// Whether an upload is in flight.
|
||||||
@@ -425,6 +427,20 @@ impl GroupPanel {
|
|||||||
this.track_participants(cx);
|
this.track_participants(cx);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// End the speech reveal when the utterance finishes ahead of its estimate
|
||||||
|
let speaker = Speaker::new();
|
||||||
|
let end_events = speaker.end_events();
|
||||||
|
let reveal_end_task = cx.spawn(async move |this, cx| {
|
||||||
|
while end_events.recv_async().await.is_ok() {
|
||||||
|
this.update(cx, |this, cx| {
|
||||||
|
this.speech_reveal = None;
|
||||||
|
cx.notify();
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
focus_handle: cx.focus_handle(),
|
focus_handle: cx.focus_handle(),
|
||||||
group: group.downgrade(),
|
group: group.downgrade(),
|
||||||
@@ -437,10 +453,11 @@ impl GroupPanel {
|
|||||||
input,
|
input,
|
||||||
speech,
|
speech,
|
||||||
speech_started: None,
|
speech_started: None,
|
||||||
speaker: Speaker::new(),
|
speaker,
|
||||||
|
speech_reveal: None,
|
||||||
attachments: Vec::new(),
|
attachments: Vec::new(),
|
||||||
uploading: false,
|
uploading: false,
|
||||||
tasks: smallvec![],
|
tasks: smallvec![reveal_end_task],
|
||||||
_subscriptions: subscriptions,
|
_subscriptions: subscriptions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1023,7 +1040,13 @@ impl GroupPanel {
|
|||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let directory = self.directory.clone();
|
let directory = self.directory.clone();
|
||||||
|
|
||||||
let content = self
|
let content = match self
|
||||||
|
.speech_reveal
|
||||||
|
.as_ref()
|
||||||
|
.filter(|(reveal_id, _)| *reveal_id == message.id)
|
||||||
|
{
|
||||||
|
Some((_, duration)) => RevealText::new(message.preview(), *duration).into_any_element(),
|
||||||
|
None => self
|
||||||
.rendered_texts_by_id
|
.rendered_texts_by_id
|
||||||
.entry(message.id)
|
.entry(message.id)
|
||||||
.or_insert_with(|| {
|
.or_insert_with(|| {
|
||||||
@@ -1038,7 +1061,8 @@ impl GroupPanel {
|
|||||||
cx,
|
cx,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.element(ix.into(), window, cx);
|
.element(ix.into(), window, cx),
|
||||||
|
};
|
||||||
|
|
||||||
let admin = nostr
|
let admin = nostr
|
||||||
.read(cx)
|
.read(cx)
|
||||||
|
|||||||
@@ -91,11 +91,16 @@ pub(crate) fn render(
|
|||||||
let speech_panel = actions.panel;
|
let speech_panel = actions.panel;
|
||||||
let panel = speech_panel.clone();
|
let panel = speech_panel.clone();
|
||||||
let speech_text = message.preview();
|
let speech_text = message.preview();
|
||||||
|
let speech_id = message.id;
|
||||||
|
|
||||||
let row = row.overlay(MessageActions::new().speech(move |_event, _window, cx| {
|
let row = row.overlay(MessageActions::new().speech(move |_event, _window, cx| {
|
||||||
_ = speech_panel.update_in(cx, |panel, window, cx| {
|
_ = speech_panel.update_in(cx, |panel, window, cx| {
|
||||||
if let Err(error) = panel.speaker.speak(&speech_text) {
|
match panel.speaker.speak(&speech_text) {
|
||||||
window.push_notification(Notification::error(error.to_string()), cx);
|
Ok(duration) => {
|
||||||
|
panel.speech_reveal = Some((speech_id, duration));
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
Err(error) => window.push_notification(Notification::error(error.to_string()), cx),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -1,33 +1,54 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use tts::Tts;
|
use tts::Tts;
|
||||||
|
|
||||||
|
/// Words per minute of an engine at its normal rate, for estimating speech duration.
|
||||||
|
const NORMAL_WPM: f32 = 160.0;
|
||||||
|
|
||||||
/// Reads text aloud through the platform's speech backend, created on first use.
|
/// Reads text aloud through the platform's speech backend, created on first use.
|
||||||
pub struct Speaker {
|
pub struct Speaker {
|
||||||
tts: Option<Tts>,
|
tts: Option<Tts>,
|
||||||
}
|
end_tx: flume::Sender<()>,
|
||||||
|
end_rx: flume::Receiver<()>,
|
||||||
impl Default for Speaker {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Speaker {
|
impl Speaker {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { tts: None }
|
let (end_tx, end_rx) = flume::unbounded();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
tts: None,
|
||||||
|
end_tx,
|
||||||
|
end_rx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cloneable stream of utterance-finish events.
|
||||||
|
pub fn end_events(&self) -> flume::Receiver<()> {
|
||||||
|
self.end_rx.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Speak the text, interrupting what this speaker is already saying.
|
/// Speak the text, interrupting what this speaker is already saying.
|
||||||
pub fn speak(&mut self, text: &str) -> Result<(), tts::Error> {
|
pub fn speak(&mut self, text: &str) -> Result<Duration, tts::Error> {
|
||||||
|
let end_tx = self.end_tx.clone();
|
||||||
let tts = match self.tts.as_ref() {
|
let tts = match self.tts.as_ref() {
|
||||||
Some(tts) => tts,
|
Some(tts) => tts,
|
||||||
None => self.tts.insert(Tts::default()?),
|
None => self.tts.insert({
|
||||||
|
let tts = Tts::default()?;
|
||||||
|
if let Err(error) = tts.on_utterance_end(move |_| {
|
||||||
|
// A send error only means the panel dropped its event loop.
|
||||||
|
let _ = end_tx.send(());
|
||||||
|
}) {
|
||||||
|
log::warn!("utterance end events unavailable ({error})");
|
||||||
|
}
|
||||||
|
tts
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
tts.speak(text, true)?;
|
tts.speak(text, true)?;
|
||||||
Ok(())
|
Ok(Self::estimate(tts, text))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stop speaking; a speaker that never spoke is already silent.
|
/// Stop speaking, a speaker that never spoke is already silent.
|
||||||
pub fn stop(&mut self) -> Result<(), tts::Error> {
|
pub fn stop(&mut self) -> Result<(), tts::Error> {
|
||||||
match self.tts.as_ref() {
|
match self.tts.as_ref() {
|
||||||
Some(tts) => tts.stop()?,
|
Some(tts) => tts.stop()?,
|
||||||
@@ -37,11 +58,27 @@ impl Speaker {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Best-effort speaking state; a speaker that never spoke is silent.
|
/// Best-effort speaking state, a speaker that never spoke is silent.
|
||||||
pub fn speaking(&self) -> bool {
|
pub fn speaking(&self) -> bool {
|
||||||
self.tts
|
self.tts
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|tts| tts.is_speaking().ok())
|
.and_then(|tts| tts.is_speaking().ok())
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// How long speaking the text is expected to take at the engine's current speed.
|
||||||
|
fn estimate(tts: &Tts, text: &str) -> Duration {
|
||||||
|
let words = text.split_whitespace().count().max(1) as f32;
|
||||||
|
let speed = match (tts.get_rate(), tts.normal_rate()) {
|
||||||
|
(Ok(rate), Ok(normal)) if normal > 0. => (rate / normal).clamp(0.2, 4.),
|
||||||
|
_ => 1.,
|
||||||
|
};
|
||||||
|
Duration::from_secs_f32(words / NORMAL_WPM * 60. / speed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Speaker {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use gpui::prelude::FluentBuilder as _;
|
use gpui::prelude::FluentBuilder as _;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
AnyElement, App, ClickEvent, Context, ElementId, InteractiveElement as _, IntoElement,
|
Animation, AnimationExt as _, AnyElement, App, ClickEvent, Context, ElementId,
|
||||||
MouseButton, MouseDownEvent, ObjectFit, ParentElement as _, RenderOnce, SharedString,
|
InteractiveElement as _, IntoElement, MouseButton, MouseDownEvent, ObjectFit,
|
||||||
SharedUri, StatefulInteractiveElement as _, StyleRefinement, Styled, StyledImage, Window, div,
|
ParentElement as _, RenderOnce, SharedString, SharedUri, StatefulInteractiveElement as _,
|
||||||
img, px,
|
StyleRefinement, Styled, StyledImage, Window, div, img, px,
|
||||||
};
|
};
|
||||||
use gpui_kit::assets::IconName;
|
use gpui_kit::assets::IconName;
|
||||||
use gpui_kit::component::button::{Button, ButtonVariants as _};
|
use gpui_kit::component::button::{Button, ButtonVariants as _};
|
||||||
@@ -750,3 +751,44 @@ impl RenderOnce for MessageActions {
|
|||||||
.refine_style(&style)
|
.refine_style(&style)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A message's text revealed word by word, paced by the estimated speech duration.
|
||||||
|
#[derive(IntoElement)]
|
||||||
|
pub struct RevealText {
|
||||||
|
text: SharedString,
|
||||||
|
duration: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RevealText {
|
||||||
|
pub fn new(text: impl Into<SharedString>, duration: Duration) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
duration,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RenderOnce for RevealText {
|
||||||
|
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
|
||||||
|
let words: Vec<SharedString> = self
|
||||||
|
.text
|
||||||
|
.split_whitespace()
|
||||||
|
.map(SharedString::from)
|
||||||
|
.collect();
|
||||||
|
let total = words.len().max(1) as f32;
|
||||||
|
|
||||||
|
div().flex().flex_wrap().gap_x_1().with_animation(
|
||||||
|
"speech-reveal",
|
||||||
|
Animation::new(self.duration).with_max_fps(30.),
|
||||||
|
move |this, progress| {
|
||||||
|
let visible = ((progress * total).ceil() as usize).min(words.len());
|
||||||
|
|
||||||
|
this.children(words.iter().enumerate().map(|(ix, word)| {
|
||||||
|
div()
|
||||||
|
.when(ix >= visible, |this| this.invisible())
|
||||||
|
.child(word.clone())
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user