use std::{collections::BTreeMap, fmt::Display, ops::RangeInclusive}; use common::{ config::Config, model::Id, share::{Codec, Fps, Kind, Quality, Resolution}, string_enum, }; use gpui_kit::{ App, Pixels, component::{Theme as KitTheme, ThemeMode}, px, }; use crate::{ gui::style::{Theme, Tokens}, lang::{Lang, w}, read_config, share::{audio::AudioMode, encode::capabilities}, voice::audio::Choice, write_config, }; pub const CAMERA_QUALITY: Quality = Quality { resolution: Resolution::P720, fps: Fps::F30, }; pub const SHARE_QUALITY: Quality = Quality { resolution: Resolution::P1080, fps: Fps::F30, }; pub const SENSITIVITY_DB: RangeInclusive = -80.0..=0.0; pub const VOLUME: RangeInclusive = 0.0..=200.0; string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq)] FontSize { Small => "small", Medium => "medium", Large => "large", } } impl FontSize { pub fn label(self) -> &'static str { match self { FontSize::Small => w().settings.small, FontSize::Medium => w().settings.medium, FontSize::Large => w().settings.large, } } pub fn pixels(self) -> Pixels { match self { FontSize::Small => px(13.), FontSize::Medium => px(14.), FontSize::Large => px(16.), } } } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq)] Launch { Off => "off", Window => "window", Tray => "tray", } } impl Launch { pub fn options() -> impl Iterator { Self::ALL .into_iter() .filter(|launch| crate::tray::AVAILABLE || *launch != Launch::Tray) } pub fn label(self) -> &'static str { match self { Launch::Off => w().settings_page.launch_off, Launch::Window => w().settings_page.launch_window, Launch::Tray => w().settings_page.launch_tray, } } } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq)] Activation { VoiceActivity => "voice_activity", PushToTalk => "push_to_talk", } } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq)] Profile { Voice => "voice", Music => "music", } } #[derive(Debug, Clone, PartialEq)] pub struct VoiceSettings { pub sensitivity_db: f32, pub input_volume: f32, pub output_volume: f32, pub choice: Choice, pub activation: Activation, pub profile: Profile, pub push_to_talk_key: String, pub user_volumes: BTreeMap, } impl Default for VoiceSettings { fn default() -> Self { Self { sensitivity_db: -40.0, input_volume: 100.0, output_volume: 100.0, choice: Choice::default(), activation: Activation::VoiceActivity, profile: Profile::Voice, push_to_talk_key: String::new(), user_volumes: BTreeMap::new(), } } } #[derive(Debug, Clone, PartialEq)] pub enum ShareAudio { Off, Everything, App { name: String, pid: Option }, Device(String), } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ShareCodec { Auto, Only(Codec), } #[derive(Debug, Clone, PartialEq)] pub struct ShareSettings { pub top: Quality, pub camera: Quality, pub camera_device: Option, pub codec: ShareCodec, pub audio: ShareAudio, pub excluded: Vec, pub volume: f32, } impl Default for ShareSettings { fn default() -> Self { Self { top: SHARE_QUALITY, camera: CAMERA_QUALITY, camera_device: None, codec: ShareCodec::Auto, audio: ShareAudio::Everything, excluded: Vec::new(), volume: 100.0, } } } impl ShareSettings { pub fn top(&self, kind: Kind) -> Quality { match kind { Kind::Screen => self.top, Kind::Camera => self.camera, } } pub fn encoding_differs(&self, other: &Self) -> bool { self.top != other.top || self.camera != other.camera || self.camera_device != other.camera_device || self.codec != other.codec || self.audio != other.audio || self.excluded != other.excluded } pub fn encode(&self) -> Vec { let capable = &capabilities().encode; match self.codec { ShareCodec::Auto => capable.clone(), ShareCodec::Only(codec) => capable.iter().copied().filter(|c| *c == codec).collect(), } } pub fn audio_mode(&self) -> Option { match &self.audio { ShareAudio::Off => None, ShareAudio::Everything => Some(AudioMode::Everything { excluded: self.excluded.clone(), }), ShareAudio::App { name, pid } => Some(AudioMode::App { name: name.clone(), pid: *pid, }), ShareAudio::Device(node) => Some(AudioMode::Device(node.clone())), } } } #[derive(Debug, Clone, PartialEq)] pub struct Settings { pub theme: Theme, pub font_size: FontSize, pub language: Lang, pub voice: VoiceSettings, pub share: ShareSettings, pub close_to_tray: bool, pub launch: Launch, pub clock_seconds: bool, pub jump_usage: BTreeMap, pub last_chats: BTreeMap, } impl Settings { pub fn clock(&self) -> &'static str { if self.clock_seconds { crate::gui::CLOCK_SECONDS } else { crate::gui::CLOCK } } } impl Default for Settings { fn default() -> Self { Self { theme: Theme::Gece, font_size: FontSize::Medium, language: Lang::system(), voice: VoiceSettings::default(), share: ShareSettings::default(), close_to_tray: true, launch: if crate::tray::AVAILABLE { Launch::Tray } else { Launch::Window }, clock_seconds: false, jump_usage: BTreeMap::new(), last_chats: BTreeMap::new(), } } } fn quality_text(quality: Quality) -> String { format!("{}p{}", quality.resolution.height(), quality.fps.value()) } fn parse_quality(text: &str) -> Option { let (height, fps) = text.split_once('p')?; let height: u32 = height.parse().ok()?; let fps: u32 = fps.parse().ok()?; Some(Quality { resolution: Resolution::ALL .into_iter() .find(|resolution| resolution.height() == height)?, fps: Fps::ALL.into_iter().find(|rate| rate.value() == fps)?, }) } pub fn load() -> Settings { let config = read_config("settings").and_then(|content| Config::parse(&content).ok()); let text = |key: &str| { config .as_ref() .and_then(|config| config.get(key).ok()) .filter(|value| !value.is_empty()) }; let number = |key: &str, default: f32, range: &RangeInclusive| { text(key) .and_then(|value| value.parse::().ok()) .filter(|value| range.contains(value)) .unwrap_or(default) }; let defaults = Settings::default(); let voice = VoiceSettings { sensitivity_db: number( "voice.sensitivity", defaults.voice.sensitivity_db, &SENSITIVITY_DB, ), input_volume: number("voice.input_volume", defaults.voice.input_volume, &VOLUME), output_volume: number("voice.output_volume", defaults.voice.output_volume, &VOLUME), choice: Choice { input: text("voice.input"), output: text("voice.output"), }, activation: text("voice.activation") .and_then(|value| Activation::parse(&value)) .unwrap_or(defaults.voice.activation), profile: text("voice.profile") .and_then(|value| Profile::parse(&value)) .unwrap_or(defaults.voice.profile), push_to_talk_key: text("voice.push_to_talk").unwrap_or_default(), user_volumes: text("voice.user_volumes") .map(|value| { value .split(',') .filter_map(|pair| pair.split_once('=')) .filter_map(|(user_id, volume)| { Some((user_id.parse().ok()?, volume.parse().ok()?)) }) .filter(|(_, volume)| VOLUME.contains(volume)) .collect() }) .unwrap_or_default(), }; let share = ShareSettings { top: text("share.top") .and_then(|value| parse_quality(&value)) .unwrap_or(defaults.share.top), camera: text("share.camera") .and_then(|value| parse_quality(&value)) .unwrap_or(defaults.share.camera), camera_device: text("share.camera_device"), codec: text("share.codec") .and_then(|value| Codec::parse(&value)) .map_or(ShareCodec::Auto, ShareCodec::Only), audio: match text("share.audio").as_deref() { Some("off") => ShareAudio::Off, Some("app") => text("share.audio_source").map_or(ShareAudio::Everything, |name| { ShareAudio::App { name, pid: None } }), Some("source") => { text("share.audio_source").map_or(ShareAudio::Everything, ShareAudio::Device) } _ => ShareAudio::Everything, }, excluded: text("share.excluded") .map(|value| value.split('\t').map(str::to_string).collect()) .unwrap_or_default(), volume: number("share.volume", defaults.share.volume, &VOLUME), }; Settings { theme: text("theme") .and_then(|value| Theme::parse(&value)) .unwrap_or(defaults.theme), font_size: text("font_size") .and_then(|value| FontSize::parse(&value)) .unwrap_or(defaults.font_size), language: text("language") .and_then(|value| Lang::parse(&value)) .unwrap_or(defaults.language), voice, share, close_to_tray: text("close_to_tray") .map_or(defaults.close_to_tray, |value| value != "false"), launch: text("launch") .and_then(|value| Launch::parse(&value)) .filter(|launch| Launch::options().any(|option| option == *launch)) .unwrap_or(defaults.launch), clock_seconds: text("clock_seconds") .map_or(defaults.clock_seconds, |value| value == "true"), jump_usage: text("jump.usage") .map(|value| { value .split('\t') .filter_map(|pair| pair.rsplit_once('=')) .filter_map(|(place, count)| Some((place.to_string(), count.parse().ok()?))) .collect() }) .unwrap_or_default(), last_chats: text("last_chats") .map(|value| { value .split(',') .filter_map(|pair| pair.split_once('=')) .filter_map(|(user, chat)| Some((user.parse().ok()?, chat.parse().ok()?))) .collect() }) .unwrap_or_default(), } } fn line(key: &str, value: impl Display) -> String { format!("{key} = \"{value}\"\n") } pub fn save(settings: &Settings) { let voice = &settings.voice; let share = &settings.share; let (audio, source) = match &share.audio { ShareAudio::Off => ("off", ""), ShareAudio::Everything => ("everything", ""), ShareAudio::App { name, .. } => ("app", name.as_str()), ShareAudio::Device(node) => ("source", node.as_str()), }; let content = [ line("theme", settings.theme.as_str()), line("font_size", settings.font_size.as_str()), line("language", settings.language.as_str()), line("close_to_tray", settings.close_to_tray), line("launch", settings.launch.as_str()), line("clock_seconds", settings.clock_seconds), line("voice.sensitivity", voice.sensitivity_db), line("voice.input_volume", voice.input_volume), line("voice.output_volume", voice.output_volume), line( "voice.input", voice.choice.input.as_deref().unwrap_or_default(), ), line( "voice.output", voice.choice.output.as_deref().unwrap_or_default(), ), line("voice.activation", voice.activation.as_str()), line("voice.profile", voice.profile.as_str()), line("voice.push_to_talk", &voice.push_to_talk_key), line( "voice.user_volumes", voice .user_volumes .iter() .map(|(user_id, volume)| format!("{user_id}={volume}")) .collect::>() .join(","), ), line("share.top", quality_text(share.top)), line("share.camera", quality_text(share.camera)), line( "share.camera_device", share.camera_device.as_deref().unwrap_or_default(), ), line( "share.codec", match share.codec { ShareCodec::Auto => "auto", ShareCodec::Only(codec) => codec.name(), }, ), line("share.audio", audio), line("share.audio_source", source), line( "share.excluded", share .excluded .iter() .filter(|entry| !entry.contains('#')) .cloned() .collect::>() .join("\t"), ), line("share.volume", share.volume), line( "jump.usage", settings .jump_usage .iter() .map(|(place, count)| format!("{place}={count}")) .collect::>() .join("\t"), ), line( "last_chats", settings .last_chats .iter() .map(|(user, chat)| format!("{user}={chat}")) .collect::>() .join(","), ), ] .concat(); write_config("settings", &content); } pub fn apply(theme: Theme, size: FontSize, cx: &mut App) { cx.set_global(Tokens { theme, size }); let mode = if theme.colors().dark { ThemeMode::Dark } else { ThemeMode::Light }; KitTheme::change(mode, None, cx); crate::gui::sync_theme(cx); }