Compare commits

..

4 Commits

Author SHA1 Message Date
filipriec
f42790980d perfectly working buffer now 2025-04-15 00:18:42 +02:00
filipriec
779683de4b buffer movement is now working as I would only wish it would. Needs layers implementation, will do in the future 2025-04-15 00:10:28 +02:00
filipriec
aa8887318f history of buffers implemented now 2025-04-14 23:58:58 +02:00
filipriec
3ad8dc6490 better buffer list 2025-04-14 23:35:56 +02:00
4 changed files with 105 additions and 41 deletions

View File

@@ -3,12 +3,13 @@
use crate::config::colors::themes::Theme;
use crate::state::app::state::AppState;
use ratatui::{
layout::{Alignment, Rect}, // Added Alignment
layout::{Alignment, Rect},
style::{Style, Stylize},
text::{Line, Span}, // Added Span
widgets::{Block, Borders, Paragraph}, // Changed widgets
text::{Line, Span},
widgets::Paragraph,
Frame,
};
use unicode_width::UnicodeWidthStr;
pub fn render_buffer_list(
f: &mut Frame,
@@ -16,46 +17,47 @@ pub fn render_buffer_list(
theme: &Theme,
app_state: &AppState,
) {
let mut current_buffer_name = "*scratch*"; // Default
// --- Style Definitions ---
let active_style = Style::default()
.fg(theme.bg)
.bg(theme.highlight);
// Determine the active buffer name based on UI state
if app_state.ui.show_intro {
current_buffer_name = "Intro";
} else if app_state.ui.show_register {
current_buffer_name = "Register";
} else if app_state.ui.show_login {
current_buffer_name = "Login";
} else if app_state.ui.show_admin {
current_buffer_name = "Admin Panel";
} else if app_state.ui.show_form {
// Potentially make this more specific later (e.g., show table name)
current_buffer_name = "Data Form";
let inactive_style = Style::default()
.fg(theme.fg)
.bg(theme.bg);
// --- Create Spans ---
let mut spans = Vec::new();
let history = &app_state.ui.buffer_history;
let mut current_width = 0;
for (i, view) in history.iter().enumerate() {
let is_active = i == app_state.ui.active_buffer_index;
let buffer_name = view.display_name();
let buffer_text = format!(" {} ", buffer_name);
let text_width = UnicodeWidthStr::width(buffer_text.as_str());
// Calculate width needed for this buffer (separator + text)
let needed_width = text_width;
if current_width + needed_width > area.width as usize {
break;
}
// Add the buffer text itself
let text_style = if is_active { active_style } else { inactive_style };
spans.push(Span::styled(buffer_text, text_style));
current_width += text_width;
}
// Add more conditions if other views exist
// Create the text line for the buffer name with padding
let buffer_text = Line::from(vec![
Span::raw(" "), // Left padding
Span::styled(
current_buffer_name,
Style::default().fg(theme.bg).bg(theme.highlight), // Style active tab
),
Span::raw(" "), // Right padding
]);
// --- Filler Span ---
let remaining_width = area.width.saturating_sub(current_width as u16);
spans.push(Span::styled(
" ".repeat(remaining_width as usize),
inactive_style,
));
// Create the paragraph centered within the block
let paragraph = Paragraph::new(buffer_text)
.alignment(Alignment::Left) // Align text left, padding handles spacing
.block(
Block::default()
// Keep only the bottom border to act as a separator
.borders(Borders::BOTTOM)
.border_style(Style::default().fg(theme.secondary)),
)
// Set the background for the entire area (behind the text/block)
.style(Style::default().bg(theme.bg));
// Render the paragraph widget
// --- Render ---
let buffer_line = Line::from(spans);
let paragraph = Paragraph::new(buffer_line).alignment(Alignment::Left);
f.render_widget(paragraph, area);
}

View File

@@ -25,6 +25,7 @@ use crate::modes::{
};
use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
use crate::state::app::state::AppView;
use crate::tui::functions::common::form::SaveOutcome;
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -80,6 +81,21 @@ impl EventHandler {
let current_mode = ModeManager::derive_mode(app_state, self);
app_state.update_mode(current_mode);
// Determine the current view, including dynamic names
let current_view = {
let ui = &app_state.ui;
if ui.show_intro { AppView::Intro }
else if ui.show_login { AppView::Login }
else if ui.show_register { AppView::Register }
else if ui.show_admin { AppView::Admin }
else if ui.show_form {
let form_name = app_state.selected_profile.clone().unwrap_or_else(|| "Data Form".to_string());
AppView::Form(form_name)
}
else { AppView::Scratch }
};
app_state.ui.update_buffer_history(current_view);
// --- DIALOG MODALITY ---
if app_state.ui.dialog.dialog_show {
if let Some(dialog_result) = dialog::handle_dialog_event(

View File

@@ -5,6 +5,31 @@ use common::proto::multieko2::table_definition::ProfileTreeResponse;
use crate::modes::handlers::mode_manager::AppMode;
use crate::ui::handlers::context::DialogPurpose;
/// Represents the distinct views or "buffers" the user can navigate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AppView {
Intro,
Login,
Register,
Admin,
Form(String),
Scratch,
}
impl AppView {
/// Returns the display name for the view.
pub fn display_name(&self) -> &str {
match self {
AppView::Intro => "Intro",
AppView::Login => "Login",
AppView::Register => "Register",
AppView::Admin => "Admin Panel",
AppView::Form(name) => name.as_str(),
AppView::Scratch => "*scratch*",
}
}
}
pub struct DialogState {
pub dialog_show: bool,
pub dialog_title: String,
@@ -23,6 +48,8 @@ pub struct UiState {
pub show_login: bool,
pub show_register: bool,
pub focus_outside_canvas: bool,
pub buffer_history: Vec<AppView>,
pub active_buffer_index: usize,
pub dialog: DialogState,
}
@@ -139,6 +166,8 @@ impl Default for UiState {
show_register: false,
show_buffer_list: false,
focus_outside_canvas: false,
buffer_history: vec![AppView::Intro],
active_buffer_index: 0,
dialog: DialogState::default(),
}
}
@@ -157,3 +186,20 @@ impl Default for DialogState {
}
}
}
impl UiState {
/// Updates the buffer history and active index.
pub fn update_buffer_history(&mut self, view: AppView) {
let existing_pos = self.buffer_history.iter().position(|v| v == &view);
match existing_pos {
Some(pos) => {
self.active_buffer_index = pos;
}
None => {
self.buffer_history.push(view.clone());
self.active_buffer_index = self.buffer_history.len() - 1;
}
}
}
}

View File

@@ -44,7 +44,7 @@ pub fn render_ui(
// Adjust layout based on whether buffer list is shown
let constraints = if app_state.ui.show_buffer_list {
vec![
Constraint::Length(2), // Buffer list
Constraint::Length(1), // Buffer list
Constraint::Min(1), // Main content
Constraint::Length(1), // Status line
Constraint::Length(1), // Command line