feat(tui): add instance popup
This commit is contained in:
1 parent
8685fa9800
commit
9192089ad3
6 files changed
+214
-51
No files matched your search
+42
-28
@@ -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 color_eyre::eyre::Context;
|
||||||
use crossterm::event::{self, Event};
|
use crossterm::event::{self, Event};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
@@ -21,6 +25,7 @@ pub enum FocusedArea {
|
|||||||
Account,
|
Account,
|
||||||
Details,
|
Details,
|
||||||
Status,
|
Status,
|
||||||
|
Popup,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FocusedArea {
|
impl Default for FocusedArea {
|
||||||
@@ -38,6 +43,7 @@ impl App {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_frame(&mut self, frame: &mut Frame) {
|
fn render_frame(&mut self, frame: &mut Frame) {
|
||||||
// Divide the screen into horizontal chunks
|
// Divide the screen into horizontal chunks
|
||||||
let chunks = Layout::default()
|
let chunks = Layout::default()
|
||||||
@@ -45,71 +51,79 @@ impl App {
|
|||||||
.constraints([
|
.constraints([
|
||||||
Constraint::Percentage(20), // Instances
|
Constraint::Percentage(20), // Instances
|
||||||
Constraint::Percentage(80), // Main content
|
Constraint::Percentage(80), // Main content
|
||||||
Constraint::Min(1),
|
|
||||||
])
|
])
|
||||||
.split(frame.area());
|
.split(frame.area());
|
||||||
|
|
||||||
|
|
||||||
// Render Instances
|
// Render Instances
|
||||||
widgets::profiles::render(frame, chunks[0], self.focused, &mut self.profiles_state);
|
widgets::profiles::render(frame, chunks[0], self.focused, &mut self.profiles_state);
|
||||||
|
|
||||||
|
|
||||||
// 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), // Title
|
||||||
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
|
|
||||||
widgets::content::title(frame, main_chunks[0], self.focused);
|
widgets::content::title(frame, main_chunks[0], self.focused);
|
||||||
widgets::content::render(frame, main_chunks[1], self.focused);
|
widgets::content::render(frame, main_chunks[1], self.focused);
|
||||||
|
|
||||||
// Bottom panel split
|
|
||||||
let bottom_chunks = Layout::default()
|
let bottom_chunks = Layout::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.constraints([
|
.constraints([
|
||||||
Constraint::Percentage(30), // Account
|
Constraint::Percentage(30),
|
||||||
Constraint::Percentage(40), // Info
|
Constraint::Percentage(40),
|
||||||
Constraint::Percentage(30), // Status
|
Constraint::Percentage(30),
|
||||||
])
|
])
|
||||||
.split(main_chunks[2]);
|
.split(main_chunks[2]);
|
||||||
|
|
||||||
// Render bottom widgets
|
|
||||||
widgets::account::render(frame, bottom_chunks[0], self.focused);
|
widgets::account::render(frame, bottom_chunks[0], self.focused);
|
||||||
widgets::details::render(frame, bottom_chunks[1], self.focused);
|
widgets::details::render(frame, bottom_chunks[1], self.focused);
|
||||||
widgets::status::render(frame, bottom_chunks[2], 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<()> {
|
fn handle_events(&mut self) -> color_eyre::Result<()> {
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => self
|
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
|
||||||
.handle_key_event(key_event)
|
self.handle_key_event(key_event)
|
||||||
.wrap_err_with(|| format!("handling key event failed:\n{key_event:#?}")),
|
.wrap_err_with(|| format!("handling key event failed:\n{key_event:#?}"))
|
||||||
|
}
|
||||||
_ => Ok(()),
|
_ => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 self.focused {
|
||||||
KeyCode::Char('q') => self.exit = true,
|
FocusedArea::Popup => {
|
||||||
KeyCode::Char('P') => self.focused = FocusedArea::Profiles,
|
new_instance::handle_key(&key_event, &mut self.profiles_state);
|
||||||
KeyCode::Char('C') => self.focused = FocusedArea::Content,
|
}
|
||||||
KeyCode::Char('A') => self.focused = FocusedArea::Account,
|
_ => {
|
||||||
KeyCode::Char('D') => self.focused = FocusedArea::Details,
|
match key_event.code {
|
||||||
KeyCode::Char('S') => self.focused = FocusedArea::Status,
|
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 {
|
if self.profiles_state.wants_popup() {
|
||||||
FocusedArea::Profiles => self.profiles_state.handle_key(&key_event),
|
self.focused = FocusedArea::Popup;
|
||||||
_ => {}
|
} else if self.focused == FocusedArea::Popup {
|
||||||
|
self.focused = FocusedArea::Profiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ pub mod content;
|
|||||||
pub mod details;
|
pub mod details;
|
||||||
pub mod profiles;
|
pub mod profiles;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
|
pub mod popups;
|
||||||
|
|
||||||
pub fn styled_title(title: &str, highlight: bool) -> Line {
|
pub fn styled_title(title: &str, highlight: bool) -> Line {
|
||||||
if !highlight || title.is_empty() {
|
if !highlight || title.is_empty() {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
use ratatui::{
|
||||||
|
buffer::Buffer,
|
||||||
|
layout::Rect,
|
||||||
|
style::Style,
|
||||||
|
text::Line,
|
||||||
|
widgets::{Block, Borders, Clear, Widget},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Popup<'a> {
|
||||||
|
pub title: Line<'a>,
|
||||||
|
pub border_style: Style,
|
||||||
|
pub title_style: Style,
|
||||||
|
pub style: Style,
|
||||||
|
pub content: Box<dyn Fn(Rect, &mut Buffer) + 'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Widget for Popup<'a> {
|
||||||
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||||
|
Clear.render(area, buf);
|
||||||
|
|
||||||
|
let block = Block::new()
|
||||||
|
.title(self.title)
|
||||||
|
.title_style(self.title_style)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(self.border_style);
|
||||||
|
|
||||||
|
let inner_area = block.inner(area);
|
||||||
|
block.render(area, buf);
|
||||||
|
|
||||||
|
(self.content)(inner_area, buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod base;
|
||||||
|
pub mod new_instance;
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
use super::base::Popup;
|
||||||
|
use crate::tui::layout::FocusedArea;
|
||||||
|
use crate::tui::widgets::profiles;
|
||||||
|
use crossterm::event::KeyCode;
|
||||||
|
use ratatui::{
|
||||||
|
Frame,
|
||||||
|
layout::{Rect, Layout, Direction, Constraint},
|
||||||
|
text::{Line, Span},
|
||||||
|
widgets::{Block, Borders, Paragraph, Wrap, Widget},
|
||||||
|
};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
static INSTANCE_POPUP_STATE: Lazy<Mutex<NewInstanceState>> = Lazy::new(|| Mutex::new(NewInstanceState::default()));
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
enum NewInstanceMode {
|
||||||
|
#[default]
|
||||||
|
Buttons,
|
||||||
|
Input,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct NewInstanceState {
|
||||||
|
mode: NewInstanceMode,
|
||||||
|
input_text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(frame: &mut Frame, area: Rect, _focused: FocusedArea) {
|
||||||
|
let state = INSTANCE_POPUP_STATE.lock().unwrap();
|
||||||
|
|
||||||
|
let popup = Popup {
|
||||||
|
title: Line::from("New Instance"),
|
||||||
|
content: Box::new(move |area, buf| {
|
||||||
|
match state.mode {
|
||||||
|
NewInstanceMode::Buttons => {
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(40),
|
||||||
|
Constraint::Length(3),
|
||||||
|
Constraint::Length(3),
|
||||||
|
Constraint::Percentage(40),
|
||||||
|
])
|
||||||
|
.split(area);
|
||||||
|
|
||||||
|
let create_button = Paragraph::new(Line::from(vec![
|
||||||
|
Span::styled("[C]", ratatui::style::Style::default().add_modifier(ratatui::style::Modifier::BOLD)),
|
||||||
|
Span::raw("reate New Instance"),
|
||||||
|
])).alignment(ratatui::layout::Alignment::Center);
|
||||||
|
|
||||||
|
let import_button = Paragraph::new(Line::from(vec![
|
||||||
|
Span::styled("[I]", ratatui::style::Style::default().add_modifier(ratatui::style::Modifier::BOLD)),
|
||||||
|
Span::raw("mport Morinth Modpack"),
|
||||||
|
])).alignment(ratatui::layout::Alignment::Center);
|
||||||
|
|
||||||
|
create_button.render(chunks[1], buf);
|
||||||
|
import_button.render(chunks[2], buf);
|
||||||
|
}
|
||||||
|
NewInstanceMode::Input => {
|
||||||
|
let paragraph = Paragraph::new(state.input_text.clone())
|
||||||
|
.block(Block::default().title("Enter URL or Path").borders(Borders::ALL))
|
||||||
|
.wrap(Wrap { trim: true });
|
||||||
|
|
||||||
|
paragraph.render(area, buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
border_style: Default::default(),
|
||||||
|
title_style: Default::default(),
|
||||||
|
style: Default::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
frame.render_widget(popup, area);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_key(key_event: &crossterm::event::KeyEvent, state: &mut profiles::State) {
|
||||||
|
let mut popup_state = INSTANCE_POPUP_STATE.lock().unwrap();
|
||||||
|
|
||||||
|
match popup_state.mode {
|
||||||
|
NewInstanceMode::Buttons => {
|
||||||
|
match key_event.code {
|
||||||
|
KeyCode::Char('q') => {
|
||||||
|
state.show_popup = false;
|
||||||
|
}
|
||||||
|
KeyCode::Char('c') => {
|
||||||
|
// Create new instance
|
||||||
|
state.show_popup = false;
|
||||||
|
}
|
||||||
|
KeyCode::Char('i') => {
|
||||||
|
// Switch to input mode
|
||||||
|
popup_state.mode = NewInstanceMode::Input;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NewInstanceMode::Input => match key_event.code {
|
||||||
|
KeyCode::Esc => {
|
||||||
|
popup_state.mode = NewInstanceMode::Buttons;
|
||||||
|
}
|
||||||
|
KeyCode::Enter => {
|
||||||
|
// Confirm input
|
||||||
|
state.show_popup = false;
|
||||||
|
}
|
||||||
|
KeyCode::Char(c) => {
|
||||||
|
popup_state.input_text.push(c);
|
||||||
|
}
|
||||||
|
KeyCode::Backspace => {
|
||||||
|
popup_state.input_text.pop();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
-23
@@ -4,20 +4,22 @@ use ratatui::{
|
|||||||
style::{Modifier, Style},
|
style::{Modifier, Style},
|
||||||
text::Text,
|
text::Text,
|
||||||
widgets::{
|
widgets::{
|
||||||
Block, BorderType, Borders, Cell, Row, Scrollbar, ScrollbarOrientation, ScrollbarState, Table, TableState
|
Block, BorderType, Borders, Cell, Row, Scrollbar, ScrollbarOrientation, ScrollbarState,
|
||||||
|
Table, TableState,
|
||||||
},
|
},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{config::SETTINGS, tui::layout::FocusedArea};
|
use crate::{config::SETTINGS, tui::layout::FocusedArea};
|
||||||
|
|
||||||
use super::{styled_title, WidgetKey};
|
use super::{popups, styled_title, WidgetKey};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub profiles: Vec<Data>,
|
pub profiles: Vec<Data>,
|
||||||
pub table_state: TableState,
|
pub table_state: TableState,
|
||||||
pub scrollbar_state: ScrollbarState,
|
pub scrollbar_state: ScrollbarState,
|
||||||
|
pub show_popup: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@@ -27,17 +29,6 @@ pub struct Data {
|
|||||||
pub running: bool,
|
pub running: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl std::fmt::Debug for Data {
|
|
||||||
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
// f.debug_struct("Data")
|
|
||||||
// .field("title", &self.title)
|
|
||||||
// .field("id", &self.id)
|
|
||||||
// .field("running", &self.running)
|
|
||||||
// .field("img", &"<StatefulImage>")
|
|
||||||
// .finish()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn next(&mut self) {
|
fn next(&mut self) {
|
||||||
let i = match self.table_state.selected() {
|
let i = match self.table_state.selected() {
|
||||||
@@ -48,7 +39,7 @@ impl State {
|
|||||||
i + 1
|
i + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => 0,
|
_none => 0,
|
||||||
};
|
};
|
||||||
self.table_state.select(Some(i));
|
self.table_state.select(Some(i));
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
@@ -63,7 +54,7 @@ impl State {
|
|||||||
i - 1
|
i - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => 0,
|
_none => 0,
|
||||||
};
|
};
|
||||||
self.table_state.select(Some(i));
|
self.table_state.select(Some(i));
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
@@ -83,17 +74,17 @@ impl State {
|
|||||||
|
|
||||||
self.scrollbar_state = ScrollbarState::new(items).position(index);
|
self.scrollbar_state = ScrollbarState::new(items).position(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn wants_popup(&self) -> bool {
|
||||||
|
self.show_popup
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WidgetKey for State {
|
impl WidgetKey for State {
|
||||||
fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) {
|
fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) {
|
||||||
match key_event.code {
|
match key_event.code {
|
||||||
KeyCode::Char('a') => {
|
KeyCode::Char('a') => {
|
||||||
self.profiles.push(Data {
|
self.show_popup = true;
|
||||||
title: "Test".to_string(),
|
|
||||||
id: "123".to_string(),
|
|
||||||
running: false,
|
|
||||||
});
|
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
}
|
}
|
||||||
KeyCode::Char('d') => {
|
KeyCode::Char('d') => {
|
||||||
@@ -140,14 +131,13 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
|||||||
SETTINGS.colors.row_alternate_bg
|
SETTINGS.colors.row_alternate_bg
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Row::new(vec![
|
Row::new(vec![
|
||||||
Cell::from(Text::from(format!("\n{}\n", data.title))),
|
Cell::from(Text::from(format!("\n{}\n", data.title))),
|
||||||
Cell::from(Text::from(format!("\n{}\n", data.id))),
|
Cell::from(Text::from(format!("\n{}\n", data.id))),
|
||||||
Cell::from(Text::from(format!("\n{}\n", status))),
|
Cell::from(Text::from(format!("\n{}\n", status))),
|
||||||
])
|
])
|
||||||
.height(4)
|
.height(4)
|
||||||
.style(Style::default().bg(background_color))
|
.style(Style::default().bg(background_color))
|
||||||
});
|
});
|
||||||
|
|
||||||
let widths = [
|
let widths = [
|
||||||
@@ -181,4 +171,14 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
|||||||
scrollbar_area,
|
scrollbar_area,
|
||||||
&mut state.scrollbar_state,
|
&mut state.scrollbar_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if state.show_popup {
|
||||||
|
let popup_area = Rect {
|
||||||
|
x: area.width / 4,
|
||||||
|
y: area.height / 3,
|
||||||
|
width: area.width / 2,
|
||||||
|
height: area.height / 3,
|
||||||
|
};
|
||||||
|
popups::new_instance::render(frame, popup_area, focused);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user