config and ffmpeg bootstrap
This commit is contained in:
+145
-1
@@ -1 +1,145 @@
|
||||
// TOML config load/save - state lives in ./config.toml next to the exe
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default)]
|
||||
pub export: ExportConfig,
|
||||
#[serde(default)]
|
||||
pub capture: CaptureConfig,
|
||||
#[serde(default)]
|
||||
pub hotkeys: HotkeyConfig,
|
||||
#[serde(default)]
|
||||
pub updates: UpdatesConfig,
|
||||
#[serde(default)]
|
||||
pub output_dir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ExportConfig {
|
||||
pub default_format: Format,
|
||||
pub gif_dither: GifDither,
|
||||
pub webm_codec: WebmCodec,
|
||||
pub mp4_codec: Mp4Codec,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CaptureConfig {
|
||||
pub default_fps: u32,
|
||||
pub capture_cursor: bool,
|
||||
pub capture_audio: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct HotkeyConfig {
|
||||
pub start: Option<String>,
|
||||
pub stop: Option<String>,
|
||||
pub pause: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UpdatesConfig {
|
||||
pub check_on_launch: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Format {
|
||||
Gif,
|
||||
Webm,
|
||||
Mp4,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum GifDither {
|
||||
FloydSteinberg,
|
||||
Bayer,
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum WebmCodec {
|
||||
Vp9,
|
||||
Av1,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Mp4Codec {
|
||||
H264,
|
||||
H265,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
export: ExportConfig::default(),
|
||||
capture: CaptureConfig::default(),
|
||||
hotkeys: HotkeyConfig::default(),
|
||||
updates: UpdatesConfig::default(),
|
||||
output_dir: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ExportConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
default_format: Format::Gif,
|
||||
gif_dither: GifDither::FloydSteinberg,
|
||||
webm_codec: WebmCodec::Vp9,
|
||||
mp4_codec: Mp4Codec::H264,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CaptureConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
default_fps: 30,
|
||||
capture_cursor: true,
|
||||
capture_audio: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UpdatesConfig {
|
||||
fn default() -> Self {
|
||||
Self { check_on_launch: true }
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load_or_create(path: &Path) -> Result<Self> {
|
||||
if !path.exists() {
|
||||
let cfg = Self::default();
|
||||
cfg.save(path)?;
|
||||
return Ok(cfg);
|
||||
}
|
||||
let raw = std::fs::read_to_string(path)?;
|
||||
Ok(toml::from_str(&raw)?)
|
||||
}
|
||||
|
||||
pub fn save(&self, path: &Path) -> Result<()> {
|
||||
let serialized = toml::to_string_pretty(self)?;
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
std::fs::write(path, serialized)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn needs_first_run(&self) -> bool {
|
||||
self.hotkeys.start.is_none()
|
||||
|| self.hotkeys.stop.is_none()
|
||||
|| self.hotkeys.pause.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn config_path(app_dir: &Path) -> PathBuf {
|
||||
app_dir.join("config.toml")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user