-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
163 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,109 @@ | ||
use ratatui::{ | ||
layout::Alignment, | ||
style::{Color, Style}, | ||
widgets::{Block, BorderType, Borders, Paragraph}, | ||
}; | ||
|
||
use crate::{frontend::app::App, frontend::tui::Frame}; | ||
|
||
pub fn render(app: &mut App, f: &mut Frame) { | ||
f.render_widget( | ||
Paragraph::new(format!( | ||
" | ||
Press `Esc`, `Ctrl-C` or `q` to stop running.\n\ | ||
Press `j` and `k` to increment and decrement the counter respectively.\n\ | ||
Counter: {} | ||
", | ||
app.counter | ||
)) | ||
.block( | ||
Block::default() | ||
.title("Counter App") | ||
.title_alignment(Alignment::Center) | ||
.borders(Borders::ALL) | ||
.border_type(BorderType::Rounded), | ||
) | ||
.style(Style::default().fg(Color::Yellow)) | ||
.alignment(Alignment::Center), | ||
f.size(), | ||
) | ||
} | ||
use ratatui::layout::{Constraint, Direction, Layout, Rect}; | ||
use ratatui::prelude::Stylize; | ||
use ratatui::style::Modifier; | ||
use ratatui::text::Line; | ||
use ratatui::widgets::{Clear, ListItem}; | ||
use ratatui::{ | ||
layout::Alignment, | ||
style::{Color, Style}, | ||
widgets::{Block, BorderType, Borders, Paragraph}, | ||
}; | ||
|
||
use crate::{frontend::app::App, frontend::tui::Frame}; | ||
|
||
pub fn render(app: &mut App, f: &mut Frame) { | ||
let chunks = Layout::default() | ||
.direction(Direction::Horizontal) | ||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) | ||
.split(f.size()); | ||
|
||
// Iterate through all elements in the `items` app and append some debug text to it. | ||
let items: Vec<ListItem> = app | ||
.items | ||
.items | ||
.iter() | ||
.map(|i| { | ||
let mut lines = vec![Line::from(i.0)]; | ||
for _ in 0..i.1 { | ||
lines.push( | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit." | ||
.italic() | ||
.into(), | ||
); | ||
} | ||
ListItem::new(lines).style(Style::default().fg(Color::Black).bg(Color::White)) | ||
}) | ||
.collect(); | ||
|
||
let items = ratatui::widgets::List::new(items) | ||
.block(Block::default().borders(Borders::ALL).title("List")) | ||
.highlight_style( | ||
Style::default() | ||
.bg(Color::LightGreen) | ||
.add_modifier(Modifier::BOLD), | ||
) | ||
.highlight_symbol(">> "); | ||
|
||
f.render_stateful_widget(items, chunks[0], &mut app.items.state); | ||
f.render_widget( | ||
Paragraph::new("Hello World!").block( | ||
Block::default() | ||
.borders(Borders::ALL) | ||
.border_type(BorderType::Rounded), | ||
), | ||
chunks[1], | ||
); | ||
|
||
dialog(app, f); | ||
} | ||
|
||
fn dialog(app: &mut App, f: &mut Frame) { | ||
let dialog_rect = centered_rect(f.size(), 50, 50); | ||
f.render_widget(Clear, dialog_rect); | ||
f.render_widget( | ||
Paragraph::new(format!( | ||
" | ||
Press `Esc`, `Ctrl-C` or `q` to stop running.\n\ | ||
Press `j` and `k` to increment and decrement the counter respectively.\n\ | ||
Counter: {} | ||
", | ||
app.counter | ||
)) | ||
.block( | ||
Block::default() | ||
.title("Counter App") | ||
.title_alignment(Alignment::Center) | ||
.borders(Borders::ALL) | ||
.border_type(BorderType::Rounded), | ||
) | ||
.style(Style::default().fg(Color::Yellow)) | ||
.alignment(Alignment::Center), | ||
dialog_rect, | ||
) | ||
} | ||
|
||
fn centered_rect(r: Rect, percent_x: u16, percent_y: u16) -> Rect { | ||
let popup_layout = Layout::default() | ||
.direction(Direction::Vertical) | ||
.constraints( | ||
[ | ||
Constraint::Percentage((100 - percent_y) / 2), | ||
Constraint::Percentage(percent_y), | ||
Constraint::Percentage((100 - percent_y) / 2), | ||
] | ||
.as_ref(), | ||
) | ||
.split(r); | ||
|
||
Layout::default() | ||
.direction(Direction::Horizontal) | ||
.constraints( | ||
[ | ||
Constraint::Percentage((100 - percent_x) / 2), | ||
Constraint::Percentage(percent_x), | ||
Constraint::Percentage((100 - percent_x) / 2), | ||
] | ||
.as_ref(), | ||
) | ||
.split(popup_layout[1])[1] | ||
} |