Move language config field into general config

This commit is contained in:
Avery
2024-01-13 23:57:02 -05:00
parent a4c9e7d439
commit 1055ca2363
6 changed files with 13 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
use relm4::adw; use relm4::adw;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use unic_langid::LanguageIdentifier;
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq)] #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq)]
pub enum Theme { pub enum Theme {
@@ -21,6 +22,7 @@ impl From<Theme> for adw::ColorScheme {
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct GeneralConfig { pub struct GeneralConfig {
pub language: Option<LanguageIdentifier>,
#[serde(default)] #[serde(default)]
pub theme: Theme, pub theme: Theme,
} }

View File

@@ -113,6 +113,7 @@ italic = false
let config: Config = config_version.into(); let config: Config = config_version.into();
let expected = Config { let expected = Config {
version: 2,
window: Window { window: Window {
width: 967, width: 967,
height: 670, height: 670,
@@ -142,7 +143,10 @@ italic = false
}], }],
}, },
], ],
general: GeneralConfig { theme: Theme::Dark }, general: GeneralConfig {
language: None,
theme: Theme::Dark,
},
video_player: VideoPlayerConfig { video_player: VideoPlayerConfig {
volume: 0.0, volume: 0.0,
muted: true, muted: true,
@@ -150,7 +154,6 @@ italic = false
on_left_click: VideoPlayerOnLeftClick::ToggleControls, on_left_click: VideoPlayerOnLeftClick::ToggleControls,
..Default::default() ..Default::default()
}, },
..Default::default()
}; };
assert_eq!(config, expected); assert_eq!(config, expected);

View File

@@ -7,7 +7,6 @@ use std::{fs, path::PathBuf};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use unic_langid::LanguageIdentifier;
use uuid::Uuid; use uuid::Uuid;
use self::{ use self::{
@@ -17,29 +16,23 @@ use self::{
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] #[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct Config { pub struct Config {
version: usize, version: usize,
pub language: Option<LanguageIdentifier>,
#[serde(default)] #[serde(default)]
pub window: Window, pub window: Window,
pub servers: Vec<Server>,
#[serde(default)] #[serde(default)]
pub general: GeneralConfig, pub general: GeneralConfig,
#[serde(default)] #[serde(default)]
pub video_player: VideoPlayerConfig, pub video_player: VideoPlayerConfig,
pub servers: Vec<Server>,
} }
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
version: 2, version: 2,
language: None,
window: Window::default(), window: Window::default(),
servers: Vec::default(),
general: GeneralConfig::default(), general: GeneralConfig::default(),
video_player: VideoPlayerConfig::default(), video_player: VideoPlayerConfig::default(),
servers: Vec::default(),
} }
} }
} }

View File

@@ -182,7 +182,6 @@ impl Migrate<Config> for ConfigV1 {
Config { Config {
version: 2, version: 2,
language: self.language,
window: Window { window: Window {
width: self.window.width, width: self.window.width,
height: self.window.height, height: self.window.height,
@@ -190,6 +189,7 @@ impl Migrate<Config> for ConfigV1 {
}, },
servers, servers,
general: GeneralConfig { general: GeneralConfig {
language: self.language,
theme: match self.general.theme { theme: match self.general.theme {
Theme::Light => config::general::Theme::Light, Theme::Light => config::general::Theme::Light,
Theme::Dark => config::general::Theme::Dark, Theme::Dark => config::general::Theme::Dark,

View File

@@ -25,6 +25,7 @@ lazy_static! {
pub fn current_language() -> LanguageIdentifier { pub fn current_language() -> LanguageIdentifier {
CONFIG CONFIG
.read() .read()
.general
.language .language
.clone() .clone()
.unwrap_or(DEFAULT_LANGUAGE.clone()) .unwrap_or(DEFAULT_LANGUAGE.clone())

View File

@@ -103,7 +103,7 @@ impl SimpleComponent for GeneralPreferences {
match message { match message {
GeneralPreferencesInput::Language(language) => { GeneralPreferencesInput::Language(language) => {
config.language = language config.general.language = language
.map(|l| LanguageIdentifier::from_str(&l).expect("Error setting language")); .map(|l| LanguageIdentifier::from_str(&l).expect("Error setting language"));
} }
GeneralPreferencesInput::ThemeChanged(theme) => { GeneralPreferencesInput::ThemeChanged(theme) => {
@@ -116,7 +116,7 @@ impl SimpleComponent for GeneralPreferences {
} }
fn get_selected_language(language: &adw::ComboRow, config: Config) -> u32 { fn get_selected_language(language: &adw::ComboRow, config: Config) -> u32 {
match config.language { match config.general.language {
// First item is system default // First item is system default
None => 0, None => 0,
Some(selected_language) => { Some(selected_language) => {