Fix Button on_key_event

KEY_ENTER is actually almost never called. The return key sends the
ascii code for '\n' instead.
This commit is contained in:
Alexandre Bury 2015-05-19 10:58:42 -07:00
parent 5713fd490a
commit ca5c485847
3 changed files with 21 additions and 6 deletions

View File

@ -69,6 +69,7 @@ impl Cursive {
ncurses::initscr();
ncurses::keypad(ncurses::stdscr, true);
ncurses::noecho();
ncurses::cbreak();
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
let mut res = Cursive {

View File

@ -28,16 +28,19 @@ impl Button {
impl View for Button {
fn draw(&self, printer: &Printer) {
printer.print(Vec2::zero(), &self.label);
printer.print((1u32,0u32), &self.label);
printer.print((0u32,0u32), "<");
printer.print((printer.size.x-1,0), ">");
}
fn get_min_size(&self, req: SizeRequest) -> Vec2 {
Vec2::new(self.label.len() as u32, 1)
Vec2::new(2 + self.label.len() as u32, 1)
}
fn on_key_event(&mut self, ch: i32) -> EventResult {
match ch {
ncurses::KEY_ENTER => EventResult::callback(self.callback.clone()),
// 10 is the ascii code for '\n', that is the return key
10 => EventResult::callback(self.callback.clone()),
_ => EventResult::Ignored,
}
}

View File

@ -9,6 +9,7 @@ use view::{Button,SizedView};
use vec::Vec2;
use printer::Printer;
#[derive(PartialEq)]
enum Focus {
Content,
Button(usize),
@ -31,7 +32,7 @@ impl Dialog {
Dialog {
content: Box::new(view),
buttons: Vec::new(),
focus: Focus::Nothing,
focus: Focus::Content,
padding: Margins::new(1,1,0,0),
borders: Margins::new(1,1,1,1),
}
@ -60,10 +61,12 @@ impl View for Dialog {
let mut height = 0;
let mut x = 0;
for button in self.buttons.iter().rev() {
// button.draw(&printer.sub_printer(),
for (i,button) in self.buttons.iter().enumerate().rev() {
let size = button.size;
let offset = printer.size - self.borders.bot_right() - self.padding.bot_right() - size - Vec2::new(x, 0);
if self.focus == Focus::Button(i) {
// Add some special effect to the focused button
}
button.draw(&printer.sub_printer(offset, size));
x += size.x + 1;
height = max(height, size.y+1);
@ -132,6 +135,14 @@ impl View for Dialog {
},
Focus::Button(i) => match self.buttons[i].on_key_event(ch) {
EventResult::Ignored => match ch {
ncurses::KEY_RIGHT if i+1 < self.buttons.len() => {
self.focus = Focus::Button(i+1);
EventResult::consume()
},
ncurses::KEY_LEFT if i > 0 => {
self.focus = Focus::Button(i-1);
EventResult::consume()
},
_ => EventResult::Ignored,
},
res => res,