From ca5c4858477d89e5ac61b1826837393720976759 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Tue, 19 May 2015 10:58:42 -0700 Subject: [PATCH] Fix Button on_key_event KEY_ENTER is actually almost never called. The return key sends the ascii code for '\n' instead. --- src/lib.rs | 1 + src/view/button.rs | 9 ++++++--- src/view/dialog.rs | 17 ++++++++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fdab802..42e6912 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { diff --git a/src/view/button.rs b/src/view/button.rs index d56b22c..6070dcf 100644 --- a/src/view/button.rs +++ b/src/view/button.rs @@ -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, } } diff --git a/src/view/dialog.rs b/src/view/dialog.rs index 32e0314..a73a764 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -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,