diff --git a/src/ui/render.rs b/src/ui/render.rs index de27bb0..f46ae50 100644 --- a/src/ui/render.rs +++ b/src/ui/render.rs @@ -1,8 +1,7 @@ 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, }; @@ -10,9 +9,8 @@ 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) { @@ -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()); } } @@ -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), - ) -} diff --git a/src/widget.rs b/src/widget.rs index 5c3af99..8ca3f6c 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -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; @@ -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}; diff --git a/src/widget/loading_dialog.rs b/src/widget/loading_dialog.rs new file mode 100644 index 0000000..3d0a7d2 --- /dev/null +++ b/src/widget/loading_dialog.rs @@ -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..."; +}