Skip to content

Commit

Permalink
Cleanup user prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Dec 14, 2023
1 parent 493fcb4 commit 7f4af6e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ macro_rules! decl_newtype_prefs {

$(
impl Default for $name {
#[inline(always)]
fn default() -> Self {
$name($default.into())
}
Expand All @@ -43,11 +44,19 @@ macro_rules! decl_newtype_prefs {
impl core::ops::Deref for $name {
type Target = $ty;

#[inline(always)]
fn deref(&self) -> &$ty {
&self.0
}
}

impl core::ops::DerefMut for $name {
#[inline(always)]
fn deref_mut(&mut self) -> &mut $ty {
&mut self.0
}
}

common::impl_rkyv_for_pod!($name);
)*
};
Expand Down
39 changes: 35 additions & 4 deletions src/models/user/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ impl Default for UserPrefsFlags {

pub mod preferences {
decl_newtype_prefs! {
Temperature: f32 = 7500.0,
TabSize: u8 = 4,
Padding: u8 = 16,
FontSize: f32 = 16.0,
Temperature: u16 = 7500u16,
FontSize: f32 = 16.0f32,
TabSize: u8 = 4u8,
Padding: u8 = 16u8,
}
}

Expand All @@ -150,4 +150,35 @@ pub struct UserPreferences {
pub ufs: preferences::FontSize,
#[serde(default, skip_serializing_if = "is_default", alias = "padding")]
pub pad: preferences::Padding,
#[serde(default, skip_serializing_if = "is_default", alias = "tab_size")]
pub tab: preferences::TabSize,
}

impl UserPreferences {
pub fn clean(&mut self) {
use std::ops::Range;

#[inline]
fn clamp_range<T: PartialOrd>(value: &mut T, range: Range<T>) {
if *value < range.start {
*value = range.start;
} else if *value > range.end {
*value = range.end;
}
}

#[inline]
fn round_2(value: &mut f32) {
*value = (*value * 100.0).round() / 100.0;
}

round_2(&mut self.cfs);
round_2(&mut self.ufs);

clamp_range::<u16>(&mut self.temp, 965..12000);
clamp_range::<f32>(&mut self.cfs, 8.0..32.0);
clamp_range::<f32>(&mut self.ufs, 8.0..32.0);
clamp_range::<u8>(&mut self.pad, 0..32);
clamp_range::<u8>(&mut self.tab, 1..64);
}
}

0 comments on commit 7f4af6e

Please sign in to comment.