summaryrefslogtreecommitdiffstats
path: root/src/miller_columns.rs
blob: ca621e44b2c12d3756e051528b6b8da671e30e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use termion::event::Key;
use failure::Backtrace;

use crate::coordinates::{Coordinates};
use crate::widget::{Widget, WidgetCore};
use crate::hbox::HBox;
use crate::fail::{HError, HResult, ErrorLog};

#[derive(PartialEq)]
pub struct MillerColumns<T> where T: Widget {
    pub widgets: HBox<T>,
    pub core: WidgetCore,
}

impl<T> MillerColumns<T>
where
    T: Widget + PartialEq,
{
    pub fn new(core: &WidgetCore) -> MillerColumns<T> {
        MillerColumns {
            widgets: HBox::new(core),
            core: core.clone(),
        }
    }

    pub fn push_widget(&mut self, widget: T) {
        self.widgets.push_widget(widget);
        self.refresh().log();
    }

    pub fn pop_widget(&mut self) -> Option<T> {
        let widget = self.widgets.pop_widget();
        self.refresh().log();
        widget
    }

    pub fn prepend_widget(&mut self, widget: T) {
        self.widgets.prepend_widget(widget);
    }

    pub fn set_ratios(&mut self, ratios: Vec<usize>) {
        self.widgets.set_ratios(ratios);
    }

    pub fn calculate_coordinates(&self) -> HResult<Vec<Coordinates>> {
        self.widgets.calculate_coordinates()
    }

    pub fn get_left_widget(&self) -> HResult<&T> {
        let len = self.widgets.widgets.len();
        if len < 3 {
            return Err(HError::NoWidgetError(Backtrace::new()));
        }
        let widget = self.widgets.widgets.get(len - 3)?;
        Ok(widget)
    }
    pub fn get_left_widget_mut(&mut self) -> HResult<&mut T> {
        let len = self.widgets.widgets.len();
        if len < 3 {
            return Err(HError::NoWidgetError(Backtrace::new()));
        }
        let widget = self.widgets.widgets.get_mut(len - 3)?;
        Ok(widget)
    }
    pub fn get_main_widget(&self) -> HResult<&T> {
        let len = self.widgets.widgets.len();
        let widget = self.widgets.widgets.get(len-2)?;
        Ok(widget)
    }
    pub fn get_main_widget_mut(&mut self) -> HResult<&mut T> {
        let len = self.widgets.widgets.len();
        let widget = self.widgets.widgets.get_mut(len-2)?;
        Ok(widget)
    }
    pub fn get_right_widget(&self) -> HResult<&T> {
        let widget = self.widgets.widgets.last()?;
        Ok(widget)
    }
    pub fn get_right_widget_mut(&mut self) -> HResult<&mut T> {
        let widget = self.widgets.widgets.last_mut()?;
        Ok(widget)
    }
}

impl<T> Widget for MillerColumns<T>
where
    T: Widget,
    T: PartialEq
{
    fn get_core(&self) -> HResult<&WidgetCore> {
        Ok(&self.core)
    }
    fn get_core_mut(&mut self) -> HResult<&mut WidgetCore> {
        Ok(&mut self.core)
    }
    fn refresh(&mut self) -> HResult<()> {
        self.widgets.refresh().log();
        Ok(())
    }

    fn get_drawlist(&self) -> HResult<String> {
        let left_widget = self.get_left_widget()?;
        let main_widget = self.get_main_widget()?;
        let right_widget = self.get_right_widget()?;
        Ok(format!("{}{}{}",
                   main_widget.get_drawlist()?,
                   left_widget.get_drawlist()?,
                   right_widget.get_drawlist()?))
    }

    fn on_key(&mut self, key: Key) -> HResult<()> {
        self.get_main_widget_mut().unwrap().on_key(key)
    }
}