From aee8ebb72b28206d16e907066ab89147c24db72b Mon Sep 17 00:00:00 2001 From: 72-S Date: Sun, 16 Feb 2025 18:03:15 +0100 Subject: [PATCH] feat(tui): add focus navigation --- src/tui/layout.rs | 46 +++++++++++----- src/tui/mod.rs | 2 +- src/tui/widgets.rs | 128 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 137 insertions(+), 39 deletions(-) diff --git a/src/tui/layout.rs b/src/tui/layout.rs index 1a913b8..8ca386b 100644 --- a/src/tui/layout.rs +++ b/src/tui/layout.rs @@ -1,3 +1,5 @@ +use super::widgets; +use super::Tui; use color_eyre::eyre::Context; use crossterm::event::{self, Event}; use ratatui::{ @@ -5,13 +7,26 @@ use ratatui::{ layout::{Constraint, Direction, Layout}, Frame, }; -use super::widgets; -use super::Tui; - #[derive(Debug, Default)] pub struct App { 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 { @@ -34,23 +49,23 @@ impl App { .split(frame.area()); // Render Instances - widgets::render_instances(frame, chunks[0]); + widgets::render_instances(frame, chunks[0], self.focused); // Divide the main content into vertical chunks let main_chunks = Layout::default() .direction(Direction::Vertical) .constraints([ - Constraint::Length(3), // Instances - Constraint::Min(10), // Main content - Constraint::Length(5), // Bottom panel + Constraint::Length(3), // Instances + Constraint::Min(10), // Main content + Constraint::Length(5), // Bottom panel ]) .split(chunks[1]); // Render widgets in the main content - widgets::render_title(frame, main_chunks[0]); - widgets::render_content(frame, main_chunks[1]); + widgets::render_title(frame, main_chunks[0], self.focused); + widgets::render_content(frame, main_chunks[1], self.focused); - // Bottom panel split + // Bottom panel split let bottom_chunks = Layout::default() .direction(Direction::Horizontal) .constraints([ @@ -61,9 +76,9 @@ impl App { .split(main_chunks[2]); // Render bottom widgets - widgets::render_account(frame, bottom_chunks[0]); - widgets::render_info(frame, bottom_chunks[1]); - widgets::render_status(frame, bottom_chunks[2]); + widgets::render_account(frame, bottom_chunks[0], self.focused); + widgets::render_details(frame, bottom_chunks[1], self.focused); + widgets::render_status(frame, bottom_chunks[2], self.focused); } /// 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<()> { match key_event.code { 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(()) diff --git a/src/tui/mod.rs b/src/tui/mod.rs index 992d3d6..7bb89e3 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -1,6 +1,6 @@ pub mod layout; -pub mod widgets; pub mod widget; +pub mod widgets; use std::io::{stdout, Result, Stdout}; diff --git a/src/tui/widgets.rs b/src/tui/widgets.rs index a828d38..5c88551 100644 --- a/src/tui/widgets.rs +++ b/src/tui/widgets.rs @@ -1,42 +1,120 @@ use ratatui::{ layout::Rect, - widgets::{Block, Borders, Paragraph}, + style::{Color, Style}, + text::{Line, Span}, + widgets::{Block, BorderType, Borders, Paragraph}, Frame, }; -use ratatui::widgets::BorderType; -pub fn render_instances(frame: &mut Frame, area: Rect) { - let sidebar = Block::default() - .title(" Instances ") - .borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded); - frame.render_widget(sidebar, area); +use super::layout::FocusedArea; + +fn styled_title(title: &str, highlight: bool) -> Line { + if !highlight || title.is_empty() { + 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) { - let top_bar = Paragraph::new(" TEST INSTANCE NAME").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded)).centered(); - frame.render_widget(top_bar, area); +fn render_section( + frame: &mut Frame, + 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) { - let file_list = Paragraph::new(" TEST CONTENT LIBRARY PAGE").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded)); - frame.render_widget(file_list, area); +pub fn render_instances(frame: &mut Frame, area: Rect, focused: FocusedArea) { + render_section( + frame, + area, + focused, + "Instances", + "", + FocusedArea::Instances, + true, + ); } -pub fn render_account(frame: &mut Frame, area: Rect) { - let metadata = Paragraph::new(" ") - .block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Account ")); - frame.render_widget(metadata, area); +pub fn render_title(frame: &mut Frame, area: Rect, focused: FocusedArea) { + render_section( + frame, + area, + focused, + "Title", + " TEST INSTANCE NAME", + FocusedArea::Content, + false, + ); } -pub fn render_info(frame: &mut Frame, area: Rect) { - let clipboard = Paragraph::new(" ") - .block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Info ")); - frame.render_widget(clipboard, area); +pub fn render_content(frame: &mut Frame, area: Rect, focused: FocusedArea) { + render_section( + frame, + area, + focused, + "Content", + " TEST CONTENT LIBRARY PAGE", + FocusedArea::Content, + true, + ); } -pub fn render_status(frame: &mut Frame, area: Rect) { - let processes = Paragraph::new(" ") - .block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Status ")); - frame.render_widget(processes, area); +pub fn render_account(frame: &mut Frame, area: Rect, focused: FocusedArea) { + render_section( + frame, + 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, + ); +}