2016-07-25 06:00:13 +00:00
|
|
|
use align::HAlign;
|
2016-07-15 03:27:15 +00:00
|
|
|
use direction::Direction;
|
2016-10-02 22:22:29 +00:00
|
|
|
use event::*;
|
2018-05-22 00:54:43 +00:00
|
|
|
use rect::Rect;
|
2016-07-01 06:38:01 +00:00
|
|
|
use theme::ColorStyle;
|
2016-10-02 22:22:29 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
2015-05-19 02:41:35 +00:00
|
|
|
use vec::Vec2;
|
2016-07-10 02:05:51 +00:00
|
|
|
use view::View;
|
2018-05-18 00:37:39 +00:00
|
|
|
use {Cursive, Printer, With};
|
2015-05-19 02:41:35 +00:00
|
|
|
|
2016-07-21 05:08:06 +00:00
|
|
|
/// Simple text label with a callback when <Enter> is pressed.
|
|
|
|
///
|
2015-05-19 22:54:11 +00:00
|
|
|
/// A button shows its content in a single line and has a fixed size.
|
2016-07-21 05:08:06 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::views::Button;
|
2016-07-21 05:08:06 +00:00
|
|
|
/// let quit_button = Button::new("Quit", |s| s.quit());
|
|
|
|
/// ```
|
2015-05-19 02:41:35 +00:00
|
|
|
pub struct Button {
|
|
|
|
label: String,
|
2016-07-02 22:02:42 +00:00
|
|
|
callback: Callback,
|
2016-07-17 01:48:20 +00:00
|
|
|
enabled: bool,
|
2018-05-22 00:54:43 +00:00
|
|
|
last_size: Vec2,
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Button {
|
2015-05-19 22:54:11 +00:00
|
|
|
/// Creates a new button with the given content and callback.
|
2018-03-16 22:50:56 +00:00
|
|
|
pub fn new<F, S>(label: S, cb: F) -> Self
|
2017-11-29 10:07:42 +00:00
|
|
|
where
|
2018-03-16 22:50:56 +00:00
|
|
|
F: 'static + Fn(&mut Cursive),
|
2018-01-12 08:33:47 +00:00
|
|
|
S: Into<String>,
|
2017-11-29 10:07:42 +00:00
|
|
|
{
|
|
|
|
let label = label.into();
|
|
|
|
Self::new_raw(format!("<{}>", label), cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new button without angle brackets.
|
2018-03-16 22:50:56 +00:00
|
|
|
pub fn new_raw<F, S: Into<String>>(label: S, cb: F) -> Self
|
2017-10-11 22:07:25 +00:00
|
|
|
where
|
2018-03-16 22:50:56 +00:00
|
|
|
F: 'static + Fn(&mut Cursive),
|
2015-05-19 02:41:35 +00:00
|
|
|
{
|
|
|
|
Button {
|
2016-07-30 19:58:25 +00:00
|
|
|
label: label.into(),
|
2016-07-25 06:00:13 +00:00
|
|
|
callback: Callback::from_fn(cb),
|
2016-07-17 01:48:20 +00:00
|
|
|
enabled: true,
|
2018-05-22 00:54:43 +00:00
|
|
|
last_size: Vec2::zero(),
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-17 01:48:20 +00:00
|
|
|
|
2017-05-19 18:12:45 +00:00
|
|
|
/// Sets the function to be called when the button is pressed.
|
|
|
|
///
|
|
|
|
/// Replaces the previous callback.
|
|
|
|
pub fn set_callback<F>(&mut self, cb: F)
|
2017-10-11 22:07:25 +00:00
|
|
|
where
|
|
|
|
F: Fn(&mut Cursive) + 'static,
|
2017-05-19 18:12:45 +00:00
|
|
|
{
|
|
|
|
self.callback = Callback::from_fn(cb);
|
|
|
|
}
|
|
|
|
|
2016-07-17 01:48:20 +00:00
|
|
|
/// Disables this view.
|
|
|
|
///
|
|
|
|
/// A disabled view cannot be selected.
|
|
|
|
pub fn disable(&mut self) {
|
|
|
|
self.enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disables this view.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn disabled(self) -> Self {
|
|
|
|
self.with(Self::disable)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Re-enables this view.
|
|
|
|
pub fn enable(&mut self) {
|
|
|
|
self.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable or disable this view.
|
|
|
|
pub fn set_enabled(&mut self, enabled: bool) {
|
|
|
|
self.enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this view is enabled.
|
|
|
|
pub fn is_enabled(&self) -> bool {
|
|
|
|
self.enabled
|
|
|
|
}
|
2017-10-11 22:07:25 +00:00
|
|
|
|
2018-01-12 08:30:49 +00:00
|
|
|
/// Returns the label for this button.
|
|
|
|
///
|
|
|
|
/// Includes brackets.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use cursive::views::Button;
|
|
|
|
/// let button = Button::new("Quit", |s| s.quit());
|
|
|
|
/// assert_eq!(button.label(), "<Quit>");
|
|
|
|
/// ```
|
|
|
|
pub fn label(&self) -> &str {
|
|
|
|
&self.label
|
|
|
|
}
|
|
|
|
|
2018-01-12 08:33:47 +00:00
|
|
|
/// Sets the label to the given value.
|
|
|
|
///
|
|
|
|
/// This will include brackets.
|
|
|
|
pub fn set_label<S>(&mut self, label: S)
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
self.set_label_raw(format!("<{}>", label.into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the label exactly to the given value.
|
|
|
|
///
|
|
|
|
/// This will not include brackets.
|
|
|
|
pub fn set_label_raw<S>(&mut self, label: S)
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
self.label = label.into();
|
|
|
|
}
|
|
|
|
|
2017-10-11 22:07:25 +00:00
|
|
|
fn req_size(&self) -> Vec2 {
|
2017-11-29 10:07:42 +00:00
|
|
|
Vec2::new(self.label.width(), 1)
|
2017-10-11 22:07:25 +00:00
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for Button {
|
2016-07-16 06:44:38 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2016-07-17 08:20:41 +00:00
|
|
|
if printer.size.x == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-17 01:48:20 +00:00
|
|
|
let style = if !self.enabled {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::secondary()
|
2016-07-17 01:48:20 +00:00
|
|
|
} else if !printer.focused {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::primary()
|
2016-03-15 22:37:57 +00:00
|
|
|
} else {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::highlight()
|
2016-03-15 22:37:57 +00:00
|
|
|
};
|
2016-07-25 06:00:13 +00:00
|
|
|
|
2018-05-22 00:54:43 +00:00
|
|
|
let offset =
|
|
|
|
HAlign::Center.get_offset(self.label.width(), printer.size.x);
|
2015-05-19 22:54:11 +00:00
|
|
|
|
2015-05-27 04:45:00 +00:00
|
|
|
printer.with_color(style, |printer| {
|
2017-11-29 10:07:42 +00:00
|
|
|
printer.print((offset, 0), &self.label);
|
2015-05-23 22:58:06 +00:00
|
|
|
});
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 00:54:43 +00:00
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
self.last_size = size;
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn required_size(&mut self, _: Vec2) -> Vec2 {
|
2015-05-19 22:54:11 +00:00
|
|
|
// Meh. Fixed size we are.
|
2017-10-11 22:07:25 +00:00
|
|
|
self.req_size()
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2017-10-11 22:08:19 +00:00
|
|
|
// eprintln!("{:?}", event);
|
|
|
|
// eprintln!("{:?}", self.req_size());
|
2018-05-22 00:54:43 +00:00
|
|
|
let width = self.label.width();
|
|
|
|
let self_offset = HAlign::Center.get_offset(width, self.last_size.x);
|
2015-05-28 01:04:33 +00:00
|
|
|
match event {
|
2015-05-19 17:58:42 +00:00
|
|
|
// 10 is the ascii code for '\n', that is the return key
|
2016-07-10 02:05:51 +00:00
|
|
|
Event::Key(Key::Enter) => {
|
|
|
|
EventResult::Consumed(Some(self.callback.clone()))
|
|
|
|
}
|
2017-10-11 22:07:25 +00:00
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Release(MouseButton::Left),
|
|
|
|
position,
|
|
|
|
offset,
|
2018-05-22 02:21:27 +00:00
|
|
|
} if position
|
|
|
|
.fits_in_rect(offset + (self_offset, 0), self.req_size()) =>
|
2017-10-11 22:07:25 +00:00
|
|
|
{
|
|
|
|
EventResult::Consumed(Some(self.callback.clone()))
|
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
_ => EventResult::Ignored,
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 22:54:11 +00:00
|
|
|
|
2016-07-15 03:27:15 +00:00
|
|
|
fn take_focus(&mut self, _: Direction) -> bool {
|
2016-07-17 01:48:20 +00:00
|
|
|
self.enabled
|
2015-05-19 22:54:11 +00:00
|
|
|
}
|
2018-05-22 00:25:32 +00:00
|
|
|
|
|
|
|
fn important_area(&self, view_size: Vec2) -> Rect {
|
|
|
|
let width = self.label.width();
|
|
|
|
let offset = HAlign::Center.get_offset(width, view_size.x);
|
|
|
|
|
|
|
|
Rect::from_size((offset, 0), (width, 1))
|
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|