refactor: use more reasonable numeric field types for configs (#368)

This commit is contained in:
三咲雅 · Misaki Masa 2023-11-15 08:51:11 +08:00 committed by GitHub
parent ff30b244b1
commit f038391ef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 20 deletions

View file

@ -48,10 +48,10 @@ impl Ueberzug {
let scale = PREVIEW.ueberzug_scale;
let (x, y, w, h) = PREVIEW.ueberzug_offset;
rect.x = 0f64.max(rect.x as f64 * scale + x) as u16;
rect.y = 0f64.max(rect.y as f64 * scale + y) as u16;
rect.width = 0f64.max(rect.width as f64 * scale + w) as u16;
rect.height = 0f64.max(rect.height as f64 * scale + h) as u16;
rect.x = 0f32.max(rect.x as f32 * scale + x) as u16;
rect.y = 0f32.max(rect.y as f32 * scale + y) as u16;
rect.width = 0f32.max(rect.width as f32 * scale + w) as u16;
rect.height = 0f32.max(rect.height as f32 * scale + h) as u16;
rect
}

View file

@ -7,18 +7,18 @@ use yazi_shared::Term;
use crate::THEME;
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(try_from = "Vec<u32>")]
#[serde(try_from = "Vec<u16>")]
pub struct ManagerLayout {
pub parent: u32,
pub current: u32,
pub preview: u32,
pub all: u32,
pub parent: u16,
pub current: u16,
pub preview: u16,
pub all: u16,
}
impl TryFrom<Vec<u32>> for ManagerLayout {
impl TryFrom<Vec<u16>> for ManagerLayout {
type Error = anyhow::Error;
fn try_from(ratio: Vec<u32>) -> Result<Self, Self::Error> {
fn try_from(ratio: Vec<u16>) -> Result<Self, Self::Error> {
if ratio.len() != 3 {
bail!("invalid layout ratio: {:?}", ratio);
}
@ -39,7 +39,7 @@ impl ManagerLayout {
pub fn preview_rect(&self) -> Rect {
let WindowSize { columns, rows, .. } = Term::size();
let width = (columns as u32 * self.preview) as f64 / self.all as f64;
let width = (columns * self.preview) as f64 / self.all as f64;
let width = if width.fract() > 0.5 { width.ceil() as u16 } else { width.floor() as u16 };
let offset = THEME.manager.preview_offset;
@ -59,9 +59,9 @@ impl ManagerLayout {
let offset = THEME.manager.folder_offset;
Block::default().padding(Padding::new(offset.3, offset.1, offset.0, offset.2)).inner(Rect {
x: (columns as u32 * self.parent / self.all) as u16,
x: columns * self.parent / self.all,
y: 0,
width: (columns as u32 * self.current / self.all) as u16,
width: columns * self.current / self.all,
height: rows,
})
}

View file

@ -8,14 +8,14 @@ use crate::{xdg::Xdg, MERGED_YAZI};
#[derive(Debug)]
pub struct Preview {
pub tab_size: u32,
pub tab_size: u8,
pub max_width: u32,
pub max_height: u32,
pub cache_dir: PathBuf,
pub ueberzug_scale: f64,
pub ueberzug_offset: (f64, f64, f64, f64),
pub ueberzug_scale: f32,
pub ueberzug_offset: (f32, f32, f32, f32),
}
impl Default for Preview {
@ -26,14 +26,14 @@ impl Default for Preview {
}
#[derive(Deserialize)]
struct Shadow {
tab_size: u32,
tab_size: u8,
max_width: u32,
max_height: u32,
cache_dir: Option<String>,
ueberzug_scale: f64,
ueberzug_offset: (f64, f64, f64, f64),
ueberzug_scale: f32,
ueberzug_offset: (f32, f32, f32, f32),
}
let preview = toml::from_str::<Outer>(&MERGED_YAZI).unwrap().preview;