feat(tui): add focus navigation

This commit is contained in:
72-S committed 2025-02-16 18:03:15 +01:00
1 parent 8f1977c10c
commit aee8ebb72b
3 files changed
+137 -39

No files matched your search

+33 -13
View File
@@ -1,3 +1,5 @@
use super::widgets;
use super::Tui;
use color_eyre::eyre::Context; use color_eyre::eyre::Context;
use crossterm::event::{self, Event}; use crossterm::event::{self, Event};
use ratatui::{ use ratatui::{
@@ -5,13 +7,26 @@ use ratatui::{
layout::{Constraint, Direction, Layout}, layout::{Constraint, Direction, Layout},
Frame, Frame,
}; };
use super::widgets;
use super::Tui;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct App { pub struct App {
exit: bool, exit: bool,
focused: FocusedArea,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FocusedArea {
Instances,
Content,
Account,
Details,
Status,
}
impl Default for FocusedArea {
fn default() -> Self {
FocusedArea::Instances
}
} }
impl App { impl App {
@@ -34,23 +49,23 @@ impl App {
.split(frame.area()); .split(frame.area());
// Render Instances // Render Instances
widgets::render_instances(frame, chunks[0]); widgets::render_instances(frame, chunks[0], self.focused);
// Divide the main content into vertical chunks // Divide the main content into vertical chunks
let main_chunks = Layout::default() let main_chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([ .constraints([
Constraint::Length(3), // Instances Constraint::Length(3), // Instances
Constraint::Min(10), // Main content Constraint::Min(10), // Main content
Constraint::Length(5), // Bottom panel Constraint::Length(5), // Bottom panel
]) ])
.split(chunks[1]); .split(chunks[1]);
// Render widgets in the main content // Render widgets in the main content
widgets::render_title(frame, main_chunks[0]); widgets::render_title(frame, main_chunks[0], self.focused);
widgets::render_content(frame, main_chunks[1]); widgets::render_content(frame, main_chunks[1], self.focused);
// Bottom panel split // Bottom panel split
let bottom_chunks = Layout::default() let bottom_chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([ .constraints([
@@ -61,9 +76,9 @@ impl App {
.split(main_chunks[2]); .split(main_chunks[2]);
// Render bottom widgets // Render bottom widgets
widgets::render_account(frame, bottom_chunks[0]); widgets::render_account(frame, bottom_chunks[0], self.focused);
widgets::render_info(frame, bottom_chunks[1]); widgets::render_details(frame, bottom_chunks[1], self.focused);
widgets::render_status(frame, bottom_chunks[2]); widgets::render_status(frame, bottom_chunks[2], self.focused);
} }
/// updates the applications state based on user input /// updates the applications state based on user input
@@ -79,6 +94,11 @@ impl App {
fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> { fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> {
match key_event.code { match key_event.code {
KeyCode::Char('q') => self.exit(), KeyCode::Char('q') => self.exit(),
KeyCode::Char('I') => self.focused = FocusedArea::Instances,
KeyCode::Char('C') => self.focused = FocusedArea::Content,
KeyCode::Char('A') => self.focused = FocusedArea::Account,
KeyCode::Char('D') => self.focused = FocusedArea::Details,
KeyCode::Char('S') => self.focused = FocusedArea::Status,
_ => {} _ => {}
} }
Ok(()) Ok(())
+1 -1
View File
@@ -1,6 +1,6 @@
pub mod layout; pub mod layout;
pub mod widgets;
pub mod widget; pub mod widget;
pub mod widgets;
use std::io::{stdout, Result, Stdout}; use std::io::{stdout, Result, Stdout};
+103 -25
View File
@@ -1,42 +1,120 @@
use ratatui::{ use ratatui::{
layout::Rect, layout::Rect,
widgets::{Block, Borders, Paragraph}, style::{Color, Style},
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
Frame, Frame,
}; };
use ratatui::widgets::BorderType;
pub fn render_instances(frame: &mut Frame, area: Rect) { use super::layout::FocusedArea;
let sidebar = Block::default()
.title(" Instances ") fn styled_title(title: &str, highlight: bool) -> Line {
.borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded); if !highlight || title.is_empty() {
frame.render_widget(sidebar, area); Line::from(Span::raw(title))
} else {
let mut chars = title.chars();
let first = chars.next().unwrap_or_default().to_string();
let rest: String = chars.collect();
Line::from(vec![
Span::styled(first, Style::default().fg(Color::Yellow)),
Span::raw(rest),
])
}
} }
pub fn render_title(frame: &mut Frame, area: Rect) { fn render_section(
let top_bar = Paragraph::new(" TEST INSTANCE NAME").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded)).centered(); frame: &mut Frame,
frame.render_widget(top_bar, area); area: Rect,
focused: FocusedArea,
title: &str,
content: &str,
focus_area: FocusedArea,
highlight: bool,
) {
let color = if focused == focus_area {
Color::White
} else {
Color::DarkGray
};
let block = Block::default()
.title(styled_title(title, highlight))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(color));
let widget = Paragraph::new(content).block(block);
frame.render_widget(widget, area);
} }
pub fn render_content(frame: &mut Frame, area: Rect) { pub fn render_instances(frame: &mut Frame, area: Rect, focused: FocusedArea) {
let file_list = Paragraph::new(" TEST CONTENT LIBRARY PAGE").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded)); render_section(
frame.render_widget(file_list, area); frame,
area,
focused,
"Instances",
"",
FocusedArea::Instances,
true,
);
} }
pub fn render_account(frame: &mut Frame, area: Rect) { pub fn render_title(frame: &mut Frame, area: Rect, focused: FocusedArea) {
let metadata = Paragraph::new(" ") render_section(
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Account ")); frame,
frame.render_widget(metadata, area); area,
focused,
"Title",
" TEST INSTANCE NAME",
FocusedArea::Content,
false,
);
} }
pub fn render_info(frame: &mut Frame, area: Rect) { pub fn render_content(frame: &mut Frame, area: Rect, focused: FocusedArea) {
let clipboard = Paragraph::new(" ") render_section(
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Info ")); frame,
frame.render_widget(clipboard, area); area,
focused,
"Content",
" TEST CONTENT LIBRARY PAGE",
FocusedArea::Content,
true,
);
} }
pub fn render_status(frame: &mut Frame, area: Rect) { pub fn render_account(frame: &mut Frame, area: Rect, focused: FocusedArea) {
let processes = Paragraph::new(" ") render_section(
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Status ")); frame,
frame.render_widget(processes, area); area,
focused,
"Account",
"",
FocusedArea::Account,
true,
);
} }
pub fn render_details(frame: &mut Frame, area: Rect, focused: FocusedArea) {
render_section(
frame,
area,
focused,
"Details",
"",
FocusedArea::Details,
true,
);
}
pub fn render_status(frame: &mut Frame, area: Rect, focused: FocusedArea) {
render_section(
frame,
area,
focused,
"Status",
"",
FocusedArea::Status,
true,
);
}