Skip to content

Commit b492a8a

Browse files
committed
refactor: reorganize modules into screen/ and widget/ directories
1 parent f1d44f7 commit b492a8a

13 files changed

Lines changed: 284 additions & 259 deletions

File tree

src/app.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use ratatui::{
99
};
1010

1111
use crate::{
12-
exit_handler::ExitHandler,
13-
footer::Footer,
14-
menu_screen::MenuScreen,
15-
screen::{Screen, ScreenCommand},
12+
exit::ExitHandler,
13+
screen::{Screen, ScreenCommand, home::MenuScreen},
1614
theme::Theme,
15+
widget::footer::Footer,
1716
};
1817

1918
const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -54,10 +53,7 @@ impl App {
5453
terminal.draw(|frame| {
5554
let area = frame.area();
5655

57-
frame.render_widget(
58-
Block::new().style(Style::default().bg(self.theme.bg)),
59-
area,
60-
);
56+
frame.render_widget(Block::new().style(Style::default().bg(self.theme.bg)), area);
6157

6258
let [body, footer_area] =
6359
Layout::vertical([Constraint::Fill(1), Constraint::Length(2)]).areas(area);
File renamed without changes.

src/main.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
//! 按两次 Ctrl+C 退出。
44
55
mod app;
6-
mod exit_handler;
7-
mod footer;
8-
mod input;
9-
mod logo;
10-
mod menu_screen;
6+
mod exit;
117
mod screen;
12-
mod tabs;
138
mod theme;
9+
mod widget;
1410

1511
fn main() -> color_eyre::Result<()> {
1612
color_eyre::install()?;

src/menu_screen.rs

Lines changed: 0 additions & 138 deletions
This file was deleted.

src/screen.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/screen/home.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use crossterm::event::{KeyCode, KeyEvent};
2+
use ratatui::{
3+
Frame,
4+
layout::{Constraint, Layout, Rect},
5+
};
6+
7+
use crate::{
8+
theme::Theme,
9+
widget::{
10+
logo::{LOGO_HEIGHT, Logo},
11+
tabs::Tabs,
12+
},
13+
};
14+
15+
use super::{Screen, ScreenCommand};
16+
17+
pub struct MenuScreen {
18+
logo: Logo,
19+
tabs: Tabs,
20+
}
21+
22+
impl MenuScreen {
23+
pub fn new(theme: Theme) -> Self {
24+
Self {
25+
logo: Logo::new(theme),
26+
tabs: Tabs::new(theme),
27+
}
28+
}
29+
}
30+
31+
impl Screen for MenuScreen {
32+
fn render(&self, frame: &mut Frame, area: Rect) {
33+
let [_, logo_area, _, body_area, _] = Layout::vertical([
34+
Constraint::Fill(1),
35+
Constraint::Length(LOGO_HEIGHT),
36+
Constraint::Length(3),
37+
Constraint::Fill(3),
38+
Constraint::Fill(1),
39+
])
40+
.areas(area);
41+
42+
self.logo.render(frame, logo_area);
43+
self.tabs.render(frame, body_area);
44+
}
45+
46+
fn handle_key(&mut self, key: KeyEvent) -> Option<ScreenCommand> {
47+
match key.code {
48+
KeyCode::Enter => Some(ScreenCommand::Stay),
49+
_ if self.tabs.handle_key(key) => Some(ScreenCommand::Stay),
50+
_ => Some(ScreenCommand::Stay),
51+
}
52+
}
53+
}

src/screen/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub mod home;
2+
3+
use crossterm::event::KeyEvent;
4+
use ratatui::{Frame, layout::Rect};
5+
6+
#[allow(dead_code)]
7+
pub enum ScreenCommand {
8+
Stay,
9+
Navigate(Box<dyn Screen>),
10+
}
11+
12+
pub trait Screen {
13+
fn render(&self, frame: &mut Frame, area: Rect);
14+
15+
fn handle_key(&mut self, key: KeyEvent) -> Option<ScreenCommand>;
16+
}

src/tabs.rs

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/footer.rs renamed to src/widget/footer.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,33 @@ impl Footer {
4747
Span::styled(" 切换标签", dim_style),
4848
]);
4949

50+
// Cwd
5051
frame.render_widget(
5152
Paragraph::new(Line::from(self.current_dir.as_str()).style(dim_style)),
5253
path_area,
5354
);
55+
56+
// Tip
5457
frame.render_widget(Paragraph::new(hint).alignment(Alignment::Center), hint_area);
5558

56-
if let Some(text) = exit_hint {
57-
let exit_width = text.width() as u16 + 2;
58-
let [exit_hint_area, version_text_area] =
59-
Layout::horizontal([Constraint::Length(exit_width), Constraint::Fill(1)])
60-
.areas(version_area);
59+
// Exit hint
60+
let exit_width = exit_hint.map_or(0, |t| t.width() as u16 + 2);
61+
let [exit_hint_area, version_text_area] =
62+
Layout::horizontal([Constraint::Length(exit_width), Constraint::Fill(1)])
63+
.areas(version_area);
6164

65+
if let Some(text) = exit_hint {
6266
frame.render_widget(
6367
Paragraph::new(Line::from(text)).yellow().bold(),
6468
exit_hint_area,
6569
);
66-
frame.render_widget(
67-
Paragraph::new(Line::from(self.version.as_str()).style(dim_style))
68-
.alignment(Alignment::Right),
69-
version_text_area,
70-
);
71-
} else {
72-
frame.render_widget(
73-
Paragraph::new(Line::from(self.version.as_str()).style(dim_style))
74-
.alignment(Alignment::Right),
75-
version_area,
76-
);
7770
}
71+
72+
// Version
73+
frame.render_widget(
74+
Paragraph::new(Line::from(self.version.as_str()).style(dim_style))
75+
.alignment(Alignment::Right),
76+
version_text_area,
77+
);
7878
}
7979
}

0 commit comments

Comments
 (0)