mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
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:
parent
5713fd490a
commit
ca5c485847
@ -69,6 +69,7 @@ impl Cursive {
|
|||||||
ncurses::initscr();
|
ncurses::initscr();
|
||||||
ncurses::keypad(ncurses::stdscr, true);
|
ncurses::keypad(ncurses::stdscr, true);
|
||||||
ncurses::noecho();
|
ncurses::noecho();
|
||||||
|
ncurses::cbreak();
|
||||||
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
|
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
|
||||||
|
|
||||||
let mut res = Cursive {
|
let mut res = Cursive {
|
||||||
|
@ -28,16 +28,19 @@ impl Button {
|
|||||||
impl View for Button {
|
impl View for Button {
|
||||||
|
|
||||||
fn draw(&self, printer: &Printer) {
|
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 {
|
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 {
|
fn on_key_event(&mut self, ch: i32) -> EventResult {
|
||||||
match ch {
|
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,
|
_ => EventResult::Ignored,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ use view::{Button,SizedView};
|
|||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
use printer::Printer;
|
use printer::Printer;
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
enum Focus {
|
enum Focus {
|
||||||
Content,
|
Content,
|
||||||
Button(usize),
|
Button(usize),
|
||||||
@ -31,7 +32,7 @@ impl Dialog {
|
|||||||
Dialog {
|
Dialog {
|
||||||
content: Box::new(view),
|
content: Box::new(view),
|
||||||
buttons: Vec::new(),
|
buttons: Vec::new(),
|
||||||
focus: Focus::Nothing,
|
focus: Focus::Content,
|
||||||
padding: Margins::new(1,1,0,0),
|
padding: Margins::new(1,1,0,0),
|
||||||
borders: Margins::new(1,1,1,1),
|
borders: Margins::new(1,1,1,1),
|
||||||
}
|
}
|
||||||
@ -60,10 +61,12 @@ impl View for Dialog {
|
|||||||
|
|
||||||
let mut height = 0;
|
let mut height = 0;
|
||||||
let mut x = 0;
|
let mut x = 0;
|
||||||
for button in self.buttons.iter().rev() {
|
for (i,button) in self.buttons.iter().enumerate().rev() {
|
||||||
// button.draw(&printer.sub_printer(),
|
|
||||||
let size = button.size;
|
let size = button.size;
|
||||||
let offset = printer.size - self.borders.bot_right() - self.padding.bot_right() - size - Vec2::new(x, 0);
|
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));
|
button.draw(&printer.sub_printer(offset, size));
|
||||||
x += size.x + 1;
|
x += size.x + 1;
|
||||||
height = max(height, size.y+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) {
|
Focus::Button(i) => match self.buttons[i].on_key_event(ch) {
|
||||||
EventResult::Ignored => match 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,
|
_ => EventResult::Ignored,
|
||||||
},
|
},
|
||||||
res => res,
|
res => res,
|
||||||
|
Loading…
Reference in New Issue
Block a user