cursive/src/view/dialog.rs

389 lines
13 KiB
Rust
Raw Normal View History

2015-05-19 02:41:35 +00:00
use std::cmp::max;
use std::any::Any;
2015-05-19 02:41:35 +00:00
2016-03-15 22:37:57 +00:00
use Cursive;
use With;
2016-07-15 05:32:43 +00:00
use direction::Direction;
2015-06-03 02:36:22 +00:00
use align::*;
use event::*;
use theme::ColorStyle;
use view::{DummyView, Selector, TextView, View};
2016-03-15 22:37:57 +00:00
use view::{Button, SizedView};
use vec::{Vec2, Vec4};
2016-07-14 06:25:54 +00:00
use Printer;
2015-05-16 21:02:15 +00:00
2016-07-04 23:04:32 +00:00
use unicode_width::UnicodeWidthStr;
#[derive(PartialEq)]
2015-05-16 21:02:15 +00:00
enum Focus {
Content,
Button(usize),
}
2015-05-20 00:31:52 +00:00
/// Popup-like view with a main content, and optional buttons under it.
///
/// # Examples
///
/// ```
2015-06-04 18:40:35 +00:00
/// # use cursive::view::{Dialog,TextView};
/// let dialog = Dialog::new(TextView::new("Hello!"))
/// .button("Ok", |s| s.quit());
2015-05-20 00:31:52 +00:00
/// ```
2015-05-16 21:02:15 +00:00
pub struct Dialog {
2015-05-22 06:29:49 +00:00
title: String,
2015-05-16 21:02:15 +00:00
content: Box<View>,
2015-05-19 02:41:35 +00:00
buttons: Vec<SizedView<Button>>,
padding: Vec4,
borders: Vec4,
2015-05-19 02:41:35 +00:00
2015-05-16 21:02:15 +00:00
focus: Focus,
2015-06-03 02:36:22 +00:00
align: Align,
2015-05-16 21:02:15 +00:00
}
impl Dialog {
/// Creates a new `Dialog` with empty content.
///
/// You should probably call `content()` next.
pub fn empty() -> Self {
Self::new(DummyView)
}
/// Creates a new `Dialog` with the given content.
2015-05-16 21:02:15 +00:00
pub fn new<V: View + 'static>(view: V) -> Self {
Dialog {
content: Box::new(view),
buttons: Vec::new(),
2015-05-22 06:29:49 +00:00
title: String::new(),
focus: Focus::Content,
2016-03-15 22:37:57 +00:00
padding: Vec4::new(1, 1, 0, 0),
borders: Vec4::new(1, 1, 1, 1),
2015-06-03 02:36:22 +00:00
align: Align::top_right(),
2015-05-16 21:02:15 +00:00
}
}
/// Sets the content for this dialog.
///
/// Chainable variant.
pub fn content<V: View + 'static>(self, view: V) -> Self {
self.with(|s| s.set_content(view))
}
/// Sets the content for this dialog.
///
/// Previous content will be dropped.
pub fn set_content<V: View + 'static>(&mut self, view: V) {
self.content = Box::new(view);
}
/// Convenient method to create an infobox.
///
/// It will contain the given text and a `Ok` dismiss button.
2016-06-28 05:10:59 +00:00
pub fn info(text: &str) -> Self {
Self::new(TextView::new(text)).dismiss_button("Ok")
}
2015-05-20 00:31:52 +00:00
/// Adds a button to the dialog with the given label and callback.
///
/// Consumes and returns self for easy chaining.
2016-06-28 05:40:11 +00:00
pub fn button<F>(mut self, label: &str, cb: F) -> Self
2015-05-24 00:07:22 +00:00
where F: Fn(&mut Cursive) + 'static
2015-05-16 21:02:15 +00:00
{
2015-05-19 02:41:35 +00:00
self.buttons.push(SizedView::new(Button::new(label, cb)));
2015-05-16 21:02:15 +00:00
self
}
2015-05-19 02:41:35 +00:00
2015-06-03 22:36:51 +00:00
/// Sets the horizontal alignment for the buttons, if any.
/// Only works if the buttons are as a row at the bottom of the dialog.
2015-06-03 02:36:22 +00:00
pub fn h_align(mut self, h: HAlign) -> Self {
self.align.h = h;
self
}
2015-06-03 22:36:51 +00:00
/// Sets the vertical alignment for the buttons, if any.
/// Only works if the buttons are as a column to the right of the dialog.
2015-06-03 02:36:22 +00:00
pub fn v_align(mut self, v: VAlign) -> Self {
self.align.v = v;
self
}
/// Shortcut method to add a button that will dismiss the dialog.
2016-06-28 05:40:11 +00:00
pub fn dismiss_button(self, label: &str) -> Self {
2015-05-24 00:07:22 +00:00
self.button(label, |s| s.screen_mut().pop_layer())
}
/// Sets the title of the dialog.
/// If not empty, it will be visible at the top.
2015-05-22 06:29:49 +00:00
pub fn title(mut self, label: &str) -> Self {
self.title = label.to_string();
self
}
2015-05-26 23:48:27 +00:00
/// Sets the padding in the dialog (around content and buttons).
pub fn padding<T: Into<Vec4>>(mut self, padding: T) -> Self {
self.padding = padding.into();
self
}
/// Sets the top padding in the dialog (under the title).
pub fn padding_top(mut self, padding: usize) -> Self {
self.padding.top = padding;
self
}
/// Sets the bottom padding in the dialog (under buttons).
pub fn padding_bottom(mut self, padding: usize) -> Self {
self.padding.bottom = padding;
self
}
/// Sets the left padding in the dialog.
pub fn padding_left(mut self, padding: usize) -> Self {
self.padding.left = padding;
self
}
/// Sets the right padding in the dialog.
pub fn padding_right(mut self, padding: usize) -> Self {
self.padding.right = padding;
self
}
2015-05-16 21:02:15 +00:00
}
impl View for Dialog {
fn draw(&self, printer: &Printer) {
2015-05-19 02:41:35 +00:00
// This will be the buttons_height used by the buttons.
let mut buttons_height = 0;
2015-05-22 07:43:58 +00:00
// Current horizontal position of the next button we'll draw.
2015-06-03 02:36:22 +00:00
2015-06-08 22:11:36 +00:00
// Sum of the sizes + len-1 for margins
2015-07-28 19:54:32 +00:00
let width = if self.buttons.is_empty() {
0
} else {
2016-03-15 22:37:57 +00:00
self.buttons
.iter()
2015-07-28 19:54:32 +00:00
.map(|button| button.size.x)
2016-03-15 22:37:57 +00:00
.fold(0, |a, b| a + b) + self.buttons.len() - 1
2015-07-28 19:54:32 +00:00
};
2015-06-03 02:36:22 +00:00
let overhead = self.padding + self.borders;
2016-07-17 05:05:28 +00:00
if printer.size.x < overhead.horizontal() {
return;
}
2015-07-28 19:54:32 +00:00
let mut offset = overhead.left +
self.align
.h
.get_offset(width, printer.size.x - overhead.horizontal());
2016-07-17 05:05:28 +00:00
let overhead_bottom = self.padding.bottom + self.borders.bottom + 1;
if overhead_bottom > printer.size.y {
return;
}
let y = printer.size.y - overhead_bottom;
2015-06-03 02:36:22 +00:00
for (i, button) in self.buttons.iter().enumerate() {
2015-05-19 02:41:35 +00:00
let size = button.size;
// Add some special effect to the focused button
2016-03-15 22:37:57 +00:00
button.draw(&printer.sub_printer(Vec2::new(offset, y),
size,
self.focus == Focus::Button(i)));
2015-05-22 07:43:58 +00:00
// Keep 1 blank between two buttons
2015-06-03 02:36:22 +00:00
offset += size.x + 1;
2015-05-22 07:43:58 +00:00
// Also keep 1 blank above the buttons
buttons_height = max(buttons_height, size.y + 1);
2015-05-19 02:41:35 +00:00
}
2015-05-22 07:43:58 +00:00
// What do we have left?
2016-07-20 03:44:20 +00:00
let taken = Vec2::new(0, buttons_height) + self.borders.combined() +
self.padding.combined();
2016-07-17 05:05:28 +00:00
if !taken.fits_in(printer.size) {
return;
}
let inner_size = printer.size - taken;
2015-05-19 02:41:35 +00:00
self.content
.draw(&printer.sub_printer(self.borders.top_left() +
self.padding.top_left(),
inner_size,
self.focus == Focus::Content));
2015-05-19 02:41:35 +00:00
2016-03-15 22:37:57 +00:00
printer.print_box(Vec2::new(0, 0), printer.size);
2015-05-22 06:29:49 +00:00
2016-06-28 05:40:11 +00:00
if !self.title.is_empty() {
2016-07-04 23:04:32 +00:00
let len = self.title.width();
2016-07-17 08:20:41 +00:00
if len + 4 > printer.size.x {
return;
}
let x = (printer.size.x - len) / 2;
2016-03-15 22:37:57 +00:00
printer.print((x - 2, 0), "");
printer.print((x + len, 0), "");
2015-05-22 06:29:49 +00:00
printer.with_color(ColorStyle::TitlePrimary,
|p| p.print((x, 0), &self.title));
2015-05-22 06:29:49 +00:00
}
2015-05-16 21:02:15 +00:00
}
fn get_min_size(&mut self, req: Vec2) -> Vec2 {
2015-05-22 07:43:58 +00:00
// Padding and borders are not available for kids.
let nomans_land = self.padding.combined() + self.borders.combined();
2015-05-16 21:02:15 +00:00
// Buttons are not flexible, so their size doesn't depend on ours.
2016-03-15 22:37:57 +00:00
let mut buttons_size = Vec2::new(0, 0);
2015-07-28 19:54:32 +00:00
if !self.buttons.is_empty() {
buttons_size.x += self.buttons.len() - 1;
}
for button in &mut self.buttons {
2015-05-19 02:41:35 +00:00
let s = button.view.get_min_size(req);
2015-06-09 05:24:59 +00:00
buttons_size.x += s.x;
2015-05-19 02:41:35 +00:00
buttons_size.y = max(buttons_size.y, s.y + 1);
}
// We also remove one row for the buttons.
2016-07-17 05:05:28 +00:00
let taken = nomans_land + Vec2::new(0, buttons_size.y);
if !taken.fits_in(req) {
// Bad!!
return taken;
}
let content_req = req - taken;
let content_size = self.content.get_min_size(content_req);
2015-05-22 07:43:58 +00:00
// On the Y axis, we add buttons and content.
// On the X axis, we take the max.
2015-05-22 06:29:49 +00:00
let mut inner_size = Vec2::new(max(content_size.x, buttons_size.x),
2016-03-15 22:37:57 +00:00
content_size.y + buttons_size.y) +
self.padding.combined() +
self.borders.combined();
2015-05-22 06:29:49 +00:00
2016-06-28 05:40:11 +00:00
if !self.title.is_empty() {
2015-05-22 07:43:58 +00:00
// If we have a title, we have to fit it too!
2016-07-04 23:04:32 +00:00
inner_size.x = max(inner_size.x, self.title.width() + 6);
2015-05-22 06:29:49 +00:00
}
2015-05-19 02:41:35 +00:00
2015-05-22 06:29:49 +00:00
inner_size
2015-05-19 02:41:35 +00:00
}
fn layout(&mut self, mut size: Vec2) {
2015-05-22 07:43:58 +00:00
// Padding and borders are taken, sorry.
// TODO: handle border-less themes?
2016-07-17 05:05:28 +00:00
let taken = self.borders.combined() + self.padding.combined();
size = if taken.fits_in(size) {
size - taken
} else {
Vec2::zero()
};
2015-05-19 02:41:35 +00:00
2015-05-22 07:43:58 +00:00
// Buttons are kings, we give them everything they want.
2015-05-19 02:41:35 +00:00
let mut buttons_height = 0;
for button in self.buttons.iter_mut().rev() {
let size = button.get_min_size(size);
2016-03-15 22:37:57 +00:00
buttons_height = max(buttons_height, size.y + 1);
2015-05-19 02:41:35 +00:00
button.layout(size);
}
2015-05-22 07:43:58 +00:00
// Poor content will have to make do with what's left.
2016-07-17 05:05:28 +00:00
if buttons_height > size.y {
buttons_height = size.y;
}
2015-05-19 02:41:35 +00:00
self.content.layout(size - Vec2::new(0, buttons_height));
}
fn on_event(&mut self, event: Event) -> EventResult {
2015-05-19 02:41:35 +00:00
match self.focus {
2015-05-22 07:43:58 +00:00
// If we are on the content, we can only go down.
2016-06-25 23:36:22 +00:00
Focus::Content => {
match self.content.on_event(event) {
EventResult::Ignored if !self.buttons.is_empty() => {
match event {
2016-06-28 05:40:11 +00:00
Event::Key(Key::Down) |
Event::Key(Key::Tab) |
Event::Shift(Key::Tab) => {
2016-06-25 23:36:22 +00:00
// Default to leftmost button when going down.
self.focus = Focus::Button(0);
EventResult::Consumed(None)
}
_ => EventResult::Ignored,
}
2016-03-15 22:37:57 +00:00
}
2016-06-25 23:36:22 +00:00
res => res,
}
}
// If we are on a button, we have more choice
Focus::Button(i) => {
match self.buttons[i].on_event(event) {
EventResult::Ignored => {
match event {
// Up goes back to the content
Event::Key(Key::Up) => {
if self.content
2016-07-15 05:32:43 +00:00
.take_focus(Direction::down()) {
self.focus = Focus::Content;
EventResult::Consumed(None)
} else {
EventResult::Ignored
}
}
Event::Shift(Key::Tab) => {
if self.content
2016-07-15 05:32:43 +00:00
.take_focus(Direction::back()) {
2016-06-25 23:36:22 +00:00
self.focus = Focus::Content;
EventResult::Consumed(None)
} else {
EventResult::Ignored
}
}
Event::Key(Key::Tab) => {
if self.content
2016-07-15 05:32:43 +00:00
.take_focus(Direction::front()) {
self.focus = Focus::Content;
EventResult::Consumed(None)
} else {
EventResult::Ignored
}
}
2016-06-25 23:36:22 +00:00
// Left and Right move to other buttons
2016-06-28 05:40:11 +00:00
Event::Key(Key::Right) if i + 1 <
self.buttons
.len() => {
2016-06-25 23:36:22 +00:00
self.focus = Focus::Button(i + 1);
EventResult::Consumed(None)
}
2016-06-28 05:40:11 +00:00
Event::Key(Key::Left) if i > 0 => {
2016-06-25 23:36:22 +00:00
self.focus = Focus::Button(i - 1);
EventResult::Consumed(None)
}
_ => EventResult::Ignored,
}
2016-03-15 22:37:57 +00:00
}
2016-06-25 23:36:22 +00:00
res => res,
}
}
}
}
2016-07-15 05:32:43 +00:00
fn take_focus(&mut self, source: Direction) -> bool {
// Dialogs aren't meant to be used in layouts, so...
// Let's be super lazy and not even care about the focus source.
if self.content.take_focus(source) {
self.focus = Focus::Content;
true
} else if !self.buttons.is_empty() {
self.focus = Focus::Button(0);
true
} else {
false
2015-05-19 02:41:35 +00:00
}
2015-05-16 21:02:15 +00:00
}
fn find(&mut self, selector: &Selector) -> Option<&mut Any> {
self.content.find(selector)
}
2015-05-16 21:02:15 +00:00
}