Persist recording settings locally
This commit is contained in:
1 parent
7598b954b3
commit
389bdbc604
5 files changed
+134
-10
No files matched your search
Generated
+2
@@ -83,6 +83,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"reqwest",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sysinfo",
|
||||
]
|
||||
@@ -855,6 +856,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -16,6 +16,7 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "multipart", "rustls-tls"] }
|
||||
rusqlite = { version = "0.32", features = ["bundled"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
sysinfo = "0.31"
|
||||
|
||||
|
||||
+32
-5
@@ -15,7 +15,7 @@ use clipforge::media::{
|
||||
};
|
||||
use clipforge::models::{Clip, ClipSource, GameEvent, RecordingStatus};
|
||||
use clipforge::recorder::{RecorderAction, RecorderService};
|
||||
use clipforge::settings::AppSettings;
|
||||
use clipforge::settings::{load_or_create_settings, save_settings};
|
||||
use clipforge::storage::{clip_path, is_inside_root, LibraryPaths};
|
||||
use clipforge::upload::{CatboxUploader, UploadMetadata, Uploader};
|
||||
use serde::Serialize;
|
||||
@@ -31,6 +31,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use tauri::{Manager, State};
|
||||
|
||||
struct AppRuntime {
|
||||
library_root: PathBuf,
|
||||
recorder: Mutex<RecorderService>,
|
||||
database: Mutex<ClipDatabase>,
|
||||
capture: Mutex<Option<ActiveCapture>>,
|
||||
@@ -113,6 +114,27 @@ fn list_clips(runtime: State<'_, AppRuntime>) -> Result<Vec<ClipDto>, String> {
|
||||
Ok(recorder.library.all().iter().map(clip_to_dto).collect())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_replay_buffer(seconds: u64, runtime: State<'_, AppRuntime>) -> Result<DesktopStatus, String> {
|
||||
let mut recorder = runtime.recorder.lock().map_err(|error| error.to_string())?;
|
||||
let capture = runtime.capture.lock().map_err(|error| error.to_string())?;
|
||||
recorder.settings.replay_buffer = Duration::from_secs(seconds);
|
||||
recorder.settings.clamp_replay_buffer();
|
||||
save_settings(&runtime.library_root, &recorder.settings)
|
||||
.map_err(|error| format!("Could not save settings: {error}"))?;
|
||||
Ok(status_from_recorder(&recorder, capture.as_ref()))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_mic_enabled(enabled: bool, runtime: State<'_, AppRuntime>) -> Result<DesktopStatus, String> {
|
||||
let mut recorder = runtime.recorder.lock().map_err(|error| error.to_string())?;
|
||||
let capture = runtime.capture.lock().map_err(|error| error.to_string())?;
|
||||
recorder.settings.privacy.mic_enabled = enabled;
|
||||
save_settings(&runtime.library_root, &recorder.settings)
|
||||
.map_err(|error| format!("Could not save settings: {error}"))?;
|
||||
Ok(status_from_recorder(&recorder, capture.as_ref()))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn poll_auto_clip_events(runtime: State<'_, AppRuntime>) -> Result<Vec<ClipDto>, String> {
|
||||
let (session_id, game_id, session_started_at) = {
|
||||
@@ -557,10 +579,12 @@ fn upload_clip(clip_id: String, runtime: State<'_, AppRuntime>) -> Result<ClipDt
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let (recorder, database) = create_runtime().expect("could not initialize ClipForge runtime");
|
||||
let (library_root, recorder, database) =
|
||||
create_runtime().expect("could not initialize ClipForge runtime");
|
||||
|
||||
tauri::Builder::default()
|
||||
.manage(AppRuntime {
|
||||
library_root,
|
||||
recorder: Mutex::new(recorder),
|
||||
database: Mutex::new(database),
|
||||
capture: Mutex::new(None),
|
||||
@@ -618,6 +642,8 @@ fn main() {
|
||||
get_status,
|
||||
refresh_detected_game,
|
||||
list_clips,
|
||||
set_replay_buffer,
|
||||
set_mic_enabled,
|
||||
poll_auto_clip_events,
|
||||
save_manual_clip,
|
||||
start_capture,
|
||||
@@ -631,11 +657,12 @@ fn main() {
|
||||
.expect("error while running ClipForge desktop shell");
|
||||
}
|
||||
|
||||
fn create_runtime() -> Result<(RecorderService, ClipDatabase), String> {
|
||||
fn create_runtime() -> Result<(PathBuf, RecorderService, ClipDatabase), String> {
|
||||
let root = env::current_dir()
|
||||
.unwrap_or_else(|_| PathBuf::from("."))
|
||||
.join("clipforge-library");
|
||||
let settings = AppSettings::default_for_root(&root);
|
||||
let settings = load_or_create_settings(&root)
|
||||
.map_err(|error| format!("Could not load settings: {error}"))?;
|
||||
let paths = LibraryPaths::new(&root);
|
||||
paths.ensure().map_err(|error| error.to_string())?;
|
||||
let database = ClipDatabase::open(root.join("library.sqlite"))
|
||||
@@ -649,7 +676,7 @@ fn create_runtime() -> Result<(RecorderService, ClipDatabase), String> {
|
||||
}
|
||||
|
||||
let _ = detect_game(&[], &default_profiles());
|
||||
Ok((recorder, database))
|
||||
Ok((root, recorder, database))
|
||||
}
|
||||
|
||||
fn add_session_clip(
|
||||
|
||||
+67
-5
@@ -1,8 +1,12 @@
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct QualityPreset {
|
||||
pub name: String,
|
||||
pub width: u32,
|
||||
@@ -11,7 +15,7 @@ pub struct QualityPreset {
|
||||
pub bitrate_kbps: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct HotkeySettings {
|
||||
pub clip_last_60s: String,
|
||||
pub clip_last_30s: String,
|
||||
@@ -19,7 +23,7 @@ pub struct HotkeySettings {
|
||||
pub screenshot: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PrivacySettings {
|
||||
pub mic_enabled: bool,
|
||||
pub desktop_capture_requires_confirmation: bool,
|
||||
@@ -27,7 +31,7 @@ pub struct PrivacySettings {
|
||||
pub upload_requires_confirmation: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AutoClipSettings {
|
||||
pub enabled: bool,
|
||||
pub pre_roll: Duration,
|
||||
@@ -36,7 +40,7 @@ pub struct AutoClipSettings {
|
||||
pub enabled_events_by_game: BTreeMap<String, BTreeSet<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct AppSettings {
|
||||
pub schema_version: u32,
|
||||
pub clip_root: PathBuf,
|
||||
@@ -96,3 +100,61 @@ impl AppSettings {
|
||||
.clamp(Duration::from_secs(15), Duration::from_secs(600));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_or_create_settings(root: impl Into<PathBuf>) -> io::Result<AppSettings> {
|
||||
let root = root.into();
|
||||
let path = settings_path(&root);
|
||||
if path.exists() {
|
||||
let mut settings: AppSettings = serde_json::from_str(&fs::read_to_string(&path)?)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
settings.clamp_replay_buffer();
|
||||
Ok(settings)
|
||||
} else {
|
||||
let settings = AppSettings::default_for_root(&root);
|
||||
save_settings(&root, &settings)?;
|
||||
Ok(settings)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_settings(root: impl Into<PathBuf>, settings: &AppSettings) -> io::Result<()> {
|
||||
let root = root.into();
|
||||
fs::create_dir_all(&root)?;
|
||||
let contents = serde_json::to_string_pretty(settings)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
fs::write(settings_path(&root), contents)
|
||||
}
|
||||
|
||||
pub fn settings_path(root: impl Into<PathBuf>) -> PathBuf {
|
||||
root.into().join("settings.json")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn settings_round_trip_as_versioned_json() {
|
||||
let root =
|
||||
std::env::temp_dir().join(format!("clipforge-settings-{}", SystemTimeCompat::millis()));
|
||||
let mut settings = AppSettings::default_for_root(&root);
|
||||
settings.replay_buffer = Duration::from_secs(120);
|
||||
|
||||
save_settings(&root, &settings).expect("save");
|
||||
let loaded = load_or_create_settings(&root).expect("load");
|
||||
|
||||
assert_eq!(loaded.schema_version, 1);
|
||||
assert_eq!(loaded.replay_buffer, Duration::from_secs(120));
|
||||
let _ = fs::remove_dir_all(root);
|
||||
}
|
||||
|
||||
struct SystemTimeCompat;
|
||||
|
||||
impl SystemTimeCompat {
|
||||
fn millis() -> u128 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_millis()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -477,10 +477,14 @@ function bindEvents() {
|
||||
state.replayBufferSeconds = Number((event.target as HTMLInputElement).value);
|
||||
render();
|
||||
});
|
||||
appRoot.querySelector<HTMLInputElement>("[data-action='buffer-length']")?.addEventListener("change", (event) => {
|
||||
void saveReplayBufferSetting(Number((event.target as HTMLInputElement).value));
|
||||
});
|
||||
|
||||
appRoot.querySelector<HTMLInputElement>("[data-action='toggle-mic']")?.addEventListener("change", (event) => {
|
||||
state.micEnabled = (event.target as HTMLInputElement).checked;
|
||||
render();
|
||||
void saveMicSetting(state.micEnabled);
|
||||
});
|
||||
|
||||
appRoot.querySelector<HTMLInputElement>("[data-action='toggle-auto-upload']")?.addEventListener("change", (event) => {
|
||||
@@ -655,6 +659,34 @@ async function refreshClips() {
|
||||
}
|
||||
}
|
||||
|
||||
async function saveReplayBufferSetting(seconds: number) {
|
||||
if (!tauriInvoke) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const status = await tauriInvoke<DesktopStatus>("set_replay_buffer", { seconds });
|
||||
applyDesktopStatus(status);
|
||||
render();
|
||||
} catch (error) {
|
||||
console.warn("Could not save replay buffer setting", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function saveMicSetting(enabled: boolean) {
|
||||
if (!tauriInvoke) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const status = await tauriInvoke<DesktopStatus>("set_mic_enabled", { enabled });
|
||||
applyDesktopStatus(status);
|
||||
render();
|
||||
} catch (error) {
|
||||
console.warn("Could not save mic setting", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshStatus() {
|
||||
if (!tauriInvoke) {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user