tui-rs/src/widgets/tabs.rs

132 lines
3.2 KiB
Rust
Raw Normal View History

2016-10-27 17:34:45 +00:00
use unicode_width::UnicodeWidthStr;
use buffer::Buffer;
use layout::Rect;
2016-11-06 17:49:57 +00:00
use style::Style;
2016-10-27 17:34:45 +00:00
use symbols::line;
2018-05-06 10:59:24 +00:00
use widgets::{Block, Widget};
2016-10-27 17:34:45 +00:00
2016-11-04 16:54:57 +00:00
/// A widget to display available tabs in a multiple panels context.
///
/// # Examples
///
/// ```
/// # extern crate tui;
/// # use tui::widgets::{Block, Borders, Tabs};
2016-11-06 20:41:32 +00:00
/// # use tui::style::{Style, Color};
2016-11-04 16:54:57 +00:00
/// # fn main() {
2016-11-04 18:27:19 +00:00
/// Tabs::default()
/// .block(Block::default().title("Tabs").borders(Borders::ALL))
2016-11-04 18:27:19 +00:00
/// .titles(&["Tab1", "Tab2", "Tab3", "Tab4"])
2016-11-06 20:41:32 +00:00
/// .style(Style::default().fg(Color::White))
/// .highlight_style(Style::default().fg(Color::Yellow));
2016-11-04 16:54:57 +00:00
/// # }
/// ```
pub struct Tabs<'a, T>
where
T: AsRef<str> + 'a,
{
2016-11-04 16:54:57 +00:00
/// A block to wrap this widget in if necessary
2016-10-27 17:34:45 +00:00
block: Option<Block<'a>>,
2016-11-04 16:54:57 +00:00
/// One title for each tab
titles: &'a [T],
2016-11-04 16:54:57 +00:00
/// The index of the selected tabs
2016-10-27 17:34:45 +00:00
selected: usize,
2016-11-06 17:49:57 +00:00
/// The style used to draw the text
style: Style,
/// The style used to display the selected item
highlight_style: Style,
2016-10-27 17:34:45 +00:00
}
impl<'a, T> Default for Tabs<'a, T>
where
T: AsRef<str>,
{
fn default() -> Tabs<'a, T> {
2016-10-27 17:34:45 +00:00
Tabs {
block: None,
titles: &[],
selected: 0,
2016-11-06 17:49:57 +00:00
style: Default::default(),
highlight_style: Default::default(),
2016-10-27 17:34:45 +00:00
}
}
}
impl<'a, T> Tabs<'a, T>
where
T: AsRef<str>,
{
pub fn block(&mut self, block: Block<'a>) -> &mut Tabs<'a, T> {
2016-10-27 17:34:45 +00:00
self.block = Some(block);
self
}
pub fn titles(&mut self, titles: &'a [T]) -> &mut Tabs<'a, T> {
2016-10-27 17:34:45 +00:00
self.titles = titles;
self
}
pub fn select(&mut self, selected: usize) -> &mut Tabs<'a, T> {
2016-10-27 17:34:45 +00:00
self.selected = selected;
self
}
pub fn style(&mut self, style: Style) -> &mut Tabs<'a, T> {
2016-11-06 17:49:57 +00:00
self.style = style;
2016-10-27 17:34:45 +00:00
self
}
pub fn highlight_style(&mut self, style: Style) -> &mut Tabs<'a, T> {
2016-11-06 17:49:57 +00:00
self.highlight_style = style;
2016-10-27 17:34:45 +00:00
self
}
}
impl<'a, T> Widget for Tabs<'a, T>
where
T: AsRef<str>,
{
2018-08-12 22:27:56 +00:00
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
2016-10-27 17:34:45 +00:00
let tabs_area = match self.block {
Some(ref mut b) => {
2016-11-02 18:16:53 +00:00
b.draw(area, buf);
2016-10-27 17:34:45 +00:00
b.inner(area)
}
2018-08-12 22:27:56 +00:00
None => area,
2016-10-27 17:34:45 +00:00
};
2016-10-27 17:34:45 +00:00
if tabs_area.height < 1 {
return;
}
2016-11-06 17:49:57 +00:00
self.background(&tabs_area, buf, self.style.bg);
2016-10-27 17:34:45 +00:00
let mut x = tabs_area.left();
2017-12-26 11:05:47 +00:00
for (title, style) in self.titles.iter().enumerate().map(|(i, t)| {
if i == self.selected {
2017-10-30 21:28:18 +00:00
(t, &self.highlight_style)
} else {
(t, &self.style)
2017-12-26 11:05:47 +00:00
}
}) {
2016-10-27 17:34:45 +00:00
x += 1;
if x > tabs_area.right() {
break;
} else {
buf.set_string(x, tabs_area.top(), title.as_ref(), style);
x += title.as_ref().width() as u16 + 1;
if x >= tabs_area.right() {
2016-10-27 17:34:45 +00:00
break;
} else {
2016-11-06 17:49:57 +00:00
buf.get_mut(x, tabs_area.top())
.set_symbol(line::VERTICAL)
.set_fg(self.style.fg)
.set_bg(self.style.bg);
2016-10-27 17:34:45 +00:00
x += 1;
}
}
}
}
}