feat #14740: settings, keyring, tray and manual recording
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
//! Tauri commands and the UI side effects of recording (windows, events).
|
||||
//!
|
||||
//! The handlers stay thin: the work happens in [`AppState`]. Anything that
|
||||
//! blocks (audio devices, stopping a recorder, the keyring) runs on the
|
||||
//! blocking pool so the UI thread never waits on it.
|
||||
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use recorder_core::capture::Devices;
|
||||
use recorder_core::model::CallId;
|
||||
use recorder_core::secrets::Service;
|
||||
use recorder_core::settings::Settings;
|
||||
use tauri::{AppHandle, Emitter, Manager, State};
|
||||
use tauri_plugin_autostart::ManagerExt;
|
||||
|
||||
use crate::state::{AppState, DeviceLevels, RecordingState};
|
||||
|
||||
const LEVELS_INTERVAL: Duration = Duration::from_millis(100);
|
||||
|
||||
type CmdResult<T> = Result<T, String>;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_settings(state: State<'_, AppState>) -> Settings {
|
||||
state.settings()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_settings(
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
settings: Settings,
|
||||
) -> CmdResult<()> {
|
||||
let autostart = settings.autostart;
|
||||
state.save_settings(settings)?;
|
||||
apply_autostart(&app, autostart);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Stores `key` in the keyring; an empty key deletes the entry.
|
||||
#[tauri::command]
|
||||
pub async fn set_api_key(app: AppHandle, service: Service, key: String) -> CmdResult<()> {
|
||||
blocking(app, move |app| {
|
||||
let secrets = &app.state::<AppState>().secrets;
|
||||
let key = key.trim();
|
||||
let result = if key.is_empty() {
|
||||
secrets.delete(service)
|
||||
} else {
|
||||
secrets.set(service, key)
|
||||
};
|
||||
result.map_err(|e| e.to_string())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Whether a key is stored. Never returns the key itself.
|
||||
#[tauri::command]
|
||||
pub async fn has_api_key(app: AppHandle, service: Service) -> CmdResult<bool> {
|
||||
blocking(app, move |app| {
|
||||
let key = app.state::<AppState>().secrets.get(service);
|
||||
Ok(key.is_some_and(|key| !key.is_empty()))
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn list_devices() -> CmdResult<Devices> {
|
||||
tauri::async_runtime::spawn_blocking(recorder_audio::list_devices)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn test_devices(app: AppHandle) -> CmdResult<DeviceLevels> {
|
||||
blocking(app, |app| app.state::<AppState>().test_devices()).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_recording_state(state: State<'_, AppState>) -> RecordingState {
|
||||
state.recording_state()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn start_manual_recording(app: AppHandle) -> CmdResult<CallId> {
|
||||
blocking(app, start_manual).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn stop_recording(app: AppHandle) -> CmdResult<()> {
|
||||
blocking(app, stop).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn discard_current_recording(app: AppHandle) -> CmdResult<()> {
|
||||
blocking(app, discard).await
|
||||
}
|
||||
|
||||
/// 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);
|
||||
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(|_| ())
|
||||
}
|
||||
|
||||
/// Throws the current recording away and hides the floating window.
|
||||
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
|
||||
}
|
||||
|
||||
/// Enables or disables launching at login. Debug builds leave the login
|
||||
/// items alone so `cargo tauri dev` never registers a dev binary.
|
||||
pub fn apply_autostart(app: &AppHandle, enabled: bool) {
|
||||
if cfg!(debug_assertions) {
|
||||
log::info!("debug build: not changing autostart (wanted {enabled})");
|
||||
return;
|
||||
}
|
||||
let autolaunch = app.autolaunch();
|
||||
let result = if enabled {
|
||||
autolaunch.enable()
|
||||
} else {
|
||||
autolaunch.disable()
|
||||
};
|
||||
if let Err(e) = result {
|
||||
log::error!("could not apply autostart={enabled}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_main(app: &AppHandle) {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
let _ = window.unminimize();
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
}
|
||||
|
||||
/// Brings back the floating window if the user closed it mid-recording.
|
||||
pub fn show_recorder_if_recording(app: &AppHandle) {
|
||||
if matches!(
|
||||
app.state::<AppState>().recording_state(),
|
||||
RecordingState::Recording { .. }
|
||||
) {
|
||||
set_window_visible(app, "recorder", true);
|
||||
}
|
||||
}
|
||||
|
||||
/// Emits `levels` every 100 ms until `call_id` stops being the current
|
||||
/// recording.
|
||||
fn spawn_level_emitter(app: AppHandle, call_id: CallId) {
|
||||
thread::spawn(move || loop {
|
||||
thread::sleep(LEVELS_INTERVAL);
|
||||
let Some(levels) = app.state::<AppState>().levels(call_id) else {
|
||||
break;
|
||||
};
|
||||
let _ = app.emit("levels", levels);
|
||||
});
|
||||
}
|
||||
|
||||
fn emit_state(app: &AppHandle, state: &RecordingState) {
|
||||
if let Err(e) = app.emit("recording-state", state) {
|
||||
log::error!("could not emit recording-state: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
fn set_window_visible(app: &AppHandle, label: &str, visible: bool) {
|
||||
if let Some(window) = app.get_webview_window(label) {
|
||||
let result = if visible {
|
||||
window.show()
|
||||
} else {
|
||||
window.hide()
|
||||
};
|
||||
if let Err(e) = result {
|
||||
log::error!("could not change visibility of window {label}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn blocking<T, F>(app: AppHandle, f: F) -> CmdResult<T>
|
||||
where
|
||||
T: Send + 'static,
|
||||
F: FnOnce(&AppHandle) -> CmdResult<T> + Send + 'static,
|
||||
{
|
||||
tauri::async_runtime::spawn_blocking(move || f(&app))
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
Reference in New Issue
Block a user