switched stuff in the state.rs

This commit is contained in:
filipriec
2025-03-21 22:46:50 +01:00
parent 44c5963c71
commit 1b0aaa55c9
5 changed files with 121 additions and 14 deletions

View File

@@ -0,0 +1,106 @@
// src/components/handlers/intro.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
use crate::config::colors::Theme;
pub struct IntroState {
pub selected_option: usize,
}
impl IntroState {
pub fn new() -> Self {
Self { selected_option: 0 }
}
pub fn render(&self, f: &mut Frame, area: Rect, theme: &Theme) {
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.accent))
.style(Style::default().bg(theme.bg));
let inner_area = block.inner(area);
f.render_widget(block, area);
// Center layout
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(35),
Constraint::Length(5),
Constraint::Percentage(35),
])
.split(inner_area);
// Title
let title = Line::from(vec![
Span::styled("multieko2", Style::default().fg(theme.highlight)),
Span::styled(" v", Style::default().fg(theme.fg)),
Span::styled(env!("CARGO_PKG_VERSION"), Style::default().fg(theme.secondary)),
]);
let title_para = Paragraph::new(title)
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[1]);
// Buttons
let button_area = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(chunks[1].inner(chunks[1]));
self.render_button(
f,
button_area[0],
"Continue",
self.selected_option == 0,
theme,
);
self.render_button(
f,
button_area[1],
"Admin",
self.selected_option == 1,
theme,
);
}
fn render_button(&self, f: &mut Frame, area: Rect, text: &str, selected: bool, theme: &Theme) {
let button_style = if selected {
Style::default()
.fg(theme.highlight)
.bg(theme.bg)
.add_modifier(ratatui::style::Modifier::BOLD)
} else {
Style::default().fg(theme.fg).bg(theme.bg)
};
let button = Paragraph::new(text)
.style(button_style)
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Double)
.border_style(if selected {
Style::default().fg(theme.accent)
} else {
Style::default().fg(theme.border)
}),
);
f.render_widget(button, area);
}
pub fn next_option(&mut self) {
self.selected_option = (self.selected_option + 1) % 2;
}
pub fn previous_option(&mut self) {
self.selected_option = if self.selected_option == 0 { 1 } else { 0 };
}
}

View File

@@ -1,4 +1,4 @@
// src/modes/handlers/event.rs //
use crossterm::event::Event; use crossterm::event::Event;
use crossterm::cursor::SetCursorStyle; use crossterm::cursor::SetCursorStyle;
@@ -74,7 +74,7 @@ impl EventHandler {
let message = common::save( let message = common::save(
form_state, form_state,
grpc_client, grpc_client,
&mut app_state.is_saved, &mut app_state.ui.is_saved,
current_position, current_position,
total_count, total_count,
).await?; ).await?;
@@ -109,7 +109,7 @@ impl EventHandler {
&mut self.command_input, &mut self.command_input,
&mut self.command_message, &mut self.command_message,
grpc_client, grpc_client,
&mut app_state.is_saved, &mut app_state.ui.is_saved,
current_position, current_position,
total_count, total_count,
).await?; ).await?;
@@ -146,7 +146,7 @@ impl EventHandler {
form_state, form_state,
&mut self.ideal_cursor_column, &mut self.ideal_cursor_column,
&mut self.command_message, &mut self.command_message,
&mut app_state.is_saved, &mut app_state.ui.is_saved,
current_position, current_position,
total_count, total_count,
grpc_client, grpc_client,

View File

@@ -5,15 +5,16 @@ use common::proto::multieko2::table_definition::ProfileTreeResponse;
pub struct UiState { pub struct UiState {
pub show_sidebar: bool, pub show_sidebar: bool,
pub profile_tree: ProfileTreeResponse, pub is_saved: bool,
// pub show_intro: bool,
} }
pub struct AppState { pub struct AppState {
// Core editor state // Core editor state
pub is_saved: bool,
pub current_dir: String, pub current_dir: String,
pub total_count: u64, pub total_count: u64,
pub current_position: u64, pub current_position: u64,
pub profile_tree: ProfileTreeResponse,
// UI preferences // UI preferences
pub ui: UiState, pub ui: UiState,
@@ -25,10 +26,10 @@ impl AppState {
.to_string_lossy() .to_string_lossy()
.to_string(); .to_string();
Ok(AppState { Ok(AppState {
is_saved: false,
current_dir, current_dir,
total_count: 0, total_count: 0,
current_position: 0, current_position: 0,
profile_tree: ProfileTreeResponse::default(),
ui: UiState::default(), ui: UiState::default(),
}) })
} }
@@ -47,7 +48,7 @@ impl Default for UiState {
fn default() -> Self { fn default() -> Self {
Self { Self {
show_sidebar: true, show_sidebar: true,
profile_tree: ProfileTreeResponse::default(), is_saved: false,
} }
} }
} }

View File

@@ -9,7 +9,7 @@ use crate::config::colors::Theme;
use ratatui::layout::{Constraint, Direction, Layout}; use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::Frame; use ratatui::Frame;
use super::form::FormState; use super::form::FormState;
use crate::state::state::UiState; use crate::state::state::AppState;
pub fn render_ui( pub fn render_ui(
f: &mut Frame, f: &mut Frame,
@@ -22,7 +22,7 @@ pub fn render_ui(
command_input: &str, command_input: &str,
command_mode: bool, command_mode: bool,
command_message: &str, command_message: &str,
ui_state: &UiState, app_state: &AppState,
) { ) {
render_background(f, f.area(), theme); render_background(f, f.area(), theme);
@@ -36,7 +36,7 @@ pub fn render_ui(
.split(f.area()); .split(f.area());
let main_content_area = root[0]; let main_content_area = root[0];
let (sidebar_area, form_area) = calculate_sidebar_layout(ui_state.show_sidebar, main_content_area); let (sidebar_area, form_area) = calculate_sidebar_layout(app_state.ui.show_sidebar, main_content_area);
let available_width = form_area.width; let available_width = form_area.width;
let form_constraint = if available_width >= 80 { let form_constraint = if available_width >= 80 {
@@ -69,7 +69,7 @@ pub fn render_ui(
); );
if let Some(sidebar_rect) = sidebar_area { if let Some(sidebar_rect) = sidebar_area {
sidebar::render_sidebar(f, sidebar_rect, theme, &ui_state.profile_tree); sidebar::render_sidebar(f, sidebar_rect, theme, &app_state.profile_tree);
} }
render_status_line(f, root[1], current_dir, theme, is_edit_mode); render_status_line(f, root[1], current_dir, theme, is_edit_mode);

View File

@@ -38,7 +38,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let mut event_handler = EventHandler::new(); let mut event_handler = EventHandler::new();
let event_reader = EventReader::new(); let event_reader = EventReader::new();
let mut app_state = AppState::new()?; let mut app_state = AppState::new()?;
app_state.ui.profile_tree = profile_tree; app_state.profile_tree = profile_tree;
// Fetch the total count of Adresar entries // Fetch the total count of Adresar entries
let total_count = grpc_client.get_adresar_count().await?; let total_count = grpc_client.get_adresar_count().await?;
@@ -62,7 +62,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&event_handler.command_input, &event_handler.command_input,
event_handler.command_mode, event_handler.command_mode,
&event_handler.command_message, &event_handler.command_message,
&app_state.ui, &app_state,
); );
})?; })?;