feat #14740: automatic call detection on windows
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+129
-21
@@ -1,4 +1,5 @@
|
||||
//! Tauri commands and the UI side effects of recording (windows, events).
|
||||
//! Tauri commands and the UI side effects of recording (windows, events),
|
||||
//! shared by the UI, the tray and call detection.
|
||||
//!
|
||||
//! The handlers stay thin: the work happens in [`AppState`]. Anything that
|
||||
//! blocks (audio devices, stopping a recorder, the keyring) runs on the
|
||||
@@ -11,15 +12,22 @@ use recorder_core::capture::Devices;
|
||||
use recorder_core::model::CallId;
|
||||
use recorder_core::secrets::Service;
|
||||
use recorder_core::settings::Settings;
|
||||
use serde::Serialize;
|
||||
use tauri::{AppHandle, Emitter, Manager, State};
|
||||
use tauri_plugin_autostart::ManagerExt;
|
||||
|
||||
use crate::state::{AppState, DeviceLevels, RecordingState};
|
||||
use crate::state::{AppState, CallBrief, DeviceLevels, Ended, RecordingState};
|
||||
|
||||
const LEVELS_INTERVAL: Duration = Duration::from_millis(100);
|
||||
|
||||
type CmdResult<T> = Result<T, String>;
|
||||
|
||||
/// Payload of the `call-ended` event: a call awaits the prompt's decision.
|
||||
#[derive(Serialize, Clone)]
|
||||
struct CallEnded {
|
||||
id: CallId,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_settings(state: State<'_, AppState>) -> Settings {
|
||||
state.settings()
|
||||
@@ -95,32 +103,122 @@ pub async fn discard_current_recording(app: AppHandle) -> CmdResult<()> {
|
||||
blocking(app, discard).await
|
||||
}
|
||||
|
||||
/// The call the end-of-call prompt asks about, so the prompt can render even
|
||||
/// if it missed the `call-ended` event.
|
||||
#[tauri::command]
|
||||
pub fn get_pending_prompt(state: State<'_, AppState>) -> Option<CallBrief> {
|
||||
state.pending_prompt()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_call_brief(state: State<'_, AppState>, id: CallId) -> CmdResult<CallBrief> {
|
||||
state.call_brief(id)
|
||||
}
|
||||
|
||||
/// Deletes a finished call's audio and row; closes the prompt if it asked
|
||||
/// about this call.
|
||||
#[tauri::command]
|
||||
pub async fn discard_call(app: AppHandle, id: CallId) -> CmdResult<()> {
|
||||
blocking(app, move |app| {
|
||||
if app.state::<AppState>().discard_call(id)? {
|
||||
set_window_visible(app, "prompt", false);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Placeholder until transcription exists; the prompt shows the error.
|
||||
#[tauri::command]
|
||||
pub fn transcribe_call(id: CallId) -> CmdResult<()> {
|
||||
let _ = id;
|
||||
Err("Transkription ist noch nicht verfügbar".to_string())
|
||||
}
|
||||
|
||||
/// Starts a manual recording, shows the floating window and streams levels.
|
||||
pub fn start_manual(app: &AppHandle) -> CmdResult<CallId> {
|
||||
let state = app.state::<AppState>().start_manual_recording()?;
|
||||
let RecordingState::Recording { call_id, .. } = state else {
|
||||
return Err("Aufnahme wurde nicht gestartet".to_string());
|
||||
};
|
||||
spawn_level_emitter(app.clone(), call_id);
|
||||
set_window_visible(app, "recorder", true);
|
||||
emit_state(app, &state);
|
||||
let call_id = app.state::<AppState>().start_manual_recording()?;
|
||||
on_started(app, call_id);
|
||||
Ok(call_id)
|
||||
}
|
||||
|
||||
/// Stops the current recording and hides the floating window.
|
||||
pub fn stop(app: &AppHandle) -> CmdResult<()> {
|
||||
let result = app.state::<AppState>().stop_recording();
|
||||
set_window_visible(app, "recorder", false);
|
||||
emit_state(app, &RecordingState::Idle);
|
||||
result.map(|_| ())
|
||||
/// Starts recording a call that detection found (`app` is its display
|
||||
/// name, `root_pid` the process tree whose audio is the far end).
|
||||
pub fn start_detected(app: &AppHandle, name: &str, root_pid: u32) -> CmdResult<CallId> {
|
||||
let call_id = app
|
||||
.state::<AppState>()
|
||||
.start_detected_recording(name, root_pid)?;
|
||||
on_started(app, call_id);
|
||||
Ok(call_id)
|
||||
}
|
||||
|
||||
/// Throws the current recording away and hides the floating window.
|
||||
/// Stops the current recording at the user's request (■ Stopp). Stopping
|
||||
/// a detected call also keeps detection from restarting it until the call
|
||||
/// app releases the mic.
|
||||
pub fn stop(app: &AppHandle) -> CmdResult<()> {
|
||||
let state = app.state::<AppState>();
|
||||
let ended = state.stop_recording();
|
||||
if ended.as_ref().is_some_and(|ended| ended.detected) {
|
||||
state.suppress_detected_call();
|
||||
}
|
||||
after_stop(app, ended)
|
||||
}
|
||||
|
||||
/// Stops the current recording because detection saw the call end. A
|
||||
/// manual recording is left running.
|
||||
pub fn end_detected_call(app: &AppHandle) {
|
||||
let ended = app.state::<AppState>().stop_detected_recording();
|
||||
if let Err(e) = after_stop(app, ended) {
|
||||
log::error!("finishing the detected call failed: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Throws the current recording away. Never shows the prompt; a discarded
|
||||
/// detected call is not detected again until the call app releases the mic.
|
||||
pub fn discard(app: &AppHandle) -> CmdResult<()> {
|
||||
let result = app.state::<AppState>().discard_current_recording();
|
||||
set_window_visible(app, "recorder", false);
|
||||
emit_state(app, &RecordingState::Idle);
|
||||
result
|
||||
let state = app.state::<AppState>();
|
||||
let ended = state.discard_current_recording();
|
||||
if ended.as_ref().is_some_and(|ended| ended.detected) {
|
||||
state.suppress_detected_call();
|
||||
}
|
||||
sync_recorder_window(app);
|
||||
ended.map_or(Ok(()), |ended| ended.result)
|
||||
}
|
||||
|
||||
/// Streams levels, shows the floating window and announces a recording
|
||||
/// that just started.
|
||||
fn on_started(app: &AppHandle, call_id: CallId) {
|
||||
spawn_level_emitter(app.clone(), call_id);
|
||||
set_window_visible(app, "recorder", true);
|
||||
emit_state(app, &app.state::<AppState>().recording_state());
|
||||
}
|
||||
|
||||
/// After a recording stopped: updates the floating window and, if the call
|
||||
/// was saved, asks what to do with it in the prompt window.
|
||||
fn after_stop(app: &AppHandle, ended: Option<Ended>) -> CmdResult<()> {
|
||||
sync_recorder_window(app);
|
||||
let Some(ended) = ended else {
|
||||
return Ok(());
|
||||
};
|
||||
ended.result?;
|
||||
let id = ended.call_id;
|
||||
app.state::<AppState>().set_pending_prompt(id);
|
||||
show_and_focus(app, "prompt");
|
||||
if let Err(e) = app.emit("call-ended", CallEnded { id }) {
|
||||
log::error!("could not emit call-ended: {e}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Announces the actual recording state and hides the floating window only
|
||||
/// if nothing records any more: detection may already have started the
|
||||
/// next recording.
|
||||
fn sync_recorder_window(app: &AppHandle) {
|
||||
let state = app.state::<AppState>().recording_state();
|
||||
if matches!(state, RecordingState::Idle) {
|
||||
set_window_visible(app, "recorder", false);
|
||||
}
|
||||
emit_state(app, &state);
|
||||
}
|
||||
|
||||
/// Enables or disables launching at login. Debug builds leave the login
|
||||
@@ -142,7 +240,17 @@ pub fn apply_autostart(app: &AppHandle, enabled: bool) {
|
||||
}
|
||||
|
||||
pub fn show_main(app: &AppHandle) {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
show_and_focus(app, "main");
|
||||
}
|
||||
|
||||
/// Brings the floating window to the front (tray "Aufnahme manuell
|
||||
/// starten" while a recording runs).
|
||||
pub fn show_recorder(app: &AppHandle) {
|
||||
show_and_focus(app, "recorder");
|
||||
}
|
||||
|
||||
fn show_and_focus(app: &AppHandle, label: &str) {
|
||||
if let Some(window) = app.get_webview_window(label) {
|
||||
let _ = window.unminimize();
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
|
||||
Reference in New Issue
Block a user