133 lines
3.8 KiB
Rust
133 lines
3.8 KiB
Rust
// src/components/auth/login.rs
|
|
use crate::{
|
|
config::colors::themes::Theme,
|
|
state::pages::auth::AuthState,
|
|
};
|
|
use ratatui::{
|
|
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
|
|
style::{Color, Style, Modifier},
|
|
widgets::{Block, BorderType, Borders, Paragraph},
|
|
Frame,
|
|
text::{Line, Span},
|
|
};
|
|
|
|
pub fn render_login(
|
|
f: &mut Frame,
|
|
area: Rect,
|
|
theme: &Theme,
|
|
state: &AuthState,
|
|
is_edit_mode: bool,
|
|
) {
|
|
// Main container
|
|
let block = Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_type(BorderType::Plain)
|
|
.border_style(Style::default().fg(theme.border))
|
|
.title(" Login ")
|
|
.style(Style::default().bg(theme.bg));
|
|
|
|
f.render_widget(block, area);
|
|
|
|
let inner_area = area.inner(Margin {
|
|
horizontal: 1,
|
|
vertical: 1,
|
|
});
|
|
|
|
// Layout chunks
|
|
let chunks = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints([
|
|
Constraint::Length(4), // Form (2 fields + padding)
|
|
Constraint::Length(1), // Error message
|
|
Constraint::Length(3), // Buttons
|
|
])
|
|
.split(inner_area);
|
|
|
|
// --- FORM RENDERING ---
|
|
let input_block = Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_style(if is_edit_mode {
|
|
Style::default().fg(theme.accent)
|
|
} else {
|
|
Style::default().fg(theme.border)
|
|
})
|
|
.style(Style::default().bg(theme.bg));
|
|
|
|
// Calculate inner area BEFORE rendering
|
|
let input_area = input_block.inner(chunks[0]);
|
|
|
|
f.render_widget(input_block, chunks[0]);
|
|
|
|
// Use the canvas renderer for fields
|
|
crate::components::handlers::canvas::render_canvas(
|
|
f,
|
|
input_area, // Use the pre-calculated area
|
|
state,
|
|
&["Username/Email", "Password"],
|
|
&state.current_field,
|
|
&[&state.username, &state.password],
|
|
theme,
|
|
is_edit_mode,
|
|
);
|
|
|
|
// --- BUTTONS --- (Keep this unchanged)
|
|
let button_chunks = Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
|
.split(chunks[2]);
|
|
|
|
// Login Button
|
|
let login_active = !state.return_selected;
|
|
let mut login_style = Style::default().fg(theme.fg);
|
|
let mut login_border = Style::default().fg(theme.border);
|
|
if login_active {
|
|
login_style = login_style.fg(theme.highlight).add_modifier(Modifier::BOLD);
|
|
login_border = login_border.fg(theme.accent);
|
|
}
|
|
|
|
f.render_widget(
|
|
Paragraph::new("Login")
|
|
.style(login_style)
|
|
.alignment(Alignment::Center)
|
|
.block(
|
|
Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_type(BorderType::Plain)
|
|
.border_style(login_border),
|
|
),
|
|
button_chunks[0],
|
|
);
|
|
|
|
// Return Button
|
|
let return_active = state.return_selected;
|
|
let mut return_style = Style::default().fg(theme.fg);
|
|
let mut return_border = Style::default().fg(theme.border);
|
|
if return_active {
|
|
return_style = return_style.fg(theme.highlight).add_modifier(Modifier::BOLD);
|
|
return_border = return_border.fg(theme.accent);
|
|
}
|
|
|
|
f.render_widget(
|
|
Paragraph::new("Return")
|
|
.style(return_style)
|
|
.alignment(Alignment::Center)
|
|
.block(
|
|
Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_type(BorderType::Plain)
|
|
.border_style(return_border),
|
|
),
|
|
button_chunks[1],
|
|
);
|
|
|
|
// Error message
|
|
if let Some(err) = &state.error_message {
|
|
f.render_widget(
|
|
Paragraph::new(err.as_str())
|
|
.style(Style::default().fg(Color::Red))
|
|
.alignment(Alignment::Center),
|
|
chunks[1],
|
|
);
|
|
}
|
|
}
|