fixed key handling

This commit is contained in:
rabite 2019-01-21 15:53:16 +01:00
parent 67c973c0af
commit b29a318928
5 changed files with 33 additions and 39 deletions

View File

@ -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);
}
}

View File

@ -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)); }
}
}
}

View File

@ -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();
}

View File

@ -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 {

View File

@ -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();
}
}