Customizable column ratios (#58)

* Customizable column ratios

* Allow ratios sum to be different from 99

* Moved the ratio logic to HBox, column ratios are not based on 0-99 anymore, improved parsing from config file
This commit is contained in:
ath3 2019-07-03 19:35:30 +00:00 committed by rabite0
parent fae8706f7a
commit 6198b4e421
3 changed files with 42 additions and 9 deletions

View File

@ -77,7 +77,8 @@ pub struct Config {
pub icons: bool,
pub media_autoplay: bool,
pub media_mute: bool,
pub media_previewer: String
pub media_previewer: String,
pub ratios: Vec::<usize>
}
@ -97,7 +98,8 @@ impl Config {
icons: false,
media_autoplay: false,
media_mute: false,
media_previewer: "hunter-media".to_string()
media_previewer: "hunter-media".to_string(),
ratios: vec![20,30,49]
}
}
@ -133,6 +135,19 @@ impl Config {
Ok(("media_previewer", cmd)) => {
let cmd = cmd.to_string();
config.select_cmd = cmd;
},
Ok(("ratios", ratios)) => {
let ratios_str = ratios.to_string();
if ratios_str.chars().all(|x| x.is_digit(10) || x.is_whitespace()
|| x == ':' || x == ',' ) {
let ratios: Vec<usize> = ratios_str.split([',', ':'].as_ref())
.map(|r| r.trim().parse::<usize>().unwrap()).collect();
let ratios_sum: usize = ratios.iter().sum();
if ratios.len() == 3 && ratios_sum > 0 && ratios.iter()
.filter(|&r| *r > u16::max_value() as usize).next() == None {
config.ratios = ratios;
}
}
}
_ => { HError::config_error::<Config>(line.to_string()).log(); }
}

View File

@ -234,6 +234,7 @@ impl Tabbable for TabView<FileBrowser> {
}).log();
tab.preview_widget_mut().map(|w| w.config_loaded()).ok();
tab.columns.set_ratios(self.core.config().ratios);
}
Ok(())
}
@ -255,7 +256,7 @@ impl FileBrowser {
let mut core_p = core.clone();
let mut columns = HBox::new(core);
columns.set_ratios(vec![20,30,49]);
columns.set_ratios(core.config().ratios);
let list_coords = columns.calculate_coordinates()?;
core_l.coordinates = list_coords[0].clone();

View File

@ -96,17 +96,34 @@ impl<T> HBox<T> where T: Widget + PartialEq {
let box_ysize = box_coords.ysize();
let box_top = box_coords.top().y();
let ratios = match &self.ratios {
let mut ratios = match &self.ratios {
Some(ratios) => ratios.clone(),
None => self.calculate_equal_ratios()?
};
let coords = ratios.iter().fold(Vec::<Coordinates>::new(), |mut coords, ratio| {
let ratio = *ratio as u16;
let len = coords.len();
let gap = if len == 0 { 0 } else { 1 };
let mut ratios_sum: usize = ratios.iter().sum();
let widget_xsize = box_xsize * ratio / 100;
ratios = ratios.iter().map(|&r|
(r as f64 * box_xsize as f64 / ratios_sum as f64).round() as usize).collect();
for r in &mut ratios {
if *r < 10 { *r = 10 }
}
ratios_sum = ratios.iter().sum();
while ratios_sum + ratios.len() > box_xsize as usize {
let ratios_max = ratios.iter()
.position(|&r| r == *ratios.iter().max().unwrap()).unwrap();
ratios[ratios_max] = ratios[ratios_max] - 1;
ratios_sum -= 1;
}
let coords = ratios.iter().fold(Vec::<Coordinates>::new(), |mut coords, ratio| {
let len = coords.len();
let gap = if len == ratios.len() { 0 } else { 1 };
let widget_xsize = *ratio as u16;
let widget_xpos = if len == 0 {
box_coords.top().x()
} else {