8 Commits
Author SHA1 Message Date
reya 690dc37fc5 chore: fix web build 2026-11-01 16:40:49 +07:00
reya 2f2661ecd2 chore: improve native theme usage 2026-11-01 16:15:28 +07:00
reya d045a7ef3a chore: update the unlock dialog 2026-10-30 20:17:37 +07:00
reya 23d7017f25 feat: add text to speech (#69)
Reviewed-on: #69
2026-10-30 07:16:46 +00:00
reya af2a276b1b chore: update deps 2026-10-28 10:08:47 +07:00
reya 69ca199a7d chore: drop macos x64 from release builds 2026-10-28 09:09:18 +07:00
reya 83a440d893 chore: release version 2.0.0-alpha3 2026-10-28 05:14:03 +07:00
reya 77b2a5f3cd feat: add support for speech to text (#68)
Reviewed-on: #68
2026-10-27 06:25:19 +00:00
41 changed files with 3018 additions and 614 deletions
-3
View File
@@ -20,9 +20,6 @@ jobs:
- platform: windows-arm64 - platform: windows-arm64
os: windows-11-arm os: windows-11-arm
target: aarch64-pc-windows-msvc target: aarch64-pc-windows-msvc
- platform: macos-x64
os: macos-15-intel
target: x86_64-apple-darwin
- platform: macos-arm64 - platform: macos-arm64
os: macos-latest os: macos-latest
target: aarch64-apple-darwin target: aarch64-apple-darwin
+3 -2
View File
@@ -39,8 +39,9 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Install Rust nightly - name: Install Rust nightly
uses: dtolnay/rust-toolchain@nightly uses: dtolnay/rust-toolchain@master
with: with:
toolchain: nightly-2026-10-31
targets: wasm32-unknown-unknown targets: wasm32-unknown-unknown
components: llvm-tools-preview components: llvm-tools-preview
@@ -51,4 +52,4 @@ jobs:
env: env:
CC_wasm32_unknown_unknown: clang CC_wasm32_unknown_unknown: clang
AR_wasm32_unknown_unknown: llvm-ar AR_wasm32_unknown_unknown: llvm-ar
run: cargo +nightly check -p coop_web --target wasm32-unknown-unknown run: cargo +nightly-2026-10-31 check -p coop_web --target wasm32-unknown-unknown
Generated
+1193 -237
View File
File diff suppressed because it is too large Load Diff
+15 -15
View File
@@ -4,27 +4,26 @@ members = ["crates/*", "desktop", "web"]
default-members = ["desktop"] default-members = ["desktop"]
[workspace.package] [workspace.package]
version = "2.0.0-alpha2" version = "2.0.0-alpha3"
edition = "2024" edition = "2024"
publish = false publish = false
[workspace.dependencies] [workspace.dependencies]
gpui = { package = "gpui-pre", version = "0.3.8" } gpui = { package = "gpui-pre", version = "0.3.10" }
gpui_platform = { package = "gpui-pre-platform", version = "0.3.8", features = ["font-kit", "x11", "wayland"] } gpui_platform = { package = "gpui-pre-platform", version = "0.3.10", features = ["font-kit", "x11", "wayland"] }
gpui_linux = { package = "gpui-pre-linux", version = "0.3.8" } gpui_linux = { package = "gpui-pre-linux", version = "0.3.10" }
gpui_windows = { package = "gpui-pre-windows", version = "0.3.8" } gpui_windows = { package = "gpui-pre-windows", version = "0.3.10" }
gpui_macos = { package = "gpui-pre-macos", version = "0.3.8" } gpui_macos = { package = "gpui-pre-macos", version = "0.3.10" }
gpui_web = { package = "gpui-pre-web", version = "0.3.8" } gpui_web = { package = "gpui-pre-web", version = "0.3.10" }
gpui_util = { package = "gpui-pre-util", version = "0.3.8" } gpui_util = { package = "gpui-pre-util", version = "0.3.10" }
reqwest_client = { package = "gpui-pre-reqwest-client", version = "0.3.8" } reqwest_client = { package = "gpui-pre-reqwest-client", version = "0.3.10" }
gpui_tokio = { path = "crates/gpui_tokio" } gpui_tokio = { path = "crates/gpui_tokio" }
gpui-base = "0.7.1"
gpui-kit = "0.7.3"
# Native OS theme reading; the connector is written against gpui-component 0.7.x gpui-base = "0.8.1"
# and gpui-pre 0.3.8, both satisfied by the versions above (0.7.3 / 0.3.9). gpui-kit = "0.8.1"
native-theme = { version = "0.6.1", features = ["watch"] }
native-theme-gpui = { version = "0.6.1", default-features = false, features = ["system-fonts"] } native-theme = { version = "0.7", features = ["native", "watch"] }
native-theme-gpui = { version = "0.7", default-features = false, features = ["system-fonts"] }
# Nostr # Nostr
nostr-lmdb = { git = "https://github.com/rust-nostr/nostr" } nostr-lmdb = { git = "https://github.com/rust-nostr/nostr" }
@@ -51,6 +50,7 @@ rand = { version = "0.10", default-features = false, features = [ "std", "sys_rn
# Others # Others
anyhow = "1.0.44" anyhow = "1.0.44"
tts = { git = "https://github.com/ndarilek/tts-rs" }
chrono = { version = "0.4.38", features = ["wasmbind"] } chrono = { version = "0.4.38", features = ["wasmbind"] }
futures = "0.3" futures = "0.3"
itertools = "0.13.0" itertools = "0.13.0"
+3
View File
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">
<path d="M12.2126 10.7642L7.07919 15.8829C6.51216 16.4483 6.51216 17.365 7.07919 17.9304C7.64621 18.4958 8.56554 18.4958 9.13257 17.9304L19.3995 7.69303C19.9664 7.12769 20.25 6.38673 20.25 5.64575C20.25 4.90465 19.9665 4.16353 19.3995 3.59809C18.8324 3.03271 18.0893 2.75001 17.3461 2.75C16.6029 2.74999 15.8597 3.03269 15.2927 3.59809L5.02581 13.8354C4.17522 14.6836 3.74995 15.7952 3.75 16.9068C3.75005 18.0183 4.17532 19.1298 5.02581 19.9779C5.87635 20.826 6.99113 21.25 8.10591 21.25C9.22067 21.25 10.3354 20.8259 11.1859 19.9779L16.3194 14.8592" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
</svg>

After

Width:  |  Height:  |  Size: 697 B

+3
View File
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">
<path d="M10.75 14.75V17.25" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M14.25 7.75V20.25" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M17.75 10.75V17.25" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M21.25 12.75V15.25" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M2.75 5.25V3.75H7M11.25 5.25V3.75H7M7 3.75V12.25M7 12.25H5.25M7 12.25H8.75" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 726 B

-39
View File
@@ -1,39 +0,0 @@
use gpui::AssetSource;
#[test]
fn icons_resolve_from_the_vendored_folder() {
// Icons coop's code and the kit components draw, served by the vendored
// folder (signed's set, filled with the kit bundle where coop needs more).
let vendored = [
"icons/search.svg",
"icons/close.svg",
"icons/panel-left.svg",
"icons/window-minimize.svg",
"icons/settings-2.svg",
"icons/x.svg",
"icons/compass.svg",
"icons/radio-tower.svg",
];
for path in vendored {
let own = assets::Assets
.load(path)
.unwrap_or_else(|e| panic!("{path} errored in own embed: {e}"));
assert!(own.is_some(), "own embed is missing {path}");
let combined = assets::CombinedAssets.load(path).unwrap();
assert!(combined.is_some(), "CombinedAssets fails to serve {path}");
}
// Coop's own files still load through the combined source.
let own = [
"brand/coop.svg",
"brand/bg1.jpg",
"fonts/Inter/Inter-Medium.ttf",
];
for path in own {
assert!(
matches!(assets::CombinedAssets.load(path), Ok(Some(_))),
"CombinedAssets fails to serve coop asset {path}"
);
}
}
+3 -3
View File
@@ -37,9 +37,8 @@ pub fn asset_filter_for(os: &str, arch: &str) -> Option<AssetFilter> {
}; };
let arch = match (os, arch) { let arch = match (os, arch) {
// cargo-packager names the disk images `aarch64`/`x64`. // cargo-packager names the disk images `aarch64`.
("macos", "aarch64") => "aarch64", ("macos", "aarch64") => "aarch64",
("macos", "x86_64") => "x64",
// `script/bundle-linux` names the tarballs `aarch64`/`x86_64`. // `script/bundle-linux` names the tarballs `aarch64`/`x86_64`.
("linux", "aarch64") => "aarch64", ("linux", "aarch64") => "aarch64",
("linux", "x86_64") => "x86_64", ("linux", "x86_64") => "x86_64",
@@ -243,7 +242,7 @@ mod tests {
#[test] #[test]
fn picks_the_artifact_matching_os_and_architecture() { fn picks_the_artifact_matching_os_and_architecture() {
assert_eq!(selected("macos", "aarch64"), Some("Coop_1.0.1_aarch64.dmg")); assert_eq!(selected("macos", "aarch64"), Some("Coop_1.0.1_aarch64.dmg"));
assert_eq!(selected("macos", "x86_64"), Some("Coop_1.0.1_x64.dmg")); assert_eq!(selected("macos", "x86_64"), None);
assert_eq!( assert_eq!(
selected("linux", "aarch64"), selected("linux", "aarch64"),
Some("coop-linux-aarch64.tar.gz") Some("coop-linux-aarch64.tar.gz")
@@ -258,6 +257,7 @@ mod tests {
fn has_no_target_for_windows_or_unknown_platforms() { fn has_no_target_for_windows_or_unknown_platforms() {
assert_eq!(asset_filter_for("windows", "x86_64"), None); assert_eq!(asset_filter_for("windows", "x86_64"), None);
assert_eq!(asset_filter_for("freebsd", "x86_64"), None); assert_eq!(asset_filter_for("freebsd", "x86_64"), None);
assert_eq!(asset_filter_for("macos", "x86_64"), None);
assert_eq!(asset_filter_for("macos", "riscv64"), None); assert_eq!(asset_filter_for("macos", "riscv64"), None);
} }
+3 -1
View File
@@ -6,14 +6,16 @@ publish.workspace = true
[dependencies] [dependencies]
state = { path = "../state" } state = { path = "../state" }
speech = { path = "../speech" }
ui = { path = "../ui" } ui = { path = "../ui" }
gpui-kit.workspace = true gpui-kit = { workspace = true, features = ["speech"] }
common = { path = "../common" } common = { path = "../common" }
person = { path = "../person" } person = { path = "../person" }
chat = { path = "../chat" } chat = { path = "../chat" }
settings = { path = "../settings" } settings = { path = "../settings" }
gpui.workspace = true gpui.workspace = true
instant.workspace = true
nostr-sdk.workspace = true nostr-sdk.workspace = true
anyhow.workspace = true anyhow.workspace = true
itertools.workspace = true itertools.workspace = true
+4 -1
View File
@@ -1,4 +1,4 @@
use gpui::Action; use gpui::{Action, actions};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use serde::Deserialize; use serde::Deserialize;
use settings::SignerKind; use settings::SignerKind;
@@ -15,3 +15,6 @@ pub enum Command {
Njump(PublicKey), Njump(PublicKey),
Trace(EventId), Trace(EventId),
} }
// Start or stop dictation in the focused composer
actions!(chat, [ToggleDictation]);
+209 -56
View File
@@ -1,5 +1,6 @@
use std::collections::{BTreeMap, HashMap, HashSet}; use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::{Arc, LazyLock, RwLock}; use std::sync::{Arc, LazyLock, RwLock};
use std::time::Duration;
pub use actions::*; pub use actions::*;
use anyhow::Error; use anyhow::Error;
@@ -20,6 +21,7 @@ use gpui_kit::component::input::{Input, InputEvent, InputState, Textarea, Textar
use gpui_kit::component::menu::DropdownMenu; use gpui_kit::component::menu::DropdownMenu;
use gpui_kit::component::notification::Notification; use gpui_kit::component::notification::Notification;
use gpui_kit::component::scroll::Scrollbar; use gpui_kit::component::scroll::Scrollbar;
use gpui_kit::component::speech::{SpeechButton, SpeechEvent, SpeechState, SpeechWaveform};
use gpui_kit::component::tooltip::Tooltip; use gpui_kit::component::tooltip::Tooltip;
use gpui_kit::component::{ActiveTheme, Disableable, Icon, Sizable, WindowExt, h_flex, v_flex}; use gpui_kit::component::{ActiveTheme, Disableable, Icon, Sizable, WindowExt, h_flex, v_flex};
use itertools::Itertools; use itertools::Itertools;
@@ -28,13 +30,14 @@ use person::{Directory, Person};
use regex::Regex; use regex::Regex;
use settings::{AppSettings, SignerKind}; use settings::{AppSettings, SignerKind};
use smallvec::{SmallVec, smallvec}; use smallvec::{SmallVec, smallvec};
use speech::speaker::{END_GRACE, Speaker};
use state::{ use state::{
FileAttachment, LocalFile, NostrRegistry, PickedFile, download_and_decrypt_file, pick_file, FileAttachment, LocalFile, NostrRegistry, PickedFile, download_and_decrypt_file, pick_file,
}; };
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::*;
@@ -43,8 +46,7 @@ use crate::file::*;
const PRIVATE_NOTICE: &str = const PRIVATE_NOTICE: &str =
"This conversation is private. Only members can see each other's messages."; "This conversation is private. Only members can see each other's messages.";
/// Regex matching strings that consist entirely of emoji characters, /// Regex matching strings that consist entirely of emoji characters.
/// zero-width joiners, variation selectors, and keycap combiners.
static EMOJI_RE: LazyLock<Regex> = static EMOJI_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[\p{Emoji}\u{200D}\u{FE0F}\u{20E3}]+$").unwrap()); LazyLock::new(|| Regex::new(r"^[\p{Emoji}\u{200D}\u{FE0F}\u{20E3}]+$").unwrap());
@@ -63,60 +65,50 @@ pub fn init(room: WeakEntity<Room>, window: &mut Window, cx: &mut App) -> Entity
pub struct ChatPanel { pub struct ChatPanel {
id: SharedString, id: SharedString,
focus_handle: FocusHandle, focus_handle: FocusHandle,
/// Chat Room /// Chat Room
room: WeakEntity<Room>, room: WeakEntity<Room>,
/// Person directory shared with the room /// Person directory shared with the room
directory: Entity<Directory>, directory: Entity<Directory>,
/// Message list state /// Message list state
list_state: ListState, list_state: ListState,
/// All messages (sorted by created_at) /// All messages (sorted by created_at)
messages: Vec<Message>, messages: Vec<Message>,
/// O(1) message lookup by EventId /// O(1) message lookup by EventId
message_index: HashMap<EventId, usize>, message_index: HashMap<EventId, usize>,
/// All reactions /// All reactions
reactions: BTreeMap<EventId, Vec<(SharedString, Option<SharedUri>, PublicKey)>>, reactions: BTreeMap<EventId, Vec<(SharedString, Option<SharedUri>, PublicKey)>>,
/// Mapping message ids to their rendered texts /// Mapping message ids to their rendered texts
rendered_texts_by_id: BTreeMap<EventId, RenderedText>, rendered_texts_by_id: BTreeMap<EventId, RenderedText>,
/// Mapping message (rumor event) ids to their reports /// Mapping message (rumor event) ids to their reports
reports_by_id: Arc<RwLock<BTreeMap<EventId, Vec<SendReport>>>>, reports_by_id: Arc<RwLock<BTreeMap<EventId, Vec<SendReport>>>>,
/// Chat input state /// Chat input state
input: Entity<TextareaState>, 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,
/// The message being revealed in step with speech, with its estimated duration
speech_reveal: Option<(EventId, Duration)>,
/// Fallback reveal end for backends that cannot report utterance end
reveal_timeout: Option<Task<Result<(), Error>>>,
/// Subject input state /// Subject input state
subject_input: Entity<InputState>, subject_input: Entity<InputState>,
/// Subject bar visibility /// Subject bar visibility
subject_bar: Entity<bool>, subject_bar: Entity<bool>,
/// Replies to /// Replies to
replies_to: Entity<HashSet<EventId>>, replies_to: Entity<HashSet<EventId>>,
/// Media Attachment /// Media Attachment
attachments: Entity<Vec<Url>>, attachments: Entity<Vec<Url>>,
/// Uploaded, encrypted file attachments which are not sent yet /// Uploaded, encrypted file attachments which are not sent yet
encrypted_attachments: Entity<Vec<PendingFile>>, encrypted_attachments: Entity<Vec<PendingFile>>,
/// Decrypted attachments of file messages, by message id /// Decrypted attachments of file messages, by message id
decrypted_files: HashMap<EventId, DecryptedFile>, decrypted_files: HashMap<EventId, DecryptedFile>,
/// Upload state /// Upload state
uploading: bool, uploading: bool,
/// Async operations
tasks: Vec<Task<Result<(), Error>>>, tasks: Vec<Task<Result<(), Error>>>,
subscriptions: SmallVec<[Subscription; 4]>,
/// Event subscriptions
subscriptions: SmallVec<[Subscription; 3]>,
} }
impl ChatPanel { impl ChatPanel {
@@ -160,6 +152,19 @@ impl ChatPanel {
.clean_on_escape() .clean_on_escape()
}); });
// Define dictation state
let speech = cx.new(|cx| {
let state = SpeechState::new(cx)
.recognizer(speech::recognizer())
// Batch transcription of a long recording can take a while.
.stop_timeout(Duration::from_secs(180));
#[cfg(target_arch = "wasm32")]
let state = state.input(speech::input());
state
});
// Define subject input state // Define subject input state
let subject_input = cx.new(|cx| InputState::new(window, cx).placeholder("New subject...")); let subject_input = cx.new(|cx| InputState::new(window, cx).placeholder("New subject..."));
let subject_bar = cx.new(|_cx| false); let subject_bar = cx.new(|_cx| false);
@@ -178,15 +183,39 @@ impl ChatPanel {
subscriptions.push( subscriptions.push(
// Subscribe the subject input event // Subscribe the subject input event
cx.subscribe_in( cx.subscribe_in(&subject_input, window, move |this, _, event, window, cx| {
&subject_input, if let InputEvent::PressEnter { .. } = event {
window, this.change_subject(window, cx);
move |this, _input, event, window, cx| { };
if let InputEvent::PressEnter { .. } = event { }),
this.change_subject(window, cx); );
};
}, subscriptions.push(
), // Insert the dictated text into the chat input
cx.subscribe_in(&speech, window, move |this, _speech, event, window, cx| {
match event {
SpeechEvent::Started => {
this.speech_started = Some(instant::Instant::now());
cx.notify();
}
SpeechEvent::Final(text) => {
this.speech_started = None;
if !text.is_empty() {
this.input
.update(cx, |input, cx| input.insert(text.clone(), window, cx));
}
}
SpeechEvent::Error(error) => {
this.speech_started = None;
window.push_notification(Notification::error(error.to_string()), cx);
}
SpeechEvent::Cancelled => {
this.speech_started = None;
}
_ => {}
};
}),
); );
// Define all functions that will run after the current cycle // Define all functions that will run after the current cycle
@@ -196,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,
@@ -206,6 +249,11 @@ impl ChatPanel {
directory, directory,
list_state, list_state,
input, input,
speech,
speech_started: None,
speaker,
speech_reveal: None,
reveal_timeout: None,
subject_input, subject_input,
subject_bar, subject_bar,
replies_to, replies_to,
@@ -216,7 +264,7 @@ impl ChatPanel {
reports_by_id, reports_by_id,
uploading: false, uploading: false,
subscriptions, subscriptions,
tasks: vec![], tasks: vec![reveal_end_task],
} }
} }
@@ -835,6 +883,17 @@ impl ChatPanel {
self.directory.read(cx).person(public_key, cx) self.directory.read(cx).person(public_key, cx)
} }
fn toggle_dictation(
&mut self,
_: &ToggleDictation,
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.speech.update(cx, |speech, cx| {
speech.toggle(cx);
});
}
fn on_command(&mut self, command: &Command, window: &mut Window, cx: &mut Context<Self>) { fn on_command(&mut self, command: &Command, window: &mut Window, cx: &mut Context<Self>) {
match command { match command {
Command::Insert(content) => { Command::Insert(content) => {
@@ -1134,6 +1193,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)
@@ -1158,7 +1226,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))
@@ -1390,21 +1458,63 @@ impl ChatPanel {
} }
let reply_id = *id; let reply_id = *id;
let copy_id = *id;
let dropdown_id = *id; let dropdown_id = *id;
let public_key = *public_key; let public_key = *public_key;
actions let actions = actions.reply(cx.listener(move |this, _event, _window, cx| {
.reply(cx.listener(move |this, _event, _window, cx| { this.reply_to(&reply_id, cx);
this.reply_to(&reply_id, cx); }));
}))
.copy(cx.listener(move |this, _event, _window, cx| { let actions = if let Some(message) = self.message(id) {
this.copy_message(&copy_id, cx); actions.copy(
})) SharedString::from(format!("message-copy-{id}")),
.dropdown(move |menu, _window, _cx| { message.preview(),
menu.menu("Copy author", Box::new(Command::Copy(public_key))) )
.menu("Seen on", Box::new(Command::Trace(dropdown_id))) } else {
}) actions
};
let speaking = self.speech_reveal.is_some();
let speech_id = *id;
let actions = actions.speech(
speaking,
cx.listener(move |this, _event, window, cx| {
let text = this.message(&speech_id).map(|message| message.preview());
if let Some(text) = text {
match this.speaker.speak(&text) {
Ok(duration) => {
this.speech_reveal = Some((speech_id, duration));
// Re-enable speaking when no end event ever arrives
let session = (speech_id, duration);
let timer = cx.background_executor().timer(duration + END_GRACE);
this.reveal_timeout = Some(cx.spawn(async move |this, cx| {
timer.await;
this.update(cx, |this, cx| {
if this.speech_reveal.as_ref() == Some(&session) {
this.speech_reveal = None;
cx.notify();
}
})?;
Ok(())
}));
cx.notify();
}
Err(error) => {
window.push_notification(Notification::error(error.to_string()), cx)
}
}
}
}),
);
actions.dropdown(move |menu, _window, _cx| {
menu.menu("Copy author", Box::new(Command::Copy(public_key)))
.menu("Seen on", Box::new(Command::Trace(dropdown_id)))
})
} }
fn render_attachment(&self, url: &Url, cx: &Context<Self>) -> impl IntoElement { fn render_attachment(&self, url: &Url, cx: &Context<Self>) -> impl IntoElement {
@@ -1669,7 +1779,12 @@ impl ChatPanel {
items items
} }
fn render_config_menu(&self, _window: &mut Window, cx: &Context<Self>) -> impl IntoElement { fn render_config_menu(
&self,
disabled: bool,
_window: &mut Window,
cx: &Context<Self>,
) -> impl IntoElement {
let (backup, signer_kind) = self let (backup, signer_kind) = self
.room .room
.read_with(cx, |this, _cx| { .read_with(cx, |this, _cx| {
@@ -1682,6 +1797,7 @@ impl ChatPanel {
.icon(IconName::Settings2) .icon(IconName::Settings2)
.tooltip("Configuration") .tooltip("Configuration")
.ghost() .ghost()
.disabled(disabled)
.dropdown_menu(move |this, _window, _cx| { .dropdown_menu(move |this, _window, _cx| {
let auto = matches!(signer_kind, SignerKind::Auto); let auto = matches!(signer_kind, SignerKind::Auto);
let encryption = matches!(signer_kind, SignerKind::Encryption); let encryption = matches!(signer_kind, SignerKind::Encryption);
@@ -1712,7 +1828,12 @@ impl ChatPanel {
}) })
} }
fn render_emoji_menu(&self, _window: &Window, cx: &Context<Self>) -> impl IntoElement { fn render_emoji_menu(
&self,
disabled: bool,
_window: &Window,
cx: &Context<Self>,
) -> impl IntoElement {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let current_user = nostr.read(cx).current_user(); let current_user = nostr.read(cx).current_user();
@@ -1729,6 +1850,7 @@ impl ChatPanel {
Button::new("emoji") Button::new("emoji")
.icon(IconName::Emoji) .icon(IconName::Emoji)
.ghost() .ghost()
.disabled(disabled)
.dropdown_menu_with_anchor(gpui::Anchor::BottomLeft, move |this, _window, _cx| { .dropdown_menu_with_anchor(gpui::Anchor::BottomLeft, move |this, _window, _cx| {
let menu = REACTION_EMOJIS.iter().fold(this, |this, emoji| { let menu = REACTION_EMOJIS.iter().fold(this, |this, emoji| {
this.menu(*emoji, Box::new(Command::Insert(emoji.to_string()))) this.menu(*emoji, Box::new(Command::Insert(emoji.to_string())))
@@ -1802,10 +1924,12 @@ impl Render for ChatPanel {
let is_typing = !self.input.read(cx).value().trim().is_empty(); let is_typing = !self.input.read(cx).value().trim().is_empty();
let pending_attachments = !self.encrypted_attachments.read(cx).is_empty(); let pending_attachments = !self.encrypted_attachments.read(cx).is_empty();
let dictating = self.speech.read(cx).status().is_active();
v_flex() v_flex()
.image_cache(retain_all(self.id.clone())) .image_cache(retain_all(self.id.clone()))
.on_action(cx.listener(Self::on_command)) .on_action(cx.listener(Self::on_command))
.on_action(cx.listener(Self::toggle_dictation))
.size_full() .size_full()
.when(*self.subject_bar.read(cx), |this| { .when(*self.subject_bar.read(cx), |this| {
this.child( this.child(
@@ -1879,26 +2003,55 @@ impl Render for ChatPanel {
.items_end() .items_end()
.child( .child(
Button::new("upload") Button::new("upload")
.icon(IconName::Plus) .icon(IconName::Paperclip)
.tooltip("Upload media") .tooltip("Upload media")
.loading(self.uploading) .loading(self.uploading)
.disabled(self.uploading) .disabled(dictating || self.uploading)
.ghost() .ghost()
.on_click(cx.listener(move |this, _ev, window, cx| { .on_click(cx.listener(move |this, _ev, window, cx| {
this.upload(window, cx); this.upload(window, cx);
})), })),
) )
.child(Textarea::new(&self.input).appearance(false).flex_1()) .when(dictating, |this| {
this.child(
h_flex()
.h_8()
.flex_1()
.gap_2()
.pl_1()
.child(SpeechWaveform::new(&self.speech).small().flex_1())
.when_some(self.speech_started, |this, started| {
this.child(speech::elapsed(started, cx))
}),
)
})
.when(!dictating, |this| {
this.child(
Textarea::new(&self.input)
.appearance(false)
.small()
.min_h_8()
.py_1()
.flex_1(),
)
})
.child(
h_flex()
.items_end()
.gap_1()
.pl_1()
.child(SpeechButton::new(&self.speech)),
)
.child( .child(
h_flex() h_flex()
.pl_1() .pl_1()
.gap_1() .gap_1()
.child(self.render_emoji_menu(window, cx)) .child(self.render_emoji_menu(dictating, window, cx))
.child(self.render_config_menu(window, cx)) .child(self.render_config_menu(dictating, window, cx))
.child( .child(
Button::new("send") Button::new("send")
.icon(IconName::PaperPlaneFill) .icon(IconName::PaperPlaneFill)
.disabled(self.uploading) .disabled(dictating || self.uploading)
.ghost() .ghost()
.on_click(cx.listener(move |this, _ev, window, cx| { .on_click(cx.listener(move |this, _ev, window, cx| {
this.send_text_message(window, cx); this.send_text_message(window, cx);
+3 -1
View File
@@ -11,11 +11,13 @@ common = { path = "../common" }
nip29 = { path = "../nip29" } nip29 = { path = "../nip29" }
person = { path = "../person" } person = { path = "../person" }
settings = { path = "../settings" } settings = { path = "../settings" }
speech = { path = "../speech" }
state = { path = "../state" } state = { path = "../state" }
gpui-kit.workspace = true gpui-kit = { workspace = true, features = ["speech"] }
ui = { path = "../ui" } ui = { path = "../ui" }
gpui.workspace = true gpui.workspace = true
instant.workspace = true
nostr-sdk.workspace = true nostr-sdk.workspace = true
anyhow.workspace = true anyhow.workspace = true
log.workspace = true log.workspace = true
+149 -33
View File
@@ -1,11 +1,14 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::time::Duration;
use anyhow::Result; use anyhow::Result;
use chat_ui::ToggleDictation;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
Action, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, Action, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
FollowMode, IntoElement, ListAlignment, ListScrollEvent, ListState, ParentElement, Render, FollowMode, InteractiveElement, IntoElement, ListAlignment, ListScrollEvent, ListState,
SharedString, Styled, Subscription, Task, WeakEntity, Window, div, list, px, ParentElement, Render, SharedString, Styled, Subscription, Task, WeakEntity, Window, div, list,
px,
}; };
use gpui_kit::assets::IconName; use gpui_kit::assets::IconName;
use gpui_kit::base::StyledExt as _; use gpui_kit::base::StyledExt as _;
@@ -15,6 +18,7 @@ use gpui_kit::component::dock::{Panel, PanelEvent};
use gpui_kit::component::input::{InputEvent, Textarea, TextareaState}; use gpui_kit::component::input::{InputEvent, Textarea, TextareaState};
use gpui_kit::component::notification::Notification; use gpui_kit::component::notification::Notification;
use gpui_kit::component::scroll::Scrollbar; use gpui_kit::component::scroll::Scrollbar;
use gpui_kit::component::speech::{SpeechButton, SpeechEvent, SpeechState, SpeechWaveform};
use gpui_kit::component::{ActiveTheme, Disableable, Icon, Sizable, WindowExt, h_flex, v_flex}; use gpui_kit::component::{ActiveTheme, Disableable, Icon, Sizable, WindowExt, h_flex, v_flex};
use nip29::{Group, GroupEvent, GroupKey, GroupsRegistry, Membership, Pin, Row}; use nip29::{Group, GroupEvent, GroupKey, GroupsRegistry, Membership, Pin, Row};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
@@ -22,8 +26,9 @@ use person::Directory;
use serde::Deserialize; use serde::Deserialize;
use settings::AppSettings; use settings::AppSettings;
use smallvec::{SmallVec, smallvec}; use smallvec::{SmallVec, smallvec};
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;
@@ -312,6 +317,16 @@ pub struct GroupPanel {
reply_to: Option<EventId>, reply_to: Option<EventId>,
rendered_texts_by_id: BTreeMap<EventId, RenderedText>, rendered_texts_by_id: BTreeMap<EventId, RenderedText>,
input: Entity<TextareaState>, input: Entity<TextareaState>,
/// Dictation session for the composer input
speech: Entity<SpeechState>,
/// When the running dictation started, for the elapsed counter
speech_started: Option<instant::Instant>,
/// Reads messages aloud
speaker: Speaker,
/// The message being revealed in step with speech, with its estimated duration
speech_reveal: Option<(EventId, Duration)>,
/// Fallback reveal end for backends that cannot report utterance end
reveal_timeout: Option<Task<Result<()>>>,
/// 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.
@@ -319,7 +334,7 @@ pub struct GroupPanel {
/// Whether the user closed the encryption notice this visit /// Whether the user closed the encryption notice this visit
notice_dismissed: bool, notice_dismissed: bool,
tasks: SmallVec<[Task<Result<()>>; 4]>, tasks: SmallVec<[Task<Result<()>>; 4]>,
_subscriptions: SmallVec<[Subscription; 3]>, _subscriptions: SmallVec<[Subscription; 4]>,
} }
impl GroupPanel { impl GroupPanel {
@@ -338,6 +353,18 @@ impl GroupPanel {
.clean_on_escape() .clean_on_escape()
}); });
let speech = cx.new(|cx| {
let state = SpeechState::new(cx)
.recognizer(speech::recognizer())
// Batch transcription of a long recording can take a while.
.stop_timeout(Duration::from_secs(180));
#[cfg(target_arch = "wasm32")]
let state = state.input(speech::input());
state
});
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
subscriptions.push( subscriptions.push(
@@ -348,6 +375,34 @@ impl GroupPanel {
}), }),
); );
subscriptions.push(
cx.subscribe_in(&speech, window, |this, _, event, window, cx| {
match event {
SpeechEvent::Started => {
this.speech_started = Some(instant::Instant::now());
cx.notify();
}
SpeechEvent::Final(text) => {
this.speech_started = None;
if !text.is_empty() {
this.input.update(cx, |input, cx| {
input.insert(text.clone(), window, cx);
});
}
}
SpeechEvent::Error(error) => {
this.speech_started = None;
window.push_notification(Notification::error(error.to_string()), cx);
}
SpeechEvent::Cancelled => {
this.speech_started = None;
}
_ => {}
};
}),
);
subscriptions.push(cx.subscribe(&group, |this, _group, event, cx| { subscriptions.push(cx.subscribe(&group, |this, _group, event, cx| {
if !matches!(event, GroupEvent::Sent(_)) { if !matches!(event, GroupEvent::Sent(_)) {
this.sync(cx); this.sync(cx);
@@ -371,9 +426,24 @@ impl GroupPanel {
} }
}, },
)); ));
this.track_participants(cx);
}); });
let mut this = Self { // 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 {
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
group: group.downgrade(), group: group.downgrade(),
directory: Directory::new(cx), directory: Directory::new(cx),
@@ -383,19 +453,19 @@ impl GroupPanel {
notice_dismissed: false, notice_dismissed: false,
rendered_texts_by_id: BTreeMap::new(), rendered_texts_by_id: BTreeMap::new(),
input, input,
speech,
speech_started: None,
speaker,
speech_reveal: None,
reveal_timeout: None,
attachments: Vec::new(), attachments: Vec::new(),
uploading: false, uploading: false,
tasks: smallvec![], tasks: smallvec![reveal_end_task],
_subscriptions: subscriptions, _subscriptions: subscriptions,
}; }
this.track_participants(cx);
this
} }
/// Track the keys rendered by this panel: members, message authors, and /// Track the keys rendered by this panel.
/// activity subjects
fn track_participants(&mut self, cx: &mut Context<Self>) { fn track_participants(&mut self, cx: &mut Context<Self>) {
let Some(group) = self.group.upgrade() else { let Some(group) = self.group.upgrade() else {
return; return;
@@ -973,22 +1043,29 @@ 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
.rendered_texts_by_id .speech_reveal
.entry(message.id) .as_ref()
.or_insert_with(|| { .filter(|(reveal_id, _)| *reveal_id == message.id)
rendered_text( {
&message.content, Some((_, duration)) => RevealText::new(message.preview(), *duration).into_any_element(),
message None => self
.mentions .rendered_texts_by_id
.iter() .entry(message.id)
.map(|mention| (mention.range.clone(), mention.public_key)), .or_insert_with(|| {
&message.emojis, rendered_text(
&directory, &message.content,
cx, message
) .mentions
}) .iter()
.element(ix.into(), window, cx); .map(|mention| (mention.range.clone(), mention.public_key)),
&message.emojis,
&directory,
cx,
)
})
.element(ix.into(), window, cx),
};
let admin = nostr let admin = nostr
.read(cx) .read(cx)
@@ -1009,12 +1086,24 @@ impl GroupPanel {
panel: cx.weak_entity(), panel: cx.weak_entity(),
admin, admin,
pinned, pinned,
speaking: self.speech_reveal.is_some(),
persons: self.directory.clone(), persons: self.directory.clone(),
}, },
cx, cx,
) )
} }
fn toggle_dictation(
&mut self,
_ev: &ToggleDictation,
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.speech.update(cx, |speech, cx| {
speech.toggle(cx);
});
}
fn render_composer(&self, cx: &mut Context<Self>) -> AnyElement { fn render_composer(&self, cx: &mut Context<Self>) -> AnyElement {
let notice = self.group.upgrade().and_then(|group| { let notice = self.group.upgrade().and_then(|group| {
let group = group.read(cx); let group = group.read(cx);
@@ -1040,6 +1129,8 @@ impl GroupPanel {
return self.render_notice(&notice, cx); return self.render_notice(&notice, cx);
} }
let dictating = self.speech.read(cx).status().is_active();
v_flex() v_flex()
.flex_shrink_0() .flex_shrink_0()
.w_full() .w_full()
@@ -1055,21 +1146,45 @@ impl GroupPanel {
.items_end() .items_end()
.child( .child(
Button::new("upload") Button::new("upload")
.icon(IconName::Upload) .icon(ui::IconName::Paperclip)
.tooltip("Upload file") .tooltip("Upload media")
.loading(self.uploading) .loading(self.uploading)
.disabled(self.uploading) .disabled(dictating || self.uploading)
.ghost() .ghost()
.on_click(cx.listener(|this, _event, window, cx| { .on_click(cx.listener(|this, _event, window, cx| {
this.upload(window, cx); this.upload(window, cx);
})), })),
) )
.child(Textarea::new(&self.input).appearance(false).flex_1()) .when(dictating, |this| {
this.child(
h_flex()
.h_8()
.flex_1()
.gap_2()
.child(SpeechWaveform::new(&self.speech).small().flex_1())
.when_some(self.speech_started, |this, started| {
this.child(speech::elapsed(started, cx))
}),
)
})
.when(!dictating, |this| {
this.child(
Textarea::new(&self.input)
.appearance(false)
.bg(cx.theme().input)
.small()
.min_h_8()
.py_1()
.flex_1(),
)
})
.child(SpeechButton::new(&self.speech))
.child( .child(
Button::new("send") Button::new("send")
.icon(ui::IconName::PaperPlaneFill) .icon(ui::IconName::PaperPlaneFill)
.tooltip("Send") .tooltip("Send")
.ghost() .ghost()
.disabled(dictating || self.uploading)
.on_click(cx.listener(|this, _event, window, cx| { .on_click(cx.listener(|this, _event, window, cx| {
this.send(window, cx); this.send(window, cx);
})), })),
@@ -1267,6 +1382,7 @@ impl Render for GroupPanel {
v_flex() v_flex()
.size_full() .size_full()
.min_w_0() .min_w_0()
.on_action(cx.listener(Self::toggle_dictation))
.child(self.render_encryption_notice(cx)) .child(self.render_encryption_notice(cx))
.child(self.render_banner(cx)) .child(self.render_banner(cx))
.child(self.render_pins(cx)) .child(self.render_pins(cx))
+44 -6
View File
@@ -8,15 +8,17 @@ use gpui::{
use gpui_kit::assets::IconName; use gpui_kit::assets::IconName;
use gpui_kit::base::StyledExt as _; use gpui_kit::base::StyledExt as _;
use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenuItem}; 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 nip29::{Activity, Group, Reaction};
use nostr_sdk::prelude::{EventId, Kind}; use nostr_sdk::prelude::{EventId, Kind};
use person::Directory; use person::Directory;
use settings::AppSettings; use settings::AppSettings;
use speech::speaker::END_GRACE;
use state::NostrRegistry; use state::NostrRegistry;
use ui::{ use ui::{
Avatar, MessageMedia, MessageRow, REACTION_EMOJIS, ReactionBar, ReplyPreview, group_reactions, Avatar, MessageActions, MessageMedia, MessageRow, REACTION_EMOJIS, ReactionBar, ReplyPreview,
react_menu, group_reactions, react_menu,
}; };
use crate::GroupPanel; use crate::GroupPanel;
@@ -26,6 +28,8 @@ pub(crate) struct Actions {
pub panel: WeakEntity<GroupPanel>, pub panel: WeakEntity<GroupPanel>,
pub admin: bool, pub admin: bool,
pub pinned: bool, pub pinned: bool,
/// Whether a message is currently being spoken, disabling the speech buttons
pub speaking: bool,
pub persons: Entity<Directory>, pub persons: Entity<Directory>,
} }
@@ -87,10 +91,44 @@ pub(crate) fn render(
let admin = actions.admin; let admin = actions.admin;
let pinned = actions.pinned; let pinned = actions.pinned;
let group = actions.group; let group = actions.group;
let panel = actions.panel; let speech_panel = actions.panel;
let panel = speech_panel.clone();
let speech_text = message.preview();
let speech_id = message.id;
let speaking = actions.speaking;
let row = row.overlay(
MessageActions::new().speech(speaking, move |_event, _window, cx| {
_ = speech_panel.update_in(cx, |panel, window, cx| {
match panel.speaker.speak(&speech_text) {
Ok(duration) => {
panel.speech_reveal = Some((speech_id, duration));
let session = (speech_id, duration);
let timer = cx.background_executor().timer(duration + END_GRACE);
panel.reveal_timeout = Some(cx.spawn(async move |this, cx| {
timer.await;
this.update(cx, |this, cx| {
if this.speech_reveal.as_ref() == Some(&session) {
this.speech_reveal = None;
cx.notify();
}
})?;
Ok(())
}));
cx.notify();
}
Err(error) => {
window.push_notification(Notification::error(error.to_string()), cx)
}
}
});
}),
);
// 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 let custom = nostr
.read(cx) .read(cx)
.current_user() .current_user()
+32 -13
View File
@@ -330,22 +330,40 @@ impl AppSettings {
}); });
} }
/// Apply the persisted theme tuning on top of the active theme
pub fn apply_theme_style(cx: &mut App) { pub fn apply_theme_style(cx: &mut App) {
Self::apply_theme_tuning(Self::get_theme(cx), cx);
}
fn apply_theme_tuning(tuning: ThemeSettings, cx: &mut App) {
use gpui_kit::component::Theme; use gpui_kit::component::Theme;
let theme = Self::get_theme(cx); {
let styled = Theme::global_mut(cx); let styled = Theme::global_mut(cx);
styled.font_size = px(theme.font_size); styled.font_size = px(tuning.font_size);
styled.focus_ring = theme.focus_ring; styled.focus_ring = tuning.focus_ring;
if let Some(radius) = theme.radius { if let Some(radius) = tuning.radius {
styled.radius = px(radius); styled.radius = px(radius);
}
if let Some(radius_lg) = tuning.radius_lg {
styled.radius_lg = px(radius_lg);
}
} }
if let Some(radius_lg) = theme.radius_lg {
styled.radius_lg = px(radius_lg); Theme::update(cx, |styled| {
} let background = styled.background;
Theme::sync_base(cx); let foreground = styled.foreground;
cx.refresh_windows(); let (hover_alpha, active_alpha) = if styled.is_dark() {
(0.08, 0.12)
} else {
(0.04, 0.06)
};
if styled.list_hover == background {
styled.list_hover = foreground.alpha(hover_alpha);
}
if styled.tab_active == background {
styled.tab_active = foreground.alpha(active_alpha);
}
});
} }
/// Load settings /// Load settings
@@ -416,7 +434,8 @@ impl AppSettings {
AppearanceMode::Light => Theme::change(gpui_kit::component::ThemeMode::Light, None, cx), AppearanceMode::Light => Theme::change(gpui_kit::component::ThemeMode::Light, None, cx),
AppearanceMode::Dark => Theme::change(gpui_kit::component::ThemeMode::Dark, None, cx), AppearanceMode::Dark => Theme::change(gpui_kit::component::ThemeMode::Dark, None, cx),
} }
cx.refresh_windows(); let tuning = self.inner.read(cx).theme;
Self::apply_theme_tuning(tuning, cx);
} }
/// Check if decoupling encryption key is enabled /// Check if decoupling encryption key is enabled
+31
View File
@@ -0,0 +1,31 @@
[package]
name = "speech"
version.workspace = true
edition.workspace = true
publish.workspace = true
[dependencies]
anyhow.workspace = true
log.workspace = true
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" }
parakeet-rs = "=0.3.8"
ort = "=2.0.0-rc.13"
reqwest = { version = "0.12", default-features = false, features = [
"blocking",
"rustls-tls",
] }
serde.workspace = true
serde_json.workspace = true
sha2.workspace = true
[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["Window"] }
+54
View File
@@ -0,0 +1,54 @@
#[cfg(not(target_arch = "wasm32"))]
pub mod model;
#[cfg(not(target_arch = "wasm32"))]
mod parakeet;
pub mod speaker;
#[cfg(target_arch = "wasm32")]
mod web;
use std::time::Duration;
use gpui::{
Animation, AnimationExt as _, AnyElement, App, IntoElement as _, ParentElement as _,
Styled as _, div,
};
use gpui_kit::component::ActiveTheme as _;
use gpui_kit::component::speech::SpeechRecognizer;
/// A ticking `M:SS` counter for the dictation in progress.
pub fn elapsed(started: instant::Instant, cx: &App) -> AnyElement {
div()
.text_xs()
.text_color(cx.theme().muted_foreground)
.with_animation(
"dictation-elapsed",
Animation::new(Duration::from_secs(1))
.repeat()
.with_max_fps(1.),
move |counter, _| counter.child(format_elapsed(started)),
)
.into_any_element()
}
fn format_elapsed(started: instant::Instant) -> String {
let seconds = started.elapsed().as_secs();
format!("{}:{:02}", seconds / 60, seconds % 60)
}
/// The recognizer backing the composer's microphone button.
#[cfg(not(target_arch = "wasm32"))]
pub fn recognizer() -> impl SpeechRecognizer {
parakeet::ParakeetRecognizer
}
/// The recognizer backing the composer's microphone button.
#[cfg(target_arch = "wasm32")]
pub fn recognizer() -> impl SpeechRecognizer {
web::WebSpeechRecognizer
}
/// The audio source backing the composer's microphone button on the web.
#[cfg(target_arch = "wasm32")]
pub fn input() -> impl gpui_kit::component::speech::AudioInput {
web::SelfCapturedInput
}
+23
View File
@@ -0,0 +1,23 @@
{
"repository": "istupakov/parakeet-tdt-0.6b-v3-onnx",
"revision": "8f23f0c03c8761650bdb5b40aaf3e40d2c15f1ce",
"source": "nvidia/parakeet-tdt-0.6b-v3",
"license": "CC-BY-4.0",
"files": [
{
"name": "decoder_joint-model.int8.onnx",
"size": 18202004,
"sha256": "eea7483ee3d1a30375daedc8ed83e3960c91b098812127a0d99d1c8977667a70"
},
{
"name": "encoder-model.int8.onnx",
"size": 652183999,
"sha256": "6139d2fa7e1b086097b277c7149725edbab89cc7c7ae64b23c741be4055aff09"
},
{
"name": "vocab.txt",
"size": 93939,
"sha256": "d58544679ea4bc6ac563d1f545eb7d474bd6cfa467f0a6e2c1dc1c7d37e3c35d"
}
]
}
+295
View File
@@ -0,0 +1,295 @@
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::{fs, io};
use anyhow::{Result, bail};
use serde::Deserialize;
/// How the model is currently available.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
NotInstalled,
Downloading { downloaded: u64, total: u64 },
Installed,
}
impl Status {
pub fn is_downloading(self) -> bool {
matches!(self, Status::Downloading { .. })
}
}
#[derive(Debug, Deserialize)]
struct Manifest {
repository: String,
revision: String,
files: Vec<Artifact>,
}
#[derive(Debug, Deserialize)]
struct Artifact {
name: String,
size: u64,
sha256: String,
}
fn manifest() -> &'static Manifest {
static MANIFEST: OnceLock<Manifest> = OnceLock::new();
MANIFEST.get_or_init(|| {
serde_json::from_str(include_str!("model.json")).expect("pinned model manifest")
})
}
/// The directory the model artifacts are downloaded into.
pub fn dir() -> PathBuf {
common::config_dir()
.join("speech")
.join("parakeet-tdt-0.6b-v3")
}
/// Total download size in bytes.
pub fn total_size() -> u64 {
manifest().files.iter().map(|file| file.size).sum()
}
/// The revision of the installed model, if its artifacts are complete.
fn installed_revision() -> Option<String> {
let manifest = manifest();
let dir = dir();
let complete = manifest.files.iter().all(|file| {
fs::metadata(dir.join(&file.name)).is_ok_and(|metadata| metadata.len() == file.size)
});
if complete
&& fs::read_to_string(dir.join("verified"))
.is_ok_and(|revision| revision == manifest.revision)
{
Some(manifest.revision.clone())
} else {
None
}
}
/// Whether the model is ready to load.
pub fn installed() -> bool {
status() == Status::Installed
}
/// The current status, reconciling the download state with the disk.
pub fn status() -> Status {
let state = state().lock().unwrap_or_else(|error| error.into_inner());
if let Download::Running {
downloaded, total, ..
} = state.download
{
return Status::Downloading { downloaded, total };
}
if installed_revision().is_some() {
Status::Installed
} else {
Status::NotInstalled
}
}
/// Take the last download failure, if it has not been reported yet.
pub fn take_error() -> Option<String> {
let mut state = state().lock().unwrap_or_else(|error| error.into_inner());
state.error.take()
}
/// Start downloading the model, unless a download is already running.
pub fn start_download() {
let mut state = state().lock().unwrap_or_else(|error| error.into_inner());
if matches!(state.download, Download::Running { .. }) {
return;
}
let cancel = Arc::new(AtomicBool::new(false));
state.error = None;
state.download = Download::Running {
downloaded: 0,
total: total_size(),
cancel: cancel.clone(),
};
if let Err(error) = std::thread::Builder::new()
.name("speech-model-download".into())
.spawn(move || download(cancel))
{
state.download = Download::Idle;
state.error = Some(error.to_string());
}
}
/// Request cancellation of the running download.
pub fn cancel_download() {
let state = state().lock().unwrap_or_else(|error| error.into_inner());
if let Download::Running { cancel, .. } = &state.download {
cancel.store(true, Ordering::Release);
}
}
/// Delete the downloaded model and any partial download.
pub fn delete() {
super::parakeet::unload();
cancel_download();
if let Err(error) = fs::remove_dir_all(dir())
&& error.kind() != io::ErrorKind::NotFound
{
log::warn!("failed to remove the speech model: {error}");
}
}
fn download(cancel: Arc<AtomicBool>) {
let result = download_inner(&cancel);
let mut state = state().lock().unwrap_or_else(|error| error.into_inner());
state.download = Download::Idle;
// A cancelled download must not report a spurious failure.
if !cancelled(&cancel)
&& let Err(error) = result
{
state.error = Some(format!("{error:#}"));
}
}
fn download_inner(cancel: &AtomicBool) -> Result<()> {
let manifest = manifest();
let dir = dir();
let client = reqwest::blocking::Client::builder()
.connect_timeout(std::time::Duration::from_secs(20))
.build()?;
fs::create_dir_all(&dir)?;
for file in &manifest.files {
if cancelled(cancel) {
bail!("Cancelled");
}
let staging = dir.join(format!("{}.part", file.name));
let result = download_file(&client, manifest, file, &staging, cancel);
if result.is_err() || cancelled(cancel) {
let _unused = fs::remove_file(&staging);
result?;
bail!("Cancelled");
}
}
fs::write(dir.join("verified"), &manifest.revision)?;
Ok(())
}
fn download_file(
client: &reqwest::blocking::Client,
manifest: &Manifest,
file: &Artifact,
staging: &Path,
cancel: &AtomicBool,
) -> Result<()> {
use std::io::{Read as _, Write as _};
use sha2::Digest as _;
let url = format!(
"https://huggingface.co/{}/resolve/{}/{}",
manifest.repository, manifest.revision, file.name
);
let mut response = client.get(url).send()?.error_for_status()?;
let mut out = fs::File::create(staging)?;
let mut hash = sha2::Sha256::new();
let mut size = 0u64;
loop {
if cancelled(cancel) {
bail!("Cancelled");
}
let mut chunk = [0u8; 64 * 1024];
let read = response.read(&mut chunk)?;
if read == 0 {
break;
}
size += read as u64;
if size > file.size {
bail!("Unexpected model size");
}
hash.update(&chunk[..read]);
out.write_all(&chunk[..read])?;
let mut state = state().lock().unwrap_or_else(|error| error.into_inner());
if let Download::Running { downloaded, .. } = &mut state.download {
*downloaded += read as u64;
}
}
if size != file.size || format!("{:x}", hash.finalize()) != file.sha256 {
bail!("Model checksum verification failed");
}
out.sync_all()?;
fs::rename(staging, dir().join(&file.name))?;
Ok(())
}
fn cancelled(cancel: &AtomicBool) -> bool {
cancel.load(Ordering::Acquire)
}
enum Download {
Idle,
Running {
downloaded: u64,
total: u64,
cancel: Arc<AtomicBool>,
},
}
struct State {
download: Download,
error: Option<String>,
}
fn state() -> &'static Mutex<State> {
static STATE: Mutex<State> = Mutex::new(State {
download: Download::Idle,
error: None,
});
&STATE
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn manifest_pins_v3_artifacts_with_exact_sizes() {
let manifest = manifest();
assert_eq!(manifest.repository, "istupakov/parakeet-tdt-0.6b-v3-onnx");
assert_eq!(manifest.revision.len(), 40);
assert_eq!(manifest.files.len(), 3);
assert_eq!(total_size(), 670_479_942);
assert!(
manifest
.files
.iter()
.all(|file| file.sha256.len() == 64 && !file.name.contains('/'))
);
}
}
+214
View File
@@ -0,0 +1,214 @@
use std::path::PathBuf;
use std::sync::OnceLock;
use std::time::Duration;
use anyhow::{Context as _, Result, anyhow};
use gpui::{App, Task};
use gpui_kit::component::speech::{
AudioFormat, RecognitionSession, SpeechError, SpeechRecognizer, SpeechSink,
};
use parakeet_rs::{ParakeetTDT, Transcriber as _};
const MODEL_RATE: u32 = 16_000;
const MAX_SECONDS: usize = 120;
/// The on-device recognizer.
pub struct ParakeetRecognizer;
impl SpeechRecognizer for ParakeetRecognizer {
fn audio_format(&self) -> AudioFormat {
AudioFormat::new(MODEL_RATE, 1)
}
fn is_available(&self, _cx: &App) -> bool {
true
}
fn start(
&self,
sink: SpeechSink,
cx: &mut App,
) -> Result<Box<dyn RecognitionSession>, SpeechError> {
if super::model::installed() {
worker().send_or_log(Command::Warm(super::model::dir()));
} else if super::model::status().is_downloading() {
return Err(SpeechError::recognizer(anyhow!(
"The speech model is still downloading. Try again when it finishes."
)));
} else {
return Err(SpeechError::recognizer(anyhow!(
"No speech model is installed. Download it from Settings."
)));
}
// The session buffers everything it is pushed,
// so audio is accepted right away.
sink.ready(cx);
Ok(Box::new(ParakeetSession {
sink,
samples: Vec::new(),
_result: None,
}))
}
}
struct ParakeetSession {
sink: SpeechSink,
samples: Vec<f32>,
_result: Option<Task<()>>,
}
impl RecognitionSession for ParakeetSession {
fn push_audio(&mut self, samples: &[i16], _cx: &mut App) {
if self.samples.len() < MAX_SECONDS * MODEL_RATE as usize {
self.samples
.extend(samples.iter().map(|sample| f32::from(*sample) / 32_768.0));
}
}
fn finish(&mut self, cx: &mut App) {
let samples = std::mem::take(&mut self.samples);
let sink = self.sink.clone();
// Speech too short to transcribe ends the session at once.
let silent = samples.iter().all(|sample| sample.abs() < 0.0001);
if samples.len() < MODEL_RATE as usize / 5 || silent {
sink.finish(cx);
return;
}
let (sender, receiver) = flume::unbounded();
worker().send_or_log(Command::Transcribe { samples, sender });
self._result = Some(cx.spawn(async move |cx| {
let result = receiver.recv_async().await;
cx.update(|cx| match result {
Ok(Ok(text)) if !text.is_empty() => {
sink.phrase(text, cx);
sink.finish(cx);
}
Ok(Ok(_)) => sink.finish(cx),
Ok(Err(error)) => sink.error(SpeechError::recognizer(error), cx),
Err(_) => sink.error(
SpeechError::recognizer(anyhow!("The speech worker is unavailable")),
cx,
),
});
}));
}
}
enum Command {
Warm(PathBuf),
Transcribe {
samples: Vec<f32>,
sender: flume::Sender<Result<String, anyhow::Error>>,
},
Unload,
}
/// A handle to the single inference worker.
fn worker() -> &'static flume::Sender<Command> {
static WORKER: OnceLock<flume::Sender<Command>> = OnceLock::new();
WORKER.get_or_init(|| {
let (sender, receiver) = flume::unbounded::<Command>();
let spawned = std::thread::Builder::new()
.name("speech-parakeet".into())
.spawn(move || run_worker(receiver));
if let Err(error) = spawned {
log::error!("failed to spawn the speech worker: {error}");
}
sender
})
}
fn run_worker(receiver: flume::Receiver<Command>) {
let mut cached: Option<(PathBuf, ParakeetTDT)> = None;
while let Ok(command) = receiver.recv_timeout(Duration::from_secs(30)) {
match command {
Command::Unload => {
cached = None;
}
Command::Warm(dir) => {
let loaded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
load(&mut cached, &dir).map(|_| ())
}));
match flatten_panic(loaded) {
Ok(Ok(())) => {}
Ok(Err(error)) | Err(error) => {
log::warn!("failed to load the speech model: {error:#}")
}
}
}
Command::Transcribe { samples, sender } => {
let transcribed = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
transcribe(&mut cached, samples)
}));
let result = match flatten_panic(transcribed) {
Ok(result) => result,
Err(_) => {
cached = None;
Err(anyhow!("Dictation stopped unexpectedly. Try again."))
}
};
if sender.send(result).is_err() {
log::debug!("speech session was cancelled before its result arrived");
}
}
}
}
}
fn load<'a>(
cached: &'a mut Option<(PathBuf, ParakeetTDT)>,
dir: &PathBuf,
) -> Result<&'a mut ParakeetTDT> {
if cached.as_ref().is_none_or(|(path, _)| path != dir) {
let model = ParakeetTDT::from_pretrained(dir, None)
.map_err(|error| anyhow!("Could not load the speech model: {error}"))?;
*cached = Some((dir.clone(), model));
}
let (_, model) = cached.as_mut().context("speech model was unloaded")?;
Ok(model)
}
fn transcribe(cached: &mut Option<(PathBuf, ParakeetTDT)>, samples: Vec<f32>) -> Result<String> {
let model = load(cached, &super::model::dir())?;
model
.transcribe_samples(samples, MODEL_RATE, 1, None)
.map(|result| result.text)
.map_err(|error| anyhow!("Could not transcribe this recording: {error}"))
}
trait SendOrLog {
fn send_or_log(&self, command: Command);
}
impl SendOrLog for flume::Sender<Command> {
fn send_or_log(&self, command: Command) {
if self.send(command).is_err() {
log::debug!("speech worker is gone; command dropped");
}
}
}
/// Retire cached weights, e.g. after the model was deleted.
pub(crate) fn unload() {
worker().send_or_log(Command::Unload);
}
fn flatten_panic<T>(result: std::thread::Result<T>) -> Result<T> {
result.map_err(|_| anyhow!("The speech worker panicked"))
}
+88
View File
@@ -0,0 +1,88 @@
use std::time::Duration;
use tts::Tts;
/// Words per minute of an engine at its normal rate, for estimating speech duration.
const NORMAL_WPM: f32 = 160.0;
/// Extra time past the estimate before speech is considered finished, for backends that
/// cannot report utterance end.
pub const END_GRACE: Duration = Duration::from_secs(5);
/// Reads text aloud through the platform's speech backend, created on first use.
pub struct Speaker {
tts: Option<Tts>,
end_tx: flume::Sender<()>,
end_rx: flume::Receiver<()>,
}
impl Speaker {
pub fn new() -> Self {
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.
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() {
Some(tts) => tts,
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)?;
Ok(Self::estimate(tts, text))
}
/// 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)
}
/// 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()
}
}
+256
View File
@@ -0,0 +1,256 @@
use std::cell::RefCell;
use anyhow::anyhow;
use gpui::App;
use gpui_kit::Subscription;
use gpui_kit::component::speech::{
AudioFormat, AudioInput, AudioSink, RecognitionSession, SpeechError, SpeechRecognizer,
SpeechSink,
};
use js_sys::{Array, Function, JsString, Reflect};
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast as _, JsValue};
use web_sys::window;
/// The recognizer backed by `SpeechRecognition` (or its `webkit` prefix).
pub struct WebSpeechRecognizer;
impl SpeechRecognizer for WebSpeechRecognizer {
fn audio_format(&self) -> AudioFormat {
AudioFormat::default()
}
fn is_available(&self, _cx: &App) -> bool {
true
}
fn start(
&self,
sink: SpeechSink,
cx: &mut App,
) -> Result<Box<dyn RecognitionSession>, SpeechError> {
let recognition = Recognition::open(sink, cx).map_err(SpeechError::recognizer)?;
Ok(Box::new(recognition))
}
}
pub struct SelfCapturedInput;
impl AudioInput for SelfCapturedInput {
fn start(
&self,
_format: AudioFormat,
_sink: AudioSink,
_cx: &mut App,
) -> Result<Subscription, SpeechError> {
Ok(Subscription::new(|| {}))
}
}
enum Event {
Hypothesis(String),
Phrase(String),
Error(String),
End,
}
struct Recognition {
recognition: JsValue,
on_result: Option<Closure<dyn FnMut(JsValue)>>,
on_error: Option<Closure<dyn FnMut(JsValue)>>,
on_end: Option<Closure<dyn FnMut(JsValue)>>,
_task: gpui::Task<()>,
}
impl Recognition {
fn open(sink: SpeechSink, cx: &mut App) -> anyhow::Result<Self> {
let window = window().ok_or_else(|| anyhow!("No browser window"))?;
let constructor = Reflect::get(&window, &JsString::from("SpeechRecognition"))
.ok()
.filter(|value| value.is_function())
.or_else(|| {
Reflect::get(&window, &JsString::from("webkitSpeechRecognition"))
.ok()
.filter(|value| value.is_function())
})
.ok_or_else(|| anyhow!("This browser does not support speech recognition"))?;
let recognition =
Reflect::construct(constructor.unchecked_ref::<Function>(), &Array::new())
.map_err(|error| js_error("constructing the recognizer", error))?;
let (sender, receiver) = flume::unbounded::<Event>();
let error_sender = sender.clone();
let end_sender = sender.clone();
let finalized = RefCell::new(0usize);
let on_result = Closure::wrap(Box::new(move |event: JsValue| {
let read = |value: &JsValue, property: &str| -> Option<JsValue> {
Reflect::get(value, &JsString::from(property)).ok()
};
let Some(results) = read(&event, "results") else {
return;
};
let length = read(&results, "length")
.and_then(|length| length.as_f64())
.unwrap_or(0.0) as usize;
let mut finalized = finalized.borrow_mut();
let mut interim = String::new();
for index in *finalized..length {
let Some(result) = read(&results, &index.to_string()) else {
continue;
};
let Some(alternative) = read(&result, "0") else {
continue;
};
let Some(text) = read(&alternative, "transcript").and_then(|text| text.as_string())
else {
continue;
};
let is_final = read(&result, "isFinal")
.and_then(|final_value| final_value.as_bool())
.unwrap_or(false);
if is_final {
*finalized += 1;
if sender.send(Event::Phrase(text)).is_err() {
log::debug!("speech session was cancelled mid-utterance");
}
} else {
interim.push_str(&text);
}
}
if !interim.is_empty() && sender.send(Event::Hypothesis(interim)).is_err() {
log::debug!("speech session was cancelled mid-utterance");
}
}) as Box<dyn FnMut(JsValue)>);
let on_error = Closure::wrap(Box::new(move |event: JsValue| {
let message = Reflect::get(&event, &JsString::from("error"))
.ok()
.and_then(|error| error.as_string())
.unwrap_or_else(|| "unknown speech error".into());
if error_sender.send(Event::Error(message)).is_err() {
log::debug!("speech session was cancelled before its error arrived");
}
}) as Box<dyn FnMut(JsValue)>);
let on_end = Closure::wrap(Box::new(move |_event: JsValue| {
if end_sender.send(Event::End).is_err() {
log::debug!("speech session was cancelled before its end arrived");
}
}) as Box<dyn FnMut(JsValue)>);
set_callback(&recognition, "onresult", on_result.as_ref().unchecked_ref())?;
set_callback(&recognition, "onerror", on_error.as_ref().unchecked_ref())?;
set_callback(&recognition, "onend", on_end.as_ref().unchecked_ref())?;
set_bool(&recognition, "continuous", true)?;
set_bool(&recognition, "interimResults", true)?;
call_method(&recognition, "start")?;
// The browser consumes audio as soon as it has permission
sink.ready(cx);
let task = cx.spawn(async move |cx| {
while let Ok(event) = receiver.recv_async().await {
let done = matches!(event, Event::End);
// The speech state ignores sink calls once its session ended,
// so a late event after a cancel is harmless.
cx.update(|cx| match event {
Event::Hypothesis(text) => sink.hypothesis(text, cx),
Event::Phrase(text) => sink.phrase(text, cx),
Event::Error(message) => {
sink.error(SpeechError::recognizer(anyhow!("{message}")), cx)
}
Event::End => sink.finish(cx),
});
if done {
break;
}
}
});
Ok(Self {
recognition,
on_result: Some(on_result),
on_error: Some(on_error),
on_end: Some(on_end),
_task: task,
})
}
fn stop(&mut self) {
self.on_result = None;
self.on_error = None;
self.on_end = None;
let _ignored = call_method(&self.recognition, "abort");
}
}
impl Drop for Recognition {
fn drop(&mut self) {
self.stop();
}
}
impl RecognitionSession for Recognition {
fn push_audio(&mut self, _samples: &[i16], _cx: &mut App) {
// The browser recognizer captures the microphone itself.
// the state still pushes audio through its input, which is a no-op here.
}
fn finish(&mut self, _cx: &mut App) {
// The end handler reports completion, so no result is fabricated here.
let _ignored = call_method(&self.recognition, "stop");
}
}
fn set_callback(target: &JsValue, property: &str, callback: &Function) -> anyhow::Result<()> {
Reflect::set(target, &JsString::from(property), callback)
.map_err(|error| js_error(property, error))?;
Ok(())
}
fn set_bool(target: &JsValue, property: &str, value: bool) -> anyhow::Result<()> {
Reflect::set(target, &JsString::from(property), &JsValue::from(value))
.map_err(|error| js_error(property, error))?;
Ok(())
}
fn call_method(target: &JsValue, method: &str) -> anyhow::Result<()> {
let function = Reflect::get(target, &JsString::from(method))
.map_err(|error| js_error(method, error))?
.unchecked_into::<Function>();
function
.call0(target)
.map(|_| ())
.map_err(|error| js_error(method, error))
}
fn js_error(what: &str, error: JsValue) -> anyhow::Error {
let description = error
.as_string()
.or_else(|| {
Reflect::get(&error, &JsString::from("message"))
.ok()
.and_then(|message| message.as_string())
})
.unwrap_or_else(|| "unknown error".into());
anyhow!("{what} failed: {description}")
}
+6
View File
@@ -20,14 +20,17 @@ pub enum IconName {
Link, Link,
Lock, Lock,
Message, Message,
Paperclip,
PaperPlaneFill, PaperPlaneFill,
Plus, Plus,
PlusChat, PlusChat,
Profile, Profile,
Relay, Relay,
Speech,
Search, Search,
Settings, Settings,
Settings2, Settings2,
Unlock,
Upload, Upload,
Warning, Warning,
} }
@@ -51,14 +54,17 @@ impl IconNamed for IconName {
Self::Link => "icons/link.svg", Self::Link => "icons/link.svg",
Self::Lock => "icons/lock.svg", Self::Lock => "icons/lock.svg",
Self::Message => "icons/message.svg", Self::Message => "icons/message.svg",
Self::Paperclip => "icons/paperclip.svg",
Self::PaperPlaneFill => "icons/paper-plane-fill.svg", Self::PaperPlaneFill => "icons/paper-plane-fill.svg",
Self::Plus => "icons/plus.svg", Self::Plus => "icons/plus.svg",
Self::PlusChat => "icons/plus-chat.svg", Self::PlusChat => "icons/plus-chat.svg",
Self::Profile => "icons/profile.svg", Self::Profile => "icons/profile.svg",
Self::Relay => "icons/relay.svg", Self::Relay => "icons/relay.svg",
Self::Speech => "icons/speech.svg",
Self::Search => "icons/search.svg", Self::Search => "icons/search.svg",
Self::Settings => "icons/settings.svg", Self::Settings => "icons/settings.svg",
Self::Settings2 => "icons/settings2.svg", Self::Settings2 => "icons/settings2.svg",
Self::Unlock => "icons/unlock.svg",
Self::Upload => "icons/upload.svg", Self::Upload => "icons/upload.svg",
Self::Warning => "icons/warning.svg", Self::Warning => "icons/warning.svg",
} }
+80 -16
View File
@@ -1,18 +1,21 @@
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 _};
use gpui_kit::component::clipboard::Clipboard;
use gpui_kit::component::menu::{DropdownMenu as _, PopupMenu, PopupMenuItem}; use gpui_kit::component::menu::{DropdownMenu as _, PopupMenu, PopupMenuItem};
use gpui_kit::component::{ use gpui_kit::component::{
ActiveTheme, InteractiveElementExt as _, Sizable as _, StyledExt as _, h_flex, v_flex, ActiveTheme, Disableable, InteractiveElementExt as _, Sizable as _, StyledExt as _, h_flex,
v_flex,
}; };
use smallvec::SmallVec; use smallvec::SmallVec;
@@ -596,13 +599,14 @@ pub fn react_menu(
}) })
} }
/// A hovered message's action bar: quick reactions, reply, copy, and an overflow menu. /// A hovered message's action bar: quick reactions, reply, copy, read aloud, and an overflow menu.
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct MessageActions { pub struct MessageActions {
style: StyleRefinement, style: StyleRefinement,
reactions: SmallVec<[(&'static str, SharedClickListener); 4]>, reactions: SmallVec<[(&'static str, SharedClickListener); 4]>,
reply: Option<SharedClickListener>, reply: Option<SharedClickListener>,
copy: Option<SharedClickListener>, copy: Option<(ElementId, SharedString)>,
speech: Option<(bool, SharedClickListener)>,
dropdown: Option<DropdownBuilder>, dropdown: Option<DropdownBuilder>,
} }
@@ -614,6 +618,7 @@ impl MessageActions {
reactions: SmallVec::new(), reactions: SmallVec::new(),
reply: None, reply: None,
copy: None, copy: None,
speech: None,
dropdown: None, dropdown: None,
} }
} }
@@ -634,9 +639,20 @@ impl MessageActions {
self self
} }
/// Add the copy button. /// Add the copy button for the given value.
pub fn copy(mut self, handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self { /// The id must be unique per bar, the clipboard component keys its copied state on it.
self.copy = Some(Rc::new(handler)); pub fn copy(mut self, id: impl Into<ElementId>, value: impl Into<SharedString>) -> Self {
self.copy = Some((id.into(), value.into()));
self
}
/// Add the read-aloud button, disabled while a message is already being spoken.
pub fn speech(
mut self,
disabled: bool,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.speech = Some((disabled, Rc::new(handler)));
self self
} }
@@ -669,10 +685,13 @@ impl RenderOnce for MessageActions {
reactions, reactions,
reply, reply,
copy, copy,
speech,
dropdown, dropdown,
} = self; } = self;
let has_center = reply.is_some() || copy.is_some(); // The leading separator only separates reactions from the center buttons
let has_center =
!reactions.is_empty() && (reply.is_some() || copy.is_some() || speech.is_some());
h_flex() h_flex()
.p_0p5() .p_0p5()
@@ -707,14 +726,18 @@ impl RenderOnce for MessageActions {
.on_click(move |event, window, cx| reply(event, window, cx)), .on_click(move |event, window, cx| reply(event, window, cx)),
) )
}) })
.when_some(copy, |this, copy| { .when_some(copy, |this, (id, value)| {
this.child(Clipboard::new(id).value(value).tooltip("Copy").small())
})
.when_some(speech, |this, (disabled, speech)| {
this.child( this.child(
Button::new("copy") Button::new("speech")
.icon(IconName::Copy) .icon(crate::IconName::Speech)
.tooltip("Copy") .tooltip("Read aloud")
.small() .small()
.ghost() .ghost()
.on_click(move |event, window, cx| copy(event, window, cx)), .disabled(disabled)
.on_click(move |event, window, cx| speech(event, window, cx)),
) )
}) })
.when_some(dropdown, |this, dropdown| { .when_some(dropdown, |this, dropdown| {
@@ -731,3 +754,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())
}))
},
)
}
}
+1
View File
@@ -15,6 +15,7 @@ chat_ui = { path = "../chat_ui" }
nip29 = { path = "../nip29" } nip29 = { path = "../nip29" }
nip29_ui = { path = "../nip29_ui" } nip29_ui = { path = "../nip29_ui" }
settings = { path = "../settings" } settings = { path = "../settings" }
speech = { path = "../speech" }
person = { path = "../person" } person = { path = "../person" }
auto_update = { path = "../auto_update" } auto_update = { path = "../auto_update" }
+88 -44
View File
@@ -1,15 +1,18 @@
use anyhow::Error; use anyhow::Error;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
App, AppContext, Context, Entity, IntoElement, ParentElement, Render, SharedString, Styled, App, AppContext, Context, Div, Entity, IntoElement, ParentElement, Render, SharedString,
Subscription, Task, Window, div, px, relative, Styled, Subscription, Task, Window, div, px, relative,
}; };
use gpui_kit::component::button::{Button, ButtonVariants}; use gpui_kit::component::button::{Button, ButtonVariants};
use gpui_kit::component::dialog::{DialogDescription, DialogFooter, DialogHeader, DialogTitle};
use gpui_kit::component::form::{field, v_form};
use gpui_kit::component::input::{Input, InputEvent, InputState}; use gpui_kit::component::input::{Input, InputEvent, InputState};
use gpui_kit::component::{ActiveTheme, Disableable, WindowExt, v_flex}; use gpui_kit::component::{ActiveTheme, Disableable, WindowExt, v_flex};
use instant::Duration; use instant::Duration;
use nostr_connect::prelude::*; use nostr_connect::prelude::*;
use state::{NostrRegistry, StateEvent}; use state::{NostrRegistry, StateEvent};
use ui::IconName;
pub fn open(window: &mut Window, cx: &mut App) { pub fn open(window: &mut Window, cx: &mut App) {
let import = cx.new(|cx| ImportIdentity::new(window, cx)); let import = cx.new(|cx| ImportIdentity::new(window, cx));
@@ -32,7 +35,6 @@ pub fn open_unlock(window: &mut Window, cx: &mut App) {
this.close_button(false) this.close_button(false)
.overlay_closable(false) .overlay_closable(false)
.keyboard(false) .keyboard(false)
.title("Unlock your identity")
.child(import.clone()) .child(import.clone())
}); });
} }
@@ -74,7 +76,15 @@ impl ImportIdentity {
fn build(mode: Mode, window: &mut Window, cx: &mut Context<Self>) -> Self { fn build(mode: Mode, window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let key_input = cx.new(|cx| InputState::new(window, cx).placeholder("secret or bunker://")); let key_input = cx.new(|cx| InputState::new(window, cx).placeholder("secret or bunker://"));
let pass_input = cx.new(|cx| InputState::new(window, cx).masked(true)); let pass_input = cx.new(|cx| {
let input = InputState::new(window, cx).masked(true);
if mode == Mode::Unlock {
input.placeholder("Passphrase to unlock your identity")
} else {
input
}
});
let repass_input = cx.new(|cx| InputState::new(window, cx).masked(true)); let repass_input = cx.new(|cx| InputState::new(window, cx).masked(true));
let error = cx.new(|_| None); let error = cx.new(|_| None);
@@ -235,15 +245,59 @@ impl ImportIdentity {
Ok(()) Ok(())
})); }));
} }
fn render_unlock(&self, cx: &mut Context<Self>) -> Div {
v_flex()
.gap_4()
.child(
DialogHeader::new()
.child(DialogTitle::new().child("Unlock your identity"))
.child(
DialogDescription::new()
.child("Enter the passphrase used to encrypt this identity."),
),
)
.child(
v_form().child(
field()
.label("Passphrase")
.required(true)
.child(Input::new(&self.pass_input)),
),
)
.when_some(self.error.read(cx).as_ref(), |this, error| {
this.child(
div()
.text_sm()
.text_color(cx.theme().danger)
.child(error.clone()),
)
})
.child(
DialogFooter::new().child(
Button::new("unlock")
.primary()
.icon(IconName::Unlock)
.label("Unlock")
.loading(self.loading)
.disabled(self.loading)
.on_click(cx.listener(move |this, _ev, _window, cx| {
this.login(cx);
})),
),
)
}
} }
impl Render for ImportIdentity { impl Render for ImportIdentity {
fn render(&mut self, _window: &mut gpui::Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut gpui::Window, cx: &mut Context<Self>) -> impl IntoElement {
if self.mode == Mode::Unlock {
return self.render_unlock(cx);
}
const BUNKER_WARN: &str = "Nostr Connect will usually take more time to get all your messages. Please keep your session open until you see all your messages."; const BUNKER_WARN: &str = "Nostr Connect will usually take more time to get all your messages. Please keep your session open until you see all your messages.";
const KEY_WARN: &str = "Coop encrypts your key with your passphrase and stores it on this device. You only need the passphrase to sign in next time."; const KEY_WARN: &str = "Coop encrypts your key with your passphrase and stores it on this device. You only need the passphrase to sign in next time.";
let is_unlock = self.mode == Mode::Unlock;
let value = self.key_input.read(cx).value().trim().to_string(); let value = self.key_input.read(cx).value().trim().to_string();
let require_password = value.starts_with("ncryptsec1"); let require_password = value.starts_with("ncryptsec1");
let require_encrypt = value.starts_with("nsec1"); let require_encrypt = value.starts_with("nsec1");
@@ -257,25 +311,19 @@ impl Render for ImportIdentity {
.child( .child(
v_flex() v_flex()
.gap_2() .gap_2()
.when(!is_unlock, |this| { .child(
v_flex()
.gap_1()
.text_color(cx.theme().muted_foreground)
.child("Use secret key or bunker connection")
.child(Input::new(&self.key_input)),
)
.when(require_password, |this| {
this.child( this.child(
v_flex() v_flex()
.gap_1() .gap_1()
.text_color(cx.theme().muted_foreground) .text_color(cx.theme().muted_foreground)
.child("Use secret key or bunker connection") .child("Decrypt Password:")
.child(Input::new(&self.key_input)),
)
})
.when(is_unlock || require_password, |this| {
this.child(
v_flex()
.gap_1()
.text_color(cx.theme().muted_foreground)
.child(if is_unlock {
"Passphrase to unlock your identity:"
} else {
"Decrypt Password:"
})
.child(Input::new(&self.pass_input)), .child(Input::new(&self.pass_input)),
) )
}) })
@@ -323,7 +371,7 @@ impl Render for ImportIdentity {
.child({ .child({
let content = v_flex().gap_1().child( let content = v_flex().gap_1().child(
Button::new("login") Button::new("login")
.label(if is_unlock { "Unlock" } else { "Continue" }) .label("Continue")
.primary() .primary()
.loading(self.loading) .loading(self.loading)
.disabled(self.loading) .disabled(self.loading)
@@ -333,30 +381,26 @@ impl Render for ImportIdentity {
); );
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
let content = content.when(!is_unlock, |this| { let content = content.child(
this.child( Button::new("proxy")
Button::new("proxy") .label("Connect via Web Extension")
.label("Connect via Web Extension") .ghost()
.ghost() .disabled(self.loading)
.disabled(self.loading) .on_click(cx.listener(move |this, _ev, _window, cx| {
.on_click(cx.listener(move |this, _ev, _window, cx| { this.proxy(cx);
this.proxy(cx); })),
})), );
)
});
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
let content = content.when(!is_unlock, |this| { let content = content.child(
this.child( Button::new("extension")
Button::new("extension") .label("Continue with Browser Extension")
.label("Continue with Browser Extension") .ghost()
.ghost() .disabled(self.loading)
.disabled(self.loading) .on_click(cx.listener(move |this, _ev, _window, cx| {
.on_click(cx.listener(move |this, _ev, _window, cx| { this.extension(cx);
this.extension(cx); })),
})), );
)
});
content content
}) })
+2 -4
View File
@@ -10,7 +10,6 @@ use gpui::{
Window, div, px, uniform_list, Window, div, px, uniform_list,
}; };
use gpui_kit::assets::IconName; use gpui_kit::assets::IconName;
use gpui_kit::base::StyledExt as _;
use gpui_kit::component::button::{Button, ButtonVariants}; use gpui_kit::component::button::{Button, ButtonVariants};
use gpui_kit::component::dock::{DockArea, DockPlacement, PanelHandle}; use gpui_kit::component::dock::{DockArea, DockPlacement, PanelHandle};
use gpui_kit::component::input::{Input, InputEvent, InputState}; use gpui_kit::component::input::{Input, InputEvent, InputState};
@@ -205,6 +204,7 @@ impl NewChat {
Ok(()) Ok(())
})); }));
return; return;
} }
@@ -369,15 +369,13 @@ impl NewChat {
.p_0p5() .p_0p5()
.rounded_full() .rounded_full()
.bg(cx.theme().secondary) .bg(cx.theme().secondary)
.text_xs()
.font_medium()
.hover(|this| this.bg(cx.theme().primary)) .hover(|this| this.bg(cx.theme().primary))
.child( .child(
Avatar::new(profile.avatar()) Avatar::new(profile.avatar())
.seed(profile.avatar_seed()) .seed(profile.avatar_seed())
.xsmall(), .xsmall(),
) )
.child(div().max_w_32().truncate().child(profile.name())) .child(div().max_w_32().truncate().text_xs().child(profile.name()))
.on_click(cx.listener(move |this, _event, _window, cx| { .on_click(cx.listener(move |this, _event, _window, cx| {
this.toggle_member(&public_key, cx); this.toggle_member(&public_key, cx);
})) }))
+12 -55
View File
@@ -4,17 +4,17 @@ use anyhow::Error;
use chat::{ChatRegistry, Room, RoomKind}; use chat::{ChatRegistry, Room, RoomKind};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
App, AppContext, ClipboardItem, Context, Div, Entity, IntoElement, ParentElement, Render, App, AppContext, Context, Div, Entity, IntoElement, ParentElement, Render, SharedString,
SharedString, Styled, Task, Window, div, retain_all, Styled, Task, Window, div, retain_all,
}; };
use gpui_kit::assets::IconName; use gpui_kit::assets::IconName;
use gpui_kit::base::StyledExt as _; use gpui_kit::base::StyledExt as _;
use gpui_kit::component::button::{Button, ButtonVariants}; use gpui_kit::component::button::{Button, ButtonVariants};
use gpui_kit::component::clipboard::Clipboard;
use gpui_kit::component::dock::DockPlacement; use gpui_kit::component::dock::DockPlacement;
use gpui_kit::component::input::{Input, InputState, Textarea, TextareaState}; use gpui_kit::component::input::{Input, InputState, Textarea, TextareaState};
use gpui_kit::component::notification::Notification; use gpui_kit::component::notification::Notification;
use gpui_kit::component::{ActiveTheme, Disableable, Sizable, WindowExt, h_flex, v_flex}; use gpui_kit::component::{ActiveTheme, Disableable, Sizable, WindowExt, h_flex, v_flex};
use instant::Duration;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::{Directory, Person, PersonRegistry, shorten_pubkey}; use person::{Directory, Person, PersonRegistry, shorten_pubkey};
use settings::AppSettings; use settings::AppSettings;
@@ -49,8 +49,6 @@ struct ProfileDialog {
website_input: Entity<InputState>, website_input: Entity<InputState>,
/// Uploading state /// Uploading state
uploading: bool, uploading: bool,
/// Copied states
copied: bool,
/// Updating state /// Updating state
updating: bool, updating: bool,
/// Tasks /// Tasks
@@ -92,7 +90,6 @@ impl ProfileDialog {
bio_input, bio_input,
website_input, website_input,
uploading: false, uploading: false,
copied: false,
updating: false, updating: false,
tasks: vec![], tasks: vec![],
} }
@@ -141,35 +138,6 @@ impl ProfileDialog {
cx.notify(); cx.notify();
} }
fn copy(&mut self, value: String, window: &mut Window, cx: &mut Context<Self>) {
let item = ClipboardItem::new_string(value);
cx.write_to_clipboard(item);
self.set_copied(true, window, cx);
}
fn set_copied(&mut self, status: bool, window: &mut Window, cx: &mut Context<Self>) {
self.copied = status;
cx.notify();
if status {
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
cx.background_executor().timer(Duration::from_secs(2)).await;
// Reset the copied state after a delay
cx.update(|window, cx| {
this.update(cx, |this, cx| {
this.set_copied(false, window, cx);
})
.ok();
})
.ok();
Ok(())
}));
}
}
fn set_uploading(&mut self, status: bool, cx: &mut Context<Self>) { fn set_uploading(&mut self, status: bool, cx: &mut Context<Self>) {
self.uploading = status; self.uploading = status;
cx.notify(); cx.notify();
@@ -368,7 +336,8 @@ impl Render for ProfileDialog {
.w_full() .w_full()
.items_center() .items_center()
.justify_center() .justify_center()
.bg(cx.theme().sidebar) .rounded(cx.theme().radius_lg)
.bg(cx.theme().muted)
.child(Avatar::new(avatar).seed(profile.avatar_seed()).large()) .child(Avatar::new(avatar).seed(profile.avatar_seed()).large())
.when(self.editing, |this| { .when(self.editing, |this| {
this.child( this.child(
@@ -393,25 +362,13 @@ impl Render for ProfileDialog {
.justify_center() .justify_center()
.gap_3() .gap_3()
.rounded(cx.theme().radius) .rounded(cx.theme().radius)
.bg(cx.theme().sidebar) .bg(cx.theme().muted)
.text_xs() .text_xs()
.child(shorten_pkey) .child(shorten_pkey)
.child( .child(Clipboard::new("copy").value_fn({
Button::new("copy") let public_key = self.public_key;
.icon({ move |_, _| public_key.to_bech32().unwrap_or_default().into()
if self.copied { })),
IconName::CircleCheck
} else {
IconName::Copy
}
})
.xsmall()
.secondary()
.on_click(cx.listener(move |this, _ev, window, cx| {
let pk = this.public_key.to_bech32().unwrap();
this.copy(pk, window, cx)
})),
),
), ),
) )
.when(self.editing, |this| { .when(self.editing, |this| {
@@ -544,7 +501,7 @@ fn field(
}; };
v_flex() v_flex()
.gap_1p5() .gap_1()
.child(label(text, cx)) .child(label(text, cx))
.child(div().text_xs().text_color(color).child(content)) .child(div().text_sm().text_color(color).child(content))
} }
+1 -1
View File
@@ -264,7 +264,7 @@ impl RelayManager {
fn render_tab_bar(&self, cx: &mut Context<Self>) -> impl IntoElement { fn render_tab_bar(&self, cx: &mut Context<Self>) -> impl IntoElement {
TabBar::new("relays-tabs") TabBar::new("relays-tabs")
.segmented() .pill()
.selected_index(self.active_tab) .selected_index(self.active_tab)
.on_click(cx.listener(|this, ix: &usize, _window, cx| { .on_click(cx.listener(|this, ix: &usize, _window, cx| {
this.active_tab = *ix; this.active_tab = *ix;
+140 -3
View File
@@ -1,19 +1,30 @@
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
use std::time::Duration;
use chat_ui::ToggleDictation;
#[cfg(not(target_arch = "wasm32"))]
use gpui::PathPromptOptions; use gpui::PathPromptOptions;
#[cfg(not(target_arch = "wasm32"))]
use gpui::Task;
use gpui::http_client::Url; use gpui::http_client::Url;
use gpui::{ use gpui::{
App, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Subscription, AnyElement, App, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled,
Window, div, Subscription, Window, div,
}; };
use gpui_kit::assets::IconName; use gpui_kit::assets::IconName;
use gpui_kit::component::button::{Button, ButtonVariants}; use gpui_kit::component::button::{Button, ButtonVariants};
use gpui_kit::component::input::{Input, InputEvent, InputState, NumberInput}; use gpui_kit::component::input::{Input, InputEvent, InputState, NumberInput};
use gpui_kit::component::kbd::Kbd;
use gpui_kit::component::menu::{DropdownMenu, PopupMenuItem}; use gpui_kit::component::menu::{DropdownMenu, PopupMenuItem};
use gpui_kit::component::notification::Notification; use gpui_kit::component::notification::Notification;
#[cfg(not(target_arch = "wasm32"))]
use gpui_kit::component::progress::Progress;
use gpui_kit::component::separator::Separator; use gpui_kit::component::separator::Separator;
use gpui_kit::component::switch::Switch; use gpui_kit::component::switch::Switch;
use gpui_kit::component::{Disableable, Sizable, WindowExt, h_flex, v_flex}; use gpui_kit::component::{Disableable, Sizable, WindowExt, h_flex, v_flex};
use settings::{AppSettings, AppearanceMode}; use settings::{AppSettings, AppearanceMode};
#[cfg(not(target_arch = "wasm32"))]
use speech::model::{self as speech_model};
use ui::{settings_block, settings_row}; use ui::{settings_block, settings_row};
/// Bounds for the theme number inputs /// Bounds for the theme number inputs
@@ -29,6 +40,8 @@ pub struct Preferences {
font_size_input: Entity<InputState>, font_size_input: Entity<InputState>,
radius_input: Entity<InputState>, radius_input: Entity<InputState>,
radius_lg_input: Entity<InputState>, radius_lg_input: Entity<InputState>,
#[cfg(not(target_arch = "wasm32"))]
_speech_poll: Task<()>,
/// Keeps the control subscriptions alive while the dialog is open /// Keeps the control subscriptions alive while the dialog is open
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
} }
@@ -94,11 +107,40 @@ impl Preferences {
}), }),
]; ];
#[cfg(not(target_arch = "wasm32"))]
let _speech_poll = cx.spawn_in(window, async move |this, cx| {
let timer = Duration::from_millis(500);
let mut last = speech_model::status();
loop {
cx.background_executor().timer(timer).await;
let status = speech_model::status();
let error = speech_model::take_error();
if status == last {
continue;
}
last = status;
this.update_in(cx, |_, window, cx| {
if let Some(error) = error {
window.push_notification(Notification::error(error).autohide(false), cx);
}
cx.notify();
})
.ok();
}
});
Self { Self {
file_input, file_input,
font_size_input, font_size_input,
radius_input, radius_input,
radius_lg_input, radius_lg_input,
#[cfg(not(target_arch = "wasm32"))]
_speech_poll,
_subscriptions: subscriptions, _subscriptions: subscriptions,
} }
} }
@@ -188,10 +230,102 @@ impl Preferences {
.detach(); .detach();
} }
} }
fn render_speech(
&mut self,
#[cfg_attr(target_arch = "wasm32", allow(unused_variables))] cx: &mut Context<Self>,
) -> AnyElement {
#[cfg(target_arch = "wasm32")]
{
return div().into_any_element();
}
#[cfg(not(target_arch = "wasm32"))]
{
let size = speech_model::total_size() / 1_000_000;
let status = speech_model::status();
let (description, control) = match &status {
speech_model::Status::NotInstalled => (
format!("{size} MB download"),
Button::new("download-model")
.label("Download")
.secondary()
.small()
.on_click(|_, _window, cx| {
speech_model::start_download();
cx.refresh_windows();
})
.into_any_element(),
),
speech_model::Status::Downloading { downloaded, total } => (
format!(
"Downloading · {} of {} MB",
downloaded / 1_000_000,
total / 1_000_000
),
Button::new("cancel-download")
.label("Cancel")
.ghost()
.small()
.on_click(|_, _window, cx| {
speech_model::cancel_download();
cx.refresh_windows();
})
.into_any_element(),
),
speech_model::Status::Installed => (
format!("Parakeet v3 · {size} MB"),
Button::new("remove-model")
.label("Remove")
.secondary()
.small()
.on_click(|_, _window, cx| {
speech_model::delete();
cx.refresh_windows();
})
.into_any_element(),
),
};
let mut block = v_flex().w_full().gap_3().child(settings_row(
"Speech model",
description,
control,
cx,
));
if let speech_model::Status::Downloading { downloaded, total } = &status {
let (downloaded, total) = (*downloaded, *total);
block = block.child(
Progress::new("speech-model-download")
.value(downloaded as f32 / total.max(1) as f32 * 100.)
.small(),
);
}
block.into_any_element()
}
}
/// Shows the keystroke actually bound to the dictation action at startup
fn render_dictation_shortcut(
&self,
window: &mut Window,
cx: &mut Context<Self>,
) -> impl IntoElement {
let control = match Kbd::global_binding_for_action(&ToggleDictation, window) {
Some(kbd) => kbd.into_any_element(),
None => div().into_any_element(),
};
settings_row("Shortcut", "Press to start or stop dictation", control, cx)
}
} }
impl Render for Preferences { impl Render for Preferences {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
const SCREENING: &str = "Show an screening dialog to verify the unknown sender."; const SCREENING: &str = "Show an screening dialog to verify the unknown sender.";
const WOT: &str = "Treat people followed by your contacts as known senders."; const WOT: &str = "Treat people followed by your contacts as known senders.";
const AVATAR: &str = "Hide all avatar pictures to improve performance."; const AVATAR: &str = "Hide all avatar pictures to improve performance.";
@@ -374,6 +508,9 @@ impl Render for Preferences {
cx, cx,
)) ))
.child(Separator::horizontal()) .child(Separator::horizontal())
.child(self.render_speech(cx))
.child(self.render_dictation_shortcut(window, cx))
.child(Separator::horizontal())
.child(settings_block( .child(settings_block(
"Media Upload Service", "Media Upload Service",
MEDIA, MEDIA,
+1 -2
View File
@@ -1,5 +1,4 @@
use std::cell::Cell; use std::cell::Cell;
use std::ops::Deref as _;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
@@ -166,7 +165,7 @@ impl CoopDockSkin {
.on_drag(ResizePanel, move |info, _, _, cx| { .on_drag(ResizePanel, move |info, _, _, cx| {
cx.stop_propagation(); cx.stop_propagation();
shared.resizing_dock().set(Some(placement)); shared.resizing_dock().set(Some(placement));
cx.new(|_| info.deref().clone()) cx.new(|_| info.clone())
}) })
} }
} }
+5 -4
View File
@@ -91,11 +91,12 @@ impl Workspace {
let workspace = cx.entity().downgrade(); let workspace = cx.entity().downgrade();
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
if AppSettings::get_appearance_mode(cx) == AppearanceMode::System { subscriptions.push(cx.observe_window_appearance(window, |_this, window, cx| {
subscriptions.push(cx.observe_window_appearance(window, |_this, window, cx| { if AppSettings::get_appearance_mode(cx) == AppearanceMode::System {
Theme::sync_system_appearance(Some(window), cx); Theme::sync_system_appearance(Some(window), cx);
})); AppSettings::apply_theme_style(cx);
} }
}));
subscriptions.push( subscriptions.push(
// Subscribe to the nostr events // Subscribe to the nostr events
+10 -49
View File
@@ -1,16 +1,14 @@
use anyhow::Error; use anyhow::Error;
use gpui::{ use gpui::{
App, AppContext, ClipboardItem, Context, Entity, EventEmitter, FocusHandle, Focusable, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, IntoElement,
IntoElement, ParentElement, Render, SharedString, Styled, Task, Window, div, ParentElement, Render, SharedString, Styled, Task, Window, div,
}; };
use gpui_kit::assets::IconName;
use gpui_kit::base::StyledExt as _; use gpui_kit::base::StyledExt as _;
use gpui_kit::component::button::{Button, ButtonVariants}; use gpui_kit::component::clipboard::Clipboard;
use gpui_kit::component::dock::{Panel, PanelEvent}; use gpui_kit::component::dock::{Panel, PanelEvent};
use gpui_kit::component::input::{Input, InputState}; use gpui_kit::component::input::{Input, InputState};
use gpui_kit::component::separator::Separator; use gpui_kit::component::separator::Separator;
use gpui_kit::component::{ActiveTheme, Sizable, v_flex}; use gpui_kit::component::{ActiveTheme, v_flex};
use instant::Duration;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use state::USER_KEYRING; use state::USER_KEYRING;
@@ -32,9 +30,6 @@ pub struct BackupPanel {
/// Secret key input /// Secret key input
nsec_input: Entity<InputState>, nsec_input: Entity<InputState>,
/// Copied status
copied: bool,
/// Background tasks /// Background tasks
tasks: Vec<Task<Result<(), Error>>>, tasks: Vec<Task<Result<(), Error>>>,
} }
@@ -54,7 +49,6 @@ impl BackupPanel {
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
npub_input, npub_input,
nsec_input, nsec_input,
copied: false,
tasks: vec![], tasks: vec![],
} }
} }
@@ -81,31 +75,6 @@ impl BackupPanel {
Ok(()) Ok(())
})); }));
} }
fn copy_secret(&mut self, cx: &mut Context<Self>) {
let value = self.nsec_input.read(cx).value();
let item = ClipboardItem::new_string(value.to_string());
cx.write_to_clipboard(item);
// Set the copied status to true
self.set_copied(true, cx);
}
fn set_copied(&mut self, status: bool, cx: &mut Context<Self>) {
self.copied = status;
cx.notify();
self.tasks.push(cx.spawn(async move |this, cx| {
cx.background_executor().timer(Duration::from_secs(2)).await;
// Clear the error message after a delay
this.update(cx, |this, cx| {
this.set_copied(false, cx);
})?;
Ok(())
}));
}
} }
impl gpui_kit::component::dock::BasePanel for BackupPanel { impl gpui_kit::component::dock::BasePanel for BackupPanel {
@@ -174,20 +143,12 @@ impl Render for BackupPanel {
.child(Input::new(&self.nsec_input).disabled(true)), .child(Input::new(&self.nsec_input).disabled(true)),
) )
.child( .child(
Button::new("copy") Clipboard::new("copy-secret")
.icon(IconName::Copy) .accessibility_label("Copy secret key")
.label({ .value_fn({
if self.copied { let nsec_input = self.nsec_input.clone();
"Copied" move |_, cx| nsec_input.read(cx).value()
} else { }),
"Copy secret key"
}
})
.primary()
.small()
.on_click(cx.listener(move |this, _ev, _window, cx| {
this.copy_secret(cx);
})),
), ),
) )
} }
+1 -1
View File
@@ -340,7 +340,7 @@ impl Sidebar {
) )
.child( .child(
div() div()
.text_sm() .text_xs()
.text_color(cx.theme().sidebar_foreground) .text_color(cx.theme().sidebar_foreground)
.child(label), .child(label),
) )
+7 -1
View File
@@ -14,7 +14,7 @@ product-name = "Coop"
description = "Chat Freely, Stay Private on Nostr" description = "Chat Freely, Stay Private on Nostr"
identifier = "su.reya.coop" identifier = "su.reya.coop"
category = "SocialNetworking" category = "SocialNetworking"
version = "2.0.0-alpha2" version = "2.0.0-alpha3"
out-dir = "../dist" out-dir = "../dist"
before-packaging-command = "cargo build --release" before-packaging-command = "cargo build --release"
resources = ["Cargo.toml", "src"] resources = ["Cargo.toml", "src"]
@@ -26,6 +26,11 @@ icons = [
"resources/icon.ico", "resources/icon.ico",
] ]
# Merged over the generated Info.plist; required for the speech input to
# request microphone and speech recognition access on macOS.
[package.metadata.packager.macos]
info_plist_path = "resources/Info.plist"
[dependencies] [dependencies]
assets = { path = "../crates/assets" } assets = { path = "../crates/assets" }
workspace = { path = "../crates/workspace" } workspace = { path = "../crates/workspace" }
@@ -33,6 +38,7 @@ ui = { path = "../crates/ui" }
state = { path = "../crates/state" } state = { path = "../crates/state" }
nip4e = { path = "../crates/nip4e" } nip4e = { path = "../crates/nip4e" }
chat = { path = "../crates/chat" } chat = { path = "../crates/chat" }
chat_ui = { path = "../crates/chat_ui" }
nip29 = { path = "../crates/nip29" } nip29 = { path = "../crates/nip29" }
settings = { path = "../crates/settings" } settings = { path = "../crates/settings" }
auto_update = { path = "../crates/auto_update" } auto_update = { path = "../crates/auto_update" }
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSMicrophoneUsageDescription</key>
<string>Dictate text into messages.</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>Turn your speech into text.</string>
</dict>
</plist>
+18 -18
View File
@@ -1,5 +1,6 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use chat_ui::ToggleDictation;
use gpui::{ use gpui::{
App, Bounds, Global, KeyBinding, Menu, MenuItem, SharedString, TitlebarOptions, App, Bounds, Global, KeyBinding, Menu, MenuItem, SharedString, TitlebarOptions,
WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind, WindowOptions, WindowBackgroundAppearance, WindowBounds, WindowDecorations, WindowKind, WindowOptions,
@@ -45,13 +46,19 @@ fn main() {
// Register the `quit` function // Register the `quit` function
cx.on_action(quit); cx.on_action(quit);
// Register the `quit` function with CMD+Q (macOS) // Register the `quit` function with CMD+Q (macOS) and dictation with CMD+D
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]); cx.bind_keys([
KeyBinding::new("cmd-q", Quit, None),
KeyBinding::new("cmd-d", ToggleDictation, None),
]);
// Register the `quit` function with Super+Q (others) // Register the `quit` function with Super+Q (others) and dictation with Ctrl+D
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
cx.bind_keys([KeyBinding::new("super-q", Quit, None)]); cx.bind_keys([
KeyBinding::new("super-q", Quit, None),
KeyBinding::new("ctrl-d", ToggleDictation, None),
]);
// Set menu items // Set menu items
cx.set_menus(vec![Menu { cx.set_menus(vec![Menu {
@@ -65,14 +72,13 @@ fn main() {
let appearance = settings::persisted_appearance_mode(); let appearance = settings::persisted_appearance_mode();
let font_size = settings::persisted_theme().font_size; let font_size = settings::persisted_theme().font_size;
match apply_native_theme(appearance, font_size, cx) {
Ok(()) => start_theme_watcher(appearance, cx), if let Err(error) = apply_native_theme(appearance, font_size, cx) {
Err(error) => { log::warn!("native theme unavailable ({error})");
log::warn!("native theme unavailable ({error})"); apply_appearance(appearance, cx);
apply_appearance(appearance, cx); AppSettings::apply_theme_style(cx);
AppSettings::apply_theme_style(cx);
}
} }
start_theme_watcher(cx);
// Initialize the nostr client // Initialize the nostr client
state::init(cx, cli_key); state::init(cx, cli_key);
@@ -206,14 +212,12 @@ fn apply_appearance(appearance: AppearanceMode, cx: &mut App) {
/// Holds the watcher subscription so it lives as long as the app. /// Holds the watcher subscription so it lives as long as the app.
struct ThemeWatcher { struct ThemeWatcher {
_subscription: ThemeSubscription, _subscription: ThemeSubscription,
/// The appearance mode the observer in `workspace` was registered with.
appearance: std::cell::Cell<AppearanceMode>,
} }
impl Global for ThemeWatcher {} impl Global for ThemeWatcher {}
/// Start a background task to watch for OS theme changes and re-apply the native theme. /// Start a background task to watch for OS theme changes and re-apply the native theme.
fn start_theme_watcher(initial_appearance: AppearanceMode, cx: &mut App) { fn start_theme_watcher(cx: &mut App) {
let (sender, receiver) = flume::unbounded(); let (sender, receiver) = flume::unbounded();
match native_theme::watch::on_theme_change(move |event| { match native_theme::watch::on_theme_change(move |event| {
if let Err(error) = sender.send(event) { if let Err(error) = sender.send(event) {
@@ -223,7 +227,6 @@ fn start_theme_watcher(initial_appearance: AppearanceMode, cx: &mut App) {
Ok(subscription) => { Ok(subscription) => {
cx.set_global(ThemeWatcher { cx.set_global(ThemeWatcher {
_subscription: subscription, _subscription: subscription,
appearance: std::cell::Cell::new(initial_appearance),
}); });
cx.spawn(async move |cx| { cx.spawn(async move |cx| {
while receiver.recv_async().await.is_ok() { while receiver.recv_async().await.is_ok() {
@@ -231,9 +234,6 @@ fn start_theme_watcher(initial_appearance: AppearanceMode, cx: &mut App) {
cx.update(|cx| { cx.update(|cx| {
let appearance = AppSettings::get_appearance_mode(cx); let appearance = AppSettings::get_appearance_mode(cx);
let font_size = AppSettings::get_theme(cx).font_size; let font_size = AppSettings::get_theme(cx).font_size;
if let Some(watcher) = cx.try_global::<ThemeWatcher>() {
watcher.appearance.set(appearance);
}
if let Err(error) = apply_native_theme(appearance, font_size, cx) { if let Err(error) = apply_native_theme(appearance, font_size, cx) {
log::warn!("failed to apply updated OS theme ({error})"); log::warn!("failed to apply updated OS theme ({error})");
} }
-1
View File
@@ -3,7 +3,6 @@ channel = "1.98"
profile = "minimal" profile = "minimal"
components = ["rustfmt", "clippy"] components = ["rustfmt", "clippy"]
targets = [ targets = [
"x86_64-apple-darwin",
"aarch64-apple-darwin", "aarch64-apple-darwin",
"x86_64-unknown-linux-gnu", "x86_64-unknown-linux-gnu",
"x86_64-pc-windows-msvc", "x86_64-pc-windows-msvc",
+1
View File
@@ -14,6 +14,7 @@ ui = { path = "../crates/ui" }
state = { path = "../crates/state" } state = { path = "../crates/state" }
nip4e = { path = "../crates/nip4e" } nip4e = { path = "../crates/nip4e" }
chat = { path = "../crates/chat" } chat = { path = "../crates/chat" }
chat_ui = { path = "../crates/chat_ui" }
nip29 = { path = "../crates/nip29" } nip29 = { path = "../crates/nip29" }
settings = { path = "../crates/settings" } settings = { path = "../crates/settings" }
person = { path = "../crates/person" } person = { path = "../crates/person" }
+5 -5
View File
@@ -21,12 +21,12 @@ if [[ "$1" == "--release" ]]; then
echo -e "${YELLOW}Building in release mode${NC}" echo -e "${YELLOW}Building in release mode${NC}"
fi fi
# wasm_thread requires a nightly compiler for its atomics features NIGHTLY="nightly-2026-10-31"
CARGO="cargo +nightly" CARGO="cargo +$NIGHTLY"
# Step 0: Toolchain prerequisites # Step 0: Toolchain prerequisites
echo -e "${GREEN}Step 0: Checking toolchain...${NC}" echo -e "${GREEN}Step 0: Checking toolchain...${NC}"
rustup target add wasm32-unknown-unknown --toolchain nightly rustup target add wasm32-unknown-unknown --toolchain "$NIGHTLY"
if [[ "$(uname)" == "Darwin" ]]; then if [[ "$(uname)" == "Darwin" ]]; then
# secp256k1-sys compiles C for wasm; Apple clang has the wasm32 target and # secp256k1-sys compiles C for wasm; Apple clang has the wasm32 target and
@@ -35,7 +35,7 @@ if [[ "$(uname)" == "Darwin" ]]; then
echo -e "${RED}Apple clang not found. Install the Xcode command line tools: xcode-select --install${NC}" echo -e "${RED}Apple clang not found. Install the Xcode command line tools: xcode-select --install${NC}"
exit 1 exit 1
fi fi
rustup component add llvm-tools-preview --toolchain nightly rustup component add llvm-tools-preview --toolchain "$NIGHTLY"
RUSTUP_HOME=$(rustup show home) RUSTUP_HOME=$(rustup show home)
if [[ "$(uname -m)" == "arm64" ]]; then if [[ "$(uname -m)" == "arm64" ]]; then
@@ -43,7 +43,7 @@ if [[ "$(uname)" == "Darwin" ]]; then
else else
HOST_ARCH="x86_64-apple-darwin" HOST_ARCH="x86_64-apple-darwin"
fi fi
LLVM_BIN="$RUSTUP_HOME/toolchains/nightly-$HOST_ARCH/lib/rustlib/$HOST_ARCH/bin" LLVM_BIN="$RUSTUP_HOME/toolchains/$NIGHTLY-$HOST_ARCH/lib/rustlib/$HOST_ARCH/bin"
export CC_wasm32_unknown_unknown=clang export CC_wasm32_unknown_unknown=clang
export AR_wasm32_unknown_unknown="$LLVM_BIN/llvm-ar" export AR_wasm32_unknown_unknown="$LLVM_BIN/llvm-ar"
+5
View File
@@ -78,6 +78,11 @@ pub fn run() -> Result<(), JsValue> {
APPLICATION.with(|application| { APPLICATION.with(|application| {
*application.borrow_mut() = Some(app.run_embedded(|cx| { *application.borrow_mut() = Some(app.run_embedded(|cx| {
cx.bind_keys([
KeyBinding::new("cmd-d", chat_ui::ToggleDictation, None),
KeyBinding::new("ctrl-d", chat_ui::ToggleDictation, None),
]);
gpui_kit::init(cx); gpui_kit::init(cx);
load_embedded_fonts(cx); load_embedded_fonts(cx);