The GPUI shell repeated the Slint crate's connect, observe, gateway lookup, save and remove of a server, its default nickname, its FallbackSecrets and its settings opening and key registration. They now live once and both front ends call them: - voelin_ui_vm::servers::sequence: connect_voice(_to), observe, discover_gateway, gateway_found, select_server, save_bookmark, save_and_connect, delete_bookmark, bookmark_form and default_nickname, as transitions over the state, the Cx and a Backing (the store, the secrets, the identity a connection uses). The commands, the status line and what to keep go out through the Update as for every other transition; a new field, Update::discover, asks the front end to look a gateway up (the thread hop stays with each front end: Later in GPUI, later() in Slint). stream_peer is the one input a front end gives (Slint's codecs; GPUI passes none). - voelin_ui_vm::settings::open_settings and register_keys; a front end adds its own key (ui.window, studio.ui) through a function it passes, so Slint's settings page still lists what it listed. - voelin_store::FallbackSecrets (feature keyring) and Store::identity_for. - voelin-ui: the methods of servers/bind.rs, open_settings and default_secrets call them, so their call sites are unchanged; host.rs gets transition_with and the discover arm of perform. voelin-gpui: AppState's methods and the Backend call them. What cannot move is named in docs/ui.md. voelin-ui-vm now depends on tracing (the sequences log as the copies did): one line of Cargo.lock, THIRD_PARTY_NOTICES.md unchanged (scripts/notices.sh --check clean), cargo deny check clean. The sequences have ten tests over a fake front end (commands, status, store, secrets, gateways), open_settings and Store::identity_for one each; the GPUI and Slint suites pass unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
832 lines
28 KiB
Rust
832 lines
28 KiB
Rust
//! The UI's settings keys (besides the engine's, in
|
||
//! `voelin_core::settings`), and their conversion to and from the forms of
|
||
//! the settings page.
|
||
|
||
use std::collections::BTreeMap;
|
||
use std::path::{Path, PathBuf};
|
||
|
||
use serde::{Deserialize, Serialize};
|
||
use tracing::warn;
|
||
use voelin_audio::process::NoiseLevel;
|
||
use voelin_core::settings::{Key, Kind, Settings};
|
||
use voelin_core::{AudioSettings, TransmitMode};
|
||
|
||
use crate::bridge::AudioForm;
|
||
|
||
/// The window's settings, one value (the `ui` blob of earlier versions).
|
||
pub static UI: Key<UiSettings> = Key::new(
|
||
"ui",
|
||
Kind::Json,
|
||
"The window's settings: push-to-talk key, H.264, the share dialog's choices.",
|
||
UiSettings::default,
|
||
);
|
||
|
||
/// Volume and mute of clients, by unique id.
|
||
pub static CLIENT_PLAYBACK: Key<ClientPlaybackMap> = Key::new(
|
||
"client_playback",
|
||
Kind::Json,
|
||
"Volume and mute of clients, by unique id.",
|
||
ClientPlaybackMap::new,
|
||
);
|
||
|
||
/// Colours of the window.
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "lowercase")]
|
||
pub enum ThemeChoice {
|
||
/// The dark palette.
|
||
#[default]
|
||
Dark,
|
||
/// The light palette.
|
||
Light,
|
||
/// The system's light or dark scheme.
|
||
System,
|
||
}
|
||
|
||
impl ThemeChoice {
|
||
/// The value stored for this choice.
|
||
pub fn as_str(self) -> &'static str {
|
||
match self {
|
||
Self::Dark => "dark",
|
||
Self::Light => "light",
|
||
Self::System => "system",
|
||
}
|
||
}
|
||
|
||
/// The choice a stored value names (dark for any other).
|
||
pub fn parse(text: &str) -> Self {
|
||
match text {
|
||
"light" => Self::Light,
|
||
"system" => Self::System,
|
||
_ => Self::Dark,
|
||
}
|
||
}
|
||
}
|
||
|
||
/// `ui.theme`. (In a config file write `"ui.theme" = "light"` at the top:
|
||
/// a `[ui]` table would be read as the `ui` blob.)
|
||
pub static UI_THEME: Key<ThemeChoice> = Key::new(
|
||
"ui.theme",
|
||
Kind::Choice(&["dark", "light", "system"]),
|
||
"Colours of the window: dark, light, or the system's scheme.",
|
||
ThemeChoice::default,
|
||
);
|
||
|
||
fn positive_scale(v: &f32) -> Result<(), String> {
|
||
if v.is_finite() && *v > 0.0 { Ok(()) } else { Err("must be a number above 0".into()) }
|
||
}
|
||
|
||
/// `ui.font_scale`: text size, 1 = normal (no upper limit).
|
||
pub static UI_FONT_SCALE: Key<f32> =
|
||
Key::new("ui.font_scale", Kind::Json, "Text size as a factor (a number; 1 = normal).", || 1.0)
|
||
.validated(positive_scale);
|
||
|
||
/// `ui.narrow_breakpoint`: below this window width (logical pixels) the
|
||
/// phone layout is used.
|
||
pub static UI_NARROW_BREAKPOINT: Key<u32> = Key::new(
|
||
"ui.narrow_breakpoint",
|
||
Kind::UInt { min: 0 },
|
||
"Window width in pixels below which the phone layout is used.",
|
||
|| 800,
|
||
);
|
||
|
||
/// `ui.image_cache_mb`: memory for decoded images (avatars, icons, emoji).
|
||
pub static UI_IMAGE_CACHE_MB: Key<u32> = Key::new(
|
||
"ui.image_cache_mb",
|
||
Kind::UInt { min: 0 },
|
||
"Memory in MB for decoded images (avatars, icons, banners, emoji); 0 keeps none.",
|
||
|| 256,
|
||
);
|
||
|
||
/// `ui.members_width`: how wide the members and streams panel is, in
|
||
/// pixels. Dragging its handle stores it; there is no built-in maximum.
|
||
pub static UI_MEMBERS_WIDTH: Key<u32> = Key::new(
|
||
"ui.members_width",
|
||
Kind::UInt { min: 0 },
|
||
"Width of the members and streams panel in pixels.",
|
||
|| 280,
|
||
);
|
||
|
||
/// `ui.voice_compact`: the voice channel view's smaller stage (a strip of
|
||
/// people and stream rows, more room for the chat). Its toggle stores it.
|
||
pub static UI_VOICE_COMPACT: Key<bool> = Key::new(
|
||
"ui.voice_compact",
|
||
Kind::Bool,
|
||
"The voice channel view's smaller stage, leaving more room for the chat.",
|
||
|| false,
|
||
);
|
||
|
||
/// Where the main window was and how large, as the last run left it: size and
|
||
/// position in logical pixels, and whether it was maximized (then the size is
|
||
/// the one it returns to). The position is left out where the platform does
|
||
/// not tell it (Wayland). A front end keeps it in [`UI_WINDOW`].
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||
pub struct WindowState {
|
||
/// Width in logical pixels.
|
||
pub width: u32,
|
||
/// Height in logical pixels.
|
||
pub height: u32,
|
||
/// Distance of the window's left edge from the screen's, when known.
|
||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||
pub x: Option<i32>,
|
||
/// Distance of the window's top edge from the screen's, when known.
|
||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||
pub y: Option<i32>,
|
||
/// The window was maximized.
|
||
#[serde(default)]
|
||
pub maximized: bool,
|
||
}
|
||
|
||
impl WindowState {
|
||
/// The smallest size a window can have been left at.
|
||
pub const MIN_SIZE: (u32, u32) = (320, 240);
|
||
/// The largest size and the farthest position taken as real: a value
|
||
/// beyond it is a damaged setting, not a screen.
|
||
pub const LIMIT: u32 = 16_384;
|
||
|
||
/// Whether the values can be a window: a size within [`Self::MIN_SIZE`]
|
||
/// and [`Self::LIMIT`], a position within it either way, and a position
|
||
/// that is given whole or not at all.
|
||
pub fn is_sane(&self) -> bool {
|
||
let limit = i64::from(Self::LIMIT);
|
||
(Self::MIN_SIZE.0..=Self::LIMIT).contains(&self.width)
|
||
&& (Self::MIN_SIZE.1..=Self::LIMIT).contains(&self.height)
|
||
&& self.x.is_some() == self.y.is_some()
|
||
&& [self.x, self.y].into_iter().flatten().all(|at| i64::from(at).abs() <= limit)
|
||
}
|
||
}
|
||
|
||
fn valid_window(state: &Option<WindowState>) -> Result<(), String> {
|
||
match state {
|
||
Some(state) if !state.is_sane() => Err("a window of 320x240 to 16384x16384".into()),
|
||
_ => Ok(()),
|
||
}
|
||
}
|
||
|
||
/// `ui.window`: the main window's size and position as the last run left them
|
||
/// (none until a run has). The GPUI front end writes it; the Slint one does
|
||
/// not read it.
|
||
pub static UI_WINDOW: Key<Option<WindowState>> = Key::new(
|
||
"ui.window",
|
||
Kind::Json,
|
||
"The main window's size and position as the last run left them (none: not stored yet).",
|
||
|| None,
|
||
)
|
||
.validated(valid_window);
|
||
|
||
/// The UI's keys besides [`UI`] and [`CLIENT_PLAYBACK`], for registering.
|
||
pub fn appearance_keys() -> [&'static dyn voelin_core::settings::Setting; 6] {
|
||
[
|
||
&UI_THEME,
|
||
&UI_FONT_SCALE,
|
||
&UI_NARROW_BREAKPOINT,
|
||
&UI_IMAGE_CACHE_MB,
|
||
&UI_MEMBERS_WIDTH,
|
||
&UI_VOICE_COMPACT,
|
||
]
|
||
}
|
||
|
||
/// What a kind of notification does: nothing, the bell, or the bell and a
|
||
/// desktop notification.
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "lowercase")]
|
||
pub enum NotifyLevel {
|
||
/// Nothing.
|
||
Off,
|
||
/// The bell in the app.
|
||
App,
|
||
/// The bell and a desktop notification.
|
||
#[default]
|
||
Desktop,
|
||
}
|
||
|
||
impl NotifyLevel {
|
||
/// The position in the settings' list (off, app, desktop).
|
||
pub fn index(self) -> i32 {
|
||
self as i32
|
||
}
|
||
|
||
/// The choice at a list position (desktop for any other).
|
||
pub fn from_index(index: i32) -> Self {
|
||
match index {
|
||
0 => Self::Off,
|
||
1 => Self::App,
|
||
_ => Self::Desktop,
|
||
}
|
||
}
|
||
}
|
||
|
||
const NOTIFY_LEVELS: Kind = Kind::Choice(&["off", "app", "desktop"]);
|
||
|
||
/// `notify.*`: what the bell and the desktop say about each kind.
|
||
pub static NOTIFY_MENTIONS: Key<NotifyLevel> = Key::new(
|
||
"notify.mentions",
|
||
NOTIFY_LEVELS,
|
||
"Our name in a channel or server chat: off, app (the bell) or desktop.",
|
||
NotifyLevel::default,
|
||
);
|
||
/// Private messages: off, app (the bell) or desktop.
|
||
pub static NOTIFY_MESSAGES: Key<NotifyLevel> = Key::new(
|
||
"notify.private_messages",
|
||
NOTIFY_LEVELS,
|
||
"Private messages: off, app (the bell) or desktop.",
|
||
NotifyLevel::default,
|
||
);
|
||
/// Pokes: off, app (the bell) or desktop.
|
||
pub static NOTIFY_POKES: Key<NotifyLevel> =
|
||
Key::new("notify.pokes", NOTIFY_LEVELS, "Pokes: off, app (the bell) or desktop.", || {
|
||
NotifyLevel::Desktop
|
||
});
|
||
/// Reminders of scheduled events: off, app (the bell) or desktop.
|
||
pub static NOTIFY_EVENTS: Key<NotifyLevel> = Key::new(
|
||
"notify.event_reminders",
|
||
NOTIFY_LEVELS,
|
||
"Reminders of scheduled events: off, app (the bell) or desktop.",
|
||
NotifyLevel::default,
|
||
);
|
||
/// Friends coming online: off, app (the bell) or desktop.
|
||
pub static NOTIFY_FRIENDS: Key<NotifyLevel> = Key::new(
|
||
"notify.friends_online",
|
||
NOTIFY_LEVELS,
|
||
"Friends coming online: off, app (the bell) or desktop.",
|
||
|| NotifyLevel::App,
|
||
);
|
||
|
||
/// `video.camera`: the camera of the preview and the default of camera
|
||
/// sources (a device id; empty: the first camera).
|
||
pub static VIDEO_CAMERA: Key<String> = Key::new(
|
||
"video.camera",
|
||
Kind::Text { suggestions: &[] },
|
||
"Camera device (empty: the first one).",
|
||
String::new,
|
||
);
|
||
|
||
/// `video.background`: the camera's background effect.
|
||
pub static VIDEO_BACKGROUND: Key<String> = Key::new(
|
||
"video.background",
|
||
Kind::Choice(&["none", "blur"]),
|
||
"Background effect of the camera: none or blur.",
|
||
|| "none".into(),
|
||
);
|
||
|
||
/// `video.resolution`: the camera's capture size, `auto` or `WIDTHxHEIGHT`.
|
||
pub static VIDEO_RESOLUTION: Key<String> = Key::new(
|
||
"video.resolution",
|
||
Kind::Text { suggestions: &["auto", "1280x720", "1920x1080", "2560x1440"] },
|
||
"Camera resolution: auto or WIDTHxHEIGHT.",
|
||
|| "auto".into(),
|
||
)
|
||
.validated(valid_resolution);
|
||
|
||
/// `video.mirror`: show our camera mirrored, as in a mirror.
|
||
pub static VIDEO_MIRROR: Key<bool> =
|
||
Key::new("video.mirror", Kind::Bool, "Show our camera mirrored.", || true);
|
||
|
||
/// `ui.image_preview_kb`: linked pictures up to this size are downloaded
|
||
/// and shown in the chat (0: never). No maximum.
|
||
pub static UI_IMAGE_PREVIEW_KB: Key<u32> = Key::new(
|
||
"ui.image_preview_kb",
|
||
Kind::UInt { min: 0 },
|
||
"Show pictures linked in chat up to this size in KB (0: never).",
|
||
|| 8192,
|
||
);
|
||
|
||
#[allow(clippy::ptr_arg)] // a validation of `Key<String>` is `fn(&String)`
|
||
fn valid_resolution(text: &String) -> Result<(), String> {
|
||
if text == "auto" || parse_size(text).is_some() {
|
||
Ok(())
|
||
} else {
|
||
Err("auto or WIDTHxHEIGHT, e.g. 1280x720".into())
|
||
}
|
||
}
|
||
|
||
/// `1280x720` → (1280, 720), both above 0.
|
||
pub fn parse_size(text: &str) -> Option<(u32, u32)> {
|
||
let (w, h) = text.trim().split_once(['x', 'X', '×'])?;
|
||
let (w, h) = (w.trim().parse().ok()?, h.trim().parse().ok()?);
|
||
(w > 0 && h > 0).then_some((w, h))
|
||
}
|
||
|
||
/// The keys of the home, messages and settings pages, for registering.
|
||
pub fn page_keys() -> [&'static dyn voelin_core::settings::Setting; 10] {
|
||
[
|
||
&NOTIFY_MENTIONS,
|
||
&NOTIFY_MESSAGES,
|
||
&NOTIFY_POKES,
|
||
&NOTIFY_EVENTS,
|
||
&NOTIFY_FRIENDS,
|
||
&VIDEO_CAMERA,
|
||
&VIDEO_BACKGROUND,
|
||
&VIDEO_RESOLUTION,
|
||
&VIDEO_MIRROR,
|
||
&UI_IMAGE_PREVIEW_KB,
|
||
]
|
||
}
|
||
|
||
/// The settings service on the client database in `dir` (`client.db`), the way
|
||
/// both front ends open it: the keys they read registered ([`UI`],
|
||
/// [`CLIENT_PLAYBACK`], [`appearance_keys`] and [`page_keys`]; `register` adds
|
||
/// the keys of one front end only, before anything is read), then a config file
|
||
/// (`VOELIN_CONFIG`, else `<dir>/config.toml` if it exists), the
|
||
/// `VOELIN_SETTING_*` variables and `overrides` (`--set key=value`) below the
|
||
/// stored values. Without the database the settings live in memory. What
|
||
/// cannot be applied is logged, not fatal.
|
||
pub fn open_settings(
|
||
dir: &Path,
|
||
overrides: &[String],
|
||
register: impl FnOnce(&Settings),
|
||
) -> Settings {
|
||
let prefs = Settings::open(&dir.join("client.db")).unwrap_or_else(|e| {
|
||
warn!(%e, "settings are not stored this time");
|
||
Settings::in_memory()
|
||
});
|
||
register_keys(&prefs);
|
||
register(&prefs);
|
||
let config = std::env::var_os("VOELIN_CONFIG")
|
||
.map(PathBuf::from)
|
||
.or_else(|| Some(dir.join("config.toml")).filter(|p| p.exists()));
|
||
let mut errors = match config.map(|path| prefs.load_config_file(&path)) {
|
||
Some(Ok(errors)) => errors,
|
||
Some(Err(e)) => vec![e],
|
||
None => Vec::new(),
|
||
};
|
||
errors.extend(prefs.apply_env(std::env::vars()));
|
||
errors.extend(prefs.apply_overrides(overrides.iter().map(String::as_str)));
|
||
for e in errors {
|
||
warn!(%e, "setting ignored");
|
||
}
|
||
prefs
|
||
}
|
||
|
||
/// Make the keys both front ends read known to `prefs`.
|
||
pub fn register_keys(prefs: &Settings) {
|
||
prefs.register(&UI);
|
||
prefs.register(&CLIENT_PLAYBACK);
|
||
for key in appearance_keys() {
|
||
prefs.register(key);
|
||
}
|
||
for key in page_keys() {
|
||
prefs.register(key);
|
||
}
|
||
}
|
||
|
||
/// Frame rates and bitrates (kbit/s; 0: automatic) the share dialog
|
||
/// offers, in the order of its lists (`dialogs.slint`); any other value can
|
||
/// be typed in.
|
||
pub const FPS_CHOICES: [u32; 7] = [15, 30, 60, 120, 144, 240, 320];
|
||
/// The bitrates (kbit/s, 0 for automatic) the share dialog offers, in the
|
||
/// order of its list.
|
||
pub const BITRATE_CHOICES: [u32; 8] = [0, 2500, 4608, 8000, 10_000, 20_000, 40_000, 60_000];
|
||
|
||
/// The share dialog's lists before they grew, which [`ShareDefaults`]
|
||
/// indexes for older versions.
|
||
pub const LEGACY_FPS_CHOICES: [u32; 3] = [15, 30, 60];
|
||
/// The bitrates of the share dialog's list before it grew.
|
||
pub const LEGACY_BITRATE_CHOICES: [u32; 4] = [2500, 4608, 8000, 10_000];
|
||
|
||
/// The index of the choice closest to `value`.
|
||
pub fn nearest_choice(choices: &[u32], value: u32) -> usize {
|
||
(0..choices.len()).min_by_key(|&i| choices[i].abs_diff(value)).unwrap_or(0)
|
||
}
|
||
|
||
/// A whole number above 0 typed into a form field.
|
||
pub fn parse_positive(text: &str) -> Option<u32> {
|
||
text.trim().parse().ok().filter(|v| *v > 0)
|
||
}
|
||
|
||
/// The `ui` settings: what the front end keeps about itself (the push-to-talk
|
||
/// hotkey, codecs, the share dialog's defaults, the last voice channel).
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||
#[serde(default)]
|
||
pub struct UiSettings {
|
||
/// Global push-to-talk hotkey (besides the button in the window).
|
||
pub global_ptt: bool,
|
||
/// E.g. `Ctrl+Shift+T` (`voelin_platform::Hotkey` syntax).
|
||
pub ptt_key: String,
|
||
/// H.264 through Cisco's OpenH264, downloaded on request.
|
||
pub openh264: bool,
|
||
/// The ScreenCast portal's token for the last shared screen.
|
||
pub portal_restore_token: Option<String>,
|
||
/// The last choices in the share dialog.
|
||
pub share: ShareDefaults,
|
||
/// Write crash reports to disk (opt-in; never uploaded).
|
||
pub crash_reports: bool,
|
||
/// The voice channel we were in last (Home's "Continue where you left
|
||
/// off").
|
||
pub last_voice: Option<LastVoice>,
|
||
}
|
||
|
||
impl Default for UiSettings {
|
||
fn default() -> Self {
|
||
Self {
|
||
global_ptt: true,
|
||
ptt_key: "Ctrl+Shift+T".into(),
|
||
openh264: false,
|
||
portal_restore_token: None,
|
||
share: ShareDefaults::default(),
|
||
crash_reports: false,
|
||
last_voice: None,
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Where we were with voice last: the bookmark with the address it had
|
||
/// then (one that now points elsewhere does not resume), and the channel's
|
||
/// path, its names from the top as the server has them.
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||
pub struct LastVoice {
|
||
/// The id of the server's bookmark.
|
||
pub bookmark: i64,
|
||
/// The address the bookmark had then.
|
||
pub address: String,
|
||
/// The channel's names from the top.
|
||
pub channel: Vec<String>,
|
||
}
|
||
|
||
/// The last choices in the share dialog. Frame rate and bitrate are the
|
||
/// settings `stream.fps` and `stream.bitrate_kbps`; the indices of the
|
||
/// closest choices of the old lists ([`LEGACY_FPS_CHOICES`],
|
||
/// [`LEGACY_BITRATE_CHOICES`]) are kept for older versions.
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||
#[serde(default)]
|
||
pub struct ShareDefaults {
|
||
/// The frame rate's position in the old list ([`LEGACY_FPS_CHOICES`]).
|
||
pub fps_index: usize,
|
||
/// The bitrate's position in the old list ([`LEGACY_BITRATE_CHOICES`]).
|
||
pub bitrate_index: usize,
|
||
/// Share the sound too.
|
||
pub audio: bool,
|
||
/// Accept the viewers that ask to watch without asking us.
|
||
pub auto_accept: bool,
|
||
}
|
||
|
||
impl Default for ShareDefaults {
|
||
fn default() -> Self {
|
||
Self { fps_index: 1, bitrate_index: 1, audio: true, auto_accept: false }
|
||
}
|
||
}
|
||
|
||
/// Local playback of one client, kept by its unique id.
|
||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||
pub struct ClientPlayback {
|
||
/// Linear, 1 = unchanged.
|
||
pub volume: f32,
|
||
/// Not played at all.
|
||
pub muted: bool,
|
||
}
|
||
|
||
impl Default for ClientPlayback {
|
||
fn default() -> Self {
|
||
Self { volume: 1.0, muted: false }
|
||
}
|
||
}
|
||
|
||
impl ClientPlayback {
|
||
/// Nothing differs from the defaults, so there is nothing to store.
|
||
pub fn is_default(&self) -> bool {
|
||
*self == Self::default()
|
||
}
|
||
}
|
||
|
||
/// Each client's playback settings, by the client's unique id.
|
||
pub type ClientPlaybackMap = BTreeMap<String, ClientPlayback>;
|
||
|
||
/// Audio devices as listed in the settings: the system default first.
|
||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||
pub struct DeviceChoices {
|
||
/// What the list shows, by index.
|
||
pub names: Vec<String>,
|
||
/// Device ids by index; `None` for the system default.
|
||
pub ids: Vec<Option<String>>,
|
||
}
|
||
|
||
impl DeviceChoices {
|
||
/// `devices` as `(id, name, is_default)`. A selected device that is not
|
||
/// connected stays in the list, so the choice survives.
|
||
pub fn new(devices: &[(String, String, bool)], selected: Option<&str>) -> Self {
|
||
let default_name = devices.iter().find(|d| d.2).map(|d| d.1.as_str());
|
||
let mut choices = Self {
|
||
names: vec![match default_name {
|
||
Some(name) => format!("System default ({name})"),
|
||
None => "System default".into(),
|
||
}],
|
||
ids: vec![None],
|
||
};
|
||
for (id, name, _) in devices {
|
||
choices.names.push(name.clone());
|
||
choices.ids.push(Some(id.clone()));
|
||
}
|
||
if let Some(id) = selected
|
||
&& !choices.ids.iter().any(|i| i.as_deref() == Some(id))
|
||
{
|
||
choices.names.push(format!("{id} (not connected)"));
|
||
choices.ids.push(Some(id.to_owned()));
|
||
}
|
||
choices
|
||
}
|
||
|
||
/// The index of the choice for device `id` (the system default for `None` or
|
||
/// an unknown id).
|
||
pub fn index_of(&self, id: Option<&str>) -> usize {
|
||
self.ids.iter().position(|i| i.as_deref() == id).unwrap_or(0)
|
||
}
|
||
|
||
/// The device id of choice `index`; `None` for the system default or an index
|
||
/// outside the list.
|
||
pub fn id_at(&self, index: i32) -> Option<String> {
|
||
usize::try_from(index).ok().and_then(|i| self.ids.get(i).cloned()).flatten()
|
||
}
|
||
}
|
||
|
||
fn noise_index(level: NoiseLevel) -> i32 {
|
||
match level {
|
||
NoiseLevel::Low => 0,
|
||
NoiseLevel::Moderate => 1,
|
||
NoiseLevel::High => 2,
|
||
NoiseLevel::VeryHigh => 3,
|
||
}
|
||
}
|
||
|
||
fn noise_level(index: i32) -> NoiseLevel {
|
||
match index {
|
||
0 => NoiseLevel::Low,
|
||
2 => NoiseLevel::High,
|
||
3 => NoiseLevel::VeryHigh,
|
||
_ => NoiseLevel::Moderate,
|
||
}
|
||
}
|
||
|
||
/// The form's number for a transmit mode (`AudioForm.transmit`).
|
||
pub fn transmit_index(mode: TransmitMode) -> i32 {
|
||
match mode {
|
||
TransmitMode::PushToTalk => 0,
|
||
TransmitMode::VoiceActivation => 1,
|
||
TransmitMode::Continuous => 2,
|
||
}
|
||
}
|
||
|
||
fn transmit_mode(index: i32) -> TransmitMode {
|
||
match index {
|
||
1 => TransmitMode::VoiceActivation,
|
||
2 => TransmitMode::Continuous,
|
||
_ => TransmitMode::PushToTalk,
|
||
}
|
||
}
|
||
|
||
/// The settings page's form for `settings`.
|
||
pub fn audio_form(
|
||
settings: &AudioSettings,
|
||
inputs: &DeviceChoices,
|
||
outputs: &DeviceChoices,
|
||
) -> AudioForm {
|
||
let p = &settings.processing;
|
||
AudioForm {
|
||
input_index: inputs.index_of(settings.input_device.as_deref()) as i32,
|
||
output_index: outputs.index_of(settings.output_device.as_deref()) as i32,
|
||
transmit: transmit_index(settings.transmit),
|
||
vad_threshold: settings.vad.threshold_db,
|
||
echo_cancellation: p.echo_cancellation,
|
||
noise_suppression: p.noise_suppression,
|
||
noise_level: noise_index(p.noise_level),
|
||
auto_gain: p.auto_gain,
|
||
input_gain: p.input_gain_db,
|
||
output_volume: settings.output_volume * 100.0,
|
||
playback_buffer: settings.playback_buffer_ms as f32,
|
||
}
|
||
}
|
||
|
||
/// `settings` changed as the form says; what the form does not show stays.
|
||
pub fn apply_audio_form(
|
||
form: &AudioForm,
|
||
settings: &AudioSettings,
|
||
inputs: &DeviceChoices,
|
||
outputs: &DeviceChoices,
|
||
) -> AudioSettings {
|
||
let mut s = settings.clone();
|
||
s.input_device = inputs.id_at(form.input_index);
|
||
s.output_device = outputs.id_at(form.output_index);
|
||
s.transmit = transmit_mode(form.transmit);
|
||
s.vad.threshold_db = form.vad_threshold;
|
||
s.processing.echo_cancellation = form.echo_cancellation;
|
||
s.processing.noise_suppression = form.noise_suppression;
|
||
s.processing.noise_level = noise_level(form.noise_level);
|
||
s.processing.auto_gain = form.auto_gain;
|
||
s.processing.input_gain_db = form.input_gain;
|
||
s.output_volume = (form.output_volume / 100.0).clamp(0.0, 4.0);
|
||
s.playback_buffer_ms = form.playback_buffer.round().clamp(20.0, 180.0) as u32;
|
||
s
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
fn devices() -> Vec<(String, String, bool)> {
|
||
vec![
|
||
("alsa:hw:0,0".into(), "Built-in".into(), true),
|
||
("alsa:hw:1,0".into(), "Headset".into(), false),
|
||
]
|
||
}
|
||
|
||
#[test]
|
||
fn device_choices() {
|
||
let choices = DeviceChoices::new(&devices(), None);
|
||
assert_eq!(choices.names, ["System default (Built-in)", "Built-in", "Headset"]);
|
||
assert_eq!(choices.index_of(Some("alsa:hw:1,0")), 2);
|
||
assert_eq!(choices.id_at(0), None);
|
||
assert_eq!(choices.id_at(2).as_deref(), Some("alsa:hw:1,0"));
|
||
assert_eq!(choices.id_at(9), None);
|
||
// An unplugged device stays selectable.
|
||
let choices = DeviceChoices::new(&devices(), Some("usb:mic"));
|
||
assert_eq!(choices.names.last().unwrap(), "usb:mic (not connected)");
|
||
assert_eq!(choices.index_of(Some("usb:mic")), 3);
|
||
}
|
||
|
||
#[test]
|
||
fn audio_form_roundtrip() {
|
||
let inputs = DeviceChoices::new(&devices(), None);
|
||
let outputs = DeviceChoices::new(&devices(), None);
|
||
let mut settings = AudioSettings {
|
||
transmit: TransmitMode::VoiceActivation,
|
||
input_device: Some("alsa:hw:1,0".into()),
|
||
output_volume: 0.5,
|
||
playback_buffer_ms: 80,
|
||
..Default::default()
|
||
};
|
||
settings.vad.threshold_db = -35.0;
|
||
settings.processing.noise_level = NoiseLevel::High;
|
||
settings.processing.input_gain_db = 6.0;
|
||
let form = audio_form(&settings, &inputs, &outputs);
|
||
assert_eq!((form.input_index, form.output_index, form.transmit), (2, 0, 1));
|
||
assert_eq!(form.output_volume, 50.0);
|
||
assert_eq!(apply_audio_form(&form, &settings, &inputs, &outputs), settings);
|
||
|
||
let form =
|
||
AudioForm { transmit: 2, echo_cancellation: false, playback_buffer: 500.0, ..form };
|
||
let changed = apply_audio_form(&form, &settings, &inputs, &outputs);
|
||
assert_eq!(changed.transmit, TransmitMode::Continuous);
|
||
assert!(!changed.processing.echo_cancellation);
|
||
assert_eq!(changed.playback_buffer_ms, 180);
|
||
}
|
||
|
||
#[test]
|
||
fn transmit_modes() {
|
||
for (mode, index) in [
|
||
(TransmitMode::PushToTalk, 0),
|
||
(TransmitMode::VoiceActivation, 1),
|
||
(TransmitMode::Continuous, 2),
|
||
] {
|
||
assert_eq!(transmit_index(mode), index);
|
||
assert_eq!(transmit_mode(index), mode);
|
||
}
|
||
// Any other number is the default, push-to-talk (Hold to talk shows).
|
||
assert_eq!(transmit_mode(7), TransmitMode::PushToTalk);
|
||
assert_eq!(transmit_mode(-1), TransmitMode::PushToTalk);
|
||
assert_eq!(transmit_index(AudioSettings::default().transmit), 0);
|
||
}
|
||
|
||
#[test]
|
||
fn free_values() {
|
||
assert_eq!(nearest_choice(&FPS_CHOICES, 60), 2);
|
||
assert_eq!(nearest_choice(&FPS_CHOICES, 144), 4);
|
||
assert_eq!(nearest_choice(&FPS_CHOICES, 1000), 6);
|
||
assert_eq!(nearest_choice(&BITRATE_CHOICES, 0), 0, "automatic");
|
||
assert_eq!(nearest_choice(&BITRATE_CHOICES, 3000), 1);
|
||
assert_eq!(nearest_choice(&BITRATE_CHOICES, 55_000), 7);
|
||
assert_eq!(nearest_choice(&BITRATE_CHOICES, 120_000), 7);
|
||
assert_eq!(parse_positive(" 144 "), Some(144));
|
||
assert_eq!(parse_positive("0"), None);
|
||
assert_eq!(parse_positive("fast"), None);
|
||
}
|
||
|
||
#[test]
|
||
fn page_keys() {
|
||
assert_eq!(parse_size("1280x720"), Some((1280, 720)));
|
||
assert_eq!(parse_size(" 1920 × 1080 "), Some((1920, 1080)));
|
||
assert_eq!(parse_size("0x720"), None);
|
||
assert_eq!(parse_size("big"), None);
|
||
assert!(VIDEO_RESOLUTION.validate(&"auto".into()).is_ok());
|
||
assert!(VIDEO_RESOLUTION.validate(&"wide".into()).is_err());
|
||
assert_eq!(serde_json::to_value(NotifyLevel::App).unwrap(), "app");
|
||
assert_eq!(NotifyLevel::from_index(NotifyLevel::Off.index()), NotifyLevel::Off);
|
||
assert_eq!(NotifyLevel::from_index(7), NotifyLevel::Desktop);
|
||
}
|
||
|
||
/// The settings both front ends open: the shared keys, the config file's
|
||
/// values, then `--set` over it, a front end's own key only when it
|
||
/// registers it, and a database in the directory given.
|
||
#[test]
|
||
fn the_settings_open_with_the_shared_keys_a_config_file_and_overrides() {
|
||
let dir =
|
||
std::env::temp_dir().join(format!("voelin-ui-vm-settings-{}", std::process::id()));
|
||
let _ = std::fs::remove_dir_all(&dir);
|
||
std::fs::create_dir_all(&dir).unwrap();
|
||
std::fs::write(
|
||
dir.join("config.toml"),
|
||
"\"ui.theme\" = \"light\"\n\"ui.members_width\" = 300\n",
|
||
)
|
||
.unwrap();
|
||
let overrides = ["ui.font_scale=1.5".to_owned(), "ui.members_width=320".to_owned()];
|
||
let prefs = open_settings(&dir, &overrides, |prefs| prefs.register(&UI_WINDOW));
|
||
assert_eq!(prefs.get(&UI_THEME), ThemeChoice::Light, "from the config file");
|
||
assert_eq!(prefs.get(&UI_FONT_SCALE), 1.5, "from --set");
|
||
assert_eq!(prefs.get(&UI_MEMBERS_WIDTH), 320, "--set over the file");
|
||
assert_eq!(prefs.get(&UI_WINDOW), None);
|
||
let names = |prefs: &Settings| prefs.keys().iter().map(|k| k.name()).collect::<Vec<_>>();
|
||
for shared in
|
||
["ui", "client_playback", "ui.theme", "notify.mentions", "ui.image_preview_kb"]
|
||
{
|
||
assert!(names(&prefs).contains(&shared), "{shared}");
|
||
}
|
||
assert!(names(&prefs).contains(&"ui.window"), "the front end's own key");
|
||
prefs.set(&UI_FONT_SCALE, 1.25).unwrap();
|
||
prefs.flush().unwrap();
|
||
drop(prefs);
|
||
assert!(dir.join("client.db").is_file(), "the database is in the directory given");
|
||
|
||
let plain = open_settings(&dir, &[], |_| {});
|
||
assert!(!names(&plain).contains(&"ui.window"), "not registered by this front end");
|
||
assert_eq!(plain.get(&UI_FONT_SCALE), 1.25, "what was stored wins over nothing");
|
||
drop(plain);
|
||
std::fs::remove_dir_all(&dir).unwrap();
|
||
}
|
||
|
||
#[test]
|
||
fn voice_compact_key() {
|
||
let settings = voelin_core::settings::Settings::in_memory();
|
||
for key in appearance_keys() {
|
||
settings.register(key);
|
||
}
|
||
assert_eq!(settings.get_json("ui.voice_compact"), Some(false.into()), "the full stage");
|
||
assert!(settings.apply_overrides(["ui.voice_compact=true"]).is_empty());
|
||
assert!(settings.get(&UI_VOICE_COMPACT));
|
||
assert!(settings.set_json("ui.voice_compact", "small".into()).is_err());
|
||
}
|
||
|
||
/// The window's state is stored as the setting `ui.window`, comes back as it
|
||
/// went, and a damaged value is refused in words.
|
||
#[test]
|
||
fn window_state_roundtrip() {
|
||
let state =
|
||
WindowState { width: 1440, height: 960, x: Some(-20), y: Some(35), maximized: true };
|
||
let settings = voelin_core::settings::Settings::in_memory();
|
||
assert_eq!(settings.get(&UI_WINDOW), None, "none until a run left one");
|
||
settings.set(&UI_WINDOW, Some(state)).unwrap();
|
||
assert_eq!(settings.get(&UI_WINDOW), Some(state));
|
||
assert_eq!(
|
||
settings.get_json("ui.window"),
|
||
Some(
|
||
serde_json::json!({"width": 1440, "height": 960, "x": -20, "y": 35, "maximized": true})
|
||
)
|
||
);
|
||
// A platform that does not tell the position leaves it out.
|
||
let no_position = WindowState { x: None, y: None, maximized: false, ..state };
|
||
let json = serde_json::to_value(no_position).unwrap();
|
||
assert_eq!(json, serde_json::json!({"width": 1440, "height": 960, "maximized": false}));
|
||
assert_eq!(serde_json::from_value::<WindowState>(json).unwrap(), no_position);
|
||
let older: WindowState = serde_json::from_str(r#"{"width":800,"height":600}"#).unwrap();
|
||
assert_eq!(
|
||
older,
|
||
WindowState { width: 800, height: 600, x: None, y: None, maximized: false }
|
||
);
|
||
for damaged in [
|
||
WindowState { width: 0, ..state },
|
||
WindowState { height: 100_000, ..state },
|
||
WindowState { x: Some(1_000_000), ..state },
|
||
WindowState { y: None, ..state },
|
||
] {
|
||
assert!(!damaged.is_sane(), "{damaged:?}");
|
||
assert!(settings.set(&UI_WINDOW, Some(damaged)).is_err(), "{damaged:?}");
|
||
}
|
||
assert!(settings.set_json("ui.window", "big".into()).is_err());
|
||
assert_eq!(settings.get(&UI_WINDOW), Some(state), "a refused value changes nothing");
|
||
settings.set(&UI_WINDOW, None).unwrap();
|
||
assert_eq!(settings.get(&UI_WINDOW), None);
|
||
}
|
||
|
||
#[test]
|
||
fn ui_settings_defaults() {
|
||
let parsed: UiSettings = serde_json::from_str(r#"{"openh264":true}"#).unwrap();
|
||
assert!(parsed.openh264);
|
||
assert!(!parsed.crash_reports, "crash reports are opt-in");
|
||
assert_eq!(parsed.ptt_key, "Ctrl+Shift+T");
|
||
assert_eq!(parsed.share, ShareDefaults::default());
|
||
assert_eq!(parsed.last_voice, None, "older settings have no last channel");
|
||
let last = LastVoice {
|
||
bookmark: 3,
|
||
address: "ts.example".into(),
|
||
channel: vec!["Games".into(), "Chess".into()],
|
||
};
|
||
let settings = UiSettings { last_voice: Some(last.clone()), ..UiSettings::default() };
|
||
let json = serde_json::to_string(&settings).unwrap();
|
||
assert_eq!(serde_json::from_str::<UiSettings>(&json).unwrap().last_voice, Some(last));
|
||
assert_eq!(serde_json::to_value(ThemeChoice::System).unwrap(), "system");
|
||
assert_eq!(ThemeChoice::parse("light"), ThemeChoice::Light);
|
||
assert_eq!(ThemeChoice::parse("??"), ThemeChoice::Dark);
|
||
assert!(UI_FONT_SCALE.validate(&0.0).is_err());
|
||
assert!(UI_FONT_SCALE.validate(&3.5).is_ok());
|
||
let map: ClientPlaybackMap =
|
||
serde_json::from_str(r#"{"uid=":{"volume":0.5,"muted":false}}"#).unwrap();
|
||
assert_eq!(map["uid="].volume, 0.5);
|
||
}
|
||
}
|