This commit is contained in:
2024-10-30 21:18:02 +08:00
parent aea8ffc9da
commit 9bb6854125
15 changed files with 216 additions and 24 deletions

View File

@@ -6,10 +6,10 @@ use ratatui::widgets::Widget;
use super::ActiveState;
pub mod block;
pub mod button;
pub mod help;
pub mod input;
pub mod list;
pub mod button;
pub trait Component {
type EventResult;

View File

@@ -1,6 +1,9 @@
use std::marker::PhantomData;
use ratatui::{style::{Style, Stylize}, widgets::{Block, Borders}};
use ratatui::{
style::{Style, Stylize},
widgets::{Block, Borders},
};
#[derive(Default)]
pub struct BlockComponent<'a> {
@@ -13,7 +16,11 @@ impl<'a> BlockComponent<'a> {
pub fn widget(&self) -> Block<'a> {
Block::default()
.borders(Borders::ALL)
.border_style(if self.active {Style::new().blue()} else {Style::new().white()})
.border_style(if self.active {
Style::new().blue()
} else {
Style::new().white()
})
.title(self.title.clone())
}

View File

@@ -9,7 +9,7 @@ use super::{block::BlockComponent, Component};
#[derive(Default)]
pub struct HelpComponent {
active_state: ActiveState
active_state: ActiveState,
}
impl Component for HelpComponent {

View File

@@ -21,7 +21,7 @@ pub struct InputComponent {
name: String,
value: String,
cursor: usize,
active_state: ActiveState
active_state: ActiveState,
}
impl InputComponent {
@@ -39,7 +39,10 @@ impl Component for InputComponent {
type EventResult = InputEvent;
fn widget(&self) -> impl Widget {
let block = BlockComponent::default().title(self.name.clone()).active(self.active_state.is_active()).widget();
let block = BlockComponent::default()
.title(self.name.clone())
.active(self.active_state.is_active())
.widget();
let input = Paragraph::new(self.value.clone()).block(block);
input
}

View File

@@ -37,8 +37,8 @@ impl Default for Home {
help: HelpComponent::default(),
setting: SettingView::default(),
};
home.table.active_state().set_active(true);
// home.setting.active_state().set_active(true);
// home.table.active_state().set_active(true);
home.setting.active_state().set_active(true);
home
}
}

View File

@@ -25,32 +25,32 @@ impl SettingView {}
impl Default for SettingView {
fn default() -> Self {
SettingView {
let mut seting = SettingView {
area: Rect::new(0, 0, 10, 5),
menu: ListComponent::default().selectable(),
activate_state: Default::default(),
}
};
seting.menu.active_state().set_active(true);
seting
}
}
impl View for SettingView {
fn draw(&self, frame: &mut ratatui::Frame) {
let area = center_rect(frame.area(), self.area.width, self.area.height);
if self.activate_state.is_active() {
let layout = Layout::default()
.constraints([Constraint::default()])
.split(area);
frame.render_widget(Clear, area);
frame.render_widget(self.menu.widget(), layout[0]);
} else {
frame.render_widget(Clear, area);
if !self.activate_state.is_active() {
return;
}
let area = center_rect(frame.area(), self.area.width, self.area.height);
let layout = Layout::default()
.constraints([Constraint::default()])
.split(area);
frame.render_widget(self.menu.widget(), layout[0]);
}
fn handle_event(&mut self, event: &crossterm::event::Event) -> io::Result<()> {
match self.menu.event_handler(event) {
Ok(ListEvent::Select(idx)) => {
// self.menu.active_state().set_active(false);
self.activate_state.set_active(false);
println!("{}", idx);
}
_ => {}