Compare commits

...

8 Commits

Author SHA1 Message Date
filipriec
ccd76eabdd components are redesigned 2025-03-22 15:45:55 +01:00
filipriec
fabe1e0ca7 changing components infrastructure 2025-03-22 15:36:49 +01:00
filipriec
9bf1d065d5 restored functionality 2025-03-22 15:11:08 +01:00
filipriec
62aed812b6 displaying admin panel properly well 2025-03-22 13:18:50 +01:00
filipriec
a58e976227 admin panel is working, needs to clear the terminal tho from the other things 2025-03-22 10:33:09 +01:00
filipriec
c198297a5c admin panel compiled 2025-03-22 10:27:21 +01:00
filipriec
c592dfc7f5 intro page working properly well 2025-03-21 23:07:10 +01:00
filipriec
1b0aaa55c9 switched stuff in the state.rs 2025-03-21 22:46:50 +01:00
15 changed files with 290 additions and 67 deletions

View File

@@ -0,0 +1,4 @@
// src/components/admin.rs
pub mod admin_panel;
pub use admin_panel::*;

View File

@@ -0,0 +1,44 @@
// src/components/admin/admin_panel.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 AdminPanelState;
impl AdminPanelState {
pub fn new() -> Self {
Self
}
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);
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // Title
Constraint::Min(1), // Content
])
.split(inner_area);
let title = Line::from(Span::styled("Admin Panel", Style::default().fg(theme.highlight)));
let title_para = Paragraph::new(title)
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[0]);
let content = Paragraph::new("Admin panel content goes here");
f.render_widget(content, chunks[1]);
}
}

View File

@@ -0,0 +1,8 @@
// src/components/common.rs
pub mod command_line;
pub mod status_line;
pub mod background;
pub use command_line::*;
pub use status_line::*;
pub use background::*;

View File

@@ -1,14 +1,8 @@
// src/components/handlers.rs
pub mod form;
pub mod command_line;
pub mod status_line;
pub mod canvas;
pub mod sidebar;
pub mod background;
pub use command_line::render_command_line;
pub use form::*;
pub use status_line::render_status_line;
pub use canvas::*;
pub use sidebar::*;
pub use background::*;

View File

@@ -0,0 +1,4 @@
// src/components/intro.rs
pub mod intro;
pub use intro::*;

View File

