complete redesign
This commit is contained in:
10
client/src/components1/handlers.rs
Normal file
10
client/src/components1/handlers.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
// src/client/components/mod.rs
|
||||
pub mod form;
|
||||
pub mod preview_card;
|
||||
pub mod command_line;
|
||||
pub mod status_line;
|
||||
|
||||
pub use command_line::render_command_line;
|
||||
pub use form::render_form;
|
||||
pub use preview_card::render_preview_card;
|
||||
pub use status_line::render_status_line;
|
||||
35
client/src/components1/handlers/command_line.rs
Normal file
35
client/src/components1/handlers/command_line.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
// src/client/components/command_line.rs
|
||||
use ratatui::{
|
||||
widgets::{Block, Paragraph},
|
||||
style::Style,
|
||||
layout::Rect,
|
||||
Frame,
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
|
||||
pub fn render_command_line(f: &mut Frame, area: Rect, input: &str, active: bool, theme: &Theme, message: &str) {
|
||||
let prompt = if active {
|
||||
":"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
// Combine the prompt, input, and message
|
||||
let display_text = if message.is_empty() {
|
||||
format!("{}{}", prompt, input)
|
||||
} else {
|
||||
format!("{}{} | {}", prompt, input, message)
|
||||
};
|
||||
|
||||
let style = if active {
|
||||
Style::default().fg(theme.accent)
|
||||
} else {
|
||||
Style::default().fg(theme.fg)
|
||||
};
|
||||
|
||||
let paragraph = Paragraph::new(display_text)
|
||||
.block(Block::default().style(Style::default().bg(theme.bg)))
|
||||
.style(style);
|
||||
|
||||
f.render_widget(paragraph, area);
|
||||
}
|
||||
142
client/src/components1/handlers/form.rs
Normal file
142
client/src/components1/handlers/form.rs
Normal file
@@ -0,0 +1,142 @@
|
||||
// src/client/components1/handlers/form.rs
|
||||
use ratatui::{
|
||||
widgets::{Paragraph, Block, Borders},
|
||||
layout::{Layout, Constraint, Direction, Rect, Margin, Alignment},
|
||||
style::Style,
|
||||
text::{Line, Span},
|
||||
Frame,
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
use crate::client::ui::form::FormState;
|
||||
|
||||
pub fn render_form(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
form_state: &FormState,
|
||||
fields: &[&str],
|
||||
current_field: &usize,
|
||||
inputs: &[&String],
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
// Create Adresar card
|
||||
let adresar_card = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(theme.border))
|
||||
.title(" Adresar ")
|
||||
.style(Style::default().bg(theme.bg).fg(theme.fg));
|
||||
|
||||
f.render_widget(adresar_card, area);
|
||||
|
||||
// Define the inner area for the form (inside the card)
|
||||
let inner_area = area.inner(Margin {
|
||||
horizontal: 1,
|
||||
vertical: 1,
|
||||
});
|
||||
|
||||
// Create a vertical layout for the entire form content
|
||||
let main_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(1), // For count and position
|
||||
Constraint::Min(1), // For form fields
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Render the count and position at the very top
|
||||
let count_position_text = format!("Total: {} | Current Position: {}", total_count, current_position);
|
||||
let count_position_paragraph = Paragraph::new(count_position_text)
|
||||
.style(Style::default().fg(theme.fg))
|
||||
.alignment(Alignment::Left);
|
||||
f.render_widget(count_position_paragraph, main_layout[0]);
|
||||
|
||||
// Split the remaining space into label and input columns
|
||||
let columns = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(main_layout[1]);
|
||||
|
||||
// Create compact input container
|
||||
let input_container = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if is_edit_mode {
|
||||
if form_state.has_unsaved_changes {
|
||||
Style::default().fg(theme.warning) // Red color
|
||||
} else {
|
||||
Style::default().fg(theme.accent) // Blue color
|
||||
}
|
||||
} else {
|
||||
Style::default().fg(theme.secondary) // Yellow color
|
||||
})
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
// Place the input container at the top
|
||||
let input_container_area = Rect {
|
||||
x: columns[1].x,
|
||||
y: columns[1].y,
|
||||
width: columns[1].width,
|
||||
height: fields.len() as u16 + 2, // +2 for borders
|
||||
};
|
||||
|
||||
f.render_widget(&input_container, input_container_area);
|
||||
|
||||
// Input area inside borders
|
||||
let input_area = input_container.inner(input_container_area);
|
||||
|
||||
// Split the remaining area for the form inputs
|
||||
let input_rows = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(vec![Constraint::Length(1); fields.len()])
|
||||
.split(input_area);
|
||||
|
||||
// Render labels close to the border
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
let label = Paragraph::new(Line::from(Span::styled(
|
||||
format!("{}:", field),
|
||||
Style::default().fg(theme.fg),
|
||||
)));
|
||||
f.render_widget(label, Rect {
|
||||
x: columns[0].x,
|
||||
y: input_container_area.y + 1 + i as u16, // Align with input rows
|
||||
width: columns[0].width,
|
||||
height: 1,
|
||||
});
|
||||
}
|
||||
|
||||
// Render inputs with left-aligned text and free cursor movement
|
||||
for (i, input) in inputs.iter().enumerate() {
|
||||
let is_active = i == *current_field;
|
||||
|
||||
let input_display = Paragraph::new(input.as_str())
|
||||
.alignment(Alignment::Left)
|
||||
.style(if is_active {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.fg)
|
||||
});
|
||||
|
||||
f.render_widget(input_display, input_rows[i]);
|
||||
|
||||
// Position cursor at the correct position in the active field
|
||||
if is_active && is_edit_mode { // Move cursor logic inside the loop
|
||||
let cursor_x = input_rows[i].x + input.len() as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y)); // Updated to set_cursor_position
|
||||
}
|
||||
if is_active {
|
||||
if is_edit_mode {
|
||||
// Edit mode: cursor at current_cursor_pos instead of end
|
||||
let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y)); // Updated to set_cursor_position
|
||||
} else {
|
||||
// Read-only mode: cursor at current_cursor_pos
|
||||
let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y)); // Updated to set_cursor_position
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
client/src/components1/handlers/preview_card.rs
Normal file
33
client/src/components1/handlers/preview_card.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
// src/client/components/preview_card.rs
|
||||
use ratatui::{
|
||||
widgets::{Block, Borders, List, ListItem},
|
||||
layout::Rect,
|
||||
style::Style,
|
||||
text::Text,
|
||||
Frame,
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
|
||||
pub fn render_preview_card(f: &mut Frame, area: Rect, fields: &[&String], theme: &Theme) {
|
||||
let card = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(theme.border))
|
||||
.title(" Preview Card ")
|
||||
.style(Style::default().bg(theme.bg).fg(theme.fg));
|
||||
|
||||
let items = vec![
|
||||
ListItem::new(Text::from(format!("Firma: {}", fields[0]))),
|
||||
ListItem::new(Text::from(format!("Ulica: {}", fields[1]))),
|
||||
ListItem::new(Text::from(format!("Mesto: {}", fields[2]))),
|
||||
ListItem::new(Text::from(format!("PSC: {}", fields[3]))),
|
||||
ListItem::new(Text::from(format!("ICO: {}", fields[4]))),
|
||||
ListItem::new(Text::from(format!("Kontakt: {}", fields[5]))),
|
||||
ListItem::new(Text::from(format!("Telefon: {}", fields[6]))),
|
||||
];
|
||||
|
||||
let list = List::new(items)
|
||||
.block(card)
|
||||
.style(Style::default().bg(theme.bg).fg(theme.fg));
|
||||
|
||||
f.render_widget(list, area);
|
||||
}
|
||||
80
client/src/components1/handlers/status_line.rs
Normal file
80
client/src/components1/handlers/status_line.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
// src/client/components1/handlers/status_line.rs
|
||||
use ratatui::{
|
||||
widgets::Paragraph,
|
||||
style::Style,
|
||||
layout::Rect,
|
||||
Frame,
|
||||
text::{Line, Span},
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn render_status_line(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
current_dir: &str,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
) {
|
||||
// Program name and version
|
||||
let program_info = format!("multieko2 v{}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let mode_text = if is_edit_mode {
|
||||
"[EDIT]"
|
||||
} else {
|
||||
"[READ-ONLY]"
|
||||
};
|
||||
|
||||
// Shorten the current directory path
|
||||
let home_dir = dirs::home_dir().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let display_dir = if current_dir.starts_with(&home_dir) {
|
||||
current_dir.replacen(&home_dir, "~", 1)
|
||||
} else {
|
||||
current_dir.to_string()
|
||||
};
|
||||
|
||||
// Create the full status line text
|
||||
let full_text = format!("{} | {} | {}", mode_text, display_dir, program_info);
|
||||
|
||||
// Check if the full text fits in the available width
|
||||
let available_width = area.width as usize;
|
||||
let mut display_text = if full_text.len() <= available_width {
|
||||
// If it fits, use the full text
|
||||
full_text
|
||||
} else {
|
||||
// If it doesn't fit, prioritize mode and program info, and show only the directory name
|
||||
let dir_name = Path::new(current_dir)
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.unwrap_or(current_dir);
|
||||
format!("{} | {} | {}", mode_text, dir_name, program_info)
|
||||
};
|
||||
|
||||
// If even the shortened version overflows, truncate it
|
||||
if display_text.len() > available_width {
|
||||
display_text = display_text.chars().take(available_width).collect();
|
||||
}
|
||||
|
||||
// Create the status line text using Line and Span
|
||||
let status_line = Line::from(vec![
|
||||
Span::styled(mode_text, Style::default().fg(theme.accent)),
|
||||
Span::styled(" | ", Style::default().fg(theme.border)),
|
||||
Span::styled(
|
||||
display_text.split(" | ").nth(1).unwrap_or(""), // Directory part
|
||||
Style::default().fg(theme.fg),
|
||||
),
|
||||
Span::styled(" | ", Style::default().fg(theme.border)),
|
||||
Span::styled(
|
||||
program_info,
|
||||
Style::default()
|
||||
.fg(theme.secondary)
|
||||
.add_modifier(ratatui::style::Modifier::BOLD),
|
||||
),
|
||||
]);
|
||||
|
||||
// Render the status line
|
||||
let paragraph = Paragraph::new(status_line)
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
f.render_widget(paragraph, area);
|
||||
}
|
||||
5
client/src/components1/mod.rs
Normal file
5
client/src/components1/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// src/client/components1/mod.rs
|
||||
pub mod models;
|
||||
pub mod handlers;
|
||||
|
||||
pub use handlers::*;
|
||||
0
client/src/components1/models.rs
Normal file
0
client/src/components1/models.rs
Normal file
Reference in New Issue
Block a user