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
|
|
|
|
///
|
|
|
|
/// ```
|
2017-12-26 15:55:13 +00:00
|
|
|
/// # use tui::widgets::{Block, Borders, Tabs};
|
2016-11-06 20:41:32 +00:00
|
|
|
/// # use tui::style::{Style, Color};
|
2019-01-09 18:08:25 +00:00
|
|
|
/// # use tui::symbols::{DOT};
|
2016-11-04 18:27:19 +00:00
|
|
|
/// Tabs::default()
|
2017-12-26 15:55:13 +00:00
|
|
|
/// .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))
|
2019-01-09 18:42:01 +00:00
|
|
|
/// .highlight_style(Style::default().fg(Color::Yellow))
|
2019-01-09 18:08:25 +00:00
|
|
|
/// .divider(DOT);
|
2016-11-04 16:54:57 +00:00
|
|
|
/// ```
|
2017-10-30 22:34:09 +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
|
2017-10-30 22:34:09 +00:00
|
|
|
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,
|
2019-01-09 18:08:25 +00:00
|
|
|
/// Tab divider
|
|
|
|
divider: &'a str,
|
2016-10-27 17:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:34:09 +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(),
|
2019-01-09 18:08:25 +00:00
|
|
|
divider: line::VERTICAL,
|
2016-10-27 17:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 22:34:09 +00:00
|
|
|
impl<'a, T> Tabs<'a, T>
|
|
|
|
where
|
|
|
|
T: AsRef<str>,
|
|
|
|
{
|
2018-09-01 12:05:33 +00:00
|
|
|
pub fn block(mut self, block: Block<'a>) -> Tabs<'a, T> {
|
2016-10-27 17:34:45 +00:00
|
|
|
self.block = Some(block);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-09-01 12:05:33 +00:00
|
|
|
pub fn titles(mut self, titles: &'a [T]) -> Tabs<'a, T> {
|
2016-10-27 17:34:45 +00:00
|
|
|
self.titles = titles;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-09-01 12:05:33 +00:00
|
|
|
pub fn select(mut self, selected: usize) -> Tabs<'a, T> {
|
2016-10-27 17:34:45 +00:00
|
|
|
self.selected = selected;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-09-01 12:05:33 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-01 12:05:33 +00:00
|
|
|
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
|
|
|
|
}
|
2019-01-09 18:08:25 +00:00
|
|
|
|
|
|
|
pub fn divider(mut self, divider: &'a str) -> Tabs<'a, T> {
|
|
|
|
self.divider = divider;
|
|
|
|
self
|
|
|
|
}
|
2016-10-27 17:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:34:09 +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 {
|
2017-09-03 09:58:39 +00:00
|
|
|
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-11-02 16:08:52 +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-11-02 16:08:52 +00:00
|
|
|
|
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();
|
2019-01-13 17:02:58 +00:00
|
|
|
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 {
|
2017-10-30 22:34:09 +00:00
|
|
|
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 {
|
2019-05-29 13:51:05 +00:00
|
|
|
buf.set_string(x, tabs_area.top(), self.divider, self.style);
|
2019-01-09 18:08:25 +00:00
|
|
|
x += divider_width;
|
2016-10-27 17:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|