This commit is contained in:
2026-10-30 10:37:29 +07:00
parent af2a276b1b
commit 6ce20927ee
9 changed files with 205 additions and 32 deletions
+2
View File
@@ -14,6 +14,8 @@ pub enum Command {
Relays(PublicKey),
Njump(PublicKey),
Trace(EventId),
Speak(EventId),
StopSpeaking,
}
// Start or stop dictation in the focused composer
+30 -23
View File
@@ -30,6 +30,7 @@ use person::{Directory, Person};
use regex::Regex;
use settings::{AppSettings, SignerKind};
use smallvec::{SmallVec, smallvec};
use speech::speaker::Speaker;
use state::{
FileAttachment, LocalFile, NostrRegistry, PickedFile, download_and_decrypt_file, pick_file,
};
@@ -65,65 +66,45 @@ pub fn init(room: WeakEntity<Room>, window: &mut Window, cx: &mut App) -> Entity
pub struct ChatPanel {
id: SharedString,
focus_handle: FocusHandle,
/// Chat Room
room: WeakEntity<Room>,
/// Person directory shared with the room
directory: Entity<Directory>,
/// Message list state
list_state: ListState,
/// All messages (sorted by created_at)
messages: Vec<Message>,
/// O(1) message lookup by EventId
message_index: HashMap<EventId, usize>,
/// All reactions
reactions: BTreeMap<EventId, Vec<(SharedString, Option<SharedUri>, PublicKey)>>,
/// Mapping message ids to their rendered texts
rendered_texts_by_id: BTreeMap<EventId, RenderedText>,
/// Mapping message (rumor event) ids to their reports
reports_by_id: Arc<RwLock<BTreeMap<EventId, Vec<SendReport>>>>,
/// Chat input state
input: Entity<TextareaState>,
/// Dictation session for the chat input
speech: Entity<SpeechState>,
/// When the running dictation started, for the elapsed counter
speech_started: Option<instant::Instant>,
/// Reads messages aloud
speaker: Speaker,
/// Subject input state
subject_input: Entity<InputState>,
/// Subject bar visibility
subject_bar: Entity<bool>,
/// Replies to
replies_to: Entity<HashSet<EventId>>,
/// Media Attachment
attachments: Entity<Vec<Url>>,
/// Uploaded, encrypted file attachments which are not sent yet
encrypted_attachments: Entity<Vec<PendingFile>>,
/// Decrypted attachments of file messages, by message id
decrypted_files: HashMap<EventId, DecryptedFile>,
/// Upload state
uploading: bool,
/// Async operations
tasks: Vec<Task<Result<(), Error>>>,
/// Event subscriptions
subscriptions: SmallVec<[Subscription; 4]>,
}
@@ -253,6 +234,7 @@ impl ChatPanel {
input,
speech,
speech_started: None,
speaker: Speaker::new(),
subject_input,
subject_bar,
replies_to,
@@ -955,6 +937,20 @@ impl ChatPanel {
Command::Trace(id) => {
self.open_trace(id, window, cx);
}
Command::Speak(id) => {
let text = self.message(id).map(|message| message.preview());
if let Some(text) = text
&& let Err(e) = self.speaker.speak(&text)
{
window.push_notification(Notification::error(e.to_string()), cx);
}
}
Command::StopSpeaking => {
if let Err(error) = self.speaker.stop() {
window.push_notification(Notification::error(error.to_string()), cx);
}
}
}
}
@@ -1464,9 +1460,20 @@ impl ChatPanel {
actions
};
let speaking = self.speaker.speaking();
actions.dropdown(move |menu, _window, _cx| {
menu.menu("Copy author", Box::new(Command::Copy(public_key)))
let menu = menu
.menu("Copy author", Box::new(Command::Copy(public_key)))
.menu("Seen on", Box::new(Command::Trace(dropdown_id)))
.separator()
.menu("Read aloud", Box::new(Command::Speak(dropdown_id)));
if speaking {
menu.menu("Stop speaking", Box::new(Command::StopSpeaking))
} else {
menu
}
})
}
+8 -6
View File
@@ -26,6 +26,7 @@ use person::Directory;
use serde::Deserialize;
use settings::AppSettings;
use smallvec::{SmallVec, smallvec};
use speech::speaker::Speaker;
use state::{NostrRegistry, pick_file, upload_file};
use ui::{Avatar, RenderedText, WelcomeMessage, rendered_text};
use util::{attachment_name, display_name, opens_run, pin_label, report};
@@ -320,6 +321,8 @@ pub struct GroupPanel {
speech: Entity<SpeechState>,
/// When the running dictation started, for the elapsed counter
speech_started: Option<instant::Instant>,
/// Reads messages aloud
speaker: Speaker,
/// Uploaded, non-encrypted attachments waiting to be sent.
attachments: Vec<Url>,
/// Whether an upload is in flight.
@@ -419,9 +422,10 @@ impl GroupPanel {
}
},
));
this.track_participants(cx);
});
let mut this = Self {
Self {
focus_handle: cx.focus_handle(),
group: group.downgrade(),
directory: Directory::new(cx),
@@ -433,15 +437,12 @@ impl GroupPanel {
input,
speech,
speech_started: None,
speaker: Speaker::new(),
attachments: Vec::new(),
uploading: false,
tasks: smallvec![],
_subscriptions: subscriptions,
};
this.track_participants(cx);
this
}
}
/// Track the keys rendered by this panel: members, message authors, and
@@ -1059,6 +1060,7 @@ impl GroupPanel {
panel: cx.weak_entity(),
admin,
pinned,
speaking: self.speaker.speaking(),
persons: self.directory.clone(),
},
cx,
+40 -3
View File
@@ -8,7 +8,8 @@ use gpui::{
use gpui_kit::assets::IconName;
use gpui_kit::base::StyledExt as _;
use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenuItem};
use gpui_kit::component::{ActiveTheme, Icon, Sizable, h_flex};
use gpui_kit::component::notification::Notification;
use gpui_kit::component::{ActiveTheme, Icon, Sizable, WindowExt, h_flex};
use nip29::{Activity, Group, Reaction};
use nostr_sdk::prelude::{EventId, Kind};
use person::Directory;
@@ -26,6 +27,8 @@ pub(crate) struct Actions {
pub panel: WeakEntity<GroupPanel>,
pub admin: bool,
pub pinned: bool,
/// Whether the panel's speaker is currently saying something
pub speaking: bool,
pub persons: Entity<Directory>,
}
@@ -88,9 +91,9 @@ pub(crate) fn render(
let pinned = actions.pinned;
let group = actions.group;
let panel = actions.panel;
let speaking = actions.speaking;
let text = message.preview();
// Reading the panel here would re-enter its ongoing update, so resolve
// the current user's custom emojis through the shared person directory.
let custom = nostr
.read(cx)
.current_user()
@@ -139,6 +142,40 @@ pub(crate) fn render(
}
});
let menu = menu.separator().item({
let panel = panel.clone();
let text = text.clone();
PopupMenuItem::new("Read aloud").on_click(move |_event, _window, cx| {
if let Err(error) = panel.update_in(cx, |panel, window, cx| {
if let Err(error) = panel.speaker.speak(&text) {
window.push_notification(Notification::error(error.to_string()), cx);
}
}) {
log::warn!("nip29: reading a message aloud failed: {error}");
}
})
});
let menu = if speaking {
menu.item({
let panel = panel.clone();
PopupMenuItem::new("Stop speaking").on_click(move |_event, _window, cx| {
if let Err(error) = panel.update_in(cx, |panel, window, cx| {
if let Err(error) = panel.speaker.stop() {
window
.push_notification(Notification::error(error.to_string()), cx);
}
}) {
log::warn!("nip29: stopping the speaker failed: {error}");
}
})
})
} else {
menu
};
if !admin {
return menu;
}
+1
View File
@@ -11,6 +11,7 @@ gpui.workspace = true
gpui-kit = { workspace = true, features = ["speech"] }
flume.workspace = true
instant.workspace = true
tts.workspace = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
common = { path = "../common" }
+1
View File
@@ -2,6 +2,7 @@
pub mod model;
#[cfg(not(target_arch = "wasm32"))]
mod parakeet;
pub mod speaker;
#[cfg(target_arch = "wasm32")]
mod web;
+47
View File
@@ -0,0 +1,47 @@
use tts::Tts;
/// Reads text aloud through the platform's speech backend, created on first use.
pub struct Speaker {
tts: Option<Tts>,
}
impl Default for Speaker {
fn default() -> Self {
Self::new()
}
}
impl Speaker {
pub fn new() -> Self {
Self { tts: None }
}
/// Speak the text, interrupting what this speaker is already saying.
pub fn speak(&mut self, text: &str) -> Result<(), tts::Error> {
let tts = match self.tts.as_ref() {
Some(tts) => tts,
None => self.tts.insert(Tts::default()?),
};
tts.speak(text, true)?;
Ok(())
}
/// Stop speaking; a speaker that never spoke is already silent.
pub fn stop(&mut self) -> Result<(), tts::Error> {
match self.tts.as_ref() {
Some(tts) => tts.stop()?,
None => return Ok(()),
}
Ok(())
}
/// Best-effort speaking state; a speaker that never spoke is silent.
pub fn speaking(&self) -> bool {
self.tts
.as_ref()
.and_then(|tts| tts.is_speaking().ok())
.unwrap_or(false)
}
}