This commit is contained in:
fromost
2025-11-10 14:17:48 +08:00
parent fcb9297fdc
commit 6d197bdf78
7 changed files with 132 additions and 97 deletions

View File

@@ -1,19 +1,18 @@
use crate::config::types::ApplicationConfig; use crate::config::types::ApplicationConfig;
use crate::event::{AppEvent, EventHandler}; use crate::event::{AppEvent, EventHandler};
use crate::widgets::views::{AppView, MainView}; use crate::widgets::views::{AppView, MainView};
use crate::widgets::views::View;
use color_eyre::Result; use color_eyre::Result;
use crossterm::event::{Event}; use crossterm::event::{Event};
use rat_cursor::HasScreenCursor;
use ratatui::{DefaultTerminal, Frame}; use ratatui::{DefaultTerminal, Frame};
use std::time::Duration; use std::time::Duration;
use color_eyre::eyre::eyre;
pub(crate) struct App { pub(crate) struct App {
events: EventHandler, events: EventHandler,
state: AppState, state: AppState,
} }
struct AppState { pub struct AppState {
view: Option<AppView>, view: Option<AppView>,
} }
@@ -21,7 +20,7 @@ impl App {
pub async fn create() -> Result<Self> { pub async fn create() -> Result<Self> {
let config = ApplicationConfig::get_config()?; let config = ApplicationConfig::get_config()?;
let state = AppState { let state = AppState {
view: Some(AppView::MainView(MainView::new())), view: Some(AppView::Main(MainView::new())),
}; };
let app = Self { let app = Self {
events: EventHandler::new(Duration::from_millis(config.basic_config.tick_rate)), events: EventHandler::new(Duration::from_millis(config.basic_config.tick_rate)),
@@ -32,15 +31,16 @@ impl App {
pub async fn run(mut self, terminal: &mut DefaultTerminal) -> Result<()> { pub async fn run(mut self, terminal: &mut DefaultTerminal) -> Result<()> {
loop { loop {
terminal.draw(|frame| self.draw(frame))?;
let event = self.events.next().await?; let event = self.events.next().await?;
self.update(event)?; self.update(event)?;
if let Some(view) = self.state.view.as_mut() let Some(current_view) = self.state.view.as_mut() else {
&& let AppView::MainView(main_view) = view continue;
&& !main_view.is_running() };
{ let Some(view) = current_view.get_view() else {
break Ok(()); continue;
} };
if !view.is_running() { break Ok(()) }
terminal.draw(|frame| self.draw(frame))?;
} }
} }
@@ -48,34 +48,37 @@ impl App {
if let AppEvent::Raw(cross_event) = event { if let AppEvent::Raw(cross_event) = event {
self.handle_event(&cross_event)?; self.handle_event(&cross_event)?;
} }
Ok(()) Ok(())
} }
fn handle_event(&mut self, key: &Event) -> Result<()> { fn handle_event(&mut self, key: &Event) -> Result<()> {
if let Some(current_view) = self.state.view.as_mut() { let Some(current_view) = self.state.view.as_mut() else {
match current_view { return Err(eyre!("there is no view"));
AppView::MainView(main_view) => { };
main_view.handle_input(key)? let Some(view) = current_view.get_view() else {
} return Err(eyre!("there is no view"));
} };
} view.handle_input(key)?;
Ok(()) Ok(())
} }
fn draw(&mut self, frame: &mut Frame) { fn draw(&mut self, frame: &mut Frame) {
if let Some(view) = self.state.view.as_mut() { let Some(current_view) = self.state.view.as_mut() else {
match view { return;
AppView::MainView(main_view) => { };
frame.render_stateful_widget( let Some(view) = current_view.get_view() else {
MainView::new(), return;
frame.area(), };
&mut main_view.state, if let Some(pos) = view.screen_cursor() {
); frame.set_cursor_position(pos);
if let Some(pos) = main_view.screen_cursor() { }
frame.set_cursor_position(pos); match current_view {
} AppView::Main(main_view) => {
} frame.render_stateful_widget(
MainView::new(),
frame.area(),
&mut main_view.state,
);
} }
} }
} }

View File

@@ -1,5 +1,5 @@
use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig}; use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig};
use crate::constants::{APP_CONIFG_FILE_PATH, APP_DB_DATA_DIR, CACHE_MAP}; use crate::constants::{APP_CONIFG_FILE_PATH, CACHE_MAP};
use color_eyre::Result; use color_eyre::Result;
use std::path::PathBuf; use std::path::PathBuf;
use language_tags::LanguageTag; use language_tags::LanguageTag;

View File

@@ -1,6 +1,6 @@
pub mod dlsite; pub mod dlsite;
pub use dlsite::*; pub use dlsite::*;
use color_eyre::eyre::eyre; use color_eyre::eyre::eyre;
use crate::constants::APP_CACHE_PATH; use crate::constants::APP_CACHE_PATH;
use color_eyre::Result; use color_eyre::Result;

View File

@@ -43,34 +43,37 @@ impl StatefulWidget for TextArea {
{ {
let input_value = state.input.value().to_string(); let input_value = state.input.value().to_string();
let title = self.title.clone(); let title = self.title.clone();
if matches!(self.style, TextAreaStyle::Block) { match self.style {
let block = Block::default().borders(Borders::ALL).title(title); TextAreaStyle::Block => {
let paragraph = Paragraph::new(Text::from(input_value)).block(block); let block = Block::default().borders(Borders::ALL).title(title);
state.input_area = Some(area); let paragraph = Paragraph::new(Text::from(input_value)).block(block);
paragraph.render(area, buf); state.input_area = Some(area);
} else if matches!(self.style, TextAreaStyle::SingleLine) { paragraph.render(area, buf);
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Max((self.title.len() + 1) as u16), Constraint::Fill(0)])
.split(area);
let label_text = Text::from(self.title);
let label = Paragraph::new(label_text);
let input_text = Span::from(input_value.clone()).fg(Color::White);
let input_line = Line::from(input_text);
let paragraph = Paragraph::new(input_line)
.bg(if state.is_valid { Color::Green } else { Color::Red })
.scroll((0, state.scroll_offset));
if state.input_area.is_none() {
state.input_area = Some(chunks[1]);
}
else if let Some(area) = self.state.input_area && area != chunks[1] {
state.input_area = Some(chunks[1]);
} }
TextAreaStyle::SingleLine => {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Max((self.title.len() + 1) as u16), Constraint::Fill(0)])
.split(area);
let label_text = Text::from(self.title);
let label = Paragraph::new(label_text);
label.render(chunks[0], buf); let input_text = Span::from(input_value.clone()).fg(Color::White);
paragraph.render(chunks[1], buf); let input_line = Line::from(input_text);
let paragraph = Paragraph::new(input_line)
.bg(if state.is_valid { Color::Green } else { Color::Red })
.scroll((0, state.scroll_offset));
if state.input_area.is_none() {
state.input_area = Some(chunks[1]);
}
else if let Some(area) = self.state.input_area && area != chunks[1] {
state.input_area = Some(chunks[1]);
}
label.render(chunks[0], buf);
paragraph.render(chunks[1], buf);
}
} }
} }
} }
@@ -91,9 +94,7 @@ impl TextArea {
pub fn new(title: &str, pub fn new(title: &str,
placeholder_text: &str, placeholder_text: &str,
validate_fn: fn(&str) -> bool, validate_fn: fn(&str) -> bool,
) ) -> Self {
-> Self
{
let func = Arc::new(validate_fn); let func = Arc::new(validate_fn);
Self { Self {
title: title.to_string(), title: title.to_string(),
@@ -129,14 +130,15 @@ impl TextArea {
{ {
let scroll_offset = self.state.scroll_offset; let scroll_offset = self.state.scroll_offset;
let cursor_pos = self.state.input.cursor() as u16; let cursor_pos = self.state.input.cursor() as u16;
if scroll_offset> cursor_pos { if scroll_offset > cursor_pos {
self.state.scroll_offset = cursor_pos; self.state.scroll_offset = cursor_pos;
} else if cursor_pos >= area.width + scroll_offset { } else if cursor_pos >= area.width + scroll_offset {
self.state.scroll_offset = cursor_pos - area.width; self.state.scroll_offset = cursor_pos - area.width;
} else if self.auto_scroll && scroll_offset > 0 && key.code.is_delete() { } else if self.auto_scroll && scroll_offset > 0 && key.code.is_delete() {
self.state.scroll_offset -= 1; self.state.scroll_offset -= 1;
// HACK: with_cursor function requires to be owned so use handle event // HACK: with_cursor function requires to be owned so use handle event
let _ = self.state.input.handle_event(&Event::Key(KeyEvent::new(KeyCode::Left, KeyModifiers::empty()))); let key_event = Event::Key(KeyEvent::new(KeyCode::Left, KeyModifiers::empty()));
let _ = self.state.input.handle_event(&key_event);
} }
} }
@@ -151,6 +153,7 @@ impl TextArea {
} }
pub fn reset_value(&mut self) -> Result<()> { pub fn reset_value(&mut self) -> Result<()> {
self.state.is_valid = false;
Ok(self.state.input.reset()) Ok(self.state.input.reset())
} }
} }

View File

@@ -1,3 +1,5 @@
use ratatui::widgets::StatefulWidget;
pub mod folder; pub mod folder;
pub enum AppPopup { pub enum AppPopup {

View File

@@ -1,6 +1,6 @@
use crate::config::types::ApplicationConfig; use crate::config::types::ApplicationConfig;
use crate::widgets::popups::folder::AddFolderPopup; use crate::widgets::popups::folder::AddFolderPopup;
use crate::widgets::views::View; use crate::widgets::views::{StatefulView, View};
use crossterm::event::KeyCode::Char; use crossterm::event::KeyCode::Char;
use crossterm::event::{Event, KeyCode, KeyEventKind}; use crossterm::event::{Event, KeyCode, KeyEventKind};
use rat_cursor::HasScreenCursor; use rat_cursor::HasScreenCursor;
@@ -11,7 +11,7 @@ use ratatui::widgets::{Block, Borders, Paragraph, StatefulWidget};
use crate::widgets::popups::AppPopup; use crate::widgets::popups::AppPopup;
pub struct MainView { pub struct MainView {
pub state: MainViewState, pub state: MainViewState
} }
pub struct MainViewState { pub struct MainViewState {
@@ -35,53 +35,63 @@ impl MainView {
} }
} }
} }
}
impl MainViewState {
fn quit(&mut self) -> color_eyre::Result<()> { fn quit(&mut self) -> color_eyre::Result<()> {
if self.state.popup.is_none() { if self.popup.is_none() {
self.state.status = Status::Exiting; self.status = Status::Exiting;
ApplicationConfig::get_config()?.save()?; ApplicationConfig::get_config()?.save()?;
} }
Ok(()) Ok(())
} }
fn folder_popup(&mut self) { fn folder_popup(&mut self) {
self.state.popup = Some(AppPopup::AddFolder(AddFolderPopup::new())); self.popup = Some(AppPopup::AddFolder(AddFolderPopup::new()));
self.state.status = Status::Popup; self.status = Status::Popup;
} }
}
impl View for MainView { fn handle_popup(&mut self, event: &Event) -> color_eyre::Result<()> {
fn handle_input(&mut self, event: &Event) -> color_eyre::Result<()> { let Some(current_popup) = self.popup.as_mut() else {
if let Some(current_popup) = self.state.popup.as_mut() { return Ok(());
match current_popup { };
AppPopup::AddFolder(folder_popup) => { match current_popup {
folder_popup.textarea.handle_input(event)?; AppPopup::AddFolder(folder_popup) => {
if let Event::Key(key) = event && folder_popup.textarea.handle_input(event)?;
key.code.is_enter() && if let Event::Key(key) = event &&
let Some(value) = folder_popup.get_folder_value() key.code.is_enter() &&
{ let Some(value) = folder_popup.get_folder_value()
let mut config = ApplicationConfig::get_config()?; {
config.path_config.dlsite_paths.push(value); let mut config = ApplicationConfig::get_config()?;
config.path_config.dlsite_paths.push(value);
folder_popup.textarea.reset_value()?; folder_popup.textarea.reset_value()?;
config.save()?; config.save()?;
}
} }
} }
} }
Ok(())
}
}
impl StatefulView for MainView {
type State = MainViewState;
fn handle_input(state: &mut Self::State, event: &Event) -> color_eyre::Result<()> {
state.handle_popup(event)?;
if let Event::Key(key_event) = event { if let Event::Key(key_event) = event {
if matches!(self.state.status, Status::Popup) && if matches!(state.status, Status::Popup) &&
matches!(key_event.code, KeyCode::Esc) matches!(key_event.code, KeyCode::Esc)
{ {
self.state.status = Status::Running; state.status = Status::Running;
self.state.popup = None; state.popup = None;
} }
if !matches!(self.state.status, Status::Popup) && if !matches!(state.status, Status::Popup) &&
matches!(key_event.kind, KeyEventKind::Press) matches!(key_event.kind, KeyEventKind::Press)
{ {
match key_event.code { match key_event.code {
Char('q') => self.quit()?, Char('q') => state.quit()?,
Char('a') => self.folder_popup(), Char('a') => state.folder_popup(),
_ => {} _ => {}
} }
} }
@@ -89,8 +99,8 @@ impl View for MainView {
Ok(()) Ok(())
} }
fn is_running(&self) -> bool { fn is_running(state: &Self::State) -> bool {
!matches!(self.state.status, Status::Exiting) !matches!(state.status, Status::Exiting)
} }
} }

View File

@@ -1,13 +1,30 @@
mod main_view; mod main_view;
use crossterm::event::{Event}; use crossterm::event::{Event};
use rat_cursor::HasScreenCursor;
pub use main_view::MainView; pub use main_view::MainView;
pub trait View { pub trait View: HasScreenCursor {
fn handle_input(&mut self, event: &Event) -> color_eyre::Result<()>; fn handle_input(&mut self, event: &Event) -> color_eyre::Result<()>;
fn is_running(&self) -> bool; fn is_running(&self) -> bool;
} }
pub enum AppView { pub trait StatefulView: HasScreenCursor {
MainView(MainView), type State;
fn handle_input(state: &mut Self::State, event: &Event) -> color_eyre::Result<()>;
fn is_running(state: &Self::State) -> bool;
}
pub enum AppView {
Main(MainView),
}
impl AppView {
pub fn get_view(&mut self) -> Option<&mut dyn View> {
match self {
_ => None
}
}
//TODO: Implement Stateful View
} }