2021-07-18 15:50:39 +00:00
|
|
|
use super::{Component, DrawableComponent, EventState};
|
2021-07-31 16:03:39 +00:00
|
|
|
use crate::components::command::{self, CommandInfo};
|
|
|
|
use crate::config::KeyConfig;
|
2021-07-08 14:08:04 +00:00
|
|
|
use crate::event::Key;
|
|
|
|
use anyhow::Result;
|
|
|
|
use strum_macros::EnumIter;
|
|
|
|
use tui::{
|
|
|
|
backend::Backend,
|
|
|
|
layout::Rect,
|
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
text::Spans,
|
|
|
|
widgets::{Block, Borders, Tabs},
|
|
|
|
Frame,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, EnumIter)]
|
|
|
|
pub enum Tab {
|
|
|
|
Records,
|
2021-10-09 07:03:52 +00:00
|
|
|
Properties,
|
2021-10-07 16:30:13 +00:00
|
|
|
Sql,
|
2021-07-08 14:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Tab {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TabComponent {
|
|
|
|
pub selected_tab: Tab,
|
2021-07-31 16:03:39 +00:00
|
|
|
key_config: KeyConfig,
|
2021-07-08 14:08:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 16:03:39 +00:00
|
|
|
impl TabComponent {
|
|
|
|
pub fn new(key_config: KeyConfig) -> Self {
|
2021-07-08 14:08:04 +00:00
|
|
|
Self {
|
|
|
|
selected_tab: Tab::Records,
|
2021-07-31 16:03:39 +00:00
|
|
|
key_config,
|
2021-07-08 14:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-31 16:03:39 +00:00
|
|
|
|
2021-08-08 11:57:40 +00:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.selected_tab = Tab::Records;
|
|
|
|
}
|
|
|
|
|
2021-07-31 16:03:39 +00:00
|
|
|
fn names(&self) -> Vec<String> {
|
|
|
|
vec![
|
|
|
|
command::tab_records(&self.key_config).name,
|
2021-10-09 07:03:52 +00:00
|
|
|
command::tab_properties(&self.key_config).name,
|
2021-10-07 16:30:13 +00:00
|
|
|
command::tab_sql_editor(&self.key_config).name,
|
2021-07-31 16:03:39 +00:00
|
|
|
]
|
|
|
|
}
|
2021-07-08 14:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DrawableComponent for TabComponent {
|
2021-09-17 06:16:31 +00:00
|
|
|
fn draw<B: Backend>(&self, f: &mut Frame<B>, area: Rect, _focused: bool) -> Result<()> {
|
2021-07-31 16:03:39 +00:00
|
|
|
let titles = self.names().iter().cloned().map(Spans::from).collect();
|
2021-07-08 14:08:04 +00:00
|
|
|
let tabs = Tabs::new(titles)
|
|
|
|
.block(Block::default().borders(Borders::ALL))
|
|
|
|
.select(self.selected_tab as usize)
|
|
|
|
.style(Style::default().fg(Color::DarkGray))
|
|
|
|
.highlight_style(
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Reset)
|
|
|
|
.add_modifier(Modifier::UNDERLINED),
|
|
|
|
);
|
|
|
|
f.render_widget(tabs, area);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for TabComponent {
|
2021-07-31 16:03:39 +00:00
|
|
|
fn commands(&self, _out: &mut Vec<CommandInfo>) {}
|
2021-07-29 10:47:36 +00:00
|
|
|
|
2021-07-18 15:50:39 +00:00
|
|
|
fn event(&mut self, key: Key) -> Result<EventState> {
|
2021-07-31 16:03:39 +00:00
|
|
|
if key == self.key_config.tab_records {
|
|
|
|
self.selected_tab = Tab::Records;
|
|
|
|
return Ok(EventState::Consumed);
|
2021-10-09 07:03:52 +00:00
|
|
|
} else if key == self.key_config.tab_sql_editor {
|
2021-10-07 16:30:13 +00:00
|
|
|
self.selected_tab = Tab::Sql;
|
2021-08-28 08:16:38 +00:00
|
|
|
return Ok(EventState::Consumed);
|
2021-10-09 07:03:52 +00:00
|
|
|
} else if key == self.key_config.tab_properties {
|
|
|
|
self.selected_tab = Tab::Properties;
|
|
|
|
return Ok(EventState::Consumed);
|
2021-07-08 14:08:04 +00:00
|
|
|
}
|
2021-07-31 16:03:39 +00:00
|
|
|
Ok(EventState::NotConsumed)
|
2021-07-08 14:08:04 +00:00
|
|
|
}
|
|
|
|
}
|