@@ -0,0 +1,110 @@
// src/components/handlers/intro.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
prelude::Margin,
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(Margin {
horizontal: 1,
vertical: 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,5 +1,10 @@
// src/components/mod.rs
pub mod models;
pub mod handlers;
pub mod intro;
pub mod admin;
pub mod common;
pub use handlers::*;
pub use intro::*;
pub use admin::*;
pub use common::*;

View File

@@ -1,6 +1,5 @@
// src/modes/handlers/event.rs
use crossterm::event::Event;
use crossterm::event::{Event, KeyCode};
use crossterm::cursor::SetCursorStyle;
use crate::tui::terminal::{
core::TerminalCore,
@@ -48,21 +47,42 @@ impl EventHandler {
app_state: &mut crate::state::state::AppState,
total_count: u64,
current_position: &mut u64,
intro_state: &mut crate::components::intro::intro::IntroState,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if app_state.ui.show_intro {
if let Event::Key(key) = event {
match key.code {
KeyCode::Left => intro_state.previous_option(),
KeyCode::Right => intro_state.next_option(),
KeyCode::Enter => {
if intro_state.selected_option == 0 {
app_state.ui.show_intro = false;
} else {
app_state.ui.show_intro = false;
app_state.ui.show_admin = true;
}
return Ok((false, String::new()));
}
_ => {}
}
}
return Ok((false, String::new()));
}
if let Event::Key(key) = event {
let key_code = key.code;
let modifiers = key.modifiers;
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
key_code,
modifiers,
) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
key_code,
modifiers,
) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
@@ -74,7 +94,7 @@ impl EventHandler {
let message = common::save(
form_state,
grpc_client,
&mut app_state.is_saved,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
@@ -109,7 +129,7 @@ impl EventHandler {
&mut self.command_input,
&mut self.command_message,
grpc_client,
&mut app_state.is_saved,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
@@ -146,7 +166,7 @@ impl EventHandler {
form_state,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.is_saved,
&mut app_state.ui.is_saved,
current_position,
total_count,
grpc_client,

View File

@@ -1,19 +1,21 @@
// src/client/ui/handlers/state.rs
// src/state/state.rs
use std::env;
use common::proto::multieko2::table_definition::ProfileTreeResponse;
pub struct UiState {
pub show_sidebar: bool,
pub profile_tree: ProfileTreeResponse,
pub is_saved: bool,
pub show_intro: bool,
pub show_admin: bool,
}
pub struct AppState {
// Core editor state
pub is_saved: bool,
pub current_dir: String,
pub total_count: u64,
pub current_position: u64,
pub profile_tree: ProfileTreeResponse,
// UI preferences
pub ui: UiState,
@@ -25,10 +27,10 @@ impl AppState {
.to_string_lossy()
.to_string();
Ok(AppState {
is_saved: false,
current_dir,
total_count: 0,
current_position: 0,
profile_tree: ProfileTreeResponse::default(),
ui: UiState::default(),
})
}
@@ -47,7 +49,9 @@ impl Default for UiState {
fn default() -> Self {
Self {
show_sidebar: true,
profile_tree: ProfileTreeResponse::default(),
is_saved: false,
show_intro: true,
show_admin: false,
}
}
}

View File

@@ -1,15 +1,17 @@
// src/ui/handlers/render.rs
use crate::components::{
render_background,
render_command_line,
render_background,
render_command_line,
render_status_line,
handlers::sidebar::{self, calculate_sidebar_layout}
handlers::{sidebar::{self, calculate_sidebar_layout}, admin_panel::AdminPanelState, form::render_form},
intro::{intro},
};
use crate::config::colors::Theme;
use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::Frame;
use super::form::FormState;
use crate::state::state::UiState;
use crate::state::state::AppState;
pub fn render_ui(
f: &mut Frame,
@@ -22,54 +24,76 @@ pub fn render_ui(
command_input: &str,
command_mode: bool,
command_message: &str,
ui_state: &UiState,
app_state: &AppState,
intro_state: &intro::IntroState,
admin_panel_state: &AdminPanelState,
) {
render_background(f, f.area(), theme);
let root = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Min(10),
Constraint::Min(1),
Constraint::Length(1),
Constraint::Length(1),
])
.split(f.area());
let main_content_area = root[0];
let (sidebar_area, form_area) = calculate_sidebar_layout(ui_state.show_sidebar, main_content_area);
let available_width = form_area.width;
let form_constraint = if available_width >= 80 {
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0),
Constraint::Length(80),
Constraint::Min(0),
])
.split(main_content_area)[1]
if app_state.ui.show_intro {
intro_state.render(f, main_content_area, theme);
} else if app_state.ui.show_admin {
admin_panel_state.render(f, main_content_area, theme);
} else {
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0),
Constraint::Length(80.min(available_width)),
Constraint::Min(0),
])
.split(form_area)[1]
};
let (sidebar_area, form_area) = calculate_sidebar_layout(
app_state.ui.show_sidebar,
main_content_area
);
form_state.render(
f,
form_constraint,
theme,
is_edit_mode,
total_count,
current_position,
);
if let Some(sidebar_rect) = sidebar_area {
sidebar::render_sidebar(f, sidebar_rect, theme, &app_state.profile_tree);
}
if let Some(sidebar_rect) = sidebar_area {
sidebar::render_sidebar(f, sidebar_rect, theme, &ui_state.profile_tree);
// This change makes the form stay stationary when toggling sidebar
let available_width = form_area.width;
let form_constraint = if available_width >= 80 {
// Use main_content_area for centering when enough space
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0),
Constraint::Length(80),
Constraint::Min(0),
])
.split(main_content_area)[1]
} else {
// Use form_area (post sidebar) when limited space
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0),
Constraint::Length(80.min(available_width)),
Constraint::Min(0),
])
.split(form_area)[1]
};
// Convert fields to &[&str] and values to &[&String]
let fields: Vec<&str> = form_state.fields.iter().map(|s| s.as_str()).collect();
let values: Vec<&String> = form_state.values.iter().collect();
render_form(
f,
form_constraint,
form_state,
&fields,
&form_state.current_field,
&values,
theme,
is_edit_mode,
total_count,
current_position,
);
}
render_status_line(f, root[1], current_dir, theme, is_edit_mode);

View File

@@ -9,7 +9,8 @@ use crate::config::config::Config;
use crate::ui::handlers::{form::FormState, render::render_ui};
use crate::modes::handlers::event::EventHandler;
use crate::state::state::AppState;
use crate::components::handlers::{admin_panel::AdminPanelState};
use crate::components::intro::{intro::IntroState};
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::load()?;
@@ -17,6 +18,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let mut grpc_client = GrpcClient::new().await?;
let mut command_handler = CommandHandler::new();
let theme = Theme::from_str(&config.colors.theme);
let mut intro_state = IntroState::new();
let admin_panel_state = AdminPanelState::new();
// Fetch table structure at startup (one-time)
// TODO: Later, consider implementing a live update for table structure changes.
@@ -38,7 +41,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let mut event_handler = EventHandler::new();
let event_reader = EventReader::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
let total_count = grpc_client.get_adresar_count().await?;
@@ -62,7 +65,9 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&event_handler.command_input,
event_handler.command_mode,
&event_handler.command_message,
&app_state.ui,
&app_state,
&intro_state,
&admin_panel_state,
);
})?;
@@ -80,6 +85,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&mut app_state,
total_count,
&mut current_position,
&mut intro_state,
).await?;
app_state.current_position = current_position;