feat(tui): add instance popup

This commit is contained in:
72-S committed 2025-04-26 16:17:03 +02:00
1 parent 8685fa9800
commit 9192089ad3
6 files changed
+214 -51

No files matched your search

+42 -28
View File
@@ -1,4 +1,8 @@
use super::{widgets::{self, profiles, WidgetKey}, Tui};
use super::{
widgets::{self, profiles, WidgetKey},
Tui,
};
use super::widgets::popups::new_instance;
use color_eyre::eyre::Context;
use crossterm::event::{self, Event};
use ratatui::{
@@ -21,6 +25,7 @@ pub enum FocusedArea {
Account,
Details,
Status,
Popup,
}
impl Default for FocusedArea {
@@ -38,6 +43,7 @@ impl App {
}
Ok(())
}
fn render_frame(&mut self, frame: &mut Frame) {
// Divide the screen into horizontal chunks
let chunks = Layout::default()
@@ -45,71 +51,79 @@ impl App {
.constraints([
Constraint::Percentage(20), // Instances
Constraint::Percentage(80), // Main content
Constraint::Min(1),
])
.split(frame.area());
// Render Instances
widgets::profiles::render(frame, chunks[0], self.focused, &mut self.profiles_state);
// 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(3), // Title
Constraint::Min(10), // Main Content
Constraint::Length(5), // Bottom panel
])
.split(chunks[1]);
// Render widgets in the main content
widgets::content::title(frame, main_chunks[0], self.focused);
widgets::content::render(frame, main_chunks[1], self.focused);
// Bottom panel split
let bottom_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(30), // Account
Constraint::Percentage(40), // Info
Constraint::Percentage(30), // Status
Constraint::Percentage(30),
Constraint::Percentage(40),
Constraint::Percentage(30),
])
.split(main_chunks[2]);
// Render bottom widgets
widgets::account::render(frame, bottom_chunks[0], self.focused);
widgets::details::render(frame, bottom_chunks[1], self.focused);
widgets::status::render(frame, bottom_chunks[2], self.focused);
}
/// updates the applications state based on user input
/// updates the application's state based on user input
fn handle_events(&mut self) -> color_eyre::Result<()> {
match event::read()? {
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => self
.handle_key_event(key_event)
.wrap_err_with(|| format!("handling key event failed:\n{key_event:#?}")),
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
self.handle_key_event(key_event)
.wrap_err_with(|| format!("handling key event failed:\n{key_event:#?}"))
}
_ => Ok(()),
}
}
fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> {
match key_event.code {
KeyCode::Char('q') => self.exit = true,
KeyCode::Char('P') => self.focused = FocusedArea::Profiles,
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,
_ => {}
match self.focused {
FocusedArea::Popup => {
new_instance::handle_key(&key_event, &mut self.profiles_state);
}
_ => {
match key_event.code {
KeyCode::Char('q') => self.exit = true,
KeyCode::Char('P') => self.focused = FocusedArea::Profiles,
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,
_ => {}
}
match self.focused {
FocusedArea::Profiles => self.profiles_state.handle_key(&key_event),
_ => {}
}
}
}
match self.focused {
FocusedArea::Profiles => self.profiles_state.handle_key(&key_event),
_ => {}
if self.profiles_state.wants_popup() {
self.focused = FocusedArea::Popup;
} else if self.focused == FocusedArea::Popup {
self.focused = FocusedArea::Profiles;
}
Ok(())
}
}