diff options
author | rabite <rabite@posteo.de> | 2019-01-21 15:53:16 +0100 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-01-21 15:53:16 +0100 |
commit | b29a3189287a4a603bcfb74ff7db52410f345d3d (patch) | |
tree | 233f1b67287726084e43bb38afd2361f66348721 | |
parent | 67c973c0af1d35f0f832d4eb594c12868ef78008 (diff) |
fixed key handling
-rw-r--r-- | src/hbox.rs | 19 | ||||
-rw-r--r-- | src/listview.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | src/widget.rs | 8 | ||||
-rw-r--r-- | src/window.rs | 27 |
5 files changed, 33 insertions, 39 deletions
diff --git a/src/hbox.rs b/src/hbox.rs index 3be950a..7013b2b 100644 --- a/src/hbox.rs +++ b/src/hbox.rs @@ -1,5 +1,14 @@ +use termion::event::{Event}; + use crate::widget::Widget; +// pub struct Child<T> { +// widget: T, +// position: (u16, u16), +// size: (u16, u16), +// active: bool +// } + pub struct HBox { dimensions: (u16, u16), position: (u16, u16), @@ -20,9 +29,7 @@ impl HBox { impl Widget for HBox { fn render(&self) -> Vec<String> { - // self.children.iter().map(|child| { - // child.render() - // }).collect() + // HBox doesnt' draw anything itself vec![] } @@ -47,5 +54,9 @@ impl Widget for HBox { } fn get_position(&self) -> (u16, u16) { self.position - } + } + + fn on_event(&mut self, event: Event) { + self.children[self.main].on_event(event); + } } diff --git a/src/listview.rs b/src/listview.rs index 8fab137..2019227 100644 --- a/src/listview.rs +++ b/src/listview.rs @@ -1,4 +1,4 @@ -use termion::event::{Key}; +use termion::event::{Key,Event}; use crate::term; use crate::files::Files; @@ -81,7 +81,7 @@ impl Widget for ListView<Files> { fn get_drawlist(&mut self) -> String { let mut output = term::reset(); - let (_xsize, ysize) = self.dimensions; + let (xsize, ysize) = self.dimensions; let (xpos, ypos) = self.position; output += &term::reset(); @@ -102,7 +102,7 @@ impl Widget for ListView<Files> { // if ysize as usize > self.buffer.len() { - // let start_y = self.buffer.len() + 1; + // let start_y = self.buffer.len() + 1 + ypos as usize; // for i in start_y..ysize as usize { // output += &format!("{}{:xsize$}{}", term::gotoy(i), " ", xsize = xsize as usize); // } @@ -119,7 +119,7 @@ impl Widget for ListView<Files> { Key::Up => { self.move_up(); self.refresh() }, Key::Down => { self.move_down(); self.refresh() }, //Key::Right => self.go(), - _ => {} + _ => { self.bad(Event::Key(key)); } } } } diff --git a/src/main.rs b/src/main.rs index c0e786f..b6180e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,18 +29,18 @@ fn main() { let mut _screen = AlternateScreen::from(Box::new(stdout())); let mut _stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap()); - let files = files::get_files("/home/project/").unwrap(); - let listview = ListView::new(files, (50,20), (10,10)); + let files = files::get_files("/home/project/code").unwrap(); + let listview = ListView::new(files, (50,50), (10,10)); - let files = files::get_files("/home/project/").unwrap(); - let listview2 = ListView::new(files, (50,20), (80,10)); + let files = files::get_files("/home/project/code").unwrap(); + let listview2 = ListView::new(files, (50,50), (80,10)); let boxed = vec![listview.to_trait(), listview2.to_trait()]; let hbox = HBox::new(boxed); let mut win = Window::new(hbox); - win.run(); + win.handle_input(); write!(_stdout, "{}", termion::cursor::Show).unwrap(); } diff --git a/src/widget.rs b/src/widget.rs index 8b9c166..72ca00b 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -41,6 +41,14 @@ pub trait Widget { lenstr } + fn on_event(&mut self, event: Event) { + match event { + Event::Key(Key::Char('q')) => panic!("It's your fault!"), + Event::Key(key) => self.on_key(key), + Event::Mouse(button) => self.on_mouse(button), + Event::Unsupported(wtf) => self.on_wtf(wtf), + } + } fn on_key(&mut self, key: Key) { match key { diff --git a/src/window.rs b/src/window.rs index 51368dd..d8deb40 100644 --- a/src/window.rs +++ b/src/window.rs @@ -44,11 +44,6 @@ where win } - pub fn run(&mut self) { - self.draw(); - self.handle_input(); - } - pub fn draw(&mut self) { let output = self.widget.get_drawlist(); self.screen.write(output.as_ref()).unwrap(); @@ -99,33 +94,13 @@ where None } - pub fn quit(&mut self) { - panic!("It's your fault!"); - } - pub fn handle_input(&mut self) { self.draw(); for event in stdin().events() { Self::clear_status(); self.draw(); let event = event.unwrap(); - match event { - Event::Key(Key::Char('q')) => { - self.quit(); - } - Event::Key(Key::Left) => { - return; - } - Event::Key(key) => { - self.widget.on_key(key); - } - Event::Mouse(button) => { - self.widget.on_mouse(button); - } - Event::Unsupported(value) => { - self.widget.on_wtf(value); - } - } + self.widget.on_event(event); self.draw(); } } |