Skip to content

Commit

Permalink
Extract LoadingDialog widget
Browse files Browse the repository at this point in the history
  • Loading branch information
lusingander committed Dec 30, 2024
1 parent 557fb0c commit 9b88aa5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/ui/render.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use ratatui::{
layout::{Alignment, Constraint, Layout, Rect},
layout::{Constraint, Layout, Rect},
style::{Modifier, Stylize},
text::Line,
widgets::{Block, BorderType, Padding, Paragraph},
widgets::{Block, Padding, Paragraph},
Frame,
};

use crate::{
app::{App, Notification},
color::ColorTheme,
pages::page::Page,
ui::common::calc_centered_dialog_rect,
util,
widget::{Dialog, Header},
widget::{Header, LoadingDialog},
};

pub fn render(f: &mut Frame, app: &mut App) {
Expand Down Expand Up @@ -81,10 +79,8 @@ fn render_footer(f: &mut Frame, area: Rect, app: &App) {

fn render_loading_dialog(f: &mut Frame, app: &App) {
if app.loading() {
let loading = build_loading_dialog("Loading...", app.theme());
let area = calc_centered_dialog_rect(f.area(), 30, 5);
let dialog = Dialog::new(Box::new(loading), app.theme().bg);
f.render_widget_ref(dialog, area);
let dialog = LoadingDialog::default().theme(app.theme());
f.render_widget(dialog, f.area());
}
}

Expand Down Expand Up @@ -141,13 +137,3 @@ fn build_error_status<'a>(err: &'a str, theme: &'a ColorTheme) -> Paragraph<'a>
Paragraph::new(err.add_modifier(Modifier::BOLD).fg(theme.status_error))
.block(Block::default().padding(Padding::horizontal(2)))
}

fn build_loading_dialog<'a>(msg: &'a str, theme: &'a ColorTheme) -> Paragraph<'a> {
let text = Line::from(msg.add_modifier(Modifier::BOLD));
Paragraph::new(text).alignment(Alignment::Center).block(
Block::bordered()
.border_type(BorderType::Rounded)
.padding(Padding::vertical(1))
.fg(theme.fg),
)
}
2 changes: 2 additions & 0 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod divider;
mod header;
mod image_preview;
mod input_dialog;
mod loading_dialog;
mod scroll;
mod scroll_lines;
mod scroll_list;
Expand All @@ -18,6 +19,7 @@ pub use divider::Divider;
pub use header::Header;
pub use image_preview::{ImagePicker, ImagePreview, ImagePreviewState};
pub use input_dialog::{InputDialog, InputDialogState};
pub use loading_dialog::LoadingDialog;
pub use scroll::ScrollBar;
pub use scroll_lines::{ScrollLines, ScrollLinesOptions, ScrollLinesState};
pub use scroll_list::{ScrollList, ScrollListState};
Expand Down
59 changes: 59 additions & 0 deletions src/widget/loading_dialog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use ratatui::{
buffer::Buffer,
layout::{Alignment, Rect},
style::{Color, Modifier, Stylize},
text::Line,
widgets::{Block, BorderType, Padding, Paragraph, Widget, WidgetRef},
};

use crate::{color::ColorTheme, ui::common::calc_centered_dialog_rect, widget::Dialog};

#[derive(Debug, Default)]
struct LoadingDialogColor {
bg: Color,
block: Color,
text: Color,
}

impl LoadingDialogColor {
fn new(theme: &ColorTheme) -> Self {
LoadingDialogColor {
bg: theme.bg,
block: theme.fg,
text: theme.fg,
}
}
}

#[derive(Debug, Default)]
pub struct LoadingDialog {
color: LoadingDialogColor,
}

impl LoadingDialog {
pub fn theme(mut self, theme: &ColorTheme) -> Self {
self.color = LoadingDialogColor::new(theme);
self
}
}

impl Widget for LoadingDialog {
fn render(self, area: Rect, buf: &mut Buffer) {
let area = calc_centered_dialog_rect(area, 30, 5);

let text = Line::from(Self::MSG.fg(self.color.text).add_modifier(Modifier::BOLD));
let paragraph = Paragraph::new(text).alignment(Alignment::Center).block(
Block::bordered()
.border_type(BorderType::Rounded)
.padding(Padding::vertical(1))
.fg(self.color.block),
);

let dialog = Dialog::new(Box::new(paragraph), self.color.bg);
dialog.render_ref(area, buf);
}
}

impl LoadingDialog {
const MSG: &'static str = "Loading...";
}

0 comments on commit 9b88aa5

Please sign in to comment.