cursive/src/view/button.rs

58 lines
1.5 KiB
Rust
Raw Normal View History

2015-05-19 02:41:35 +00:00
use std::rc::Rc;
2015-05-22 06:29:49 +00:00
use color;
2015-05-19 02:41:35 +00:00
use ::Cursive;
use vec::Vec2;
use view::{View,ViewPath,SizeRequest};
use event::{Callback,EventResult};
use printer::Printer;
/// Simple text label with a callback when ENTER is pressed.
/// A button shows its content in a single line and has a fixed size.
2015-05-19 02:41:35 +00:00
pub struct Button {
label: String,
callback: Rc<Callback>,
}
impl Button {
/// Creates a new button with the given content and callback.
2015-05-19 02:41:35 +00:00
pub fn new<F>(label: &str, cb: F) -> Self
where F: Fn(&mut Cursive, &ViewPath) + 'static
{
Button {
label: label.to_string(),
callback: Rc::new(Box::new(cb)),
}
}
}
impl View for Button {
2015-05-22 23:28:05 +00:00
fn draw(&mut self, printer: &Printer, focused: bool) {
2015-05-22 06:29:49 +00:00
let style = if !focused { color::PRIMARY } else { color::HIGHLIGHT };
let x = printer.size.x - 1;
2015-05-22 06:29:49 +00:00
let printer = printer.style(style);
printer.print((1u32,0u32), &self.label);
printer.print((0u32,0u32), "<");
printer.print((x,0), ">");
2015-05-19 02:41:35 +00:00
}
fn get_min_size(&self, _: SizeRequest) -> Vec2 {
// Meh. Fixed size we are.
Vec2::new(2 + self.label.len() as u32, 1)
2015-05-19 02:41:35 +00:00
}
fn on_key_event(&mut self, ch: i32) -> EventResult {
match ch {
// 10 is the ascii code for '\n', that is the return key
10 => EventResult::callback(self.callback.clone()),
2015-05-19 02:41:35 +00:00
_ => EventResult::Ignored,
}
}
fn take_focus(&mut self) -> bool {
true
}
2015-05-19 02:41:35 +00:00
}