hunter/src/hbox.rs

214 lines
5.8 KiB
Rust
Raw Normal View History

2019-01-21 15:53:16 +01:00
use termion::event::{Event};
2019-03-02 19:39:24 +01:00
use crate::widget::{Widget, WidgetCore};
2019-01-29 13:17:33 +01:00
use crate::coordinates::{Coordinates, Size, Position};
use crate::fail::{HResult, HError, ErrorLog};
2019-05-28 00:50:15 +02:00
#[derive(Debug, PartialEq)]
2019-02-04 18:51:07 +01:00
pub struct HBox<T: Widget> {
2019-03-02 19:39:24 +01:00
pub core: WidgetCore,
2019-02-04 18:51:07 +01:00
pub widgets: Vec<T>,
pub ratios: Option<Vec<usize>>,
2019-03-20 21:56:59 +01:00
pub zoom_active: bool,
2019-02-04 18:51:07 +01:00
pub active: Option<usize>,
}
2019-01-22 15:11:15 +01:00
2019-02-12 22:55:16 +01:00
impl<T> HBox<T> where T: Widget + PartialEq {
2019-03-02 19:39:24 +01:00
pub fn new(core: &WidgetCore) -> HBox<T> {
HBox { core: core.clone(),
2019-02-04 18:51:07 +01:00
widgets: vec![],
ratios: None,
2019-03-20 21:56:59 +01:00
zoom_active: false,
2019-02-04 18:51:07 +01:00
active: None
}
}
2019-01-22 15:11:15 +01:00
pub fn resize_children(&mut self) -> HResult<()> {
let len = self.widgets.len();
if len == 0 { return Ok(()) }
2019-03-20 21:56:59 +01:00
if self.zoom_active {
let coords = self.core.coordinates.clone();
self.active_widget_mut()?.set_coordinates(&coords).log();
return Ok(());
}
let coords: Vec<Coordinates> = self.calculate_coordinates()?;
2019-02-04 18:51:07 +01:00
for (widget, coord) in self.widgets.iter_mut().zip(coords.iter()) {
2019-03-02 19:39:24 +01:00
widget.set_coordinates(coord).log();
2019-01-22 15:11:15 +01:00
}
Ok(())
2019-01-22 15:11:15 +01:00
}
pub fn push_widget(&mut self, widget: T) {
2019-02-04 18:51:07 +01:00
self.widgets.push(widget);
}
2019-01-22 15:11:15 +01:00
2019-02-04 18:51:07 +01:00
pub fn pop_widget(&mut self) -> Option<T> {
let widget = self.widgets.pop();
widget
2019-01-22 15:11:15 +01:00
}
2019-03-31 03:28:07 +02:00
pub fn remove_widget(&mut self, index: usize) -> T {
self.widgets.remove(index)
}
2019-02-04 18:51:07 +01:00
pub fn prepend_widget(&mut self, widget: T) {
self.widgets.insert(0, widget);
}
2019-03-31 03:28:07 +02:00
pub fn insert_widget(&mut self, index: usize, widget: T) {
self.widgets.insert(index, widget);
}
pub fn replace_widget(&mut self, index: usize, mut widget: T) -> T {
std::mem::swap(&mut self.widgets[index], &mut widget);
widget
}
2019-03-20 21:56:59 +01:00
pub fn toggle_zoom(&mut self) -> HResult<()> {
self.core.clear().log();
2019-03-20 21:56:59 +01:00
self.zoom_active = !self.zoom_active;
self.resize_children()
}
pub fn set_ratios(&mut self, ratios: Vec<usize>) {
self.ratios = Some(ratios);
}
2019-01-22 15:11:15 +01:00
pub fn calculate_equal_ratios(&self) -> HResult<Vec<usize>> {
let len = self.widgets.len();
if len == 0 { return HError::no_widget(); }
2019-01-22 15:11:15 +01:00
let ratios = (0..len).map(|_| 100 / len).collect();
Ok(ratios)
}
2019-01-22 15:11:15 +01:00
pub fn calculate_coordinates(&self)
-> HResult<Vec<Coordinates>> {
let box_coords = self.get_coordinates()?;
let box_xsize = box_coords.xsize();
let box_ysize = box_coords.ysize();
let box_top = box_coords.top().y();
let mut ratios = match &self.ratios {
Some(ratios) => ratios.clone(),
None => self.calculate_equal_ratios()?
};
let mut ratios_sum: usize = ratios.iter().sum();
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 {
let prev_coords = coords.last().unwrap();
let prev_xsize = prev_coords.xsize();
let prev_xpos = prev_coords.position().x();
prev_xsize + prev_xpos + gap
};
coords.push(Coordinates {
size: Size((widget_xsize,
box_ysize)),
position: Position((widget_xpos,
box_top))
});
coords
});
Ok(coords)
2019-02-04 18:51:07 +01:00
}
2019-03-02 19:39:24 +01:00
2019-03-20 20:48:46 +01:00
pub fn set_active(&mut self, i: usize) -> HResult<()> {
if i+1 > self.widgets.len() {
HError::no_widget()?
}
self.active = Some(i);
Ok(())
}
2019-03-20 20:48:46 +01:00
pub fn active_widget(&self) -> Option<&T> {
self.widgets.get(self.active?)
}
pub fn active_widget_mut(&mut self) -> Option<&mut T> {
self.widgets.get_mut(self.active?)
}
2019-02-04 18:51:07 +01:00
}
2019-02-12 22:55:16 +01:00
impl<T> Widget for HBox<T> where T: Widget + PartialEq {
2019-03-02 19:39:24 +01:00
fn get_core(&self) -> HResult<&WidgetCore> {
Ok(&self.core)
}
fn get_core_mut(&mut self) -> HResult<&mut WidgetCore> {
Ok(&mut self.core)
}
2019-03-19 01:08:22 +01:00
fn set_coordinates(&mut self, coordinates: &Coordinates) -> HResult<()> {
self.core.coordinates = coordinates.clone();
self.resize_children()
}
2019-03-02 19:39:24 +01:00
fn render_header(&self) -> HResult<String> {
2019-03-20 20:48:46 +01:00
self.active_widget()?.render_header()
}
2019-03-02 19:39:24 +01:00
fn refresh(&mut self) -> HResult<()> {
2019-03-20 21:56:59 +01:00
if self.zoom_active {
self.active_widget_mut()?.refresh().log();
return Ok(());
}
self.resize_children().log();
2019-02-04 18:51:07 +01:00
for child in &mut self.widgets {
2019-03-19 01:08:22 +01:00
child.refresh().log();
}
2019-03-02 19:39:24 +01:00
Ok(())
}
2019-03-02 19:39:24 +01:00
fn get_drawlist(&self) -> HResult<String> {
2019-03-20 21:56:59 +01:00
if self.zoom_active {
return self.active_widget()?.get_drawlist();
}
2019-03-02 19:39:24 +01:00
Ok(self.widgets.iter().map(|child| {
2019-03-20 21:56:59 +01:00
child.get_drawlist().log_and().unwrap_or_else(|_| String::new())
2019-03-02 19:39:24 +01:00
}).collect())
}
2019-02-28 18:43:11 +01:00
fn on_event(&mut self, event: Event) -> HResult<()> {
2019-03-20 21:56:59 +01:00
self.active_widget_mut()?.on_event(event)?;
2019-02-28 18:43:11 +01:00
Ok(())
2019-01-21 15:53:16 +01:00
}
}