use crate::{render_themed, Theme};
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, BorderType, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
Frame,
};
use crate::app::{App, CopyProgress, Modal, Snackbar};
use crate::inline_editor::render_inline_editor;
use crate::preview::render_preview;
use tui_slider::{style::SliderStyle, Slider, SliderOrientation, SliderState};
fn key_span<'a>(s: &'a str, theme: &Theme) -> Span<'a> {
Span::styled(
s,
Style::default()
.fg(theme.accent)
.add_modifier(Modifier::BOLD),
)
}
fn dim_span<'a>(s: &'a str, theme: &Theme) -> Span<'a> {
Span::styled(s, Style::default().fg(theme.dim))
}
pub fn draw(app: &mut App, frame: &mut Frame) {
let theme = *app.theme();
let full = frame.area();
if theme.bg != Color::Reset {
frame.render_widget(Block::default().style(Style::default().bg(theme.bg)), full);
}
if let Some(ref editor) = app.inline_editor {
render_inline_editor(frame, full, editor, &theme);
return;
}
let debug_height = if full.height >= 30 {
10
} else if full.height >= 20 {
6
} else {
3
};
let v_chunks = if app.verbose {
Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Min(0),
Constraint::Length(debug_height),
Constraint::Length(6),
])
.split(full)
} else {
Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0), Constraint::Length(6)])
.split(full)
};
let main_area = v_chunks[0];
let action_area = if app.verbose {
v_chunks[2]
} else {
v_chunks[1]
};
let action_rows = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Length(3)])
.split(action_area);
let nav_fileops_area = action_rows[0];
let global_status_area = action_rows[1];
if app.verbose {
render_debug_panel(frame, v_chunks[1], app, &theme);
}
let visible_pane_count = if app.single_pane { 1 } else { app.panes.len() };
let mut h_constraints = vec![];
if app.show_preview {
if app.single_pane {
h_constraints.push(Constraint::Percentage(40));
} else {
let pct = (50 / visible_pane_count.max(1)) as u16;
for _ in 0..visible_pane_count {
h_constraints.push(Constraint::Percentage(pct));
}
}
h_constraints.push(Constraint::Min(0)); } else {
let pct = (100 / visible_pane_count.max(1)) as u16;
for _ in 0..visible_pane_count {
h_constraints.push(Constraint::Percentage(pct));
}
}
if app.show_theme_panel {
h_constraints.push(Constraint::Length(32));
}
if app.show_options_panel {
h_constraints.push(Constraint::Length(42));
}
if app.show_editor_panel {
h_constraints.push(Constraint::Length(42));
}
let h_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints(h_constraints)
.split(main_area);
let active_theme = theme;
let inactive_theme = theme.accent(theme.dim).brand(theme.dim);
let theme_changed = app.panes[0].theme_name != app.theme_name();
if theme_changed {
let theme_name = app.theme_name().to_string();
for p in app.panes.iter_mut() {
p.theme_name = theme_name.clone();
}
}
let editor_label = if app.editor == crate::app::Editor::None {
""
} else {
app.editor.label()
};
if app.panes[0].editor_name != editor_label {
let editor_name = editor_label.to_string();
for p in app.panes.iter_mut() {
p.editor_name = editor_name.clone();
}
}
let active_idx = app.active_idx;
if app.single_pane {
render_themed(
&mut app.panes[active_idx],
frame,
h_chunks[0],
&active_theme,
);
} else {
for (i, pane) in app.panes.iter_mut().enumerate() {
let pane_theme = if i == active_idx {
&active_theme
} else {
&inactive_theme
};
render_themed(pane, frame, h_chunks[i], pane_theme);
}
}
if app.show_preview {
let preview_idx = visible_pane_count;
let preview_area = h_chunks[preview_idx];
let current_path = app.active_pane().current_entry().map(|e| e.path.clone());
app.preview_state.update(
current_path.as_deref(),
preview_area.width,
preview_area.height,
);
render_preview(frame, preview_area, &app.preview_state, &theme);
}
if app.show_theme_panel {
let panel_area = h_chunks[h_chunks.len() - 1];
render_theme_panel(frame, panel_area, app);
}
if app.show_options_panel {
let panel_area = h_chunks[h_chunks.len() - 1];
render_options_panel(frame, panel_area, app);
}
if app.show_editor_panel {
let panel_area = h_chunks[h_chunks.len() - 1];
render_editor_panel(frame, panel_area, app);
}
render_nav_hints(frame, nav_fileops_area, global_status_area, app, &theme);
if let Some(modal) = &app.modal {
render_modal(frame, full, modal, &theme);
}
if let Some(progress) = &app.copy_progress {
render_copy_progress(frame, full, progress, &theme);
}
if app.snackbar.as_ref().is_some_and(|s| s.is_expired()) {
app.snackbar = None;
}
if let Some(snackbar) = &app.snackbar {
render_snackbar(frame, full, snackbar, &theme);
}
}
mod action_bar;
mod modal;
mod overlays;
mod panels;
pub use self::action_bar::{render_action_bar, render_nav_hints};
#[cfg(test)]
pub use self::action_bar::{render_action_bar_spans, render_nav_hints_spans};
pub use self::modal::render_modal;
pub use self::overlays::{render_copy_progress, render_debug_panel, render_snackbar};
pub use self::panels::{render_editor_panel, render_options_panel, render_theme_panel};
#[cfg(test)]
#[path = "tests.rs"]
mod tests;