history of buffers implemented now

This commit is contained in:
filipriec
2025-04-14 23:58:58 +02:00
parent 3ad8dc6490
commit aa8887318f
3 changed files with 110 additions and 57 deletions

View File

@@ -4,90 +4,91 @@ use crate::config::colors::themes::Theme;
use crate::state::app::state::AppState;
use ratatui::{
layout::{Alignment, Rect},
style::{Modifier, Style, Stylize},
style::{Style, Stylize},
text::{Line, Span},
widgets::Paragraph, // Removed Block and Borders
widgets::Paragraph,
Frame,
};
use unicode_width::UnicodeWidthStr;
pub fn render_buffer_list(
f: &mut Frame,
area: Rect, // Still 1 line high
area: Rect,
theme: &Theme,
app_state: &AppState,
) {
let mut current_buffer_name = "*scratch*"; // Default
// 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 {
current_buffer_name = "Data Form";
}
// Add more conditions if other views exist
// --- Style Definitions ---
// Use Powerline symbols if available, otherwise fallback
// Ensure your terminal font supports these for the best look!
const RIGHT_SEPARATOR: &str = ""; // U+E0B0
// const RIGHT_SEPARATOR: &str = ">"; // Fallback
const LEFT_SEPARATOR: &str = ""; // U+E0B2
// const LEFT_SEPARATOR: &str = "<"; // Fallback
let active_style = Style::default()
.fg(theme.bg) // Text on active background
.bg(theme.highlight); // Active background
.fg(theme.bg)
.bg(theme.highlight);
let separator_style_active_to_inactive = Style::default()
.fg(theme.highlight) // Separator color matches the background it comes from
.bg(theme.bg); // Background matches the area it points into
.fg(theme.highlight)
.bg(theme.bg);
let separator_style_inactive_to_active = Style::default()
.fg(theme.highlight)
.bg(theme.bg);
let separator_style_inactive_to_inactive = Style::default()
.fg(theme.fg)
.bg(theme.bg);
let inactive_style = Style::default()
.fg(theme.fg) // Default text color
.bg(theme.bg); // Default background
.fg(theme.fg)
.bg(theme.bg);
// --- Create Spans ---
let buffer_text = format!(" {} ", current_buffer_name); // Add padding
let mut spans = Vec::new();
let history = &app_state.ui.buffer_history;
let history_len = history.len();
let mut current_width = 0;
// Span 1: Left edge (no separator needed if it's the first element)
// If you had multiple buffers, the *first* one wouldn't have a left separator.
// let left_sep = Span::raw(""); // Example for first buffer
for (i, view) in history.iter().enumerate() {
let is_active = i == history_len - 1;
let buffer_name = view.display_name();
let buffer_text = format!(" {} ", buffer_name);
let text_width = UnicodeWidthStr::width(buffer_text.as_str());
let separator_width = UnicodeWidthStr::width(RIGHT_SEPARATOR);
// Span 2: The active buffer text
let buffer_span = Span::styled(buffer_text.clone(), active_style);
let needed_width = if i > 0 { separator_width } else { 0 } + text_width + separator_width;
if current_width + needed_width > area.width as usize {
break;
}
// Span 3: The right separator pointing away from the active buffer
let right_sep = Span::styled(
RIGHT_SEPARATOR,
separator_style_active_to_inactive,
);
if i > 0 {
let sep_style = if is_active {
separator_style_inactive_to_active
} else {
separator_style_inactive_to_inactive
};
spans.push(Span::styled(LEFT_SEPARATOR, sep_style));
current_width += separator_width;
}
// Span 4: Filler for the rest of the line
let text_width = UnicodeWidthStr::width(buffer_text.as_str());
// Account for the width of the separator character (usually 1)
let separator_width = UnicodeWidthStr::width(RIGHT_SEPARATOR);
let total_used_width = (text_width + separator_width) as u16;
let remaining_width = area.width.saturating_sub(total_used_width);
let filler_span = Span::styled(
let text_style = if is_active { active_style } else { inactive_style };
spans.push(Span::styled(buffer_text, text_style));
current_width += text_width;
let sep_style = if is_active {
separator_style_active_to_inactive
} else {
separator_style_inactive_to_inactive
};
spans.push(Span::styled(RIGHT_SEPARATOR, sep_style));
current_width += separator_width;
}
let remaining_width = area.width.saturating_sub(current_width as u16);
spans.push(Span::styled(
" ".repeat(remaining_width as usize),
inactive_style, // Filler uses inactive style
);
inactive_style,
));
// --- Combine into Line ---
// Order: Buffer Text -> Right Separator -> Filler
// (If multiple buffers: LeftSep -> Text -> RightSep -> LeftSep -> Text -> ...)
let buffer_line = Line::from(vec![buffer_span, right_sep, filler_span]);
// --- 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,7 @@ pub struct UiState {
pub show_login: bool,
pub show_register: bool,
pub focus_outside_canvas: bool,
pub buffer_history: Vec<AppView>,
pub dialog: DialogState,
}
@@ -139,6 +165,7 @@ impl Default for UiState {
show_register: false,
show_buffer_list: false,
focus_outside_canvas: false,
buffer_history: vec![AppView::Intro],
dialog: DialogState::default(),
}
}
@@ -157,3 +184,12 @@ impl Default for DialogState {
}
}
}
impl UiState {
/// Adds the given view to the history if it's different from the last one.
pub fn update_buffer_history(&mut self, view: AppView) {
if self.buffer_history.last() != Some(&view) {
self.buffer_history.push(view.clone());
}
}
}