yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use std::{collections::BTreeMap, fmt::Display, ops::RangeInclusive, path::PathBuf}; |
| 2 | |
| 3 | use common::{ |
| 4 | config::Config, |
| 5 | model::Id, |
| 6 | share::{Codec, Fps, Kind, Quality, Resolution}, |
| 7 | string_enum, |
| 8 | }; |
| 9 | use gpui_kit::{ |
| 10 | App, Global, Pixels, |
| 11 | component::{Theme as KitTheme, ThemeMode}, |
| 12 | px, |
| 13 | }; |
| 14 | |
| 15 | use crate::{ |
| 16 | gui::style::{Look, Theme}, |
| 17 | lang::{Lang, w}, |
| 18 | read_config, |
| 19 | share::{audio::AudioMode, encode::capabilities}, |
| 20 | voice::audio::Choice, |
| 21 | write_config, |
| 22 | }; |
| 23 | |
| 24 | pub const CAMERA_QUALITY: Quality = Quality { |
| 25 | resolution: Resolution::P720, |
| 26 | fps: Fps::F30, |
| 27 | }; |
| 28 | |
| 29 | pub const SHARE_QUALITY: Quality = Quality { |
| 30 | resolution: Resolution::P1080, |
| 31 | fps: Fps::F30, |
| 32 | }; |
| 33 | |
| 34 | pub const SENSITIVITY_DB: RangeInclusive<f32> = -80.0..=0.0; |
| 35 | pub const VOLUME: RangeInclusive<f32> = 0.0..=200.0; |
| 36 | |
| 37 | string_enum! { |
| 38 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 39 | FontSize { |
| 40 | Small => "small", |
| 41 | Medium => "medium", |
| 42 | Large => "large", |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub struct Scale(pub FontSize); |
| 47 | |
| 48 | impl Global for Scale {} |
| 49 | |
| 50 | impl FontSize { |
| 51 | pub fn label(self) -> &'static str { |
| 52 | match self { |
| 53 | FontSize::Small => w().settings.small, |
| 54 | FontSize::Medium => w().settings.medium, |
| 55 | FontSize::Large => w().settings.large, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub fn pixels(self) -> Pixels { |
| 60 | match self { |
| 61 | FontSize::Small => px(13.), |
| 62 | FontSize::Medium => px(14.), |
| 63 | FontSize::Large => px(16.), |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | string_enum! { |
| 69 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 70 | Launch { |
| 71 | Off => "off", |
| 72 | Window => "window", |
| 73 | Tray => "tray", |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl Launch { |
| 78 | pub fn options() -> impl Iterator<Item = Self> { |
| 79 | Self::ALL |
| 80 | .into_iter() |
| 81 | .filter(|launch| crate::tray::AVAILABLE || *launch != Launch::Tray) |
| 82 | } |
| 83 | |
| 84 | pub fn label(self) -> &'static str { |
| 85 | match self { |
| 86 | Launch::Off => w().settings_page.launch_off, |
| 87 | Launch::Window => w().settings_page.launch_window, |
| 88 | Launch::Tray => w().settings_page.launch_tray, |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | string_enum! { |
| 94 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 95 | Activation { |
| 96 | VoiceActivity => "voice_activity", |
| 97 | PushToTalk => "push_to_talk", |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | string_enum! { |
| 102 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 103 | Profile { |
| 104 | Voice => "voice", |
| 105 | Music => "music", |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #[derive(Debug, Clone, PartialEq)] |
| 110 | pub struct VoiceSettings { |
| 111 | pub sensitivity_db: f32, |
| 112 | pub input_volume: f32, |
| 113 | pub output_volume: f32, |
| 114 | pub choice: Choice, |
| 115 | pub activation: Activation, |
| 116 | pub profile: Profile, |
| 117 | pub push_to_talk_key: String, |
| 118 | pub user_volumes: BTreeMap<Id, f32>, |
| 119 | } |
| 120 | |
| 121 | impl Default for VoiceSettings { |
| 122 | fn default() -> Self { |
| 123 | Self { |
| 124 | sensitivity_db: -40.0, |
| 125 | input_volume: 100.0, |
| 126 | output_volume: 100.0, |
| 127 | choice: Choice::default(), |
| 128 | activation: Activation::VoiceActivity, |
| 129 | profile: Profile::Voice, |
| 130 | push_to_talk_key: String::new(), |
| 131 | user_volumes: BTreeMap::new(), |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #[derive(Debug, Clone, PartialEq)] |
| 137 | pub enum ShareAudio { |
| 138 | Off, |
| 139 | Everything, |
| 140 | App { name: String, pid: Option<u32> }, |
| 141 | Device(String), |
| 142 | } |
| 143 | |
| 144 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 145 | pub enum ShareCodec { |
| 146 | Auto, |
| 147 | Only(Codec), |
| 148 | } |
| 149 | |
| 150 | #[derive(Debug, Clone, PartialEq)] |
| 151 | pub struct ShareSettings { |
| 152 | pub top: Quality, |
| 153 | pub camera: Quality, |
| 154 | pub camera_device: Option<String>, |
| 155 | pub codec: ShareCodec, |
| 156 | pub audio: ShareAudio, |
| 157 | pub excluded: Vec<String>, |
| 158 | pub volume: f32, |
| 159 | } |
| 160 | |
| 161 | impl Default for ShareSettings { |
| 162 | fn default() -> Self { |
| 163 | Self { |
| 164 | top: SHARE_QUALITY, |
| 165 | camera: CAMERA_QUALITY, |
| 166 | camera_device: None, |
| 167 | codec: ShareCodec::Auto, |
| 168 | audio: ShareAudio::Everything, |
| 169 | excluded: Vec::new(), |
| 170 | volume: 100.0, |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | impl ShareSettings { |
| 176 | pub fn top(&self, kind: Kind) -> Quality { |
| 177 | match kind { |
| 178 | Kind::Screen => self.top, |
| 179 | Kind::Camera => self.camera, |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | pub fn encoding_differs(&self, other: &Self) -> bool { |
| 184 | self.top != other.top |
| 185 | || self.camera != other.camera |
| 186 | || self.camera_device != other.camera_device |
| 187 | || self.codec != other.codec |
| 188 | || self.audio != other.audio |
| 189 | || self.excluded != other.excluded |
| 190 | } |
| 191 | |
| 192 | pub fn encode(&self) -> Vec<Codec> { |
| 193 | let capable = &capabilities().encode; |
| 194 | match self.codec { |
| 195 | ShareCodec::Auto => capable.clone(), |
| 196 | ShareCodec::Only(codec) => capable.iter().copied().filter(|c| *c == codec).collect(), |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | pub fn audio_mode(&self) -> Option<AudioMode> { |
| 201 | match &self.audio { |
| 202 | ShareAudio::Off => None, |
| 203 | ShareAudio::Everything => Some(AudioMode::Everything { |
| 204 | excluded: self.excluded.clone(), |
| 205 | }), |
| 206 | ShareAudio::App { name, pid } => Some(AudioMode::App { |
| 207 | name: name.clone(), |
| 208 | pid: *pid, |
| 209 | }), |
| 210 | ShareAudio::Device(node) => Some(AudioMode::Device(node.clone())), |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #[derive(Debug, Clone, PartialEq)] |
| 216 | pub struct Settings { |
| 217 | pub theme: Theme, |
| 218 | pub font_size: FontSize, |
| 219 | pub language: Lang, |
| 220 | pub voice: VoiceSettings, |
| 221 | pub share: ShareSettings, |
| 222 | pub close_to_tray: bool, |
| 223 | pub launch: Launch, |
| 224 | pub clock_seconds: bool, |
| 225 | pub jump_usage: BTreeMap<String, u32>, |
| 226 | pub last_chats: BTreeMap<Id, Id>, |
| 227 | pub release_source: Option<PathBuf>, |
| 228 | } |
| 229 | |
| 230 | impl Settings { |
| 231 | pub fn clock(&self) -> &'static str { |
| 232 | if self.clock_seconds { |
| 233 | crate::gui::CLOCK_SECONDS |
| 234 | } else { |
| 235 | crate::gui::CLOCK |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | impl Default for Settings { |
| 241 | fn default() -> Self { |
| 242 | Self { |
| 243 | theme: Theme::Gece, |
| 244 | font_size: FontSize::Medium, |
| 245 | language: Lang::system(), |
| 246 | voice: VoiceSettings::default(), |
| 247 | share: ShareSettings::default(), |
| 248 | close_to_tray: true, |
| 249 | launch: if crate::tray::AVAILABLE { |
| 250 | Launch::Tray |
| 251 | } else { |
| 252 | Launch::Window |
| 253 | }, |
| 254 | clock_seconds: false, |
| 255 | jump_usage: BTreeMap::new(), |
| 256 | last_chats: BTreeMap::new(), |
| 257 | release_source: None, |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | fn quality_text(quality: Quality) -> String { |
| 263 | format!("{}p{}", quality.resolution.height(), quality.fps.value()) |
| 264 | } |
| 265 | |
| 266 | fn parse_quality(text: &str) -> Option<Quality> { |
| 267 | let (height, fps) = text.split_once('p')?; |
| 268 | let height: u32 = height.parse().ok()?; |
| 269 | let fps: u32 = fps.parse().ok()?; |
| 270 | |
| 271 | Some(Quality { |
| 272 | resolution: Resolution::ALL |
| 273 | .into_iter() |
| 274 | .find(|resolution| resolution.height() == height)?, |
| 275 | fps: Fps::ALL.into_iter().find(|rate| rate.value() == fps)?, |
| 276 | }) |
| 277 | } |
| 278 | |
| 279 | pub fn load() -> Settings { |
| 280 | let config = read_config("settings").and_then(|content| Config::parse(&content).ok()); |
| 281 | let text = |key: &str| { |
| 282 | config |
| 283 | .as_ref() |
| 284 | .and_then(|config| config.get(key).ok()) |
| 285 | .filter(|value| !value.is_empty()) |
| 286 | }; |
| 287 | let number = |key: &str, default: f32, range: &RangeInclusive<f32>| { |
| 288 | text(key) |
| 289 | .and_then(|value| value.parse::<f32>().ok()) |
| 290 | .filter(|value| range.contains(value)) |
| 291 | .unwrap_or(default) |
| 292 | }; |
| 293 | |
| 294 | let defaults = Settings::default(); |
| 295 | let voice = VoiceSettings { |
| 296 | sensitivity_db: number( |
| 297 | "voice.sensitivity", |
| 298 | defaults.voice.sensitivity_db, |
| 299 | &SENSITIVITY_DB, |
| 300 | ), |
| 301 | input_volume: number("voice.input_volume", defaults.voice.input_volume, &VOLUME), |
| 302 | output_volume: number("voice.output_volume", defaults.voice.output_volume, &VOLUME), |
| 303 | choice: Choice { |
| 304 | input: text("voice.input"), |
| 305 | output: text("voice.output"), |
| 306 | }, |
| 307 | activation: text("voice.activation") |
| 308 | .and_then(|value| Activation::parse(&value)) |
| 309 | .unwrap_or(defaults.voice.activation), |
| 310 | profile: text("voice.profile") |
| 311 | .and_then(|value| Profile::parse(&value)) |
| 312 | .unwrap_or(defaults.voice.profile), |
| 313 | push_to_talk_key: text("voice.push_to_talk").unwrap_or_default(), |
| 314 | user_volumes: text("voice.user_volumes") |
| 315 | .map(|value| { |
| 316 | value |
| 317 | .split(',') |
| 318 | .filter_map(|pair| pair.split_once('=')) |
| 319 | .filter_map(|(user_id, volume)| { |
| 320 | Some((user_id.parse().ok()?, volume.parse().ok()?)) |
| 321 | }) |
| 322 | .filter(|(_, volume)| VOLUME.contains(volume)) |
| 323 | .collect() |
| 324 | }) |
| 325 | .unwrap_or_default(), |
| 326 | }; |
| 327 | |
| 328 | let share = ShareSettings { |
| 329 | top: text("share.top") |
| 330 | .and_then(|value| parse_quality(&value)) |
| 331 | .unwrap_or(defaults.share.top), |
| 332 | camera: text("share.camera") |
| 333 | .and_then(|value| parse_quality(&value)) |
| 334 | .unwrap_or(defaults.share.camera), |
| 335 | camera_device: text("share.camera_device"), |
| 336 | codec: text("share.codec") |
| 337 | .and_then(|value| Codec::parse(&value)) |
| 338 | .map_or(ShareCodec::Auto, ShareCodec::Only), |
| 339 | audio: match text("share.audio").as_deref() { |
| 340 | Some("off") => ShareAudio::Off, |
| 341 | Some("app") => text("share.audio_source").map_or(ShareAudio::Everything, |name| { |
| 342 | ShareAudio::App { name, pid: None } |
| 343 | }), |
| 344 | Some("source") => { |
| 345 | text("share.audio_source").map_or(ShareAudio::Everything, ShareAudio::Device) |
| 346 | } |
| 347 | _ => ShareAudio::Everything, |
| 348 | }, |
| 349 | excluded: text("share.excluded") |
| 350 | .map(|value| value.split('\t').map(str::to_string).collect()) |
| 351 | .unwrap_or_default(), |
| 352 | volume: number("share.volume", defaults.share.volume, &VOLUME), |
| 353 | }; |
| 354 | |
| 355 | Settings { |
| 356 | theme: text("theme") |
| 357 | .and_then(|value| Theme::parse(&value)) |
| 358 | .unwrap_or(defaults.theme), |
| 359 | font_size: text("font_size") |
| 360 | .and_then(|value| FontSize::parse(&value)) |
| 361 | .unwrap_or(defaults.font_size), |
| 362 | language: text("language") |
| 363 | .and_then(|value| Lang::parse(&value)) |
| 364 | .unwrap_or(defaults.language), |
| 365 | voice, |
| 366 | share, |
| 367 | close_to_tray: text("close_to_tray") |
| 368 | .map_or(defaults.close_to_tray, |value| value != "false"), |
| 369 | launch: text("launch") |
| 370 | .and_then(|value| Launch::parse(&value)) |
| 371 | .filter(|launch| Launch::options().any(|option| option == *launch)) |
| 372 | .unwrap_or(defaults.launch), |
| 373 | clock_seconds: text("clock_seconds") |
| 374 | .map_or(defaults.clock_seconds, |value| value == "true"), |
| 375 | jump_usage: text("jump.usage") |
| 376 | .map(|value| { |
| 377 | value |
| 378 | .split('\t') |
| 379 | .filter_map(|pair| pair.rsplit_once('=')) |
| 380 | .filter_map(|(place, count)| Some((place.to_string(), count.parse().ok()?))) |
| 381 | .collect() |
| 382 | }) |
| 383 | .unwrap_or_default(), |
| 384 | last_chats: text("last_chats") |
| 385 | .map(|value| { |
| 386 | value |
| 387 | .split(',') |
| 388 | .filter_map(|pair| pair.split_once('=')) |
| 389 | .filter_map(|(user, chat)| Some((user.parse().ok()?, chat.parse().ok()?))) |
| 390 | .collect() |
| 391 | }) |
| 392 | .unwrap_or_default(), |
| 393 | release_source: text("release.source").map(PathBuf::from), |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | fn line(key: &str, value: impl Display) -> String { |
| 398 | format!("{key} = \"{value}\"\n") |
| 399 | } |
| 400 | |
| 401 | pub fn save(settings: &Settings) { |
| 402 | let voice = &settings.voice; |
| 403 | let share = &settings.share; |
| 404 | let (audio, source) = match &share.audio { |
| 405 | ShareAudio::Off => ("off", ""), |
| 406 | ShareAudio::Everything => ("everything", ""), |
| 407 | ShareAudio::App { name, .. } => ("app", name.as_str()), |
| 408 | ShareAudio::Device(node) => ("source", node.as_str()), |
| 409 | }; |
| 410 | let content = [ |
| 411 | line("theme", settings.theme.as_str()), |
| 412 | line("font_size", settings.font_size.as_str()), |
| 413 | line("language", settings.language.as_str()), |
| 414 | line("close_to_tray", settings.close_to_tray), |
| 415 | line("launch", settings.launch.as_str()), |
| 416 | line("clock_seconds", settings.clock_seconds), |
| 417 | line("voice.sensitivity", voice.sensitivity_db), |
| 418 | line("voice.input_volume", voice.input_volume), |
| 419 | line("voice.output_volume", voice.output_volume), |
| 420 | line( |
| 421 | "voice.input", |
| 422 | voice.choice.input.as_deref().unwrap_or_default(), |
| 423 | ), |
| 424 | line( |
| 425 | "voice.output", |
| 426 | voice.choice.output.as_deref().unwrap_or_default(), |
| 427 | ), |
| 428 | line("voice.activation", voice.activation.as_str()), |
| 429 | line("voice.profile", voice.profile.as_str()), |
| 430 | line("voice.push_to_talk", &voice.push_to_talk_key), |
| 431 | line( |
| 432 | "voice.user_volumes", |
| 433 | voice |
| 434 | .user_volumes |
| 435 | .iter() |
| 436 | .map(|(user_id, volume)| format!("{user_id}={volume}")) |
| 437 | .collect::<Vec<_>>() |
| 438 | .join(","), |
| 439 | ), |
| 440 | line("share.top", quality_text(share.top)), |
| 441 | line("share.camera", quality_text(share.camera)), |
| 442 | line( |
| 443 | "share.camera_device", |
| 444 | share.camera_device.as_deref().unwrap_or_default(), |
| 445 | ), |
| 446 | line( |
| 447 | "share.codec", |
| 448 | match share.codec { |
| 449 | ShareCodec::Auto => "auto", |
| 450 | ShareCodec::Only(codec) => codec.name(), |
| 451 | }, |
| 452 | ), |
| 453 | line("share.audio", audio), |
| 454 | line("share.audio_source", source), |
| 455 | line( |
| 456 | "share.excluded", |
| 457 | share |
| 458 | .excluded |
| 459 | .iter() |
| 460 | .filter(|entry| !entry.contains('#')) |
| 461 | .cloned() |
| 462 | .collect::<Vec<_>>() |
| 463 | .join("\t"), |
| 464 | ), |
| 465 | line("share.volume", share.volume), |
| 466 | line( |
| 467 | "jump.usage", |
| 468 | settings |
| 469 | .jump_usage |
| 470 | .iter() |
| 471 | .map(|(place, count)| format!("{place}={count}")) |
| 472 | .collect::<Vec<_>>() |
| 473 | .join("\t"), |
| 474 | ), |
| 475 | line( |
| 476 | "release.source", |
| 477 | settings |
| 478 | .release_source |
| 479 | .as_ref() |
| 480 | .map(|path| path.display().to_string()) |
| 481 | .unwrap_or_default(), |
| 482 | ), |
| 483 | line( |
| 484 | "last_chats", |
| 485 | settings |
| 486 | .last_chats |
| 487 | .iter() |
| 488 | .map(|(user, chat)| format!("{user}={chat}")) |
| 489 | .collect::<Vec<_>>() |
| 490 | .join(","), |
| 491 | ), |
| 492 | ] |
| 493 | .concat(); |
| 494 | |
| 495 | write_config("settings", &content); |
| 496 | } |
| 497 | |
| 498 | pub fn apply(theme: Theme, size: FontSize, cx: &mut App) { |
| 499 | cx.set_global(Look { theme, size }); |
| 500 | cx.set_global(Scale(size)); |
| 501 | let mode = if theme.colors().dark { |
| 502 | ThemeMode::Dark |
| 503 | } else { |
| 504 | ThemeMode::Light |
| 505 | }; |
| 506 | KitTheme::change(mode, None, cx); |
| 507 | crate::gui::sync_theme(cx); |
| 508 | } |