Skip to content

Commit

Permalink
Move render functions to app
Browse files Browse the repository at this point in the history
  • Loading branch information
lusingander committed Dec 31, 2024
1 parent 5605d54 commit 0146c04
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 72 deletions.
66 changes: 66 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use ratatui::{
layout::{Constraint, Layout, Rect},
style::Stylize,
widgets::Block,
Frame,
};
use std::{path::PathBuf, rc::Rc, sync::Arc};
use tokio::spawn;

Expand All @@ -16,6 +22,7 @@ use crate::{
file::{copy_to_clipboard, save_binary, save_error_log},
object::{AppObjects, FileDetail, ObjectItem, RawObject},
pages::page::{Page, PageStack},
widget::{Header, LoadingDialog, Status, StatusType},
};

#[derive(Debug)]
Expand Down Expand Up @@ -739,3 +746,62 @@ impl App {
(self.client.as_ref().unwrap().clone(), self.tx.clone())
}
}

impl App {
pub fn render(&mut self, f: &mut Frame) {
let chunks = Layout::vertical([
Constraint::Length(self.header_height()),
Constraint::Min(0),
Constraint::Length(2),
])
.split(f.area());

self.render_background(f, f.area());
self.render_header(f, chunks[0]);
self.render_content(f, chunks[1]);
self.render_footer(f, chunks[2]);
self.render_loading_dialog(f);
}

fn header_height(&self) -> u16 {
match self.page_stack.current_page() {
Page::Help(_) => 0, // Hide header
_ => 3,
}
}

fn render_background(&self, f: &mut Frame, area: Rect) {
let block = Block::default().bg(self.theme().bg);
f.render_widget(block, area);
}

fn render_header(&self, f: &mut Frame, area: Rect) {
if !area.is_empty() {
let header = Header::new(self.breadcrumb()).theme(self.theme());
f.render_widget(header, area);
}
}

fn render_content(&mut self, f: &mut Frame, area: Rect) {
self.page_stack.current_page_mut().render(f, area);
}

fn render_footer(&self, f: &mut Frame, area: Rect) {
let status_type = match self.current_notification() {
Notification::Info(msg) => StatusType::Info(msg.into()),
Notification::Success(msg) => StatusType::Success(msg.into()),
Notification::Warn(msg) => StatusType::Warn(msg.into()),
Notification::Error(msg) => StatusType::Error(msg.into()),
Notification::None => StatusType::Help(self.page_stack.current_page().short_helps()),
};
let status = Status::new(status_type).theme(self.theme());
f.render_widget(status, area);
}

fn render_loading_dialog(&self, f: &mut Frame) {
if self.loading() {
let dialog = LoadingDialog::default().theme(self.theme());
f.render_widget(dialog, f.area());
}
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod format;
mod macros;
mod object;
mod pages;
mod render;
mod run;
mod util;
mod widget;
Expand Down
69 changes: 0 additions & 69 deletions src/render.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
app::{App, Notification},
event::{AppEventType, Receiver},
pages::page::Page,
render::render,
};

pub async fn run<B: Backend>(
Expand All @@ -15,7 +14,7 @@ pub async fn run<B: Backend>(
rx: Receiver,
) -> Result<()> {
loop {
terminal.draw(|f| render(f, app))?;
terminal.draw(|f| app.render(f))?;

let event = rx.recv();
tracing::debug!("event received: {:?}", event);
Expand Down

0 comments on commit 0146c04

Please sign in to comment.