tui-rs/src/widgets/tabs.rs

139 lines
3.5 KiB
Rust
Raw Normal View History

2016-10-27 17:34:45 +00:00
use unicode_width::UnicodeWidthStr;
2019-01-06 11:57:06 +00:00
use crate::buffer::Buffer;
use crate::layout::Rect;
use crate::style::Style;
use crate::symbols::line;
use crate::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
///
/// ```
/// # use tui::widgets::{Block, Borders, Tabs};
2016-11-06 20:41:32 +00:00
/// # use tui::style::{Style, Color};
/// # use tui::symbols::{DOT};
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))
/// .divider(DOT);
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,
/// Tab divider
divider: &'a str,
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(),
divider: line::VERTICAL,
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>) -> Tabs<'a, T> {
2016-10-27 17:34:45 +00:00
self.block = Some(block);
self
}
pub fn titles(mut self, titles: &'a [T]) -> Tabs<'a, T> {
2016-10-27 17:34:45 +00:00
self.titles = titles;
self
}
pub fn select(mut self, selected: usize) -> Tabs<'a, T> {
2016-10-27 17:34:45 +00:00
self.selected = selected;
self
}
pub fn style(mut self, style: Style) -> 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) -> Tabs<'a, T> {
2016-11-06 17:49:57 +00:00
self.highlight_style = style;
2016-10-27 17:34:45 +00:00
self
}
pub fn divider(mut self, divider: &'a str) -> Tabs<'a, T> {
self.divider = divider;
self
}
2016-10-27 17:34:45 +00:00
}
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;
}
2019-02-03 21:07:58 +00:00
self.background(tabs_area, buf, self.style.bg);
2016-10-27 17:34:45 +00:00
let mut x = tabs_area.left();
2019-01-09 18:09:18 +00:00
let titles_length = self.titles.len();
let divider_width = self.divider.width() as u16;
2019-01-09 18:09:18 +00:00
for (title, style, last_title) in self.titles.iter().enumerate().map(|(i, t)| {
let lt = i + 1 == titles_length;
2017-12-26 11:05:47 +00:00
if i == self.selected {
2019-01-09 18:09:18 +00:00
(t, self.highlight_style, lt)
2017-10-30 21:28:18 +00:00
} else {
2019-01-09 18:09:18 +00:00
(t, self.style, lt)
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;
2019-01-09 18:09:18 +00:00
if x >= tabs_area.right() || last_title {
2016-10-27 17:34:45 +00:00
break;
} else {
buf.set_string(x, tabs_area.top(), self.divider, self.style);
x += divider_width;
2016-10-27 17:34:45 +00:00
}
}
}
}
}